[9cbdc9c] | 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
|
---|
[af116c0] | 9 | #include <GLFW/glfw3.h>
|
---|
| 10 | GLFWwindow* window;
|
---|
[9cbdc9c] | 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 |
|
---|
[fccd588] | 21 | int main( void ) {
|
---|
[9cbdc9c] | 22 | // Initialise GLFW
|
---|
[fccd588] | 23 | if( !glfwInit() ) {
|
---|
[9cbdc9c] | 24 | fprintf( stderr, "Failed to initialize GLFW\n" );
|
---|
| 25 | return -1;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[fccd588] | 28 | #ifdef __APPLE__
|
---|
| 29 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 30 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 31 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
| 32 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
| 33 | #else
|
---|
| 34 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
---|
| 35 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 36 | #endif
|
---|
[9cbdc9c] | 37 |
|
---|
| 38 | // Open a window and create its OpenGL context
|
---|
[af116c0] | 39 | window = glfwCreateWindow( 1024, 768, "Tutorial 0 - Keyboard and Mouse", NULL, NULL);
|
---|
| 40 | if( window == NULL ){
|
---|
[9cbdc9c] | 41 | 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" );
|
---|
| 42 | glfwTerminate();
|
---|
| 43 | return -1;
|
---|
| 44 | }
|
---|
[af116c0] | 45 | glfwMakeContextCurrent(window);
|
---|
[9cbdc9c] | 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 | return -1;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | // Ensure we can capture the escape key being pressed below
|
---|
[af116c0] | 55 | glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
|
---|
| 56 | glfwSetCursorPos(window, 1024/2, 768/2);
|
---|
[9cbdc9c] | 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 | // Cull triangles which normal is not towards the camera
|
---|
| 67 | glEnable(GL_CULL_FACE);
|
---|
| 68 |
|
---|
| 69 | GLuint VertexArrayID;
|
---|
| 70 | glGenVertexArrays(1, &VertexArrayID);
|
---|
| 71 | glBindVertexArray(VertexArrayID);
|
---|
| 72 |
|
---|
| 73 | // Create and compile our GLSL program from the shaders
|
---|
| 74 | GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "TextureFragmentShader.fragmentshader" );
|
---|
| 75 |
|
---|
| 76 | // Get a handle for our "MVP" uniform
|
---|
| 77 | GLuint MatrixID = glGetUniformLocation(programID, "MVP");
|
---|
| 78 |
|
---|
| 79 | // Load the texture
|
---|
| 80 | GLuint Texture = loadDDS("uvtemplate.DDS");
|
---|
| 81 |
|
---|
| 82 | // Get a handle for our "myTextureSampler" uniform
|
---|
| 83 | GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler");
|
---|
| 84 |
|
---|
| 85 | // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
|
---|
| 86 | // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
|
---|
| 87 | static const GLfloat g_vertex_buffer_data[] = {
|
---|
| 88 | -1.0f,-1.0f,-1.0f,
|
---|
| 89 | -1.0f,-1.0f, 1.0f,
|
---|
| 90 | -1.0f, 1.0f, 1.0f,
|
---|
| 91 | 1.0f, 1.0f,-1.0f,
|
---|
| 92 | -1.0f,-1.0f,-1.0f,
|
---|
| 93 | -1.0f, 1.0f,-1.0f,
|
---|
| 94 | 1.0f,-1.0f, 1.0f,
|
---|
| 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 | };
|
---|
| 125 |
|
---|
| 126 | // Two UV coordinatesfor each vertex. They were created withe Blender.
|
---|
| 127 | static const GLfloat g_uv_buffer_data[] = {
|
---|
| 128 | 0.000059f, 0.000004f,
|
---|
| 129 | 0.000103f, 0.336048f,
|
---|
| 130 | 0.335973f, 0.335903f,
|
---|
| 131 | 1.000023f, 0.000013f,
|
---|
| 132 | 0.667979f, 0.335851f,
|
---|
| 133 | 0.999958f, 0.336064f,
|
---|
| 134 | 0.667979f, 0.335851f,
|
---|
| 135 | 0.336024f, 0.671877f,
|
---|
| 136 | 0.667969f, 0.671889f,
|
---|
| 137 | 1.000023f, 0.000013f,
|
---|
| 138 | 0.668104f, 0.000013f,
|
---|
| 139 | 0.667979f, 0.335851f,
|
---|
| 140 | 0.000059f, 0.000004f,
|
---|
| 141 | 0.335973f, 0.335903f,
|
---|
| 142 | 0.336098f, 0.000071f,
|
---|
| 143 | 0.667979f, 0.335851f,
|
---|
| 144 | 0.335973f, 0.335903f,
|
---|
| 145 | 0.336024f, 0.671877f,
|
---|
| 146 | 1.000004f, 0.671847f,
|
---|
| 147 | 0.999958f, 0.336064f,
|
---|
| 148 | 0.667979f, 0.335851f,
|
---|
| 149 | 0.668104f, 0.000013f,
|
---|
| 150 | 0.335973f, 0.335903f,
|
---|
| 151 | 0.667979f, 0.335851f,
|
---|
| 152 | 0.335973f, 0.335903f,
|
---|
| 153 | 0.668104f, 0.000013f,
|
---|
| 154 | 0.336098f, 0.000071f,
|
---|
| 155 | 0.000103f, 0.336048f,
|
---|
| 156 | 0.000004f, 0.671870f,
|
---|
| 157 | 0.336024f, 0.671877f,
|
---|
| 158 | 0.000103f, 0.336048f,
|
---|
| 159 | 0.336024f, 0.671877f,
|
---|
| 160 | 0.335973f, 0.335903f,
|
---|
| 161 | 0.667969f, 0.671889f,
|
---|
| 162 | 1.000004f, 0.671847f,
|
---|
| 163 | 0.667979f, 0.335851f
|
---|
| 164 | };
|
---|
| 165 |
|
---|
| 166 | GLuint vertexbuffer;
|
---|
| 167 | glGenBuffers(1, &vertexbuffer);
|
---|
| 168 | glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
---|
| 169 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
|
---|
| 170 |
|
---|
| 171 | GLuint uvbuffer;
|
---|
| 172 | glGenBuffers(1, &uvbuffer);
|
---|
| 173 | glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
|
---|
| 174 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
|
---|
| 175 |
|
---|
| 176 | do{
|
---|
| 177 |
|
---|
| 178 | // Clear the screen
|
---|
| 179 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 180 |
|
---|
| 181 | // Use our shader
|
---|
| 182 | glUseProgram(programID);
|
---|
| 183 |
|
---|
| 184 | // Compute the MVP matrix from keyboard and mouse input
|
---|
| 185 | computeMatricesFromInputs();
|
---|
| 186 | glm::mat4 ProjectionMatrix = getProjectionMatrix();
|
---|
| 187 | glm::mat4 ViewMatrix = getViewMatrix();
|
---|
| 188 | glm::mat4 ModelMatrix = glm::mat4(1.0);
|
---|
| 189 | glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
|
---|
| 190 |
|
---|
| 191 | // Send our transformation to the currently bound shader,
|
---|
| 192 | // in the "MVP" uniform
|
---|
| 193 | glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
|
---|
| 194 |
|
---|
| 195 | // Bind our texture in Texture Unit 0
|
---|
| 196 | glActiveTexture(GL_TEXTURE0);
|
---|
| 197 | glBindTexture(GL_TEXTURE_2D, Texture);
|
---|
| 198 | // Set our "myTextureSampler" sampler to user Texture Unit 0
|
---|
| 199 | glUniform1i(TextureID, 0);
|
---|
| 200 |
|
---|
| 201 | // 1rst attribute buffer : vertices
|
---|
| 202 | glEnableVertexAttribArray(0);
|
---|
| 203 | glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
---|
| 204 | glVertexAttribPointer(
|
---|
| 205 | 0, // attribute. No particular reason for 0, but must match the layout in the shader.
|
---|
| 206 | 3, // size
|
---|
| 207 | GL_FLOAT, // type
|
---|
| 208 | GL_FALSE, // normalized?
|
---|
| 209 | 0, // stride
|
---|
| 210 | (void*)0 // array buffer offset
|
---|
| 211 | );
|
---|
| 212 |
|
---|
| 213 | // 2nd attribute buffer : UVs
|
---|
| 214 | glEnableVertexAttribArray(1);
|
---|
| 215 | glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
|
---|
| 216 | glVertexAttribPointer(
|
---|
| 217 | 1, // attribute. No particular reason for 1, but must match the layout in the shader.
|
---|
| 218 | 2, // size : U+V => 2
|
---|
| 219 | GL_FLOAT, // type
|
---|
| 220 | GL_FALSE, // normalized?
|
---|
| 221 | 0, // stride
|
---|
| 222 | (void*)0 // array buffer offset
|
---|
| 223 | );
|
---|
| 224 |
|
---|
| 225 | // Draw the triangle !
|
---|
| 226 | glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles
|
---|
| 227 |
|
---|
| 228 | glDisableVertexAttribArray(0);
|
---|
| 229 | glDisableVertexAttribArray(1);
|
---|
| 230 |
|
---|
| 231 | // Swap buffers
|
---|
[af116c0] | 232 | glfwSwapBuffers(window);
|
---|
| 233 | glfwPollEvents();
|
---|
[9cbdc9c] | 234 |
|
---|
| 235 | } // Check if the ESC key was pressed or the window was closed
|
---|
[af116c0] | 236 | while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
|
---|
| 237 | glfwWindowShouldClose(window) == 0 );
|
---|
[9cbdc9c] | 238 |
|
---|
| 239 | // Cleanup VBO and shader
|
---|
| 240 | glDeleteBuffers(1, &vertexbuffer);
|
---|
| 241 | glDeleteBuffers(1, &uvbuffer);
|
---|
| 242 | glDeleteProgram(programID);
|
---|
| 243 | glDeleteTextures(1, &TextureID);
|
---|
| 244 | glDeleteVertexArrays(1, &VertexArrayID);
|
---|
| 245 |
|
---|
| 246 | // Close OpenGL window and terminate GLFW
|
---|
| 247 | glfwTerminate();
|
---|
| 248 |
|
---|
| 249 | return 0;
|
---|
[af116c0] | 250 | }
|
---|