1 | #include "logger.h"
|
---|
2 |
|
---|
3 | #include "stb_image.h"
|
---|
4 |
|
---|
5 | // I think this was for the OpenGL 4 book font file tutorial
|
---|
6 | //#define STB_IMAGE_WRITE_IMPLEMENTATION
|
---|
7 | //#include "stb_image_write.h"
|
---|
8 |
|
---|
9 | #define _USE_MATH_DEFINES
|
---|
10 | #define GLM_SWIZZLE
|
---|
11 |
|
---|
12 | // This is to fix a non-alignment issue when passing vec4 params.
|
---|
13 | // Check if it got fixed in a later version of GLM
|
---|
14 | #define GLM_FORCE_PURE
|
---|
15 |
|
---|
16 | #include <glm/mat4x4.hpp>
|
---|
17 | #include <glm/gtc/matrix_transform.hpp>
|
---|
18 | #include <glm/gtc/type_ptr.hpp>
|
---|
19 |
|
---|
20 | #include "IMGUI/imgui.h"
|
---|
21 | #include "imgui_impl_glfw_gl3.h"
|
---|
22 |
|
---|
23 | #include <GL/glew.h>
|
---|
24 | #include <GLFW/glfw3.h>
|
---|
25 |
|
---|
26 | #include <cstdio>
|
---|
27 | #include <iostream>
|
---|
28 | #include <fstream>
|
---|
29 | #include <cmath>
|
---|
30 | #include <string>
|
---|
31 | #include <array>
|
---|
32 | #include <vector>
|
---|
33 | #include <queue>
|
---|
34 |
|
---|
35 | using namespace std;
|
---|
36 | using namespace glm;
|
---|
37 |
|
---|
38 | #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
|
---|
39 |
|
---|
40 | struct SceneObject {
|
---|
41 | unsigned int id;
|
---|
42 | mat4 model_mat;
|
---|
43 | GLuint shader_program;
|
---|
44 | unsigned int num_points;
|
---|
45 | GLint vertex_vbo_offset;
|
---|
46 | vector<GLfloat> points;
|
---|
47 | vector<GLfloat> colors;
|
---|
48 | vector<GLfloat> texcoords;
|
---|
49 | vector<GLfloat> normals;
|
---|
50 | vector<GLfloat> selected_colors;
|
---|
51 | };
|
---|
52 |
|
---|
53 | enum State {
|
---|
54 | STATE_MAIN_MENU,
|
---|
55 | STATE_GAME,
|
---|
56 | };
|
---|
57 |
|
---|
58 | enum Event {
|
---|
59 | EVENT_GO_TO_MAIN_MENU,
|
---|
60 | EVENT_GO_TO_GAME,
|
---|
61 | EVENT_QUIT,
|
---|
62 | };
|
---|
63 |
|
---|
64 | const bool FULLSCREEN = false;
|
---|
65 | int width = 640;
|
---|
66 | int height = 480;
|
---|
67 |
|
---|
68 | double fps;
|
---|
69 |
|
---|
70 | vec3 cam_pos;
|
---|
71 |
|
---|
72 | mat4 view_mat;
|
---|
73 | mat4 proj_mat;
|
---|
74 |
|
---|
75 | vector<SceneObject> objects;
|
---|
76 | queue<Event> events;
|
---|
77 |
|
---|
78 | SceneObject* clickedObject = NULL;
|
---|
79 | SceneObject* selectedObject;
|
---|
80 |
|
---|
81 | float NEAR_CLIP = 0.1f;
|
---|
82 | float FAR_CLIP = 100.0f;
|
---|
83 |
|
---|
84 | // Should really have some array or struct of UI-related variables
|
---|
85 | bool isRunning = true;
|
---|
86 |
|
---|
87 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
---|
88 |
|
---|
89 | bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point);
|
---|
90 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
|
---|
91 |
|
---|
92 | GLuint loadShader(GLenum type, string file);
|
---|
93 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
|
---|
94 | unsigned char* loadImage(string file_name, int* x, int* y);
|
---|
95 |
|
---|
96 | void printVector(string label, vec3 v);
|
---|
97 | void print4DVector(string label, vec4 v);
|
---|
98 |
|
---|
99 | void renderMainMenu();
|
---|
100 | void renderMainMenuGui();
|
---|
101 |
|
---|
102 | void renderScene(vector<SceneObject>& objects,
|
---|
103 | GLuint color_sp, GLuint texture_sp,
|
---|
104 | GLuint vao1, GLuint vao2,
|
---|
105 | GLuint shader1_mat_loc, GLuint shader2_mat_loc,
|
---|
106 | GLuint points_vbo, GLuint normals_vbo,
|
---|
107 | GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo,
|
---|
108 | SceneObject* selectedObject);
|
---|
109 | void renderSceneGui();
|
---|
110 |
|
---|
111 | void glfw_error_callback(int error, const char* description) {
|
---|
112 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
113 | }
|
---|
114 |
|
---|
115 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
---|
116 | double mouse_x, mouse_y;
|
---|
117 | glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
---|
118 |
|
---|
119 | if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
---|
120 | cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
|
---|
121 | selectedObject = NULL;
|
---|
122 |
|
---|
123 | float x = (2.0f*mouse_x) / width - 1.0f;
|
---|
124 | float y = 1.0f - (2.0f*mouse_y) / height;
|
---|
125 |
|
---|
126 | cout << "x: " << x << ", y: " << y << endl;
|
---|
127 |
|
---|
128 | vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
|
---|
129 | vec4 ray_eye = inverse(proj_mat) * ray_clip;
|
---|
130 | ray_eye = vec4(ray_eye.xy(), -1.0f, 1.0f);
|
---|
131 | vec4 ray_world = inverse(view_mat) * ray_eye;
|
---|
132 |
|
---|
133 | vec4 cam_pos_temp = vec4(cam_pos, 1.0f);
|
---|
134 |
|
---|
135 | vec4 click_point;
|
---|
136 | 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
|
---|
137 | SceneObject* closest_object = NULL;
|
---|
138 |
|
---|
139 | SceneObject* obj;
|
---|
140 | for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) {
|
---|
141 | obj = &*it;
|
---|
142 |
|
---|
143 | for (unsigned int p_idx = 0; p_idx < it->points.size(); p_idx += 9) {
|
---|
144 | if (faceClicked({
|
---|
145 | vec3(it->points[p_idx], it->points[p_idx + 1], it->points[p_idx + 2]),
|
---|
146 | vec3(it->points[p_idx + 3], it->points[p_idx + 4], it->points[p_idx + 5]),
|
---|
147 | vec3(it->points[p_idx + 6], it->points[p_idx + 7], it->points[p_idx + 8]),
|
---|
148 | },
|
---|
149 | obj, ray_world, cam_pos_temp, click_point)) {
|
---|
150 | click_point = view_mat * click_point;
|
---|
151 |
|
---|
152 | if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
|
---|
153 | closest_point = click_point.xyz();
|
---|
154 | closest_object = obj;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (closest_object == NULL) {
|
---|
161 | cout << "No object was clicked" << endl;
|
---|
162 | } else {
|
---|
163 | clickedObject = closest_object;
|
---|
164 | cout << "Clicked object: " << clickedObject->id << endl;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | int main(int argc, char* argv[]) {
|
---|
170 | cout << "New OpenGL Game" << endl;
|
---|
171 |
|
---|
172 | if (!restart_gl_log()) {}
|
---|
173 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
174 |
|
---|
175 | glfwSetErrorCallback(glfw_error_callback);
|
---|
176 | if (!glfwInit()) {
|
---|
177 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
178 | return 1;
|
---|
179 | }
|
---|
180 |
|
---|
181 | #ifdef __APPLE__
|
---|
182 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
183 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
184 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
185 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
189 |
|
---|
190 | GLFWwindow* window = NULL;
|
---|
191 | GLFWmonitor* mon = NULL;
|
---|
192 |
|
---|
193 | if (FULLSCREEN) {
|
---|
194 | mon = glfwGetPrimaryMonitor();
|
---|
195 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
196 |
|
---|
197 | width = vmode->width;
|
---|
198 | height = vmode->height;
|
---|
199 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
200 | }
|
---|
201 | window = glfwCreateWindow(width, height, "New OpenGL Game", mon, NULL);
|
---|
202 |
|
---|
203 | if (!window) {
|
---|
204 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
205 | glfwTerminate();
|
---|
206 | return 1;
|
---|
207 | }
|
---|
208 |
|
---|
209 | glfwMakeContextCurrent(window);
|
---|
210 | glewExperimental = GL_TRUE;
|
---|
211 | glewInit();
|
---|
212 |
|
---|
213 | // Setup Dear ImGui binding
|
---|
214 | IMGUI_CHECKVERSION();
|
---|
215 | ImGui::CreateContext();
|
---|
216 | ImGuiIO& io = ImGui::GetIO(); (void)io;
|
---|
217 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
---|
218 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
---|
219 | ImGui_ImplGlfwGL3_Init(window, true);
|
---|
220 |
|
---|
221 | // Setup style
|
---|
222 | ImGui::StyleColorsDark();
|
---|
223 | //ImGui::StyleColorsClassic();
|
---|
224 |
|
---|
225 | glfwSetMouseButtonCallback(window, mouse_button_callback);
|
---|
226 |
|
---|
227 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
228 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
229 | printf("Renderer: %s\n", renderer);
|
---|
230 | printf("OpenGL version supported %s\n", version);
|
---|
231 |
|
---|
232 | glEnable(GL_DEPTH_TEST);
|
---|
233 | glDepthFunc(GL_LESS);
|
---|
234 |
|
---|
235 | glEnable(GL_CULL_FACE);
|
---|
236 | // glCullFace(GL_BACK);
|
---|
237 | // glFrontFace(GL_CW);
|
---|
238 |
|
---|
239 | int x, y;
|
---|
240 | unsigned char* texImage = loadImage("test.png", &x, &y);
|
---|
241 | if (texImage) {
|
---|
242 | cout << "Yay, I loaded an image!" << endl;
|
---|
243 | cout << x << endl;
|
---|
244 | cout << y << endl;
|
---|
245 | printf("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
|
---|
246 | }
|
---|
247 |
|
---|
248 | GLuint tex = 0;
|
---|
249 | glGenTextures(1, &tex);
|
---|
250 | glActiveTexture(GL_TEXTURE0);
|
---|
251 | glBindTexture(GL_TEXTURE_2D, tex);
|
---|
252 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
|
---|
253 |
|
---|
254 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
255 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
256 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
257 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
258 |
|
---|
259 | // I can create a vbo to store all points for all models,
|
---|
260 | // and another vbo to store all colors for all models, but how do I allow alternating between
|
---|
261 | // using colors and textures for each model?
|
---|
262 | // Do I create a third vbo for texture coordinates and change which vertex attribute array I have bound
|
---|
263 | // when I want to draw a textured model?
|
---|
264 | // Or do I create one vao with vertices and colors and another with vertices and textures and switch between the two?
|
---|
265 | // Since I would have to switch shader programs to toggle between using colors or textures,
|
---|
266 | // I think I should use one vao for both cases and have a points vbo, a colors vbo, and a textures vbo
|
---|
267 | // One program will use the points and colors, and the other will use the points and texture coords
|
---|
268 | // Review how to bind vbos to vertex attributes in the shader.
|
---|
269 | //
|
---|
270 | // Binding vbos is done using glVertexAttribPointer(...) on a per-vao basis and is not tied to any specific shader.
|
---|
271 | // This means, I could create two vaos, one for each shader and have one use points+colors, while the other
|
---|
272 | // uses points+texxcoords.
|
---|
273 | //
|
---|
274 | // At some point, when I have lots of objects, I want to group them by shader when drawing them.
|
---|
275 | // I'd probably create some sort of set per shader and have each set contain the ids of all objects currently using that shader
|
---|
276 | // Most likely, I'd want to implement each set using a bit field. Makes it constant time for updates and iterating through them
|
---|
277 | // should not be much of an issue either.
|
---|
278 | // Assuming making lots of draw calls instead of one is not innefficient, I should be fine.
|
---|
279 | // I might also want to use one glDrawElements call per shader to draw multiple non-memory-adjacent models
|
---|
280 | //
|
---|
281 | // DECISION: Use a glDrawElements call per shader since I use a regular array to specify the elements to draw
|
---|
282 | // Actually, this will only work once I get UBOs working since each object will have a different model matrix
|
---|
283 | // For now, I could implement this with a glDrawElements call per object and update the model uniform for each object
|
---|
284 |
|
---|
285 | GLuint color_sp = loadShaderProgram("./color.vert", "./color.frag");
|
---|
286 | GLuint texture_sp = loadShaderProgram("./texture.vert", "./texture.frag");
|
---|
287 |
|
---|
288 | mat4 T_model, R_model;
|
---|
289 |
|
---|
290 | // triangle
|
---|
291 | objects.push_back(SceneObject());
|
---|
292 | objects[0].id = 0;
|
---|
293 | objects[0].shader_program = color_sp;
|
---|
294 | objects[0].vertex_vbo_offset = 0;
|
---|
295 | objects[0].points = {
|
---|
296 | 0.0f, 0.5f, 0.0f,
|
---|
297 | -0.5f, -0.5f, 0.0f,
|
---|
298 | 0.5f, -0.5f, 0.0f,
|
---|
299 | 0.5f, -0.5f, 0.0f,
|
---|
300 | -0.5f, -0.5f, 0.0f,
|
---|
301 | 0.0f, 0.5f, 0.0f,
|
---|
302 | };
|
---|
303 | objects[0].colors = {
|
---|
304 | 1.0f, 0.0f, 0.0f,
|
---|
305 | 0.0f, 0.0f, 1.0f,
|
---|
306 | 0.0f, 1.0f, 0.0f,
|
---|
307 | 0.0f, 1.0f, 0.0f,
|
---|
308 | 0.0f, 0.0f, 1.0f,
|
---|
309 | 1.0f, 0.0f, 0.0f,
|
---|
310 | };
|
---|
311 | objects[0].texcoords = {
|
---|
312 | 1.0f, 1.0f,
|
---|
313 | 0.0f, 1.0f,
|
---|
314 | 0.0f, 0.0f,
|
---|
315 | 1.0f, 1.0f,
|
---|
316 | 0.0f, 0.0f,
|
---|
317 | 1.0f, 0.0f
|
---|
318 | };
|
---|
319 | objects[0].normals = {
|
---|
320 | 0.0f, 0.0f, 1.0f,
|
---|
321 | 0.0f, 0.0f, 1.0f,
|
---|
322 | 0.0f, 0.0f, 1.0f,
|
---|
323 | 0.0f, 0.0f, -1.0f,
|
---|
324 | 0.0f, 0.0f, -1.0f,
|
---|
325 | 0.0f, 0.0f, -1.0f,
|
---|
326 | };
|
---|
327 | objects[0].selected_colors = {
|
---|
328 | 0.0f, 1.0f, 0.0f,
|
---|
329 | 0.0f, 1.0f, 0.0f,
|
---|
330 | 0.0f, 1.0f, 0.0f,
|
---|
331 | 0.0f, 1.0f, 0.0f,
|
---|
332 | 0.0f, 1.0f, 0.0f,
|
---|
333 | 0.0f, 1.0f, 0.0f,
|
---|
334 | };
|
---|
335 | objects[0].num_points = objects[0].points.size() / 3;
|
---|
336 |
|
---|
337 | T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f));
|
---|
338 | R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
|
---|
339 | objects[0].model_mat = T_model*R_model;
|
---|
340 |
|
---|
341 | // square
|
---|
342 | objects.push_back(SceneObject());
|
---|
343 | objects[1].id = 1;
|
---|
344 | objects[1].shader_program = texture_sp;
|
---|
345 | objects[1].vertex_vbo_offset = 6;
|
---|
346 | objects[1].points = {
|
---|
347 | 0.5f, 0.5f, 0.0f,
|
---|
348 | -0.5f, 0.5f, 0.0f,
|
---|
349 | -0.5f, -0.5f, 0.0f,
|
---|
350 | 0.5f, 0.5f, 0.0f,
|
---|
351 | -0.5f, -0.5f, 0.0f,
|
---|
352 | 0.5f, -0.5f, 0.0f,
|
---|
353 | };
|
---|
354 | objects[1].colors = {
|
---|
355 | 1.0f, 0.0f, 0.0f,
|
---|
356 | 0.0f, 0.0f, 1.0f,
|
---|
357 | 0.0f, 1.0f, 0.0f,
|
---|
358 | 0.0f, 1.0f, 0.0f,
|
---|
359 | 0.0f, 0.0f, 1.0f,
|
---|
360 | 1.0f, 0.0f, 0.0f,
|
---|
361 | };
|
---|
362 | objects[1].texcoords = {
|
---|
363 | 1.0f, 1.0f,
|
---|
364 | 0.0f, 1.0f,
|
---|
365 | 0.0f, 0.0f,
|
---|
366 | 1.0f, 1.0f,
|
---|
367 | 0.0f, 0.0f,
|
---|
368 | 1.0f, 0.0f
|
---|
369 | };
|
---|
370 | objects[1].normals = {
|
---|
371 | 0.0f, 0.0f, 1.0f,
|
---|
372 | 0.0f, 0.0f, 1.0f,
|
---|
373 | 0.0f, 0.0f, 1.0f,
|
---|
374 | 0.0f, 0.0f, 1.0f,
|
---|
375 | 0.0f, 0.0f, 1.0f,
|
---|
376 | 0.0f, 0.0f, 1.0f,
|
---|
377 | };
|
---|
378 | objects[1].selected_colors = {
|
---|
379 | 0.0f, 0.6f, 0.9f,
|
---|
380 | 0.0f, 0.6f, 0.9f,
|
---|
381 | 0.0f, 0.6f, 0.9f,
|
---|
382 | 0.0f, 0.6f, 0.9f,
|
---|
383 | 0.0f, 0.6f, 0.9f,
|
---|
384 | 0.0f, 0.6f, 0.9f,
|
---|
385 | };
|
---|
386 | objects[1].num_points = objects[1].points.size() / 3;
|
---|
387 |
|
---|
388 | T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
|
---|
389 | R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f));
|
---|
390 | objects[1].model_mat = T_model*R_model;
|
---|
391 |
|
---|
392 | vector<SceneObject>::iterator obj_it;
|
---|
393 | GLsizeiptr offset;
|
---|
394 |
|
---|
395 | GLsizeiptr points_buffer_size = 0;
|
---|
396 | GLsizeiptr textures_buffer_size = 0;
|
---|
397 |
|
---|
398 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
399 | points_buffer_size += obj_it->points.size() * sizeof(GLfloat);
|
---|
400 | textures_buffer_size += obj_it->texcoords.size() * sizeof(GLfloat);
|
---|
401 | }
|
---|
402 |
|
---|
403 | GLuint points_vbo = 0;
|
---|
404 | glGenBuffers(1, &points_vbo);
|
---|
405 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
406 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
407 |
|
---|
408 | offset = 0;
|
---|
409 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
410 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->points.size() * sizeof(GLfloat), &obj_it->points[0]);
|
---|
411 | offset += obj_it->points.size() * sizeof(GLfloat);
|
---|
412 | }
|
---|
413 |
|
---|
414 | GLuint colors_vbo = 0;
|
---|
415 | glGenBuffers(1, &colors_vbo);
|
---|
416 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
417 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
418 |
|
---|
419 | offset = 0;
|
---|
420 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
421 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->colors.size() * sizeof(GLfloat), &obj_it->colors[0]);
|
---|
422 | offset += obj_it->colors.size() * sizeof(GLfloat);
|
---|
423 | }
|
---|
424 |
|
---|
425 | GLuint selected_colors_vbo = 0;
|
---|
426 | glGenBuffers(1, &selected_colors_vbo);
|
---|
427 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
428 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
429 |
|
---|
430 | offset = 0;
|
---|
431 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
432 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->selected_colors.size() * sizeof(GLfloat), &obj_it->selected_colors[0]);
|
---|
433 | offset += obj_it->selected_colors.size() * sizeof(GLfloat);
|
---|
434 | }
|
---|
435 |
|
---|
436 | GLuint texcoords_vbo = 0;
|
---|
437 | glGenBuffers(1, &texcoords_vbo);
|
---|
438 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
439 | glBufferData(GL_ARRAY_BUFFER, textures_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
440 |
|
---|
441 | offset = 0;
|
---|
442 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
443 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->texcoords.size() * sizeof(GLfloat), &obj_it->texcoords[0]);
|
---|
444 | offset += obj_it->texcoords.size() * sizeof(GLfloat);
|
---|
445 | }
|
---|
446 |
|
---|
447 | GLuint normals_vbo = 0;
|
---|
448 | glGenBuffers(1, &normals_vbo);
|
---|
449 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
450 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
451 |
|
---|
452 | offset = 0;
|
---|
453 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
454 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->normals.size() * sizeof(GLfloat), &obj_it->normals[0]);
|
---|
455 | offset += obj_it->normals.size() * sizeof(GLfloat);
|
---|
456 | }
|
---|
457 |
|
---|
458 | GLuint vao = 0;
|
---|
459 | glGenVertexArrays(1, &vao);
|
---|
460 | glBindVertexArray(vao);
|
---|
461 |
|
---|
462 | glEnableVertexAttribArray(0);
|
---|
463 | glEnableVertexAttribArray(1);
|
---|
464 | glEnableVertexAttribArray(2);
|
---|
465 |
|
---|
466 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
467 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
468 |
|
---|
469 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
470 | glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
471 |
|
---|
472 | GLuint vao2 = 0;
|
---|
473 | glGenVertexArrays(1, &vao2);
|
---|
474 | glBindVertexArray(vao2);
|
---|
475 |
|
---|
476 | glEnableVertexAttribArray(0);
|
---|
477 | glEnableVertexAttribArray(1);
|
---|
478 | glEnableVertexAttribArray(2);
|
---|
479 |
|
---|
480 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
481 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
482 |
|
---|
483 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
484 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
485 |
|
---|
486 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
487 | glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
488 |
|
---|
489 | float speed = 1.0f;
|
---|
490 | float last_position = 0.0f;
|
---|
491 |
|
---|
492 | float cam_speed = 1.0f;
|
---|
493 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
494 |
|
---|
495 | // glm::lookAt can create the view matrix
|
---|
496 | // glm::perspective can create the projection matrix
|
---|
497 |
|
---|
498 | cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
---|
499 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
---|
500 |
|
---|
501 | mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
502 | mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
503 | view_mat = R*T;
|
---|
504 |
|
---|
505 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
---|
506 | float aspect = (float)width / (float)height;
|
---|
507 |
|
---|
508 | float range = tan(fov * 0.5f) * NEAR_CLIP;
|
---|
509 | float Sx = NEAR_CLIP / (range * aspect);
|
---|
510 | float Sy = NEAR_CLIP / range;
|
---|
511 | float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
512 | float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
513 |
|
---|
514 | float proj_arr[] = {
|
---|
515 | Sx, 0.0f, 0.0f, 0.0f,
|
---|
516 | 0.0f, Sy, 0.0f, 0.0f,
|
---|
517 | 0.0f, 0.0f, Sz, -1.0f,
|
---|
518 | 0.0f, 0.0f, Pz, 0.0f,
|
---|
519 | };
|
---|
520 | proj_mat = make_mat4(proj_arr);
|
---|
521 |
|
---|
522 | GLuint model_test_loc = glGetUniformLocation(color_sp, "model");
|
---|
523 | GLuint view_test_loc = glGetUniformLocation(color_sp, "view");
|
---|
524 | GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj");
|
---|
525 |
|
---|
526 | GLuint model_mat_loc = glGetUniformLocation(texture_sp, "model");
|
---|
527 | GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view");
|
---|
528 | GLuint proj_mat_loc = glGetUniformLocation(texture_sp, "proj");
|
---|
529 |
|
---|
530 | glUseProgram(color_sp);
|
---|
531 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
|
---|
532 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
533 | glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
534 |
|
---|
535 | glUseProgram(texture_sp);
|
---|
536 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
|
---|
537 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
538 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
539 |
|
---|
540 | bool cam_moved = false;
|
---|
541 |
|
---|
542 | int frame_count = 0;
|
---|
543 | double elapsed_seconds_fps = 0.0f;
|
---|
544 | double previous_seconds = glfwGetTime();
|
---|
545 |
|
---|
546 | // This draws wireframes. Useful for seeing separate faces and occluded objects.
|
---|
547 | //glPolygonMode(GL_FRONT, GL_LINE);
|
---|
548 |
|
---|
549 | // disable vsync to see real framerate
|
---|
550 | //glfwSwapInterval(0);
|
---|
551 |
|
---|
552 | State curState = STATE_MAIN_MENU;
|
---|
553 |
|
---|
554 | while (!glfwWindowShouldClose(window) && isRunning) {
|
---|
555 | double current_seconds = glfwGetTime();
|
---|
556 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
557 | previous_seconds = current_seconds;
|
---|
558 |
|
---|
559 | elapsed_seconds_fps += elapsed_seconds;
|
---|
560 | if (elapsed_seconds_fps > 0.25f) {
|
---|
561 | fps = (double)frame_count / elapsed_seconds_fps;
|
---|
562 | cout << "FPS: " << fps << endl;
|
---|
563 |
|
---|
564 | frame_count = 0;
|
---|
565 | elapsed_seconds_fps = 0.0f;
|
---|
566 | }
|
---|
567 |
|
---|
568 | frame_count++;
|
---|
569 |
|
---|
570 | if (fabs(last_position) > 1.0f) {
|
---|
571 | speed = -speed;
|
---|
572 | }
|
---|
573 |
|
---|
574 | // Handle events (Ideally, move all event-handling code
|
---|
575 | // before the render code)
|
---|
576 |
|
---|
577 | clickedObject = NULL;
|
---|
578 | glfwPollEvents();
|
---|
579 |
|
---|
580 | while (!events.empty()) {
|
---|
581 | switch (events.front()) {
|
---|
582 | case EVENT_GO_TO_MAIN_MENU:
|
---|
583 | curState = STATE_MAIN_MENU;
|
---|
584 | break;
|
---|
585 | case EVENT_GO_TO_GAME:
|
---|
586 | curState = STATE_GAME;
|
---|
587 | break;
|
---|
588 | case EVENT_QUIT:
|
---|
589 | isRunning = false;
|
---|
590 | break;
|
---|
591 | }
|
---|
592 | events.pop();
|
---|
593 | }
|
---|
594 |
|
---|
595 | if (curState == STATE_GAME) {
|
---|
596 | if (clickedObject == &objects[0]) {
|
---|
597 | selectedObject = &objects[0];
|
---|
598 | }
|
---|
599 | if (clickedObject == &objects[1]) {
|
---|
600 | selectedObject = &objects[1];
|
---|
601 | }
|
---|
602 | }
|
---|
603 |
|
---|
604 | /*
|
---|
605 | model[12] = last_position + speed*elapsed_seconds;
|
---|
606 | last_position = model[12];
|
---|
607 | */
|
---|
608 |
|
---|
609 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
610 |
|
---|
611 | switch (curState) {
|
---|
612 | case STATE_MAIN_MENU:
|
---|
613 | renderMainMenu();
|
---|
614 | renderMainMenuGui();
|
---|
615 | break;
|
---|
616 | case STATE_GAME:
|
---|
617 | renderScene(objects,
|
---|
618 | color_sp, texture_sp,
|
---|
619 | vao, vao2,
|
---|
620 | model_test_loc, model_mat_loc,
|
---|
621 | points_vbo, normals_vbo,
|
---|
622 | colors_vbo, texcoords_vbo, selected_colors_vbo,
|
---|
623 | selectedObject);
|
---|
624 | renderSceneGui();
|
---|
625 | break;
|
---|
626 | }
|
---|
627 |
|
---|
628 | glfwSwapBuffers(window);
|
---|
629 |
|
---|
630 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
631 | glfwSetWindowShouldClose(window, 1);
|
---|
632 | }
|
---|
633 |
|
---|
634 | float dist = cam_speed * elapsed_seconds;
|
---|
635 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
---|
636 | cam_pos.x -= cos(cam_yaw)*dist;
|
---|
637 | cam_pos.z += sin(cam_yaw)*dist;
|
---|
638 | cam_moved = true;
|
---|
639 | }
|
---|
640 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
---|
641 | cam_pos.x += cos(cam_yaw)*dist;
|
---|
642 | cam_pos.z -= sin(cam_yaw)*dist;
|
---|
643 | cam_moved = true;
|
---|
644 | }
|
---|
645 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
---|
646 | cam_pos.x -= sin(cam_yaw)*dist;
|
---|
647 | cam_pos.z -= cos(cam_yaw)*dist;
|
---|
648 | cam_moved = true;
|
---|
649 | }
|
---|
650 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
---|
651 | cam_pos.x += sin(cam_yaw)*dist;
|
---|
652 | cam_pos.z += cos(cam_yaw)*dist;
|
---|
653 | cam_moved = true;
|
---|
654 | }
|
---|
655 | if (glfwGetKey(window, GLFW_KEY_LEFT)) {
|
---|
656 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
---|
657 | cam_moved = true;
|
---|
658 | }
|
---|
659 | if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
|
---|
660 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
---|
661 | cam_moved = true;
|
---|
662 | }
|
---|
663 | if (cam_moved) {
|
---|
664 | T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
665 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
666 | view_mat = R*T;
|
---|
667 |
|
---|
668 | glUseProgram(color_sp);
|
---|
669 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
670 |
|
---|
671 | glUseProgram(texture_sp);
|
---|
672 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
673 |
|
---|
674 | cam_moved = false;
|
---|
675 | }
|
---|
676 | }
|
---|
677 |
|
---|
678 | ImGui_ImplGlfwGL3_Shutdown();
|
---|
679 | ImGui::DestroyContext();
|
---|
680 |
|
---|
681 | glfwDestroyWindow(window);
|
---|
682 | glfwTerminate();
|
---|
683 |
|
---|
684 | return 0;
|
---|
685 | }
|
---|
686 |
|
---|
687 | GLuint loadShader(GLenum type, string file) {
|
---|
688 | cout << "Loading shader from file " << file << endl;
|
---|
689 |
|
---|
690 | ifstream shaderFile(file);
|
---|
691 | GLuint shaderId = 0;
|
---|
692 |
|
---|
693 | if (shaderFile.is_open()) {
|
---|
694 | string line, shaderString;
|
---|
695 |
|
---|
696 | while(getline(shaderFile, line)) {
|
---|
697 | shaderString += line + "\n";
|
---|
698 | }
|
---|
699 | shaderFile.close();
|
---|
700 | const char* shaderCString = shaderString.c_str();
|
---|
701 |
|
---|
702 | shaderId = glCreateShader(type);
|
---|
703 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
704 | glCompileShader(shaderId);
|
---|
705 |
|
---|
706 | cout << "Loaded successfully" << endl;
|
---|
707 | } else {
|
---|
708 | cout << "Failed to load the file" << endl;
|
---|
709 | }
|
---|
710 |
|
---|
711 | return shaderId;
|
---|
712 | }
|
---|
713 |
|
---|
714 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
|
---|
715 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
|
---|
716 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
|
---|
717 |
|
---|
718 | GLuint shader_program = glCreateProgram();
|
---|
719 | glAttachShader(shader_program, vs);
|
---|
720 | glAttachShader(shader_program, fs);
|
---|
721 |
|
---|
722 | glLinkProgram(shader_program);
|
---|
723 |
|
---|
724 | return shader_program;
|
---|
725 | }
|
---|
726 |
|
---|
727 | unsigned char* loadImage(string file_name, int* x, int* y) {
|
---|
728 | int n;
|
---|
729 | int force_channels = 4; // This forces RGBA (4 bytes per pixel)
|
---|
730 | unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
|
---|
731 |
|
---|
732 | int width_in_bytes = *x * 4;
|
---|
733 | unsigned char *top = NULL;
|
---|
734 | unsigned char *bottom = NULL;
|
---|
735 | unsigned char temp = 0;
|
---|
736 | int half_height = *y / 2;
|
---|
737 |
|
---|
738 | // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
|
---|
739 | for (int row = 0; row < half_height; row++) {
|
---|
740 | top = image_data + row * width_in_bytes;
|
---|
741 | bottom = image_data + (*y - row - 1) * width_in_bytes;
|
---|
742 | for (int col = 0; col < width_in_bytes; col++) {
|
---|
743 | temp = *top;
|
---|
744 | *top = *bottom;
|
---|
745 | *bottom = temp;
|
---|
746 | top++;
|
---|
747 | bottom++;
|
---|
748 | }
|
---|
749 | }
|
---|
750 |
|
---|
751 | if (!image_data) {
|
---|
752 | fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
|
---|
753 | }
|
---|
754 |
|
---|
755 | // Not Power-of-2 check
|
---|
756 | if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
|
---|
757 | fprintf(stderr, "WARNING: texture %s is not power-of-2 dimensions\n", file_name.c_str());
|
---|
758 | }
|
---|
759 |
|
---|
760 | return image_data;
|
---|
761 | }
|
---|
762 |
|
---|
763 | bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point) {
|
---|
764 | // LINE EQUATION: P = O + Dt
|
---|
765 | // O = cam
|
---|
766 | // D = ray_world
|
---|
767 |
|
---|
768 | // PLANE EQUATION: P dot n + d = 0
|
---|
769 | // n is the normal vector
|
---|
770 | // d is the offset from the origin
|
---|
771 |
|
---|
772 | // Take the cross-product of two vectors on the plane to get the normal
|
---|
773 | vec3 v1 = points[1] - points[0];
|
---|
774 | vec3 v2 = points[2] - points[0];
|
---|
775 |
|
---|
776 | 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);
|
---|
777 |
|
---|
778 | print4DVector("Full world ray", world_ray);
|
---|
779 |
|
---|
780 | vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
|
---|
781 | vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
|
---|
782 |
|
---|
783 | local_ray = local_ray - local_cam;
|
---|
784 |
|
---|
785 | float d = -glm::dot(points[0], normal);
|
---|
786 | cout << "d: " << d << endl;
|
---|
787 |
|
---|
788 | float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
|
---|
789 | cout << "t: " << t << endl;
|
---|
790 |
|
---|
791 | vec3 intersection = local_cam + t*local_ray;
|
---|
792 | printVector("Intersection", intersection);
|
---|
793 |
|
---|
794 | if (insideTriangle(intersection, points)) {
|
---|
795 | click_point = obj->model_mat * vec4(intersection, 1.0f);
|
---|
796 | return true;
|
---|
797 | } else {
|
---|
798 | return false;
|
---|
799 | }
|
---|
800 | }
|
---|
801 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
|
---|
802 | vec3 v21 = triangle_points[1] - triangle_points[0];
|
---|
803 | vec3 v31 = triangle_points[2] - triangle_points[0];
|
---|
804 | vec3 pv1 = p - triangle_points[0];
|
---|
805 |
|
---|
806 | float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
|
---|
807 | float x = (pv1.x-y*v31.x) / v21.x;
|
---|
808 |
|
---|
809 | return x > 0.0f && y > 0.0f && x+y < 1.0f;
|
---|
810 | }
|
---|
811 |
|
---|
812 | void printVector(string label, vec3 v) {
|
---|
813 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
|
---|
814 | }
|
---|
815 |
|
---|
816 | void print4DVector(string label, vec4 v) {
|
---|
817 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl;
|
---|
818 | }
|
---|
819 |
|
---|
820 | // The easiest thing here seems to be to set all the colors we want in a CPU array once per frame,
|
---|
821 | // them copy it over to the GPU and make one draw call
|
---|
822 | // This method also easily allows us to use any colors we want for each shape.
|
---|
823 | // Try to compare the frame times for the current method and the new one
|
---|
824 | //
|
---|
825 | // Alternatively, I have one large color buffer that has selected and unselected colors
|
---|
826 | // Then, when I know which object is selected, I can use glVertexAttribPointer to decide
|
---|
827 | // whether to use the selected or unselected color for it
|
---|
828 |
|
---|
829 | // I'll have to think of the best way to do something similar when using
|
---|
830 | // one draw call. Probably, in that case, I'll use one draw call for all unselectable objects
|
---|
831 | // and use the approach mentioned above for all selectable objects.
|
---|
832 | // I can have one colors vbo for unselectable objects and another for selected+unselected colors
|
---|
833 | // of selectable objects
|
---|
834 |
|
---|
835 | // For both colored and textured objects, using a single draw call will only work for objects
|
---|
836 | // that don't change state (i.e. don't change colors or switch from colored to textured).
|
---|
837 |
|
---|
838 | // This means I'll probably have one call for static colored objects, one call for static textured objects,
|
---|
839 | // a loop of calls for dynamic currently colored objects, and a loop of calls for dynamic currently textured objects.
|
---|
840 | // This will increase if I add new shaders since I'll need either one new call or one new loop of calls per shader
|
---|
841 |
|
---|
842 | void renderScene(vector<SceneObject>& objects,
|
---|
843 | GLuint color_sp, GLuint texture_sp,
|
---|
844 | GLuint vao1, GLuint vao2,
|
---|
845 | GLuint shader1_mat_loc, GLuint shader2_mat_loc,
|
---|
846 | GLuint points_vbo, GLuint normals_vbo,
|
---|
847 | GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo,
|
---|
848 | SceneObject* selectedObject) {
|
---|
849 |
|
---|
850 | vector<int> colored_objs, selected_objs, textured_objs, static_colored_objs, static_textured_objs;
|
---|
851 |
|
---|
852 | // group scene objects by shader and vbo
|
---|
853 | for (unsigned int i = 0; i < objects.size(); i++) {
|
---|
854 | if (selectedObject == &objects[i]) {
|
---|
855 | selected_objs.push_back(i);
|
---|
856 | } else if (objects[i].shader_program == color_sp) {
|
---|
857 | colored_objs.push_back(i);
|
---|
858 | } else if (objects[i].shader_program == texture_sp) {
|
---|
859 | textured_objs.push_back(i);
|
---|
860 | }
|
---|
861 | }
|
---|
862 |
|
---|
863 | vector<int>::iterator it;
|
---|
864 |
|
---|
865 | glUseProgram(color_sp);
|
---|
866 | glBindVertexArray(vao1);
|
---|
867 |
|
---|
868 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
869 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
870 |
|
---|
871 | for (it = colored_objs.begin(); it != colored_objs.end(); it++) {
|
---|
872 | glUniformMatrix4fv(shader1_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
873 |
|
---|
874 | glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
|
---|
875 | }
|
---|
876 |
|
---|
877 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
878 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
879 |
|
---|
880 | for (it = selected_objs.begin(); it != selected_objs.end(); it++) {
|
---|
881 | glUniformMatrix4fv(shader1_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
882 |
|
---|
883 | glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
|
---|
884 | }
|
---|
885 |
|
---|
886 | glUseProgram(texture_sp);
|
---|
887 | glBindVertexArray(vao2);
|
---|
888 |
|
---|
889 | for (it = textured_objs.begin(); it != textured_objs.end(); it++) {
|
---|
890 | glUniformMatrix4fv(shader2_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
891 |
|
---|
892 | glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
|
---|
893 | }
|
---|
894 | }
|
---|
895 |
|
---|
896 | void renderSceneGui() {
|
---|
897 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
898 |
|
---|
899 | // 1. Show a simple window.
|
---|
900 | // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
|
---|
901 | /*
|
---|
902 | {
|
---|
903 | static float f = 0.0f;
|
---|
904 | static int counter = 0;
|
---|
905 | ImGui::Text("Hello, world!"); // Display some text (you can use a format string too)
|
---|
906 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
|
---|
907 | ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
|
---|
908 |
|
---|
909 | ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our windows open/close state
|
---|
910 | ImGui::Checkbox("Another Window", &show_another_window);
|
---|
911 |
|
---|
912 | if (ImGui::Button("Button")) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
|
---|
913 | counter++;
|
---|
914 | ImGui::SameLine();
|
---|
915 | ImGui::Text("counter = %d", counter);
|
---|
916 |
|
---|
917 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
|
---|
918 | }
|
---|
919 | */
|
---|
920 |
|
---|
921 | {
|
---|
922 | ImGui::SetNextWindowSize(ImVec2(85, 22), ImGuiCond_Once);
|
---|
923 | ImGui::SetNextWindowPos(ImVec2(10, 50), ImGuiCond_Once);
|
---|
924 | ImGui::Begin("WndStats", NULL,
|
---|
925 | ImGuiWindowFlags_NoTitleBar |
|
---|
926 | ImGuiWindowFlags_NoResize |
|
---|
927 | ImGuiWindowFlags_NoMove);
|
---|
928 | ImGui::Text("Score: ???");
|
---|
929 | ImGui::End();
|
---|
930 | }
|
---|
931 |
|
---|
932 | {
|
---|
933 | ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once);
|
---|
934 | ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
|
---|
935 | ImGui::Begin("WndMenubar", NULL,
|
---|
936 | ImGuiWindowFlags_NoTitleBar |
|
---|
937 | ImGuiWindowFlags_NoResize |
|
---|
938 | ImGuiWindowFlags_NoMove);
|
---|
939 | ImGui::InvisibleButton("", ImVec2(155, 18));
|
---|
940 | ImGui::SameLine();
|
---|
941 | if (ImGui::Button("Main Menu")) {
|
---|
942 | events.push(EVENT_GO_TO_MAIN_MENU);
|
---|
943 | }
|
---|
944 | ImGui::End();
|
---|
945 | }
|
---|
946 |
|
---|
947 | ImGui::Render();
|
---|
948 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
949 | }
|
---|
950 |
|
---|
951 | void renderMainMenu() {
|
---|
952 | }
|
---|
953 |
|
---|
954 | void renderMainMenuGui() {
|
---|
955 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
956 |
|
---|
957 | {
|
---|
958 | int padding = 4;
|
---|
959 | ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once);
|
---|
960 | ImGui::SetNextWindowSize(ImVec2(width + 2 * padding, height + 2 * padding), ImGuiCond_Once);
|
---|
961 | ImGui::Begin("WndMain", NULL,
|
---|
962 | ImGuiWindowFlags_NoTitleBar |
|
---|
963 | ImGuiWindowFlags_NoResize |
|
---|
964 | ImGuiWindowFlags_NoMove);
|
---|
965 |
|
---|
966 | ImGui::InvisibleButton("", ImVec2(10, 80));
|
---|
967 | ImGui::InvisibleButton("", ImVec2(285, 18));
|
---|
968 | ImGui::SameLine();
|
---|
969 | if (ImGui::Button("New Game")) {
|
---|
970 | events.push(EVENT_GO_TO_GAME);
|
---|
971 | }
|
---|
972 |
|
---|
973 | ImGui::InvisibleButton("", ImVec2(10, 15));
|
---|
974 | ImGui::InvisibleButton("", ImVec2(300, 18));
|
---|
975 | ImGui::SameLine();
|
---|
976 | if (ImGui::Button("Quit")) {
|
---|
977 | events.push(EVENT_QUIT);
|
---|
978 | }
|
---|
979 |
|
---|
980 | ImGui::End();
|
---|
981 | }
|
---|
982 |
|
---|
983 | ImGui::Render();
|
---|
984 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
985 | }
|
---|