Mercurial > solar_system
diff 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 |
line diff
1.1 --- a/src/solar-system.cpp Sat Apr 27 13:22:17 2013 -0500 1.2 +++ b/src/solar-system.cpp Sun Apr 28 18:04:27 2013 -0500 1.3 @@ -14,9 +14,15 @@ 1.4 1.5 #include <dirent.h> 1.6 1.7 +#include <iostream> 1.8 + 1.9 #include <GL/glut.h> 1.10 #include <GL/glx.h> 1.11 1.12 +#include "MD2.h" 1.13 + 1.14 +//////////////////////////////////////////////////////////////////////////////// 1.15 + 1.16 GLShaderManager shaderManager; 1.17 GLMatrixStack modelViewMatrix; 1.18 GLMatrixStack projectionMatrix; 1.19 @@ -31,15 +37,14 @@ 1.20 int height = 600; 1.21 int fullscreen = 0; 1.22 1.23 -#define NUM_SPHERES 50 1.24 -GLFrame spheres[NUM_SPHERES]; 1.25 +//////////////////////////////////////////////////////////////////////////////// 1.26 1.27 GLTriangleBatch jupiterBatch; 1.28 -GLTriangleBatch sphereBatch; 1.29 GLTriangleBatch earthBatch; 1.30 GLTriangleBatch moonBatch; 1.31 GLTriangleBatch sunBatch; 1.32 -GLBatch floorBatch; 1.33 + 1.34 +//////////////////////////////////////////////////////////////////////////////// 1.35 1.36 #define TEX_EARTH 1 1.37 #define TEX_MOON 2 1.38 @@ -49,514 +54,587 @@ 1.39 GLuint uiTextures[NUM_TEXTURES]; 1.40 1.41 //////////////////////////////////////////////////////////////////////////////// 1.42 + 1.43 +const std::string Data_Dir("../textures/"); 1.44 + 1.45 +//////////////////////////////////////////////////////////////////////////////// 1.46 #define SCREENSHOT_FILENAME_BASE "screenshot-" 1.47 #define SCREENSHOT_FILENAME_BASELEN 11 1.48 #define SCREENSHOT_FILENAME_EXT ".tga" 1.49 #define SCREENSHOT_FILENAME_EXTLEN 4 1.50 1.51 int scandir_filter(const struct dirent * d) 1.52 - { 1.53 - if (memcmp(d->d_name, SCREENSHOT_FILENAME_BASE, 1.54 - SCREENSHOT_FILENAME_BASELEN) != 0) return 0; 1.55 - if (memcmp(d->d_name+SCREENSHOT_FILENAME_BASELEN+3, 1.56 - SCREENSHOT_FILENAME_EXT, SCREENSHOT_FILENAME_EXTLEN) != 0) 1.57 - return 0; 1.58 - if (isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN]) 1.59 - && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+1]) 1.60 - && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+2])) 1.61 - return 1; 1.62 - return 0; 1.63 - } 1.64 + { 1.65 + if (memcmp(d->d_name, SCREENSHOT_FILENAME_BASE, 1.66 + SCREENSHOT_FILENAME_BASELEN) != 0) return 0; 1.67 + if (memcmp(d->d_name+SCREENSHOT_FILENAME_BASELEN+3, 1.68 + SCREENSHOT_FILENAME_EXT, SCREENSHOT_FILENAME_EXTLEN) != 0) 1.69 + return 0; 1.70 + if (isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN]) 1.71 + && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+1]) 1.72 + && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+2])) 1.73 + return 1; 1.74 + return 0; 1.75 + } 1.76 1.77 1.78 int get_next_file_name(char * filename) 1.79 - { 1.80 - static int i = 0; 1.81 + { 1.82 + static int i = 0; 1.83 1.84 - if (i == 0) 1.85 - { 1.86 - char pattern[SCREENSHOT_FILENAME_BASELEN+3+SCREENSHOT_FILENAME_EXTLEN]; 1.87 - struct dirent ** file_list; 1.88 - int num_files = scandir(".", &file_list, scandir_filter, alphasort); 1.89 - if (num_files != 0) 1.90 - sprintf(pattern, "%s%%03d%s", SCREENSHOT_FILENAME_BASE, 1.91 - SCREENSHOT_FILENAME_EXT); 1.92 - sscanf(file_list[num_files-1]->d_name, pattern, &i); 1.93 - } 1.94 - i++; 1.95 + if (i == 0) 1.96 + { 1.97 + char pattern[SCREENSHOT_FILENAME_BASELEN+3+SCREENSHOT_FILENAME_EXTLEN]; 1.98 + struct dirent ** file_list; 1.99 + int num_files = scandir(".", &file_list, scandir_filter, alphasort); 1.100 + if (num_files != 0) 1.101 + sprintf(pattern, "%s%%03d%s", SCREENSHOT_FILENAME_BASE, 1.102 + SCREENSHOT_FILENAME_EXT); 1.103 + sscanf(file_list[num_files-1]->d_name, pattern, &i); 1.104 + } 1.105 + i++; 1.106 1.107 - sprintf(filename, "%s%03d%s", SCREENSHOT_FILENAME_BASE, i, 1.108 - SCREENSHOT_FILENAME_EXT); 1.109 - return i; 1.110 - } 1.111 + sprintf(filename, "%s%03d%s", SCREENSHOT_FILENAME_BASE, i, 1.112 + SCREENSHOT_FILENAME_EXT); 1.113 + return i; 1.114 + } 1.115 1.116 //////////////////////////////////////////////////////////////////////////////// 1.117 bool LoadTGATexture(const char * szFileName, GLenum minFilter, 1.118 - GLenum magFilter, GLenum wrapMode) 1.119 - { 1.120 - GLbyte * pBits; 1.121 - int nWidth, nHeight, nComponents; 1.122 - GLenum eFormat; 1.123 + GLenum magFilter, GLenum wrapMode) 1.124 + { 1.125 + GLbyte * pBits; 1.126 + int nWidth, nHeight, nComponents; 1.127 + GLenum eFormat; 1.128 1.129 - pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat); 1.130 - if (pBits == NULL) 1.131 - { 1.132 - fprintf(stderr, "Failed to load %s\n", szFileName); 1.133 - exit(EXIT_FAILURE); 1.134 - } 1.135 + pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat); 1.136 + if (pBits == NULL) 1.137 + { 1.138 + fprintf(stderr, "Failed to load %s\n", szFileName); 1.139 + exit(EXIT_FAILURE); 1.140 + } 1.141 1.142 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode); 1.143 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode); 1.144 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode); 1.145 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode); 1.146 1.147 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); 1.148 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); 1.149 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); 1.150 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); 1.151 1.152 - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 1.153 - glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, 1.154 - eFormat, GL_UNSIGNED_BYTE, pBits); 1.155 + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 1.156 + glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, 1.157 + eFormat, GL_UNSIGNED_BYTE, pBits); 1.158 1.159 - free(pBits); 1.160 + free(pBits); 1.161 1.162 - if (minFilter == GL_LINEAR_MIPMAP_LINEAR || 1.163 - minFilter == GL_LINEAR_MIPMAP_NEAREST || 1.164 - minFilter == GL_NEAREST_MIPMAP_LINEAR || 1.165 - minFilter == GL_NEAREST_MIPMAP_NEAREST) 1.166 - { 1.167 - glGenerateMipmap(GL_TEXTURE_2D); 1.168 - } 1.169 + if (minFilter == GL_LINEAR_MIPMAP_LINEAR || 1.170 + minFilter == GL_LINEAR_MIPMAP_NEAREST || 1.171 + minFilter == GL_NEAREST_MIPMAP_LINEAR || 1.172 + minFilter == GL_NEAREST_MIPMAP_NEAREST) 1.173 + { 1.174 + glGenerateMipmap(GL_TEXTURE_2D); 1.175 + } 1.176 1.177 - return true; 1.178 - } 1.179 + return true; 1.180 + } 1.181 1.182 //////////////////////////////////////////////////////////////////////////////// 1.183 void DrawSolarSystem(GLfloat yRot) 1.184 - { 1.185 - static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f }; 1.186 - static GLfloat vLightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f }; 1.187 - static M3DVector3f vSunPos = { 0.0f, 0.0f, 0.0f }; 1.188 - static M3DVector3f vEarthPos = { 5.0f, 0.0f, 0.0f }; 1.189 - static M3DVector3f vJupiterPos = { 10.0f, 0.0f, 0.0f }; 1.190 + { 1.191 + static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f }; 1.192 + static GLfloat vLightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f }; 1.193 + static M3DVector3f vSunPos = { 0.0f, 0.0f, 0.0f }; 1.194 + static M3DVector3f vEarthPos = { 5.0f, 0.0f, 0.0f }; 1.195 + static M3DVector3f vJupiterPos = { 10.0f, 0.0f, 0.0f }; 1.196 1.197 - float RotScale = 100.0; 1.198 - float SunRotSpeed = 1.0/(25*24) * RotScale; 1.199 + float RotScale = 100.0; 1.200 + float SunRotSpeed = 1.0/(25*24) * RotScale; 1.201 1.202 - float JupiterRotSpeed = 1.0/9 * RotScale; 1.203 - float JupiterAxialTilt = 3.13; 1.204 + float JupiterRotSpeed = 1.0/9 * RotScale; 1.205 + float JupiterAxialTilt = 3.13; 1.206 1.207 - float EarthRotSpeed = 1.0/24 * RotScale; 1.208 - float EarthAxialTilt = 23.5; 1.209 + float EarthRotSpeed = 1.0/24 * RotScale; 1.210 + float EarthAxialTilt = 23.5; 1.211 1.212 - float MoonRotSpeed = 1.0/29.5 * RotScale; 1.213 - float MoonAxialTilt = 6.7; 1.214 - float MoonOrbitSpeed = 1.0/29.5 * RotScale; 1.215 - float MoonOrbitTilt = 5.145; 1.216 + // float MoonRotSpeed = 1.0/29.5 * RotScale; 1.217 + float MoonAxialTilt = 6.7; 1.218 + float MoonOrbitSpeed = 1.0/(24*29.5) * RotScale; 1.219 + float MoonOrbitTilt = 5.145; 1.220 1.221 - static CStopWatch rotTimer; 1.222 + static CStopWatch rotTimer; 1.223 1.224 - // Get the light position in eye space 1.225 - M3DVector4f vLightTransformed; 1.226 - M3DMatrix44f mCamera; 1.227 - modelViewMatrix.GetMatrix(mCamera); 1.228 - m3dTransformVector4(vLightTransformed, vLightPos, mCamera); 1.229 + // Get the light position in eye space 1.230 + M3DVector4f vLightTransformed; 1.231 + M3DMatrix44f mCamera; 1.232 + modelViewMatrix.GetMatrix(mCamera); 1.233 + m3dTransformVector4(vLightTransformed, vLightPos, mCamera); 1.234 1.235 1.236 - //////////////////////////////// 1.237 - // Begin Sun 1.238 + //////////////////////////////// 1.239 + // Begin Sun 1.240 1.241 - float SunRot = rotTimer.GetElapsedSeconds() * SunRotSpeed; 1.242 + float SunRot = rotTimer.GetElapsedSeconds() * SunRotSpeed; 1.243 1.244 - modelViewMatrix.PushMatrix(); 1.245 + modelViewMatrix.PushMatrix(); 1.246 1.247 - modelViewMatrix.Translatev(vSunPos); 1.248 - // North is up! 1.249 - modelViewMatrix.Rotate(-90.0f, 1.0f, 0.0f, 0.0f); 1.250 - // Rotate on axis 1.251 - modelViewMatrix.Rotate(SunRot, 0.0f, 0.0f, 1.0f); 1.252 + modelViewMatrix.Translatev(vSunPos); 1.253 + // North is up! 1.254 + modelViewMatrix.Rotate(-90.0f, 1.0f, 0.0f, 0.0f); 1.255 + // Rotate on axis 1.256 + modelViewMatrix.Rotate(SunRot, 0.0f, 0.0f, 1.0f); 1.257 1.258 - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]); 1.259 - if (polymode == GL_FILL) 1.260 - { 1.261 - shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, 1.262 - transformPipeline.GetModelViewProjectionMatrix(), 1.263 - 0); 1.264 - } 1.265 - else 1.266 - { 1.267 - shaderManager.UseStockShader(GLT_SHADER_FLAT, 1.268 - transformPipeline.GetModelViewProjectionMatrix(), 1.269 - vWhite); 1.270 - } 1.271 - sunBatch.Draw(); 1.272 - modelViewMatrix.PopMatrix(); 1.273 - // End Sun 1.274 - ///////////////////////////////// 1.275 + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]); 1.276 + if (polymode == GL_FILL) 1.277 + { 1.278 + shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, 1.279 + transformPipeline.GetModelViewProjectionMatrix(), 1.280 + 0); 1.281 + } 1.282 + else 1.283 + { 1.284 + shaderManager.UseStockShader(GLT_SHADER_FLAT, 1.285 + transformPipeline.GetModelViewProjectionMatrix(), 1.286 + vWhite); 1.287 + } 1.288 + sunBatch.Draw(); 1.289 + modelViewMatrix.PopMatrix(); 1.290 + // End Sun 1.291 + ///////////////////////////////// 1.292 1.293 1.294 - //////////////////////////////// 1.295 - // Jupiter 1.296 - float JupiterRot = rotTimer.GetElapsedSeconds() * JupiterRotSpeed; 1.297 + //////////////////////////////// 1.298 + // Jupiter 1.299 + float JupiterRot = rotTimer.GetElapsedSeconds() * JupiterRotSpeed; 1.300 1.301 - modelViewMatrix.PushMatrix(); 1.302 - modelViewMatrix.Translatev(vJupiterPos); 1.303 - // North is up! 1.304 - modelViewMatrix.Rotate(-90.0f - JupiterAxialTilt, 1.0f, 0.0f, 0.0f); 1.305 - // Rotate on axis 1.306 - modelViewMatrix.Rotate(JupiterRot, 0.0f, 0.0f, 1.0f); 1.307 + modelViewMatrix.PushMatrix(); 1.308 + modelViewMatrix.Scale(1.0/1000, 1.0/1000, 1.0/1000); 1.309 + modelViewMatrix.Translatev(vJupiterPos); 1.310 + // North is up! 1.311 + modelViewMatrix.Rotate(-90.0f - JupiterAxialTilt, 1.0f, 0.0f, 0.0f); 1.312 + // Rotate on axis 1.313 + modelViewMatrix.Rotate(JupiterRot, 0.0f, 0.0f, 1.0f); 1.314 1.315 - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]); 1.316 - if (polymode == GL_FILL) 1.317 - { 1.318 - // shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, 1.319 - // transformPipeline.GetModelViewProjectionMatrix(), 1.320 - // 0); 1.321 - shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, 1.322 - modelViewMatrix.GetMatrix(), 1.323 - transformPipeline.GetProjectionMatrix(), 1.324 - vLightTransformed, 1.325 - vWhite, 1.326 - 0); 1.327 - } 1.328 - else 1.329 - { 1.330 - shaderManager.UseStockShader(GLT_SHADER_FLAT, 1.331 - transformPipeline.GetModelViewProjectionMatrix(), 1.332 - vWhite); 1.333 + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]); 1.334 + if (polymode == GL_FILL) 1.335 + { 1.336 + shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, 1.337 + modelViewMatrix.GetMatrix(), 1.338 + transformPipeline.GetProjectionMatrix(), 1.339 + vLightTransformed, 1.340 + vWhite, 1.341 + 0); 1.342 + } 1.343 + else 1.344 + { 1.345 + shaderManager.UseStockShader(GLT_SHADER_FLAT, 1.346 + transformPipeline.GetModelViewProjectionMatrix(), 1.347 + vWhite); 1.348 1.349 - } 1.350 - jupiterBatch.Draw(); 1.351 - modelViewMatrix.PopMatrix(); 1.352 - // End Jupiter 1.353 - //////////////////////////////// 1.354 + } 1.355 + jupiterBatch.Draw(); 1.356 + modelViewMatrix.PopMatrix(); 1.357 + // End Jupiter 1.358 + //////////////////////////////// 1.359 1.360 1.361 1.362 1.363 - ///////////////////////////////// 1.364 - // Begin Earth/Moon 1.365 + ///////////////////////////////// 1.366 + // Begin Earth/Moon 1.367 1.368 - // Begin Earth 1.369 - float EarthRot = rotTimer.GetElapsedSeconds() * EarthRotSpeed; 1.370 + // Begin Earth 1.371 + float EarthRot = rotTimer.GetElapsedSeconds() * EarthRotSpeed; 1.372 1.373 - modelViewMatrix.PushMatrix(); 1.374 - modelViewMatrix.Translatev(vEarthPos); 1.375 - modelViewMatrix.PushMatrix(); // Save unrotated matrix for when we do the Moon 1.376 + modelViewMatrix.PushMatrix(); 1.377 + modelViewMatrix.Translatev(vEarthPos); 1.378 + modelViewMatrix.PushMatrix(); // Save unrotated matrix for when we do the Moon 1.379 1.380 - // NOrth is up! 1.381 - modelViewMatrix.Rotate(-90.0f - EarthAxialTilt, 1.0f, 0.0f, 0.0f); 1.382 - // Rotate on the axis 1.383 - modelViewMatrix.Rotate(EarthRot, 0.0f, 0.0f, 1.0f); 1.384 + // NOrth is up! 1.385 + modelViewMatrix.Rotate(-90.0f - EarthAxialTilt, 1.0f, 0.0f, 0.0f); 1.386 + // Rotate on the axis 1.387 + modelViewMatrix.Rotate(EarthRot, 0.0f, 0.0f, 1.0f); 1.388 1.389 - if (polymode == GL_FILL) 1.390 - { 1.391 - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]); 1.392 - shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, 1.393 - modelViewMatrix.GetMatrix(), 1.394 - transformPipeline.GetProjectionMatrix(), 1.395 - vLightTransformed, 1.396 - vWhite, 1.397 - 0); 1.398 - } 1.399 - else 1.400 - { 1.401 - shaderManager.UseStockShader(GLT_SHADER_FLAT, 1.402 - transformPipeline.GetModelViewProjectionMatrix(), 1.403 - vWhite); 1.404 + if (polymode == GL_FILL) 1.405 + { 1.406 + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]); 1.407 + shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, 1.408 + modelViewMatrix.GetMatrix(), 1.409 + transformPipeline.GetProjectionMatrix(), 1.410 + vLightTransformed, 1.411 + vWhite, 1.412 + 0); 1.413 + } 1.414 + else 1.415 + { 1.416 + shaderManager.UseStockShader(GLT_SHADER_FLAT, 1.417 + transformPipeline.GetModelViewProjectionMatrix(), 1.418 + vWhite); 1.419 1.420 - } 1.421 - earthBatch.Draw(); 1.422 - modelViewMatrix.PopMatrix(); 1.423 + } 1.424 + earthBatch.Draw(); 1.425 + modelViewMatrix.PopMatrix(); 1.426 1.427 - // Begin Moon 1.428 + // Begin Moon 1.429 1.430 + 1.431 + // orbit the Earth 1.432 + modelViewMatrix.Rotate(MoonOrbitTilt, 0.0f, 0.0f, 1.0f); 1.433 1.434 - // orbit the Earth 1.435 - modelViewMatrix.Rotate(MoonOrbitTilt, 0.0f, 0.0f, 1.0f); 1.436 - // NOrth is up! 1.437 - modelViewMatrix.Rotate(-90.0f - MoonAxialTilt, 0.0f, 1.0f, 0.0f); 1.438 + // NOrth is up! 1.439 + modelViewMatrix.Rotate(90.0f - MoonAxialTilt, 1.0f, 0.0f, 0.0f); 1.440 + modelViewMatrix.Rotate(90.0f, 0.0f, 0.0f, 1.0f); 1.441 1.442 - float MoonRot = rotTimer.GetElapsedSeconds() * MoonOrbitSpeed * 10; 1.443 - modelViewMatrix.Rotate(MoonRot, 0.0f, 1.0f, 0.0f); 1.444 + float MoonRot = rotTimer.GetElapsedSeconds() * MoonOrbitSpeed ; 1.445 + modelViewMatrix.Rotate(MoonRot, 0.0f, 0.0f, -1.0f); 1.446 1.447 - modelViewMatrix.Translate(0.5f, 0.0f, 0.0f); 1.448 + modelViewMatrix.Translate(0.0f, 0.5f, 0.0f); 1.449 1.450 1.451 - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]); 1.452 - if (polymode == GL_FILL) 1.453 - { 1.454 - shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, 1.455 - modelViewMatrix.GetMatrix(), 1.456 - transformPipeline.GetProjectionMatrix(), 1.457 - vLightTransformed, 1.458 - vWhite, 1.459 - 0); 1.460 - } 1.461 - else 1.462 - { 1.463 - shaderManager.UseStockShader(GLT_SHADER_FLAT, 1.464 - transformPipeline.GetModelViewProjectionMatrix(), 1.465 - vWhite); 1.466 - } 1.467 - moonBatch.Draw(); 1.468 - modelViewMatrix.PopMatrix(); 1.469 + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]); 1.470 + if (polymode == GL_FILL) 1.471 + { 1.472 + shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, 1.473 + modelViewMatrix.GetMatrix(), 1.474 + transformPipeline.GetProjectionMatrix(), 1.475 + vLightTransformed, 1.476 + vWhite, 1.477 + 0); 1.478 + } 1.479 + else 1.480 + { 1.481 + shaderManager.UseStockShader(GLT_SHADER_FLAT, 1.482 + transformPipeline.GetModelViewProjectionMatrix(), 1.483 + vWhite); 1.484 + } 1.485 + moonBatch.Draw(); 1.486 + modelViewMatrix.PopMatrix(); 1.487 1.488 1.489 - modelViewMatrix.PopMatrix(); 1.490 - // End Earth/Moon 1.491 - //////////////////////////////// 1.492 + modelViewMatrix.PopMatrix(); 1.493 + // End Earth/Moon 1.494 + //////////////////////////////// 1.495 1.496 - } 1.497 + } 1.498 + 1.499 +//////////////////////////////////////////////////////////////////////////////// 1.500 +void load_md2(const std::string & file, GLTriangleBatch & batch) 1.501 + { 1.502 + std::string fpath(Data_Dir); 1.503 + fpath.append(file); 1.504 + 1.505 + MD2 model(fpath); 1.506 + if (! model.ok) 1.507 + { 1.508 + std::cerr << "Unable to load model " << fpath << std::endl; 1.509 + exit(EXIT_FAILURE); 1.510 + } 1.511 + 1.512 + batch.BeginMesh(model.header.num_verts); 1.513 + M3DVector3f verts[3]; 1.514 + M3DVector3f norms[3]; 1.515 + M3DVector2f tex_coords[3]; 1.516 + int vi = 0; 1.517 + for (std::vector<GL_Triangle>::iterator it = model.triangles.begin() ; it != model.triangles.end(); ++it) 1.518 + { 1.519 + 1.520 + for (int v=0; v<3; v++) 1.521 + for (int i=0; i<3; i++) 1.522 + verts[v][i] = model.frames[0].verts[ (*it).vert[v] ].coord[i]; 1.523 + 1.524 + for (int t=0; t<3; t++) 1.525 + for (int i=0; i<2; i++) 1.526 + tex_coords[t][i] = model.frames[0].verts[ (*it).tex[t] ].coord[i]; 1.527 + 1.528 + for (int v=0; v<3; v++) 1.529 + for (int i=0; i<3; i++) 1.530 + norms[v][i] = MD2::light_normals[ model.frames[0].verts[ (*it).vert[v] ].n ].coord[i]; 1.531 + 1.532 + // std::cout << "verts " ; 1.533 + // for (int v=0; v<3; v++) 1.534 + // { 1.535 + // for (int i=0; i<3; i++) 1.536 + // std::cout << verts[v][i] << " "; 1.537 + // std::cout << " "; 1.538 + // } 1.539 + // std::cout << std::endl; 1.540 + // std::cout << "norms " ; 1.541 + // for (int v=0; v<3; v++) 1.542 + // { 1.543 + // for (int i=0; i<3; i++) 1.544 + // std::cout << norms[v][i] << " "; 1.545 + // std::cout << " "; 1.546 + // } 1.547 + // std::cout << std::endl; 1.548 + 1.549 + batch.AddTriangle(verts, norms, tex_coords); 1.550 + } 1.551 + batch.End(); 1.552 + } 1.553 1.554 //////////////////////////////////////////////////////////////////////////////// 1.555 void RenderScene(void) 1.556 - { 1.557 - static CStopWatch rotTimer; 1.558 - float yRot = - rotTimer.GetElapsedSeconds() * 60.0f; 1.559 + { 1.560 + static CStopWatch rotTimer; 1.561 + float yRot = - rotTimer.GetElapsedSeconds() * 60.0f; 1.562 1.563 - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 1.564 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 1.565 1.566 - // Begin Render 1.567 - modelViewMatrix.PushMatrix(); 1.568 + // Begin Render 1.569 + modelViewMatrix.PushMatrix(); 1.570 1.571 - // Begin Camera position 1.572 - static M3DMatrix44f mCamera; 1.573 - cameraFrame.GetCameraMatrix(mCamera); 1.574 - modelViewMatrix.MultMatrix(mCamera); 1.575 - // End Camera position 1.576 + // Begin Camera position 1.577 + static M3DMatrix44f mCamera; 1.578 + cameraFrame.GetCameraMatrix(mCamera); 1.579 + modelViewMatrix.MultMatrix(mCamera); 1.580 + // End Camera position 1.581 1.582 - DrawSolarSystem(yRot); 1.583 + DrawSolarSystem(yRot); 1.584 1.585 - // End Render 1.586 - modelViewMatrix.PopMatrix(); 1.587 + // End Render 1.588 + modelViewMatrix.PopMatrix(); 1.589 1.590 - glutSwapBuffers(); 1.591 - glutPostRedisplay(); 1.592 - } 1.593 + glutSwapBuffers(); 1.594 + glutPostRedisplay(); 1.595 + } 1.596 1.597 //////////////////////////////////////////////////////////////////////////////// 1.598 void SetupRC() 1.599 - { 1.600 - shaderManager.InitializeStockShaders(); 1.601 - glEnable(GL_DEPTH_TEST); 1.602 - glEnable(GL_CULL_FACE); 1.603 - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 1.604 + { 1.605 + shaderManager.InitializeStockShaders(); 1.606 + glEnable(GL_DEPTH_TEST); 1.607 + glEnable(GL_CULL_FACE); 1.608 + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 1.609 1.610 - gltMakeSphere(jupiterBatch, 0.5f, 128, 128 ); 1.611 - gltMakeSphere(earthBatch, 0.2f, 26, 26); 1.612 - gltMakeSphere(moonBatch, 0.1f, 26, 26); 1.613 - gltMakeSphere(sunBatch, 1.0f, 26, 26); 1.614 1.615 - glGenTextures(NUM_TEXTURES, uiTextures); 1.616 1.617 - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]); 1.618 - LoadTGATexture("../textures/earth.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 1.619 + glGenTextures(NUM_TEXTURES, uiTextures); 1.620 1.621 - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]); 1.622 - LoadTGATexture("../textures/moon.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 1.623 + //////////////////////////////////////// 1.624 + // Earth 1.625 + gltMakeSphere(earthBatch, 0.2f, 26, 26); 1.626 + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]); 1.627 + LoadTGATexture("../textures/earth.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 1.628 1.629 - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]); 1.630 - LoadTGATexture("../textures/jupiter.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 1.631 + //////////////////////////////////////// 1.632 + // Moon 1.633 + gltMakeSphere(moonBatch, 0.1f, 26, 26); 1.634 + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]); 1.635 + LoadTGATexture("../textures/moon.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 1.636 1.637 - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]); 1.638 - LoadTGATexture("../textures/sun.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 1.639 + //////////////////////////////////////// 1.640 + // Jupiter 1.641 + // gltMakeSphere(jupiterBatch, 0.5f, 128, 128 ); 1.642 + load_md2("jupiter.md2", jupiterBatch); 1.643 + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]); 1.644 + std::string f(Data_Dir); 1.645 + f.append("jupiter.tga"); 1.646 + LoadTGATexture(f.c_str(), GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 1.647 1.648 - static M3DMatrix44f mCamera; 1.649 - cameraFrame.GetCameraMatrix(mCamera); 1.650 - cameraFrame.MoveForward(-10.0); 1.651 -// cameraFrame.RotateWorld(15.0, 0.0f, 1.0f, 0.0f); 1.652 - } 1.653 + //////////////////////////////////////// 1.654 + // Sun 1.655 + gltMakeSphere(sunBatch, 1.0f, 26, 26); 1.656 + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]); 1.657 + LoadTGATexture("../textures/sun.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); 1.658 + 1.659 + //////////////////////////////////////// 1.660 + // Camera 1.661 + static M3DMatrix44f mCamera; 1.662 + cameraFrame.GetCameraMatrix(mCamera); 1.663 + cameraFrame.MoveForward(-10.0); 1.664 + //cameraFrame.RotateWorld(15.0, 0.0f, 1.0f, 0.0f); 1.665 + } 1.666 1.667 //////////////////////////////////////////////////////////////////////////////// 1.668 void ShutDownRC(void) 1.669 - { 1.670 - glDeleteTextures(NUM_TEXTURES, uiTextures); 1.671 - } 1.672 + { 1.673 + glDeleteTextures(NUM_TEXTURES, uiTextures); 1.674 + } 1.675 1.676 //////////////////////////////////////////////////////////////////////////////// 1.677 void ChangeSize(int nWidth, int nHeight) 1.678 - { 1.679 - if (nHeight == 0) 1.680 - { 1.681 - nHeight = 1; 1.682 - } 1.683 - glViewport(0,0,nWidth, nHeight); 1.684 - viewFrustum.SetPerspective(35.0f, float(nWidth)/float(nHeight), 1.0f, 100.0f); 1.685 - projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()); 1.686 - transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix); 1.687 - } 1.688 + { 1.689 + if (nHeight == 0) 1.690 + { 1.691 + nHeight = 1; 1.692 + } 1.693 + glViewport(0,0,nWidth, nHeight); 1.694 + viewFrustum.SetPerspective(35.0f, float(nWidth)/float(nHeight), 1.0f, 100.0f); 1.695 + projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()); 1.696 + transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix); 1.697 + } 1.698 1.699 //////////////////////////////////////////////////////////////////////////////// 1.700 void SpecialKeys(int key, int x, int y) 1.701 - { 1.702 - } 1.703 + { 1.704 + } 1.705 1.706 //////////////////////////////////////////////////////////////////////////////// 1.707 void KeyboardFunc(unsigned char key, int x, int y) 1.708 - { 1.709 - static int changed; 1.710 - static float linear = 0.1f; 1.711 + { 1.712 + static int changed; 1.713 + static float linear = 0.1f; 1.714 1.715 - changed = 0; 1.716 + changed = 0; 1.717 1.718 - if ('w' == key) 1.719 - { 1.720 - cameraFrame.MoveForward(linear); 1.721 - changed = 1; 1.722 - } 1.723 - else if ('W' == key) 1.724 - { 1.725 - cameraFrame.MoveForward(10*linear); 1.726 - changed = 1; 1.727 - } 1.728 - else if ('s' == key) 1.729 - { 1.730 - cameraFrame.MoveForward(-linear); 1.731 - changed = 1; 1.732 - } 1.733 - else if ('S' == key) 1.734 - { 1.735 - cameraFrame.MoveForward(-10*linear); 1.736 - changed = 1; 1.737 - } 1.738 - else if ('a' == key) 1.739 - { 1.740 - cameraFrame.MoveRight(linear); 1.741 - changed = 1; 1.742 - } 1.743 - else if ('A' == key) 1.744 - { 1.745 - cameraFrame.MoveRight(10*linear); 1.746 - changed = 1; 1.747 - } 1.748 - else if ('d' == key) 1.749 - { 1.750 - cameraFrame.MoveRight(-linear); 1.751 - changed = 1; 1.752 - } 1.753 - else if ('D' == key) 1.754 - { 1.755 - cameraFrame.MoveRight(-10*linear); 1.756 - changed = 1; 1.757 - } 1.758 + if ('w' == key) 1.759 + { 1.760 + cameraFrame.MoveForward(linear); 1.761 + changed = 1; 1.762 + } 1.763 + else if ('W' == key) 1.764 + { 1.765 + cameraFrame.MoveForward(10*linear); 1.766 + changed = 1; 1.767 + } 1.768 + else if ('s' == key) 1.769 + { 1.770 + cameraFrame.MoveForward(-linear); 1.771 + changed = 1; 1.772 + } 1.773 + else if ('S' == key) 1.774 + { 1.775 + cameraFrame.MoveForward(-10*linear); 1.776 + changed = 1; 1.777 + } 1.778 + else if ('a' == key) 1.779 + { 1.780 + cameraFrame.MoveRight(linear); 1.781 + changed = 1; 1.782 + } 1.783 + else if ('A' == key) 1.784 + { 1.785 + cameraFrame.MoveRight(10*linear); 1.786 + changed = 1; 1.787 + } 1.788 + else if ('d' == key) 1.789 + { 1.790 + cameraFrame.MoveRight(-linear); 1.791 + changed = 1; 1.792 + } 1.793 + else if ('D' == key) 1.794 + { 1.795 + cameraFrame.MoveRight(-10*linear); 1.796 + changed = 1; 1.797 + } 1.798 1.799 - else if ('q' == key) 1.800 - { 1.801 - exit(0); 1.802 - } 1.803 - else if ('f' == key) 1.804 - { 1.805 - if (fullscreen) 1.806 - { 1.807 - glutReshapeWindow(width, height); 1.808 - fullscreen = 0; 1.809 - } 1.810 - else 1.811 - { 1.812 - width = glutGet(GLUT_WINDOW_WIDTH); 1.813 - height = glutGet(GLUT_WINDOW_HEIGHT); 1.814 - glutFullScreen(); 1.815 - fullscreen = 1; 1.816 - } 1.817 - } 1.818 - else if ('o' == key) 1.819 - { 1.820 - // 'o' for 'outline' - toggle wireframe rendering 1.821 - polymode = (polymode == GL_FILL) ? GL_LINE : GL_FILL; 1.822 - glPolygonMode(GL_FRONT_AND_BACK, polymode); 1.823 - } 1.824 - else if ('p' == key) 1.825 - { 1.826 - // 'p' for 'print screen' - save a screenshot 1.827 - char filename[20]; 1.828 - get_next_file_name(filename); 1.829 + else if ('q' == key) 1.830 + { 1.831 + exit(0); 1.832 + } 1.833 + else if ('f' == key) 1.834 + { 1.835 + if (fullscreen) 1.836 + { 1.837 + glutReshapeWindow(width, height); 1.838 + fullscreen = 0; 1.839 + } 1.840 + else 1.841 + { 1.842 + width = glutGet(GLUT_WINDOW_WIDTH); 1.843 + height = glutGet(GLUT_WINDOW_HEIGHT); 1.844 + glutFullScreen(); 1.845 + fullscreen = 1; 1.846 + } 1.847 + } 1.848 + else if ('o' == key) 1.849 + { 1.850 + // 'o' for 'outline' - toggle wireframe rendering 1.851 + polymode = (polymode == GL_FILL) ? GL_LINE : GL_FILL; 1.852 + glPolygonMode(GL_FRONT_AND_BACK, polymode); 1.853 + } 1.854 + else if ('p' == key) 1.855 + { 1.856 + // 'p' for 'print screen' - save a screenshot 1.857 + char filename[20]; 1.858 + get_next_file_name(filename); 1.859 1.860 - gltGrabScreenTGA(filename); 1.861 - } 1.862 + gltGrabScreenTGA(filename); 1.863 + } 1.864 1.865 - if (changed) 1.866 - { 1.867 - glutPostRedisplay(); 1.868 - } 1.869 - } 1.870 + if (changed) 1.871 + { 1.872 + glutPostRedisplay(); 1.873 + } 1.874 + } 1.875 1.876 //////////////////////////////////////////////////////////////////////////////// 1.877 void MouseMotionFunc (int x, int y) 1.878 - { 1.879 - static float angular = (float) m3dDegToRad(0.5f); 1.880 - static int xx = -1; 1.881 - static int yy = -1; 1.882 + { 1.883 + static float angular = (float) m3dDegToRad(0.5f); 1.884 + static int xx = -1; 1.885 + static int yy = -1; 1.886 1.887 - if (-1 == xx) 1.888 - { 1.889 - xx = x; 1.890 - yy = y; 1.891 - } 1.892 + if (-1 == xx) 1.893 + { 1.894 + xx = x; 1.895 + yy = y; 1.896 + } 1.897 1.898 - if ((0 == x) || (x < xx)) 1.899 - { 1.900 - cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f); 1.901 - glutPostRedisplay(); 1.902 - } 1.903 - else if ((x == glutGet(GLUT_WINDOW_WIDTH) -1) || (x > xx)) 1.904 - { 1.905 - cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f); 1.906 - glutPostRedisplay(); 1.907 - } 1.908 - // Hmm. Need to transform normal vector, don't I? 1.909 - // if ((0 == y) || (y < yy)) 1.910 - // { 1.911 - // cameraFrame.RotateWorld(angular, 1.0f, 0.0f, 0.0f); 1.912 - // } 1.913 - // else if ((y == glutGet(GLUT_WINDOW_HEIGHT) -1) || (y > yy)) 1.914 - // { 1.915 - // cameraFrame.RotateWorld(-angular, 1.0f, 0.0f, 0.0f); 1.916 - // } 1.917 + if ((0 == x) || (x < xx)) 1.918 + { 1.919 + cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f); 1.920 + glutPostRedisplay(); 1.921 + } 1.922 + else if ((x == glutGet(GLUT_WINDOW_WIDTH) -1) || (x > xx)) 1.923 + { 1.924 + cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f); 1.925 + glutPostRedisplay(); 1.926 + } 1.927 + // Hmm. Need to transform normal vector, don't I? 1.928 + // if ((0 == y) || (y < yy)) 1.929 + // { 1.930 + // cameraFrame.RotateWorld(angular, 1.0f, 0.0f, 0.0f); 1.931 + // } 1.932 + // else if ((y == glutGet(GLUT_WINDOW_HEIGHT) -1) || (y > yy)) 1.933 + // { 1.934 + // cameraFrame.RotateWorld(-angular, 1.0f, 0.0f, 0.0f); 1.935 + // } 1.936 1.937 - xx = x; 1.938 - yy = y; 1.939 - } 1.940 + xx = x; 1.941 + yy = y; 1.942 + } 1.943 1.944 //////////////////////////////////////////////////////////////////////////////// 1.945 int main (int argc, char * argv[]) 1.946 - { 1.947 - gltSetWorkingDirectory(argv[0]); 1.948 - glutInit(&argc, argv); 1.949 - glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); 1.950 - glutInitWindowSize(800, 600); 1.951 - glutCreateWindow("OpenGL SphereWorld"); 1.952 + { 1.953 + gltSetWorkingDirectory(argv[0]); 1.954 + glutInit(&argc, argv); 1.955 + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); 1.956 + glutInitWindowSize(800, 600); 1.957 + glutCreateWindow("OpenGL Solar System"); 1.958 1.959 - glutReshapeFunc(ChangeSize); 1.960 - glutDisplayFunc(RenderScene); 1.961 - glutSpecialFunc(SpecialKeys); 1.962 - glutKeyboardFunc(KeyboardFunc); 1.963 - glutMotionFunc(MouseMotionFunc); 1.964 - glutPassiveMotionFunc(MouseMotionFunc); 1.965 - glutSetCursor(GLUT_CURSOR_NONE); 1.966 + glutReshapeFunc(ChangeSize); 1.967 + glutDisplayFunc(RenderScene); 1.968 + glutSpecialFunc(SpecialKeys); 1.969 + glutKeyboardFunc(KeyboardFunc); 1.970 + glutMotionFunc(MouseMotionFunc); 1.971 + glutPassiveMotionFunc(MouseMotionFunc); 1.972 + glutSetCursor(GLUT_CURSOR_NONE); 1.973 1.974 - GLenum err = glewInit(); 1.975 - if (GLEW_OK != err) 1.976 - { 1.977 - fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); 1.978 - return 1; 1.979 - } 1.980 + GLenum err = glewInit(); 1.981 + if (GLEW_OK != err) 1.982 + { 1.983 + fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); 1.984 + return 1; 1.985 + } 1.986 1.987 - // This enabled vertical sync on Linux 1.988 - // For full generality I should use Glew and check what OS I'm on. 1.989 - // Note that the ATI Catalyst driver exports the WGL_EXT_swap_control 1.990 - // extension name instead of SGI_swap_control as it should. 1.991 - // but nonetheless the actual function provided is glXSwapIntervalSGI 1.992 + // This enabled vertical sync on Linux 1.993 + // For full generality I should use Glew and check what OS I'm on. 1.994 + // Note that the ATI Catalyst driver exports the WGL_EXT_swap_control 1.995 + // extension name instead of SGI_swap_control as it should. 1.996 + // but nonetheless the actual function provided is glXSwapIntervalSGI 1.997 1.998 - PFNGLXSWAPINTERVALSGIPROC SwapInterval; 1.999 - SwapInterval = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalSGI"); 1.1000 + PFNGLXSWAPINTERVALSGIPROC SwapInterval; 1.1001 + SwapInterval = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalSGI"); 1.1002 1.1003 - if (SwapInterval) 1.1004 - SwapInterval(1); 1.1005 + if (SwapInterval) 1.1006 + SwapInterval(1); 1.1007 1.1008 - SetupRC(); 1.1009 - glutMainLoop(); 1.1010 - ShutDownRC(); 1.1011 + SetupRC(); 1.1012 + glutMainLoop(); 1.1013 + ShutDownRC(); 1.1014 1.1015 - return 0; 1.1016 - } 1.1017 + return 0; 1.1018 + } 1.1019 1.1020 1.1021