source: opengl-game/new-game.cpp@ 201e2f8

feature/imgui-sdl points-test
Last change on this file since 201e2f8 was 201e2f8, checked in by Dmitry Portnoy <dmp1488@…>, 7 years ago

Make the model matrix support rotations and translations

  • Property mode set to 100644
File size: 7.8 KB
RevLine 
[8b7cfcf]1// NEXT STEP; Modify the vertex shader
2
[22b2c37]3#include "logger.h"
[5272b6b]4
[7ee66ea]5#include <glm/mat4x4.hpp> // glm::mat4
6#include <glm/gtc/matrix_transform.hpp>
7#include <glm/gtc/type_ptr.hpp>
8
[5272b6b]9#include <GL/glew.h>
10#include <GLFW/glfw3.h>
11
[22b2c37]12#include <cstdio>
13#include <iostream>
[ec4456b]14#include <fstream>
[7ee66ea]15
16#define _USE_MATH_DEFINES
[93baa0e]17#include <cmath>
[22b2c37]18
[5272b6b]19using namespace std;
[7ee66ea]20using namespace glm;
21
22#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
[5272b6b]23
[ec4456b]24GLuint loadShader(GLenum type, string file);
25
26const bool FULLSCREEN = false;
27
28void glfw_error_callback(int error, const char* description) {
29 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
30}
31
[5272b6b]32int main(int argc, char* argv[]) {
33 cout << "New OpenGL Game" << endl;
34
[ec4456b]35 if (!restart_gl_log()) {}
36 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
[22b2c37]37
[ec4456b]38 glfwSetErrorCallback(glfw_error_callback);
[5272b6b]39 if (!glfwInit()) {
40 fprintf(stderr, "ERROR: could not start GLFW3\n");
41 return 1;
[be246ad]42 }
43
44#ifdef __APPLE__
45 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
46 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
47 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
48 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
49#endif
[5272b6b]50
[ec4456b]51 glfwWindowHint(GLFW_SAMPLES, 4);
52
53 GLFWwindow* window = NULL;
54
55 int width = 640;
56 int height = 480;
57
58 if (FULLSCREEN) {
59 GLFWmonitor* mon = glfwGetPrimaryMonitor();
60 const GLFWvidmode* vmode = glfwGetVideoMode(mon);
61
62 cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
63 window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
64
65 width = vmode->width;
66 height = vmode->height;
67 } else {
68 window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
69 }
70
[5272b6b]71 if (!window) {
72 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
73 glfwTerminate();
74 return 1;
75 }
[644a2e4]76 glfwMakeContextCurrent(window);
[5272b6b]77 glewExperimental = GL_TRUE;
78 glewInit();
79
[ec4456b]80 // glViewport(0, 0, width*2, height*2);
81
[5272b6b]82 const GLubyte* renderer = glGetString(GL_RENDERER);
83 const GLubyte* version = glGetString(GL_VERSION);
84 printf("Renderer: %s\n", renderer);
85 printf("OpenGL version supported %s\n", version);
[93baa0e]86
[5272b6b]87 glEnable(GL_DEPTH_TEST);
88 glDepthFunc(GL_LESS);
[516668e]89
[93baa0e]90 glEnable(GL_CULL_FACE);
91 // glCullFace(GL_BACK);
92 // glFrontFace(GL_CW);
93
[516668e]94 GLfloat points[] = {
95 0.0f, 0.5f, 0.0f,
96 -0.5f, -0.5f, 0.0f,
[93baa0e]97 0.5f, -0.5f, 0.0f,
[7ee66ea]98 0.5f, -0.5f, 0.0f,
99 -0.5f, -0.5f, 0.0f,
100 0.0f, 0.5f, 0.0f,
[516668e]101 };
102
[8b7cfcf]103 GLfloat colors[] = {
104 1.0, 0.0, 0.0,
105 0.0, 0.0, 1.0,
[93baa0e]106 0.0, 1.0, 0.0,
[7ee66ea]107 0.0, 1.0, 0.0,
108 0.0, 0.0, 1.0,
109 1.0, 0.0, 0.0,
[93baa0e]110 };
111
[201e2f8]112 mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
113 mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
114 mat4 model_mat = T_model*R_model;
[8b7cfcf]115
116 GLuint points_vbo = 0;
117 glGenBuffers(1, &points_vbo);
118 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
[516668e]119 glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
120
[8b7cfcf]121 GLuint colors_vbo = 0;
122 glGenBuffers(1, &colors_vbo);
123 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
124 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
125
[644a2e4]126 GLuint vao = 0;
[516668e]127 glGenVertexArrays(1, &vao);
128 glBindVertexArray(vao);
[8b7cfcf]129 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
[516668e]130 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
[8b7cfcf]131 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
132 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
[516668e]133
[8b7cfcf]134 glEnableVertexAttribArray(0);
135 glEnableVertexAttribArray(1);
[644a2e4]136
[ec4456b]137 GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
138 GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
[644a2e4]139
140 GLuint shader_program = glCreateProgram();
141 glAttachShader(shader_program, vs);
142 glAttachShader(shader_program, fs);
[8b7cfcf]143
[644a2e4]144 glLinkProgram(shader_program);
145
[93baa0e]146 float speed = 1.0f;
147 float last_position = 0.0f;
148
[7ee66ea]149 float cam_speed = 1.0f;
[201e2f8]150 float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
[7ee66ea]151
152 float cam_pos[] = {0.0f, 0.0f, 2.0f};
153 float cam_yaw = 0.0f;
154
155 mat4 T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
156 mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
157 mat4 view_mat = R*T;
158
159 float near = 0.1f;
160 float far = 100.0f;
161 float fov = 67.0f * ONE_DEG_IN_RAD;
162 float aspect = (float)width / (float)height;
163
164 float range = tan(fov * 0.5f) * near;
165 float Sx = near / (range * aspect);
166 float Sy = near / range;
167 float Sz = -(far + near) / (far - near);
168 float Pz = -(2.0f * far * near) / (far - near);
169
170 float proj_mat[] = {
171 Sx, 0.0f, 0.0f, 0.0f,
172 0.0f, Sy, 0.0f, 0.0f,
173 0.0f, 0.0f, Sz, -1.0f,
174 0.0f, 0.0f, Pz, 0.0f,
175 };
176
177 GLint model_mat_loc = glGetUniformLocation(shader_program, "model");
178 GLint view_mat_loc = glGetUniformLocation(shader_program, "view");
179 GLint proj_mat_loc = glGetUniformLocation(shader_program, "proj");
180
181 glUseProgram(shader_program);
[201e2f8]182 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat));
[7ee66ea]183 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
184 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, proj_mat);
185
186 bool cam_moved = false;
187
[93baa0e]188 double previous_seconds = glfwGetTime();
[644a2e4]189 while (!glfwWindowShouldClose(window)) {
[93baa0e]190 double current_seconds = glfwGetTime();
191 double elapsed_seconds = current_seconds - previous_seconds;
192 previous_seconds = current_seconds;
193
194 if (fabs(last_position) > 1.0f) {
195 speed = -speed;
196 }
197
[7ee66ea]198 /*
[93baa0e]199 model[12] = last_position + speed*elapsed_seconds;
200 last_position = model[12];
[7ee66ea]201 */
[93baa0e]202
203 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[644a2e4]204 glBindVertexArray(vao);
[93baa0e]205
[7ee66ea]206 // Each point is made of 3 floats
207 int numPoints = (sizeof(points) / sizeof(float)) / 3;
208 glDrawArrays(GL_TRIANGLES, 0, numPoints);
[ec4456b]209
[644a2e4]210 glfwPollEvents();
211 glfwSwapBuffers(window);
[ec4456b]212
213 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
214 glfwSetWindowShouldClose(window, 1);
215 }
[7ee66ea]216
217 float dist = cam_speed * elapsed_seconds;
218 if (glfwGetKey(window, GLFW_KEY_A)) {
[201e2f8]219 cam_pos[0] -= cos(cam_yaw)*dist;
220 cam_pos[2] += sin(cam_yaw)*dist;
[7ee66ea]221 cam_moved = true;
222 }
223 if (glfwGetKey(window, GLFW_KEY_D)) {
224 cam_pos[0] += cos(cam_yaw)*dist;
225 cam_pos[2] -= sin(cam_yaw)*dist;
226 cam_moved = true;
227 }
228 if (glfwGetKey(window, GLFW_KEY_W)) {
229 cam_pos[0] -= sin(cam_yaw)*dist;
230 cam_pos[2] -= cos(cam_yaw)*dist;
231 cam_moved = true;
232 }
233 if (glfwGetKey(window, GLFW_KEY_S)) {
234 cam_pos[0] += sin(cam_yaw)*dist;
235 cam_pos[2] += cos(cam_yaw)*dist;
236 cam_moved = true;
237 }
238 if (glfwGetKey(window, GLFW_KEY_LEFT)) {
239 cam_yaw += cam_yaw_speed * elapsed_seconds;
240 cam_moved = true;
241 }
242 if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
243 cam_yaw -= cam_yaw_speed * elapsed_seconds;
244 cam_moved = true;
245 }
246 if (cam_moved) {
247 T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
248 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
249 view_mat = R*T;
250
251 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
252 cam_moved = false;
253 }
[644a2e4]254 }
255
[5272b6b]256 glfwTerminate();
257 return 0;
258}
[ec4456b]259
260GLuint loadShader(GLenum type, string file) {
261 cout << "Loading shader from file " << file << endl;
262
263 ifstream shaderFile(file);
264 GLuint shaderId = 0;
265
266 if (shaderFile.is_open()) {
267 string line, shaderString;
268
269 while(getline(shaderFile, line)) {
270 shaderString += line + "\n";
271 }
272 shaderFile.close();
273 const char* shaderCString = shaderString.c_str();
274
275 shaderId = glCreateShader(type);
276 glShaderSource(shaderId, 1, &shaderCString, NULL);
277 glCompileShader(shaderId);
278
279 cout << "Loaded successfully" << endl;
280 } else {
281 cout << "Failed to loade the file" << endl;
282 }
283
284 return shaderId;
285}
Note: See TracBrowser for help on using the repository browser.