annotate glxdragon.cpp @ 16:c134a186912f

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