Mercurial > hg > forks > gldragon
comparison glxdragon.cpp @ 2:b45d8958e5a6
Cosmetic indentation.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 27 Oct 2019 17:27:55 +0200 |
parents | 3d74a9dd96e4 |
children | be31ff9e5f58 |
comparison
equal
deleted
inserted
replaced
1:4727156927ea | 2:b45d8958e5a6 |
---|---|
39 #include <ctime> | 39 #include <ctime> |
40 | 40 |
41 | 41 |
42 struct Mesh | 42 struct Mesh |
43 { | 43 { |
44 std::vector<float> vertices; | 44 std::vector <float> vertices; |
45 std::vector<unsigned> faces; | 45 std::vector <unsigned> faces; |
46 } | 46 } |
47 dragonMesh; | 47 dragonMesh; |
48 | 48 |
49 | 49 |
50 void init(const int windowWidth, const int windowHeight, const std::string& windowTitle) | 50 void init(const int windowWidth, const int windowHeight, const std::string& windowTitle) |
51 { | 51 { |
52 std::stringstream ss; | 52 std::stringstream ss; |
53 | 53 |
54 | 54 |
55 if (SDL_Init(SDL_INIT_VIDEO) != 0) | 55 if (SDL_Init(SDL_INIT_VIDEO) != 0) |
56 { | 56 { |
57 ss << "Unable to initialize SDL: " << SDL_GetError() << '\n'; | 57 ss << "Unable to initialize SDL: " << SDL_GetError() << '\n'; |
58 | 58 |
59 throw std::runtime_error(ss.str()); | 59 throw std::runtime_error(ss.str()); |
60 } | 60 } |
61 | 61 |
62 | 62 |
63 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); | 63 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); |
64 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); | 64 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); |
65 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); | 65 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); |
66 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); | 66 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); |
67 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | 67 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
68 | 68 |
69 | 69 |
70 SDL_WM_SetCaption(windowTitle.c_str(), 0); | 70 SDL_WM_SetCaption(windowTitle.c_str(), 0); |
71 | 71 |
72 | 72 |
73 if ((SDL_SetVideoMode(windowWidth, windowHeight, 0, SDL_OPENGL)) == NULL) | 73 if ((SDL_SetVideoMode(windowWidth, windowHeight, 0, SDL_OPENGL)) == NULL) |
74 { | 74 { |
75 ss << "Couldn't set GL mode: " << SDL_GetError() << '\n'; | 75 ss << "Couldn't set GL mode: " << SDL_GetError() << '\n'; |
76 | 76 |
77 throw std::runtime_error(ss.str()); | 77 throw std::runtime_error(ss.str()); |
78 } | 78 } |
79 | 79 |
80 | 80 |
81 std::cout << "GL_VENDOR : " << glGetString(GL_VENDOR) << std::endl; | 81 std::cout << "GL_VENDOR : " << glGetString(GL_VENDOR) << std::endl; |
82 std::cout << "GL_RENDERER : " << glGetString(GL_RENDERER) << std::endl; | 82 std::cout << "GL_RENDERER : " << glGetString(GL_RENDERER) << std::endl; |
83 std::cout << "GL_VERSION : " << glGetString(GL_VERSION) << std::endl; | 83 std::cout << "GL_VERSION : " << glGetString(GL_VERSION) << std::endl; |
84 | 84 |
85 std::cout << std::endl; | 85 std::cout << std::endl; |
86 | 86 |
87 | 87 |
88 // Setup the window and view port | 88 // Setup the window and view port |
89 glViewport(0, 0, windowWidth, windowHeight); | 89 glViewport(0, 0, windowWidth, windowHeight); |
90 | 90 |
91 glMatrixMode(GL_PROJECTION); | 91 glMatrixMode(GL_PROJECTION); |
92 glLoadIdentity(); | 92 glLoadIdentity(); |
93 | 93 |
94 gluPerspective(45.0f, GLfloat(windowWidth) / GLfloat(windowHeight), 0.1f, 1000.0f); | 94 gluPerspective(45.0f, GLfloat(windowWidth) / GLfloat(windowHeight), 0.1f, 1000.0f); |
95 | 95 |
96 glMatrixMode(GL_MODELVIEW); | 96 glMatrixMode(GL_MODELVIEW); |
97 glLoadIdentity(); | 97 glLoadIdentity(); |
98 | 98 |
99 | 99 |
100 // Enable back face culling | 100 // Enable back face culling |
101 glEnable(GL_CULL_FACE); | 101 glEnable(GL_CULL_FACE); |
102 | 102 |
103 // Enable smooth shading | 103 // Enable smooth shading |
104 glShadeModel(GL_SMOOTH); | 104 glShadeModel(GL_SMOOTH); |
105 | 105 |
106 // Enable the depth buffer | 106 // Enable the depth buffer |
133 | 133 |
134 | 134 |
135 glEnable(GL_LIGHT0); | 135 glEnable(GL_LIGHT0); |
136 | 136 |
137 // Define the light components and position | 137 // Define the light components and position |
138 GLfloat ambient [] = { 0.2f, 0.2f, 0.2f, 1.0f }; | 138 GLfloat ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; |
139 GLfloat diffuse [] = { 0.8f, 0.8f, 0.8f, 1.0f }; | 139 GLfloat diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f }; |
140 GLfloat specular[] = { 0.5f, 0.5f, 0.5f, 1.0f }; | 140 GLfloat specular[] = { 0.5f, 0.5f, 0.5f, 1.0f }; |
141 GLfloat position[] = { 10.0f, 10.0f, 0.0f, 0.0f }; | 141 GLfloat position[] = { 10.0f, 10.0f, 0.0f, 0.0f }; |
142 | 142 |
143 // Define the light components and position | 143 // Define the light components and position |
144 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); | 144 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); |
145 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); | 145 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); |
146 glLightfv(GL_LIGHT0, GL_SPECULAR, specular); | 146 glLightfv(GL_LIGHT0, GL_SPECULAR, specular); |
147 glLightfv(GL_LIGHT0, GL_POSITION, position); | 147 glLightfv(GL_LIGHT0, GL_POSITION, position); |
148 | 148 |
149 // Define the camera | 149 // Define the camera |
150 gluLookAt(0, 0.12, 0.24, 0, 0.12, 0, 0, 1, 0); | 150 gluLookAt(0, 0.12, 0.24, 0, 0.12, 0, 0, 1, 0); |
151 } | 151 } |
152 | 152 |
153 | 153 |
154 void done() | 154 void done() |
155 { | 155 { |
156 SDL_Quit(); | 156 SDL_Quit(); |
157 } | 157 } |
158 | 158 |
159 | 159 |
160 void drawModelVA(const Mesh& mesh) | 160 void drawModelVA(const Mesh& mesh) |
161 { | 161 { |
162 int maxIndices; | 162 int maxIndices; |
163 | 163 |
164 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); | 164 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); |
165 | 165 |
166 | 166 |
167 glVertexPointer(3, GL_FLOAT, 24, &mesh.vertices[0]); | 167 glVertexPointer(3, GL_FLOAT, 24, &mesh.vertices[0]); |
168 glNormalPointer( GL_FLOAT, 24, &mesh.vertices[3]); | 168 glNormalPointer(GL_FLOAT, 24, &mesh.vertices[3]); |
169 | 169 |
170 | 170 |
171 for(size_t n = 0; n < mesh.faces.size() / 3; n += maxIndices) | 171 for (size_t n = 0; n < mesh.faces.size() / 3; n += maxIndices) |
172 { | 172 { |
173 const int count = std::min(maxIndices, int(mesh.faces.size() / 3 - n)); | 173 const int count = std::min(maxIndices, int(mesh.faces.size() / 3 - n)); |
174 | 174 |
175 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &dragonMesh.faces[n * 3]); | 175 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &dragonMesh.faces[n * 3]); |
176 } | 176 } |
177 } | 177 } |
178 | 178 |
179 | 179 |
180 bool paintGL() | 180 bool paintGL() |
181 { | 181 { |
182 glClear(GL_DEPTH_BUFFER_BIT); | 182 glClear(GL_DEPTH_BUFFER_BIT); |
183 | 183 |
184 | 184 |
185 glMatrixMode(GL_PROJECTION); | 185 glMatrixMode(GL_PROJECTION); |
186 glPushMatrix(); | 186 glPushMatrix(); |
187 glLoadIdentity(); | 187 glLoadIdentity(); |
188 | 188 |
189 glOrtho(0.0, 1.0, 0.0, 1.0, -1, 1); | 189 glOrtho(0.0, 1.0, 0.0, 1.0, -1, 1); |
190 | 190 |
191 glMatrixMode(GL_MODELVIEW); | 191 glMatrixMode(GL_MODELVIEW); |
192 glPushMatrix(); | 192 glPushMatrix(); |
193 glLoadIdentity(); | 193 glLoadIdentity(); |
194 | 194 |
195 | 195 |
196 glDisable(GL_DEPTH_TEST); | 196 glDisable(GL_DEPTH_TEST); |
197 glDisable(GL_LIGHTING); | 197 glDisable(GL_LIGHTING); |
198 | 198 |
199 // Draw the background gradient | 199 // Draw the background gradient |
200 glBegin(GL_QUADS); | 200 glBegin(GL_QUADS); |
201 { | 201 { |
202 glColor3ub(0x3B, 0x3B, 0x75); | 202 glColor3ub(0x3B, 0x3B, 0x75); |
203 glVertex2f(0.0f, 0.0f); | 203 glVertex2f(0.0f, 0.0f); |
204 glVertex2f(1.0f, 0.0f); | 204 glVertex2f(1.0f, 0.0f); |
205 | 205 |
206 glColor3ub(0x00, 0x00, 0x00); | 206 glColor3ub(0x00, 0x00, 0x00); |
207 glVertex2f(1.0f, 1.0f); | 207 glVertex2f(1.0f, 1.0f); |
208 glVertex2f(0.0f, 1.0f); | 208 glVertex2f(0.0f, 1.0f); |
209 } | 209 } |
210 glEnd(); | 210 glEnd(); |
211 | 211 |
212 | 212 |
213 // Restore the 3D projection | 213 // Restore the 3D projection |
214 glMatrixMode(GL_PROJECTION); | 214 glMatrixMode(GL_PROJECTION); |
215 glPopMatrix(); | 215 glPopMatrix(); |
216 | 216 |
217 glMatrixMode(GL_MODELVIEW); | 217 glMatrixMode(GL_MODELVIEW); |
218 glPopMatrix(); | 218 glPopMatrix(); |
219 | 219 |
220 | 220 |
221 glEnable(GL_DEPTH_TEST); | 221 glEnable(GL_DEPTH_TEST); |
222 glEnable(GL_LIGHTING); | 222 glEnable(GL_LIGHTING); |
223 | 223 |
243 } | 243 } |
244 | 244 |
245 | 245 |
246 void runEventLoop() | 246 void runEventLoop() |
247 { | 247 { |
248 static std::clock_t startTime = std::clock(); | 248 static std::clock_t startTime = std::clock(); |
249 | 249 |
250 while (true) | 250 while (true) |
251 { | 251 { |
252 SDL_Event event; | 252 SDL_Event event; |
253 | 253 |
254 if (SDL_PollEvent(&event)) | 254 if (SDL_PollEvent(&event)) |
255 { | 255 { |
256 if (event.type == SDL_QUIT) | 256 if (event.type == SDL_QUIT) |
257 break; | 257 break; |
258 } | 258 } |
259 else | 259 else |
260 { | 260 { |
261 // Render the next frame and test if a full turn was completed | 261 // Render the next frame and test if a full turn was completed |
262 if (paintGL()) | 262 if (paintGL()) |
263 { | 263 { |
264 // Get the time it took to render a full turn | 264 // Get the time it took to render a full turn |
265 double time = double(std::clock() - startTime) / CLOCKS_PER_SEC; | 265 double time = double(std::clock() - startTime) / CLOCKS_PER_SEC; |
266 | 266 |
267 // Print the current frames per second | 267 // Print the current frames per second |
268 std::printf("%.1lf seconds for 180 frames = %.1lf FPS\n", time, 180 / time); | 268 std::printf("%.1lf seconds for 180 frames = %.1lf FPS\n", time, 180 / time); |
269 | 269 |
270 // Restart the timer | 270 // Restart the timer |
271 startTime = std::clock(); | 271 startTime = std::clock(); |
272 } | 272 } |
273 } | 273 } |
274 } | 274 } |
275 } | 275 } |
276 | 276 |
277 | 277 |
278 void loadMesh(const std::string& fileName, Mesh& mesh) | 278 void loadMesh(const std::string& fileName, Mesh& mesh) |
279 { | 279 { |
280 std::ifstream in(fileName.c_str(), std::ios::binary); | 280 std::ifstream in(fileName.c_str(), std::ios::binary); |
281 | 281 |
282 | 282 |
283 if (!in.is_open()) | 283 if (!in.is_open()) |
284 { | 284 { |
285 std::stringstream ss; | 285 std::stringstream ss; |
286 | 286 |
287 ss << "Unable to open file: " << fileName << '\n'; | 287 ss << "Unable to open file: " << fileName << '\n'; |
288 | 288 |
289 throw std::runtime_error(ss.str()); | 289 throw std::runtime_error(ss.str()); |
290 | 290 |
291 } | 291 } |
292 | 292 |
293 | 293 |
294 mesh.vertices.resize(100139 * 6); | 294 mesh.vertices.resize(100139 * 6); |
295 | 295 in.read(reinterpret_cast<char*>(&mesh.vertices[0]), 100139 * 6 * 4); |
296 in.read(reinterpret_cast<char*>(&mesh.vertices[0]), 100139 * 6 * 4); | 296 |
297 | 297 |
298 | 298 mesh.faces.resize(200198 * 3); |
299 mesh.faces.resize(200198 * 3); | 299 for (unsigned i = 0; i < 200198; i++) |
300 | 300 { |
301 | 301 in.seekg(1, std::ios::cur); |
302 for(unsigned i = 0; i < 200198; i++) | 302 |
303 { | 303 in.read(reinterpret_cast<char*>(&mesh.faces[i * 3]), 3 * 4); |
304 in.seekg(1, std::ios::cur); | 304 } |
305 | |
306 in.read(reinterpret_cast<char*>(&mesh.faces[i*3]), 3 * 4); | |
307 } | |
308 } | 305 } |
309 | 306 |
310 | 307 |
311 int main() | 308 int main() |
312 { | 309 { |
313 try | 310 try |
314 { | 311 { |
315 loadMesh("dragon.mesh", dragonMesh); | 312 loadMesh("dragon.mesh", dragonMesh); |
316 | 313 |
317 init(640, 480, "glxdragon"); | 314 init(640, 480, "glxdragon"); |
318 | 315 |
319 initScene(); | 316 initScene(); |
320 | 317 |
321 runEventLoop(); | 318 runEventLoop(); |
322 } | 319 } |
323 catch(std::runtime_error& e) | 320 catch(std::runtime_error & e) |
324 { | 321 { |
325 std::cerr << e.what(); | 322 std::cerr << e.what(); |
326 } | 323 } |
327 | 324 |
328 done(); | 325 done(); |
329 } | 326 } |