# HG changeset patch # User Eris Caffee # Date 1367190267 18000 # Node ID 4f8b47ac2715e54a067af8f02261b5906f1cbc0a # Parent c24af3462002063d20b2b335fceba715b57c1727 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. diff -r c24af3462002 -r 4f8b47ac2715 CMakeLists.txt --- a/CMakeLists.txt Sat Apr 27 13:22:17 2013 -0500 +++ b/CMakeLists.txt Sun Apr 28 18:04:27 2013 -0500 @@ -117,7 +117,7 @@ # C -std=gnu99 -std=c99 if (CMAKE_COMPILER_IS_GNUCXX) - add_definitions(-pedantic -Wall -std=c++0x) + add_definitions( -Wall -std=c++0x) endif () @@ -135,11 +135,12 @@ file (GLOB SRCS src/*.c src/*.cpp /home/eris/gamedev/study/OpenGL_Superbible/work/Src/GLTools/src/* + /home/eris/gamedev/projects/MD2/src/* ) file (GLOB HDRS include/*.h include/*.hpp /home/eris/gamedev/study/OpenGL_Superbible/work/Src/GLTools/include/* /home/eris/gamedev/study/OpenGL_Superbible/work/Src/GLTools/include/GL/* - + /home/eris/gamedev/projects/MD2/include/* ) # The directories that contain the libraries we will be linking against. @@ -153,6 +154,7 @@ ${CMAKE_SOURCE_DIR}/include /home/eris/gamedev/study/OpenGL_Superbible/work/Src/GLTools/include/ /home/eris/gamedev/study/OpenGL_Superbible/work/Src/GLTools/include/GL + /home/eris/gamedev/projects/MD2/include ) # Define the executable program file we are creating. We must list all of diff -r c24af3462002 -r 4f8b47ac2715 src/solar-system.cpp --- a/src/solar-system.cpp Sat Apr 27 13:22:17 2013 -0500 +++ b/src/solar-system.cpp Sun Apr 28 18:04:27 2013 -0500 @@ -14,9 +14,15 @@ #include +#include + #include #include +#include "MD2.h" + +//////////////////////////////////////////////////////////////////////////////// + GLShaderManager shaderManager; GLMatrixStack modelViewMatrix; GLMatrixStack projectionMatrix; @@ -31,15 +37,14 @@ int height = 600; int fullscreen = 0; -#define NUM_SPHERES 50 -GLFrame spheres[NUM_SPHERES]; +//////////////////////////////////////////////////////////////////////////////// GLTriangleBatch jupiterBatch; -GLTriangleBatch sphereBatch; GLTriangleBatch earthBatch; GLTriangleBatch moonBatch; GLTriangleBatch sunBatch; -GLBatch floorBatch; + +//////////////////////////////////////////////////////////////////////////////// #define TEX_EARTH 1 #define TEX_MOON 2 @@ -49,514 +54,587 @@ GLuint uiTextures[NUM_TEXTURES]; //////////////////////////////////////////////////////////////////////////////// + +const std::string Data_Dir("../textures/"); + +//////////////////////////////////////////////////////////////////////////////// #define SCREENSHOT_FILENAME_BASE "screenshot-" #define SCREENSHOT_FILENAME_BASELEN 11 #define SCREENSHOT_FILENAME_EXT ".tga" #define SCREENSHOT_FILENAME_EXTLEN 4 int scandir_filter(const struct dirent * d) - { - if (memcmp(d->d_name, SCREENSHOT_FILENAME_BASE, - SCREENSHOT_FILENAME_BASELEN) != 0) return 0; - if (memcmp(d->d_name+SCREENSHOT_FILENAME_BASELEN+3, - SCREENSHOT_FILENAME_EXT, SCREENSHOT_FILENAME_EXTLEN) != 0) - return 0; - if (isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN]) - && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+1]) - && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+2])) - return 1; - return 0; - } + { + if (memcmp(d->d_name, SCREENSHOT_FILENAME_BASE, + SCREENSHOT_FILENAME_BASELEN) != 0) return 0; + if (memcmp(d->d_name+SCREENSHOT_FILENAME_BASELEN+3, + SCREENSHOT_FILENAME_EXT, SCREENSHOT_FILENAME_EXTLEN) != 0) + return 0; + if (isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN]) + && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+1]) + && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+2])) + return 1; + return 0; + } int get_next_file_name(char * filename) - { - static int i = 0; + { + static int i = 0; - if (i == 0) - { - char pattern[SCREENSHOT_FILENAME_BASELEN+3+SCREENSHOT_FILENAME_EXTLEN]; - struct dirent ** file_list; - int num_files = scandir(".", &file_list, scandir_filter, alphasort); - if (num_files != 0) - sprintf(pattern, "%s%%03d%s", SCREENSHOT_FILENAME_BASE, - SCREENSHOT_FILENAME_EXT); - sscanf(file_list[num_files-1]->d_name, pattern, &i); - } - i++; + if (i == 0) + { + char pattern[SCREENSHOT_FILENAME_BASELEN+3+SCREENSHOT_FILENAME_EXTLEN]; + struct dirent ** file_list; + int num_files = scandir(".", &file_list, scandir_filter, alphasort); + if (num_files != 0) + sprintf(pattern, "%s%%03d%s", SCREENSHOT_FILENAME_BASE, + SCREENSHOT_FILENAME_EXT); + sscanf(file_list[num_files-1]->d_name, pattern, &i); + } + i++; - sprintf(filename, "%s%03d%s", SCREENSHOT_FILENAME_BASE, i, - SCREENSHOT_FILENAME_EXT); - return i; - } + sprintf(filename, "%s%03d%s", SCREENSHOT_FILENAME_BASE, i, + SCREENSHOT_FILENAME_EXT); + return i; + } //////////////////////////////////////////////////////////////////////////////// bool LoadTGATexture(const char * szFileName, GLenum minFilter, - GLenum magFilter, GLenum wrapMode) - { - GLbyte * pBits; - int nWidth, nHeight, nComponents; - GLenum eFormat; + GLenum magFilter, GLenum wrapMode) + { + GLbyte * pBits; + int nWidth, nHeight, nComponents; + GLenum eFormat; - pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat); - if (pBits == NULL) - { - fprintf(stderr, "Failed to load %s\n", szFileName); - exit(EXIT_FAILURE); - } + pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat); + if (pBits == NULL) + { + fprintf(stderr, "Failed to load %s\n", szFileName); + exit(EXIT_FAILURE); + } - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, - eFormat, GL_UNSIGNED_BYTE, pBits); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, + eFormat, GL_UNSIGNED_BYTE, pBits); - free(pBits); + free(pBits); - if (minFilter == GL_LINEAR_MIPMAP_LINEAR || - minFilter == GL_LINEAR_MIPMAP_NEAREST || - minFilter == GL_NEAREST_MIPMAP_LINEAR || - minFilter == GL_NEAREST_MIPMAP_NEAREST) - { - glGenerateMipmap(GL_TEXTURE_2D); - } + if (minFilter == GL_LINEAR_MIPMAP_LINEAR || + minFilter == GL_LINEAR_MIPMAP_NEAREST || + minFilter == GL_NEAREST_MIPMAP_LINEAR || + minFilter == GL_NEAREST_MIPMAP_NEAREST) + { + glGenerateMipmap(GL_TEXTURE_2D); + } - return true; - } + return true; + } //////////////////////////////////////////////////////////////////////////////// void DrawSolarSystem(GLfloat yRot) - { - static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f }; - static GLfloat vLightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f }; - static M3DVector3f vSunPos = { 0.0f, 0.0f, 0.0f }; - static M3DVector3f vEarthPos = { 5.0f, 0.0f, 0.0f }; - static M3DVector3f vJupiterPos = { 10.0f, 0.0f, 0.0f }; + { + static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f }; + static GLfloat vLightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f }; + static M3DVector3f vSunPos = { 0.0f, 0.0f, 0.0f }; + static M3DVector3f vEarthPos = { 5.0f, 0.0f, 0.0f }; + static M3DVector3f vJupiterPos = { 10.0f, 0.0f, 0.0f }; - float RotScale = 100.0; - float SunRotSpeed = 1.0/(25*24) * RotScale; + float RotScale = 100.0; + float SunRotSpeed = 1.0/(25*24) * RotScale; - float JupiterRotSpeed = 1.0/9 * RotScale; - float JupiterAxialTilt = 3.13; + float JupiterRotSpeed = 1.0/9 * RotScale; + float JupiterAxialTilt = 3.13; - float EarthRotSpeed = 1.0/24 * RotScale; - float EarthAxialTilt = 23.5; + float EarthRotSpeed = 1.0/24 * RotScale; + float EarthAxialTilt = 23.5; - float MoonRotSpeed = 1.0/29.5 * RotScale; - float MoonAxialTilt = 6.7; - float MoonOrbitSpeed = 1.0/29.5 * RotScale; - float MoonOrbitTilt = 5.145; + // float MoonRotSpeed = 1.0/29.5 * RotScale; + float MoonAxialTilt = 6.7; + float MoonOrbitSpeed = 1.0/(24*29.5) * RotScale; + float MoonOrbitTilt = 5.145; - static CStopWatch rotTimer; + static CStopWatch rotTimer; - // Get the light position in eye space - M3DVector4f vLightTransformed; - M3DMatrix44f mCamera; - modelViewMatrix.GetMatrix(mCamera); - m3dTransformVector4(vLightTransformed, vLightPos, mCamera); + // Get the light position in eye space + M3DVector4f vLightTransformed; + M3DMatrix44f mCamera; + modelViewMatrix.GetMatrix(mCamera); + m3dTransformVector4(vLightTransformed, vLightPos, mCamera); - //////////////////////////////// - // Begin Sun + //////////////////////////////// + // Begin Sun - float SunRot = rotTimer.GetElapsedSeconds() * SunRotSpeed; + float SunRot = rotTimer.GetElapsedSeconds() * SunRotSpeed; - modelViewMatrix.PushMatrix(); + modelViewMatrix.PushMatrix(); - modelViewMatrix.Translatev(vSunPos); - // North is up! - modelViewMatrix.Rotate(-90.0f, 1.0f, 0.0f, 0.0f); - // Rotate on axis - modelViewMatrix.Rotate(SunRot, 0.0f, 0.0f, 1.0f); + modelViewMatrix.Translatev(vSunPos); + // North is up! + modelViewMatrix.Rotate(-90.0f, 1.0f, 0.0f, 0.0f); + // Rotate on axis + modelViewMatrix.Rotate(SunRot, 0.0f, 0.0f, 1.0f); - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]); - if (polymode == GL_FILL) - { - shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, - transformPipeline.GetModelViewProjectionMatrix(), - 0); - } - else - { - shaderManager.UseStockShader(GLT_SHADER_FLAT, - transformPipeline.GetModelViewProjectionMatrix(), - vWhite); - } - sunBatch.Draw(); - modelViewMatrix.PopMatrix(); - // End Sun - ///////////////////////////////// + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]); + if (polymode == GL_FILL) + { + shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, + transformPipeline.GetModelViewProjectionMatrix(), + 0); + } + else + { + shaderManager.UseStockShader(GLT_SHADER_FLAT, + transformPipeline.GetModelViewProjectionMatrix(), + vWhite); + } + sunBatch.Draw(); + modelViewMatrix.PopMatrix(); + // End Sun + ///////////////////////////////// - //////////////////////////////// - // Jupiter - float JupiterRot = rotTimer.GetElapsedSeconds() * JupiterRotSpeed; + //////////////////////////////// + // Jupiter + float JupiterRot = rotTimer.GetElapsedSeconds() * JupiterRotSpeed; - modelViewMatrix.PushMatrix(); - modelViewMatrix.Translatev(vJupiterPos); - // North is up! - modelViewMatrix.Rotate(-90.0f - JupiterAxialTilt, 1.0f, 0.0f, 0.0f); - // Rotate on axis - modelViewMatrix.Rotate(JupiterRot, 0.0f, 0.0f, 1.0f); + modelViewMatrix.PushMatrix(); + modelViewMatrix.Scale(1.0/1000, 1.0/1000, 1.0/1000); + modelViewMatrix.Translatev(vJupiterPos); + // North is up! + modelViewMatrix.Rotate(-90.0f - JupiterAxialTilt, 1.0f, 0.0f, 0.0f); + // Rotate on axis + modelViewMatrix.Rotate(JupiterRot, 0.0f, 0.0f, 1.0f); - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]); - if (polymode == GL_FILL) - { - // shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, - // transformPipeline.GetModelViewProjectionMatrix(), - // 0); - shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, - modelViewMatrix.GetMatrix(), - transformPipeline.GetProjectionMatrix(), - vLightTransformed, - vWhite, - 0); - } - else - { - shaderManager.UseStockShader(GLT_SHADER_FLAT, - transformPipeline.GetModelViewProjectionMatrix(), - vWhite); + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]); + if (polymode == GL_FILL) + { + shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, + modelViewMatrix.GetMatrix(), + transformPipeline.GetProjectionMatrix(), + vLightTransformed, + vWhite, + 0); + } + else + { + shaderManager.UseStockShader(GLT_SHADER_FLAT, + transformPipeline.GetModelViewProjectionMatrix(), + vWhite); - } - jupiterBatch.Draw(); - modelViewMatrix.PopMatrix(); - // End Jupiter - //////////////////////////////// + } + jupiterBatch.Draw(); + modelViewMatrix.PopMatrix(); + // End Jupiter + //////////////////////////////// - ///////////////////////////////// - // Begin Earth/Moon + ///////////////////////////////// + // Begin Earth/Moon - // Begin Earth - float EarthRot = rotTimer.GetElapsedSeconds() * EarthRotSpeed; + // Begin Earth + float EarthRot = rotTimer.GetElapsedSeconds() * EarthRotSpeed; - modelViewMatrix.PushMatrix(); - modelViewMatrix.Translatev(vEarthPos); - modelViewMatrix.PushMatrix(); // Save unrotated matrix for when we do the Moon + modelViewMatrix.PushMatrix(); + modelViewMatrix.Translatev(vEarthPos); + modelViewMatrix.PushMatrix(); // Save unrotated matrix for when we do the Moon - // NOrth is up! - modelViewMatrix.Rotate(-90.0f - EarthAxialTilt, 1.0f, 0.0f, 0.0f); - // Rotate on the axis - modelViewMatrix.Rotate(EarthRot, 0.0f, 0.0f, 1.0f); + // NOrth is up! + modelViewMatrix.Rotate(-90.0f - EarthAxialTilt, 1.0f, 0.0f, 0.0f); + // Rotate on the axis + modelViewMatrix.Rotate(EarthRot, 0.0f, 0.0f, 1.0f); - if (polymode == GL_FILL) - { - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]); - shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, - modelViewMatrix.GetMatrix(), - transformPipeline.GetProjectionMatrix(), - vLightTransformed, - vWhite, - 0); - } - else - { - shaderManager.UseStockShader(GLT_SHADER_FLAT, - transformPipeline.GetModelViewProjectionMatrix(), - vWhite); + if (polymode == GL_FILL) + { + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]); + shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, + modelViewMatrix.GetMatrix(), + transformPipeline.GetProjectionMatrix(), + vLightTransformed, + vWhite, + 0); + } + else + { + shaderManager.UseStockShader(GLT_SHADER_FLAT, + transformPipeline.GetModelViewProjectionMatrix(), + vWhite); - } - earthBatch.Draw(); - modelViewMatrix.PopMatrix(); + } + earthBatch.Draw(); + modelViewMatrix.PopMatrix(); - // Begin Moon + // Begin Moon + + // orbit the Earth + modelViewMatrix.Rotate(MoonOrbitTilt, 0.0f, 0.0f, 1.0f); - // orbit the Earth - modelViewMatrix.Rotate(MoonOrbitTilt, 0.0f, 0.0f, 1.0f); - // NOrth is up! - modelViewMatrix.Rotate(-90.0f - MoonAxialTilt, 0.0f, 1.0f, 0.0f); + // NOrth is up! + modelViewMatrix.Rotate(90.0f - MoonAxialTilt, 1.0f, 0.0f, 0.0f); + modelViewMatrix.Rotate(90.0f, 0.0f, 0.0f, 1.0f); - float MoonRot = rotTimer.GetElapsedSeconds() * MoonOrbitSpeed * 10; - modelViewMatrix.Rotate(MoonRot, 0.0f, 1.0f, 0.0f); + float MoonRot = rotTimer.GetElapsedSeconds() * MoonOrbitSpeed ; + modelViewMatrix.Rotate(MoonRot, 0.0f, 0.0f, -1.0f); - modelViewMatrix.Translate(0.5f, 0.0f, 0.0f); + modelViewMatrix.Translate(0.0f, 0.5f, 0.0f); - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]); - if (polymode == GL_FILL) - { - shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, - modelViewMatrix.GetMatrix(), - transformPipeline.GetProjectionMatrix(), - vLightTransformed, - vWhite, - 0); - } - else - { - shaderManager.UseStockShader(GLT_SHADER_FLAT, - transformPipeline.GetModelViewProjectionMatrix(), - vWhite); - } - moonBatch.Draw(); - modelViewMatrix.PopMatrix(); + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]); + if (polymode == GL_FILL) + { + shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, + modelViewMatrix.GetMatrix(), + transformPipeline.GetProjectionMatrix(), + vLightTransformed, + vWhite, + 0); + } + else + { + shaderManager.UseStockShader(GLT_SHADER_FLAT, + transformPipeline.GetModelViewProjectionMatrix(), + vWhite); + } + moonBatch.Draw(); + modelViewMatrix.PopMatrix(); - modelViewMatrix.PopMatrix(); - // End Earth/Moon - //////////////////////////////// + modelViewMatrix.PopMatrix(); + // End Earth/Moon + //////////////////////////////// - } + } + +//////////////////////////////////////////////////////////////////////////////// +void load_md2(const std::string & file, GLTriangleBatch & batch) + { + std::string fpath(Data_Dir); + fpath.append(file); + + MD2 model(fpath); + if (! model.ok) + { + std::cerr << "Unable to load model " << fpath << std::endl; + exit(EXIT_FAILURE); + } + + batch.BeginMesh(model.header.num_verts); + M3DVector3f verts[3]; + M3DVector3f norms[3]; + M3DVector2f tex_coords[3]; + int vi = 0; + for (std::vector::iterator it = model.triangles.begin() ; it != model.triangles.end(); ++it) + { + + for (int v=0; v<3; v++) + for (int i=0; i<3; i++) + verts[v][i] = model.frames[0].verts[ (*it).vert[v] ].coord[i]; + + for (int t=0; t<3; t++) + for (int i=0; i<2; i++) + tex_coords[t][i] = model.frames[0].verts[ (*it).tex[t] ].coord[i]; + + for (int v=0; v<3; v++) + for (int i=0; i<3; i++) + norms[v][i] = MD2::light_normals[ model.frames[0].verts[ (*it).vert[v] ].n ].coord[i]; + + // std::cout << "verts " ; + // for (int v=0; v<3; v++) + // { + // for (int i=0; i<3; i++) + // std::cout << verts[v][i] << " "; + // std::cout << " "; + // } + // std::cout << std::endl; + // std::cout << "norms " ; + // for (int v=0; v<3; v++) + // { + // for (int i=0; i<3; i++) + // std::cout << norms[v][i] << " "; + // std::cout << " "; + // } + // std::cout << std::endl; + + batch.AddTriangle(verts, norms, tex_coords); + } + batch.End(); + } //////////////////////////////////////////////////////////////////////////////// void RenderScene(void) - { - static CStopWatch rotTimer; - float yRot = - rotTimer.GetElapsedSeconds() * 60.0f; + { + static CStopWatch rotTimer; + float yRot = - rotTimer.GetElapsedSeconds() * 60.0f; - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - // Begin Render - modelViewMatrix.PushMatrix(); + // Begin Render + modelViewMatrix.PushMatrix(); - // Begin Camera position - static M3DMatrix44f mCamera; - cameraFrame.GetCameraMatrix(mCamera); - modelViewMatrix.MultMatrix(mCamera); - // End Camera position + // Begin Camera position + static M3DMatrix44f mCamera; + cameraFrame.GetCameraMatrix(mCamera); + modelViewMatrix.MultMatrix(mCamera); + // End Camera position - DrawSolarSystem(yRot); + DrawSolarSystem(yRot); - // End Render - modelViewMatrix.PopMatrix(); + // End Render + modelViewMatrix.PopMatrix(); - glutSwapBuffers(); - glutPostRedisplay(); - } + glutSwapBuffers(); + glutPostRedisplay(); + } //////////////////////////////////////////////////////////////////////////////// void SetupRC() - { - shaderManager.InitializeStockShaders(); - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + { + shaderManager.InitializeStockShaders(); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - gltMakeSphere(jupiterBatch, 0.5f, 128, 128 ); - gltMakeSphere(earthBatch, 0.2f, 26, 26); - gltMakeSphere(moonBatch, 0.1f, 26, 26); - gltMakeSphere(sunBatch, 1.0f, 26, 26); - glGenTextures(NUM_TEXTURES, uiTextures); - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]); - LoadTGATexture("../textures/earth.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); + glGenTextures(NUM_TEXTURES, uiTextures); - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]); - LoadTGATexture("../textures/moon.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); + //////////////////////////////////////// + // Earth + gltMakeSphere(earthBatch, 0.2f, 26, 26); + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]); + LoadTGATexture("../textures/earth.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]); - LoadTGATexture("../textures/jupiter.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); + //////////////////////////////////////// + // Moon + gltMakeSphere(moonBatch, 0.1f, 26, 26); + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]); + LoadTGATexture("../textures/moon.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); - glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]); - LoadTGATexture("../textures/sun.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); + //////////////////////////////////////// + // Jupiter + // gltMakeSphere(jupiterBatch, 0.5f, 128, 128 ); + load_md2("jupiter.md2", jupiterBatch); + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]); + std::string f(Data_Dir); + f.append("jupiter.tga"); + LoadTGATexture(f.c_str(), GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); - static M3DMatrix44f mCamera; - cameraFrame.GetCameraMatrix(mCamera); - cameraFrame.MoveForward(-10.0); -// cameraFrame.RotateWorld(15.0, 0.0f, 1.0f, 0.0f); - } + //////////////////////////////////////// + // Sun + gltMakeSphere(sunBatch, 1.0f, 26, 26); + glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]); + LoadTGATexture("../textures/sun.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); + + //////////////////////////////////////// + // Camera + static M3DMatrix44f mCamera; + cameraFrame.GetCameraMatrix(mCamera); + cameraFrame.MoveForward(-10.0); + //cameraFrame.RotateWorld(15.0, 0.0f, 1.0f, 0.0f); + } //////////////////////////////////////////////////////////////////////////////// void ShutDownRC(void) - { - glDeleteTextures(NUM_TEXTURES, uiTextures); - } + { + glDeleteTextures(NUM_TEXTURES, uiTextures); + } //////////////////////////////////////////////////////////////////////////////// void ChangeSize(int nWidth, int nHeight) - { - if (nHeight == 0) - { - nHeight = 1; - } - glViewport(0,0,nWidth, nHeight); - viewFrustum.SetPerspective(35.0f, float(nWidth)/float(nHeight), 1.0f, 100.0f); - projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()); - transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix); - } + { + if (nHeight == 0) + { + nHeight = 1; + } + glViewport(0,0,nWidth, nHeight); + viewFrustum.SetPerspective(35.0f, float(nWidth)/float(nHeight), 1.0f, 100.0f); + projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()); + transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix); + } //////////////////////////////////////////////////////////////////////////////// void SpecialKeys(int key, int x, int y) - { - } + { + } //////////////////////////////////////////////////////////////////////////////// void KeyboardFunc(unsigned char key, int x, int y) - { - static int changed; - static float linear = 0.1f; + { + static int changed; + static float linear = 0.1f; - changed = 0; + changed = 0; - if ('w' == key) - { - cameraFrame.MoveForward(linear); - changed = 1; - } - else if ('W' == key) - { - cameraFrame.MoveForward(10*linear); - changed = 1; - } - else if ('s' == key) - { - cameraFrame.MoveForward(-linear); - changed = 1; - } - else if ('S' == key) - { - cameraFrame.MoveForward(-10*linear); - changed = 1; - } - else if ('a' == key) - { - cameraFrame.MoveRight(linear); - changed = 1; - } - else if ('A' == key) - { - cameraFrame.MoveRight(10*linear); - changed = 1; - } - else if ('d' == key) - { - cameraFrame.MoveRight(-linear); - changed = 1; - } - else if ('D' == key) - { - cameraFrame.MoveRight(-10*linear); - changed = 1; - } + if ('w' == key) + { + cameraFrame.MoveForward(linear); + changed = 1; + } + else if ('W' == key) + { + cameraFrame.MoveForward(10*linear); + changed = 1; + } + else if ('s' == key) + { + cameraFrame.MoveForward(-linear); + changed = 1; + } + else if ('S' == key) + { + cameraFrame.MoveForward(-10*linear); + changed = 1; + } + else if ('a' == key) + { + cameraFrame.MoveRight(linear); + changed = 1; + } + else if ('A' == key) + { + cameraFrame.MoveRight(10*linear); + changed = 1; + } + else if ('d' == key) + { + cameraFrame.MoveRight(-linear); + changed = 1; + } + else if ('D' == key) + { + cameraFrame.MoveRight(-10*linear); + changed = 1; + } - else if ('q' == key) - { - exit(0); - } - else if ('f' == key) - { - if (fullscreen) - { - glutReshapeWindow(width, height); - fullscreen = 0; - } - else - { - width = glutGet(GLUT_WINDOW_WIDTH); - height = glutGet(GLUT_WINDOW_HEIGHT); - glutFullScreen(); - fullscreen = 1; - } - } - else if ('o' == key) - { - // 'o' for 'outline' - toggle wireframe rendering - polymode = (polymode == GL_FILL) ? GL_LINE : GL_FILL; - glPolygonMode(GL_FRONT_AND_BACK, polymode); - } - else if ('p' == key) - { - // 'p' for 'print screen' - save a screenshot - char filename[20]; - get_next_file_name(filename); + else if ('q' == key) + { + exit(0); + } + else if ('f' == key) + { + if (fullscreen) + { + glutReshapeWindow(width, height); + fullscreen = 0; + } + else + { + width = glutGet(GLUT_WINDOW_WIDTH); + height = glutGet(GLUT_WINDOW_HEIGHT); + glutFullScreen(); + fullscreen = 1; + } + } + else if ('o' == key) + { + // 'o' for 'outline' - toggle wireframe rendering + polymode = (polymode == GL_FILL) ? GL_LINE : GL_FILL; + glPolygonMode(GL_FRONT_AND_BACK, polymode); + } + else if ('p' == key) + { + // 'p' for 'print screen' - save a screenshot + char filename[20]; + get_next_file_name(filename); - gltGrabScreenTGA(filename); - } + gltGrabScreenTGA(filename); + } - if (changed) - { - glutPostRedisplay(); - } - } + if (changed) + { + glutPostRedisplay(); + } + } //////////////////////////////////////////////////////////////////////////////// void MouseMotionFunc (int x, int y) - { - static float angular = (float) m3dDegToRad(0.5f); - static int xx = -1; - static int yy = -1; + { + static float angular = (float) m3dDegToRad(0.5f); + static int xx = -1; + static int yy = -1; - if (-1 == xx) - { - xx = x; - yy = y; - } + if (-1 == xx) + { + xx = x; + yy = y; + } - if ((0 == x) || (x < xx)) - { - cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f); - glutPostRedisplay(); - } - else if ((x == glutGet(GLUT_WINDOW_WIDTH) -1) || (x > xx)) - { - cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f); - glutPostRedisplay(); - } - // Hmm. Need to transform normal vector, don't I? - // if ((0 == y) || (y < yy)) - // { - // cameraFrame.RotateWorld(angular, 1.0f, 0.0f, 0.0f); - // } - // else if ((y == glutGet(GLUT_WINDOW_HEIGHT) -1) || (y > yy)) - // { - // cameraFrame.RotateWorld(-angular, 1.0f, 0.0f, 0.0f); - // } + if ((0 == x) || (x < xx)) + { + cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f); + glutPostRedisplay(); + } + else if ((x == glutGet(GLUT_WINDOW_WIDTH) -1) || (x > xx)) + { + cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f); + glutPostRedisplay(); + } + // Hmm. Need to transform normal vector, don't I? + // if ((0 == y) || (y < yy)) + // { + // cameraFrame.RotateWorld(angular, 1.0f, 0.0f, 0.0f); + // } + // else if ((y == glutGet(GLUT_WINDOW_HEIGHT) -1) || (y > yy)) + // { + // cameraFrame.RotateWorld(-angular, 1.0f, 0.0f, 0.0f); + // } - xx = x; - yy = y; - } + xx = x; + yy = y; + } //////////////////////////////////////////////////////////////////////////////// int main (int argc, char * argv[]) - { - gltSetWorkingDirectory(argv[0]); - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); - glutInitWindowSize(800, 600); - glutCreateWindow("OpenGL SphereWorld"); + { + gltSetWorkingDirectory(argv[0]); + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); + glutInitWindowSize(800, 600); + glutCreateWindow("OpenGL Solar System"); - glutReshapeFunc(ChangeSize); - glutDisplayFunc(RenderScene); - glutSpecialFunc(SpecialKeys); - glutKeyboardFunc(KeyboardFunc); - glutMotionFunc(MouseMotionFunc); - glutPassiveMotionFunc(MouseMotionFunc); - glutSetCursor(GLUT_CURSOR_NONE); + glutReshapeFunc(ChangeSize); + glutDisplayFunc(RenderScene); + glutSpecialFunc(SpecialKeys); + glutKeyboardFunc(KeyboardFunc); + glutMotionFunc(MouseMotionFunc); + glutPassiveMotionFunc(MouseMotionFunc); + glutSetCursor(GLUT_CURSOR_NONE); - GLenum err = glewInit(); - if (GLEW_OK != err) - { - fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); - return 1; - } + GLenum err = glewInit(); + if (GLEW_OK != err) + { + fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); + return 1; + } - // This enabled vertical sync on Linux - // For full generality I should use Glew and check what OS I'm on. - // Note that the ATI Catalyst driver exports the WGL_EXT_swap_control - // extension name instead of SGI_swap_control as it should. - // but nonetheless the actual function provided is glXSwapIntervalSGI + // This enabled vertical sync on Linux + // For full generality I should use Glew and check what OS I'm on. + // Note that the ATI Catalyst driver exports the WGL_EXT_swap_control + // extension name instead of SGI_swap_control as it should. + // but nonetheless the actual function provided is glXSwapIntervalSGI - PFNGLXSWAPINTERVALSGIPROC SwapInterval; - SwapInterval = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalSGI"); + PFNGLXSWAPINTERVALSGIPROC SwapInterval; + SwapInterval = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalSGI"); - if (SwapInterval) - SwapInterval(1); + if (SwapInterval) + SwapInterval(1); - SetupRC(); - glutMainLoop(); - ShutDownRC(); + SetupRC(); + glutMainLoop(); + ShutDownRC(); - return 0; - } + return 0; + } diff -r c24af3462002 -r 4f8b47ac2715 textures/Marble.tga Binary file textures/Marble.tga has changed diff -r c24af3462002 -r 4f8b47ac2715 textures/Marslike.tga Binary file textures/Marslike.tga has changed diff -r c24af3462002 -r 4f8b47ac2715 textures/MoonLike.tga Binary file textures/MoonLike.tga has changed diff -r c24af3462002 -r 4f8b47ac2715 textures/jupiter.md2 Binary file textures/jupiter.md2 has changed