[8a6d19d] | 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 |
|
---|
| 19 | int main( void )
|
---|
| 20 | {
|
---|
| 21 | // Initialise GLFW
|
---|
| 22 | if( !glfwInit() )
|
---|
| 23 | {
|
---|
| 24 | fprintf( stderr, "Failed to initialize GLFW\n" );
|
---|
| 25 | getchar();
|
---|
| 26 | return -1;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
| 30 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 31 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 32 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
|
---|
| 33 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
| 34 |
|
---|
| 35 | // Open a window and create its OpenGL context
|
---|
| 36 | window = glfwCreateWindow( 1024, 768, "Tutorial 04 - Colored Cube", NULL, NULL);
|
---|
| 37 | if( window == NULL ){
|
---|
| 38 | 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" );
|
---|
| 39 | getchar();
|
---|
| 40 | glfwTerminate();
|
---|
| 41 | return -1;
|
---|
| 42 | }
|
---|
| 43 | glfwMakeContextCurrent(window);
|
---|
| 44 |
|
---|
| 45 | // Initialize GLEW
|
---|
| 46 | glewExperimental = true; // Needed for core profile
|
---|
| 47 | if (glewInit() != GLEW_OK) {
|
---|
| 48 | fprintf(stderr, "Failed to initialize GLEW\n");
|
---|
| 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 | // Projection matrix : 45� Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
|
---|
| 76 | glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
|
---|
| 77 | // Camera matrix
|
---|
| 78 | glm::mat4 View = glm::lookAt(
|
---|
| 79 | glm::vec3(4,3,-3), // Camera is at (4,3,-3), in World Space
|
---|
| 80 | glm::vec3(0,0,0), // and looks at the origin
|
---|
| 81 | glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
|
---|
| 82 | );
|
---|
| 83 | // Model matrix : an identity matrix (model will be at the origin)
|
---|
| 84 | glm::mat4 Model = glm::mat4(1.0f);
|
---|
| 85 | // Our ModelViewProjection : multiplication of our 3 matrices
|
---|
| 86 | glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
|
---|
| 87 |
|
---|
| 88 | // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
|
---|
| 89 | // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
|
---|
| 90 | static const GLfloat g_vertex_buffer_data[] = {
|
---|
| 91 | -1.0f, 0.0f,-1.0f,
|
---|
| 92 | -1.0f, 0.0f, 1.0f,
|
---|
| 93 | -1.0f, 2.0f, 1.0f,
|
---|
| 94 | 1.0f, 2.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, 0.0f,-1.0f,
|
---|
| 100 | 1.0f, 2.0f,-1.0f,
|
---|
| 101 | 1.0f, 0.0f,-1.0f,
|
---|
| 102 | -1.0f, 0.0f,-1.0f,
|
---|
| 103 | -1.0f, 0.0f,-1.0f,
|
---|
| 104 | -1.0f, 2.0f, 1.0f,
|
---|
| 105 | -1.0f, 2.0f,-1.0f,
|
---|
| 106 | 1.0f, 0.0f, 1.0f,
|
---|
| 107 | -1.0f, 0.0f, 1.0f,
|
---|
| 108 | -1.0f, 0.0f,-1.0f,
|
---|
| 109 | -1.0f, 2.0f, 1.0f,
|
---|
| 110 | -1.0f, 0.0f, 1.0f,
|
---|
| 111 | 1.0f, 0.0f, 1.0f,
|
---|
| 112 | 1.0f, 2.0f, 1.0f,
|
---|
| 113 | 1.0f, 0.0f,-1.0f,
|
---|
| 114 | 1.0f, 2.0f,-1.0f,
|
---|
| 115 | 1.0f, 0.0f,-1.0f,
|
---|
| 116 | 1.0f, 2.0f, 1.0f,
|
---|
| 117 | 1.0f, 0.0f, 1.0f,
|
---|
| 118 | 1.0f, 2.0f, 1.0f,
|
---|
| 119 | 1.0f, 2.0f,-1.0f,
|
---|
| 120 | -1.0f, 2.0f,-1.0f,
|
---|
| 121 | 1.0f, 2.0f, 1.0f,
|
---|
| 122 | -1.0f, 2.0f,-1.0f,
|
---|
| 123 | -1.0f, 2.0f, 1.0f,
|
---|
| 124 | 1.0f, 2.0f, 1.0f,
|
---|
| 125 | -1.0f, 2.0f, 1.0f,
|
---|
| 126 | 1.0f, 0.0f, 1.0f,
|
---|
| 127 |
|
---|
| 128 | // floor
|
---|
| 129 | 10.0f, 0.0f, 10.0f,
|
---|
| 130 | 10.0f, 0.0f, -10.0f,
|
---|
| 131 | -10.0f, 0.0f, 10.0f,
|
---|
| 132 | 10.0f, 0.0f, -10.0f,
|
---|
| 133 | -10.0f, 0.0f, 10.0f,
|
---|
| 134 | -10.0f, 0.0f, -10.0f,
|
---|
| 135 |
|
---|
| 136 | // back wall
|
---|
| 137 | 10.0f, 5.0f, 10.0f,
|
---|
| 138 | 10.0f, 0.0f, 10.0f,
|
---|
| 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 |
|
---|
| 144 | // right wall
|
---|
| 145 | -10.0f, 5.0f, 10.0f,
|
---|
| 146 | -10.0f, 0.0f, 10.0f,
|
---|
| 147 | -10.0f, 5.0f, -10.0f,
|
---|
| 148 | -10.0f, 0.0f, 10.0f,
|
---|
| 149 | -10.0f, 5.0f, -10.0f,
|
---|
| 150 | -10.0f, 0.0f, -10.0f,
|
---|
| 151 | };
|
---|
| 152 |
|
---|
| 153 | // One color for each vertex. They were generated randomly.
|
---|
| 154 | static const GLfloat g_color_buffer_data[] = {
|
---|
| 155 | 0.583f, 0.771f, 0.014f,
|
---|
| 156 | 0.609f, 0.115f, 0.436f,
|
---|
| 157 | 0.327f, 0.483f, 0.844f,
|
---|
| 158 | 0.822f, 0.569f, 0.201f,
|
---|
| 159 | 0.435f, 0.602f, 0.223f,
|
---|
| 160 | 0.310f, 0.747f, 0.185f,
|
---|
| 161 | 0.597f, 0.770f, 0.761f,
|
---|
| 162 | 0.559f, 0.436f, 0.730f,
|
---|
| 163 | 0.359f, 0.583f, 0.152f,
|
---|
| 164 | 0.483f, 0.596f, 0.789f,
|
---|
| 165 | 0.559f, 0.861f, 0.639f,
|
---|
| 166 | 0.195f, 0.548f, 0.859f,
|
---|
| 167 | 0.014f, 0.184f, 0.576f,
|
---|
| 168 | 0.771f, 0.328f, 0.970f,
|
---|
| 169 | 0.406f, 0.615f, 0.116f,
|
---|
| 170 | 0.676f, 0.977f, 0.133f,
|
---|
| 171 | 0.971f, 0.572f, 0.833f,
|
---|
| 172 | 0.140f, 0.616f, 0.489f,
|
---|
| 173 | 0.997f, 0.513f, 0.064f,
|
---|
| 174 | 0.945f, 0.719f, 0.592f,
|
---|
| 175 | 0.543f, 0.021f, 0.978f,
|
---|
| 176 | 0.279f, 0.317f, 0.505f,
|
---|
| 177 | 0.167f, 0.620f, 0.077f,
|
---|
| 178 | 0.347f, 0.857f, 0.137f,
|
---|
| 179 | 0.055f, 0.953f, 0.042f,
|
---|
| 180 | 0.714f, 0.505f, 0.345f,
|
---|
| 181 | 0.783f, 0.290f, 0.734f,
|
---|
| 182 | 0.722f, 0.645f, 0.174f,
|
---|
| 183 | 0.302f, 0.455f, 0.848f,
|
---|
| 184 | 0.225f, 0.587f, 0.040f,
|
---|
| 185 | 0.517f, 0.713f, 0.338f,
|
---|
| 186 | 0.053f, 0.959f, 0.120f,
|
---|
| 187 | 0.393f, 0.621f, 0.362f,
|
---|
| 188 | 0.673f, 0.211f, 0.457f,
|
---|
| 189 | 0.820f, 0.883f, 0.371f,
|
---|
| 190 | 0.982f, 0.099f, 0.879f,
|
---|
| 191 |
|
---|
| 192 | // floor
|
---|
| 193 | 0.000f, 0.600f, 0.600f,
|
---|
| 194 | 0.000f, 0.600f, 0.600f,
|
---|
| 195 | 0.000f, 0.600f, 0.600f,
|
---|
| 196 | 0.000f, 0.600f, 0.600f,
|
---|
| 197 | 0.000f, 0.600f, 0.600f,
|
---|
| 198 | 0.000f, 0.600f, 0.600f,
|
---|
| 199 |
|
---|
| 200 | // back wall
|
---|
| 201 | 1.000f, 0.600f, 0.600f,
|
---|
| 202 | 1.000f, 0.600f, 0.600f,
|
---|
| 203 | 1.000f, 0.600f, 0.600f,
|
---|
| 204 | 1.000f, 0.600f, 0.600f,
|
---|
| 205 | 1.000f, 0.600f, 0.600f,
|
---|
| 206 | 1.000f, 0.600f, 0.600f,
|
---|
| 207 |
|
---|
| 208 | // right wall
|
---|
| 209 | 1.000f, 0.000f, 0.000f,
|
---|
| 210 | 1.000f, 0.000f, 0.000f,
|
---|
| 211 | 1.000f, 0.000f, 0.000f,
|
---|
| 212 | 1.000f, 0.000f, 0.000f,
|
---|
| 213 | 1.000f, 0.000f, 0.000f,
|
---|
| 214 | 1.000f, 0.000f, 0.000f,
|
---|
| 215 | };
|
---|
| 216 |
|
---|
| 217 | GLuint vertexbuffer;
|
---|
| 218 | glGenBuffers(1, &vertexbuffer);
|
---|
| 219 | glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
---|
| 220 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
|
---|
| 221 |
|
---|
| 222 | GLuint colorbuffer;
|
---|
| 223 | glGenBuffers(1, &colorbuffer);
|
---|
| 224 | glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
|
---|
| 225 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
|
---|
| 226 |
|
---|
| 227 | do {
|
---|
| 228 |
|
---|
| 229 | // Clear the screen
|
---|
| 230 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 231 |
|
---|
| 232 | // Use our shader
|
---|
| 233 | glUseProgram(programID);
|
---|
| 234 |
|
---|
| 235 | // Send our transformation to the currently bound shader,
|
---|
| 236 | // in the "MVP" uniform
|
---|
| 237 | glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
|
---|
| 238 |
|
---|
| 239 | // 1rst attribute buffer : vertices
|
---|
| 240 | glEnableVertexAttribArray(0);
|
---|
| 241 | glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
---|
| 242 | glVertexAttribPointer(
|
---|
| 243 | 0, // attribute. No particular reason for 0, but must match the layout in the shader.
|
---|
| 244 | 3, // size
|
---|
| 245 | GL_FLOAT, // type
|
---|
| 246 | GL_FALSE, // normalized?
|
---|
| 247 | 0, // stride
|
---|
| 248 | (void*)0 // array buffer offset
|
---|
| 249 | );
|
---|
| 250 |
|
---|
| 251 | // 2nd attribute buffer : colors
|
---|
| 252 | glEnableVertexAttribArray(1);
|
---|
| 253 | glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
|
---|
| 254 | glVertexAttribPointer(
|
---|
| 255 | 1, // attribute. No particular reason for 1, but must match the layout in the shader.
|
---|
| 256 | 3, // size
|
---|
| 257 | GL_FLOAT, // type
|
---|
| 258 | GL_FALSE, // normalized?
|
---|
| 259 | 0, // stride
|
---|
| 260 | (void*)0 // array buffer offset
|
---|
| 261 | );
|
---|
| 262 |
|
---|
| 263 | // Draw the triangle !
|
---|
| 264 | glDrawArrays(GL_TRIANGLES, 0, 12*3+18); // 12*3 indices starting at 0 -> 12 triangles
|
---|
| 265 |
|
---|
| 266 | glDisableVertexAttribArray(0);
|
---|
| 267 | glDisableVertexAttribArray(1);
|
---|
| 268 |
|
---|
| 269 | // Swap buffers
|
---|
| 270 | glfwSwapBuffers(window);
|
---|
| 271 | glfwPollEvents();
|
---|
| 272 |
|
---|
| 273 | } // Check if the ESC key was pressed or the window was closed
|
---|
| 274 | while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
|
---|
| 275 | glfwWindowShouldClose(window) == 0 );
|
---|
| 276 |
|
---|
| 277 | // Cleanup VBO and shader
|
---|
| 278 | glDeleteBuffers(1, &vertexbuffer);
|
---|
| 279 | glDeleteBuffers(1, &colorbuffer);
|
---|
| 280 | glDeleteProgram(programID);
|
---|
| 281 | glDeleteVertexArrays(1, &VertexArrayID);
|
---|
| 282 |
|
---|
| 283 | // Close OpenGL window and terminate GLFW
|
---|
| 284 | glfwTerminate();
|
---|
| 285 |
|
---|
| 286 | return 0;
|
---|
| 287 | }
|
---|