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 |
|
---|
10 | using namespace std;
|
---|
11 |
|
---|
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 |
|
---|
20 | int main(int argc, char* argv[]) {
|
---|
21 | cout << "New OpenGL Game" << endl;
|
---|
22 |
|
---|
23 | if (!restart_gl_log()) {}
|
---|
24 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
25 |
|
---|
26 | glfwSetErrorCallback(glfw_error_callback);
|
---|
27 | if (!glfwInit()) {
|
---|
28 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
29 | return 1;
|
---|
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
|
---|
38 |
|
---|
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 |
|
---|
59 | if (!window) {
|
---|
60 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
61 | glfwTerminate();
|
---|
62 | return 1;
|
---|
63 | }
|
---|
64 | glfwMakeContextCurrent(window);
|
---|
65 | glewExperimental = GL_TRUE;
|
---|
66 | glewInit();
|
---|
67 |
|
---|
68 | // glViewport(0, 0, width*2, height*2);
|
---|
69 |
|
---|
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);
|
---|
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 |
|
---|
88 | GLuint vao = 0;
|
---|
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 |
|
---|
95 | //string vertexShaderString = loadShader();
|
---|
96 |
|
---|
97 | GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
|
---|
98 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
|
---|
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);
|
---|
110 |
|
---|
111 | glfwPollEvents();
|
---|
112 | glfwSwapBuffers(window);
|
---|
113 |
|
---|
114 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
115 | glfwSetWindowShouldClose(window, 1);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | glfwTerminate();
|
---|
120 | return 0;
|
---|
121 | }
|
---|
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 | }
|
---|