comparison src/solar-system.cpp @ 3:4f8b47ac2715

Aborted test of loading an MD2 model. The GLTools TriangleBatch class doesn't play nicely with MD2, as it erroneously marks vertices as duplicates of each other.
author Eris Caffee <discordia@eldalin.com>
date Sun, 28 Apr 2013 18:04:27 -0500
parents c24af3462002
children
comparison
equal deleted inserted replaced
0:d13c292333fc 1:4eb8a0c5a060
12 #include <stdlib.h> 12 #include <stdlib.h>
13 #include <string.h> 13 #include <string.h>
14 14
15 #include <dirent.h> 15 #include <dirent.h>
16 16
17 #include <iostream>
18
17 #include <GL/glut.h> 19 #include <GL/glut.h>
18 #include <GL/glx.h> 20 #include <GL/glx.h>
21
22 #include "MD2.h"
23
24 ////////////////////////////////////////////////////////////////////////////////
19 25
20 GLShaderManager shaderManager; 26 GLShaderManager shaderManager;
21 GLMatrixStack modelViewMatrix; 27 GLMatrixStack modelViewMatrix;
22 GLMatrixStack projectionMatrix; 28 GLMatrixStack projectionMatrix;
23 GLFrustum viewFrustum; 29 GLFrustum viewFrustum;
29 35
30 int width = 800; 36 int width = 800;
31 int height = 600; 37 int height = 600;
32 int fullscreen = 0; 38 int fullscreen = 0;
33 39
34 #define NUM_SPHERES 50 40 ////////////////////////////////////////////////////////////////////////////////
35 GLFrame spheres[NUM_SPHERES];
36 41
37 GLTriangleBatch jupiterBatch; 42 GLTriangleBatch jupiterBatch;
38 GLTriangleBatch sphereBatch;
39 GLTriangleBatch earthBatch; 43 GLTriangleBatch earthBatch;
40 GLTriangleBatch moonBatch; 44 GLTriangleBatch moonBatch;
41 GLTriangleBatch sunBatch; 45 GLTriangleBatch sunBatch;
42 GLBatch floorBatch; 46
47 ////////////////////////////////////////////////////////////////////////////////
43 48
44 #define TEX_EARTH 1 49 #define TEX_EARTH 1
45 #define TEX_MOON 2 50 #define TEX_MOON 2
46 #define TEX_JUPITER 3 51 #define TEX_JUPITER 3
47 #define TEX_SUN 4 52 #define TEX_SUN 4
48 #define NUM_TEXTURES 5 53 #define NUM_TEXTURES 5
49 GLuint uiTextures[NUM_TEXTURES]; 54 GLuint uiTextures[NUM_TEXTURES];
50 55
51 //////////////////////////////////////////////////////////////////////////////// 56 ////////////////////////////////////////////////////////////////////////////////
57
58 const std::string Data_Dir("../textures/");
59
60 ////////////////////////////////////////////////////////////////////////////////
52 #define SCREENSHOT_FILENAME_BASE "screenshot-" 61 #define SCREENSHOT_FILENAME_BASE "screenshot-"
53 #define SCREENSHOT_FILENAME_BASELEN 11 62 #define SCREENSHOT_FILENAME_BASELEN 11
54 #define SCREENSHOT_FILENAME_EXT ".tga" 63 #define SCREENSHOT_FILENAME_EXT ".tga"
55 #define SCREENSHOT_FILENAME_EXTLEN 4 64 #define SCREENSHOT_FILENAME_EXTLEN 4
56 65
57 int scandir_filter(const struct dirent * d) 66 int scandir_filter(const struct dirent * d)
58 { 67 {
59 if (memcmp(d->d_name, SCREENSHOT_FILENAME_BASE, 68 if (memcmp(d->d_name, SCREENSHOT_FILENAME_BASE,
60 SCREENSHOT_FILENAME_BASELEN) != 0) return 0; 69 SCREENSHOT_FILENAME_BASELEN) != 0) return 0;
61 if (memcmp(d->d_name+SCREENSHOT_FILENAME_BASELEN+3, 70 if (memcmp(d->d_name+SCREENSHOT_FILENAME_BASELEN+3,
62 SCREENSHOT_FILENAME_EXT, SCREENSHOT_FILENAME_EXTLEN) != 0) 71 SCREENSHOT_FILENAME_EXT, SCREENSHOT_FILENAME_EXTLEN) != 0)
63 return 0; 72 return 0;
64 if (isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN]) 73 if (isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN])
65 && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+1]) 74 && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+1])
66 && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+2])) 75 && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+2]))
67 return 1; 76 return 1;
68 return 0; 77 return 0;
69 } 78 }
70 79
71 80
72 int get_next_file_name(char * filename) 81 int get_next_file_name(char * filename)
73 { 82 {
74 static int i = 0; 83 static int i = 0;
75 84
76 if (i == 0) 85 if (i == 0)
77 { 86 {
78 char pattern[SCREENSHOT_FILENAME_BASELEN+3+SCREENSHOT_FILENAME_EXTLEN]; 87 char pattern[SCREENSHOT_FILENAME_BASELEN+3+SCREENSHOT_FILENAME_EXTLEN];
79 struct dirent ** file_list; 88 struct dirent ** file_list;
80 int num_files = scandir(".", &file_list, scandir_filter, alphasort); 89 int num_files = scandir(".", &file_list, scandir_filter, alphasort);
81 if (num_files != 0) 90 if (num_files != 0)
82 sprintf(pattern, "%s%%03d%s", SCREENSHOT_FILENAME_BASE, 91 sprintf(pattern, "%s%%03d%s", SCREENSHOT_FILENAME_BASE,
83 SCREENSHOT_FILENAME_EXT); 92 SCREENSHOT_FILENAME_EXT);
84 sscanf(file_list[num_files-1]->d_name, pattern, &i); 93 sscanf(file_list[num_files-1]->d_name, pattern, &i);
85 } 94 }
86 i++; 95 i++;
87 96
88 sprintf(filename, "%s%03d%s", SCREENSHOT_FILENAME_BASE, i, 97 sprintf(filename, "%s%03d%s", SCREENSHOT_FILENAME_BASE, i,
89 SCREENSHOT_FILENAME_EXT); 98 SCREENSHOT_FILENAME_EXT);
90 return i; 99 return i;
91 } 100 }
92 101
93 //////////////////////////////////////////////////////////////////////////////// 102 ////////////////////////////////////////////////////////////////////////////////
94 bool LoadTGATexture(const char * szFileName, GLenum minFilter, 103 bool LoadTGATexture(const char * szFileName, GLenum minFilter,
95 GLenum magFilter, GLenum wrapMode) 104 GLenum magFilter, GLenum wrapMode)
96 { 105 {
97 GLbyte * pBits; 106 GLbyte * pBits;
98 int nWidth, nHeight, nComponents; 107 int nWidth, nHeight, nComponents;
99 GLenum eFormat; 108 GLenum eFormat;
100 109
101 pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat); 110 pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat);
102 if (pBits == NULL) 111 if (pBits == NULL)
103 { 112 {
104 fprintf(stderr, "Failed to load %s\n", szFileName); 113 fprintf(stderr, "Failed to load %s\n", szFileName);
105 exit(EXIT_FAILURE); 114 exit(EXIT_FAILURE);
106 } 115 }
107 116
108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode); 117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode); 118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);
110 119
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); 120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); 121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
113 122
114 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 123 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
115 glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, 124 glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0,
116 eFormat, GL_UNSIGNED_BYTE, pBits); 125 eFormat, GL_UNSIGNED_BYTE, pBits);
117 126
118 free(pBits); 127 free(pBits);
119 128
120 if (minFilter == GL_LINEAR_MIPMAP_LINEAR || 129 if (minFilter == GL_LINEAR_MIPMAP_LINEAR ||
121 minFilter == GL_LINEAR_MIPMAP_NEAREST || 130 minFilter == GL_LINEAR_MIPMAP_NEAREST ||
122 minFilter == GL_NEAREST_MIPMAP_LINEAR || 131 minFilter == GL_NEAREST_MIPMAP_LINEAR ||
123 minFilter == GL_NEAREST_MIPMAP_NEAREST) 132 minFilter == GL_NEAREST_MIPMAP_NEAREST)
124 { 133 {
125 glGenerateMipmap(GL_TEXTURE_2D); 134 glGenerateMipmap(GL_TEXTURE_2D);
126 } 135 }
127 136
128 return true; 137 return true;
129 } 138 }
130 139
131 //////////////////////////////////////////////////////////////////////////////// 140 ////////////////////////////////////////////////////////////////////////////////
132 void DrawSolarSystem(GLfloat yRot) 141 void DrawSolarSystem(GLfloat yRot)
133 { 142 {
134 static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f }; 143 static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f };
135 static GLfloat vLightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f }; 144 static GLfloat vLightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
136 static M3DVector3f vSunPos = { 0.0f, 0.0f, 0.0f }; 145 static M3DVector3f vSunPos = { 0.0f, 0.0f, 0.0f };
137 static M3DVector3f vEarthPos = { 5.0f, 0.0f, 0.0f }; 146 static M3DVector3f vEarthPos = { 5.0f, 0.0f, 0.0f };
138 static M3DVector3f vJupiterPos = { 10.0f, 0.0f, 0.0f }; 147 static M3DVector3f vJupiterPos = { 10.0f, 0.0f, 0.0f };
139 148
140 float RotScale = 100.0; 149 float RotScale = 100.0;
141 float SunRotSpeed = 1.0/(25*24) * RotScale; 150 float SunRotSpeed = 1.0/(25*24) * RotScale;
142 151
143 float JupiterRotSpeed = 1.0/9 * RotScale; 152 float JupiterRotSpeed = 1.0/9 * RotScale;
144 float JupiterAxialTilt = 3.13; 153 float JupiterAxialTilt = 3.13;
145 154
146 float EarthRotSpeed = 1.0/24 * RotScale; 155 float EarthRotSpeed = 1.0/24 * RotScale;
147 float EarthAxialTilt = 23.5; 156 float EarthAxialTilt = 23.5;
148 157
149 float MoonRotSpeed = 1.0/29.5 * RotScale; 158 // float MoonRotSpeed = 1.0/29.5 * RotScale;
150 float MoonAxialTilt = 6.7; 159 float MoonAxialTilt = 6.7;
151 float MoonOrbitSpeed = 1.0/29.5 * RotScale; 160 float MoonOrbitSpeed = 1.0/(24*29.5) * RotScale;
152 float MoonOrbitTilt = 5.145; 161 float MoonOrbitTilt = 5.145;
153 162
154 static CStopWatch rotTimer; 163 static CStopWatch rotTimer;
155 164
156 // Get the light position in eye space 165 // Get the light position in eye space
157 M3DVector4f vLightTransformed; 166 M3DVector4f vLightTransformed;
158 M3DMatrix44f mCamera; 167 M3DMatrix44f mCamera;
159 modelViewMatrix.GetMatrix(mCamera); 168 modelViewMatrix.GetMatrix(mCamera);
160 m3dTransformVector4(vLightTransformed, vLightPos, mCamera); 169 m3dTransformVector4(vLightTransformed, vLightPos, mCamera);
161 170
162 171
163 //////////////////////////////// 172 ////////////////////////////////
164 // Begin Sun 173 // Begin Sun
165 174
166 float SunRot = rotTimer.GetElapsedSeconds() * SunRotSpeed; 175 float SunRot = rotTimer.GetElapsedSeconds() * SunRotSpeed;
167 176
168 modelViewMatrix.PushMatrix(); 177 modelViewMatrix.PushMatrix();
169 178
170 modelViewMatrix.Translatev(vSunPos); 179 modelViewMatrix.Translatev(vSunPos);
171 // North is up! 180 // North is up!
172 modelViewMatrix.Rotate(-90.0f, 1.0f, 0.0f, 0.0f); 181 modelViewMatrix.Rotate(-90.0f, 1.0f, 0.0f, 0.0f);
173 // Rotate on axis 182 // Rotate on axis
174 modelViewMatrix.Rotate(SunRot, 0.0f, 0.0f, 1.0f); 183 modelViewMatrix.Rotate(SunRot, 0.0f, 0.0f, 1.0f);
175 184
176 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]); 185 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]);
177 if (polymode == GL_FILL) 186 if (polymode == GL_FILL)
178 { 187 {
179 shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, 188 shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE,
180 transformPipeline.GetModelViewProjectionMatrix(), 189 transformPipeline.GetModelViewProjectionMatrix(),
181 0); 190 0);
182 } 191 }
183 else 192 else
184 { 193 {
185 shaderManager.UseStockShader(GLT_SHADER_FLAT, 194 shaderManager.UseStockShader(GLT_SHADER_FLAT,
186 transformPipeline.GetModelViewProjectionMatrix(), 195 transformPipeline.GetModelViewProjectionMatrix(),
187 vWhite); 196 vWhite);
188 } 197 }
189 sunBatch.Draw(); 198 sunBatch.Draw();
190 modelViewMatrix.PopMatrix(); 199 modelViewMatrix.PopMatrix();
191 // End Sun 200 // End Sun
192 ///////////////////////////////// 201 /////////////////////////////////
193 202
194 203
195 //////////////////////////////// 204 ////////////////////////////////
196 // Jupiter 205 // Jupiter
197 float JupiterRot = rotTimer.GetElapsedSeconds() * JupiterRotSpeed; 206 float JupiterRot = rotTimer.GetElapsedSeconds() * JupiterRotSpeed;
198 207
199 modelViewMatrix.PushMatrix(); 208 modelViewMatrix.PushMatrix();
200 modelViewMatrix.Translatev(vJupiterPos); 209 modelViewMatrix.Scale(1.0/1000, 1.0/1000, 1.0/1000);
201 // North is up! 210 modelViewMatrix.Translatev(vJupiterPos);
202 modelViewMatrix.Rotate(-90.0f - JupiterAxialTilt, 1.0f, 0.0f, 0.0f); 211 // North is up!
203 // Rotate on axis 212 modelViewMatrix.Rotate(-90.0f - JupiterAxialTilt, 1.0f, 0.0f, 0.0f);
204 modelViewMatrix.Rotate(JupiterRot, 0.0f, 0.0f, 1.0f); 213 // Rotate on axis
205 214 modelViewMatrix.Rotate(JupiterRot, 0.0f, 0.0f, 1.0f);
206 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]); 215
207 if (polymode == GL_FILL) 216 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]);
208 { 217 if (polymode == GL_FILL)
209 // shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, 218 {
210 // transformPipeline.GetModelViewProjectionMatrix(), 219 shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF,
211 // 0); 220 modelViewMatrix.GetMatrix(),
212 shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, 221 transformPipeline.GetProjectionMatrix(),
213 modelViewMatrix.GetMatrix(), 222 vLightTransformed,
214 transformPipeline.GetProjectionMatrix(), 223 vWhite,
215 vLightTransformed, 224 0);
216 vWhite, 225 }
217 0); 226 else
218 } 227 {
219 else 228 shaderManager.UseStockShader(GLT_SHADER_FLAT,
220 { 229 transformPipeline.GetModelViewProjectionMatrix(),
221 shaderManager.UseStockShader(GLT_SHADER_FLAT, 230 vWhite);
222 transformPipeline.GetModelViewProjectionMatrix(), 231
223 vWhite); 232 }
224 233 jupiterBatch.Draw();
225 } 234 modelViewMatrix.PopMatrix();
226 jupiterBatch.Draw(); 235 // End Jupiter
227 modelViewMatrix.PopMatrix(); 236 ////////////////////////////////
228 // End Jupiter 237
229 //////////////////////////////// 238
230 239
231 240
232 241 /////////////////////////////////
233 242 // Begin Earth/Moon
234 ///////////////////////////////// 243
235 // Begin Earth/Moon 244 // Begin Earth
236 245 float EarthRot = rotTimer.GetElapsedSeconds() * EarthRotSpeed;
237 // Begin Earth 246
238 float EarthRot = rotTimer.GetElapsedSeconds() * EarthRotSpeed; 247 modelViewMatrix.PushMatrix();
239 248 modelViewMatrix.Translatev(vEarthPos);
240 modelViewMatrix.PushMatrix(); 249 modelViewMatrix.PushMatrix(); // Save unrotated matrix for when we do the Moon
241 modelViewMatrix.Translatev(vEarthPos); 250
242 modelViewMatrix.PushMatrix(); // Save unrotated matrix for when we do the Moon 251 // NOrth is up!
243 252 modelViewMatrix.Rotate(-90.0f - EarthAxialTilt, 1.0f, 0.0f, 0.0f);
244 // NOrth is up! 253 // Rotate on the axis
245 modelViewMatrix.Rotate(-90.0f - EarthAxialTilt, 1.0f, 0.0f, 0.0f); 254 modelViewMatrix.Rotate(EarthRot, 0.0f, 0.0f, 1.0f);
246 // Rotate on the axis 255
247 modelViewMatrix.Rotate(EarthRot, 0.0f, 0.0f, 1.0f); 256 if (polymode == GL_FILL)
248 257 {
249 if (polymode == GL_FILL) 258 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]);
250 { 259 shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF,
251 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]); 260 modelViewMatrix.GetMatrix(),
252 shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, 261 transformPipeline.GetProjectionMatrix(),
253 modelViewMatrix.GetMatrix(), 262 vLightTransformed,
254 transformPipeline.GetProjectionMatrix(), 263 vWhite,
255 vLightTransformed, 264 0);
256 vWhite, 265 }
257 0); 266 else
258 } 267 {
259 else 268 shaderManager.UseStockShader(GLT_SHADER_FLAT,
260 { 269 transformPipeline.GetModelViewProjectionMatrix(),
261 shaderManager.UseStockShader(GLT_SHADER_FLAT, 270 vWhite);
262 transformPipeline.GetModelViewProjectionMatrix(), 271
263 vWhite); 272 }
264 273 earthBatch.Draw();
265 } 274 modelViewMatrix.PopMatrix();
266 earthBatch.Draw(); 275
267 modelViewMatrix.PopMatrix(); 276 // Begin Moon
268
269 // Begin Moon
270 277
271 278
272 // orbit the Earth 279 // orbit the Earth
273 modelViewMatrix.Rotate(MoonOrbitTilt, 0.0f, 0.0f, 1.0f); 280 modelViewMatrix.Rotate(MoonOrbitTilt, 0.0f, 0.0f, 1.0f);
274 // NOrth is up! 281
275 modelViewMatrix.Rotate(-90.0f - MoonAxialTilt, 0.0f, 1.0f, 0.0f); 282 // NOrth is up!
276 283 modelViewMatrix.Rotate(90.0f - MoonAxialTilt, 1.0f, 0.0f, 0.0f);
277 float MoonRot = rotTimer.GetElapsedSeconds() * MoonOrbitSpeed * 10; 284 modelViewMatrix.Rotate(90.0f, 0.0f, 0.0f, 1.0f);
278 modelViewMatrix.Rotate(MoonRot, 0.0f, 1.0f, 0.0f); 285
279 286 float MoonRot = rotTimer.GetElapsedSeconds() * MoonOrbitSpeed ;
280 modelViewMatrix.Translate(0.5f, 0.0f, 0.0f); 287 modelViewMatrix.Rotate(MoonRot, 0.0f, 0.0f, -1.0f);
281 288
282 289 modelViewMatrix.Translate(0.0f, 0.5f, 0.0f);
283 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]); 290
284 if (polymode == GL_FILL) 291
285 { 292 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]);
286 shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, 293 if (polymode == GL_FILL)
287 modelViewMatrix.GetMatrix(), 294 {
288 transformPipeline.GetProjectionMatrix(), 295 shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF,
289 vLightTransformed, 296 modelViewMatrix.GetMatrix(),
290 vWhite, 297 transformPipeline.GetProjectionMatrix(),
291 0); 298 vLightTransformed,
292 } 299 vWhite,
293 else 300 0);
294 { 301 }
295 shaderManager.UseStockShader(GLT_SHADER_FLAT, 302 else
296 transformPipeline.GetModelViewProjectionMatrix(), 303 {
297 vWhite); 304 shaderManager.UseStockShader(GLT_SHADER_FLAT,
298 } 305 transformPipeline.GetModelViewProjectionMatrix(),
299 moonBatch.Draw(); 306 vWhite);
300 modelViewMatrix.PopMatrix(); 307 }
301 308 moonBatch.Draw();
302 309 modelViewMatrix.PopMatrix();
303 modelViewMatrix.PopMatrix(); 310
304 // End Earth/Moon 311
305 //////////////////////////////// 312 modelViewMatrix.PopMatrix();
313 // End Earth/Moon
314 ////////////////////////////////
306 315
307 } 316 }
317
318 ////////////////////////////////////////////////////////////////////////////////
319 void load_md2(const std::string & file, GLTriangleBatch & batch)
320 {
321 std::string fpath(Data_Dir);
322 fpath.append(file);
323
324 MD2 model(fpath);
325 if (! model.ok)
326 {
327 std::cerr << "Unable to load model " << fpath << std::endl;
328 exit(EXIT_FAILURE);
329 }
330
331 batch.BeginMesh(model.header.num_verts);
332 M3DVector3f verts[3];
333 M3DVector3f norms[3];
334 M3DVector2f tex_coords[3];
335 int vi = 0;
336 for (std::vector<GL_Triangle>::iterator it = model.triangles.begin() ; it != model.triangles.end(); ++it)
337 {
338
339 for (int v=0; v<3; v++)
340 for (int i=0; i<3; i++)
341 verts[v][i] = model.frames[0].verts[ (*it).vert[v] ].coord[i];
342
343 for (int t=0; t<3; t++)
344 for (int i=0; i<2; i++)
345 tex_coords[t][i] = model.frames[0].verts[ (*it).tex[t] ].coord[i];
346
347 for (int v=0; v<3; v++)
348 for (int i=0; i<3; i++)
349 norms[v][i] = MD2::light_normals[ model.frames[0].verts[ (*it).vert[v] ].n ].coord[i];
350
351 // std::cout << "verts " ;
352 // for (int v=0; v<3; v++)
353 // {
354 // for (int i=0; i<3; i++)
355 // std::cout << verts[v][i] << " ";
356 // std::cout << " ";
357 // }
358 // std::cout << std::endl;
359 // std::cout << "norms " ;
360 // for (int v=0; v<3; v++)
361 // {
362 // for (int i=0; i<3; i++)
363 // std::cout << norms[v][i] << " ";
364 // std::cout << " ";
365 // }
366 // std::cout << std::endl;
367
368 batch.AddTriangle(verts, norms, tex_coords);
369 }
370 batch.End();
371 }
308 372
309 //////////////////////////////////////////////////////////////////////////////// 373 ////////////////////////////////////////////////////////////////////////////////
310 void RenderScene(void) 374 void RenderScene(void)
311 { 375 {
312 static CStopWatch rotTimer; 376 static CStopWatch rotTimer;
313 float yRot = - rotTimer.GetElapsedSeconds() * 60.0f; 377 float yRot = - rotTimer.GetElapsedSeconds() * 60.0f;
314 378
315 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 379 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
316 380
317 // Begin Render 381 // Begin Render
318 modelViewMatrix.PushMatrix(); 382 modelViewMatrix.PushMatrix();
319 383
320 // Begin Camera position 384 // Begin Camera position
321 static M3DMatrix44f mCamera; 385 static M3DMatrix44f mCamera;
322 cameraFrame.GetCameraMatrix(mCamera); 386 cameraFrame.GetCameraMatrix(mCamera);
323 modelViewMatrix.MultMatrix(mCamera); 387 modelViewMatrix.MultMatrix(mCamera);
324 // End Camera position 388 // End Camera position
325 389
326 DrawSolarSystem(yRot); 390 DrawSolarSystem(yRot);
327 391
328 // End Render 392 // End Render
329 modelViewMatrix.PopMatrix(); 393 modelViewMatrix.PopMatrix();
330 394
331 glutSwapBuffers(); 395 glutSwapBuffers();
332 glutPostRedisplay(); 396 glutPostRedisplay();
333 } 397 }
334 398
335 //////////////////////////////////////////////////////////////////////////////// 399 ////////////////////////////////////////////////////////////////////////////////
336 void SetupRC() 400 void SetupRC()
337 { 401 {
338 shaderManager.InitializeStockShaders(); 402 shaderManager.InitializeStockShaders();
339 glEnable(GL_DEPTH_TEST); 403 glEnable(GL_DEPTH_TEST);
340 glEnable(GL_CULL_FACE); 404 glEnable(GL_CULL_FACE);
341 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 405 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
342 406
343 gltMakeSphere(jupiterBatch, 0.5f, 128, 128 ); 407
344 gltMakeSphere(earthBatch, 0.2f, 26, 26); 408
345 gltMakeSphere(moonBatch, 0.1f, 26, 26); 409 glGenTextures(NUM_TEXTURES, uiTextures);
346 gltMakeSphere(sunBatch, 1.0f, 26, 26); 410
347 411 ////////////////////////////////////////
348 glGenTextures(NUM_TEXTURES, uiTextures); 412 // Earth
349 413 gltMakeSphere(earthBatch, 0.2f, 26, 26);
350 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]); 414 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]);
351 LoadTGATexture("../textures/earth.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 415 LoadTGATexture("../textures/earth.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
352 416
353 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]); 417 ////////////////////////////////////////
354 LoadTGATexture("../textures/moon.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 418 // Moon
355 419 gltMakeSphere(moonBatch, 0.1f, 26, 26);
356 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]); 420 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]);
357 LoadTGATexture("../textures/jupiter.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 421 LoadTGATexture("../textures/moon.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
358 422
359 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]); 423 ////////////////////////////////////////
360 LoadTGATexture("../textures/sun.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 424 // Jupiter
361 425 // gltMakeSphere(jupiterBatch, 0.5f, 128, 128 );
362 static M3DMatrix44f mCamera; 426 load_md2("jupiter.md2", jupiterBatch);
363 cameraFrame.GetCameraMatrix(mCamera); 427 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]);
364 cameraFrame.MoveForward(-10.0); 428 std::string f(Data_Dir);
365 // cameraFrame.RotateWorld(15.0, 0.0f, 1.0f, 0.0f); 429 f.append("jupiter.tga");
366 } 430 LoadTGATexture(f.c_str(), GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
431
432 ////////////////////////////////////////
433 // Sun
434 gltMakeSphere(sunBatch, 1.0f, 26, 26);
435 glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]);
436 LoadTGATexture("../textures/sun.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
437
438 ////////////////////////////////////////
439 // Camera
440 static M3DMatrix44f mCamera;
441 cameraFrame.GetCameraMatrix(mCamera);
442 cameraFrame.MoveForward(-10.0);
443 //cameraFrame.RotateWorld(15.0, 0.0f, 1.0f, 0.0f);
444 }
367 445
368 //////////////////////////////////////////////////////////////////////////////// 446 ////////////////////////////////////////////////////////////////////////////////
369 void ShutDownRC(void) 447 void ShutDownRC(void)
370 { 448 {
371 glDeleteTextures(NUM_TEXTURES, uiTextures); 449 glDeleteTextures(NUM_TEXTURES, uiTextures);
372 } 450 }
373 451
374 //////////////////////////////////////////////////////////////////////////////// 452 ////////////////////////////////////////////////////////////////////////////////
375 void ChangeSize(int nWidth, int nHeight) 453 void ChangeSize(int nWidth, int nHeight)
376 { 454 {
377 if (nHeight == 0) 455 if (nHeight == 0)
378 { 456 {
379 nHeight = 1; 457 nHeight = 1;
380 } 458 }
381 glViewport(0,0,nWidth, nHeight); 459 glViewport(0,0,nWidth, nHeight);
382 viewFrustum.SetPerspective(35.0f, float(nWidth)/float(nHeight), 1.0f, 100.0f); 460 viewFrustum.SetPerspective(35.0f, float(nWidth)/float(nHeight), 1.0f, 100.0f);
383 projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()); 461 projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
384 transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix); 462 transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
385 } 463 }
386 464
387 //////////////////////////////////////////////////////////////////////////////// 465 ////////////////////////////////////////////////////////////////////////////////
388 void SpecialKeys(int key, int x, int y) 466 void SpecialKeys(int key, int x, int y)
389 { 467 {
390 } 468 }
391 469
392 //////////////////////////////////////////////////////////////////////////////// 470 ////////////////////////////////////////////////////////////////////////////////
393 void KeyboardFunc(unsigned char key, int x, int y) 471 void KeyboardFunc(unsigned char key, int x, int y)
394 { 472 {
395 static int changed; 473 static int changed;
396 static float linear = 0.1f; 474 static float linear = 0.1f;
397 475
398 changed = 0; 476 changed = 0;
399 477
400 if ('w' == key) 478 if ('w' == key)
401 { 479 {
402 cameraFrame.MoveForward(linear); 480 cameraFrame.MoveForward(linear);
403 changed = 1; 481 changed = 1;
404 } 482 }
405 else if ('W' == key) 483 else if ('W' == key)
406 { 484 {
407 cameraFrame.MoveForward(10*linear); 485 cameraFrame.MoveForward(10*linear);
408 changed = 1; 486 changed = 1;
409 } 487 }
410 else if ('s' == key) 488 else if ('s' == key)
411 { 489 {
412 cameraFrame.MoveForward(-linear); 490 cameraFrame.MoveForward(-linear);
413 changed = 1; 491 changed = 1;
414 } 492 }
415 else if ('S' == key) 493 else if ('S' == key)
416 { 494 {
417 cameraFrame.MoveForward(-10*linear); 495 cameraFrame.MoveForward(-10*linear);
418 changed = 1; 496 changed = 1;
419 } 497 }
420 else if ('a' == key) 498 else if ('a' == key)
421 { 499 {
422 cameraFrame.MoveRight(linear); 500 cameraFrame.MoveRight(linear);
423 changed = 1; 501 changed = 1;
424 } 502 }
425 else if ('A' == key) 503 else if ('A' == key)
426 { 504 {
427 cameraFrame.MoveRight(10*linear); 505 cameraFrame.MoveRight(10*linear);
428 changed = 1; 506 changed = 1;
429 } 507 }
430 else if ('d' == key) 508 else if ('d' == key)
431 { 509 {
432 cameraFrame.MoveRight(-linear); 510 cameraFrame.MoveRight(-linear);
433 changed = 1; 511 changed = 1;
434 } 512 }
435 else if ('D' == key) 513 else if ('D' == key)
436 { 514 {
437 cameraFrame.MoveRight(-10*linear); 515 cameraFrame.MoveRight(-10*linear);
438 changed = 1; 516 changed = 1;
439 } 517 }
440 518
441 else if ('q' == key) 519 else if ('q' == key)
442 { 520 {
443 exit(0); 521 exit(0);
444 } 522 }
445 else if ('f' == key) 523 else if ('f' == key)
446 { 524 {
447 if (fullscreen) 525 if (fullscreen)
448 { 526 {
449 glutReshapeWindow(width, height); 527 glutReshapeWindow(width, height);
450 fullscreen = 0; 528 fullscreen = 0;
451 } 529 }
452 else 530 else
453 { 531 {
454 width = glutGet(GLUT_WINDOW_WIDTH); 532 width = glutGet(GLUT_WINDOW_WIDTH);
455 height = glutGet(GLUT_WINDOW_HEIGHT); 533 height = glutGet(GLUT_WINDOW_HEIGHT);
456 glutFullScreen(); 534 glutFullScreen();
457 fullscreen = 1; 535 fullscreen = 1;
458 } 536 }
459 } 537 }
460 else if ('o' == key) 538 else if ('o' == key)
461 { 539 {
462 // 'o' for 'outline' - toggle wireframe rendering 540 // 'o' for 'outline' - toggle wireframe rendering
463 polymode = (polymode == GL_FILL) ? GL_LINE : GL_FILL; 541 polymode = (polymode == GL_FILL) ? GL_LINE : GL_FILL;
464 glPolygonMode(GL_FRONT_AND_BACK, polymode); 542 glPolygonMode(GL_FRONT_AND_BACK, polymode);
465 } 543 }
466 else if ('p' == key) 544 else if ('p' == key)
467 { 545 {
468 // 'p' for 'print screen' - save a screenshot 546 // 'p' for 'print screen' - save a screenshot
469 char filename[20]; 547 char filename[20];
470 get_next_file_name(filename); 548 get_next_file_name(filename);
471 549
472 gltGrabScreenTGA(filename); 550 gltGrabScreenTGA(filename);
473 } 551 }
474 552
475 if (changed) 553 if (changed)
476 { 554 {
477 glutPostRedisplay(); 555 glutPostRedisplay();
478 } 556 }
479 } 557 }
480 558
481 //////////////////////////////////////////////////////////////////////////////// 559 ////////////////////////////////////////////////////////////////////////////////
482 void MouseMotionFunc (int x, int y) 560 void MouseMotionFunc (int x, int y)
483 { 561 {
484 static float angular = (float) m3dDegToRad(0.5f); 562 static float angular = (float) m3dDegToRad(0.5f);
485 static int xx = -1; 563 static int xx = -1;
486 static int yy = -1; 564 static int yy = -1;
487 565
488 if (-1 == xx) 566 if (-1 == xx)
489 { 567 {
490 xx = x; 568 xx = x;
491 yy = y; 569 yy = y;
492 } 570 }
493 571
494 if ((0 == x) || (x < xx)) 572 if ((0 == x) || (x < xx))
495 { 573 {
496 cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f); 574 cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f);
497 glutPostRedisplay(); 575 glutPostRedisplay();
498 } 576 }
499 else if ((x == glutGet(GLUT_WINDOW_WIDTH) -1) || (x > xx)) 577 else if ((x == glutGet(GLUT_WINDOW_WIDTH) -1) || (x > xx))
500 { 578 {
501 cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f); 579 cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f);
502 glutPostRedisplay(); 580 glutPostRedisplay();
503 } 581 }
504 // Hmm. Need to transform normal vector, don't I? 582 // Hmm. Need to transform normal vector, don't I?
505 // if ((0 == y) || (y < yy)) 583 // if ((0 == y) || (y < yy))
506 // { 584 // {
507 // cameraFrame.RotateWorld(angular, 1.0f, 0.0f, 0.0f); 585 // cameraFrame.RotateWorld(angular, 1.0f, 0.0f, 0.0f);
508 // } 586 // }
509 // else if ((y == glutGet(GLUT_WINDOW_HEIGHT) -1) || (y > yy)) 587 // else if ((y == glutGet(GLUT_WINDOW_HEIGHT) -1) || (y > yy))
510 // { 588 // {
511 // cameraFrame.RotateWorld(-angular, 1.0f, 0.0f, 0.0f); 589 // cameraFrame.RotateWorld(-angular, 1.0f, 0.0f, 0.0f);
512 // } 590 // }
513 591
514 xx = x; 592 xx = x;
515 yy = y; 593 yy = y;
516 } 594 }
517 595
518 //////////////////////////////////////////////////////////////////////////////// 596 ////////////////////////////////////////////////////////////////////////////////
519 int main (int argc, char * argv[]) 597 int main (int argc, char * argv[])
520 { 598 {
521 gltSetWorkingDirectory(argv[0]); 599 gltSetWorkingDirectory(argv[0]);
522 glutInit(&argc, argv); 600 glutInit(&argc, argv);
523 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); 601 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
524 glutInitWindowSize(800, 600); 602 glutInitWindowSize(800, 600);
525 glutCreateWindow("OpenGL SphereWorld"); 603 glutCreateWindow("OpenGL Solar System");
526 604
527 glutReshapeFunc(ChangeSize); 605 glutReshapeFunc(ChangeSize);
528 glutDisplayFunc(RenderScene); 606 glutDisplayFunc(RenderScene);
529 glutSpecialFunc(SpecialKeys); 607 glutSpecialFunc(SpecialKeys);
530 glutKeyboardFunc(KeyboardFunc); 608 glutKeyboardFunc(KeyboardFunc);
531 glutMotionFunc(MouseMotionFunc); 609 glutMotionFunc(MouseMotionFunc);
532 glutPassiveMotionFunc(MouseMotionFunc); 610 glutPassiveMotionFunc(MouseMotionFunc);
533 glutSetCursor(GLUT_CURSOR_NONE); 611 glutSetCursor(GLUT_CURSOR_NONE);
534 612
535 GLenum err = glewInit(); 613 GLenum err = glewInit();
536 if (GLEW_OK != err) 614 if (GLEW_OK != err)
537 { 615 {
538 fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); 616 fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
539 return 1; 617 return 1;
540 } 618 }
541 619
542 // This enabled vertical sync on Linux 620 // This enabled vertical sync on Linux
543 // For full generality I should use Glew and check what OS I'm on. 621 // For full generality I should use Glew and check what OS I'm on.
544 // Note that the ATI Catalyst driver exports the WGL_EXT_swap_control 622 // Note that the ATI Catalyst driver exports the WGL_EXT_swap_control
545 // extension name instead of SGI_swap_control as it should. 623 // extension name instead of SGI_swap_control as it should.
546 // but nonetheless the actual function provided is glXSwapIntervalSGI 624 // but nonetheless the actual function provided is glXSwapIntervalSGI
547 625
548 PFNGLXSWAPINTERVALSGIPROC SwapInterval; 626 PFNGLXSWAPINTERVALSGIPROC SwapInterval;
549 SwapInterval = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalSGI"); 627 SwapInterval = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalSGI");
550 628
551 if (SwapInterval) 629 if (SwapInterval)
552 SwapInterval(1); 630 SwapInterval(1);
553 631
554 SetupRC(); 632 SetupRC();
555 glutMainLoop(); 633 glutMainLoop();
556 ShutDownRC(); 634 ShutDownRC();
557 635
558 return 0; 636 return 0;
559 } 637 }
560 638
561 639
562 640
563 641
564 642