Mercurial > hg > forks > gldragon
annotate gldragon.cpp @ 42:3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
"define" the entrypoints we require and some wonderful preprocessor macro
magic to add checks for them via SDL_GL_ExtensionSupported() and initialize
function pointers with SDL_GL_GetProcAddress().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 05 Dec 2019 21:05:43 +0200 |
parents | 4de11a54215a |
children | d93b1c2690f8 |
rev | line source |
---|---|
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
1 // |
22
03b86b9c2f29
Add copyright blurbs and licenses.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
2 // GLDragon - OpenGL PLY model viewer / simple benchmark |
03b86b9c2f29
Add copyright blurbs and licenses.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
3 // Programmed and designed by Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
4 // (C) Copyright 2019 Tecnic Software productions (TNSP) |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
5 // |
22
03b86b9c2f29
Add copyright blurbs and licenses.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
6 // See file "COPYING" for license information. |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
7 // |
22
03b86b9c2f29
Add copyright blurbs and licenses.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
8 // Originally based on 'glxdragon' Copyright (c) 2009, Thomas Trummer |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
9 // |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
10 #include <SDL.h> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
11 #include <SDL_opengl.h> |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
12 #include <GL/glu.h> |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
13 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
14 #include "dmutil.h" |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
15 #include "dmmodel.h" |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
16 |
16 | 17 |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
18 /* Default settings etc. constants |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
19 */ |
18
b1e75c65016d
Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents:
17
diff
changeset
|
20 #define SET_DEF_WIDTH 1280 |
b1e75c65016d
Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents:
17
diff
changeset
|
21 #define SET_DEF_HEIGHT 960 |
b1e75c65016d
Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents:
17
diff
changeset
|
22 #define SET_FRAMES (180) |
b1e75c65016d
Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents:
17
diff
changeset
|
23 #define SET_MAX_SHADER_SIZE (128 * 1024) |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
24 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
25 |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
26 /* Options |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
27 */ |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
28 bool optUseShaders = false; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
29 int optWidth = SET_DEF_WIDTH, |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
30 optHeight = SET_DEF_HEIGHT, |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
31 optVSyncMode = 1; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
32 |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
33 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
34 /* Globals |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
35 */ |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
36 SDL_Window *dmWindow = NULL; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
37 SDL_GLContext dmGLContext = NULL; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
38 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
39 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
40 /* Helpers |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
41 */ |
42
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
42 #ifdef GL_GLEXT_PROTOTYPES |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
43 #define DM_GLEXT_INIT(extproctype, extprocname) /* stub */ |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
44 #else |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
45 // GL_GLEXT_PROTOTYPES not defined |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
46 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
47 #define DM_GLEXT_INIT(extproctype, extprocname) extproctype extprocname = NULL; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
48 #include "dmglexts.h" |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
49 #undef DM_GLEXT_INIT |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
50 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
51 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
52 void * dmGLExtTry(const std::string &name) |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
53 { |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
54 bool res = SDL_GL_ExtensionSupported(name.c_str()); |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
55 void *ptr = SDL_GL_GetProcAddress(name.c_str()); |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
56 res = true; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
57 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
58 printf(" - Checking '%s' : %s : %p\n", |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
59 name.c_str(), |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
60 res ? "YES" : "no", |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
61 ptr); |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
62 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
63 if (res && ptr != NULL) |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
64 return ptr; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
65 else |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
66 return NULL; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
67 } |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
68 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
69 void * dmGLExtInit(const std::string &name, bool &status) |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
70 { |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
71 void *ptr; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
72 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
73 if ((ptr = dmGLExtTry(name)) != NULL) |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
74 return ptr; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
75 else |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
76 if ((ptr = dmGLExtTry(name + "EXT")) != NULL) |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
77 return ptr; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
78 else |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
79 if ((ptr = dmGLExtTry(name + "ARB")) != NULL) |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
80 return ptr; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
81 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
82 status = false; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
83 return NULL; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
84 } |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
85 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
86 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
87 #define DM_GLEXT_INIT(extproctype, extprocname) \ |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
88 extprocname = (extproctype) dmGLExtInit(#extprocname, status); |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
89 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
90 #endif |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
91 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
92 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
93 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
94 bool dmInitGLExtensions(void) |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
95 { |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
96 #ifdef GL_GLEXT_PROTOTYPES |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
97 return true; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
98 #else |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
99 bool status = true; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
100 #include "dmglexts.h" |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
101 return status; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
102 #endif |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
103 } |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
104 |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
105 |
5 | 106 bool dmInitSDLGL(const int width, const int height, const char *title) |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
107 { |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
108 int ret; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
109 std::string msg; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
110 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
111 // Set GL attributes |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
112 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
113 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
114 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
115 |
2 | 116 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); |
117 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
118 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
119 // Attempt to initialize libSDL |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
120 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS) != 0) |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
121 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
122 printf("ERROR: Unable to initialize SDL: %s\n", |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
123 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
124 return false; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
125 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
126 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
127 // Attempt to create a window |
8 | 128 if ((dmWindow = SDL_CreateWindow(title, |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
129 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
130 width, height, |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
131 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE)) == NULL) |
2 | 132 { |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
133 printf("ERROR: Could not create SDL window: %s", |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
134 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
135 return false; |
2 | 136 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
137 |
8 | 138 if ((dmGLContext = SDL_GL_CreateContext(dmWindow)) == NULL) |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
139 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
140 printf("ERROR: Unable to create SDL OpenGL context: %s\n", |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
141 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
142 return false; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
143 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
144 |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
145 // Check if we want to attempt to use vsync |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
146 switch (optVSyncMode) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
147 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
148 case 3: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
149 ret = SDL_GL_SetSwapInterval(-1); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
150 msg = "adaptive vsync"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
151 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
152 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
153 case 2: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
154 ret = SDL_GL_SetSwapInterval(1); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
155 msg = "synchronized (vsync)"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
156 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
157 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
158 case 1: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
159 ret = SDL_GL_SetSwapInterval(0); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
160 msg = "immediate (no vsync)"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
161 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
162 |
38
372fa249ce26
Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
163 case 0: |
372fa249ce26
Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
164 msg = "vsync handling disabled"; |
372fa249ce26
Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
165 ret = 0; |
372fa249ce26
Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
166 break; |
372fa249ce26
Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
167 |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
168 default: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
169 ret = -1; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
170 msg = "INVALID VSYNC MODE"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
171 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
172 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
173 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
174 if (ret != 0) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
175 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
176 printf("ERROR: Could not set vsync mode to %s.\n", |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
177 msg.c_str()); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
178 return false; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
179 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
180 |
42
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
181 // Get/initialize OpenGL extension function pointers |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
182 if (optUseShaders && !dmInitGLExtensions()) |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
183 return false; |
3c7e1d3fa5a2
Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
184 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
185 // Dump some information |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
186 printf( |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
187 "GL_VENDOR : %s\n" |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
188 "GL_RENDERER : %s\n" |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
189 "GL_VERSION : %s\n" |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
190 "VSync mode : %s\n", |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
191 glGetString(GL_VENDOR), |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
192 glGetString(GL_RENDERER), |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
193 glGetString(GL_VERSION), |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
194 msg.c_str()); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
195 |
2 | 196 // Setup the window and view port |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
197 glViewport(0, 0, width, height); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
198 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
199 glMatrixMode(GL_PROJECTION); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
200 glLoadIdentity(); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
201 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
202 gluPerspective(45.0f, GLfloat(width) / GLfloat(height), 0.1f, 1000.0f); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
203 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
204 glMatrixMode(GL_MODELVIEW); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
205 glLoadIdentity(); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
206 |
2 | 207 // Enable back face culling |
208 glEnable(GL_CULL_FACE); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
209 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
210 // Enable smooth shading |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
211 glShadeModel(GL_SMOOTH); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
212 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
213 // Enable the depth buffer |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
214 glEnable(GL_DEPTH_TEST); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
215 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
216 // Enable normal rescaling |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
217 glEnable(GL_RESCALE_NORMAL); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
218 |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
219 // Setup depth buffer |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
220 glClearDepth(1.0f); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
221 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
222 // Set the depth buffer function |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
223 glDepthFunc(GL_LEQUAL); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
224 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
225 // Enable vertex and and normal arrays |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
226 glEnableClientState(GL_VERTEX_ARRAY); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
227 glEnableClientState(GL_NORMAL_ARRAY); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
228 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
229 // Set correct perspective correction |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
230 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
231 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
232 return true; |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
233 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
234 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
235 |
37 | 236 void dmDrawModel(const DMSimpleScene &scene, const DMModel &model) |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
237 { |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
238 int maxIndices; |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
239 |
37 | 240 if (optUseShaders) |
241 { | |
242 // Enable shader program | |
243 glUseProgram(model.id_prog); | |
244 glUniform1i(glGetUniformLocation(model.id_prog, "nlights"), scene.lights.size()); | |
245 } | |
246 | |
247 // Set the material of the model | |
32
1215fdd0a8ab
Add support for specifying specular and shininess values per model.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
248 glEnable(GL_COLOR_MATERIAL); |
1215fdd0a8ab
Add support for specifying specular and shininess values per model.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
249 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); |
1215fdd0a8ab
Add support for specifying specular and shininess values per model.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
250 glMateriali(GL_FRONT, GL_SHININESS, model.shininess); |
1215fdd0a8ab
Add support for specifying specular and shininess values per model.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
251 glMaterialfv(GL_FRONT, GL_SPECULAR, model.specular.values); |
37 | 252 glColor4fv(model.diffuse.values); |
32
1215fdd0a8ab
Add support for specifying specular and shininess values per model.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
253 |
17 | 254 // Render the model |
2 | 255 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
256 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
257 // Add transforms |
36
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
258 if (model.scaleSet) |
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
259 glScalef(model.scale.x, model.scale.y, model.scale.z); |
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
260 |
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
261 if (model.translateSet) |
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
262 glTranslatef(model.translate.x, model.translate.y, model.translate.z); |
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
263 |
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
264 if (model.rotateSet) |
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
265 { |
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
266 glRotatef(model.rotate.x, 1.0f, 0.0f, 0.0f); |
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
267 glRotatef(model.rotate.y, 0.0f, 1.0f, 0.0f); |
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
268 glRotatef(model.rotate.z, 0.0f, 0.0f, 1.0f); |
d640f2a34031
Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
269 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
270 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
271 glVertexPointer(3, GL_FLOAT, 0, &model.vertices[0]); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
272 glNormalPointer( GL_FLOAT, 0, &model.normals[0]); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
273 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
274 for (int n = 0; n < model.nfaces; n += maxIndices) |
2 | 275 { |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
276 const int count = std::min(maxIndices, model.nfaces - n); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
277 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &model.faces[n * 3]); |
2 | 278 } |
37 | 279 |
280 // Restore | |
281 if (optUseShaders) | |
282 { | |
283 glUseProgram(0); | |
284 } | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
285 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
286 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
287 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
288 void dmDrawScene(const DMSimpleScene &scene) |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
289 { |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
290 glClear(GL_DEPTH_BUFFER_BIT); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
291 |
2 | 292 glMatrixMode(GL_PROJECTION); |
293 glPushMatrix(); | |
294 glLoadIdentity(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
295 |
2 | 296 glOrtho(0.0, 1.0, 0.0, 1.0, -1, 1); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
297 |
2 | 298 glMatrixMode(GL_MODELVIEW); |
299 glPushMatrix(); | |
300 glLoadIdentity(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
301 |
17 | 302 // Draw the background gradient |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
303 glDisable(GL_DEPTH_TEST); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
304 glDisable(GL_LIGHTING); |
2 | 305 glBegin(GL_QUADS); |
306 { | |
307 glColor3ub(0x3B, 0x3B, 0x75); | |
308 glVertex2f(0.0f, 0.0f); | |
309 glVertex2f(1.0f, 0.0f); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
310 |
2 | 311 glColor3ub(0x00, 0x00, 0x00); |
312 glVertex2f(1.0f, 1.0f); | |
313 glVertex2f(0.0f, 1.0f); | |
314 } | |
315 glEnd(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
316 |
2 | 317 // Restore the 3D projection |
318 glMatrixMode(GL_PROJECTION); | |
319 glPopMatrix(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
320 |
2 | 321 glMatrixMode(GL_MODELVIEW); |
322 glPopMatrix(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
323 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
324 glEnable(GL_DEPTH_TEST); |
37 | 325 glEnable(GL_LIGHTING); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
326 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
327 // Draw models |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
328 for (const DMModel &model : scene.models) |
27
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
329 { |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
330 glPushMatrix(); |
37 | 331 dmDrawModel(scene, model); |
27
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
332 glPopMatrix(); |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
333 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
334 } |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
335 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
336 |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
337 bool dmCompileShader(const GLenum stype, const std::string &src, GLuint &shader) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
338 { |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
339 GLint status; |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
340 const char *tmp = src.c_str(); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
341 |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
342 shader = glCreateShader(stype); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
343 glShaderSource(shader, 1, &tmp, 0); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
344 glCompileShader(shader); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
345 |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
346 glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
347 if (status == GL_TRUE) |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
348 return true; |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
349 else |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
350 { |
26
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
351 GLint bufLen = 0; |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
352 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &bufLen); |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
353 |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
354 if (bufLen > 0) |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
355 { |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
356 char *buf = new char[bufLen]; |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
357 glGetShaderInfoLog(shader, bufLen, NULL, buf); |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
358 printf("ERROR: Shader compliation error:\n%s\n", |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
359 buf); |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
360 delete buf; |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
361 } |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
362 else |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
363 { |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
364 printf("ERROR: Shader compilation error occured, but no error information got.\n"); |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
365 } |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
366 return false; |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
367 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
368 } |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
369 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
370 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
371 void dmLinkModelShaders(DMModel &model) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
372 { |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
373 model.id_prog = glCreateProgram(); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
374 glAttachShader(model.id_prog, model.id_fs); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
375 glAttachShader(model.id_prog, model.id_vs); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
376 glLinkProgram(model.id_prog); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
377 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
378 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
379 |
25
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
380 void dmSetupLight(const int n, const DMLight &light) |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
381 { |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
382 glEnable(GL_LIGHT0 + n); |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
383 glLightfv(GL_LIGHT0 + n, GL_AMBIENT, light.ambient.values); |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
384 glLightfv(GL_LIGHT0 + n, GL_DIFFUSE, light.diffuse.values); |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
385 glLightfv(GL_LIGHT0 + n, GL_SPECULAR, light.specular.values); |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
386 glLightfv(GL_LIGHT0 + n, GL_POSITION, light.position.values); |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
387 } |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
388 |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
389 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
390 int main(int argc, char *argv[]) |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
391 { |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
392 int startTime, cycleStart, cycleFrames = 0, totalFrames = 0; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
393 double totalTime; |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
394 bool |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
395 exitFlag = false, |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
396 optShowHelp = false, |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
397 optSetInputFilename = false; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
398 std::string optInputFilename = "dragon.scene", basePath; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
399 DMSimpleScene scene; |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
400 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
401 // Check commandline argument for enabling shaders |
7 | 402 for (int narg = 1; narg < argc; narg++) |
403 { | |
404 char *arg = argv[narg]; | |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
405 if (arg[0] == '-') |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
406 { |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
407 char *opt = arg + 1; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
408 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
409 if ((opt[0] == '-' && opt[1] == 'h' && opt[2] == 'e') || |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
410 opt[0] == '?' || (opt[0] == '-' && opt[1] == '?')) |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
411 { |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
412 optShowHelp = true; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
413 break; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
414 } |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
415 else |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
416 if (opt[0] == '-') |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
417 opt++; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
418 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
419 if (opt[0] == 'g') |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
420 optUseShaders = true; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
421 else |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
422 switch (opt[0]) |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
423 { |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
424 case 'w': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
425 case 'h': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
426 case 'm': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
427 case 'v': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
428 if (opt[1] == 0) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
429 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
430 printf("Option '%s' requires an argument.\n", opt); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
431 goto exit; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
432 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
433 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
434 switch (opt[0]) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
435 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
436 case 'w': optWidth = atoi(opt + 1); break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
437 case 'h': optHeight = atoi(opt + 1); break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
438 case 'v': optVSyncMode = atoi(opt + 1); break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
439 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
440 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
441 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
442 default: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
443 printf("Unknown option '%s'.\n", opt); |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
444 goto exit; |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
445 } |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
446 } |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
447 else |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
448 { |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
449 if (optSetInputFilename) |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
450 { |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
451 printf("ERROR: Please specify only one scene file.\n"); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
452 goto exit; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
453 } |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
454 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
455 optSetInputFilename = true; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
456 optInputFilename = std::string(arg); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
457 if (optInputFilename.empty()) |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
458 { |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
459 printf("ERROR: Invalid input filename.\n"); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
460 goto exit; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
461 } |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
462 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
463 } |
7 | 464 } |
465 | |
8 | 466 if (optShowHelp) |
7 | 467 { |
468 printf( | |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
469 "Usage: %s [options] [<scenefile.scene>]\n" |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
470 "-? Show this help\n" |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
471 "-g Use GLSL shader instead of basic OpenGL lighting\n" |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
472 "-w<width> Window width (default %d)\n" |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
473 "-h<height> Window height (default %d)\n" |
38
372fa249ce26
Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
474 "-v<0-3> Set vsync mode: 0 = do not attempt to set vsync mode\n" |
372fa249ce26
Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
475 " (may be required for Mesa software rendering),\n" |
372fa249ce26
Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
476 " 1 = no vsync, 2 = vsync, 3 = adaptive. Default is 1\n" |
372fa249ce26
Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
477 " (no vsync). Using vsync will result in FPS being\n" |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
478 " approx whatever your monitor refresh rate is.\n" |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
479 "\n", |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
480 argv[0], |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
481 SET_DEF_WIDTH, SET_DEF_HEIGHT |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
482 ); |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
483 |
7 | 484 goto exit; |
485 } | |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
486 |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
487 if (optWidth < 100 || optWidth > 8192 || optHeight < 100 || optHeight > 8192) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
488 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
489 printf("ERROR: Invalid window width or height (%d x %d).\n", |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
490 optWidth, optHeight); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
491 goto exit; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
492 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
493 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
494 // Load the scene |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
495 if (!scene.loadInfo(optInputFilename)) |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
496 goto exit; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
497 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
498 if (scene.models.size() == 0) |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
499 { |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
500 printf("ERROR: Scenefile '%s' contains no models.\n", |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
501 optInputFilename.c_str()); |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
502 goto exit; |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
503 } |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
504 |
31
6847715b46cd
Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
505 // Define a default light if none defined in scene file |
6847715b46cd
Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
506 if (scene.lights.size() == 0) |
6847715b46cd
Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
507 { |
6847715b46cd
Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
508 DMLight light; // Default light |
6847715b46cd
Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
509 scene.lights.push_back(light); |
6847715b46cd
Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
510 } |
6847715b46cd
Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
511 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
512 printf("INFO: Loading %ld model(s) ..\n", |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
513 scene.models.size()); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
514 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
515 basePath = dmGetPath(optInputFilename); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
516 printf("INFO: Model base path '%s'\n", basePath.c_str()); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
517 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
518 for (DMModel &model : scene.models) |
2 | 519 { |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
520 if (!model.loadFromPLY(basePath + model.modelFile)) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
521 goto exit; |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
522 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
523 if (optUseShaders) |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
524 { |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
525 std::string |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
526 fragFile = model.fragShaderFile.empty() ? "shader.frag" : basePath + model.fragShaderFile, |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
527 vertFile = model.vertShaderFile.empty() ? "shader.vert" : basePath + model.vertShaderFile; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
528 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
529 if (!dmReadText(fragFile, model.fragShaderStr, SET_MAX_SHADER_SIZE) || |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
530 !dmReadText(vertFile, model.vertShaderStr, SET_MAX_SHADER_SIZE)) |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
531 goto exit; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
532 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
533 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
534 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
535 // Initialize SDL + OpenGL |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
536 if (!dmInitSDLGL(optWidth, optHeight, "GLDragon")) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
537 goto exit; |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
538 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
539 // According to our mode .. |
8 | 540 if (optUseShaders) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
541 { |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
542 for (DMModel &model : scene.models) |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
543 { |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
544 if (!dmCompileShader(GL_FRAGMENT_SHADER, model.fragShaderStr, model.id_fs) || |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
545 !dmCompileShader(GL_VERTEX_SHADER, model.vertShaderStr, model.id_vs)) |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
546 goto exit; |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
547 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
548 dmLinkModelShaders(model); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
549 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
550 } |
25
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
551 |
27
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
552 // Setup lights |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
553 for (size_t n = 0; n < scene.lights.size(); n++) |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
554 dmSetupLight(n, scene.lights[n]); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
555 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
556 // Define the camera |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
557 gluLookAt(0, 0.12, 0.24, 0, 0.12, 0, 0, 1, 0); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
558 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
559 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
560 // Main loop starts |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
561 startTime = cycleStart = SDL_GetTicks(); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
562 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
563 while (!exitFlag) |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
564 { |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
565 SDL_Event event; |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
566 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
567 // Check for quit events |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
568 while (SDL_PollEvent(&event)) |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
569 switch (event.type) |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
570 { |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
571 case SDL_QUIT: |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
572 exitFlag = true; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
573 break; |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
574 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
575 case SDL_KEYDOWN: |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
576 switch (event.key.keysym.sym) |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
577 { |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
578 case SDLK_ESCAPE: |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
579 case SDLK_q: |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
580 exitFlag = true; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
581 break; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
582 } |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
583 } |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
584 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
585 // Render the next frame |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
586 dmDrawScene(scene); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
587 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
588 // Draw the current frame |
8 | 589 SDL_GL_SwapWindow(dmWindow); |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
590 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
591 // Rotate for 2 degrees |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
592 glRotatef(2.0f, 0, 1, 0); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
593 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
594 // Return true if a full rotation was done |
15
2d2aadfa3df3
Fix calculation of totalFrames.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
595 totalFrames++; |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
596 if (cycleFrames++ == SET_FRAMES) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
597 { |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
598 // Reset cycleFrames |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
599 cycleFrames = 0; |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
600 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
601 // Get the time it took to render a full turn |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
602 int cycleEnd = SDL_GetTicks(); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
603 double cycleTime = cycleEnd - cycleStart; |
10
c6c419861101
Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
604 |
c6c419861101
Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
605 // Restart the timer |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
606 cycleStart = SDL_GetTicks(); |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
607 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
608 // Print the current frames per second |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
609 printf("%.1lf ms for %d frames = %.1lf FPS\n", |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
610 cycleTime, SET_FRAMES, (SET_FRAMES * 1000.0f) / cycleTime); |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
611 } |
2 | 612 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
613 |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
614 // Show totals |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
615 totalTime = SDL_GetTicks() - startTime; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
616 printf("%.1lf ms total for %d total frames = %.2lf FPS average\n", |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
617 totalTime, totalFrames, (totalFrames * 1000.0f) / totalTime); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
618 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
619 exit: |
8 | 620 if (dmGLContext != NULL) |
621 SDL_GL_DeleteContext(dmGLContext); | |
5 | 622 |
8 | 623 if (dmWindow != NULL) |
624 SDL_DestroyWindow(dmWindow); | |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
625 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
626 SDL_Quit(); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
627 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
628 return 0; |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
629 } |