[9e81839] | 1 | #include "logger.h"
|
---|
| 2 |
|
---|
| 3 | #include <GL/glew.h>
|
---|
| 4 | #include <GLFW/glfw3.h>
|
---|
| 5 |
|
---|
| 6 | #include <cstdio>
|
---|
| 7 | #include <iostream>
|
---|
| 8 | #include <fstream>
|
---|
| 9 | #include <cmath>
|
---|
| 10 |
|
---|
| 11 | using namespace std;
|
---|
| 12 |
|
---|
| 13 | GLuint loadShader(GLenum type, string file);
|
---|
| 14 | GLuint createDataBuffer(size_t size, GLfloat* data);
|
---|
| 15 | GLuint createArrayBuffer(GLuint points_vbo, GLuint colors_vbo);
|
---|
| 16 |
|
---|
| 17 | const bool FULLSCREEN = false;
|
---|
| 18 |
|
---|
| 19 | void glfw_error_callback(int error, const char* description) {
|
---|
| 20 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | int main(int argc, char* argv[]) {
|
---|
| 24 | cout << "New OpenGL Game" << endl;
|
---|
| 25 |
|
---|
| 26 | if (!restart_gl_log()) {}
|
---|
| 27 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
| 28 |
|
---|
| 29 | glfwSetErrorCallback(glfw_error_callback);
|
---|
| 30 | if (!glfwInit()) {
|
---|
| 31 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
| 32 | return 1;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | #ifdef __APPLE__
|
---|
| 36 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 37 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 38 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
| 39 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
| 40 | #endif
|
---|
| 41 |
|
---|
| 42 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
| 43 |
|
---|
| 44 | GLFWwindow* window = NULL;
|
---|
| 45 |
|
---|
| 46 | int width = 640;
|
---|
| 47 | int height = 480;
|
---|
| 48 |
|
---|
| 49 | if (FULLSCREEN) {
|
---|
| 50 | GLFWmonitor* mon = glfwGetPrimaryMonitor();
|
---|
| 51 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
| 52 |
|
---|
| 53 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
| 54 | window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
|
---|
| 55 |
|
---|
| 56 | width = vmode->width;
|
---|
| 57 | height = vmode->height;
|
---|
| 58 | } else {
|
---|
| 59 | window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | if (!window) {
|
---|
| 63 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
| 64 | glfwTerminate();
|
---|
| 65 | return 1;
|
---|
| 66 | }
|
---|
| 67 | glfwMakeContextCurrent(window);
|
---|
| 68 | glewExperimental = GL_TRUE;
|
---|
| 69 | glewInit();
|
---|
| 70 |
|
---|
| 71 | // glViewport(0, 0, width*2, height*2);
|
---|
| 72 |
|
---|
| 73 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
| 74 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
| 75 | printf("Renderer: %s\n", renderer);
|
---|
| 76 | printf("OpenGL version supported %s\n", version);
|
---|
| 77 |
|
---|
| 78 | glEnable(GL_DEPTH_TEST);
|
---|
| 79 | glDepthFunc(GL_LESS);
|
---|
| 80 |
|
---|
| 81 | glEnable(GL_CULL_FACE);
|
---|
| 82 | // glCullFace(GL_BACK);
|
---|
| 83 | // glFrontFace(GL_CW);
|
---|
| 84 |
|
---|
| 85 | GLfloat points[] = {
|
---|
| 86 | 0.0f, 0.5f, 0.5f,
|
---|
| 87 | -0.5f, -0.5f, 0.5f,
|
---|
| 88 | 0.5f, -0.5f, 0.5f,
|
---|
| 89 | };
|
---|
| 90 |
|
---|
| 91 | GLfloat colors[] = {
|
---|
| 92 | 1.0, 0.0, 0.0,
|
---|
| 93 | 0.0, 0.0, 1.0,
|
---|
| 94 | 0.0, 1.0, 0.0,
|
---|
| 95 | };
|
---|
| 96 |
|
---|
| 97 | GLfloat points_paddle[] = {
|
---|
[363d5ff] | 98 | -1.0f, 0.15f, 0.0f,
|
---|
| 99 | -1.0f, -0.15f, 0.0f,
|
---|
| 100 | -0.9f, -0.15f, 0.0f,
|
---|
| 101 | -1.0f, 0.15f, 0.0f,
|
---|
| 102 | -0.9f, -0.15f, 0.0f,
|
---|
| 103 | -0.9f, 0.15f, 0.0f,
|
---|
[9e81839] | 104 | };
|
---|
| 105 |
|
---|
| 106 | GLfloat colors_paddle[] = {
|
---|
| 107 | 1.0, 0.0, 0.0,
|
---|
[eeece4e] | 108 | 1.0, 0.0, 0.0,
|
---|
| 109 | 1.0, 0.0, 0.0,
|
---|
| 110 | 1.0, 0.0, 0.0,
|
---|
| 111 | 1.0, 0.0, 0.0,
|
---|
| 112 | 1.0, 0.0, 0.0,
|
---|
[9e81839] | 113 | };
|
---|
| 114 |
|
---|
| 115 | GLfloat model[] = {
|
---|
| 116 | 1.0f, 0.0f, 0.0f, 0.0f, // column 1
|
---|
| 117 | 0.0f, 1.0f, 0.0f, 0.0f, // column 2
|
---|
| 118 | 0.0f, 0.0f, 1.0f, 0.0f, // column 3
|
---|
[eeece4e] | 119 | 0.0f, 0.0f, 0.0f, 1.0f, // column 4
|
---|
[9e81839] | 120 | };
|
---|
| 121 |
|
---|
| 122 | GLuint ball_points_vbo = createDataBuffer(sizeof(points), points);
|
---|
| 123 | GLuint ball_colors_vbo = createDataBuffer(sizeof(colors), colors);
|
---|
| 124 | GLuint ball_vao = createArrayBuffer(ball_points_vbo, ball_colors_vbo);
|
---|
| 125 |
|
---|
| 126 | GLuint paddle_points_vbo = createDataBuffer(sizeof(points_paddle), points_paddle);
|
---|
| 127 | GLuint paddle_colors_vbo = createDataBuffer(sizeof(colors_paddle), colors_paddle);
|
---|
| 128 | GLuint paddle_vao = createArrayBuffer(paddle_points_vbo, paddle_colors_vbo);
|
---|
| 129 |
|
---|
| 130 | GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
|
---|
| 131 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
|
---|
| 132 |
|
---|
| 133 | GLuint shader_program = glCreateProgram();
|
---|
| 134 | glAttachShader(shader_program, vs);
|
---|
| 135 | glAttachShader(shader_program, fs);
|
---|
| 136 |
|
---|
| 137 | glLinkProgram(shader_program);
|
---|
[eeece4e] | 138 | glUseProgram(shader_program);
|
---|
[9e81839] | 139 |
|
---|
| 140 | GLint location = glGetUniformLocation(shader_program, "model");
|
---|
| 141 |
|
---|
| 142 | float speed = 1.0f;
|
---|
| 143 | float last_position = 0.0f;
|
---|
[363d5ff] | 144 | float last_paddle_pos = 0.0f;
|
---|
[9e81839] | 145 |
|
---|
| 146 | double previous_seconds = glfwGetTime();
|
---|
| 147 | while (!glfwWindowShouldClose(window)) {
|
---|
| 148 | double current_seconds = glfwGetTime();
|
---|
| 149 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
| 150 | previous_seconds = current_seconds;
|
---|
| 151 |
|
---|
| 152 | if (fabs(last_position) > 1.0f) {
|
---|
| 153 | speed = -speed;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 157 |
|
---|
[eeece4e] | 158 | model[12] = 0.0f;
|
---|
[363d5ff] | 159 | model[13] = last_paddle_pos;
|
---|
[eeece4e] | 160 | glUniformMatrix4fv(location, 1, GL_FALSE, model);
|
---|
| 161 |
|
---|
| 162 | glBindVertexArray(paddle_vao);
|
---|
[9e81839] | 163 | glEnableVertexAttribArray(0);
|
---|
| 164 | glEnableVertexAttribArray(1);
|
---|
| 165 |
|
---|
[eeece4e] | 166 | glDrawArrays(GL_TRIANGLES, 0, sizeof(points_paddle)/sizeof(GLfloat)/3);
|
---|
[9e81839] | 167 |
|
---|
[eeece4e] | 168 | model[12] = last_position + speed*elapsed_seconds;
|
---|
| 169 | last_position = model[12];
|
---|
[363d5ff] | 170 | model[13] = 0.0f;
|
---|
[eeece4e] | 171 | glUniformMatrix4fv(location, 1, GL_FALSE, model);
|
---|
| 172 |
|
---|
| 173 | glBindVertexArray(ball_vao);
|
---|
[9e81839] | 174 | glEnableVertexAttribArray(0);
|
---|
| 175 | glEnableVertexAttribArray(1);
|
---|
| 176 |
|
---|
[eeece4e] | 177 | glDrawArrays(GL_TRIANGLES, 0, sizeof(points)/sizeof(GLfloat)/3);
|
---|
[9e81839] | 178 |
|
---|
| 179 | glfwPollEvents();
|
---|
| 180 | glfwSwapBuffers(window);
|
---|
| 181 |
|
---|
| 182 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
| 183 | glfwSetWindowShouldClose(window, 1);
|
---|
| 184 | }
|
---|
[363d5ff] | 185 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_UP)) {
|
---|
| 186 | last_paddle_pos += 1.0f * elapsed_seconds;
|
---|
| 187 | if (last_paddle_pos > 0.85f) {
|
---|
| 188 | last_paddle_pos = 0.85f;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_DOWN)) {
|
---|
| 192 | last_paddle_pos -= 1.0f * elapsed_seconds;
|
---|
| 193 | if (last_paddle_pos < -0.85f) {
|
---|
| 194 | last_paddle_pos = -0.85f;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
[9e81839] | 197 | }
|
---|
| 198 |
|
---|
| 199 | glfwTerminate();
|
---|
| 200 | return 0;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | GLuint loadShader(GLenum type, string file) {
|
---|
| 204 | cout << "Loading shader from file " << file << endl;
|
---|
| 205 |
|
---|
| 206 | ifstream shaderFile(file);
|
---|
| 207 | GLuint shaderId = 0;
|
---|
| 208 |
|
---|
| 209 | if (shaderFile.is_open()) {
|
---|
| 210 | string line, shaderString;
|
---|
| 211 |
|
---|
| 212 | while(getline(shaderFile, line)) {
|
---|
| 213 | shaderString += line + "\n";
|
---|
| 214 | }
|
---|
| 215 | shaderFile.close();
|
---|
| 216 | const char* shaderCString = shaderString.c_str();
|
---|
| 217 |
|
---|
| 218 | shaderId = glCreateShader(type);
|
---|
| 219 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
| 220 | glCompileShader(shaderId);
|
---|
| 221 |
|
---|
| 222 | cout << "Loaded successfully" << endl;
|
---|
| 223 | } else {
|
---|
| 224 | cout << "Failed to loade the file" << endl;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | return shaderId;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | GLuint createDataBuffer(size_t size, GLfloat* data) {
|
---|
| 231 | GLuint vbo = 0;
|
---|
| 232 | glGenBuffers(1, &vbo);
|
---|
| 233 | glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
---|
| 234 | glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
|
---|
| 235 | return vbo;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | GLuint createArrayBuffer(GLuint points_vbo, GLuint colors_vbo) {
|
---|
| 239 | GLuint vao = 0;
|
---|
| 240 | glGenVertexArrays(1, &vao);
|
---|
| 241 | glBindVertexArray(vao);
|
---|
| 242 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
| 243 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
| 244 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
| 245 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
| 246 | return vao;
|
---|
| 247 | }
|
---|