[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[] = {
|
---|
[eeece4e] | 98 | -1.0f, 1.0f, 0.0f,
|
---|
| 99 | -1.0f, 0.7f, 0.0f,
|
---|
| 100 | -0.9f, 0.7f, 0.0f,
|
---|
| 101 | -1.0f, 1.0f, 0.0f,
|
---|
| 102 | -0.9f, 0.7f, 0.0f,
|
---|
| 103 | -0.9f, 1.0f, 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;
|
---|
| 144 |
|
---|
| 145 | double previous_seconds = glfwGetTime();
|
---|
| 146 | while (!glfwWindowShouldClose(window)) {
|
---|
| 147 | double current_seconds = glfwGetTime();
|
---|
| 148 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
| 149 | previous_seconds = current_seconds;
|
---|
| 150 |
|
---|
| 151 | if (fabs(last_position) > 1.0f) {
|
---|
| 152 | speed = -speed;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 156 |
|
---|
[eeece4e] | 157 | model[12] = 0.0f;
|
---|
| 158 | glUniformMatrix4fv(location, 1, GL_FALSE, model);
|
---|
| 159 |
|
---|
| 160 | glBindVertexArray(paddle_vao);
|
---|
[9e81839] | 161 | glEnableVertexAttribArray(0);
|
---|
| 162 | glEnableVertexAttribArray(1);
|
---|
| 163 |
|
---|
[eeece4e] | 164 | glDrawArrays(GL_TRIANGLES, 0, sizeof(points_paddle)/sizeof(GLfloat)/3);
|
---|
[9e81839] | 165 |
|
---|
[eeece4e] | 166 | model[12] = last_position + speed*elapsed_seconds;
|
---|
| 167 | last_position = model[12];
|
---|
| 168 | glUniformMatrix4fv(location, 1, GL_FALSE, model);
|
---|
| 169 |
|
---|
| 170 | glBindVertexArray(ball_vao);
|
---|
[9e81839] | 171 | glEnableVertexAttribArray(0);
|
---|
| 172 | glEnableVertexAttribArray(1);
|
---|
| 173 |
|
---|
[eeece4e] | 174 | glDrawArrays(GL_TRIANGLES, 0, sizeof(points)/sizeof(GLfloat)/3);
|
---|
[9e81839] | 175 |
|
---|
| 176 | glfwPollEvents();
|
---|
| 177 | glfwSwapBuffers(window);
|
---|
| 178 |
|
---|
| 179 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
| 180 | glfwSetWindowShouldClose(window, 1);
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | glfwTerminate();
|
---|
| 185 | return 0;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | GLuint loadShader(GLenum type, string file) {
|
---|
| 189 | cout << "Loading shader from file " << file << endl;
|
---|
| 190 |
|
---|
| 191 | ifstream shaderFile(file);
|
---|
| 192 | GLuint shaderId = 0;
|
---|
| 193 |
|
---|
| 194 | if (shaderFile.is_open()) {
|
---|
| 195 | string line, shaderString;
|
---|
| 196 |
|
---|
| 197 | while(getline(shaderFile, line)) {
|
---|
| 198 | shaderString += line + "\n";
|
---|
| 199 | }
|
---|
| 200 | shaderFile.close();
|
---|
| 201 | const char* shaderCString = shaderString.c_str();
|
---|
| 202 |
|
---|
| 203 | shaderId = glCreateShader(type);
|
---|
| 204 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
| 205 | glCompileShader(shaderId);
|
---|
| 206 |
|
---|
| 207 | cout << "Loaded successfully" << endl;
|
---|
| 208 | } else {
|
---|
| 209 | cout << "Failed to loade the file" << endl;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | return shaderId;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | GLuint createDataBuffer(size_t size, GLfloat* data) {
|
---|
| 216 | GLuint vbo = 0;
|
---|
| 217 | glGenBuffers(1, &vbo);
|
---|
| 218 | glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
---|
| 219 | glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
|
---|
| 220 | return vbo;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | GLuint createArrayBuffer(GLuint points_vbo, GLuint colors_vbo) {
|
---|
| 224 | GLuint vao = 0;
|
---|
| 225 | glGenVertexArrays(1, &vao);
|
---|
| 226 | glBindVertexArray(vao);
|
---|
| 227 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
| 228 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
| 229 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
| 230 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
| 231 | return vao;
|
---|
| 232 | }
|
---|