source: opengl-game/new-game.cpp@ 7ee66ea

feature/imgui-sdl points-test
Last change on this file since 7ee66ea was 7ee66ea, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 7 years ago

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

  • Property mode set to 100644
File size: 7.8 KB
Line 
1// NEXT STEP; Modify the vertex shader
2
3#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>
8
9#include <GL/glew.h>
10#include <GLFW/glfw3.h>
11
12#include <cstdio>
13#include <iostream>
14#include <fstream>
15
16#define _USE_MATH_DEFINES
17#include <cmath>
18
19using namespace std;
20using namespace glm;
21
22#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
23
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
32int main(int argc, char* argv[]) {
33 cout << "New OpenGL Game" << endl;
34
35 if (!restart_gl_log()) {}
36 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
37
38 glfwSetErrorCallback(glfw_error_callback);
39 if (!glfwInit()) {
40 fprintf(stderr, "ERROR: could not start GLFW3\n");
41 return 1;
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
50
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
71 if (!window) {
72 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
73 glfwTerminate();
74 return 1;
75 }
76 glfwMakeContextCurrent(window);
77 glewExperimental = GL_TRUE;
78 glewInit();
79
80 // glViewport(0, 0, width*2, height*2);
81
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);
86
87 glEnable(GL_DEPTH_TEST);
88 glDepthFunc(GL_LESS);
89
90 glEnable(GL_CULL_FACE);
91 // glCullFace(GL_BACK);
92 // glFrontFace(GL_CW);
93
94 GLfloat points[] = {
95 0.0f, 0.5f, 0.0f,
96 -0.5f, -0.5f, 0.0f,
97 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,
101 };
102
103 GLfloat colors[] = {
104 1.0, 0.0, 0.0,
105 0.0, 0.0, 1.0,
106 0.0, 1.0, 0.0,
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[] = {
113 1.0f, 0.0f, 0.0f, 0.0f, // column 1
114 0.0f, 1.0f, 0.0f, 0.0f, // column 2
115 0.0f, 0.0f, 1.0f, 0.0f, // column 3
116 0.5f, 0.0f, 0.0f, 1.0f, // column 4
117 };
118
119 GLuint points_vbo = 0;
120 glGenBuffers(1, &points_vbo);
121 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
122 glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
123
124 GLuint colors_vbo = 0;
125 glGenBuffers(1, &colors_vbo);
126 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
127 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
128
129 GLuint vao = 0;
130 glGenVertexArrays(1, &vao);
131 glBindVertexArray(vao);
132 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
133 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
134 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
135 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
136
137 glEnableVertexAttribArray(0);
138 glEnableVertexAttribArray(1);
139
140 GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
141 GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
142
143 GLuint shader_program = glCreateProgram();
144 glAttachShader(shader_program, vs);
145 glAttachShader(shader_program, fs);
146
147 glLinkProgram(shader_program);
148
149 float speed = 1.0f;
150 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;
190
191 double previous_seconds = glfwGetTime();
192 while (!glfwWindowShouldClose(window)) {
193 double current_seconds = glfwGetTime();
194 double elapsed_seconds = current_seconds - previous_seconds;
195 previous_seconds = current_seconds;
196
197 if (fabs(last_position) > 1.0f) {
198 speed = -speed;
199 }
200
201 /*
202 model[12] = last_position + speed*elapsed_seconds;
203 last_position = model[12];
204 */
205
206 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
207 glBindVertexArray(vao);
208
209 // Each point is made of 3 floats
210 int numPoints = (sizeof(points) / sizeof(float)) / 3;
211 glDrawArrays(GL_TRIANGLES, 0, numPoints);
212
213 glfwPollEvents();
214 glfwSwapBuffers(window);
215
216 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
217 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;
256 }
257 }
258
259 glfwTerminate();
260 return 0;
261}
262
263GLuint loadShader(GLenum type, string file) {
264 cout << "Loading shader from file " << file << endl;
265
266 ifstream shaderFile(file);
267 GLuint shaderId = 0;
268
269 if (shaderFile.is_open()) {
270 string line, shaderString;
271
272 while(getline(shaderFile, line)) {
273 shaderString += line + "\n";
274 }
275 shaderFile.close();
276 const char* shaderCString = shaderString.c_str();
277
278 shaderId = glCreateShader(type);
279 glShaderSource(shaderId, 1, &shaderCString, NULL);
280 glCompileShader(shaderId);
281
282 cout << "Loaded successfully" << endl;
283 } else {
284 cout << "Failed to loade the file" << endl;
285 }
286
287 return shaderId;
288}
Note: See TracBrowser for help on using the repository browser.