source: opengl-game/mygame.cpp@ 0424bd8

feature/imgui-sdl points-test
Last change on this file since 0424bd8 was 0424bd8, checked in by Dmitry Portnoy <dmp1488@…>, 7 years ago

Update the OpenGLGame project to only include mygame.cpp and the files required to run it

  • Property mode set to 100644
File size: 7.9 KB
RevLine 
[8a6d19d]1#include <GL/glew.h>
2#include <GLFW/glfw3.h>
3
4#include <glm/glm.hpp>
5#include <glm/gtc/matrix_transform.hpp>
[0424bd8]6
7#include <cstdio>
8#include <cstdlib>
9#include <iostream>
10
11GLFWwindow* window;
[8a6d19d]12
13#include "common/shader.hpp"
14#include "common/controls.hpp"
15
[0424bd8]16using namespace std;
17using namespace glm;
18
[1f63bdb]19int main(int argc, char* argv[]) {
20 if (!glfwInit()) {
21 cerr << "Failed to initialize GLFW" << endl;;
[8a6d19d]22 getchar();
23 return -1;
24 }
25
26 glfwWindowHint(GLFW_SAMPLES, 4);
27 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
28 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
29 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
30 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
31
[92bc4fe]32 const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
33
34 // Open a window and create its OpenGL context
35 window = glfwCreateWindow(mode->width, mode->height, "My Space Game", glfwGetPrimaryMonitor(), NULL);
[1f63bdb]36 if (window == NULL) {
37 cerr << "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials." << endl;
[8a6d19d]38 getchar();
39 glfwTerminate();
40 return -1;
41 }
42 glfwMakeContextCurrent(window);
[92bc4fe]43 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
[8a6d19d]44
45 // Initialize GLEW
46 glewExperimental = true; // Needed for core profile
47 if (glewInit() != GLEW_OK) {
[1f63bdb]48 cout << "Failed to initialize GLEW\n" << endl;
[8a6d19d]49 getchar();
50 glfwTerminate();
51 return -1;
52 }
53
54 // Ensure we can capture the escape key being pressed below
55 glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
56
57 // Dark blue background
58 glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
59
60 // Enable depth test
61 glEnable(GL_DEPTH_TEST);
62 // Accept fragment if it closer to the camera than the former one
63 glDepthFunc(GL_LESS);
64
65 GLuint VertexArrayID;
66 glGenVertexArrays(1, &VertexArrayID);
67 glBindVertexArray(VertexArrayID);
68
69 // Create and compile our GLSL program from the shaders
70 GLuint programID = LoadShaders( "TransformVertexShader-color.vertexshader", "ColorFragmentShader.fragmentshader" );
71
72 // Get a handle for our "MVP" uniform
73 GLuint MatrixID = glGetUniformLocation(programID, "MVP");
74
75 // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
76 // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
77 static const GLfloat g_vertex_buffer_data[] = {
78 -1.0f, 0.0f,-1.0f,
79 -1.0f, 0.0f, 1.0f,
80 -1.0f, 2.0f, 1.0f,
81 1.0f, 2.0f,-1.0f,
82 -1.0f, 0.0f,-1.0f,
83 -1.0f, 2.0f,-1.0f,
84 1.0f, 0.0f, 1.0f,
85 -1.0f, 0.0f,-1.0f,
86 1.0f, 0.0f,-1.0f,
87 1.0f, 2.0f,-1.0f,
88 1.0f, 0.0f,-1.0f,
89 -1.0f, 0.0f,-1.0f,
90 -1.0f, 0.0f,-1.0f,
91 -1.0f, 2.0f, 1.0f,
92 -1.0f, 2.0f,-1.0f,
93 1.0f, 0.0f, 1.0f,
94 -1.0f, 0.0f, 1.0f,
95 -1.0f, 0.0f,-1.0f,
96 -1.0f, 2.0f, 1.0f,
97 -1.0f, 0.0f, 1.0f,
98 1.0f, 0.0f, 1.0f,
99 1.0f, 2.0f, 1.0f,
100 1.0f, 0.0f,-1.0f,
101 1.0f, 2.0f,-1.0f,
102 1.0f, 0.0f,-1.0f,
103 1.0f, 2.0f, 1.0f,
104 1.0f, 0.0f, 1.0f,
105 1.0f, 2.0f, 1.0f,
106 1.0f, 2.0f,-1.0f,
107 -1.0f, 2.0f,-1.0f,
108 1.0f, 2.0f, 1.0f,
109 -1.0f, 2.0f,-1.0f,
110 -1.0f, 2.0f, 1.0f,
111 1.0f, 2.0f, 1.0f,
112 -1.0f, 2.0f, 1.0f,
113 1.0f, 0.0f, 1.0f,
114
115 // floor
116 10.0f, 0.0f, 10.0f,
117 10.0f, 0.0f, -10.0f,
118 -10.0f, 0.0f, 10.0f,
119 10.0f, 0.0f, -10.0f,
120 -10.0f, 0.0f, 10.0f,
121 -10.0f, 0.0f, -10.0f,
122
123 // back wall
124 10.0f, 5.0f, 10.0f,
125 10.0f, 0.0f, 10.0f,
126 -10.0f, 5.0f, 10.0f,
127 10.0f, 0.0f, 10.0f,
128 -10.0f, 5.0f, 10.0f,
129 -10.0f, 0.0f, 10.0f,
130
131 // right wall
132 -10.0f, 5.0f, 10.0f,
133 -10.0f, 0.0f, 10.0f,
134 -10.0f, 5.0f, -10.0f,
135 -10.0f, 0.0f, 10.0f,
136 -10.0f, 5.0f, -10.0f,
137 -10.0f, 0.0f, -10.0f,
138 };
139
140 // One color for each vertex. They were generated randomly.
141 static const GLfloat g_color_buffer_data[] = {
142 0.583f, 0.771f, 0.014f,
143 0.609f, 0.115f, 0.436f,
144 0.327f, 0.483f, 0.844f,
145 0.822f, 0.569f, 0.201f,
146 0.435f, 0.602f, 0.223f,
147 0.310f, 0.747f, 0.185f,
148 0.597f, 0.770f, 0.761f,
149 0.559f, 0.436f, 0.730f,
150 0.359f, 0.583f, 0.152f,
151 0.483f, 0.596f, 0.789f,
152 0.559f, 0.861f, 0.639f,
153 0.195f, 0.548f, 0.859f,
154 0.014f, 0.184f, 0.576f,
155 0.771f, 0.328f, 0.970f,
156 0.406f, 0.615f, 0.116f,
157 0.676f, 0.977f, 0.133f,
158 0.971f, 0.572f, 0.833f,
159 0.140f, 0.616f, 0.489f,
160 0.997f, 0.513f, 0.064f,
161 0.945f, 0.719f, 0.592f,
162 0.543f, 0.021f, 0.978f,
163 0.279f, 0.317f, 0.505f,
164 0.167f, 0.620f, 0.077f,
165 0.347f, 0.857f, 0.137f,
166 0.055f, 0.953f, 0.042f,
167 0.714f, 0.505f, 0.345f,
168 0.783f, 0.290f, 0.734f,
169 0.722f, 0.645f, 0.174f,
170 0.302f, 0.455f, 0.848f,
171 0.225f, 0.587f, 0.040f,
172 0.517f, 0.713f, 0.338f,
173 0.053f, 0.959f, 0.120f,
174 0.393f, 0.621f, 0.362f,
175 0.673f, 0.211f, 0.457f,
176 0.820f, 0.883f, 0.371f,
177 0.982f, 0.099f, 0.879f,
178
179 // floor
180 0.000f, 0.600f, 0.600f,
181 0.000f, 0.600f, 0.600f,
182 0.000f, 0.600f, 0.600f,
183 0.000f, 0.600f, 0.600f,
184 0.000f, 0.600f, 0.600f,
185 0.000f, 0.600f, 0.600f,
186
187 // back wall
188 1.000f, 0.600f, 0.600f,
189 1.000f, 0.600f, 0.600f,
190 1.000f, 0.600f, 0.600f,
191 1.000f, 0.600f, 0.600f,
192 1.000f, 0.600f, 0.600f,
193 1.000f, 0.600f, 0.600f,
194
195 // right wall
196 1.000f, 0.000f, 0.000f,
197 1.000f, 0.000f, 0.000f,
198 1.000f, 0.000f, 0.000f,
199 1.000f, 0.000f, 0.000f,
200 1.000f, 0.000f, 0.000f,
201 1.000f, 0.000f, 0.000f,
202 };
203
204 GLuint vertexbuffer;
205 glGenBuffers(1, &vertexbuffer);
206 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
207 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
208
209 GLuint colorbuffer;
210 glGenBuffers(1, &colorbuffer);
211 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
212 glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
213
[92bc4fe]214 do {
[8a6d19d]215
[92bc4fe]216 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[8a6d19d]217
[92bc4fe]218 glUseProgram(programID);
[8a6d19d]219
[92bc4fe]220 computeMatricesFromInputs(mode->width, mode->height);
[8a6d19d]221
222 // Projection matrix : 45� Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
223 glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
224
225 glm::mat4 View = getViewMatrix();
226
227 glm::mat4 Model = glm::mat4(1.0f);
228
229 // Remember, matrix multiplication is the other way around
[92bc4fe]230 glm::mat4 MVP = Projection * View * Model;
[8a6d19d]231
232 // Send our transformation to the currently bound shader,
[92bc4fe]233 // in the "MVP" uniform
234 glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
235
236 // 1rst attribute buffer : vertices
237 glEnableVertexAttribArray(0);
238 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
239 glVertexAttribPointer(
240 0, // attribute. No particular reason for 0, but must match the layout in the shader.
241 3, // size
242 GL_FLOAT, // type
243 GL_FALSE, // normalized?
244 0, // stride
245 (void*)0 // array buffer offset
246 );
247
248 // 2nd attribute buffer : colors
249 glEnableVertexAttribArray(1);
250 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
251 glVertexAttribPointer(
252 1, // attribute. No particular reason for 1, but must match the layout in the shader.
253 3, // size
254 GL_FLOAT, // type
255 GL_FALSE, // normalized?
256 0, // stride
257 (void*)0 // array buffer offset
258 );
259
260 // Draw the triangle !
261 glDrawArrays(GL_TRIANGLES, 0, 12*3+18); // 12*3 indices starting at 0 -> 12 triangles
262
263 glDisableVertexAttribArray(0);
264 glDisableVertexAttribArray(1);
265
266 // Swap buffers
267 glfwSwapBuffers(window);
268 glfwPollEvents();
269
270 // Check if the ESC key was pressed or the window was closed
271 } while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 );
[8a6d19d]272
273 // Cleanup VBO and shader
274 glDeleteBuffers(1, &vertexbuffer);
275 glDeleteBuffers(1, &colorbuffer);
276 glDeleteProgram(programID);
277 glDeleteVertexArrays(1, &VertexArrayID);
278
279 // Close OpenGL window and terminate GLFW
280 glfwTerminate();
281
282 return 0;
[0424bd8]283}
Note: See TracBrowser for help on using the repository browser.