Changeset 7ee66ea in opengl-game for new-game.cpp


Ignore:
Timestamp:
Aug 17, 2017, 2:30:31 AM (7 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
201e2f8
Parents:
9d22ee9
Message:

Add view and projection matrices allow the player to move and rotate the camera

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r9d22ee9 r7ee66ea  
    22
    33#include "logger.h"
     4
     5#include <glm/mat4x4.hpp> // glm::mat4
     6#include <glm/gtc/matrix_transform.hpp>
     7#include <glm/gtc/type_ptr.hpp>
    48
    59#include <GL/glew.h>
     
    913#include <iostream>
    1014#include <fstream>
     15
     16#define _USE_MATH_DEFINES
    1117#include <cmath>
    1218
    1319using namespace std;
     20using namespace glm;
     21
     22#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
    1423
    1524GLuint loadShader(GLenum type, string file);
     
    8796     -0.5f, -0.5f,  0.0f,
    8897      0.5f, -0.5f,  0.0f,
     98      0.5f, -0.5f,  0.0f,
     99     -0.5f, -0.5f,  0.0f,
     100      0.0f,  0.5f,  0.0f,
    89101   };
    90102
     
    93105     0.0, 0.0, 1.0,
    94106     0.0, 1.0, 0.0,
    95    };
    96 
    97    GLfloat model[] = {
     107     0.0, 1.0, 0.0,
     108     0.0, 0.0, 1.0,
     109     1.0, 0.0, 0.0,
     110   };
     111
     112   GLfloat model_mat[] = {
    98113     1.0f, 0.0f, 0.0f, 0.0f, // column 1
    99114     0.0f, 1.0f, 0.0f, 0.0f, // column 2
     
    132147   glLinkProgram(shader_program);
    133148
    134    GLint location = glGetUniformLocation(shader_program, "model");
    135 
    136149   float speed = 1.0f;
    137150   float last_position = 0.0f;
     151
     152   float cam_speed = 1.0f;
     153   float cam_yaw_speed = 30.0f*ONE_DEG_IN_RAD;
     154
     155   float cam_pos[] = {0.0f, 0.0f, 2.0f};
     156   float cam_yaw = 0.0f;
     157
     158   mat4 T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
     159   mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
     160   mat4 view_mat = R*T;
     161
     162   float near = 0.1f;
     163   float far = 100.0f;
     164   float fov = 67.0f * ONE_DEG_IN_RAD;
     165   float aspect = (float)width / (float)height;
     166
     167   float range = tan(fov * 0.5f) * near;
     168   float Sx = near / (range * aspect);
     169   float Sy = near / range;
     170   float Sz = -(far + near) / (far - near);
     171   float Pz = -(2.0f * far * near) / (far - near);
     172
     173   float proj_mat[] = {
     174     Sx, 0.0f, 0.0f, 0.0f,
     175     0.0f, Sy, 0.0f, 0.0f,
     176     0.0f, 0.0f, Sz, -1.0f,
     177     0.0f, 0.0f, Pz, 0.0f,
     178   };
     179
     180   GLint model_mat_loc = glGetUniformLocation(shader_program, "model");
     181   GLint view_mat_loc = glGetUniformLocation(shader_program, "view");
     182   GLint proj_mat_loc = glGetUniformLocation(shader_program, "proj");
     183
     184   glUseProgram(shader_program);
     185   glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, model_mat);
     186   glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
     187   glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, proj_mat);
     188
     189   bool cam_moved = false;
    138190
    139191   double previous_seconds = glfwGetTime();
     
    147199      }
    148200
     201      /*
    149202      model[12] = last_position + speed*elapsed_seconds;
    150203      last_position = model[12];
    151       glUseProgram(shader_program);
    152       glUniformMatrix4fv(location, 1, GL_FALSE, model);
     204      */
    153205
    154206      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    155207      glBindVertexArray(vao);
    156208
    157       glDrawArrays(GL_TRIANGLES, 0, 3);
     209      // Each point is made of 3 floats
     210      int numPoints = (sizeof(points) / sizeof(float)) / 3;
     211      glDrawArrays(GL_TRIANGLES, 0, numPoints);
    158212
    159213      glfwPollEvents();
     
    162216      if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
    163217         glfwSetWindowShouldClose(window, 1);
     218      }
     219
     220      float dist = cam_speed * elapsed_seconds;
     221      if (glfwGetKey(window, GLFW_KEY_A)) {
     222         cam_pos[0] -= cos(cam_yaw*ONE_DEG_IN_RAD)*dist;
     223         cam_pos[2] += sin(cam_yaw*ONE_DEG_IN_RAD)*dist;
     224         cam_moved = true;
     225      }
     226      if (glfwGetKey(window, GLFW_KEY_D)) {
     227         cam_pos[0] += cos(cam_yaw)*dist;
     228         cam_pos[2] -= sin(cam_yaw)*dist;
     229         cam_moved = true;
     230      }
     231      if (glfwGetKey(window, GLFW_KEY_W)) {
     232         cam_pos[0] -= sin(cam_yaw)*dist;
     233         cam_pos[2] -= cos(cam_yaw)*dist;
     234         cam_moved = true;
     235      }
     236      if (glfwGetKey(window, GLFW_KEY_S)) {
     237         cam_pos[0] += sin(cam_yaw)*dist;
     238         cam_pos[2] += cos(cam_yaw)*dist;
     239         cam_moved = true;
     240      }
     241      if (glfwGetKey(window, GLFW_KEY_LEFT)) {
     242         cam_yaw += cam_yaw_speed * elapsed_seconds;
     243         cam_moved = true;
     244      }
     245      if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
     246         cam_yaw -= cam_yaw_speed * elapsed_seconds;
     247         cam_moved = true;
     248      }
     249      if (cam_moved) {
     250         T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
     251         R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
     252         view_mat = R*T;
     253
     254         glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
     255         cam_moved = false;
    164256      }
    165257   }
Note: See TracChangeset for help on using the changeset viewer.