From 532483c6f9838a8c3e5e5c81b0178425f6bd1b54 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 27 Oct 2023 00:58:50 -0500 Subject: [PATCH] Orbiting via 3D Transformations --- Models/main.cpp | 70 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/Models/main.cpp b/Models/main.cpp index 6f33d3c..4c155b2 100644 --- a/Models/main.cpp +++ b/Models/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #define numVAOs 1 #define numVBOs 2 @@ -16,11 +17,11 @@ GLuint vao[numVAOs]; GLuint vbo[numVBOs]; glm::vec3 camera; -glm::vec3 cubeLoc,pyramidLoc; GLuint mvLoc,projLoc; int width, height; float aspect; glm::mat4 mMat, pMat, vMat, mvMat; +std::stacktransforms; void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed at origin float cubePositions[108] = { @@ -58,14 +59,27 @@ void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed void init(GLFWwindow* window) { renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl"); - camera={0.0f,0.0f,-8.0f}; - cubeLoc={0.0f,-2.0f,0.0f}; - pyramidLoc={4.0f,4.0f,-4.0f}; + camera={0.0f,0.0f,-12.0f}; setupVertices(); } double lastTime=0; +void DrawCube(){ + glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mMat)); + glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); + glBindBuffer(GL_ARRAY_BUFFER,vbo[0]); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); + glDrawArrays(GL_TRIANGLES,0,36); +} +void DrawSquarePyramid(){ + glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mMat)); + glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); + glBindBuffer(GL_ARRAY_BUFFER,vbo[1]); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); + glDrawArrays(GL_TRIANGLES,0,18); +} + void display(GLFWwindow* window, double currentTime) { double elapsedTime=currentTime-lastTime; lastTime=currentTime; @@ -77,6 +91,7 @@ void display(GLFWwindow* window, double currentTime) { glUseProgram(renderingProgram); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); + glEnableVertexAttribArray(0); mvLoc=glGetUniformLocation(renderingProgram,"mv_matrix"); projLoc=glGetUniformLocation(renderingProgram,"proj_matrix"); @@ -86,24 +101,37 @@ void display(GLFWwindow* window, double currentTime) { pMat=glm::perspective(utils::degToRad(60),aspect,0.1f,1000.f); vMat=glm::translate(glm::mat4(1.0f),glm::vec3{camera.x,camera.y,camera.z}); - mMat=glm::translate(glm::mat4(1.0f),cubeLoc); - mvMat=vMat*mMat; - - glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat)); - glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); + transforms.push(vMat); - glBindBuffer(GL_ARRAY_BUFFER,vbo[0]); - glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); - glEnableVertexAttribArray(0); - glDrawArrays(GL_TRIANGLES,0,36); - - mMat=glm::translate(glm::mat4(1.0f),pyramidLoc); - mvMat=vMat*mMat; - glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat)); - glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); - glBindBuffer(GL_ARRAY_BUFFER,vbo[1]); - glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); - glDrawArrays(GL_TRIANGLES,0,18); + mMat=glm::translate(transforms.top(),{0.f,0.f,0.f}); + transforms.push(mMat); + mMat=glm::rotate(mMat,float(utils::degToRad(90)*currentTime),{0.f,1.f,0.f}); + mMat=glm::rotate(mMat,float(utils::degToRad(12)),{0.f,0.f,1.f}); + DrawSquarePyramid(); + mMat=glm::translate(transforms.top(),{0.f,0.f,0.f}); + mMat=glm::rotate(mMat,float(utils::degToRad(90)*currentTime),{0.f,1.f,0.f}); + mMat=glm::rotate(mMat,float(utils::degToRad(192)),{0.f,0.f,1.f}); + mMat=glm::translate(mMat,{0.f,2.f,0.f}); + mMat=glm::rotate(mMat,float(utils::degToRad(270)),{0.f,1.f,0.f}); + DrawSquarePyramid(); + + mMat=glm::translate(transforms.top(),{sin(currentTime*0.5)*6,sin(currentTime*0.5)*0.8-1,cos(currentTime*0.5)*6}); + transforms.push(mMat); + mMat=glm::rotate(mMat,float(utils::degToRad(24)-28),{0,0,1}); + mMat=glm::rotate(mMat,float(utils::degToRad(24)*currentTime+28),{0,1,0}); + DrawCube(); + + mMat=glm::rotate(transforms.top(),utils::degToRad(90),{1,0,0}); + mMat=glm::translate(mMat,{sin(currentTime*3)*3,0,cos(currentTime*3)*3}); + mMat=glm::scale(mMat,{0.3,0.3,0.3}); + mMat=glm::rotate(mMat,float(-utils::degToRad(220)*currentTime+4),{0,1,0}); + DrawCube(); + + mMat=glm::rotate(transforms.top(),utils::degToRad(90),{1,1,0}); + mMat=glm::translate(mMat,{sin(currentTime*3)*3,0,cos(currentTime*3)*3}); + mMat=glm::scale(mMat,{0.3,0.3,0.3}); + mMat=glm::rotate(mMat,float(utils::degToRad(120)*currentTime),{0,1,0}); + DrawCube(); } int main(void) {