annotate glxdragon.cpp @ 15:2d2aadfa3df3

Fix calculation of totalFrames.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 29 Oct 2019 12:51:25 +0200
parents 62be2036f604
children c134a186912f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
42 /* Default settings etc. constants
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
43 */
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
44 #define SET_DEF_WIDTH 1280
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
45 #define SET_DEF_HEIGHT 960
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
46 #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
47
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
48
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
49 /* Structures
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
50 */
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
51 struct Mesh
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
52 {
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
53 int nvertices, nfaces;
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
54 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
55 std::vector<unsigned> faces;
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
56
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
57 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
58 };
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
59
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
60
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
61 /* Options
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
62 */
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
63 bool optUseShaders = false;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
64 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
65 optHeight = SET_DEF_HEIGHT,
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
66 optVSyncMode = 1;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
67
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
68 std::string optModelPrefix = "dragon";
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
69
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
70
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
71 /* Globals
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 SDL_Window *dmWindow = NULL;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
74 SDL_GLContext dmGLContext = NULL;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
75
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
76
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
77 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
78 {
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
79 int ret;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
80 std::string msg;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
81
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
82 // Set GL attributes
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
83 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
84 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
85 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
86
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
87 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
88 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
89
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
90 // 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
91 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
92 {
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
93 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
94 SDL_GetError());
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
95 return false;
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
96 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
97
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
98 // Attempt to create a window
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
99 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
100 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
101 width, height,
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
102 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE)) == NULL)
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
103 {
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
104 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
105 SDL_GetError());
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
106 return false;
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
107 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
108
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
109 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
110 {
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
111 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
112 SDL_GetError());
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
113 return false;
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
114 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
115
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
116 // 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
117 switch (optVSyncMode)
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
118 {
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
119 case 3:
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
120 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
121 msg = "adaptive vsync";
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
122 break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
123
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
124 case 2:
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
125 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
126 msg = "synchronized (vsync)";
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
127 break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
128
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
129 case 1:
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
130 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
131 msg = "immediate (no vsync)";
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
132 break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
133
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
134 default:
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
135 ret = -1;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
136 msg = "INVALID VSYNC MODE";
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
137 break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
138 }
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
139
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
140 if (ret != 0)
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 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
143 msg.c_str());
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
144 return false;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
145 }
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
146
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
147 // 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
148 printf(
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
149 "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
150 "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
151 "GL_VERSION : %s\n"
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
152 "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
153 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
154 glGetString(GL_RENDERER),
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
155 glGetString(GL_VERSION),
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
156 msg.c_str());
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
157
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
158 // 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
159 glViewport(0, 0, width, height);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
160
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
161 glMatrixMode(GL_PROJECTION);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
162 glLoadIdentity();
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
163
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
164 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
165
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
166 glMatrixMode(GL_MODELVIEW);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
167 glLoadIdentity();
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
168
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
169
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
170 // Enable back face culling
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
171 glEnable(GL_CULL_FACE);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
172
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
173 // Enable smooth shading
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
174 glShadeModel(GL_SMOOTH);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
175
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
176 // Enable the depth buffer
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
177 glEnable(GL_DEPTH_TEST);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
178
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
179 // Setup depth buffer
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
180 glClearDepth(1.0f);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
181
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
182 // Set the depth buffer function
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
183 glDepthFunc(GL_LEQUAL);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
184
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
185 // Enable vertex and and normal arrays
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
186 glEnableClientState(GL_VERTEX_ARRAY);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
187 glEnableClientState(GL_NORMAL_ARRAY);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
188
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
189 // Set correct perspective correction
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
190 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
191
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
192 return true;
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
193 }
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
194
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
195
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
196 void dmDrawModelVA(const Mesh &mesh)
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
197 {
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
198 int maxIndices;
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
199
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
200 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
201
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
202 glVertexPointer(3, GL_FLOAT, 24, &mesh.vertices[0]);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
203 glNormalPointer(GL_FLOAT, 24, &mesh.vertices[3]);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
204
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
205 for (int n = 0; n < mesh.nfaces; n += maxIndices)
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
206 {
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
207 const int count = std::min(maxIndices, int(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
208 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &mesh.faces[n * 3]);
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
209 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
210 }
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
211
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
212
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
213 void dmPaintGL(Mesh &mesh)
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
214 {
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
215 glClear(GL_DEPTH_BUFFER_BIT);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
216
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
217 glMatrixMode(GL_PROJECTION);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
218 glPushMatrix();
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
219 glLoadIdentity();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
220
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
221 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
222
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
223 glMatrixMode(GL_MODELVIEW);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
224 glPushMatrix();
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
225 glLoadIdentity();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
226
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
227
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
228 glDisable(GL_DEPTH_TEST);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
229 glDisable(GL_LIGHTING);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
230
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
231 // Draw the background gradient
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
232 glBegin(GL_QUADS);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
233 {
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
234 glColor3ub(0x3B, 0x3B, 0x75);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
235 glVertex2f(0.0f, 0.0f);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
236 glVertex2f(1.0f, 0.0f);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
237
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
238 glColor3ub(0x00, 0x00, 0x00);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
239 glVertex2f(1.0f, 1.0f);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
240 glVertex2f(0.0f, 1.0f);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
241 }
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
242 glEnd();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
243
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
244
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
245 // Restore the 3D projection
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
246 glMatrixMode(GL_PROJECTION);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
247 glPopMatrix();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
248
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
249 glMatrixMode(GL_MODELVIEW);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
250 glPopMatrix();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
251
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
252 glEnable(GL_DEPTH_TEST);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
253
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
254 // Render the model
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
255 if (optUseShaders)
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
256 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
257 // Enable shader program
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
258 glUseProgram(mesh.id_prog);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
259 dmDrawModelVA(mesh);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
260 glUseProgram(0);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
261 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
262 else
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
263 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
264 // Set the color of the model
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
265 glEnable(GL_LIGHTING);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
266 glColor3ub(0x90, 0x80, 0x90);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
267 dmDrawModelVA(mesh);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
268 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
269 }
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
270
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
271
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
272 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
273 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
274 std::ifstream in(filename.c_str());
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
275
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
276 if (!in.is_open())
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
277 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
278 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
279 filename.c_str());
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
280 return false;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
281 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
282
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
283 in.seekg(0, std::ios::end);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
284 tstr.reserve(in.tellg());
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
285 in.seekg(0, std::ios::beg);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
286
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
287 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
288
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
289 return true;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
290 }
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
14
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
293 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
294 {
14
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
295 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
296 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
297
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
298 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
299 infofilename.c_str());
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 if (!info.is_open())
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
302 {
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
303 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
304 infofilename.c_str());
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
305 return false;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
306 }
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
307
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
308 while (required != 0x03)
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 std::string tmp;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
311 int value;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
312 char key;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
313
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
314 // Read one line
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
315 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
316 {
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
317 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
318 infofilename.c_str());
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
319 return false;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
320 }
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
321
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
322 nline++;
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 // 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
325 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
326 continue;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
327
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
328 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
329 {
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
330 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
331 infofilename.c_str(),
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
332 nline,
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
333 tmp.c_str());
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
334 return false;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
335 }
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
336
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
337 switch (key)
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 case 'v':
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
340 mesh.nvertices = value;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
341 required |= 0x01;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
342 break;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
343
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
344 case 'f':
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
345 mesh.nfaces = value;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
346 required |= 0x02;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
347 break;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
348
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
349 default:
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
350 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
351 infofilename.c_str(),
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
352 nline,
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
353 key);
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
354 break;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
355 }
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
356 }
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
357
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
358 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
359 {
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
360 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
361 mesh.nvertices, mesh.nfaces,
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
362 infofilename.c_str());
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
363 return false;
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
364 }
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
365
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
366 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
367 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
368 filename.c_str());
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
369
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
370 std::ifstream in(filename.c_str(), std::ios::binary);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
371
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
372 if (!in.is_open())
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
373 {
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
374 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
375 filename.c_str());
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
376 return false;
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
377 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
378
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
379 mesh.vertices.resize(mesh.nvertices * 6);
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
380 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
381
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
382 mesh.faces.resize(mesh.nfaces * 3);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
383
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
384 for (int i = 0; i < mesh.nfaces; i++)
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
385 {
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
386 in.seekg(1, std::ios::cur);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
387 in.read(reinterpret_cast<char*>(&mesh.faces[i * 3]), 3 * 4);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
388 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
389
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
390 return true;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
391 }
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
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
394 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
395 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
396 GLuint shader = glCreateShader(stype);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
397 const char *tmp = src.c_str();
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 glShaderSource(shader, 1, &tmp, 0);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
400 glCompileShader(shader);
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 return shader;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
403 }
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
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
406 void dmLinkMeshShaders(Mesh &mesh)
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 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
409 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
410 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
411 glLinkProgram(mesh.id_prog);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
412 }
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
413
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
414
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
415 int main(int argc, char *argv[])
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
416 {
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
417 std::string modelVertStr, modelFragStr;
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
418 struct Mesh modelMesh;
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
419 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
420 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
421 double totalTime;
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
422
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
423 // Check commandline argument for enabling shaders
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
424 for (int narg = 1; narg < argc; narg++)
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
425 {
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
426 char *arg = argv[narg];
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
427 if (arg[0] == '-')
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
428 {
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
429 char *opt = arg + 1;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
430
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
431 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
432 opt[0] == '?' || (opt[0] == '-' && opt[1] == '?'))
11
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 optShowHelp = true;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
435 break;
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 else
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
438 if (opt[0] == '-')
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
439 opt++;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
440
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
441 if (opt[0] == 'g')
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
442 optUseShaders = true;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
443 else
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
444 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
445 {
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
446 case 'w':
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
447 case 'h':
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
448 case 'm':
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
449 case 'v':
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
450 if (opt[1] == 0)
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
451 {
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
452 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
453 goto exit;
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
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
456 switch (opt[0])
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 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
459 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
460 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
461 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
462 }
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
463 break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
464
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
465 default:
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
466 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
467 goto exit;
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
468 }
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
469 }
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
470 }
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
471
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
472 if (optShowHelp)
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
473 {
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
474 printf(
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
475 "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
476 "-? 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
477 "-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
478 "-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
479 "-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
480 "-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
481 " 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
482 "-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
483 " Default is no vsync.\n"
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
484 "\n",
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
485 argv[0],
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
486 SET_DEF_WIDTH, SET_DEF_HEIGHT
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
487 );
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
488
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
489 goto exit;
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
490 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
491
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
492 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
493 {
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
494 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
495 optWidth, optHeight);
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
496 goto exit;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
497 }
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
498
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
499 if (optModelPrefix.empty())
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
500 {
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
501 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
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
14
62be2036f604 Read mesh vertices/faces information from "<modelfilename>.info" instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
505 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
506 goto exit;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
507
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
508 if (optUseShaders)
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
509 {
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
510 // 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
511 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
512 !dmReadText(optModelPrefix + ".vert", modelVertStr))
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
513 goto exit;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
514 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
515
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
516 // Initialize SDL + OpenGL
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
517 if (!dmInitSDLGL(optWidth, optHeight, "GLXDragon2"))
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
518 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
519
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
520 // According to our mode ..
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
521 if (optUseShaders)
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
522 {
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
523 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
524 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
525 dmLinkMeshShaders(modelMesh);
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
526 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
527 else
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
528 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
529 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
530
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
531 glEnable(GL_COLOR_MATERIAL);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
532
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
533 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
534 glMateriali(GL_FRONT, GL_SHININESS, 96);
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 glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
537
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
538 glEnable(GL_LIGHT0);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
539
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
540 // Define the light components and position
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
541 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
542 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
543 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
544 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
545
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
546 // Define the light components and position
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
547 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
548 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
549 glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
550 glLightfv(GL_LIGHT0, GL_POSITION, position);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
551 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
552
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
553 // Define the camera
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
554 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
555
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
556
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
557 // Main loop starts
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
558 startTime = cycleStart = SDL_GetTicks();
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
559
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
560 while (!exitFlag)
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
561 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
562 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
563
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
564 // Check for quit events
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
565 while (SDL_PollEvent(&event))
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
566 switch (event.type)
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
567 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
568 case SDL_QUIT:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
569 exitFlag = true;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
570 break;
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
571
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
572 case SDL_KEYDOWN:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
573 switch (event.key.keysym.sym)
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
574 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
575 case SDLK_ESCAPE:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
576 case SDLK_q:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
577 exitFlag = true;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
578 break;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
579 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
580 }
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
581
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
582 // Render the next frame
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
583 dmPaintGL(modelMesh);
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
584
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
585 // Draw the current frame
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
586 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
587
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
588 // Rotate for 2 degrees
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
589 glRotatef(2.0f, 0, 1, 0);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
590
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
591 // Return true if a full rotation was done
15
2d2aadfa3df3 Fix calculation of totalFrames.
Matti Hamalainen <ccr@tnsp.org>
parents: 14
diff changeset
592 totalFrames++;
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
593 if (cycleFrames++ == SET_FRAMES)
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
594 {
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
595 // Reset cycleFrames
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
596 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
597
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
598 // 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
599 int cycleEnd = SDL_GetTicks();
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
600 double cycleTime = cycleEnd - cycleStart;
10
c6c419861101 Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
601
c6c419861101 Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
602 // Restart the timer
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
603 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
604
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
605 // Print the current frames per second
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
606 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
607 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
608 }
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
609 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
610
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
611 // Show totals
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
612 totalTime = SDL_GetTicks() - startTime;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
613 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
614 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
615
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
616 exit:
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
617 if (dmGLContext != NULL)
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
618 SDL_GL_DeleteContext(dmGLContext);
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
619
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
620 if (dmWindow != NULL)
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
621 SDL_DestroyWindow(dmWindow);
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
622
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
623 SDL_Quit();
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
624
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
625 return 0;
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
626 }