Changeset 809ce16 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Jun 10, 2018, 10:34:19 PM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
81f28c0
Parents:
5c403fe
Message:

Add the ability to tilt the camera up or down with arrow keys. Moving the camera still moves it forward, backward, left, or right based on the cmera's current orientation. Using the right or left arrow keys will pan the cemra around the world y axis, regardless of its vertical tilt.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r5c403fe r809ce16  
    475475   float cam_speed = 1.0f;
    476476   float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
     477   float cam_pitch_speed = 60.0f*ONE_DEG_IN_RAD;
    477478
    478479   // glm::lookAt can create the view matrix
     
    481482   cam_pos = vec3(0.0f, 0.0f, 2.0f);
    482483   float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
     484   float cam_pitch = 0.0f * 2.0f * 3.14159f / 360.0f;
    483485
    484486   mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
     
    636638      float dist = cam_speed * elapsed_seconds;
    637639      if (glfwGetKey(window, GLFW_KEY_A)) {
    638          cam_pos.x -= cos(cam_yaw)*dist;
    639          cam_pos.z += sin(cam_yaw)*dist;
     640         vec3 dir = (inverse(R) * vec4(-1.0f, 0.0f, 0.0f, 1.0f)).xyz();
     641         cam_pos += dir * dist;
    640642
    641643         cam_moved = true;
    642644      }
    643645      if (glfwGetKey(window, GLFW_KEY_D)) {
    644          cam_pos.x += cos(cam_yaw)*dist;
    645          cam_pos.z -= sin(cam_yaw)*dist;
     646         vec3 dir = (inverse(R) * vec4(1.0f, 0.0f, 0.0f, 1.0f)).xyz();
     647         cam_pos += dir * dist;
    646648
    647649         cam_moved = true;
    648650      }
    649651      if (glfwGetKey(window, GLFW_KEY_W)) {
    650          cam_pos.x -= sin(cam_yaw)*dist;
    651          cam_pos.z -= cos(cam_yaw)*dist;
     652         vec3 dir = (inverse(R) * vec4(0.0f, 0.0f, -1.0f, 1.0f)).xyz();
     653         cam_pos += dir * dist;
    652654
    653655         cam_moved = true;
    654656      }
    655657      if (glfwGetKey(window, GLFW_KEY_S)) {
    656          cam_pos.x += sin(cam_yaw)*dist;
    657          cam_pos.z += cos(cam_yaw)*dist;
     658         vec3 dir = (inverse(R) * vec4(0.0f, 0.0f, 1.0f, 1.0f)).xyz();
     659         cam_pos += dir * dist;
    658660
    659661         cam_moved = true;
     
    667669         cam_moved = true;
    668670      }
     671      if (glfwGetKey(window, GLFW_KEY_UP)) {
     672         cam_pitch += cam_pitch_speed * elapsed_seconds;
     673         cam_moved = true;
     674      }
     675      if (glfwGetKey(window, GLFW_KEY_DOWN)) {
     676         cam_pitch -= cam_pitch_speed * elapsed_seconds;
     677         cam_moved = true;
     678      }
    669679      if (cam_moved) {
    670680         T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
    671          R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
     681
     682         mat4 yaw_mat = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
     683         mat4 pitch_mat = rotate(mat4(), -cam_pitch, vec3(1.0f, 0.0f, 0.0f));
     684         R = pitch_mat * yaw_mat;
    672685
    673686         view_mat = R*T;
     687
     688         printVector("cam pos", cam_pos);
    674689
    675690         glUseProgram(color_sp);
Note: See TracChangeset for help on using the changeset viewer.