comparison glxdragon.cpp @ 8:628c42e94149

Clean up variable names.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 27 Oct 2019 22:24:43 +0200
parents 95dd5417e7de
children c6c419861101
comparison
equal deleted inserted replaced
7:95dd5417e7de 8:628c42e94149
42 42
43 43
44 #define SET_FRAMES (180 * 2) 44 #define SET_FRAMES (180 * 2)
45 45
46 46
47 bool opt_shaders = false; 47 bool optUseShaders = false;
48 SDL_Window *sdl_window = NULL; 48 SDL_Window *dmWindow = NULL;
49 SDL_GLContext sdl_glctx = NULL; 49 SDL_GLContext dmGLContext = NULL;
50 50
51 51
52 struct Mesh 52 struct Mesh
53 { 53 {
54 int nvertices, nfaces; 54 int nvertices, nfaces;
77 SDL_GetError()); 77 SDL_GetError());
78 return false; 78 return false;
79 } 79 }
80 80
81 // Attempt to create a window 81 // Attempt to create a window
82 if ((sdl_window = SDL_CreateWindow(title, 82 if ((dmWindow = SDL_CreateWindow(title,
83 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 83 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
84 width, height, 84 width, height,
85 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE)) == NULL) 85 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE)) == NULL)
86 { 86 {
87 printf("ERROR: Could not create SDL window: %s", 87 printf("ERROR: Could not create SDL window: %s",
88 SDL_GetError()); 88 SDL_GetError());
89 return false; 89 return false;
90 } 90 }
91 91
92 if ((sdl_glctx = SDL_GL_CreateContext(sdl_window)) == NULL) 92 if ((dmGLContext = SDL_GL_CreateContext(dmWindow)) == NULL)
93 { 93 {
94 printf("ERROR: Unable to create SDL OpenGL context: %s\n", 94 printf("ERROR: Unable to create SDL OpenGL context: %s\n",
95 SDL_GetError()); 95 SDL_GetError());
96 return false; 96 return false;
97 } 97 }
200 glPopMatrix(); 200 glPopMatrix();
201 201
202 glEnable(GL_DEPTH_TEST); 202 glEnable(GL_DEPTH_TEST);
203 203
204 // Render the model 204 // Render the model
205 if (opt_shaders) 205 if (optUseShaders)
206 { 206 {
207 // Enable shader program 207 // Enable shader program
208 glUseProgram(mesh.id_prog); 208 glUseProgram(mesh.id_prog);
209 dmDrawModelVA(mesh); 209 dmDrawModelVA(mesh);
210 glUseProgram(0); 210 glUseProgram(0);
256 in.read(reinterpret_cast<char*>(&mesh.vertices[0]), mesh.nvertices * 6 * 4); 256 in.read(reinterpret_cast<char*>(&mesh.vertices[0]), mesh.nvertices * 6 * 4);
257 257
258 mesh.nfaces = nfaces; 258 mesh.nfaces = nfaces;
259 mesh.faces.resize(mesh.nfaces * 3); 259 mesh.faces.resize(mesh.nfaces * 3);
260 260
261 for (int i = 0; i < nfaces; i++) 261 for (int i = 0; i < mesh.nfaces; i++)
262 { 262 {
263 in.seekg(1, std::ios::cur); 263 in.seekg(1, std::ios::cur);
264 in.read(reinterpret_cast<char*>(&mesh.faces[i * 3]), 3 * 4); 264 in.read(reinterpret_cast<char*>(&mesh.faces[i * 3]), 3 * 4);
265 } 265 }
266 266
267 return true; 267 return true;
268 } 268 }
269 269
270 270
271 GLuint dmSetCompileShader(const GLenum stype, const std::string &src) 271 GLuint dmCompileShader(const GLenum stype, const std::string &src)
272 { 272 {
273 GLuint shader = glCreateShader(stype); 273 GLuint shader = glCreateShader(stype);
274 const char *tmp = src.c_str(); 274 const char *tmp = src.c_str();
275 275
276 glShaderSource(shader, 1, &tmp, 0); 276 glShaderSource(shader, 1, &tmp, 0);
292 int main(int argc, char *argv[]) 292 int main(int argc, char *argv[])
293 { 293 {
294 struct Mesh dragonMesh; 294 struct Mesh dragonMesh;
295 std::string dragonVS, dragonFS; 295 std::string dragonVS, dragonFS;
296 std::clock_t startTime; 296 std::clock_t startTime;
297 bool exitFlag = false, opt_showhelp = false; 297 bool exitFlag = false, optShowHelp = false;
298 int steps = 0; 298 int nframes = 0;
299 299
300 // Check commandline argument for enabling shaders 300 // Check commandline argument for enabling shaders
301 301
302 for (int narg = 1; narg < argc; narg++) 302 for (int narg = 1; narg < argc; narg++)
303 { 303 {
304 char *arg = argv[narg]; 304 char *arg = argv[narg];
305 if (strstr(arg, "help") != NULL || 305 if (strstr(arg, "help") != NULL ||
306 strstr(arg, "?") != NULL) 306 strstr(arg, "?") != NULL)
307 opt_showhelp = true; 307 optShowHelp = true;
308 else 308 else
309 if (strstr(arg, "glsl") != NULL || 309 if (strstr(arg, "glsl") != NULL ||
310 strstr(arg, "sha") != NULL) 310 strstr(arg, "sha") != NULL)
311 opt_shaders = true; 311 optUseShaders = true;
312 } 312 }
313 313
314 if (opt_showhelp) 314 if (optShowHelp)
315 { 315 {
316 printf( 316 printf(
317 "Usage: %s [glsl]\n" 317 "Usage: %s [glsl]\n"
318 "glsl argument will turn on GLSL shader mode instead of basic OpenGL lighting.\n" 318 "glsl argument will turn on GLSL shader mode instead of basic OpenGL lighting.\n"
319 , argv[0]); 319 , argv[0]);
321 } 321 }
322 322
323 if (!dmLoadMesh("dragon.mesh", dragonMesh, 100139, 200198)) 323 if (!dmLoadMesh("dragon.mesh", dragonMesh, 100139, 200198))
324 goto exit; 324 goto exit;
325 325
326 if (opt_shaders) 326 if (optUseShaders)
327 { 327 {
328 // Read shader files 328 // Read shader files
329 if (!dmReadText("dragon.frag", dragonFS) || 329 if (!dmReadText("dragon.frag", dragonFS) ||
330 !dmReadText("dragon.vert", dragonVS)) 330 !dmReadText("dragon.vert", dragonVS))
331 goto exit; 331 goto exit;
334 // Initialize SDL + OpenGL 334 // Initialize SDL + OpenGL
335 if (!dmInitSDLGL(1280, 960, "GLXDragon2")) 335 if (!dmInitSDLGL(1280, 960, "GLXDragon2"))
336 goto exit; 336 goto exit;
337 337
338 // According to our mode .. 338 // According to our mode ..
339 if (opt_shaders) 339 if (optUseShaders)
340 { 340 {
341 dragonMesh.id_ps = dmSetCompileShader(GL_FRAGMENT_SHADER, dragonFS); 341 dragonMesh.id_ps = dmCompileShader(GL_FRAGMENT_SHADER, dragonFS);
342 dragonMesh.id_vs = dmSetCompileShader(GL_VERTEX_SHADER, dragonVS); 342 dragonMesh.id_vs = dmCompileShader(GL_VERTEX_SHADER, dragonVS);
343 dmLinkMeshShaders(dragonMesh); 343 dmLinkMeshShaders(dragonMesh);
344 } 344 }
345 else 345 else
346 { 346 {
347 float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f }; 347 float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f };
399 399
400 // Render the next frame 400 // Render the next frame
401 dmPaintGL(dragonMesh); 401 dmPaintGL(dragonMesh);
402 402
403 // Draw the current frame 403 // Draw the current frame
404 SDL_GL_SwapWindow(sdl_window); 404 SDL_GL_SwapWindow(dmWindow);
405 405
406 // Rotate for 2 degrees 406 // Rotate for 2 degrees
407 glRotatef(2.0f, 0, 1, 0); 407 glRotatef(2.0f, 0, 1, 0);
408 408
409 // Return true if a full rotation was done 409 // Return true if a full rotation was done
410 if (steps++ == SET_FRAMES) 410 if (nframes++ == SET_FRAMES)
411 { 411 {
412 // Reset steps 412 // Reset nframes
413 steps = 0; 413 nframes = 0;
414 414
415 // Get the time it took to render a full turn 415 // Get the time it took to render a full turn
416 double time = (double(std::clock() - startTime) * 1000.0f) / CLOCKS_PER_SEC; 416 double time = (double(std::clock() - startTime) * 1000.0f) / CLOCKS_PER_SEC;
417 417
418 // Print the current frames per second 418 // Print the current frames per second
423 startTime = std::clock(); 423 startTime = std::clock();
424 } 424 }
425 } 425 }
426 426
427 exit: 427 exit:
428 if (sdl_glctx != NULL) 428 if (dmGLContext != NULL)
429 SDL_GL_DeleteContext(sdl_glctx); 429 SDL_GL_DeleteContext(dmGLContext);
430 430
431 if (sdl_window != NULL) 431 if (dmWindow != NULL)
432 SDL_DestroyWindow(sdl_window); 432 SDL_DestroyWindow(dmWindow);
433 433
434 SDL_Quit(); 434 SDL_Quit();
435 435
436 return 0; 436 return 0;
437 } 437 }