1 | // NEXT STEP; Modify the vertex shader
|
---|
2 |
|
---|
3 | #include "logger.h"
|
---|
4 |
|
---|
5 | #include <GL/glew.h>
|
---|
6 | #include <GLFW/glfw3.h>
|
---|
7 |
|
---|
8 | #include <cstdio>
|
---|
9 | #include <iostream>
|
---|
10 | #include <fstream>
|
---|
11 | #include <cmath>
|
---|
12 |
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 | GLuint loadShader(GLenum type, string file);
|
---|
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.0f,
|
---|
87 | -0.5f, -0.5f, 0.0f,
|
---|
88 | 0.5f, -0.5f, 0.0f,
|
---|
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 model[] = {
|
---|
98 | 1.0f, 0.0f, 0.0f, 0.0f, // column 1
|
---|
99 | 0.0f, 1.0f, 0.0f, 0.0f, // column 2
|
---|
100 | 0.0f, 0.0f, 1.0f, 0.0f, // column 3
|
---|
101 | 0.5f, 0.0f, 0.0f, 1.0f, // column 4
|
---|
102 | };
|
---|
103 |
|
---|
104 | GLuint points_vbo = 0;
|
---|
105 | glGenBuffers(1, &points_vbo);
|
---|
106 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
107 | glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
|
---|
108 |
|
---|
109 | GLuint colors_vbo = 0;
|
---|
110 | glGenBuffers(1, &colors_vbo);
|
---|
111 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
112 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
|
---|
113 |
|
---|
114 | GLuint vao = 0;
|
---|
115 | glGenVertexArrays(1, &vao);
|
---|
116 | glBindVertexArray(vao);
|
---|
117 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
118 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
119 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
120 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
121 |
|
---|
122 | glEnableVertexAttribArray(0);
|
---|
123 | glEnableVertexAttribArray(1);
|
---|
124 |
|
---|
125 | GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
|
---|
126 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
|
---|
127 |
|
---|
128 | GLuint shader_program = glCreateProgram();
|
---|
129 | glAttachShader(shader_program, vs);
|
---|
130 | glAttachShader(shader_program, fs);
|
---|
131 |
|
---|
132 | glLinkProgram(shader_program);
|
---|
133 |
|
---|
134 | GLint location = glGetUniformLocation(shader_program, "model");
|
---|
135 |
|
---|
136 | float speed = 1.0f;
|
---|
137 | float last_position = 0.0f;
|
---|
138 |
|
---|
139 | double previous_seconds = glfwGetTime();
|
---|
140 | while (!glfwWindowShouldClose(window)) {
|
---|
141 | double current_seconds = glfwGetTime();
|
---|
142 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
143 | previous_seconds = current_seconds;
|
---|
144 |
|
---|
145 | if (fabs(last_position) > 1.0f) {
|
---|
146 | speed = -speed;
|
---|
147 | }
|
---|
148 |
|
---|
149 | model[12] = last_position + speed*elapsed_seconds;
|
---|
150 | last_position = model[12];
|
---|
151 | glUseProgram(shader_program);
|
---|
152 | glUniformMatrix4fv(location, 1, GL_FALSE, model);
|
---|
153 |
|
---|
154 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
155 | glBindVertexArray(vao);
|
---|
156 |
|
---|
157 | glDrawArrays(GL_TRIANGLES, 0, 3);
|
---|
158 |
|
---|
159 | glfwPollEvents();
|
---|
160 | glfwSwapBuffers(window);
|
---|
161 |
|
---|
162 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
163 | glfwSetWindowShouldClose(window, 1);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | glfwTerminate();
|
---|
168 | return 0;
|
---|
169 | }
|
---|
170 |
|
---|
171 | GLuint loadShader(GLenum type, string file) {
|
---|
172 | cout << "Loading shader from file " << file << endl;
|
---|
173 |
|
---|
174 | ifstream shaderFile(file);
|
---|
175 | GLuint shaderId = 0;
|
---|
176 |
|
---|
177 | if (shaderFile.is_open()) {
|
---|
178 | string line, shaderString;
|
---|
179 |
|
---|
180 | while(getline(shaderFile, line)) {
|
---|
181 | shaderString += line + "\n";
|
---|
182 | }
|
---|
183 | shaderFile.close();
|
---|
184 | const char* shaderCString = shaderString.c_str();
|
---|
185 |
|
---|
186 | shaderId = glCreateShader(type);
|
---|
187 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
188 | glCompileShader(shaderId);
|
---|
189 |
|
---|
190 | cout << "Loaded successfully" << endl;
|
---|
191 | } else {
|
---|
192 | cout << "Failed to loade the file" << endl;
|
---|
193 | }
|
---|
194 |
|
---|
195 | return shaderId;
|
---|
196 | }
|
---|