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