source: opengl-game/mygame.cpp@ 8a6d19d

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

Implement movement in a basic 3D scene and add some examples from the new OpenGL tutorials site

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