annotate glxdragon.cpp @ 12:52a586c344f4

Add option -m<filename prefix> to specify mesh/shader filename prefix.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 28 Oct 2019 15:03:52 +0200
parents 89dc8caeff41
children c1e8057cc4d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
1 //
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
2 // Copyright (c) 2009, Thomas Trummer
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
3 // All rights reserved.
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
4 //
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
5 // Port to libSDL2 and cleanups by Matti Hämäläinen <ccr@tnsp.org>
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
6 // (C) Copyright 2019 Tecnic Software productions (TNSP)
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
7 //
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
8 // Redistribution and use in source and binary forms, with or without
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
9 // modification, are permitted provided that the following conditions are met:
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
10 // * Redistributions of source code must retain the above copyright
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
11 // notice, this list of conditions and the following disclaimer.
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
12 // * Redistributions in binary form must reproduce the above copyright
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
13 // notice, this list of conditions and the following disclaimer in the
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
14 // documentation and/or other materials provided with the distribution.
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
15 // * Neither the name of the <organization> nor the
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
16 // names of its contributors may be used to endorse or promote products
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
17 // derived from this software without specific prior written permission.
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
18 //
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
19 // THIS SOFTWARE IS PROVIDED BY Thomas Trummer ''AS IS'' AND ANY
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
21 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
22 // DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
23 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
24 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
25 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
26 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
28 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
29 //
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
30
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
31 #include <SDL.h>
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
32 #include <SDL_opengl.h>
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
33 #include <GL/glu.h>
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
34 #include <GL/glext.h>
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
35
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
36 #include <iostream>
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
37 #include <fstream>
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
38 #include <string>
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
39 #include <vector>
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
40 #include <cstdio>
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
41
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
42 /* Default settings etc. constants
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
43 */
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
44 #define SET_DEF_WIDTH 1280
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
45 #define SET_DEF_HEIGHT 960
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
46 #define SET_FRAMES (180 * 2)
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
47
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
48
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
49 /* Structures
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
50 */
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
51 struct Mesh
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
52 {
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
53 int nvertices, nfaces;
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
54 std::vector<float> vertices;
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
55 std::vector<unsigned> faces;
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
56
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
57 GLuint id_prog, id_fs, id_vs;
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
58 };
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
59
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
60
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
61 /* Options
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
62 */
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
63 bool optUseShaders = false;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
64 int optWidth = SET_DEF_WIDTH,
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
65 optHeight = SET_DEF_HEIGHT;
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
66 std::string optModelPrefix = "dragon";
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
67
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
68
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
69 /* Globals
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
70 */
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
71 SDL_Window *dmWindow = NULL;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
72 SDL_GLContext dmGLContext = NULL;
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
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
75 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
76 {
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
77 // Set GL attributes
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
78 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
79 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
80 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
81
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
82 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
83 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
84
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
85 // 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
86 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
87 {
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
88 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
89 SDL_GetError());
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
90 return false;
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
91 }
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 create a window
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
94 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
95 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
96 width, height,
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
97 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE)) == NULL)
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
98 {
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
99 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
100 SDL_GetError());
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
101 return false;
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
102 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
103
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
104 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
105 {
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
106 printf("ERROR: 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
107 SDL_GetError());
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
108 return false;
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
109 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
110
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
111 // 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
112 printf(
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
113 "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
114 "GL_RENDERER : %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 "GL_VERSION : %s\n",
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
116 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
117 glGetString(GL_RENDERER),
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
118 glGetString(GL_VERSION));
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
119
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
120 // 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
121 glViewport(0, 0, width, height);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
122
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
123 glMatrixMode(GL_PROJECTION);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
124 glLoadIdentity();
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
125
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
126 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
127
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
128 glMatrixMode(GL_MODELVIEW);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
129 glLoadIdentity();
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
130
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
131
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
132 // Enable back face culling
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
133 glEnable(GL_CULL_FACE);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
134
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
135 // Enable smooth shading
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
136 glShadeModel(GL_SMOOTH);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
137
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
138 // Enable the depth buffer
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
139 glEnable(GL_DEPTH_TEST);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
140
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
141 // Setup depth buffer
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
142 glClearDepth(1.0f);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
143
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
144 // Set the depth buffer function
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
145 glDepthFunc(GL_LEQUAL);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
146
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
147 // Enable vertex and and normal arrays
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
148 glEnableClientState(GL_VERTEX_ARRAY);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
149 glEnableClientState(GL_NORMAL_ARRAY);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
150
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
151 // Set correct perspective correction
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
152 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
153
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
154 return true;
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
155 }
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
156
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
157
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
158 void dmDrawModelVA(const Mesh &mesh)
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
159 {
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
160 int maxIndices;
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
161
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
162 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
163
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
164 glVertexPointer(3, GL_FLOAT, 24, &mesh.vertices[0]);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
165 glNormalPointer(GL_FLOAT, 24, &mesh.vertices[3]);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
166
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
167 for (int n = 0; n < mesh.nfaces; n += maxIndices)
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
168 {
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
169 const int count = std::min(maxIndices, int(mesh.nfaces - n));
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
170 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
171 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
172 }
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
173
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
174
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
175 void dmPaintGL(Mesh &mesh)
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
176 {
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
177 glClear(GL_DEPTH_BUFFER_BIT);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
178
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
179 glMatrixMode(GL_PROJECTION);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
180 glPushMatrix();
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
181 glLoadIdentity();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
182
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
183 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
184
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
185 glMatrixMode(GL_MODELVIEW);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
186 glPushMatrix();
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
187 glLoadIdentity();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
188
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
189
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
190 glDisable(GL_DEPTH_TEST);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
191 glDisable(GL_LIGHTING);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
192
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
193 // Draw the background gradient
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
194 glBegin(GL_QUADS);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
195 {
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
196 glColor3ub(0x3B, 0x3B, 0x75);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
197 glVertex2f(0.0f, 0.0f);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
198 glVertex2f(1.0f, 0.0f);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
199
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
200 glColor3ub(0x00, 0x00, 0x00);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
201 glVertex2f(1.0f, 1.0f);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
202 glVertex2f(0.0f, 1.0f);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
203 }
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
204 glEnd();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
205
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
206
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
207 // Restore the 3D projection
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
208 glMatrixMode(GL_PROJECTION);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
209 glPopMatrix();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
210
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
211 glMatrixMode(GL_MODELVIEW);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
212 glPopMatrix();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
213
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
214 glEnable(GL_DEPTH_TEST);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
215
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
216 // Render the model
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
217 if (optUseShaders)
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
218 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
219 // Enable shader program
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
220 glUseProgram(mesh.id_prog);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
221 dmDrawModelVA(mesh);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
222 glUseProgram(0);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
223 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
224 else
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
225 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
226 // Set the color of the model
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
227 glEnable(GL_LIGHTING);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
228 glColor3ub(0x90, 0x80, 0x90);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
229 dmDrawModelVA(mesh);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
230 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
231 }
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
232
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
233
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
234 bool dmReadText(const std::string &filename, std::string &tstr)
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
235 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
236 std::ifstream in(filename.c_str());
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
237
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
238 if (!in.is_open())
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
239 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
240 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
241 filename.c_str());
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
242 return false;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
243 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
244
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
245 in.seekg(0, std::ios::end);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
246 tstr.reserve(in.tellg());
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
247 in.seekg(0, std::ios::beg);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
248
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
249 tstr.assign((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
250
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
251 return true;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
252 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
253
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
254
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
255 bool dmLoadMesh(const std::string &filename, Mesh &mesh, int nvertices, int nfaces)
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
256 {
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
257 std::ifstream in(filename.c_str(), std::ios::binary);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
258
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
259 if (!in.is_open())
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
260 {
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
261 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
262 filename.c_str());
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
263 return false;
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
264 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
265
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
266 mesh.nvertices = nvertices;
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
267 mesh.vertices.resize(mesh.nvertices * 6);
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
268 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
269
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
270 mesh.nfaces = nfaces;
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
271 mesh.faces.resize(mesh.nfaces * 3);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
272
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
273 for (int i = 0; i < mesh.nfaces; i++)
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
274 {
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
275 in.seekg(1, std::ios::cur);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
276 in.read(reinterpret_cast<char*>(&mesh.faces[i * 3]), 3 * 4);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
277 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
278
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
279 return true;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
280 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
281
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
282
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
283 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
284 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
285 GLuint shader = glCreateShader(stype);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
286 const char *tmp = src.c_str();
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
287
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
288 glShaderSource(shader, 1, &tmp, 0);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
289 glCompileShader(shader);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
290
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
291 return shader;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
292 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
293
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
294
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
295 void dmLinkMeshShaders(Mesh &mesh)
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
296 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
297 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
298 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
299 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
300 glLinkProgram(mesh.id_prog);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
301 }
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
302
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
303
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
304 int main(int argc, char *argv[])
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
305 {
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
306 std::string modelVertStr, modelFragStr;
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
307 struct Mesh modelMesh;
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
308 bool exitFlag = false, optShowHelp = false;
10
c6c419861101 Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
309 int startTime, nframes = 0;
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
310
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
311 // Check commandline argument for enabling shaders
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
312 for (int narg = 1; narg < argc; narg++)
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
313 {
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
314 char *arg = argv[narg];
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
315 if (arg[0] == '-')
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
316 {
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
317 char *opt = arg + 1;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
318
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
319 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
320 opt[0] == '?' || (opt[0] == '-' && opt[1] == '?'))
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
321 {
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
322 optShowHelp = true;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
323 break;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
324 }
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
325 else
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
326 if (opt[0] == '-')
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
327 opt++;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
328
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
329 if (opt[0] == 'g')
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
330 optUseShaders = true;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
331 else
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
332 if (opt[0] == 'w')
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
333 optWidth = atoi(opt + 1);
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
334 else
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
335 if (opt[0] == 'h')
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
336 optHeight = atoi(opt + 1);
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
337 else
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
338 if (opt[0] == 'm')
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
339 {
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
340 if (opt[1] == 0)
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
341 {
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
342 printf("Option -m requires an argument.\n");
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
343 goto exit;
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
344 }
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
345
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
346 optModelPrefix = std::string(opt + 1);
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
347 }
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
348 }
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
349 }
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
350
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
351 if (optShowHelp)
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
352 {
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
353 printf(
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
354 "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
355 "-? 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
356 "-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
357 "-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
358 "-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
359 "-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
360 " specify \"foo.mesh\", \"foo.frag\", \"foo.vert\".\n"
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
361 "\n",
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
362 argv[0],
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
363 SET_DEF_WIDTH, SET_DEF_HEIGHT
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
364 );
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
365
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
366 goto exit;
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
367 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
368
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
369 if (optModelPrefix.empty())
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
370 {
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
371 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
372 goto exit;
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
373 }
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
374
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
375 if (!dmLoadMesh(optModelPrefix + ".mesh", modelMesh, 100139, 200198))
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
376 goto exit;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
377
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
378 if (optUseShaders)
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
379 {
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
380 // Read shader files
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
381 if (!dmReadText(optModelPrefix + ".frag", modelFragStr) ||
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
382 !dmReadText(optModelPrefix + ".vert", modelVertStr))
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
383 goto exit;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
384 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
385
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
386 // Initialize SDL + OpenGL
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
387 if (!dmInitSDLGL(optWidth, optHeight, "GLXDragon2"))
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
388 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
389
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
390 // According to our mode ..
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
391 if (optUseShaders)
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
392 {
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
393 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
394 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
395 dmLinkMeshShaders(modelMesh);
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
396 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
397 else
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
398 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
399 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
400
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
401 glEnable(GL_COLOR_MATERIAL);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
402
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
403 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
404 glMateriali(GL_FRONT, GL_SHININESS, 96);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
405
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
406 glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection);
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 glEnable(GL_LIGHT0);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
409
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
410 // Define the light components and position
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
411 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
412 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
413 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
414 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
415
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
416 // Define the light components and position
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
417 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
418 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
419 glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
420 glLightfv(GL_LIGHT0, GL_POSITION, position);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
421 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
422
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
423 // Define the camera
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
424 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
425
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
426
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
427 // Main loop starts
10
c6c419861101 Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
428 startTime = SDL_GetTicks();
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
429
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
430 while (!exitFlag)
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
431 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
432 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
433
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
434 // Check for quit events
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
435 while (SDL_PollEvent(&event))
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
436 switch (event.type)
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
437 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
438 case SDL_QUIT:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
439 exitFlag = true;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
440 break;
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
441
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
442 case SDL_KEYDOWN:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
443 switch (event.key.keysym.sym)
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
444 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
445 case SDLK_ESCAPE:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
446 case SDLK_q:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
447 exitFlag = true;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
448 break;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
449 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
450 }
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
451
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
452 // Render the next frame
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
453 dmPaintGL(modelMesh);
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
454
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
455 // Draw the current frame
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
456 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
457
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
458 // Rotate for 2 degrees
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
459 glRotatef(2.0f, 0, 1, 0);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
460
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
461 // Return true if a full rotation was done
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
462 if (nframes++ == SET_FRAMES)
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
463 {
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
464 // Reset nframes
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
465 nframes = 0;
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
466
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
467 // Get the time it took to render a full turn
10
c6c419861101 Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
468 double time = SDL_GetTicks() - startTime;
c6c419861101 Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
469
c6c419861101 Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
470 // Restart the timer
c6c419861101 Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
471 startTime = 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
472
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
473 // Print the current frames per second
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
474 printf("%.1lf ms for %d frames = %.1lf FPS\n",
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
475 time, SET_FRAMES, (SET_FRAMES * 1000.0f) / time);
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
476 }
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
477 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
478
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
479 exit:
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
480 if (dmGLContext != NULL)
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
481 SDL_GL_DeleteContext(dmGLContext);
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
482
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
483 if (dmWindow != NULL)
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
484 SDL_DestroyWindow(dmWindow);
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
485
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
486 SDL_Quit();
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
487
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
488 return 0;
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
489 }