# HG changeset patch # User Eris Caffee # Date 1367086937 18000 # Node ID c24af3462002063d20b2b335fceba715b57c1727 # Parent 9c54994a2635c3583ff1cd69d64f5fc157973dc1 initial commit diff -r 9c54994a2635 -r c24af3462002 CMakeLists.txt --- a/CMakeLists.txt Tue Mar 29 00:27:15 2011 -0500 +++ b/CMakeLists.txt Sat Apr 27 13:22:17 2013 -0500 @@ -66,7 +66,7 @@ #set (CMAKE_VERBOSE_MAKEFILE ON) # Name your program! -set (App_Name "") +set (App_Name "solar-system") if (App_Name STREQUAL "") message (FATAL_ERROR "You must set the App_Name variable!") endif () @@ -112,12 +112,12 @@ ################################################################################ # When using GCC turn on lots of warnings. -# Some other options to consider: -# C++ -Weffc++ + # Some other options to consider: +# C++ -Weffc++ -std=c++0x # C -std=gnu99 -std=c99 if (CMAKE_COMPILER_IS_GNUCXX) - add_definitions(-pedantic -Wall) + add_definitions(-pedantic -Wall -std=c++0x) endif () @@ -133,8 +133,14 @@ ################################################################################ # The core project files -file (GLOB SRCS src/*.c src/*.cpp) -file (GLOB HDRS include/*.h include/*.hpp) +file (GLOB SRCS src/*.c src/*.cpp + /home/eris/gamedev/study/OpenGL_Superbible/work/Src/GLTools/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/* + +) # The directories that contain the libraries we will be linking against. # This must come before the ADD_EXECUTABLE directive. @@ -145,6 +151,8 @@ # This must come before the ADD_EXECUTABLE directive. include_directories ( ${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 ) # Define the executable program file we are creating. We must list all of @@ -155,6 +163,9 @@ ${HDRS} ) else () + # I should NOT have to define this symbol explictly. Why do I? + add_definitions(-Dlinux) + add_executable (${App_Name} ${SRCS} ${HDRS} @@ -400,14 +411,14 @@ ################################################################################ -option(Option_OpenGL_Dev "Build an OpenGL Application." OFF) +option(Option_OpenGL_Dev "Build an OpenGL Application." ON) if (Option_OpenGL_Dev) find_package(OpenGL) - if (NOT OPEN_FOUND) + if (NOT OPENGL_FOUND) message (FATAL_ERROR "OpenGL not found!") - endif (NOT OPEN_FOUND) + endif (NOT OPENGL_FOUND) include_directories( ${OPENGL_INCLUDE_DIR} @@ -419,6 +430,27 @@ ${TARGET_LINK_LIBRARIES} ) + option(Option_GLUT_Dev "Build a GLUT Application." ON) + + if (Option_GLUT_Dev) + + find_package(GLUT) + if (NOT GLUT_FOUND) + message (FATAL_ERROR "GLUT not found!") + endif() + + include_directories( + ${GLUT_INCLUDE_DIR} + ${INCLUDE_DIRECTORIES} + ) + + target_link_libraries(${App_Name} + ${GLUT_LIBRARIES} + ${TARGET_LINK_LIBRARIES} + ) + + endif() + endif () ################################################################################ @@ -445,3 +477,4 @@ endif () ################################################################################ + diff -r 9c54994a2635 -r c24af3462002 src/solar-system.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/solar-system.cpp Sat Apr 27 13:22:17 2013 -0500 @@ -0,0 +1,564 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include + +GLShaderManager shaderManager; +GLMatrixStack modelViewMatrix; +GLMatrixStack projectionMatrix; +GLFrustum viewFrustum; +GLGeometryTransform transformPipeline; + +GLFrame cameraFrame; + +GLenum polymode = GL_FILL; + +int width = 800; +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 +#define TEX_JUPITER 3 +#define TEX_SUN 4 +#define NUM_TEXTURES 5 +GLuint uiTextures[NUM_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; + } + + +int get_next_file_name(char * filename) + { + 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++; + + 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; + + 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_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); + + 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); + } + + 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 }; + + float RotScale = 100.0; + float SunRotSpeed = 1.0/(25*24) * RotScale; + + float JupiterRotSpeed = 1.0/9 * RotScale; + float JupiterAxialTilt = 3.13; + + 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; + + static CStopWatch rotTimer; + + // Get the light position in eye space + M3DVector4f vLightTransformed; + M3DMatrix44f mCamera; + modelViewMatrix.GetMatrix(mCamera); + m3dTransformVector4(vLightTransformed, vLightPos, mCamera); + + + //////////////////////////////// + // Begin Sun + + float SunRot = rotTimer.GetElapsedSeconds() * SunRotSpeed; + + 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); + + 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; + + 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); + + 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); + + } + jupiterBatch.Draw(); + modelViewMatrix.PopMatrix(); + // End Jupiter + //////////////////////////////// + + + + + ///////////////////////////////// + // Begin Earth/Moon + + // Begin Earth + float EarthRot = rotTimer.GetElapsedSeconds() * EarthRotSpeed; + + 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); + + 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(); + + // Begin Moon + + + // 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); + + float MoonRot = rotTimer.GetElapsedSeconds() * MoonOrbitSpeed * 10; + modelViewMatrix.Rotate(MoonRot, 0.0f, 1.0f, 0.0f); + + modelViewMatrix.Translate(0.5f, 0.0f, 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(); + + + modelViewMatrix.PopMatrix(); + // End Earth/Moon + //////////////////////////////// + + } + +//////////////////////////////////////////////////////////////////////////////// +void RenderScene(void) + { + static CStopWatch rotTimer; + float yRot = - rotTimer.GetElapsedSeconds() * 60.0f; + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + + // Begin Render + modelViewMatrix.PushMatrix(); + + // Begin Camera position + static M3DMatrix44f mCamera; + cameraFrame.GetCameraMatrix(mCamera); + modelViewMatrix.MultMatrix(mCamera); + // End Camera position + + DrawSolarSystem(yRot); + + // End Render + modelViewMatrix.PopMatrix(); + + glutSwapBuffers(); + glutPostRedisplay(); + } + +//////////////////////////////////////////////////////////////////////////////// +void SetupRC() + { + 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); + + 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_JUPITER]); + LoadTGATexture("../textures/jupiter.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); + + 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); + } + +//////////////////////////////////////////////////////////////////////////////// +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); + } + +//////////////////////////////////////////////////////////////////////////////// +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; + + 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; + } + + 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); + } + + if (changed) + { + glutPostRedisplay(); + } + } + +//////////////////////////////////////////////////////////////////////////////// +void MouseMotionFunc (int x, int y) + { + static float angular = (float) m3dDegToRad(0.5f); + static int xx = -1; + static int yy = -1; + + 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); + // } + + 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"); + + 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; + } + + // 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"); + + if (SwapInterval) + SwapInterval(1); + + SetupRC(); + glutMainLoop(); + ShutDownRC(); + + return 0; + } + + + + + diff -r 9c54994a2635 -r c24af3462002 textures/Marble.tga Binary file textures/Marble.tga has changed diff -r 9c54994a2635 -r c24af3462002 textures/Marslike.tga Binary file textures/Marslike.tga has changed diff -r 9c54994a2635 -r c24af3462002 textures/MoonLike.tga Binary file textures/MoonLike.tga has changed diff -r 9c54994a2635 -r c24af3462002 textures/earth.tga Binary file textures/earth.tga has changed diff -r 9c54994a2635 -r c24af3462002 textures/jupiter.png Binary file textures/jupiter.png has changed diff -r 9c54994a2635 -r c24af3462002 textures/jupiter.tga Binary file textures/jupiter.tga has changed diff -r 9c54994a2635 -r c24af3462002 textures/jupiter.xcf Binary file textures/jupiter.xcf has changed diff -r 9c54994a2635 -r c24af3462002 textures/marble.tga Binary file textures/marble.tga has changed diff -r 9c54994a2635 -r c24af3462002 textures/marslike.tga Binary file textures/marslike.tga has changed diff -r 9c54994a2635 -r c24af3462002 textures/moon.tga Binary file textures/moon.tga has changed diff -r 9c54994a2635 -r c24af3462002 textures/moonlike.tga Binary file textures/moonlike.tga has changed diff -r 9c54994a2635 -r c24af3462002 textures/sun.tga Binary file textures/sun.tga has changed