Mercurial > hg > forks > gldragon
annotate glxdragon.cpp @ 3:be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 27 Oct 2019 17:57:37 +0200 |
parents | b45d8958e5a6 |
children | 5dcae4dddcd9 |
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> |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
34 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
35 #include <iostream> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
36 #include <sstream> |
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 <stdexcept> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
39 #include <string> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
40 #include <vector> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
41 #include <algorithm> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
42 #include <cstdio> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
43 #include <ctime> |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
44 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
45 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
46 #define SET_FRAMES (180 * 2) |
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 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
49 SDL_Window *s_window = NULL; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
50 SDL_GLContext s_context; |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
51 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
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 { |
3
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<float> vertices; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
56 std::vector<unsigned> faces; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
57 }; |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
58 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
59 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
60 bool init(const int width, const int height, const char *title) |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
61 { |
2 | 62 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); |
63 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); | |
64 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); | |
65 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); | |
66 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
67 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
68 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
|
69 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
70 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
|
71 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
72 return false; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
73 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
74 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
75 if ((s_window = SDL_CreateWindow(title, |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
76 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
|
77 width, height, |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
78 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE)) == NULL) |
2 | 79 { |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
80 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
|
81 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
82 return false; |
2 | 83 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
84 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
85 if ((s_context = SDL_GL_CreateContext(s_window)) == NULL) |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
86 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
87 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
|
88 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
89 return false; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
90 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
91 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
92 printf( |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
93 "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
|
94 "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
|
95 "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
|
96 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
|
97 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
|
98 glGetString(GL_VERSION)); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
99 |
2 | 100 // 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
|
101 glViewport(0, 0, width, height); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
102 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
103 glMatrixMode(GL_PROJECTION); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
104 glLoadIdentity(); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
105 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
106 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
|
107 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
108 glMatrixMode(GL_MODELVIEW); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
109 glLoadIdentity(); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
110 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
111 |
2 | 112 // Enable back face culling |
113 glEnable(GL_CULL_FACE); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
114 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
115 // Enable smooth shading |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
116 glShadeModel(GL_SMOOTH); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
117 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
118 // Enable the depth buffer |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
119 glEnable(GL_DEPTH_TEST); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
120 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
121 // Setup depth buffer |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
122 glClearDepth(1.0f); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
123 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
124 // Set the depth buffer function |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
125 glDepthFunc(GL_LEQUAL); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
126 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
127 // Enable vertex and and normal arrays |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
128 glEnableClientState(GL_VERTEX_ARRAY); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
129 glEnableClientState(GL_NORMAL_ARRAY); |
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 // Set correct perspective correction |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
132 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
|
133 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
134 return true; |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
135 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
136 |
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 void initScene() |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
139 { |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
140 glEnable(GL_COLOR_MATERIAL); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
141 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
142 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
143 glMateriali(GL_FRONT, GL_SHININESS, 96); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
144 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
145 float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f }; |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
146 glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
147 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
148 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
149 glEnable(GL_LIGHT0); |
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 // Define the light components and position |
2 | 152 GLfloat ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; |
153 GLfloat diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f }; | |
154 GLfloat specular[] = { 0.5f, 0.5f, 0.5f, 1.0f }; | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
155 GLfloat position[] = { 10.0f, 10.0f, 0.0f, 0.0f }; |
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 // Define the light components and position |
2 | 158 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); |
159 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
160 glLightfv(GL_LIGHT0, GL_SPECULAR, specular); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
161 glLightfv(GL_LIGHT0, GL_POSITION, position); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
162 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
163 // Define the camera |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
164 gluLookAt(0, 0.12, 0.24, 0, 0.12, 0, 0, 1, 0); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
165 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
166 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
167 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
168 void done() |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
169 { |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
170 SDL_GL_DeleteContext(s_context); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
171 SDL_DestroyWindow(s_window); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
172 s_window = NULL; |
2 | 173 SDL_Quit(); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
174 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
175 |
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 void drawModelVA(const Mesh& mesh) |
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 int maxIndices; |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
180 |
2 | 181 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
182 |
2 | 183 glVertexPointer(3, GL_FLOAT, 24, &mesh.vertices[0]); |
184 glNormalPointer(GL_FLOAT, 24, &mesh.vertices[3]); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
185 |
2 | 186 for (size_t n = 0; n < mesh.faces.size() / 3; n += maxIndices) |
187 { | |
188 const int count = std::min(maxIndices, int(mesh.faces.size() / 3 - n)); | |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
189 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &mesh.faces[n * 3]); |
2 | 190 } |
0
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 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
193 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
194 void paintGL(Mesh &mesh) |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
195 { |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
196 glClear(GL_DEPTH_BUFFER_BIT); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
197 |
2 | 198 glMatrixMode(GL_PROJECTION); |
199 glPushMatrix(); | |
200 glLoadIdentity(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
201 |
2 | 202 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
|
203 |
2 | 204 glMatrixMode(GL_MODELVIEW); |
205 glPushMatrix(); | |
206 glLoadIdentity(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
207 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
208 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
209 glDisable(GL_DEPTH_TEST); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
210 glDisable(GL_LIGHTING); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
211 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
212 // Draw the background gradient |
2 | 213 glBegin(GL_QUADS); |
214 { | |
215 glColor3ub(0x3B, 0x3B, 0x75); | |
216 glVertex2f(0.0f, 0.0f); | |
217 glVertex2f(1.0f, 0.0f); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
218 |
2 | 219 glColor3ub(0x00, 0x00, 0x00); |
220 glVertex2f(1.0f, 1.0f); | |
221 glVertex2f(0.0f, 1.0f); | |
222 } | |
223 glEnd(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
224 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
225 |
2 | 226 // Restore the 3D projection |
227 glMatrixMode(GL_PROJECTION); | |
228 glPopMatrix(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
229 |
2 | 230 glMatrixMode(GL_MODELVIEW); |
231 glPopMatrix(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
232 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
233 glEnable(GL_DEPTH_TEST); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
234 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
235 glEnable(GL_LIGHTING); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
236 // Set the color of the model |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
237 glColor3ub(0x90, 0x80, 0x90); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
238 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
239 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
240 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
241 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
242 // Draw the model using vertex arrays |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
243 drawModelVA(mesh); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
244 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
245 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
246 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
247 void loadMesh(const std::string& filename, Mesh& mesh, size_t nvertices, size_t nfaces) |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
248 { |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
249 std::ifstream in(filename.c_str(), std::ios::binary); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
250 |
2 | 251 if (!in.is_open()) |
252 { | |
253 std::stringstream ss; | |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
254 ss << "Unable to open file: " << filename << '\n'; |
2 | 255 throw std::runtime_error(ss.str()); |
256 } | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
257 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
258 mesh.vertices.resize(nvertices * 6); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
259 in.read(reinterpret_cast<char*>(&mesh.vertices[0]), nvertices * 6 * 4); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
260 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
261 mesh.faces.resize(nfaces * 3); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
262 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
263 for (size_t i = 0; i < nfaces; i++) |
2 | 264 { |
265 in.seekg(1, std::ios::cur); | |
266 in.read(reinterpret_cast<char*>(&mesh.faces[i * 3]), 3 * 4); | |
267 } | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
268 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
269 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
270 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
271 int main() |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
272 { |
2 | 273 try |
274 { | |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
275 struct Mesh dragonMesh; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
276 loadMesh("dragon.mesh", dragonMesh, 100139, 200198); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
277 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
278 //if (!init(640, 480, "glxdragon")) |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
279 if (!init(1280, 960, "glxdragon")) |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
280 throw std::runtime_error("Fatal error."); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
281 |
2 | 282 initScene(); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
283 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
284 bool exitFlag = false; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
285 int steps = 0; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
286 std::clock_t startTime = std::clock(); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
287 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
288 while (!exitFlag) |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
289 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
290 SDL_Event event; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
291 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
292 // Check for quit events |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
293 while (SDL_PollEvent(&event)) |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
294 switch (event.type) |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
295 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
296 case SDL_QUIT: |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
297 exitFlag = true; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
298 break; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
299 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
300 case SDL_KEYDOWN: |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
301 switch (event.key.keysym.sym) |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
302 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
303 case SDLK_ESCAPE: |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
304 case SDLK_q: |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
305 exitFlag = true; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
306 break; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
307 } |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
308 } |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
309 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
310 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
311 // Render the next frame |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
312 paintGL(dragonMesh); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
313 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
314 // Draw the current frame |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
315 SDL_GL_SwapWindow(s_window); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
316 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
317 // Rotate for 2 degrees |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
318 glRotatef(2.0f, 0, 1, 0); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
319 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
320 // Return true if a full rotation was done |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
321 if (steps++ == SET_FRAMES) |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
322 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
323 // Reset steps |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
324 steps = 0; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
325 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
326 // Get the time it took to render a full turn |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
327 double time = (double(std::clock() - startTime) * 1000.0f) / CLOCKS_PER_SEC; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
328 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
329 // Print the current frames per second |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
330 std::printf("%.1lf ms for %d frames = %.1lf FPS\n", |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
331 time, SET_FRAMES, (SET_FRAMES * 1000.0f) / time); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
332 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
333 // Restart the timer |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
334 startTime = std::clock(); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
335 } |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
336 } |
2 | 337 } |
338 catch(std::runtime_error & e) | |
339 { | |
340 std::cerr << e.what(); | |
341 } | |
342 done(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
343 } |