1 | #include "logger.h"
|
---|
2 |
|
---|
3 | #include "stb_image.h"
|
---|
4 |
|
---|
5 | #define _USE_MATH_DEFINES
|
---|
6 | #define GLM_SWIZZLE
|
---|
7 |
|
---|
8 | // This is to fix a non-alignment issue when passing vec4 params.
|
---|
9 | // Check if it got fixed in a later version of GLM
|
---|
10 | #define GLM_FORCE_PURE
|
---|
11 |
|
---|
12 | #include <glm/mat4x4.hpp>
|
---|
13 | #include <glm/gtc/matrix_transform.hpp>
|
---|
14 | #include <glm/gtc/type_ptr.hpp>
|
---|
15 |
|
---|
16 | #include <GL/glew.h>
|
---|
17 | #include <GLFW/glfw3.h>
|
---|
18 |
|
---|
19 | #include <cstdio>
|
---|
20 | #include <iostream>
|
---|
21 | #include <fstream>
|
---|
22 | #include <cmath>
|
---|
23 | #include <string>
|
---|
24 | #include <array>
|
---|
25 | #include <vector>
|
---|
26 |
|
---|
27 | using namespace std;
|
---|
28 | using namespace glm;
|
---|
29 |
|
---|
30 | #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
|
---|
31 |
|
---|
32 | /*
|
---|
33 | * If I use one array to store the points for all the object faces in the scene, I'll probably remove the ObjectFace object,
|
---|
34 | * and store the start and end indices of a given object's point coordinates in that array in the SceneObject.
|
---|
35 | *
|
---|
36 | * Should probably do something similar with colors and texture coordinates, once I figure out the best way to store tex coords
|
---|
37 | * for all objects in one array.
|
---|
38 | */
|
---|
39 |
|
---|
40 |
|
---|
41 | // might also want to store the shader to be used for the object
|
---|
42 | struct SceneObject {
|
---|
43 | mat4 model_mat;
|
---|
44 | GLuint shader_program;
|
---|
45 | GLvoid* vbo_offset;
|
---|
46 | unsigned int num_points;
|
---|
47 | };
|
---|
48 |
|
---|
49 | struct ObjectFace {
|
---|
50 | unsigned int object_id;
|
---|
51 | array<vec3, 3> points;
|
---|
52 | };
|
---|
53 |
|
---|
54 | const bool FULLSCREEN = false;
|
---|
55 | int width = 640;
|
---|
56 | int height = 480;
|
---|
57 |
|
---|
58 | vec3 cam_pos;
|
---|
59 |
|
---|
60 | mat4 view_mat;
|
---|
61 | mat4 proj_mat;
|
---|
62 |
|
---|
63 | vector<SceneObject> objects;
|
---|
64 | vector<ObjectFace> faces;
|
---|
65 |
|
---|
66 | SceneObject* clickedObject = NULL;
|
---|
67 | SceneObject* selectedObject;
|
---|
68 |
|
---|
69 | double fps;
|
---|
70 |
|
---|
71 | bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point);
|
---|
72 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
|
---|
73 |
|
---|
74 | GLuint loadShader(GLenum type, string file);
|
---|
75 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
|
---|
76 | unsigned char* loadImage(string file_name, int* x, int* y);
|
---|
77 |
|
---|
78 | void printVector(string label, vec3 v);
|
---|
79 | void print4DVector(string label, vec4 v);
|
---|
80 |
|
---|
81 | float NEAR_CLIP = 0.1f;
|
---|
82 | float FAR_CLIP = 100.0f;
|
---|
83 |
|
---|
84 | void glfw_error_callback(int error, const char* description) {
|
---|
85 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
86 | }
|
---|
87 |
|
---|
88 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
---|
89 | double mouse_x, mouse_y;
|
---|
90 | glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
---|
91 |
|
---|
92 | if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
---|
93 | cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
|
---|
94 | selectedObject = NULL;
|
---|
95 |
|
---|
96 | float x = (2.0f*mouse_x) / width - 1.0f;
|
---|
97 | float y = 1.0f - (2.0f*mouse_y) / height;
|
---|
98 |
|
---|
99 | cout << "x: " << x << ", y: " << y << endl;
|
---|
100 |
|
---|
101 | vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
|
---|
102 | vec4 ray_eye = inverse(proj_mat) * ray_clip;
|
---|
103 | ray_eye = vec4(ray_eye.xy(), -1.0f, 1.0f);
|
---|
104 | vec4 ray_world = inverse(view_mat) * ray_eye;
|
---|
105 |
|
---|
106 | vec4 cam_pos_temp = vec4(cam_pos, 1.0f);
|
---|
107 |
|
---|
108 | vec4 click_point;
|
---|
109 | vec3 closest_point = vec3(0.0f, 0.0f, -FAR_CLIP); // Any valid point will be closer than the far clipping plane, so initial value to that
|
---|
110 | int closest_face_id = -1;
|
---|
111 |
|
---|
112 | for (int i = 0; i<faces.size(); i++) {
|
---|
113 | if (faceClicked(&faces[i], ray_world, cam_pos_temp, click_point)) {
|
---|
114 | click_point = view_mat * click_point;
|
---|
115 |
|
---|
116 | if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
|
---|
117 | closest_point = click_point.xyz();
|
---|
118 | closest_face_id = i;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (closest_face_id == -1) {
|
---|
124 | cout << "No object was clicked" << endl;
|
---|
125 | } else {
|
---|
126 | clickedObject = &objects[faces[closest_face_id].object_id];
|
---|
127 | cout << "Clicked object: " << faces[closest_face_id].object_id << endl;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | int main(int argc, char* argv[]) {
|
---|
133 | cout << "New OpenGL Game" << endl;
|
---|
134 |
|
---|
135 | if (!restart_gl_log()) {}
|
---|
136 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
137 |
|
---|
138 | glfwSetErrorCallback(glfw_error_callback);
|
---|
139 | if (!glfwInit()) {
|
---|
140 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
141 | return 1;
|
---|
142 | }
|
---|
143 |
|
---|
144 | #ifdef __APPLE__
|
---|
145 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
146 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
147 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
148 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
152 |
|
---|
153 | GLFWwindow* window = NULL;
|
---|
154 | GLFWmonitor* mon = NULL;
|
---|
155 |
|
---|
156 | if (FULLSCREEN) {
|
---|
157 | mon = glfwGetPrimaryMonitor();
|
---|
158 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
159 |
|
---|
160 | width = vmode->width;
|
---|
161 | height = vmode->height;
|
---|
162 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
163 | }
|
---|
164 | window = glfwCreateWindow(width, height, "New OpenGL Game", mon, NULL);
|
---|
165 |
|
---|
166 | if (!window) {
|
---|
167 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
168 | glfwTerminate();
|
---|
169 | return 1;
|
---|
170 | }
|
---|
171 |
|
---|
172 | glfwSetMouseButtonCallback(window, mouse_button_callback);
|
---|
173 |
|
---|
174 | glfwMakeContextCurrent(window);
|
---|
175 | glewExperimental = GL_TRUE;
|
---|
176 | glewInit();
|
---|
177 |
|
---|
178 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
179 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
180 | printf("Renderer: %s\n", renderer);
|
---|
181 | printf("OpenGL version supported %s\n", version);
|
---|
182 |
|
---|
183 | glEnable(GL_DEPTH_TEST);
|
---|
184 | glDepthFunc(GL_LESS);
|
---|
185 |
|
---|
186 | glEnable(GL_CULL_FACE);
|
---|
187 | // glCullFace(GL_BACK);
|
---|
188 | // glFrontFace(GL_CW);
|
---|
189 |
|
---|
190 | int x, y;
|
---|
191 | unsigned char* texImage = loadImage("test.png", &x, &y);
|
---|
192 | if (texImage) {
|
---|
193 | cout << "Yay, I loaded an image!" << endl;
|
---|
194 | cout << x << endl;
|
---|
195 | cout << y << endl;
|
---|
196 | printf("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
|
---|
197 | }
|
---|
198 |
|
---|
199 | GLuint tex = 0;
|
---|
200 | glGenTextures(1, &tex);
|
---|
201 | glActiveTexture(GL_TEXTURE0);
|
---|
202 | glBindTexture(GL_TEXTURE_2D, tex);
|
---|
203 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
|
---|
204 |
|
---|
205 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
206 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
207 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
208 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
209 |
|
---|
210 | GLfloat points[] = {
|
---|
211 | 0.0f, 0.5f, 0.0f,
|
---|
212 | -0.5f, -0.5f, 0.0f,
|
---|
213 | 0.5f, -0.5f, 0.0f,
|
---|
214 | 0.5f, -0.5f, 0.0f,
|
---|
215 | -0.5f, -0.5f, 0.0f,
|
---|
216 | 0.0f, 0.5f, 0.0f,
|
---|
217 | };
|
---|
218 |
|
---|
219 | GLfloat colors[] = {
|
---|
220 | 1.0, 0.0, 0.0,
|
---|
221 | 0.0, 0.0, 1.0,
|
---|
222 | 0.0, 1.0, 0.0,
|
---|
223 | 0.0, 1.0, 0.0,
|
---|
224 | 0.0, 0.0, 1.0,
|
---|
225 | 1.0, 0.0, 0.0,
|
---|
226 | };
|
---|
227 |
|
---|
228 | GLfloat colors_new[] = {
|
---|
229 | 0.0, 1.0, 0.0,
|
---|
230 | 0.0, 1.0, 0.0,
|
---|
231 | 0.0, 1.0, 0.0,
|
---|
232 | 0.0, 1.0, 0.0,
|
---|
233 | 0.0, 1.0, 0.0,
|
---|
234 | 0.0, 1.0, 0.0,
|
---|
235 | };
|
---|
236 |
|
---|
237 | GLfloat points2[] = {
|
---|
238 | 0.5f, 0.5f, 0.0f,
|
---|
239 | -0.5f, 0.5f, 0.0f,
|
---|
240 | -0.5f, -0.5f, 0.0f,
|
---|
241 | 0.5f, 0.5f, 0.0f,
|
---|
242 | -0.5f, -0.5f, 0.0f,
|
---|
243 | 0.5f, -0.5f, 0.0f,
|
---|
244 | };
|
---|
245 |
|
---|
246 | GLfloat colors2[] = {
|
---|
247 | 0.0, 0.9, 0.9,
|
---|
248 | 0.0, 0.9, 0.9,
|
---|
249 | 0.0, 0.9, 0.9,
|
---|
250 | 0.0, 0.9, 0.9,
|
---|
251 | 0.0, 0.9, 0.9,
|
---|
252 | 0.0, 0.9, 0.9,
|
---|
253 | };
|
---|
254 |
|
---|
255 | GLfloat texcoords[] = {
|
---|
256 | 1.0f, 1.0f,
|
---|
257 | 0.0f, 1.0f,
|
---|
258 | 0.0, 0.0,
|
---|
259 | 1.0, 1.0,
|
---|
260 | 0.0, 0.0,
|
---|
261 | 1.0, 0.0
|
---|
262 | };
|
---|
263 |
|
---|
264 | mat4 T_model, R_model;
|
---|
265 |
|
---|
266 | // triangle
|
---|
267 | objects.push_back(SceneObject());
|
---|
268 | objects[0].shader_program = 0;
|
---|
269 | objects[0].vbo_offset = (GLvoid*) (0 * sizeof(float) * 3);
|
---|
270 | objects[0].num_points = 6;
|
---|
271 |
|
---|
272 | T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f));
|
---|
273 | R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
|
---|
274 | objects[0].model_mat = T_model*R_model;
|
---|
275 |
|
---|
276 | faces.push_back(ObjectFace());
|
---|
277 | faces[0].object_id = 0;
|
---|
278 | faces[0].points = {
|
---|
279 | vec3(points[0], points[1], points[2]),
|
---|
280 | vec3(points[3], points[4], points[5]),
|
---|
281 | vec3(points[6], points[7], points[8]),
|
---|
282 | };
|
---|
283 |
|
---|
284 | // square
|
---|
285 | objects.push_back(SceneObject());
|
---|
286 | objects[1].shader_program = 0;
|
---|
287 | objects[1].vbo_offset = (GLvoid*) (6 * sizeof(float) * 3);
|
---|
288 | objects[1].num_points = 6;
|
---|
289 |
|
---|
290 | T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
|
---|
291 | R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f));
|
---|
292 | objects[1].model_mat = T_model*R_model;
|
---|
293 |
|
---|
294 | faces.push_back(ObjectFace());
|
---|
295 | faces[1].object_id = 1;
|
---|
296 | faces[1].points = {
|
---|
297 | vec3(points2[0], points2[1], points2[2]),
|
---|
298 | vec3(points2[3], points2[4], points2[5]),
|
---|
299 | vec3(points2[6], points2[7], points2[8]),
|
---|
300 | };
|
---|
301 |
|
---|
302 | faces.push_back(ObjectFace());
|
---|
303 | faces[2].object_id = 1;
|
---|
304 | faces[2].points = {
|
---|
305 | vec3(points2[9], points2[10], points2[11]),
|
---|
306 | vec3(points2[12], points2[13], points2[14]),
|
---|
307 | vec3(points2[15], points2[16], points2[17]),
|
---|
308 | };
|
---|
309 |
|
---|
310 | GLuint points_vbo = 0;
|
---|
311 | glGenBuffers(1, &points_vbo);
|
---|
312 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
313 | glBufferData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(points2), NULL, GL_DYNAMIC_DRAW);
|
---|
314 |
|
---|
315 | glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), points);
|
---|
316 | glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(points2), points2);
|
---|
317 |
|
---|
318 | GLuint colors_vbo = 0;
|
---|
319 | glGenBuffers(1, &colors_vbo);
|
---|
320 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
321 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
|
---|
322 |
|
---|
323 | GLuint vao = 0;
|
---|
324 | glGenVertexArrays(1, &vao);
|
---|
325 | glBindVertexArray(vao);
|
---|
326 |
|
---|
327 | glEnableVertexAttribArray(0);
|
---|
328 | glEnableVertexAttribArray(1);
|
---|
329 |
|
---|
330 | GLuint colors2_vbo = 0;
|
---|
331 | glGenBuffers(1, &colors2_vbo);
|
---|
332 | glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
|
---|
333 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors2), colors2, GL_STATIC_DRAW);
|
---|
334 |
|
---|
335 | GLuint vt_vbo;
|
---|
336 | glGenBuffers(1, &vt_vbo);
|
---|
337 | glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
|
---|
338 | glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW);
|
---|
339 |
|
---|
340 | GLuint vao2 = 0;
|
---|
341 | glGenVertexArrays(1, &vao2);
|
---|
342 | glBindVertexArray(vao2);
|
---|
343 |
|
---|
344 | glEnableVertexAttribArray(0);
|
---|
345 | glEnableVertexAttribArray(1);
|
---|
346 |
|
---|
347 | // I can create a vbo to store all points for all models,
|
---|
348 | // and another vbo to store all colors for all models, but how do I allow alternating between
|
---|
349 | // using colors and textures for each model?
|
---|
350 | // Do I create a third vbo for texture coordinates and change which vertex attribute array I have bound
|
---|
351 | // when I want to draw a textured model?
|
---|
352 | // Or do I create one vao with vertices and colors and another with vertices and textures and switch between the two?
|
---|
353 | // Since I would have to switch shader programs to toggle between using colors or textures,
|
---|
354 | // I think I should use one vao for both cases and have a points vbo, a colors vbo, and a textures vbo
|
---|
355 | // One program will use the points and colors, and the other will use the points and texture coords
|
---|
356 | // Review how to bind vbos to vertex attributes in the shader.
|
---|
357 | //
|
---|
358 | // Binding vbos is done using glVertexAttribPointer(...) on a per-vao basis and is not tied to any specific shader.
|
---|
359 | // This means, I could create two vaos, one for each shader and have one use points+colors, while the other
|
---|
360 | // uses points+texxcoords.
|
---|
361 | //
|
---|
362 | // At some point, when I have lots of objects, I want to group them by shader when drawing them.
|
---|
363 | // I'd probably create some sort of set per shader and have each set contain the ids of all objects currently using that shader
|
---|
364 | // Most likely, I'd want to implement each set using a bit field. Makes it constant time for updates and iterating through them
|
---|
365 | // should not be much of an issue either.
|
---|
366 | // Assuming making lots of draw calls instead of one is not innefficient, I should be fine.
|
---|
367 | // I might also want to use one glDrawElements call per shader to draw multiple non-memory-adjacent models
|
---|
368 | //
|
---|
369 | // DECISION: Use a glDrawElements call per shader since I use a regular array to specify the elements to draw
|
---|
370 | // Actually, this will only work once I get UBOs working since each object will have a different model matrix
|
---|
371 | // For now, I could implement this with a glDrawElements call per object and update the model uniform for each object
|
---|
372 |
|
---|
373 | GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
|
---|
374 | GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
|
---|
375 |
|
---|
376 | float speed = 1.0f;
|
---|
377 | float last_position = 0.0f;
|
---|
378 |
|
---|
379 | float cam_speed = 1.0f;
|
---|
380 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
381 |
|
---|
382 | // glm::lookAt can create the view matrix
|
---|
383 | // glm::perspective can create the projection matrix
|
---|
384 |
|
---|
385 | cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
---|
386 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
---|
387 |
|
---|
388 | mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
389 | mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
390 | view_mat = R*T;
|
---|
391 |
|
---|
392 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
---|
393 | float aspect = (float)width / (float)height;
|
---|
394 |
|
---|
395 | float range = tan(fov * 0.5f) * NEAR_CLIP;
|
---|
396 | float Sx = NEAR_CLIP / (range * aspect);
|
---|
397 | float Sy = NEAR_CLIP / range;
|
---|
398 | float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
399 | float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
400 |
|
---|
401 | float proj_arr[] = {
|
---|
402 | Sx, 0.0f, 0.0f, 0.0f,
|
---|
403 | 0.0f, Sy, 0.0f, 0.0f,
|
---|
404 | 0.0f, 0.0f, Sz, -1.0f,
|
---|
405 | 0.0f, 0.0f, Pz, 0.0f,
|
---|
406 | };
|
---|
407 | proj_mat = make_mat4(proj_arr);
|
---|
408 |
|
---|
409 | GLint model_test_loc = glGetUniformLocation(shader_program, "model");
|
---|
410 | GLint view_test_loc = glGetUniformLocation(shader_program, "view");
|
---|
411 | GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
|
---|
412 |
|
---|
413 | GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
|
---|
414 | GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
|
---|
415 | GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
|
---|
416 |
|
---|
417 | glUseProgram(shader_program);
|
---|
418 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
|
---|
419 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
420 | glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
421 |
|
---|
422 | glUseProgram(shader_program2);
|
---|
423 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
|
---|
424 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
425 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
426 |
|
---|
427 | objects[0].shader_program = shader_program;
|
---|
428 | objects[1].shader_program = shader_program2;
|
---|
429 |
|
---|
430 | vector<int> program1_objects, program2_objects;
|
---|
431 | vector<int>::iterator it;
|
---|
432 |
|
---|
433 | bool cam_moved = false;
|
---|
434 |
|
---|
435 | int frame_count = 0;
|
---|
436 | double elapsed_seconds_fps = 0.0f;
|
---|
437 | double previous_seconds = glfwGetTime();
|
---|
438 |
|
---|
439 | while (!glfwWindowShouldClose(window)) {
|
---|
440 | double current_seconds = glfwGetTime();
|
---|
441 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
442 | previous_seconds = current_seconds;
|
---|
443 |
|
---|
444 | elapsed_seconds_fps += elapsed_seconds;
|
---|
445 | if (elapsed_seconds_fps > 0.25f) {
|
---|
446 | fps = (double)frame_count / elapsed_seconds_fps;
|
---|
447 | cout << "FPS: " << fps << endl;
|
---|
448 |
|
---|
449 | frame_count = 0;
|
---|
450 | elapsed_seconds_fps = 0.0f;
|
---|
451 | }
|
---|
452 |
|
---|
453 | frame_count++;
|
---|
454 |
|
---|
455 | if (fabs(last_position) > 1.0f) {
|
---|
456 | speed = -speed;
|
---|
457 | }
|
---|
458 |
|
---|
459 | program1_objects.clear();
|
---|
460 | program2_objects.clear();
|
---|
461 |
|
---|
462 | // Handle events (Ideally, move all event-handling code
|
---|
463 | // before the render code)
|
---|
464 |
|
---|
465 | clickedObject = NULL;
|
---|
466 | glfwPollEvents();
|
---|
467 |
|
---|
468 | if (clickedObject == &objects[0]) {
|
---|
469 | selectedObject = &objects[0];
|
---|
470 | }
|
---|
471 | if (clickedObject == &objects[1]) {
|
---|
472 | selectedObject = &objects[1];
|
---|
473 | }
|
---|
474 |
|
---|
475 | if (selectedObject == &objects[1] &&
|
---|
476 | objects[1].shader_program == shader_program2) {
|
---|
477 | objects[1].shader_program = shader_program;
|
---|
478 | } else if (selectedObject != &objects[1] &&
|
---|
479 | objects[1].shader_program == shader_program) {
|
---|
480 | objects[1].shader_program = shader_program2;
|
---|
481 | }
|
---|
482 |
|
---|
483 | // group scene objects by shader
|
---|
484 | for (int i=0; i < objects.size(); i++) {
|
---|
485 | if (objects[i].shader_program == shader_program) {
|
---|
486 | program1_objects.push_back(i);
|
---|
487 | } else if (objects[i].shader_program == shader_program2) {
|
---|
488 | program2_objects.push_back(i);
|
---|
489 | }
|
---|
490 | }
|
---|
491 |
|
---|
492 | /*
|
---|
493 | model[12] = last_position + speed*elapsed_seconds;
|
---|
494 | last_position = model[12];
|
---|
495 | */
|
---|
496 |
|
---|
497 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
498 |
|
---|
499 | glUseProgram(shader_program);
|
---|
500 | glBindVertexArray(vao);
|
---|
501 |
|
---|
502 | for (it=program1_objects.begin(); it != program1_objects.end(); it++) {
|
---|
503 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
504 |
|
---|
505 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
506 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vbo_offset);
|
---|
507 |
|
---|
508 | if (selectedObject == &objects[*it]) {
|
---|
509 | if (*it == 1) {
|
---|
510 | glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
|
---|
511 | } else {
|
---|
512 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
513 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW);
|
---|
514 | }
|
---|
515 | } else {
|
---|
516 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
517 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
|
---|
518 | }
|
---|
519 |
|
---|
520 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
521 |
|
---|
522 | glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points);
|
---|
523 | }
|
---|
524 |
|
---|
525 | glUseProgram(shader_program2);
|
---|
526 | glBindVertexArray(vao2);
|
---|
527 |
|
---|
528 | for (it = program2_objects.begin(); it != program2_objects.end(); it++) {
|
---|
529 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
530 |
|
---|
531 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
532 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vbo_offset);
|
---|
533 |
|
---|
534 | glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
|
---|
535 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
536 |
|
---|
537 | glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points);
|
---|
538 | }
|
---|
539 |
|
---|
540 | glfwSwapBuffers(window);
|
---|
541 |
|
---|
542 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
543 | glfwSetWindowShouldClose(window, 1);
|
---|
544 | }
|
---|
545 |
|
---|
546 | float dist = cam_speed * elapsed_seconds;
|
---|
547 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
---|
548 | cam_pos.x -= cos(cam_yaw)*dist;
|
---|
549 | cam_pos.z += sin(cam_yaw)*dist;
|
---|
550 | cam_moved = true;
|
---|
551 | }
|
---|
552 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
---|
553 | cam_pos.x += cos(cam_yaw)*dist;
|
---|
554 | cam_pos.z -= sin(cam_yaw)*dist;
|
---|
555 | cam_moved = true;
|
---|
556 | }
|
---|
557 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
---|
558 | cam_pos.x -= sin(cam_yaw)*dist;
|
---|
559 | cam_pos.z -= cos(cam_yaw)*dist;
|
---|
560 | cam_moved = true;
|
---|
561 | }
|
---|
562 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
---|
563 | cam_pos.x += sin(cam_yaw)*dist;
|
---|
564 | cam_pos.z += cos(cam_yaw)*dist;
|
---|
565 | cam_moved = true;
|
---|
566 | }
|
---|
567 | if (glfwGetKey(window, GLFW_KEY_LEFT)) {
|
---|
568 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
---|
569 | cam_moved = true;
|
---|
570 | }
|
---|
571 | if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
|
---|
572 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
---|
573 | cam_moved = true;
|
---|
574 | }
|
---|
575 | if (cam_moved) {
|
---|
576 | T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
577 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
578 | view_mat = R*T;
|
---|
579 |
|
---|
580 | glUseProgram(shader_program);
|
---|
581 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
582 |
|
---|
583 | glUseProgram(shader_program2);
|
---|
584 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
585 |
|
---|
586 | cam_moved = false;
|
---|
587 | }
|
---|
588 | }
|
---|
589 |
|
---|
590 | glfwTerminate();
|
---|
591 | return 0;
|
---|
592 | }
|
---|
593 |
|
---|
594 | GLuint loadShader(GLenum type, string file) {
|
---|
595 | cout << "Loading shader from file " << file << endl;
|
---|
596 |
|
---|
597 | ifstream shaderFile(file);
|
---|
598 | GLuint shaderId = 0;
|
---|
599 |
|
---|
600 | if (shaderFile.is_open()) {
|
---|
601 | string line, shaderString;
|
---|
602 |
|
---|
603 | while(getline(shaderFile, line)) {
|
---|
604 | shaderString += line + "\n";
|
---|
605 | }
|
---|
606 | shaderFile.close();
|
---|
607 | const char* shaderCString = shaderString.c_str();
|
---|
608 |
|
---|
609 | shaderId = glCreateShader(type);
|
---|
610 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
611 | glCompileShader(shaderId);
|
---|
612 |
|
---|
613 | cout << "Loaded successfully" << endl;
|
---|
614 | } else {
|
---|
615 | cout << "Failed to load the file" << endl;
|
---|
616 | }
|
---|
617 |
|
---|
618 | return shaderId;
|
---|
619 | }
|
---|
620 |
|
---|
621 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
|
---|
622 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
|
---|
623 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
|
---|
624 |
|
---|
625 | GLuint shader_program = glCreateProgram();
|
---|
626 | glAttachShader(shader_program, vs);
|
---|
627 | glAttachShader(shader_program, fs);
|
---|
628 |
|
---|
629 | glLinkProgram(shader_program);
|
---|
630 |
|
---|
631 | return shader_program;
|
---|
632 | }
|
---|
633 |
|
---|
634 | unsigned char* loadImage(string file_name, int* x, int* y) {
|
---|
635 | int n;
|
---|
636 | int force_channels = 4; // This forces RGBA (4 bytes per pixel)
|
---|
637 | unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
|
---|
638 |
|
---|
639 | int width_in_bytes = *x * 4;
|
---|
640 | unsigned char *top = NULL;
|
---|
641 | unsigned char *bottom = NULL;
|
---|
642 | unsigned char temp = 0;
|
---|
643 | int half_height = *y / 2;
|
---|
644 |
|
---|
645 | // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
|
---|
646 | for (int row = 0; row < half_height; row++) {
|
---|
647 | top = image_data + row * width_in_bytes;
|
---|
648 | bottom = image_data + (*y - row - 1) * width_in_bytes;
|
---|
649 | for (int col = 0; col < width_in_bytes; col++) {
|
---|
650 | temp = *top;
|
---|
651 | *top = *bottom;
|
---|
652 | *bottom = temp;
|
---|
653 | top++;
|
---|
654 | bottom++;
|
---|
655 | }
|
---|
656 | }
|
---|
657 |
|
---|
658 | if (!image_data) {
|
---|
659 | fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
|
---|
660 | }
|
---|
661 |
|
---|
662 | // Not Power-of-2 check
|
---|
663 | if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
|
---|
664 | fprintf(stderr, "WARNING: texture %s is not power-of-2 dimensions\n", file_name.c_str());
|
---|
665 | }
|
---|
666 |
|
---|
667 | return image_data;
|
---|
668 | }
|
---|
669 |
|
---|
670 | bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) {
|
---|
671 | // LINE EQUATION: P = O + Dt
|
---|
672 | // O = cam
|
---|
673 | // D = ray_world
|
---|
674 |
|
---|
675 | // PLANE EQUATION: P dot n + d = 0
|
---|
676 | // n is the normal vector
|
---|
677 | // d is the offset from the origin
|
---|
678 |
|
---|
679 | // Take the cross-product of two vectors on the plane to get the normal
|
---|
680 | vec3 v1 = face->points[1] - face->points[0];
|
---|
681 | vec3 v2 = face->points[2] - face->points[0];
|
---|
682 |
|
---|
683 | vec3 normal = vec3(v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x);
|
---|
684 |
|
---|
685 | print4DVector("Full world ray", world_ray);
|
---|
686 |
|
---|
687 | SceneObject* obj = &objects[face->object_id];
|
---|
688 | vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
|
---|
689 | vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
|
---|
690 |
|
---|
691 | local_ray = local_ray - local_cam;
|
---|
692 |
|
---|
693 | float d = -glm::dot(face->points[0], normal);
|
---|
694 | cout << "d: " << d << endl;
|
---|
695 |
|
---|
696 | float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
|
---|
697 | cout << "t: " << t << endl;
|
---|
698 |
|
---|
699 | vec3 intersection = local_cam + t*local_ray;
|
---|
700 | printVector("Intersection", intersection);
|
---|
701 |
|
---|
702 | if (insideTriangle(intersection, face->points)) {
|
---|
703 | click_point = obj->model_mat * vec4(intersection, 1.0f);
|
---|
704 | return true;
|
---|
705 | } else {
|
---|
706 | return false;
|
---|
707 | }
|
---|
708 | }
|
---|
709 |
|
---|
710 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
|
---|
711 | vec3 v21 = triangle_points[1]- triangle_points[0];
|
---|
712 | vec3 v31 = triangle_points[2]- triangle_points[0];
|
---|
713 | vec3 pv1 = p- triangle_points[0];
|
---|
714 |
|
---|
715 | float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
|
---|
716 | float x = (pv1.x-y*v31.x) / v21.x;
|
---|
717 |
|
---|
718 | cout << "(" << x << ", " << y << ")" << endl;
|
---|
719 |
|
---|
720 | return x > 0.0f && y > 0.0f && x+y < 1.0f;
|
---|
721 | }
|
---|
722 |
|
---|
723 | void printVector(string label, vec3 v) {
|
---|
724 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
|
---|
725 | }
|
---|
726 |
|
---|
727 | void print4DVector(string label, vec4 v) {
|
---|
728 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl;
|
---|
729 | }
|
---|