annotate glxdragon.cpp @ 18:b1e75c65016d

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