[8b7cfcf] | 1 | // NEXT STEP; Modify the vertex shader
|
---|
| 2 |
|
---|
[22b2c37] | 3 | #include "logger.h"
|
---|
[5272b6b] | 4 |
|
---|
[7ee66ea] | 5 | #include <glm/mat4x4.hpp> // glm::mat4
|
---|
| 6 | #include <glm/gtc/matrix_transform.hpp>
|
---|
| 7 | #include <glm/gtc/type_ptr.hpp>
|
---|
| 8 |
|
---|
[5272b6b] | 9 | #include <GL/glew.h>
|
---|
| 10 | #include <GLFW/glfw3.h>
|
---|
| 11 |
|
---|
[22b2c37] | 12 | #include <cstdio>
|
---|
| 13 | #include <iostream>
|
---|
[ec4456b] | 14 | #include <fstream>
|
---|
[7ee66ea] | 15 |
|
---|
| 16 | #define _USE_MATH_DEFINES
|
---|
[93baa0e] | 17 | #include <cmath>
|
---|
[22b2c37] | 18 |
|
---|
[5272b6b] | 19 | using namespace std;
|
---|
[7ee66ea] | 20 | using namespace glm;
|
---|
| 21 |
|
---|
| 22 | #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
|
---|
[5272b6b] | 23 |
|
---|
[ec4456b] | 24 | GLuint loadShader(GLenum type, string file);
|
---|
| 25 |
|
---|
| 26 | const bool FULLSCREEN = false;
|
---|
| 27 |
|
---|
| 28 | void glfw_error_callback(int error, const char* description) {
|
---|
| 29 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[5272b6b] | 32 | int main(int argc, char* argv[]) {
|
---|
| 33 | cout << "New OpenGL Game" << endl;
|
---|
| 34 |
|
---|
[ec4456b] | 35 | if (!restart_gl_log()) {}
|
---|
| 36 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
[22b2c37] | 37 |
|
---|
[ec4456b] | 38 | glfwSetErrorCallback(glfw_error_callback);
|
---|
[5272b6b] | 39 | if (!glfwInit()) {
|
---|
| 40 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
| 41 | return 1;
|
---|
[be246ad] | 42 | }
|
---|
| 43 |
|
---|
| 44 | #ifdef __APPLE__
|
---|
| 45 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 46 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 47 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
| 48 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
| 49 | #endif
|
---|
[5272b6b] | 50 |
|
---|
[ec4456b] | 51 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
| 52 |
|
---|
| 53 | GLFWwindow* window = NULL;
|
---|
| 54 |
|
---|
| 55 | int width = 640;
|
---|
| 56 | int height = 480;
|
---|
| 57 |
|
---|
| 58 | if (FULLSCREEN) {
|
---|
| 59 | GLFWmonitor* mon = glfwGetPrimaryMonitor();
|
---|
| 60 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
| 61 |
|
---|
| 62 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
| 63 | window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
|
---|
| 64 |
|
---|
| 65 | width = vmode->width;
|
---|
| 66 | height = vmode->height;
|
---|
| 67 | } else {
|
---|
| 68 | window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[5272b6b] | 71 | if (!window) {
|
---|
| 72 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
| 73 | glfwTerminate();
|
---|
| 74 | return 1;
|
---|
| 75 | }
|
---|
[644a2e4] | 76 | glfwMakeContextCurrent(window);
|
---|
[5272b6b] | 77 | glewExperimental = GL_TRUE;
|
---|
| 78 | glewInit();
|
---|
| 79 |
|
---|
[ec4456b] | 80 | // glViewport(0, 0, width*2, height*2);
|
---|
| 81 |
|
---|
[5272b6b] | 82 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
| 83 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
| 84 | printf("Renderer: %s\n", renderer);
|
---|
| 85 | printf("OpenGL version supported %s\n", version);
|
---|
[93baa0e] | 86 |
|
---|
[5272b6b] | 87 | glEnable(GL_DEPTH_TEST);
|
---|
| 88 | glDepthFunc(GL_LESS);
|
---|
[516668e] | 89 |
|
---|
[93baa0e] | 90 | glEnable(GL_CULL_FACE);
|
---|
| 91 | // glCullFace(GL_BACK);
|
---|
| 92 | // glFrontFace(GL_CW);
|
---|
| 93 |
|
---|
[516668e] | 94 | GLfloat points[] = {
|
---|
| 95 | 0.0f, 0.5f, 0.0f,
|
---|
| 96 | -0.5f, -0.5f, 0.0f,
|
---|
[93baa0e] | 97 | 0.5f, -0.5f, 0.0f,
|
---|
[7ee66ea] | 98 | 0.5f, -0.5f, 0.0f,
|
---|
| 99 | -0.5f, -0.5f, 0.0f,
|
---|
| 100 | 0.0f, 0.5f, 0.0f,
|
---|
[516668e] | 101 | };
|
---|
| 102 |
|
---|
[8b7cfcf] | 103 | GLfloat colors[] = {
|
---|
| 104 | 1.0, 0.0, 0.0,
|
---|
| 105 | 0.0, 0.0, 1.0,
|
---|
[93baa0e] | 106 | 0.0, 1.0, 0.0,
|
---|
[7ee66ea] | 107 | 0.0, 1.0, 0.0,
|
---|
| 108 | 0.0, 0.0, 1.0,
|
---|
| 109 | 1.0, 0.0, 0.0,
|
---|
[93baa0e] | 110 | };
|
---|
| 111 |
|
---|
[7ee66ea] | 112 | GLfloat model_mat[] = {
|
---|
[93baa0e] | 113 | 1.0f, 0.0f, 0.0f, 0.0f, // column 1
|
---|
| 114 | 0.0f, 1.0f, 0.0f, 0.0f, // column 2
|
---|
| 115 | 0.0f, 0.0f, 1.0f, 0.0f, // column 3
|
---|
| 116 | 0.5f, 0.0f, 0.0f, 1.0f, // column 4
|
---|
[8b7cfcf] | 117 | };
|
---|
| 118 |
|
---|
| 119 | GLuint points_vbo = 0;
|
---|
| 120 | glGenBuffers(1, &points_vbo);
|
---|
| 121 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
[516668e] | 122 | glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
|
---|
| 123 |
|
---|
[8b7cfcf] | 124 | GLuint colors_vbo = 0;
|
---|
| 125 | glGenBuffers(1, &colors_vbo);
|
---|
| 126 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
| 127 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
|
---|
| 128 |
|
---|
[644a2e4] | 129 | GLuint vao = 0;
|
---|
[516668e] | 130 | glGenVertexArrays(1, &vao);
|
---|
| 131 | glBindVertexArray(vao);
|
---|
[8b7cfcf] | 132 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
[516668e] | 133 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
[8b7cfcf] | 134 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
| 135 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
[516668e] | 136 |
|
---|
[8b7cfcf] | 137 | glEnableVertexAttribArray(0);
|
---|
| 138 | glEnableVertexAttribArray(1);
|
---|
[644a2e4] | 139 |
|
---|
[ec4456b] | 140 | GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
|
---|
| 141 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
|
---|
[644a2e4] | 142 |
|
---|
| 143 | GLuint shader_program = glCreateProgram();
|
---|
| 144 | glAttachShader(shader_program, vs);
|
---|
| 145 | glAttachShader(shader_program, fs);
|
---|
[8b7cfcf] | 146 |
|
---|
[644a2e4] | 147 | glLinkProgram(shader_program);
|
---|
| 148 |
|
---|
[93baa0e] | 149 | float speed = 1.0f;
|
---|
| 150 | float last_position = 0.0f;
|
---|
| 151 |
|
---|
[7ee66ea] | 152 | float cam_speed = 1.0f;
|
---|
| 153 | float cam_yaw_speed = 30.0f*ONE_DEG_IN_RAD;
|
---|
| 154 |
|
---|
| 155 | float cam_pos[] = {0.0f, 0.0f, 2.0f};
|
---|
| 156 | float cam_yaw = 0.0f;
|
---|
| 157 |
|
---|
| 158 | mat4 T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
|
---|
| 159 | mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
| 160 | mat4 view_mat = R*T;
|
---|
| 161 |
|
---|
| 162 | float near = 0.1f;
|
---|
| 163 | float far = 100.0f;
|
---|
| 164 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
---|
| 165 | float aspect = (float)width / (float)height;
|
---|
| 166 |
|
---|
| 167 | float range = tan(fov * 0.5f) * near;
|
---|
| 168 | float Sx = near / (range * aspect);
|
---|
| 169 | float Sy = near / range;
|
---|
| 170 | float Sz = -(far + near) / (far - near);
|
---|
| 171 | float Pz = -(2.0f * far * near) / (far - near);
|
---|
| 172 |
|
---|
| 173 | float proj_mat[] = {
|
---|
| 174 | Sx, 0.0f, 0.0f, 0.0f,
|
---|
| 175 | 0.0f, Sy, 0.0f, 0.0f,
|
---|
| 176 | 0.0f, 0.0f, Sz, -1.0f,
|
---|
| 177 | 0.0f, 0.0f, Pz, 0.0f,
|
---|
| 178 | };
|
---|
| 179 |
|
---|
| 180 | GLint model_mat_loc = glGetUniformLocation(shader_program, "model");
|
---|
| 181 | GLint view_mat_loc = glGetUniformLocation(shader_program, "view");
|
---|
| 182 | GLint proj_mat_loc = glGetUniformLocation(shader_program, "proj");
|
---|
| 183 |
|
---|
| 184 | glUseProgram(shader_program);
|
---|
| 185 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, model_mat);
|
---|
| 186 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
| 187 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, proj_mat);
|
---|
| 188 |
|
---|
| 189 | bool cam_moved = false;
|
---|
| 190 |
|
---|
[93baa0e] | 191 | double previous_seconds = glfwGetTime();
|
---|
[644a2e4] | 192 | while (!glfwWindowShouldClose(window)) {
|
---|
[93baa0e] | 193 | double current_seconds = glfwGetTime();
|
---|
| 194 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
| 195 | previous_seconds = current_seconds;
|
---|
| 196 |
|
---|
| 197 | if (fabs(last_position) > 1.0f) {
|
---|
| 198 | speed = -speed;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[7ee66ea] | 201 | /*
|
---|
[93baa0e] | 202 | model[12] = last_position + speed*elapsed_seconds;
|
---|
| 203 | last_position = model[12];
|
---|
[7ee66ea] | 204 | */
|
---|
[93baa0e] | 205 |
|
---|
| 206 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
[644a2e4] | 207 | glBindVertexArray(vao);
|
---|
[93baa0e] | 208 |
|
---|
[7ee66ea] | 209 | // Each point is made of 3 floats
|
---|
| 210 | int numPoints = (sizeof(points) / sizeof(float)) / 3;
|
---|
| 211 | glDrawArrays(GL_TRIANGLES, 0, numPoints);
|
---|
[ec4456b] | 212 |
|
---|
[644a2e4] | 213 | glfwPollEvents();
|
---|
| 214 | glfwSwapBuffers(window);
|
---|
[ec4456b] | 215 |
|
---|
| 216 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
| 217 | glfwSetWindowShouldClose(window, 1);
|
---|
| 218 | }
|
---|
[7ee66ea] | 219 |
|
---|
| 220 | float dist = cam_speed * elapsed_seconds;
|
---|
| 221 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
---|
| 222 | cam_pos[0] -= cos(cam_yaw*ONE_DEG_IN_RAD)*dist;
|
---|
| 223 | cam_pos[2] += sin(cam_yaw*ONE_DEG_IN_RAD)*dist;
|
---|
| 224 | cam_moved = true;
|
---|
| 225 | }
|
---|
| 226 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
---|
| 227 | cam_pos[0] += cos(cam_yaw)*dist;
|
---|
| 228 | cam_pos[2] -= sin(cam_yaw)*dist;
|
---|
| 229 | cam_moved = true;
|
---|
| 230 | }
|
---|
| 231 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
---|
| 232 | cam_pos[0] -= sin(cam_yaw)*dist;
|
---|
| 233 | cam_pos[2] -= cos(cam_yaw)*dist;
|
---|
| 234 | cam_moved = true;
|
---|
| 235 | }
|
---|
| 236 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
---|
| 237 | cam_pos[0] += sin(cam_yaw)*dist;
|
---|
| 238 | cam_pos[2] += cos(cam_yaw)*dist;
|
---|
| 239 | cam_moved = true;
|
---|
| 240 | }
|
---|
| 241 | if (glfwGetKey(window, GLFW_KEY_LEFT)) {
|
---|
| 242 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
---|
| 243 | cam_moved = true;
|
---|
| 244 | }
|
---|
| 245 | if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
|
---|
| 246 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
---|
| 247 | cam_moved = true;
|
---|
| 248 | }
|
---|
| 249 | if (cam_moved) {
|
---|
| 250 | T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
|
---|
| 251 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
| 252 | view_mat = R*T;
|
---|
| 253 |
|
---|
| 254 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
| 255 | cam_moved = false;
|
---|
| 256 | }
|
---|
[644a2e4] | 257 | }
|
---|
| 258 |
|
---|
[5272b6b] | 259 | glfwTerminate();
|
---|
| 260 | return 0;
|
---|
| 261 | }
|
---|
[ec4456b] | 262 |
|
---|
| 263 | GLuint loadShader(GLenum type, string file) {
|
---|
| 264 | cout << "Loading shader from file " << file << endl;
|
---|
| 265 |
|
---|
| 266 | ifstream shaderFile(file);
|
---|
| 267 | GLuint shaderId = 0;
|
---|
| 268 |
|
---|
| 269 | if (shaderFile.is_open()) {
|
---|
| 270 | string line, shaderString;
|
---|
| 271 |
|
---|
| 272 | while(getline(shaderFile, line)) {
|
---|
| 273 | shaderString += line + "\n";
|
---|
| 274 | }
|
---|
| 275 | shaderFile.close();
|
---|
| 276 | const char* shaderCString = shaderString.c_str();
|
---|
| 277 |
|
---|
| 278 | shaderId = glCreateShader(type);
|
---|
| 279 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
| 280 | glCompileShader(shaderId);
|
---|
| 281 |
|
---|
| 282 | cout << "Loaded successfully" << endl;
|
---|
| 283 | } else {
|
---|
| 284 | cout << "Failed to loade the file" << endl;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | return shaderId;
|
---|
| 288 | }
|
---|