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>
|
---|
10 | GLFWwindow* window;
|
---|
11 |
|
---|
12 | // Include GLM
|
---|
13 | #include <glm/glm.hpp>
|
---|
14 | #include <glm/gtc/matrix_transform.hpp>
|
---|
15 | using namespace glm;
|
---|
16 |
|
---|
17 | #include "common/shader.hpp"
|
---|
18 | #include "common/texture.hpp"
|
---|
19 | #include "common/controls.hpp"
|
---|
20 |
|
---|
21 | int main( void )
|
---|
22 | {
|
---|
23 | // Initialise GLFW
|
---|
24 | if( !glfwInit() )
|
---|
25 | {
|
---|
26 | fprintf( stderr, "Failed to initialize GLFW\n" );
|
---|
27 | getchar();
|
---|
28 | return -1;
|
---|
29 | }
|
---|
30 |
|
---|
31 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
32 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
33 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
34 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
|
---|
35 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
36 |
|
---|
37 | // Open a window and create its OpenGL context
|
---|
38 | window = glfwCreateWindow( 1024, 768, "Tutorial 0 - Keyboard and Mouse", NULL, NULL);
|
---|
39 | if( window == NULL ){
|
---|
40 | 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" );
|
---|
41 | getchar();
|
---|
42 | glfwTerminate();
|
---|
43 | return -1;
|
---|
44 | }
|
---|
45 | glfwMakeContextCurrent(window);
|
---|
46 |
|
---|
47 | // Initialize GLEW
|
---|
48 | glewExperimental = true; // Needed for core profile
|
---|
49 | if (glewInit() != GLEW_OK) {
|
---|
50 | fprintf(stderr, "Failed to initialize GLEW\n");
|
---|
51 | getchar();
|
---|
52 | glfwTerminate();
|
---|
53 | return -1;
|
---|
54 | }
|
---|
55 |
|
---|
56 | // Ensure we can capture the escape key being pressed below
|
---|
57 | glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
|
---|
58 | // Hide the mouse and enable unlimited mouvement
|
---|
59 | glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
---|
60 |
|
---|
61 | // Set the mouse at the center of the screen
|
---|
62 | glfwPollEvents();
|
---|
63 | glfwSetCursorPos(window, 1024/2, 768/2);
|
---|
64 |
|
---|
65 | // Dark blue background
|
---|
66 | glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
|
---|
67 |
|
---|
68 | // Enable depth test
|
---|
69 | glEnable(GL_DEPTH_TEST);
|
---|
70 | // Accept fragment if it closer to the camera than the former one
|
---|
71 | glDepthFunc(GL_LESS);
|
---|
72 |
|
---|
73 | // Cull triangles which normal is not towards the camera
|
---|
74 | glEnable(GL_CULL_FACE);
|
---|
75 |
|
---|
76 | GLuint VertexArrayID;
|
---|
77 | glGenVertexArrays(1, &VertexArrayID);
|
---|
78 | glBindVertexArray(VertexArrayID);
|
---|
79 |
|
---|
80 | // Create and compile our GLSL program from the shaders
|
---|
81 | GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "TextureFragmentShader.fragmentshader" );
|
---|
82 |
|
---|
83 | // Get a handle for our "MVP" uniform
|
---|
84 | GLuint MatrixID = glGetUniformLocation(programID, "MVP");
|
---|
85 |
|
---|
86 | // Load the texture
|
---|
87 | GLuint Texture = loadDDS("uvtemplate.DDS");
|
---|
88 |
|
---|
89 | // Get a handle for our "myTextureSampler" uniform
|
---|
90 | GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler");
|
---|
91 |
|
---|
92 | // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
|
---|
93 | // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
|
---|
94 | static const GLfloat g_vertex_buffer_data[] = {
|
---|
95 | -1.0f,-1.0f,-1.0f,
|
---|
96 | -1.0f,-1.0f, 1.0f,
|
---|
97 | -1.0f, 1.0f, 1.0f,
|
---|
98 | 1.0f, 1.0f,-1.0f,
|
---|
99 | -1.0f,-1.0f,-1.0f,
|
---|
100 | -1.0f, 1.0f,-1.0f,
|
---|
101 | 1.0f,-1.0f, 1.0f,
|
---|
102 | -1.0f,-1.0f,-1.0f,
|
---|
103 | 1.0f,-1.0f,-1.0f,
|
---|
104 | 1.0f, 1.0f,-1.0f,
|
---|
105 | 1.0f,-1.0f,-1.0f,
|
---|
106 | -1.0f,-1.0f,-1.0f,
|
---|
107 | -1.0f,-1.0f,-1.0f,
|
---|
108 | -1.0f, 1.0f, 1.0f,
|
---|
109 | -1.0f, 1.0f,-1.0f,
|
---|
110 | 1.0f,-1.0f, 1.0f,
|
---|
111 | -1.0f,-1.0f, 1.0f,
|
---|
112 | -1.0f,-1.0f,-1.0f,
|
---|
113 | -1.0f, 1.0f, 1.0f,
|
---|
114 | -1.0f,-1.0f, 1.0f,
|
---|
115 | 1.0f,-1.0f, 1.0f,
|
---|
116 | 1.0f, 1.0f, 1.0f,
|
---|
117 | 1.0f,-1.0f,-1.0f,
|
---|
118 | 1.0f, 1.0f,-1.0f,
|
---|
119 | 1.0f,-1.0f,-1.0f,
|
---|
120 | 1.0f, 1.0f, 1.0f,
|
---|
121 | 1.0f,-1.0f, 1.0f,
|
---|
122 | 1.0f, 1.0f, 1.0f,
|
---|
123 | 1.0f, 1.0f,-1.0f,
|
---|
124 | -1.0f, 1.0f,-1.0f,
|
---|
125 | 1.0f, 1.0f, 1.0f,
|
---|
126 | -1.0f, 1.0f,-1.0f,
|
---|
127 | -1.0f, 1.0f, 1.0f,
|
---|
128 | 1.0f, 1.0f, 1.0f,
|
---|
129 | -1.0f, 1.0f, 1.0f,
|
---|
130 | 1.0f,-1.0f, 1.0f
|
---|
131 | };
|
---|
132 |
|
---|
133 | // Two UV coordinatesfor each vertex. They were created withe Blender.
|
---|
134 | static const GLfloat g_uv_buffer_data[] = {
|
---|
135 | 0.000059f, 0.000004f,
|
---|
136 | 0.000103f, 0.336048f,
|
---|
137 | 0.335973f, 0.335903f,
|
---|
138 | 1.000023f, 0.000013f,
|
---|
139 | 0.667979f, 0.335851f,
|
---|
140 | 0.999958f, 0.336064f,
|
---|
141 | 0.667979f, 0.335851f,
|
---|
142 | 0.336024f, 0.671877f,
|
---|
143 | 0.667969f, 0.671889f,
|
---|
144 | 1.000023f, 0.000013f,
|
---|
145 | 0.668104f, 0.000013f,
|
---|
146 | 0.667979f, 0.335851f,
|
---|
147 | 0.000059f, 0.000004f,
|
---|
148 | 0.335973f, 0.335903f,
|
---|
149 | 0.336098f, 0.000071f,
|
---|
150 | 0.667979f, 0.335851f,
|
---|
151 | 0.335973f, 0.335903f,
|
---|
152 | 0.336024f, 0.671877f,
|
---|
153 | 1.000004f, 0.671847f,
|
---|
154 | 0.999958f, 0.336064f,
|
---|
155 | 0.667979f, 0.335851f,
|
---|
156 | 0.668104f, 0.000013f,
|
---|
157 | 0.335973f, 0.335903f,
|
---|
158 | 0.667979f, 0.335851f,
|
---|
159 | 0.335973f, 0.335903f,
|
---|
160 | 0.668104f, 0.000013f,
|
---|
161 | 0.336098f, 0.000071f,
|
---|
162 | 0.000103f, 0.336048f,
|
---|
163 | 0.000004f, 0.671870f,
|
---|
164 | 0.336024f, 0.671877f,
|
---|
165 | 0.000103f, 0.336048f,
|
---|
166 | 0.336024f, 0.671877f,
|
---|
167 | 0.335973f, 0.335903f,
|
---|
168 | 0.667969f, 0.671889f,
|
---|
169 | 1.000004f, 0.671847f,
|
---|
170 | 0.667979f, 0.335851f
|
---|
171 | };
|
---|
172 |
|
---|
173 | GLuint vertexbuffer;
|
---|
174 | glGenBuffers(1, &vertexbuffer);
|
---|
175 | glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
---|
176 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
|
---|
177 |
|
---|
178 | GLuint uvbuffer;
|
---|
179 | glGenBuffers(1, &uvbuffer);
|
---|
180 | glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
|
---|
181 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
|
---|
182 |
|
---|
183 | do{
|
---|
184 |
|
---|
185 | // Clear the screen
|
---|
186 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
187 |
|
---|
188 | // Use our shader
|
---|
189 | glUseProgram(programID);
|
---|
190 |
|
---|
191 | // Compute the MVP matrix from keyboard and mouse input
|
---|
192 | computeMatricesFromInputs(1024, 768);
|
---|
193 | glm::mat4 ProjectionMatrix = getProjectionMatrix();
|
---|
194 | glm::mat4 ViewMatrix = getViewMatrix();
|
---|
195 | glm::mat4 ModelMatrix = glm::mat4(1.0);
|
---|
196 | glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
|
---|
197 |
|
---|
198 | // Send our transformation to the currently bound shader,
|
---|
199 | // in the "MVP" uniform
|
---|
200 | glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
|
---|
201 |
|
---|
202 | // Bind our texture in Texture Unit 0
|
---|
203 | glActiveTexture(GL_TEXTURE0);
|
---|
204 | glBindTexture(GL_TEXTURE_2D, Texture);
|
---|
205 | // Set our "myTextureSampler" sampler to user Texture Unit 0
|
---|
206 | glUniform1i(TextureID, 0);
|
---|
207 |
|
---|
208 | // 1rst attribute buffer : vertices
|
---|
209 | glEnableVertexAttribArray(0);
|
---|
210 | glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
---|
211 | glVertexAttribPointer(
|
---|
212 | 0, // attribute. No particular reason for 0, but must match the layout in the shader.
|
---|
213 | 3, // size
|
---|
214 | GL_FLOAT, // type
|
---|
215 | GL_FALSE, // normalized?
|
---|
216 | 0, // stride
|
---|
217 | (void*)0 // array buffer offset
|
---|
218 | );
|
---|
219 |
|
---|
220 | // 2nd attribute buffer : UVs
|
---|
221 | glEnableVertexAttribArray(1);
|
---|
222 | glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
|
---|
223 | glVertexAttribPointer(
|
---|
224 | 1, // attribute. No particular reason for 1, but must match the layout in the shader.
|
---|
225 | 2, // size : U+V => 2
|
---|
226 | GL_FLOAT, // type
|
---|
227 | GL_FALSE, // normalized?
|
---|
228 | 0, // stride
|
---|
229 | (void*)0 // array buffer offset
|
---|
230 | );
|
---|
231 |
|
---|
232 | // Draw the triangle !
|
---|
233 | glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles
|
---|
234 |
|
---|
235 | glDisableVertexAttribArray(0);
|
---|
236 | glDisableVertexAttribArray(1);
|
---|
237 |
|
---|
238 | // Swap buffers
|
---|
239 | glfwSwapBuffers(window);
|
---|
240 | glfwPollEvents();
|
---|
241 |
|
---|
242 | } // Check if the ESC key was pressed or the window was closed
|
---|
243 | while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
|
---|
244 | glfwWindowShouldClose(window) == 0 );
|
---|
245 |
|
---|
246 | // Cleanup VBO and shader
|
---|
247 | glDeleteBuffers(1, &vertexbuffer);
|
---|
248 | glDeleteBuffers(1, &uvbuffer);
|
---|
249 | glDeleteProgram(programID);
|
---|
250 | glDeleteTextures(1, &TextureID);
|
---|
251 | glDeleteVertexArrays(1, &VertexArrayID);
|
---|
252 |
|
---|
253 | // Close OpenGL window and terminate GLFW
|
---|
254 | glfwTerminate();
|
---|
255 |
|
---|
256 | return 0;
|
---|
257 | }
|
---|