source: network-game/graphics_library/main.cpp@ af116c0

Last change on this file since af116c0 was af116c0, checked in by Dmitry Portnoy <dmp1488@…>, 10 years ago

new graphics library code

  • Property mode set to 100644
File size: 9.4 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/texture.hpp"
19#include "common/controls.hpp"
20
21int main( void )
22{
23 // Initialise GLFW
24 if( !glfwInit() )
25 {
26 fprintf( stderr, "Failed to initialize GLFW\n" );
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_PROFILE, GLFW_OPENGL_CORE_PROFILE);
34
35 // Open a window and create its OpenGL context
36 window = glfwCreateWindow( 1024, 768, "Tutorial 0 - Keyboard and Mouse", 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 glfwTerminate();
40 return -1;
41 }
42 glfwMakeContextCurrent(window);
43
44 // Initialize GLEW
45 glewExperimental = true; // Needed for core profile
46 if (glewInit() != GLEW_OK) {
47 fprintf(stderr, "Failed to initialize GLEW\n");
48 return -1;
49 }
50
51 // Ensure we can capture the escape key being pressed below
52 glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
53 glfwSetCursorPos(window, 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(window);
230 glfwPollEvents();
231
232 } // Check if the ESC key was pressed or the window was closed
233 while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
234 glfwWindowShouldClose(window) == 0 );
235
236 // Cleanup VBO and shader
237 glDeleteBuffers(1, &vertexbuffer);
238 glDeleteBuffers(1, &uvbuffer);
239 glDeleteProgram(programID);
240 glDeleteTextures(1, &TextureID);
241 glDeleteVertexArrays(1, &VertexArrayID);
242
243 // Close OpenGL window and terminate GLFW
244 glfwTerminate();
245
246 return 0;
247}
Note: See TracBrowser for help on using the repository browser.