| 1 | // NEXT STEP; Modify the vertex shader
|
|---|
| 2 |
|
|---|
| 3 | #include "logger.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 |
|
|---|
| 19 | using namespace std;
|
|---|
| 20 | using namespace glm;
|
|---|
| 21 |
|
|---|
| 22 | #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
|
|---|
| 23 |
|
|---|
| 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 |
|
|---|
| 32 | int main(int argc, char* argv[]) {
|
|---|
| 33 | cout << "New OpenGL Game" << endl;
|
|---|
| 34 |
|
|---|
| 35 | if (!restart_gl_log()) {}
|
|---|
| 36 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
|---|
| 37 |
|
|---|
| 38 | glfwSetErrorCallback(glfw_error_callback);
|
|---|
| 39 | if (!glfwInit()) {
|
|---|
| 40 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
|---|
| 41 | return 1;
|
|---|
| 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
|
|---|
| 50 |
|
|---|
| 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 |
|
|---|
| 71 | if (!window) {
|
|---|
| 72 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
|---|
| 73 | glfwTerminate();
|
|---|
| 74 | return 1;
|
|---|
| 75 | }
|
|---|
| 76 | glfwMakeContextCurrent(window);
|
|---|
| 77 | glewExperimental = GL_TRUE;
|
|---|
| 78 | glewInit();
|
|---|
| 79 |
|
|---|
| 80 | // glViewport(0, 0, width*2, height*2);
|
|---|
| 81 |
|
|---|
| 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);
|
|---|
| 86 |
|
|---|
| 87 | glEnable(GL_DEPTH_TEST);
|
|---|
| 88 | glDepthFunc(GL_LESS);
|
|---|
| 89 |
|
|---|
| 90 | glEnable(GL_CULL_FACE);
|
|---|
| 91 | // glCullFace(GL_BACK);
|
|---|
| 92 | // glFrontFace(GL_CW);
|
|---|
| 93 |
|
|---|
| 94 | GLfloat points[] = {
|
|---|
| 95 | 0.0f, 0.5f, 0.0f,
|
|---|
| 96 | -0.5f, -0.5f, 0.0f,
|
|---|
| 97 | 0.5f, -0.5f, 0.0f,
|
|---|
| 98 | 0.5f, -0.5f, 0.0f,
|
|---|
| 99 | -0.5f, -0.5f, 0.0f,
|
|---|
| 100 | 0.0f, 0.5f, 0.0f,
|
|---|
| 101 | };
|
|---|
| 102 |
|
|---|
| 103 | GLfloat colors[] = {
|
|---|
| 104 | 1.0, 0.0, 0.0,
|
|---|
| 105 | 0.0, 0.0, 1.0,
|
|---|
| 106 | 0.0, 1.0, 0.0,
|
|---|
| 107 | 0.0, 1.0, 0.0,
|
|---|
| 108 | 0.0, 0.0, 1.0,
|
|---|
| 109 | 1.0, 0.0, 0.0,
|
|---|
| 110 | };
|
|---|
| 111 |
|
|---|
| 112 | mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
|
|---|
| 113 | mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
|
|---|
| 114 | mat4 model_mat = T_model*R_model;
|
|---|
| 115 |
|
|---|
| 116 | GLuint points_vbo = 0;
|
|---|
| 117 | glGenBuffers(1, &points_vbo);
|
|---|
| 118 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
|---|
| 119 | glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
|
|---|
| 120 |
|
|---|
| 121 | GLuint colors_vbo = 0;
|
|---|
| 122 | glGenBuffers(1, &colors_vbo);
|
|---|
| 123 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
|---|
| 124 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
|
|---|
| 125 |
|
|---|
| 126 | GLuint vao = 0;
|
|---|
| 127 | glGenVertexArrays(1, &vao);
|
|---|
| 128 | glBindVertexArray(vao);
|
|---|
| 129 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
|---|
| 130 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
|---|
| 131 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
|---|
| 132 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
|---|
| 133 |
|
|---|
| 134 | glEnableVertexAttribArray(0);
|
|---|
| 135 | glEnableVertexAttribArray(1);
|
|---|
| 136 |
|
|---|
| 137 | GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
|
|---|
| 138 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
|
|---|
| 139 |
|
|---|
| 140 | GLuint shader_program = glCreateProgram();
|
|---|
| 141 | glAttachShader(shader_program, vs);
|
|---|
| 142 | glAttachShader(shader_program, fs);
|
|---|
| 143 |
|
|---|
| 144 | glLinkProgram(shader_program);
|
|---|
| 145 |
|
|---|
| 146 | float speed = 1.0f;
|
|---|
| 147 | float last_position = 0.0f;
|
|---|
| 148 |
|
|---|
| 149 | float cam_speed = 1.0f;
|
|---|
| 150 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
|---|
| 151 |
|
|---|
| 152 | float cam_pos[] = {0.0f, 0.0f, 2.0f};
|
|---|
| 153 | float cam_yaw = 0.0f;
|
|---|
| 154 |
|
|---|
| 155 | mat4 T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
|
|---|
| 156 | mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
|---|
| 157 | mat4 view_mat = R*T;
|
|---|
| 158 |
|
|---|
| 159 | float near = 0.1f;
|
|---|
| 160 | float far = 100.0f;
|
|---|
| 161 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
|---|
| 162 | float aspect = (float)width / (float)height;
|
|---|
| 163 |
|
|---|
| 164 | float range = tan(fov * 0.5f) * near;
|
|---|
| 165 | float Sx = near / (range * aspect);
|
|---|
| 166 | float Sy = near / range;
|
|---|
| 167 | float Sz = -(far + near) / (far - near);
|
|---|
| 168 | float Pz = -(2.0f * far * near) / (far - near);
|
|---|
| 169 |
|
|---|
| 170 | float proj_mat[] = {
|
|---|
| 171 | Sx, 0.0f, 0.0f, 0.0f,
|
|---|
| 172 | 0.0f, Sy, 0.0f, 0.0f,
|
|---|
| 173 | 0.0f, 0.0f, Sz, -1.0f,
|
|---|
| 174 | 0.0f, 0.0f, Pz, 0.0f,
|
|---|
| 175 | };
|
|---|
| 176 |
|
|---|
| 177 | GLint model_mat_loc = glGetUniformLocation(shader_program, "model");
|
|---|
| 178 | GLint view_mat_loc = glGetUniformLocation(shader_program, "view");
|
|---|
| 179 | GLint proj_mat_loc = glGetUniformLocation(shader_program, "proj");
|
|---|
| 180 |
|
|---|
| 181 | glUseProgram(shader_program);
|
|---|
| 182 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat));
|
|---|
| 183 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
|---|
| 184 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, proj_mat);
|
|---|
| 185 |
|
|---|
| 186 | bool cam_moved = false;
|
|---|
| 187 |
|
|---|
| 188 | double previous_seconds = glfwGetTime();
|
|---|
| 189 | while (!glfwWindowShouldClose(window)) {
|
|---|
| 190 | double current_seconds = glfwGetTime();
|
|---|
| 191 | double elapsed_seconds = current_seconds - previous_seconds;
|
|---|
| 192 | previous_seconds = current_seconds;
|
|---|
| 193 |
|
|---|
| 194 | if (fabs(last_position) > 1.0f) {
|
|---|
| 195 | speed = -speed;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /*
|
|---|
| 199 | model[12] = last_position + speed*elapsed_seconds;
|
|---|
| 200 | last_position = model[12];
|
|---|
| 201 | */
|
|---|
| 202 |
|
|---|
| 203 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|---|
| 204 | glBindVertexArray(vao);
|
|---|
| 205 |
|
|---|
| 206 | // Each point is made of 3 floats
|
|---|
| 207 | int numPoints = (sizeof(points) / sizeof(float)) / 3;
|
|---|
| 208 | glDrawArrays(GL_TRIANGLES, 0, numPoints);
|
|---|
| 209 |
|
|---|
| 210 | glfwPollEvents();
|
|---|
| 211 | glfwSwapBuffers(window);
|
|---|
| 212 |
|
|---|
| 213 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
|---|
| 214 | glfwSetWindowShouldClose(window, 1);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | float dist = cam_speed * elapsed_seconds;
|
|---|
| 218 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
|---|
| 219 | cam_pos[0] -= cos(cam_yaw)*dist;
|
|---|
| 220 | cam_pos[2] += sin(cam_yaw)*dist;
|
|---|
| 221 | cam_moved = true;
|
|---|
| 222 | }
|
|---|
| 223 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
|---|
| 224 | cam_pos[0] += cos(cam_yaw)*dist;
|
|---|
| 225 | cam_pos[2] -= sin(cam_yaw)*dist;
|
|---|
| 226 | cam_moved = true;
|
|---|
| 227 | }
|
|---|
| 228 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
|---|
| 229 | cam_pos[0] -= sin(cam_yaw)*dist;
|
|---|
| 230 | cam_pos[2] -= cos(cam_yaw)*dist;
|
|---|
| 231 | cam_moved = true;
|
|---|
| 232 | }
|
|---|
| 233 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
|---|
| 234 | cam_pos[0] += sin(cam_yaw)*dist;
|
|---|
| 235 | cam_pos[2] += cos(cam_yaw)*dist;
|
|---|
| 236 | cam_moved = true;
|
|---|
| 237 | }
|
|---|
| 238 | if (glfwGetKey(window, GLFW_KEY_LEFT)) {
|
|---|
| 239 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
|---|
| 240 | cam_moved = true;
|
|---|
| 241 | }
|
|---|
| 242 | if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
|
|---|
| 243 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
|---|
| 244 | cam_moved = true;
|
|---|
| 245 | }
|
|---|
| 246 | if (cam_moved) {
|
|---|
| 247 | T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2]));
|
|---|
| 248 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
|---|
| 249 | view_mat = R*T;
|
|---|
| 250 |
|
|---|
| 251 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
|---|
| 252 | cam_moved = false;
|
|---|
| 253 | }
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | glfwTerminate();
|
|---|
| 257 | return 0;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | GLuint loadShader(GLenum type, string file) {
|
|---|
| 261 | cout << "Loading shader from file " << file << endl;
|
|---|
| 262 |
|
|---|
| 263 | ifstream shaderFile(file);
|
|---|
| 264 | GLuint shaderId = 0;
|
|---|
| 265 |
|
|---|
| 266 | if (shaderFile.is_open()) {
|
|---|
| 267 | string line, shaderString;
|
|---|
| 268 |
|
|---|
| 269 | while(getline(shaderFile, line)) {
|
|---|
| 270 | shaderString += line + "\n";
|
|---|
| 271 | }
|
|---|
| 272 | shaderFile.close();
|
|---|
| 273 | const char* shaderCString = shaderString.c_str();
|
|---|
| 274 |
|
|---|
| 275 | shaderId = glCreateShader(type);
|
|---|
| 276 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
|---|
| 277 | glCompileShader(shaderId);
|
|---|
| 278 |
|
|---|
| 279 | cout << "Loaded successfully" << endl;
|
|---|
| 280 | } else {
|
|---|
| 281 | cout << "Failed to loade the file" << endl;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | return shaderId;
|
|---|
| 285 | }
|
|---|