source: network-game/graphics_library/main.cpp@ 2ce5154

Last change on this file since 2ce5154 was 2ce5154, checked in by dportnoy <dmp1488@…>, 11 years ago

new OpenGL program added, that might replace the client's 2D graphics someday

  • Property mode set to 100644
File size: 10.9 KB
Line 
1#include <cstdio>
2#include <cstdlib>
3#include <string>
4#include <fstream>
5#include <vector>
6#include <algorithm>
7
8#include <GL/glew.h>
9#include <GL/glfw.h>
10
11#include <glm/glm.hpp>
12#include <glm/gtc/matrix_transform.hpp>
13using namespace glm;
14
15using namespace std;
16
17/*
18
19Rendering Pipeline
20
21All models are defined
22All vertices in each model are in model space
23
24Apply the model transformation to each model (based on model position, rotation, and scaling)
25This results in all vertices being in world space
26
27Apply the view transformation (based on camera position and rotation)
28Vertices are in camera coordinates
29
30Apply the projection matrix (camera lens angle, clipping planes) to turn coordinates into screen coordinates
31
32*/
33
34GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path);
35
36int main() {
37 // Initialise GLFW
38 if( !glfwInit() )
39 {
40 fprintf( stderr, "Failed to initialize GLFW\n" );
41 return -1;
42 }
43
44 glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
45 glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
46 glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
47 glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
48
49 // Open a window and create its OpenGL context
50 if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
51 {
52 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" );
53 glfwTerminate();
54 return -1;
55 }
56
57 // Initialize GLEW
58 glewExperimental = true; // Needed for core profile
59 if (glewInit() != GLEW_OK) {
60 fprintf(stderr, "Failed to initialize GLEW\n");
61 return -1;
62 }
63
64 glfwSetWindowTitle( "Tutorial 02" );
65
66 // Ensure we can capture the escape key being pressed below
67 glfwEnable( GLFW_STICKY_KEYS );
68
69 // Dark blue background
70 glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
71
72 // Enable depth test
73 glEnable(GL_DEPTH_TEST);
74 // Accept fragment if it closer to the camera than the former one
75 glDepthFunc(GL_LESS);
76
77 GLuint VertexArrayID;
78 glGenVertexArrays(1, &VertexArrayID);
79 glBindVertexArray(VertexArrayID);
80
81 // Create and compile our GLSL program from the shaders
82 GLuint programID = LoadShaders( "SimpleTransform.vertexshader", "SingleColor.fragmentshader" );
83
84 // Get a handle for our "MVP" uniform
85 GLuint MatrixID = glGetUniformLocation(programID, "MVP");
86
87 // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
88 glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
89 // Or, for an ortho camera :
90 //glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates
91
92 // Camera matrix
93 glm::mat4 View = glm::lookAt(
94 glm::vec3(4,3,3), // Camera is at (4,3,3), in World Space
95 glm::vec3(0,0,0), // and looks at the origin
96 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
97 );
98
99 // Model matrix : an identity matrix (model will be at the origin)
100 glm::mat4 Model = glm::mat4(1.0f);
101 // Our ModelViewProjection : multiplication of our 3 matrices
102 glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
103
104 // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
105 // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
106 static const GLfloat g_vertex_buffer_data[] = {
107 -1.0f,-1.0f,-1.0f, // triangle 1 : begin
108 -1.0f,-1.0f, 1.0f,
109 -1.0f, 1.0f, 1.0f, // triangle 1 : end
110 1.0f, 1.0f,-1.0f, // triangle 2 : begin
111 -1.0f,-1.0f,-1.0f,
112 -1.0f, 1.0f,-1.0f, // triangle 2 : end
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 1.0f,-1.0f,-1.0f,
132 1.0f, 1.0f, 1.0f,
133 1.0f,-1.0f, 1.0f,
134 1.0f, 1.0f, 1.0f,
135 1.0f, 1.0f,-1.0f,
136 -1.0f, 1.0f,-1.0f,
137 1.0f, 1.0f, 1.0f,
138 -1.0f, 1.0f,-1.0f,
139 -1.0f, 1.0f, 1.0f,
140 1.0f, 1.0f, 1.0f,
141 -1.0f, 1.0f, 1.0f,
142 1.0f,-1.0f, 1.0f
143 };
144
145 GLuint vertexbuffer;
146 glGenBuffers(1, &vertexbuffer);
147 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
148 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
149
150 // One color for each vertex. They were generated randomly.
151 static const GLfloat g_color_buffer_data[] = {
152 0.583f, 0.771f, 0.014f,
153 0.609f, 0.115f, 0.436f,
154 0.327f, 0.483f, 0.844f,
155 0.822f, 0.569f, 0.201f,
156 0.435f, 0.602f, 0.223f,
157 0.310f, 0.747f, 0.185f,
158 0.597f, 0.770f, 0.761f,
159 0.559f, 0.436f, 0.730f,
160 0.359f, 0.583f, 0.152f,
161 0.483f, 0.596f, 0.789f,
162 0.559f, 0.861f, 0.639f,
163 0.195f, 0.548f, 0.859f,
164 0.014f, 0.184f, 0.576f,
165 0.771f, 0.328f, 0.970f,
166 0.406f, 0.615f, 0.116f,
167 0.676f, 0.977f, 0.133f,
168 0.971f, 0.572f, 0.833f,
169 0.140f, 0.616f, 0.489f,
170 0.997f, 0.513f, 0.064f,
171 0.945f, 0.719f, 0.592f,
172 0.543f, 0.021f, 0.978f,
173 0.279f, 0.317f, 0.505f,
174 0.167f, 0.620f, 0.077f,
175 0.347f, 0.857f, 0.137f,
176 0.055f, 0.953f, 0.042f,
177 0.714f, 0.505f, 0.345f,
178 0.783f, 0.290f, 0.734f,
179 0.722f, 0.645f, 0.174f,
180 0.302f, 0.455f, 0.848f,
181 0.225f, 0.587f, 0.040f,
182 0.517f, 0.713f, 0.338f,
183 0.053f, 0.959f, 0.120f,
184 0.393f, 0.621f, 0.362f,
185 0.673f, 0.211f, 0.457f,
186 0.820f, 0.883f, 0.371f,
187 0.982f, 0.099f, 0.879f
188 };
189
190 GLuint colorbuffer;
191 glGenBuffers(1, &colorbuffer);
192 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
193 glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
194
195 do{
196
197 // Clear the screen
198 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
199
200 // Use our shader
201 glUseProgram(programID);
202
203 // Send our transformation to the currently bound shader,
204 // in the "MVP" uniform
205 glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
206
207 // 1rst attribute buffer : vertices
208 glEnableVertexAttribArray(0);
209 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
210 glVertexAttribPointer(
211 0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
212 3, // size
213 GL_FLOAT, // type
214 GL_FALSE, // normalized?
215 0, // stride
216 (void*)0 // array buffer offset
217 );
218
219 // 2nd attribute buffer : colors
220 glEnableVertexAttribArray(1);
221 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
222 glVertexAttribPointer(
223 1, // attribute. No particular reason for 1, but must match the layout in the shader.
224 3, // size
225 GL_FLOAT, // type
226 GL_FALSE, // normalized?
227 0, // stride
228 (void*)0 // array buffer offset
229 );
230
231 // Draw the triangle !
232 glDrawArrays(GL_TRIANGLES, 0, 12*3);
233
234 glDisableVertexAttribArray(0);
235
236 // Swap buffers
237 glfwSwapBuffers();
238
239 } // Check if the ESC key was pressed or the window was closed
240 while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
241 glfwGetWindowParam( GLFW_OPENED ) );
242
243 // Cleanup VBO
244 glDeleteBuffers(1, &vertexbuffer);
245 glDeleteProgram(programID);
246 glDeleteVertexArrays(1, &VertexArrayID);
247
248 // Close OpenGL window and terminate GLFW
249 glfwTerminate();
250
251 return 0;
252}
253
254GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path) {
255
256 // Create the shaders
257 GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
258 GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
259
260 // Read the Vertex Shader code from the file
261 string VertexShaderCode;
262 ifstream VertexShaderStream(vertex_file_path, ios::in);
263 if(VertexShaderStream.is_open()){
264 string Line = "";
265 while(getline(VertexShaderStream, Line))
266 VertexShaderCode += "\n" + Line;
267 VertexShaderStream.close();
268 }else {
269 printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
270 getchar();
271 return 0;
272 }
273
274 // Read the Fragment Shader code from the file
275 string FragmentShaderCode;
276 ifstream FragmentShaderStream(fragment_file_path, ios::in);
277 if(FragmentShaderStream.is_open()) {
278 string Line = "";
279 while(getline(FragmentShaderStream, Line))
280 FragmentShaderCode += "\n" + Line;
281 FragmentShaderStream.close();
282 }
283
284
285
286 GLint Result = GL_FALSE;
287 int InfoLogLength;
288
289
290
291 // Compile Vertex Shader
292 printf("Compiling shader : %s\n", vertex_file_path);
293 char const * VertexSourcePointer = VertexShaderCode.c_str();
294 glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
295 glCompileShader(VertexShaderID);
296
297 // Check Vertex Shader
298 glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
299 glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
300 if ( InfoLogLength > 0 ){
301 vector<char> VertexShaderErrorMessage(InfoLogLength+1);
302 glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
303 printf("%s\n", &VertexShaderErrorMessage[0]);
304 }
305
306
307
308 // Compile Fragment Shader
309 printf("Compiling shader : %s\n", fragment_file_path);
310 char const * FragmentSourcePointer = FragmentShaderCode.c_str();
311 glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
312 glCompileShader(FragmentShaderID);
313
314 // Check Fragment Shader
315 glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
316 glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
317 if ( InfoLogLength > 0 ){
318 vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
319 glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
320 printf("%s\n", &FragmentShaderErrorMessage[0]);
321 }
322
323
324
325 // Link the program
326 printf("Linking program\n");
327 GLuint ProgramID = glCreateProgram();
328 glAttachShader(ProgramID, VertexShaderID);
329 glAttachShader(ProgramID, FragmentShaderID);
330 glLinkProgram(ProgramID);
331
332 // Check the program
333 glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
334 glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
335 if ( InfoLogLength > 0 ){
336 vector<char> ProgramErrorMessage(InfoLogLength+1);
337 glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
338 printf("%s\n", &ProgramErrorMessage[0]);
339 }
340
341 glDeleteShader(VertexShaderID);
342 glDeleteShader(FragmentShaderID);
343
344 return ProgramID;
345}
Note: See TracBrowser for help on using the repository browser.