source: opengl-game/new-game.cpp@ 485424b

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

Add support for loading and applying texture and add a square to the scene with a demo texture

  • Property mode set to 100644
File size: 11.6 KB
Line 
1#include "logger.h"
2
3#include "stb_image.h"
4
5#include <glm/mat4x4.hpp> // glm::mat4
6#include <glm/gtc/matrix_transform.hpp>
7#include <glm/gtc/type_ptr.hpp>
8
9#include <GL/glew.h>
10#include <GLFW/glfw3.h>
11
12#include <cstdio>
13#include <iostream>
14#include <fstream>
15
16#define _USE_MATH_DEFINES
17#include <cmath>
18
19using namespace std;
20using namespace glm;
21
22#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
23const bool FULLSCREEN = false;
24
25GLuint loadShader(GLenum type, string file);
26GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
27unsigned char* loadImage(string file_name, int* x, int* y);
28
29void glfw_error_callback(int error, const char* description) {
30 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
31}
32
33int main(int argc, char* argv[]) {
34 cout << "New OpenGL Game" << endl;
35
36 if (!restart_gl_log()) {}
37 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
38
39 glfwSetErrorCallback(glfw_error_callback);
40 if (!glfwInit()) {
41 fprintf(stderr, "ERROR: could not start GLFW3\n");
42 return 1;
43 }
44
45#ifdef __APPLE__
46 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
47 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
48 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
49 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
50#endif
51
52 glfwWindowHint(GLFW_SAMPLES, 4);
53
54 GLFWwindow* window = NULL;
55
56 int width = 640;
57 int height = 480;
58
59 if (FULLSCREEN) {
60 GLFWmonitor* mon = glfwGetPrimaryMonitor();
61 const GLFWvidmode* vmode = glfwGetVideoMode(mon);
62
63 cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
64 window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
65
66 width = vmode->width;
67 height = vmode->height;
68 } else {
69 window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
70 }
71
72 if (!window) {
73 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
74 glfwTerminate();
75 return 1;
76 }
77 glfwMakeContextCurrent(window);
78 glewExperimental = GL_TRUE;
79 glewInit();
80
81 // glViewport(0, 0, width*2, height*2);
82
83 const GLubyte* renderer = glGetString(GL_RENDERER);
84 const GLubyte* version = glGetString(GL_VERSION);
85 printf("Renderer: %s\n", renderer);
86 printf("OpenGL version supported %s\n", version);
87
88 glEnable(GL_DEPTH_TEST);
89 glDepthFunc(GL_LESS);
90
91 glEnable(GL_CULL_FACE);
92 // glCullFace(GL_BACK);
93 // glFrontFace(GL_CW);
94
95 int x, y;
96 unsigned char* texImage = loadImage("test.png", &x, &y);
97 if (texImage) {
98 cout << "Yay, I loaded an image!" << endl;
99 cout << x << endl;
100 cout << y << endl;
101 printf ("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
102 }
103
104 GLuint tex = 0;
105 glGenTextures(1, &tex);
106 glActiveTexture(GL_TEXTURE0);
107 glBindTexture(GL_TEXTURE_2D, tex);
108 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
109
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
112 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
113 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
114
115 GLfloat points[] = {
116 0.0f, 0.5f, 0.0f,
117 -0.5f, -0.5f, 0.0f,
118 0.5f, -0.5f, 0.0f,
119 0.5f, -0.5f, 0.0f,
120 -0.5f, -0.5f, 0.0f,
121 0.0f, 0.5f, 0.0f,
122 };
123
124 GLfloat colors[] = {
125 1.0, 0.0, 0.0,
126 0.0, 0.0, 1.0,
127 0.0, 1.0, 0.0,
128 0.0, 1.0, 0.0,
129 0.0, 0.0, 1.0,
130 1.0, 0.0, 0.0,
131 };
132
133 // Each point is made of 3 floats
134 int numPoints = (sizeof(points) / sizeof(float)) / 3;
135
136 GLfloat points2[] = {
137 0.5f, 0.5f, 0.0f,
138 -0.5f, 0.5f, 0.0f,
139 -0.5f, -0.5f, 0.0f,
140 0.5f, 0.5f, 0.0f,
141 -0.5f, -0.5f, 0.0f,
142 0.5f, -0.5f, 0.0f,
143 };
144
145 /*
146 GLfloat colors2[] = {
147 0.0, 0.9, 0.9,
148 0.0, 0.9, 0.9,
149 0.0, 0.9, 0.9,
150 0.0, 0.9, 0.9,
151 0.0, 0.9, 0.9,
152 0.0, 0.9, 0.9,
153 };
154 */
155
156 GLfloat texcoords[] = {
157 1.0f, 1.0f,
158 0.0f, 1.0f,
159 0.0, 0.0,
160 1.0, 1.0,
161 0.0, 0.0,
162 1.0, 0.0
163 };
164
165 // Each point is made of 3 floats
166 int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
167
168 mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
169 mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
170 mat4 model_mat = T_model*R_model;
171
172 mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
173 mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
174 mat4 model_mat2 = T_model2*R_model2;
175
176 GLuint points_vbo = 0;
177 glGenBuffers(1, &points_vbo);
178 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
179 glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
180
181 GLuint colors_vbo = 0;
182 glGenBuffers(1, &colors_vbo);
183 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
184 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
185
186 GLuint vao = 0;
187 glGenVertexArrays(1, &vao);
188 glBindVertexArray(vao);
189 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
190 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
191 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
192 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
193
194 glEnableVertexAttribArray(0);
195 glEnableVertexAttribArray(1);
196
197 GLuint points2_vbo = 0;
198 glGenBuffers(1, &points2_vbo);
199 glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
200 glBufferData(GL_ARRAY_BUFFER, sizeof(points2), points2, GL_STATIC_DRAW);
201
202 /*
203 GLuint colors2_vbo = 0;
204 glGenBuffers(1, &colors2_vbo);
205 glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
206 glBufferData(GL_ARRAY_BUFFER, sizeof(colors2), colors2, GL_STATIC_DRAW);
207 */
208
209 GLuint vt_vbo;
210 glGenBuffers(1, &vt_vbo);
211 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
212 glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW);
213
214 GLuint vao2 = 0;
215 glGenVertexArrays(1, &vao2);
216 glBindVertexArray(vao2);
217 glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
218 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
219 // glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
220 // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
221 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
222 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
223
224 glEnableVertexAttribArray(0);
225 glEnableVertexAttribArray(1);
226
227 GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
228 GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
229
230 float speed = 1.0f;
231 float last_position = 0.0f;
232
233 float cam_speed = 1.0f;
234 float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
235
236 float cam_pos[] = {0.0f, 0.0f, 2.0f};
237 float cam_yaw = 0.0f;
238
239 mat4 T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
240 mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
241 mat4 view_mat = R*T;
242
243 float near = 0.1f;
244 float far = 100.0f;
245 float fov = 67.0f * ONE_DEG_IN_RAD;
246 float aspect = (float)width / (float)height;
247
248 float range = tan(fov * 0.5f) * near;
249 float Sx = near / (range * aspect);
250 float Sy = near / range;
251 float Sz = -(far + near) / (far - near);
252 float Pz = -(2.0f * far * near) / (far - near);
253
254 float proj_mat[] = {
255 Sx, 0.0f, 0.0f, 0.0f,
256 0.0f, Sy, 0.0f, 0.0f,
257 0.0f, 0.0f, Sz, -1.0f,
258 0.0f, 0.0f, Pz, 0.0f,
259 };
260
261 GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
262 GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
263 GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
264
265 GLint model_test_loc = glGetUniformLocation(shader_program, "model");
266 GLint view_test_loc = glGetUniformLocation(shader_program, "view");
267 GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
268
269 glUseProgram(shader_program);
270 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
271 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, proj_mat);
272
273 glUseProgram(shader_program2);
274 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
275 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, proj_mat);
276
277 // glUniform1i(tex_loc, 0);
278
279 bool cam_moved = false;
280
281 double previous_seconds = glfwGetTime();
282 while (!glfwWindowShouldClose(window)) {
283 double current_seconds = glfwGetTime();
284 double elapsed_seconds = current_seconds - previous_seconds;
285 previous_seconds = current_seconds;
286
287 if (fabs(last_position) > 1.0f) {
288 speed = -speed;
289 }
290
291 /*
292 model[12] = last_position + speed*elapsed_seconds;
293 last_position = model[12];
294 */
295
296 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
297
298 glUseProgram(shader_program);
299 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
300
301 glBindVertexArray(vao);
302
303 glDrawArrays(GL_TRIANGLES, 0, numPoints);
304
305 glUseProgram(shader_program2);
306 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
307
308 glBindVertexArray(vao2);
309
310 glDrawArrays(GL_TRIANGLES, 0, numPoints2);
311
312 glfwPollEvents();
313 glfwSwapBuffers(window);
314
315 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
316 glfwSetWindowShouldClose(window, 1);
317 }
318
319 float dist = cam_speed * elapsed_seconds;
320 if (glfwGetKey(window, GLFW_KEY_A)) {
321 cam_pos[0] -= cos(cam_yaw)*dist;
322 cam_pos[2] += sin(cam_yaw)*dist;
323 cam_moved = true;
324 }
325 if (glfwGetKey(window, GLFW_KEY_D)) {
326 cam_pos[0] += cos(cam_yaw)*dist;
327 cam_pos[2] -= sin(cam_yaw)*dist;
328 cam_moved = true;
329 }
330 if (glfwGetKey(window, GLFW_KEY_W)) {
331 cam_pos[0] -= sin(cam_yaw)*dist;
332 cam_pos[2] -= cos(cam_yaw)*dist;
333 cam_moved = true;
334 }
335 if (glfwGetKey(window, GLFW_KEY_S)) {
336 cam_pos[0] += sin(cam_yaw)*dist;
337 cam_pos[2] += cos(cam_yaw)*dist;
338 cam_moved = true;
339 }
340 if (glfwGetKey(window, GLFW_KEY_LEFT)) {
341 cam_yaw += cam_yaw_speed * elapsed_seconds;
342 cam_moved = true;
343 }
344 if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
345 cam_yaw -= cam_yaw_speed * elapsed_seconds;
346 cam_moved = true;
347 }
348 if (cam_moved) {
349 T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
350 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
351 view_mat = R*T;
352
353 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
354 cam_moved = false;
355 }
356 }
357
358 glfwTerminate();
359 return 0;
360}
361
362GLuint loadShader(GLenum type, string file) {
363 cout << "Loading shader from file " << file << endl;
364
365 ifstream shaderFile(file);
366 GLuint shaderId = 0;
367
368 if (shaderFile.is_open()) {
369 string line, shaderString;
370
371 while(getline(shaderFile, line)) {
372 shaderString += line + "\n";
373 }
374 shaderFile.close();
375 const char* shaderCString = shaderString.c_str();
376
377 shaderId = glCreateShader(type);
378 glShaderSource(shaderId, 1, &shaderCString, NULL);
379 glCompileShader(shaderId);
380
381 cout << "Loaded successfully" << endl;
382 } else {
383 cout << "Failed to loade the file" << endl;
384 }
385
386 return shaderId;
387}
388
389GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
390 GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
391 GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
392
393 GLuint shader_program = glCreateProgram();
394 glAttachShader(shader_program, vs);
395 glAttachShader(shader_program, fs);
396
397 glLinkProgram(shader_program);
398
399 return shader_program;
400}
401
402unsigned char* loadImage(string file_name, int* x, int* y) {
403 int n;
404 int force_channels = 4;
405 unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
406 if (!image_data) {
407 fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
408 }
409 return image_data;
410}
Note: See TracBrowser for help on using the repository browser.