Mercurial > hg > forks > gldragon
annotate glxdragon.cpp @ 17:0fa9302e324d
Cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 05 Nov 2019 10:48:22 +0200 |
parents | c134a186912f |
children | b1e75c65016d |
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 // |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
2 // Copyright (c) 2009, Thomas Trummer |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
3 // All rights reserved. |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
4 // |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
5 // Port to libSDL2 and cleanups by Matti Hämäläinen <ccr@tnsp.org> |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
6 // (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
|
7 // |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
8 // Redistribution and use in source and binary forms, with or without |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
9 // modification, are permitted provided that the following conditions are met: |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
10 // * Redistributions of source code must retain the above copyright |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
11 // notice, this list of conditions and the following disclaimer. |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
12 // * Redistributions in binary form must reproduce the above copyright |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
13 // notice, this list of conditions and the following disclaimer in the |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
14 // documentation and/or other materials provided with the distribution. |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
15 // * Neither the name of the <organization> nor the |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
16 // names of its contributors may be used to endorse or promote products |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
17 // derived from this software without specific prior written permission. |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
18 // |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
19 // THIS SOFTWARE IS PROVIDED BY Thomas Trummer ''AS IS'' AND ANY |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
21 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
22 // DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
23 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
24 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
25 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
26 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
28 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
29 // |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
30 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
31 #include <SDL.h> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
32 #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
|
33 #include <GL/glu.h> |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
34 #include <GL/glext.h> |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
35 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
36 #include <iostream> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
37 #include <fstream> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
38 #include <string> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
39 #include <vector> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
40 #include <cstdio> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
41 |
16 | 42 |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
43 /* Default settings etc. constants |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
44 */ |
16 | 45 #define SET_DEF_WIDTH 1280 |
46 #define SET_DEF_HEIGHT 960 | |
47 #define SET_FRAMES (180) | |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
48 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
49 |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
50 /* Structures |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
51 */ |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
52 struct Mesh |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
53 { |
5 | 54 int nvertices, nfaces; |
16 | 55 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
56 std::vector<float> vertices; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
57 std::vector<unsigned> faces; |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
58 |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
59 GLuint id_prog, id_fs, id_vs; |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
60 }; |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
61 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
62 |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
63 /* Options |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
64 */ |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
65 bool optUseShaders = false; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
66 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
|
67 optHeight = SET_DEF_HEIGHT, |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
68 optVSyncMode = 1; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
69 |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
70 std::string optModelPrefix = "dragon"; |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
71 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
72 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
73 /* Globals |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
74 */ |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
75 SDL_Window *dmWindow = NULL; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
76 SDL_GLContext dmGLContext = NULL; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
77 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
78 |
5 | 79 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
|
80 { |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
81 int ret; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
82 std::string msg; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
83 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
84 // Set GL attributes |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 |
2 | 89 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); |
90 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
91 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
92 // 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
|
93 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
|
94 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
95 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
|
96 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
97 return false; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
98 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
99 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
100 // Attempt to create a window |
8 | 101 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
|
102 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
|
103 width, height, |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
104 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE)) == NULL) |
2 | 105 { |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
106 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
|
107 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
108 return false; |
2 | 109 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
110 |
8 | 111 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
|
112 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
113 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
|
114 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
115 return false; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
116 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
117 |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
118 // 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
|
119 switch (optVSyncMode) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
120 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
121 case 3: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
122 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
|
123 msg = "adaptive vsync"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
124 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
125 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
126 case 2: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
127 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
|
128 msg = "synchronized (vsync)"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
129 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
130 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
131 case 1: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
132 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
|
133 msg = "immediate (no vsync)"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
134 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
135 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
136 default: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
137 ret = -1; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
138 msg = "INVALID VSYNC MODE"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
139 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
140 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
141 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
142 if (ret != 0) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
143 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
144 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
|
145 msg.c_str()); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
146 return false; |
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 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
149 // 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
|
150 printf( |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
151 "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
|
152 "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
|
153 "GL_VERSION : %s\n" |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
154 "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
|
155 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
|
156 glGetString(GL_RENDERER), |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
157 glGetString(GL_VERSION), |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
158 msg.c_str()); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
159 |
2 | 160 // 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
|
161 glViewport(0, 0, width, height); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
162 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
163 glMatrixMode(GL_PROJECTION); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
164 glLoadIdentity(); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
165 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
166 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
|
167 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
168 glMatrixMode(GL_MODELVIEW); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
169 glLoadIdentity(); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
170 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
171 |
2 | 172 // Enable back face culling |
173 glEnable(GL_CULL_FACE); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
174 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
175 // Enable smooth shading |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
176 glShadeModel(GL_SMOOTH); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
177 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
178 // Enable the depth buffer |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
179 glEnable(GL_DEPTH_TEST); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
180 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
181 // Setup depth buffer |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
182 glClearDepth(1.0f); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
183 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
184 // Set the depth buffer function |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
185 glDepthFunc(GL_LEQUAL); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
186 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
187 // Enable vertex and and normal arrays |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
188 glEnableClientState(GL_VERTEX_ARRAY); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
189 glEnableClientState(GL_NORMAL_ARRAY); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
190 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
191 // Set correct perspective correction |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
192 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
|
193 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
194 return true; |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
195 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
196 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
197 |
5 | 198 void dmDrawModelVA(const Mesh &mesh) |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
199 { |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
200 int maxIndices; |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
201 |
17 | 202 if (optUseShaders) |
203 { | |
204 // Enable shader program | |
205 glUseProgram(mesh.id_prog); | |
206 } | |
207 else | |
208 { | |
209 // Set the color of the model | |
210 glEnable(GL_LIGHTING); | |
211 glColor3ub(0x90, 0x80, 0x90); | |
212 } | |
213 | |
214 // Render the model | |
2 | 215 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
216 |
16 | 217 glVertexPointer(3, GL_FLOAT, 3 * 4 * 2, &mesh.vertices[0]); |
218 glNormalPointer( GL_FLOAT, 3 * 4 * 2, &mesh.vertices[3]); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
219 |
5 | 220 for (int n = 0; n < mesh.nfaces; n += maxIndices) |
2 | 221 { |
16 | 222 const int count = std::min(maxIndices, mesh.nfaces - n); |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
223 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &mesh.faces[n * 3]); |
2 | 224 } |
17 | 225 |
226 // Restore | |
227 if (optUseShaders) | |
228 { | |
229 glUseProgram(0); | |
230 } | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
231 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
232 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
233 |
17 | 234 void dmDrawBackground() |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
235 { |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
236 glClear(GL_DEPTH_BUFFER_BIT); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
237 |
2 | 238 glMatrixMode(GL_PROJECTION); |
239 glPushMatrix(); | |
240 glLoadIdentity(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
241 |
2 | 242 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
|
243 |
2 | 244 glMatrixMode(GL_MODELVIEW); |
245 glPushMatrix(); | |
246 glLoadIdentity(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
247 |
17 | 248 // Draw the background gradient |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
249 glDisable(GL_DEPTH_TEST); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
250 glDisable(GL_LIGHTING); |
2 | 251 glBegin(GL_QUADS); |
252 { | |
253 glColor3ub(0x3B, 0x3B, 0x75); | |
254 glVertex2f(0.0f, 0.0f); | |
255 glVertex2f(1.0f, 0.0f); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
256 |
2 | 257 glColor3ub(0x00, 0x00, 0x00); |
258 glVertex2f(1.0f, 1.0f); | |
259 glVertex2f(0.0f, 1.0f); | |
260 } | |
261 glEnd(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
262 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
263 |
2 | 264 // Restore the 3D projection |
265 glMatrixMode(GL_PROJECTION); | |
266 glPopMatrix(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
267 |
2 | 268 glMatrixMode(GL_MODELVIEW); |
269 glPopMatrix(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
270 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
271 glEnable(GL_DEPTH_TEST); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
272 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
273 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
274 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
275 bool dmReadText(const std::string &filename, std::string &tstr) |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
276 { |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
277 std::ifstream in(filename.c_str()); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
278 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
279 if (!in.is_open()) |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
280 { |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
281 printf("ERROR: Unable to open file '%s'.\n", |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
282 filename.c_str()); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
283 return false; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
284 } |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
285 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
286 in.seekg(0, std::ios::end); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
287 tstr.reserve(in.tellg()); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
288 in.seekg(0, std::ios::beg); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
289 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
290 tstr.assign((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
291 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
292 return true; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
293 } |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
294 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
295 |
14
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
296 bool dmLoadMesh(const std::string &filename, const std::string &infofilename, Mesh &mesh) |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
297 { |
14
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
298 std::ifstream info(infofilename.c_str()); |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
299 int required = 0, nline = 0; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
300 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
301 printf("INFO: Trying to read mesh meta from '%s'.\n", |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
302 infofilename.c_str()); |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
303 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
304 if (!info.is_open()) |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
305 { |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
306 printf("ERROR: Unable to open file '%s'.\n", |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
307 infofilename.c_str()); |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
308 return false; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
309 } |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
310 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
311 while (required != 0x03) |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
312 { |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
313 std::string tmp; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
314 int value; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
315 char key; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
316 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
317 // Read one line |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
318 if (!std::getline(info, tmp)) |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
319 { |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
320 printf("ERROR: Could not read from file '%s'.\n", |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
321 infofilename.c_str()); |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
322 return false; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
323 } |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
324 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
325 nline++; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
326 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
327 // Skip empty lines and comments |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
328 if (tmp.empty() || tmp.substr(0, 1) == "#") |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
329 continue; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
330 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
331 if (sscanf(tmp.c_str(), "%c:%d", &key, &value) != 2) |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
332 { |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
333 printf("ERROR: Syntax error in '%s' line #%d\n'%s'\n", |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
334 infofilename.c_str(), |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
335 nline, |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
336 tmp.c_str()); |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
337 return false; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
338 } |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
339 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
340 switch (key) |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
341 { |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
342 case 'v': |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
343 mesh.nvertices = value; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
344 required |= 0x01; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
345 break; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
346 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
347 case 'f': |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
348 mesh.nfaces = value; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
349 required |= 0x02; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
350 break; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
351 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
352 default: |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
353 printf("ERROR: Syntax error in '%s' line #%d\nUnknown key value '%c'.\n", |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
354 infofilename.c_str(), |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
355 nline, |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
356 key); |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
357 break; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
358 } |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
359 } |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
360 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
361 if (mesh.nvertices < 3 || mesh.nfaces < 1) |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
362 { |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
363 printf("ERROR: Invalid nvertices (%d) and/or nfaces (%d) in '%s'.\n", |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
364 mesh.nvertices, mesh.nfaces, |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
365 infofilename.c_str()); |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
366 return false; |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
367 } |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
368 |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
369 printf("INFO: %d vertices, %d faces\n", mesh.nvertices, mesh.nfaces); |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
370 printf("INFO: Trying to read mesh data from '%s'.\n", |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
371 filename.c_str()); |
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
372 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
373 std::ifstream in(filename.c_str(), std::ios::binary); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
374 |
2 | 375 if (!in.is_open()) |
376 { | |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
377 printf("ERROR: Unable to open file '%s'.\n", |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
378 filename.c_str()); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
379 return false; |
2 | 380 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
381 |
5 | 382 mesh.vertices.resize(mesh.nvertices * 6); |
383 in.read(reinterpret_cast<char*>(&mesh.vertices[0]), mesh.nvertices * 6 * 4); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
384 |
5 | 385 mesh.faces.resize(mesh.nfaces * 3); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
386 |
8 | 387 for (int i = 0; i < mesh.nfaces; i++) |
2 | 388 { |
389 in.seekg(1, std::ios::cur); | |
390 in.read(reinterpret_cast<char*>(&mesh.faces[i * 3]), 3 * 4); | |
391 } | |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
392 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
393 return true; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
394 } |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
395 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
396 |
8 | 397 GLuint dmCompileShader(const GLenum stype, const std::string &src) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
398 { |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
399 GLuint shader = glCreateShader(stype); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
400 const char *tmp = src.c_str(); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
401 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
402 glShaderSource(shader, 1, &tmp, 0); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
403 glCompileShader(shader); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
404 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
405 return shader; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
406 } |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
407 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
408 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
409 void dmLinkMeshShaders(Mesh &mesh) |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
410 { |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
411 mesh.id_prog = glCreateProgram(); |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
412 glAttachShader(mesh.id_prog, mesh.id_fs); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
413 glAttachShader(mesh.id_prog, mesh.id_vs); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
414 glLinkProgram(mesh.id_prog); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
415 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
416 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
417 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
418 int main(int argc, char *argv[]) |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
419 { |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
420 std::string modelVertStr, modelFragStr; |
17 | 421 Mesh modelMesh; |
8 | 422 bool exitFlag = false, optShowHelp = false; |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
423 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
|
424 double totalTime; |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
425 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
426 // Check commandline argument for enabling shaders |
7 | 427 for (int narg = 1; narg < argc; narg++) |
428 { | |
429 char *arg = argv[narg]; | |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
430 if (arg[0] == '-') |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
431 { |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
432 char *opt = arg + 1; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
433 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
434 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
|
435 opt[0] == '?' || (opt[0] == '-' && opt[1] == '?')) |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
436 { |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
437 optShowHelp = true; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
438 break; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
439 } |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
440 else |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
441 if (opt[0] == '-') |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
442 opt++; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
443 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
444 if (opt[0] == 'g') |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
445 optUseShaders = true; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
446 else |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
447 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
|
448 { |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
449 case 'w': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
450 case 'h': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
451 case 'm': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
452 case 'v': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
453 if (opt[1] == 0) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
454 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
455 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
|
456 goto exit; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
457 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
458 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
459 switch (opt[0]) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
460 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
461 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
|
462 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
|
463 case 'm': optModelPrefix = std::string(opt + 1); break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
464 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
|
465 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
466 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
467 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
468 default: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
469 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
|
470 goto exit; |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
471 } |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
472 } |
7 | 473 } |
474 | |
8 | 475 if (optShowHelp) |
7 | 476 { |
477 printf( | |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
478 "Usage: %s [options]\n" |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
479 "-? 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
|
480 "-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
|
481 "-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
|
482 "-h<height> Window height (default %d)\n" |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
483 "-m<modelfile> Set model filenames prefix. Using \"-mfoo\" will\n" |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
484 " specify \"foo.mesh\", \"foo.frag\", \"foo.vert\".\n" |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
485 "-v<1-3> Set vsync mode: 1 = no vsync, 2 = vsync, 3 = adaptive\n" |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
486 " Default is no vsync.\n" |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
487 "\n", |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
488 argv[0], |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
489 SET_DEF_WIDTH, SET_DEF_HEIGHT |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
490 ); |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
491 |
7 | 492 goto exit; |
493 } | |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
494 |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
495 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
|
496 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
497 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
|
498 optWidth, optHeight); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
499 goto exit; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
500 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
501 |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
502 if (optModelPrefix.empty()) |
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 printf("Model file prefix empty.\n"); |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
505 goto exit; |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
506 } |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
507 |
14
62be2036f604
Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
508 if (!dmLoadMesh(optModelPrefix + ".mesh", optModelPrefix + ".info", modelMesh)) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
509 goto exit; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
510 |
8 | 511 if (optUseShaders) |
2 | 512 { |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
513 // Read shader files |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
514 if (!dmReadText(optModelPrefix + ".frag", modelFragStr) || |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
515 !dmReadText(optModelPrefix + ".vert", modelVertStr)) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
516 goto exit; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
517 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
518 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
519 // Initialize SDL + OpenGL |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
520 if (!dmInitSDLGL(optWidth, optHeight, "GLXDragon2")) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
521 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
|
522 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
523 // According to our mode .. |
8 | 524 if (optUseShaders) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
525 { |
12
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
526 modelMesh.id_fs = dmCompileShader(GL_FRAGMENT_SHADER, modelFragStr); |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
527 modelMesh.id_vs = dmCompileShader(GL_VERTEX_SHADER, modelVertStr); |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
528 dmLinkMeshShaders(modelMesh); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
529 } |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
530 else |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
531 { |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
532 float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f }; |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
533 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
534 glEnable(GL_COLOR_MATERIAL); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
535 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
536 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
537 glMateriali(GL_FRONT, GL_SHININESS, 96); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
538 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
539 glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
540 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
541 glEnable(GL_LIGHT0); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
542 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
543 // Define the light components and position |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
544 GLfloat ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
545 GLfloat diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f }; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
546 GLfloat specular[] = { 0.5f, 0.5f, 0.5f, 1.0f }; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
547 GLfloat position[] = { 10.0f, 10.0f, 0.0f, 0.0f }; |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
548 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
549 // Define the light components and position |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
550 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
551 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
552 glLightfv(GL_LIGHT0, GL_SPECULAR, specular); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
553 glLightfv(GL_LIGHT0, GL_POSITION, position); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
554 } |
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 |
17 | 586 dmDrawBackground(); |
587 dmDrawModelVA(modelMesh); | |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
588 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
589 // Draw the current frame |
8 | 590 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
|
591 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
592 // Rotate for 2 degrees |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
593 glRotatef(2.0f, 0, 1, 0); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
594 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
595 // Return true if a full rotation was done |
15
2d2aadfa3df3
Fix calculation of totalFrames.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
596 totalFrames++; |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
597 if (cycleFrames++ == SET_FRAMES) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
598 { |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
599 // Reset cycleFrames |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
600 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
|
601 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
602 // 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
|
603 int cycleEnd = SDL_GetTicks(); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
604 double cycleTime = cycleEnd - cycleStart; |
10
c6c419861101
Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
605 |
c6c419861101
Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
606 // Restart the timer |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
607 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
|
608 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
609 // Print the current frames per second |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
610 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
|
611 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
|
612 } |
2 | 613 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
614 |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
615 // Show totals |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
616 totalTime = SDL_GetTicks() - startTime; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
617 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
|
618 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
|
619 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
620 exit: |
8 | 621 if (dmGLContext != NULL) |
622 SDL_GL_DeleteContext(dmGLContext); | |
5 | 623 |
8 | 624 if (dmWindow != NULL) |
625 SDL_DestroyWindow(dmWindow); | |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
626 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
627 SDL_Quit(); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
628 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
629 return 0; |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
630 } |