[22b2c37] | 1 | #include "logger.h"
|
---|
[5272b6b] | 2 |
|
---|
| 3 | #include <GL/glew.h>
|
---|
| 4 | #include <GLFW/glfw3.h>
|
---|
| 5 |
|
---|
[22b2c37] | 6 | #include <cstdio>
|
---|
| 7 | #include <iostream>
|
---|
[ec4456b] | 8 | #include <fstream>
|
---|
[22b2c37] | 9 |
|
---|
[5272b6b] | 10 | using namespace std;
|
---|
| 11 |
|
---|
[ec4456b] | 12 | GLuint loadShader(GLenum type, string file);
|
---|
| 13 |
|
---|
| 14 | const bool FULLSCREEN = false;
|
---|
| 15 |
|
---|
| 16 | void glfw_error_callback(int error, const char* description) {
|
---|
| 17 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
| 18 | }
|
---|
| 19 |
|
---|
[5272b6b] | 20 | int main(int argc, char* argv[]) {
|
---|
| 21 | cout << "New OpenGL Game" << endl;
|
---|
| 22 |
|
---|
[ec4456b] | 23 | if (!restart_gl_log()) {}
|
---|
| 24 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
[22b2c37] | 25 |
|
---|
[ec4456b] | 26 | glfwSetErrorCallback(glfw_error_callback);
|
---|
[5272b6b] | 27 | if (!glfwInit()) {
|
---|
| 28 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
| 29 | return 1;
|
---|
[be246ad] | 30 | }
|
---|
| 31 |
|
---|
| 32 | #ifdef __APPLE__
|
---|
| 33 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 34 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 35 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
| 36 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
| 37 | #endif
|
---|
[5272b6b] | 38 |
|
---|
[ec4456b] | 39 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
| 40 |
|
---|
| 41 | GLFWwindow* window = NULL;
|
---|
| 42 |
|
---|
| 43 | int width = 640;
|
---|
| 44 | int height = 480;
|
---|
| 45 |
|
---|
| 46 | if (FULLSCREEN) {
|
---|
| 47 | GLFWmonitor* mon = glfwGetPrimaryMonitor();
|
---|
| 48 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
| 49 |
|
---|
| 50 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
| 51 | window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
|
---|
| 52 |
|
---|
| 53 | width = vmode->width;
|
---|
| 54 | height = vmode->height;
|
---|
| 55 | } else {
|
---|
| 56 | window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[5272b6b] | 59 | if (!window) {
|
---|
| 60 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
| 61 | glfwTerminate();
|
---|
| 62 | return 1;
|
---|
| 63 | }
|
---|
[644a2e4] | 64 | glfwMakeContextCurrent(window);
|
---|
[5272b6b] | 65 | glewExperimental = GL_TRUE;
|
---|
| 66 | glewInit();
|
---|
| 67 |
|
---|
[ec4456b] | 68 | // glViewport(0, 0, width*2, height*2);
|
---|
| 69 |
|
---|
[5272b6b] | 70 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
| 71 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
| 72 | printf("Renderer: %s\n", renderer);
|
---|
| 73 | printf("OpenGL version supported %s\n", version);
|
---|
| 74 | glEnable(GL_DEPTH_TEST);
|
---|
| 75 | glDepthFunc(GL_LESS);
|
---|
[516668e] | 76 |
|
---|
| 77 | GLfloat points[] = {
|
---|
| 78 | 0.0f, 0.5f, 0.0f,
|
---|
| 79 | 0.5f, -0.5f, 0.0f,
|
---|
| 80 | -0.5f, -0.5f, 0.0f,
|
---|
| 81 | };
|
---|
| 82 |
|
---|
| 83 | GLuint vbo = 0;
|
---|
| 84 | glGenBuffers(1, &vbo);
|
---|
| 85 | glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
---|
| 86 | glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
|
---|
| 87 |
|
---|
[644a2e4] | 88 | GLuint vao = 0;
|
---|
[516668e] | 89 | glGenVertexArrays(1, &vao);
|
---|
| 90 | glBindVertexArray(vao);
|
---|
| 91 | glEnableVertexAttribArray(0);
|
---|
| 92 | glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
---|
| 93 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
| 94 |
|
---|
[ec4456b] | 95 | //string vertexShaderString = loadShader();
|
---|
[644a2e4] | 96 |
|
---|
[ec4456b] | 97 | GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
|
---|
| 98 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
|
---|
[644a2e4] | 99 |
|
---|
| 100 | GLuint shader_program = glCreateProgram();
|
---|
| 101 | glAttachShader(shader_program, vs);
|
---|
| 102 | glAttachShader(shader_program, fs);
|
---|
| 103 | glLinkProgram(shader_program);
|
---|
| 104 |
|
---|
| 105 | while (!glfwWindowShouldClose(window)) {
|
---|
| 106 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 107 | glUseProgram(shader_program);
|
---|
| 108 | glBindVertexArray(vao);
|
---|
| 109 | glDrawArrays(GL_TRIANGLES, 0, 3);
|
---|
[ec4456b] | 110 |
|
---|
[644a2e4] | 111 | glfwPollEvents();
|
---|
| 112 | glfwSwapBuffers(window);
|
---|
[ec4456b] | 113 |
|
---|
| 114 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
| 115 | glfwSetWindowShouldClose(window, 1);
|
---|
| 116 | }
|
---|
[644a2e4] | 117 | }
|
---|
| 118 |
|
---|
[5272b6b] | 119 | glfwTerminate();
|
---|
| 120 | return 0;
|
---|
| 121 | }
|
---|
[ec4456b] | 122 |
|
---|
| 123 | GLuint loadShader(GLenum type, string file) {
|
---|
| 124 | cout << "Loading shader from file " << file << endl;
|
---|
| 125 |
|
---|
| 126 | ifstream shaderFile(file);
|
---|
| 127 | GLuint shaderId = 0;
|
---|
| 128 |
|
---|
| 129 | if (shaderFile.is_open()) {
|
---|
| 130 | string line, shaderString;
|
---|
| 131 |
|
---|
| 132 | while(getline(shaderFile, line)) {
|
---|
| 133 | shaderString += line + "\n";
|
---|
| 134 | }
|
---|
| 135 | shaderFile.close();
|
---|
| 136 | const char* shaderCString = shaderString.c_str();
|
---|
| 137 |
|
---|
| 138 | shaderId = glCreateShader(type);
|
---|
| 139 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
| 140 | glCompileShader(shaderId);
|
---|
| 141 |
|
---|
| 142 | cout << "Loaded successfully" << endl;
|
---|
| 143 | } else {
|
---|
| 144 | cout << "Failed to loade the file" << endl;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | return shaderId;
|
---|
| 148 | }
|
---|