Implement proper movement and camera controls
authorLuke Lau <luke_lau@icloud.com>
Mon, 8 Oct 2018 12:46:20 +0000 (13:46 +0100)
committerLuke Lau <luke_lau@icloud.com>
Mon, 8 Oct 2018 12:46:20 +0000 (13:46 +0100)
Also rename assignment-1 to main

CMakeLists.txt
main.cpp [moved from assignment1.cpp with 87% similarity]

index d228fa0..2e6b60c 100644 (file)
@@ -1,26 +1,26 @@
 cmake_minimum_required(VERSION 2.6)
-project(assignment-1)
+project(opengl)
 set(CMAKE_BINARY_DIR "bin")
 set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
-add_executable(assignment-1 assignment1.cpp)
+add_executable(main main.cpp)
 
 find_package(OpenGL REQUIRED)
 include_directories(${OPENGL_INCLUDE_DIRS})
-target_link_libraries(assignment-1 ${OPENGL_LIBRARIES})
+target_link_libraries(main ${OPENGL_LIBRARIES})
 
 find_package(GLUT REQUIRED)
 if(NOT GLUT_FOUND)
        message(ERROR "GLUT not found")
 endif(NOT GLUT_FOUND)
 include_directories(${GLUT_INCLUDE_DIR})
-target_link_libraries(assignment-1 ${GLUT_LIBRARIES})
+target_link_libraries(main ${GLUT_LIBRARIES})
 
 find_package(GLEW REQUIRED)
 if(NOT GLEW_FOUND)
        message(ERROR "GLEW not found")
 endif(NOT GLEW_FOUND)
 include_directories(${GLEW_INCLUDE_DIR})
-target_link_libraries(assignment-1 ${GLEW_LIBRARIES})
+target_link_libraries(main ${GLEW_LIBRARIES})
 
 find_package(GLM REQUIRED)
 if(NOT GLM_FOUND)
similarity index 87%
rename from assignment1.cpp
rename to main.cpp
index 4676882..503f469 100644 (file)
+++ b/main.cpp
@@ -34,7 +34,7 @@ void display() {
        for (int i = 0; i < 100; i++) {
 
                GLuint projId = glGetUniformLocation(progId, "projection");
-               glm::mat4 proj = glm::perspective(glm::radians(45.f), 1.33f, 0.01f, 100.f);
+               glm::mat4 proj = glm::perspective(glm::radians(45.f), 1.33f, 0.01f, 10000.f);
                glUniformMatrix4fv(projId, 1, GL_FALSE, glm::value_ptr(proj));
 
                GLuint viewId = glGetUniformLocation(progId, "view");
@@ -45,7 +45,7 @@ void display() {
                float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
                glm::mat4 model = glm::mat4(1.f);
 
-               model = glm::translate(model, glm::vec3(sin(i * 30) * 2, cos(i * 30) * 2, i * 2));
+               model = glm::translate(model, glm::vec3(sin(i * 30) * 10, cos(i * 30) * 10, i * 2));
                
                if (doRotate) {
                        model = glm::rotate(model, d * glm::radians(30.f), glm::vec3(0.f, 1.f, 0.f));
@@ -53,10 +53,10 @@ void display() {
                }
                
                if (doScale)
-                       model = glm::scale(model, glm::vec3(1.f, 0.3f + 0.7f * (1 + sin(d * (i + 3))), 1.f));
+                       model = glm::scale(model, glm::vec3(1.f, 0.7f + 0.7f * (1 + sin(d + (i + 3))), 1.f));
 
                if (doTranslate)
-                       model = glm::translate(model, glm::vec3(sin(d), cos(d), sin(d)));
+                       model = glm::translate(model, glm::vec3(sin(d + (i + 1)), cos(d + (i + -3)), sin(d + (i + 4))));
        
 
                glUniformMatrix4fv(modelId, 1, GL_FALSE, glm::value_ptr(model));
@@ -207,29 +207,40 @@ void init() {
        glEnable(GL_DEPTH_TEST); 
 }
 
+bool* keyStates = new bool[256];
+
 void keyboard(unsigned char key, int x, int y) {
-       if (key == 'w')
-               camPos.z += 0.1f;
-       if (key == 's')
-               camPos.z -= 0.1f;
-       if (key == 'a')
-               camPos.x += 0.1f;
-       if (key == 'd')
-               camPos.x -= 0.1f;
-       if (key == 'q')
-               camPos.y += 0.1f;
-       if (key == 'e')
-               camPos.y -= 0.1f;
+       keyStates[key] = true;
        if (key == 'z')
                doScale = !doScale;
        if (key == 'x')
                doRotate = !doRotate;
        if (key == 'c')
                doTranslate = !doTranslate;
-       glutPostRedisplay();
+}
+
+void keyboardUp(unsigned char key, int x, int y) {
+       keyStates[key] = false;
 }
 
 void idle(int _) {
+       float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f;
+       if (keyStates['w'])
+               zSpeed = 0.1f;
+       if (keyStates['s'])
+               zSpeed = -0.1f;
+       if (keyStates['a'])
+               xSpeed = 0.1f;
+       if (keyStates['d'])
+               xSpeed = -0.1f;
+       if (keyStates['q'])
+               ySpeed = 0.1f;
+       if (keyStates['e'])
+               ySpeed = -0.1f;
+
+       camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw);
+       camPos.y += ySpeed;
+       camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw);
        glutPostRedisplay();
        glutTimerFunc(16, idle, 0);
 }
@@ -276,6 +287,7 @@ int main(int argc, char** argv) {
        init();
 
        glutKeyboardFunc(keyboard);
+       glutKeyboardUpFunc(keyboardUp);
        glutTimerFunc(16, idle, 0);
        glutMotionFunc(motion);
        glutMouseFunc(mouse);