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 | };
|
---|
45 |
|
---|
46 | struct ObjectFace {
|
---|
47 | unsigned int object_id;
|
---|
48 | array<vec3, 3> points;
|
---|
49 | };
|
---|
50 |
|
---|
51 | const bool FULLSCREEN = false;
|
---|
52 | int width = 640;
|
---|
53 | int height = 480;
|
---|
54 |
|
---|
55 | vec3 cam_pos;
|
---|
56 |
|
---|
57 | mat4 view_mat;
|
---|
58 | mat4 proj_mat;
|
---|
59 |
|
---|
60 | vector<SceneObject> objects;
|
---|
61 | vector<ObjectFace> faces;
|
---|
62 |
|
---|
63 | SceneObject* clickedObject = NULL;
|
---|
64 | SceneObject* selectedObject = NULL;
|
---|
65 |
|
---|
66 | bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point);
|
---|
67 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
|
---|
68 |
|
---|
69 | GLuint loadShader(GLenum type, string file);
|
---|
70 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
|
---|
71 | unsigned char* loadImage(string file_name, int* x, int* y);
|
---|
72 |
|
---|
73 | void printVector(string label, vec3 v);
|
---|
74 | void print4DVector(string label, vec4 v);
|
---|
75 |
|
---|
76 | float NEAR_CLIP = 0.1f;
|
---|
77 | float FAR_CLIP = 100.0f;
|
---|
78 |
|
---|
79 | void glfw_error_callback(int error, const char* description) {
|
---|
80 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
81 | }
|
---|
82 |
|
---|
83 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
---|
84 | double mouse_x, mouse_y;
|
---|
85 | glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
---|
86 |
|
---|
87 | if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
---|
88 | cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
|
---|
89 | selectedObject = NULL;
|
---|
90 |
|
---|
91 | float x = (2.0f*mouse_x) / width - 1.0f;
|
---|
92 | float y = 1.0f - (2.0f*mouse_y) / height;
|
---|
93 |
|
---|
94 | cout << "x: " << x << ", y: " << y << endl;
|
---|
95 |
|
---|
96 | vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
|
---|
97 | vec4 ray_eye = inverse(proj_mat) * ray_clip;
|
---|
98 | ray_eye = vec4(ray_eye.xy(), -1.0f, 1.0f);
|
---|
99 | vec4 ray_world = inverse(view_mat) * ray_eye;
|
---|
100 |
|
---|
101 | vec4 cam_pos_temp = vec4(cam_pos, 1.0f);
|
---|
102 |
|
---|
103 | vec4 click_point;
|
---|
104 | 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
|
---|
105 | int closest_face_id = -1;
|
---|
106 |
|
---|
107 | for (int i = 0; i<faces.size(); i++) {
|
---|
108 | if (faceClicked(&faces[i], ray_world, cam_pos_temp, click_point)) {
|
---|
109 | click_point = view_mat * click_point;
|
---|
110 |
|
---|
111 | if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
|
---|
112 | closest_point = click_point.xyz();
|
---|
113 | closest_face_id = i;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (closest_face_id == -1) {
|
---|
119 | cout << "No object was clicked" << endl;
|
---|
120 | } else {
|
---|
121 | clickedObject = &objects[faces[closest_face_id].object_id];
|
---|
122 | cout << "Clicked object: " << faces[closest_face_id].object_id << endl;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | int main(int argc, char* argv[]) {
|
---|
128 | cout << "New OpenGL Game" << endl;
|
---|
129 |
|
---|
130 | if (!restart_gl_log()) {}
|
---|
131 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
132 |
|
---|
133 | glfwSetErrorCallback(glfw_error_callback);
|
---|
134 | if (!glfwInit()) {
|
---|
135 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
136 | return 1;
|
---|
137 | }
|
---|
138 |
|
---|
139 | #ifdef __APPLE__
|
---|
140 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
141 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
142 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
143 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
147 |
|
---|
148 | GLFWwindow* window = NULL;
|
---|
149 | GLFWmonitor* mon = NULL;
|
---|
150 |
|
---|
151 | if (FULLSCREEN) {
|
---|
152 | mon = glfwGetPrimaryMonitor();
|
---|
153 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
154 |
|
---|
155 | width = vmode->width;
|
---|
156 | height = vmode->height;
|
---|
157 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
158 | }
|
---|
159 | window = glfwCreateWindow(width, height, "New OpenGL Game", mon, NULL);
|
---|
160 |
|
---|
161 | if (!window) {
|
---|
162 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
163 | glfwTerminate();
|
---|
164 | return 1;
|
---|
165 | }
|
---|
166 |
|
---|
167 | glfwSetMouseButtonCallback(window, mouse_button_callback);
|
---|
168 |
|
---|
169 | glfwMakeContextCurrent(window);
|
---|
170 | glewExperimental = GL_TRUE;
|
---|
171 | glewInit();
|
---|
172 |
|
---|
173 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
174 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
175 | printf("Renderer: %s\n", renderer);
|
---|
176 | printf("OpenGL version supported %s\n", version);
|
---|
177 |
|
---|
178 | glEnable(GL_DEPTH_TEST);
|
---|
179 | glDepthFunc(GL_LESS);
|
---|
180 |
|
---|
181 | glEnable(GL_CULL_FACE);
|
---|
182 | // glCullFace(GL_BACK);
|
---|
183 | // glFrontFace(GL_CW);
|
---|
184 |
|
---|
185 | int x, y;
|
---|
186 | unsigned char* texImage = loadImage("test.png", &x, &y);
|
---|
187 | if (texImage) {
|
---|
188 | cout << "Yay, I loaded an image!" << endl;
|
---|
189 | cout << x << endl;
|
---|
190 | cout << y << endl;
|
---|
191 | printf("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
|
---|
192 | }
|
---|
193 |
|
---|
194 | GLuint tex = 0;
|
---|
195 | glGenTextures(1, &tex);
|
---|
196 | glActiveTexture(GL_TEXTURE0);
|
---|
197 | glBindTexture(GL_TEXTURE_2D, tex);
|
---|
198 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
|
---|
199 |
|
---|
200 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
201 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
202 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
203 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
204 |
|
---|
205 | GLfloat points[] = {
|
---|
206 | 0.0f, 0.5f, 0.0f,
|
---|
207 | -0.5f, -0.5f, 0.0f,
|
---|
208 | 0.5f, -0.5f, 0.0f,
|
---|
209 | 0.5f, -0.5f, 0.0f,
|
---|
210 | -0.5f, -0.5f, 0.0f,
|
---|
211 | 0.0f, 0.5f, 0.0f,
|
---|
212 | };
|
---|
213 |
|
---|
214 | GLfloat colors[] = {
|
---|
215 | 1.0, 0.0, 0.0,
|
---|
216 | 0.0, 0.0, 1.0,
|
---|
217 | 0.0, 1.0, 0.0,
|
---|
218 | 0.0, 1.0, 0.0,
|
---|
219 | 0.0, 0.0, 1.0,
|
---|
220 | 1.0, 0.0, 0.0,
|
---|
221 | };
|
---|
222 |
|
---|
223 | GLfloat colors_new[] = {
|
---|
224 | 0.0, 1.0, 0.0,
|
---|
225 | 0.0, 1.0, 0.0,
|
---|
226 | 0.0, 1.0, 0.0,
|
---|
227 | 0.0, 1.0, 0.0,
|
---|
228 | 0.0, 1.0, 0.0,
|
---|
229 | 0.0, 1.0, 0.0,
|
---|
230 | };
|
---|
231 |
|
---|
232 | // Each point is made of 3 floats
|
---|
233 | int numPoints = (sizeof(points) / sizeof(float)) / 3;
|
---|
234 |
|
---|
235 | GLfloat points2[] = {
|
---|
236 | 0.5f, 0.5f, 0.0f,
|
---|
237 | -0.5f, 0.5f, 0.0f,
|
---|
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 | };
|
---|
243 |
|
---|
244 | GLfloat colors2[] = {
|
---|
245 | 0.0, 0.9, 0.9,
|
---|
246 | 0.0, 0.9, 0.9,
|
---|
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 | };
|
---|
252 |
|
---|
253 | GLfloat texcoords[] = {
|
---|
254 | 1.0f, 1.0f,
|
---|
255 | 0.0f, 1.0f,
|
---|
256 | 0.0, 0.0,
|
---|
257 | 1.0, 1.0,
|
---|
258 | 0.0, 0.0,
|
---|
259 | 1.0, 0.0
|
---|
260 | };
|
---|
261 |
|
---|
262 | // Each point is made of 3 floats
|
---|
263 | int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
|
---|
264 |
|
---|
265 | mat4 T_model, R_model;
|
---|
266 |
|
---|
267 | // triangle
|
---|
268 | objects.push_back(SceneObject());
|
---|
269 |
|
---|
270 | T_model = translate(mat4(), vec3(0.25f, 0.0f, 0.0f));
|
---|
271 | R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
|
---|
272 | objects[0].model_mat = T_model*R_model;
|
---|
273 |
|
---|
274 | faces.push_back(ObjectFace());
|
---|
275 | faces[0].object_id = 0;
|
---|
276 | faces[0].points = {
|
---|
277 | vec3(points[0], points[1], points[2]),
|
---|
278 | vec3(points[3], points[4], points[5]),
|
---|
279 | vec3(points[6], points[7], points[8]),
|
---|
280 | };
|
---|
281 |
|
---|
282 | // square
|
---|
283 | objects.push_back(SceneObject());
|
---|
284 |
|
---|
285 | T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
|
---|
286 | R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f));
|
---|
287 | objects[1].model_mat = T_model*R_model;
|
---|
288 |
|
---|
289 | faces.push_back(ObjectFace());
|
---|
290 | faces[1].object_id = 1;
|
---|
291 | faces[1].points = {
|
---|
292 | vec3(points2[0], points2[1], points2[2]),
|
---|
293 | vec3(points2[3], points2[4], points2[5]),
|
---|
294 | vec3(points2[6], points2[7], points2[8]),
|
---|
295 | };
|
---|
296 |
|
---|
297 | faces.push_back(ObjectFace());
|
---|
298 | faces[2].object_id = 1;
|
---|
299 | faces[2].points = {
|
---|
300 | vec3(points2[9], points2[10], points2[11]),
|
---|
301 | vec3(points2[12], points2[13], points2[14]),
|
---|
302 | vec3(points2[15], points2[16], points2[17]),
|
---|
303 | };
|
---|
304 |
|
---|
305 | GLuint points_vbo = 0;
|
---|
306 | glGenBuffers(1, &points_vbo);
|
---|
307 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
308 | glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
|
---|
309 |
|
---|
310 | GLuint colors_vbo = 0;
|
---|
311 | glGenBuffers(1, &colors_vbo);
|
---|
312 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
313 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
|
---|
314 |
|
---|
315 | GLuint vao = 0;
|
---|
316 | glGenVertexArrays(1, &vao);
|
---|
317 | glBindVertexArray(vao);
|
---|
318 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
319 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
320 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
321 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
322 |
|
---|
323 | glEnableVertexAttribArray(0);
|
---|
324 | glEnableVertexAttribArray(1);
|
---|
325 |
|
---|
326 | GLuint points2_vbo = 0;
|
---|
327 | glGenBuffers(1, &points2_vbo);
|
---|
328 | glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
|
---|
329 | glBufferData(GL_ARRAY_BUFFER, sizeof(points2), points2, GL_STATIC_DRAW);
|
---|
330 |
|
---|
331 | GLuint colors2_vbo = 0;
|
---|
332 | glGenBuffers(1, &colors2_vbo);
|
---|
333 | glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
|
---|
334 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors2), colors2, GL_STATIC_DRAW);
|
---|
335 |
|
---|
336 | GLuint vt_vbo;
|
---|
337 | glGenBuffers(1, &vt_vbo);
|
---|
338 | glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
|
---|
339 | glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW);
|
---|
340 |
|
---|
341 | GLuint vao2 = 0;
|
---|
342 | glGenVertexArrays(1, &vao2);
|
---|
343 | glBindVertexArray(vao2);
|
---|
344 | glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
|
---|
345 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
346 | // glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
|
---|
347 | // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
348 | glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
|
---|
349 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
350 |
|
---|
351 | glEnableVertexAttribArray(0);
|
---|
352 | glEnableVertexAttribArray(1);
|
---|
353 |
|
---|
354 | GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
|
---|
355 | GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
|
---|
356 |
|
---|
357 | float speed = 1.0f;
|
---|
358 | float last_position = 0.0f;
|
---|
359 |
|
---|
360 | float cam_speed = 1.0f;
|
---|
361 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
362 |
|
---|
363 | // glm::lookAt can create the view matrix
|
---|
364 | // glm::perspective can create the projection matrix
|
---|
365 |
|
---|
366 | cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
---|
367 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
---|
368 |
|
---|
369 | mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
370 | mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
371 | view_mat = R*T;
|
---|
372 |
|
---|
373 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
---|
374 | float aspect = (float)width / (float)height;
|
---|
375 |
|
---|
376 | float range = tan(fov * 0.5f) * NEAR_CLIP;
|
---|
377 | float Sx = NEAR_CLIP / (range * aspect);
|
---|
378 | float Sy = NEAR_CLIP / range;
|
---|
379 | float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
380 | float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
381 |
|
---|
382 | float proj_arr[] = {
|
---|
383 | Sx, 0.0f, 0.0f, 0.0f,
|
---|
384 | 0.0f, Sy, 0.0f, 0.0f,
|
---|
385 | 0.0f, 0.0f, Sz, -1.0f,
|
---|
386 | 0.0f, 0.0f, Pz, 0.0f,
|
---|
387 | };
|
---|
388 | proj_mat = make_mat4(proj_arr);
|
---|
389 |
|
---|
390 | GLint model_test_loc = glGetUniformLocation(shader_program, "model");
|
---|
391 | GLint view_test_loc = glGetUniformLocation(shader_program, "view");
|
---|
392 | GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
|
---|
393 |
|
---|
394 | GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
|
---|
395 | GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
|
---|
396 | GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
|
---|
397 |
|
---|
398 | glUseProgram(shader_program);
|
---|
399 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
|
---|
400 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
401 | glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
402 |
|
---|
403 | glUseProgram(shader_program2);
|
---|
404 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
|
---|
405 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
406 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
407 |
|
---|
408 | bool cam_moved = false;
|
---|
409 |
|
---|
410 | double previous_seconds = glfwGetTime();
|
---|
411 | while (!glfwWindowShouldClose(window)) {
|
---|
412 | double current_seconds = glfwGetTime();
|
---|
413 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
414 | previous_seconds = current_seconds;
|
---|
415 |
|
---|
416 | if (fabs(last_position) > 1.0f) {
|
---|
417 | speed = -speed;
|
---|
418 | }
|
---|
419 |
|
---|
420 | if (clickedObject == &objects[0]) {
|
---|
421 | selectedObject = &objects[0];
|
---|
422 | }
|
---|
423 |
|
---|
424 | // At some point, I should change this to only rebind the buffer once per click, not once per frame
|
---|
425 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
426 | if (selectedObject == &objects[0]) {
|
---|
427 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW);
|
---|
428 | }
|
---|
429 | else {
|
---|
430 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
|
---|
431 | }
|
---|
432 |
|
---|
433 | /*
|
---|
434 | model[12] = last_position + speed*elapsed_seconds;
|
---|
435 | last_position = model[12];
|
---|
436 | */
|
---|
437 |
|
---|
438 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
439 |
|
---|
440 | glUseProgram(shader_program);
|
---|
441 |
|
---|
442 | // Since every object will have a different model matrix, maybe it shouldn't be a uniform
|
---|
443 |
|
---|
444 | // this is temporary.
|
---|
445 | // It's needed to offset the code for the recoloring of the square working during click detection
|
---|
446 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
|
---|
447 |
|
---|
448 | glBindVertexArray(vao);
|
---|
449 |
|
---|
450 | glDrawArrays(GL_TRIANGLES, 0, numPoints);
|
---|
451 |
|
---|
452 | if (clickedObject == &objects[1]) {
|
---|
453 | selectedObject = &objects[1];
|
---|
454 | }
|
---|
455 |
|
---|
456 | if (selectedObject == &objects[1]) {
|
---|
457 | glUseProgram(shader_program);
|
---|
458 |
|
---|
459 | // this is temporary.
|
---|
460 | // It's needed to get the recoloring of the square working during click detection
|
---|
461 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
|
---|
462 |
|
---|
463 | glBindVertexArray(vao2);
|
---|
464 |
|
---|
465 | glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
|
---|
466 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
467 | } else {
|
---|
468 | glUseProgram(shader_program2);
|
---|
469 |
|
---|
470 | glBindVertexArray(vao2);
|
---|
471 |
|
---|
472 | glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
|
---|
473 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
474 | }
|
---|
475 |
|
---|
476 | glDrawArrays(GL_TRIANGLES, 0, numPoints2);
|
---|
477 |
|
---|
478 | clickedObject = NULL;
|
---|
479 |
|
---|
480 | glfwPollEvents();
|
---|
481 | glfwSwapBuffers(window);
|
---|
482 |
|
---|
483 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
484 | glfwSetWindowShouldClose(window, 1);
|
---|
485 | }
|
---|
486 |
|
---|
487 | float dist = cam_speed * elapsed_seconds;
|
---|
488 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
---|
489 | cam_pos.x -= cos(cam_yaw)*dist;
|
---|
490 | cam_pos.z += sin(cam_yaw)*dist;
|
---|
491 | cam_moved = true;
|
---|
492 | }
|
---|
493 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
---|
494 | cam_pos.x += cos(cam_yaw)*dist;
|
---|
495 | cam_pos.z -= sin(cam_yaw)*dist;
|
---|
496 | cam_moved = true;
|
---|
497 | }
|
---|
498 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
---|
499 | cam_pos.x -= sin(cam_yaw)*dist;
|
---|
500 | cam_pos.z -= cos(cam_yaw)*dist;
|
---|
501 | cam_moved = true;
|
---|
502 | }
|
---|
503 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
---|
504 | cam_pos.x += sin(cam_yaw)*dist;
|
---|
505 | cam_pos.z += cos(cam_yaw)*dist;
|
---|
506 | cam_moved = true;
|
---|
507 | }
|
---|
508 | if (glfwGetKey(window, GLFW_KEY_LEFT)) {
|
---|
509 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
---|
510 | cam_moved = true;
|
---|
511 | }
|
---|
512 | if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
|
---|
513 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
---|
514 | cam_moved = true;
|
---|
515 | }
|
---|
516 | if (cam_moved) {
|
---|
517 | T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
518 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
519 | view_mat = R*T;
|
---|
520 |
|
---|
521 | glUseProgram(shader_program);
|
---|
522 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
523 |
|
---|
524 | glUseProgram(shader_program2);
|
---|
525 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
526 |
|
---|
527 | cam_moved = false;
|
---|
528 | }
|
---|
529 | }
|
---|
530 |
|
---|
531 | glfwTerminate();
|
---|
532 | return 0;
|
---|
533 | }
|
---|
534 |
|
---|
535 | GLuint loadShader(GLenum type, string file) {
|
---|
536 | cout << "Loading shader from file " << file << endl;
|
---|
537 |
|
---|
538 | ifstream shaderFile(file);
|
---|
539 | GLuint shaderId = 0;
|
---|
540 |
|
---|
541 | if (shaderFile.is_open()) {
|
---|
542 | string line, shaderString;
|
---|
543 |
|
---|
544 | while(getline(shaderFile, line)) {
|
---|
545 | shaderString += line + "\n";
|
---|
546 | }
|
---|
547 | shaderFile.close();
|
---|
548 | const char* shaderCString = shaderString.c_str();
|
---|
549 |
|
---|
550 | shaderId = glCreateShader(type);
|
---|
551 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
552 | glCompileShader(shaderId);
|
---|
553 |
|
---|
554 | cout << "Loaded successfully" << endl;
|
---|
555 | } else {
|
---|
556 | cout << "Failed to load the file" << endl;
|
---|
557 | }
|
---|
558 |
|
---|
559 | return shaderId;
|
---|
560 | }
|
---|
561 |
|
---|
562 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
|
---|
563 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
|
---|
564 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
|
---|
565 |
|
---|
566 | GLuint shader_program = glCreateProgram();
|
---|
567 | glAttachShader(shader_program, vs);
|
---|
568 | glAttachShader(shader_program, fs);
|
---|
569 |
|
---|
570 | glLinkProgram(shader_program);
|
---|
571 |
|
---|
572 | return shader_program;
|
---|
573 | }
|
---|
574 |
|
---|
575 | unsigned char* loadImage(string file_name, int* x, int* y) {
|
---|
576 | int n;
|
---|
577 | int force_channels = 4; // This forces RGBA (4 bytes per pixel)
|
---|
578 | unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
|
---|
579 |
|
---|
580 | int width_in_bytes = *x * 4;
|
---|
581 | unsigned char *top = NULL;
|
---|
582 | unsigned char *bottom = NULL;
|
---|
583 | unsigned char temp = 0;
|
---|
584 | int half_height = *y / 2;
|
---|
585 |
|
---|
586 | // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
|
---|
587 | for (int row = 0; row < half_height; row++) {
|
---|
588 | top = image_data + row * width_in_bytes;
|
---|
589 | bottom = image_data + (*y - row - 1) * width_in_bytes;
|
---|
590 | for (int col = 0; col < width_in_bytes; col++) {
|
---|
591 | temp = *top;
|
---|
592 | *top = *bottom;
|
---|
593 | *bottom = temp;
|
---|
594 | top++;
|
---|
595 | bottom++;
|
---|
596 | }
|
---|
597 | }
|
---|
598 |
|
---|
599 | if (!image_data) {
|
---|
600 | fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
|
---|
601 | }
|
---|
602 |
|
---|
603 | // Not Power-of-2 check
|
---|
604 | if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
|
---|
605 | fprintf(stderr, "WARNING: texture %s is not power-of-2 dimensions\n", file_name.c_str());
|
---|
606 | }
|
---|
607 |
|
---|
608 | return image_data;
|
---|
609 | }
|
---|
610 |
|
---|
611 | bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) {
|
---|
612 | // LINE EQUATION: P = O + Dt
|
---|
613 | // O = cam
|
---|
614 | // D = ray_world
|
---|
615 |
|
---|
616 | // PLANE EQUATION: P dot n + d = 0
|
---|
617 | // n is the normal vector
|
---|
618 | // d is the offset from the origin
|
---|
619 |
|
---|
620 | // Take the cross-product of two vectors on the plane to get the normal
|
---|
621 | vec3 v1 = face->points[1] - face->points[0];
|
---|
622 | vec3 v2 = face->points[2] - face->points[0];
|
---|
623 |
|
---|
624 | 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);
|
---|
625 |
|
---|
626 | print4DVector("Full world ray", world_ray);
|
---|
627 |
|
---|
628 | SceneObject* obj = &objects[face->object_id];
|
---|
629 | vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
|
---|
630 | vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
|
---|
631 |
|
---|
632 | local_ray = local_ray - local_cam;
|
---|
633 |
|
---|
634 | float d = -glm::dot(face->points[0], normal);
|
---|
635 | cout << "d: " << d << endl;
|
---|
636 |
|
---|
637 | float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
|
---|
638 | cout << "t: " << t << endl;
|
---|
639 |
|
---|
640 | vec3 intersection = local_cam + t*local_ray;
|
---|
641 | printVector("Intersection", intersection);
|
---|
642 |
|
---|
643 | if (insideTriangle(intersection, face->points)) {
|
---|
644 | click_point = obj->model_mat * vec4(intersection, 1.0f);
|
---|
645 | return true;
|
---|
646 | } else {
|
---|
647 | return false;
|
---|
648 | }
|
---|
649 | }
|
---|
650 |
|
---|
651 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
|
---|
652 | vec3 v21 = triangle_points[1]- triangle_points[0];
|
---|
653 | vec3 v31 = triangle_points[2]- triangle_points[0];
|
---|
654 | vec3 pv1 = p- triangle_points[0];
|
---|
655 |
|
---|
656 | float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
|
---|
657 | float x = (pv1.x-y*v31.x) / v21.x;
|
---|
658 |
|
---|
659 | cout << "(" << x << ", " << y << ")" << endl;
|
---|
660 |
|
---|
661 | return x > 0.0f && y > 0.0f && x+y < 1.0f;
|
---|
662 | }
|
---|
663 |
|
---|
664 | void printVector(string label, vec3 v) {
|
---|
665 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
|
---|
666 | }
|
---|
667 |
|
---|
668 | void print4DVector(string label, vec4 v) {
|
---|
669 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl;
|
---|
670 | }
|
---|