source: network-game/graphics_library/main.cpp@ 0b6f9ec

Last change on this file since 0b6f9ec was 9cbdc9c, checked in by Dmitry Portnoy <dportnoy@…>, 11 years ago

Graphics libary can load textures and handle mouse and keyboard controls (using tutorial code)

  • Property mode set to 100644
File size: 9.3 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 <GL/glfw.h>
10
11// Include GLM
12#include <glm/glm.hpp>
13#include <glm/gtc/matrix_transform.hpp>
14using namespace glm;
15
16#include "common/shader.hpp"
17#include "common/texture.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 return -1;
27 }
28
29 glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
30 glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
31 glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
32 glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
33
34 // Open a window and create its OpenGL context
35 if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
36 {
37 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" );
38 glfwTerminate();
39 return -1;
40 }
41
42 // Initialize GLEW
43 glewExperimental = true; // Needed for core profile
44 if (glewInit() != GLEW_OK) {
45 fprintf(stderr, "Failed to initialize GLEW\n");
46 return -1;
47 }
48
49 glfwSetWindowTitle( "Tutorial 06" );
50
51 // Ensure we can capture the escape key being pressed below
52 glfwEnable( GLFW_STICKY_KEYS );
53 glfwSetMousePos(1024/2, 768/2);
54
55 // Dark blue background
56 glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
57
58 // Enable depth test
59 glEnable(GL_DEPTH_TEST);
60 // Accept fragment if it closer to the camera than the former one
61 glDepthFunc(GL_LESS);
62
63 // Cull triangles which normal is not towards the camera
64 glEnable(GL_CULL_FACE);
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.vertexshader", "TextureFragmentShader.fragmentshader" );
72
73 // Get a handle for our "MVP" uniform
74 GLuint MatrixID = glGetUniformLocation(programID, "MVP");
75
76 // Load the texture
77 GLuint Texture = loadDDS("uvtemplate.DDS");
78
79 // Get a handle for our "myTextureSampler" uniform
80 GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler");
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,-1.0f,-1.0f,
86 -1.0f,-1.0f, 1.0f,
87 -1.0f, 1.0f, 1.0f,
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 };
122
123 // Two UV coordinatesfor each vertex. They were created withe Blender.
124 static const GLfloat g_uv_buffer_data[] = {
125 0.000059f, 0.000004f,
126 0.000103f, 0.336048f,
127 0.335973f, 0.335903f,
128 1.000023f, 0.000013f,
129 0.667979f, 0.335851f,
130 0.999958f, 0.336064f,
131 0.667979f, 0.335851f,
132 0.336024f, 0.671877f,
133 0.667969f, 0.671889f,
134 1.000023f, 0.000013f,
135 0.668104f, 0.000013f,
136 0.667979f, 0.335851f,
137 0.000059f, 0.000004f,
138 0.335973f, 0.335903f,
139 0.336098f, 0.000071f,
140 0.667979f, 0.335851f,
141 0.335973f, 0.335903f,
142 0.336024f, 0.671877f,
143 1.000004f, 0.671847f,
144 0.999958f, 0.336064f,
145 0.667979f, 0.335851f,
146 0.668104f, 0.000013f,
147 0.335973f, 0.335903f,
148 0.667979f, 0.335851f,
149 0.335973f, 0.335903f,
150 0.668104f, 0.000013f,
151 0.336098f, 0.000071f,
152 0.000103f, 0.336048f,
153 0.000004f, 0.671870f,
154 0.336024f, 0.671877f,
155 0.000103f, 0.336048f,
156 0.336024f, 0.671877f,
157 0.335973f, 0.335903f,
158 0.667969f, 0.671889f,
159 1.000004f, 0.671847f,
160 0.667979f, 0.335851f
161 };
162
163 GLuint vertexbuffer;
164 glGenBuffers(1, &vertexbuffer);
165 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
166 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
167
168 GLuint uvbuffer;
169 glGenBuffers(1, &uvbuffer);
170 glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
171 glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
172
173 do{
174
175 // Clear the screen
176 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
177
178 // Use our shader
179 glUseProgram(programID);
180
181 // Compute the MVP matrix from keyboard and mouse input
182 computeMatricesFromInputs();
183 glm::mat4 ProjectionMatrix = getProjectionMatrix();
184 glm::mat4 ViewMatrix = getViewMatrix();
185 glm::mat4 ModelMatrix = glm::mat4(1.0);
186 glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
187
188 // Send our transformation to the currently bound shader,
189 // in the "MVP" uniform
190 glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
191
192 // Bind our texture in Texture Unit 0
193 glActiveTexture(GL_TEXTURE0);
194 glBindTexture(GL_TEXTURE_2D, Texture);
195 // Set our "myTextureSampler" sampler to user Texture Unit 0
196 glUniform1i(TextureID, 0);
197
198 // 1rst attribute buffer : vertices
199 glEnableVertexAttribArray(0);
200 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
201 glVertexAttribPointer(
202 0, // attribute. No particular reason for 0, but must match the layout in the shader.
203 3, // size
204 GL_FLOAT, // type
205 GL_FALSE, // normalized?
206 0, // stride
207 (void*)0 // array buffer offset
208 );
209
210 // 2nd attribute buffer : UVs
211 glEnableVertexAttribArray(1);
212 glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
213 glVertexAttribPointer(
214 1, // attribute. No particular reason for 1, but must match the layout in the shader.
215 2, // size : U+V => 2
216 GL_FLOAT, // type
217 GL_FALSE, // normalized?
218 0, // stride
219 (void*)0 // array buffer offset
220 );
221
222 // Draw the triangle !
223 glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles
224
225 glDisableVertexAttribArray(0);
226 glDisableVertexAttribArray(1);
227
228 // Swap buffers
229 glfwSwapBuffers();
230
231 } // Check if the ESC key was pressed or the window was closed
232 while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
233 glfwGetWindowParam( GLFW_OPENED ) );
234
235 // Cleanup VBO and shader
236 glDeleteBuffers(1, &vertexbuffer);
237 glDeleteBuffers(1, &uvbuffer);
238 glDeleteProgram(programID);
239 glDeleteTextures(1, &TextureID);
240 glDeleteVertexArrays(1, &VertexArrayID);
241
242 // Close OpenGL window and terminate GLFW
243 glfwTerminate();
244
245 return 0;
246}
Note: See TracBrowser for help on using the repository browser.