source: opengl-game/mygame.cpp@ 92bc4fe

feature/imgui-sdl points-test
Last change on this file since 92bc4fe was 92bc4fe, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 7 years ago

Make the game window fullscreen, hide the mouse cursor, and change the view to an overhead one as the player moves around the scene

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