diff src/solar-system.cpp @ 2:c24af3462002

initial commit
author Eris Caffee <discordia@eldalin.com>
date Sat, 27 Apr 2013 13:22:17 -0500
parents
children 4f8b47ac2715 7ae4ee5c27f8
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/solar-system.cpp	Sat Apr 27 13:22:17 2013 -0500
     1.3 @@ -0,0 +1,564 @@
     1.4 +#include <GLTools.h>
     1.5 +#include <GLShaderManager.h>
     1.6 +#include <GLFrustum.h>
     1.7 +#include <GLBatch.h>
     1.8 +#include <GLFrame.h>
     1.9 +#include <GLMatrixStack.h>
    1.10 +#include <GLGeometryTransform.h>
    1.11 +#include <StopWatch.h>
    1.12 +
    1.13 +#include <math.h>
    1.14 +#include <ctype.h>
    1.15 +#include <stdlib.h>
    1.16 +#include <string.h>
    1.17 +
    1.18 +#include <dirent.h>
    1.19 +
    1.20 +#include <GL/glut.h>
    1.21 +#include <GL/glx.h>
    1.22 +
    1.23 +GLShaderManager		shaderManager;
    1.24 +GLMatrixStack		modelViewMatrix;
    1.25 +GLMatrixStack		projectionMatrix;
    1.26 +GLFrustum		viewFrustum;
    1.27 +GLGeometryTransform	transformPipeline;
    1.28 +
    1.29 +GLFrame			cameraFrame;
    1.30 +
    1.31 +GLenum			polymode = GL_FILL;
    1.32 +
    1.33 +int			width = 800;
    1.34 +int			height = 600;
    1.35 +int			fullscreen = 0;
    1.36 +
    1.37 +#define NUM_SPHERES 50
    1.38 +GLFrame spheres[NUM_SPHERES];
    1.39 +
    1.40 +GLTriangleBatch jupiterBatch;
    1.41 +GLTriangleBatch sphereBatch;
    1.42 +GLTriangleBatch earthBatch;
    1.43 +GLTriangleBatch moonBatch;
    1.44 +GLTriangleBatch sunBatch;
    1.45 +GLBatch floorBatch;
    1.46 +
    1.47 +#define TEX_EARTH 1
    1.48 +#define TEX_MOON 2
    1.49 +#define TEX_JUPITER 3
    1.50 +#define TEX_SUN 4
    1.51 +#define NUM_TEXTURES 5
    1.52 +GLuint uiTextures[NUM_TEXTURES];
    1.53 +
    1.54 +////////////////////////////////////////////////////////////////////////////////
    1.55 +#define SCREENSHOT_FILENAME_BASE "screenshot-"
    1.56 +#define SCREENSHOT_FILENAME_BASELEN 11
    1.57 +#define SCREENSHOT_FILENAME_EXT ".tga"
    1.58 +#define SCREENSHOT_FILENAME_EXTLEN 4
    1.59 +
    1.60 +int scandir_filter(const struct dirent * d)
    1.61 +   {
    1.62 +   if (memcmp(d->d_name, SCREENSHOT_FILENAME_BASE,
    1.63 +       SCREENSHOT_FILENAME_BASELEN) != 0) return 0;
    1.64 +   if (memcmp(d->d_name+SCREENSHOT_FILENAME_BASELEN+3, 
    1.65 +       SCREENSHOT_FILENAME_EXT, SCREENSHOT_FILENAME_EXTLEN) != 0) 
    1.66 +      return 0;
    1.67 +   if (isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN]) 
    1.68 +       && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+1]) 
    1.69 +       && isdigit(d->d_name[SCREENSHOT_FILENAME_BASELEN+2])) 
    1.70 +      return 1;
    1.71 +   return 0;
    1.72 +   }
    1.73 +
    1.74 +
    1.75 +int get_next_file_name(char * filename)
    1.76 +   {
    1.77 +   static int i = 0;
    1.78 +
    1.79 +   if (i == 0)
    1.80 +      {
    1.81 +      char pattern[SCREENSHOT_FILENAME_BASELEN+3+SCREENSHOT_FILENAME_EXTLEN];
    1.82 +      struct dirent ** file_list;
    1.83 +      int num_files = scandir(".", &file_list, scandir_filter, alphasort);
    1.84 +      if (num_files != 0)
    1.85 +      sprintf(pattern, "%s%%03d%s", SCREENSHOT_FILENAME_BASE,
    1.86 +              SCREENSHOT_FILENAME_EXT);
    1.87 +	 sscanf(file_list[num_files-1]->d_name, pattern, &i);
    1.88 +      }
    1.89 +   i++;
    1.90 +
    1.91 +   sprintf(filename, "%s%03d%s", SCREENSHOT_FILENAME_BASE, i, 
    1.92 +	   SCREENSHOT_FILENAME_EXT);
    1.93 +   return i;
    1.94 +   }
    1.95 +
    1.96 +////////////////////////////////////////////////////////////////////////////////
    1.97 +bool LoadTGATexture(const char * szFileName, GLenum minFilter, 
    1.98 +		    GLenum magFilter, GLenum wrapMode)
    1.99 +   {
   1.100 +   GLbyte * pBits;
   1.101 +   int nWidth, nHeight, nComponents;
   1.102 +   GLenum eFormat;
   1.103 +
   1.104 +   pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat);
   1.105 +   if (pBits == NULL)
   1.106 +       {
   1.107 +       fprintf(stderr, "Failed to load %s\n", szFileName);
   1.108 +       exit(EXIT_FAILURE);
   1.109 +       }
   1.110 +
   1.111 +   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
   1.112 +   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);
   1.113 +
   1.114 +   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
   1.115 +   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
   1.116 +
   1.117 +   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   1.118 +   glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0,
   1.119 +		eFormat, GL_UNSIGNED_BYTE, pBits);
   1.120 +
   1.121 +   free(pBits);
   1.122 +
   1.123 +   if (minFilter == GL_LINEAR_MIPMAP_LINEAR ||
   1.124 +       minFilter == GL_LINEAR_MIPMAP_NEAREST ||
   1.125 +       minFilter == GL_NEAREST_MIPMAP_LINEAR ||
   1.126 +       minFilter == GL_NEAREST_MIPMAP_NEAREST)
   1.127 +      {
   1.128 +      glGenerateMipmap(GL_TEXTURE_2D);
   1.129 +      }
   1.130 +
   1.131 +   return true;
   1.132 +   }
   1.133 +
   1.134 +////////////////////////////////////////////////////////////////////////////////
   1.135 +void DrawSolarSystem(GLfloat yRot)
   1.136 +   {
   1.137 +   static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f };
   1.138 +   static GLfloat vLightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
   1.139 +   static M3DVector3f vSunPos = { 0.0f, 0.0f, 0.0f };
   1.140 +   static M3DVector3f vEarthPos = { 5.0f, 0.0f, 0.0f };
   1.141 +   static M3DVector3f vJupiterPos = { 10.0f, 0.0f, 0.0f };
   1.142 +
   1.143 +   float RotScale = 100.0;
   1.144 +   float SunRotSpeed = 1.0/(25*24) * RotScale;
   1.145 +
   1.146 +   float JupiterRotSpeed = 1.0/9 * RotScale;
   1.147 +   float JupiterAxialTilt = 3.13;
   1.148 +
   1.149 +   float EarthRotSpeed = 1.0/24 * RotScale;
   1.150 +   float EarthAxialTilt = 23.5;
   1.151 +
   1.152 +   float MoonRotSpeed = 1.0/29.5 * RotScale;
   1.153 +   float MoonAxialTilt = 6.7;
   1.154 +   float MoonOrbitSpeed = 1.0/29.5 * RotScale;
   1.155 +   float MoonOrbitTilt = 5.145;
   1.156 +
   1.157 +   static CStopWatch rotTimer;
   1.158 +
   1.159 +   // Get the light position in eye space
   1.160 +   M3DVector4f vLightTransformed;
   1.161 +   M3DMatrix44f mCamera;
   1.162 +   modelViewMatrix.GetMatrix(mCamera);
   1.163 +   m3dTransformVector4(vLightTransformed, vLightPos, mCamera);
   1.164 +
   1.165 +
   1.166 +   ////////////////////////////////
   1.167 +   // Begin Sun
   1.168 +
   1.169 +   float SunRot = rotTimer.GetElapsedSeconds() * SunRotSpeed;
   1.170 +
   1.171 +   modelViewMatrix.PushMatrix();
   1.172 +
   1.173 +   modelViewMatrix.Translatev(vSunPos);
   1.174 +   // North is up!
   1.175 +   modelViewMatrix.Rotate(-90.0f, 1.0f, 0.0f, 0.0f);
   1.176 +   // Rotate on axis
   1.177 +   modelViewMatrix.Rotate(SunRot, 0.0f, 0.0f, 1.0f);
   1.178 +
   1.179 +   glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]);
   1.180 +   if (polymode == GL_FILL)
   1.181 +      {
   1.182 +      shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE,
   1.183 +   				   transformPipeline.GetModelViewProjectionMatrix(),
   1.184 +   				   0);
   1.185 +      }
   1.186 +   else
   1.187 +      {
   1.188 +      shaderManager.UseStockShader(GLT_SHADER_FLAT,
   1.189 +				   transformPipeline.GetModelViewProjectionMatrix(),
   1.190 +				   vWhite);
   1.191 +      }
   1.192 +   sunBatch.Draw();
   1.193 +   modelViewMatrix.PopMatrix();
   1.194 +   // End Sun
   1.195 +   /////////////////////////////////
   1.196 +
   1.197 +
   1.198 +   ////////////////////////////////
   1.199 +   // Jupiter
   1.200 +   float JupiterRot = rotTimer.GetElapsedSeconds() * JupiterRotSpeed;
   1.201 +
   1.202 +   modelViewMatrix.PushMatrix();
   1.203 +   modelViewMatrix.Translatev(vJupiterPos);
   1.204 +   // North is up!
   1.205 +   modelViewMatrix.Rotate(-90.0f - JupiterAxialTilt, 1.0f, 0.0f, 0.0f);
   1.206 +   // Rotate on axis
   1.207 +   modelViewMatrix.Rotate(JupiterRot, 0.0f, 0.0f, 1.0f);
   1.208 +
   1.209 +   glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]);
   1.210 +   if (polymode == GL_FILL)
   1.211 +      {
   1.212 +      // shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE,
   1.213 +      // 	  transformPipeline.GetModelViewProjectionMatrix(),
   1.214 +      // 	  0);
   1.215 +      shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF,
   1.216 +       				   modelViewMatrix.GetMatrix(),
   1.217 +       				   transformPipeline.GetProjectionMatrix(),
   1.218 +       				   vLightTransformed,
   1.219 +       				   vWhite,
   1.220 +       				   0);
   1.221 +      }
   1.222 +   else
   1.223 +      {
   1.224 +      shaderManager.UseStockShader(GLT_SHADER_FLAT,
   1.225 +   				   transformPipeline.GetModelViewProjectionMatrix(),
   1.226 +   				   vWhite);
   1.227 +
   1.228 +      }
   1.229 +   jupiterBatch.Draw();
   1.230 +   modelViewMatrix.PopMatrix();
   1.231 +   // End Jupiter
   1.232 +   ////////////////////////////////
   1.233 +
   1.234 +
   1.235 +
   1.236 +
   1.237 +   /////////////////////////////////
   1.238 +   // Begin Earth/Moon
   1.239 +
   1.240 +   // Begin Earth
   1.241 +   float EarthRot = rotTimer.GetElapsedSeconds() * EarthRotSpeed;
   1.242 +
   1.243 +   modelViewMatrix.PushMatrix();
   1.244 +   modelViewMatrix.Translatev(vEarthPos);
   1.245 +   modelViewMatrix.PushMatrix(); // Save unrotated matrix for when we do the Moon
   1.246 +
   1.247 +   // NOrth is up!
   1.248 +   modelViewMatrix.Rotate(-90.0f - EarthAxialTilt, 1.0f, 0.0f, 0.0f);
   1.249 +   // Rotate on the axis
   1.250 +   modelViewMatrix.Rotate(EarthRot, 0.0f, 0.0f, 1.0f);
   1.251 +
   1.252 +   if (polymode == GL_FILL)
   1.253 +      {
   1.254 +      glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]);
   1.255 +      shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF,
   1.256 +   				   modelViewMatrix.GetMatrix(),
   1.257 +   				   transformPipeline.GetProjectionMatrix(),
   1.258 +   				   vLightTransformed,
   1.259 +   				   vWhite,
   1.260 +   				   0);
   1.261 +      }
   1.262 +   else
   1.263 +      {
   1.264 +      shaderManager.UseStockShader(GLT_SHADER_FLAT,
   1.265 +   				   transformPipeline.GetModelViewProjectionMatrix(),
   1.266 +   				   vWhite);
   1.267 +
   1.268 +      }
   1.269 +   earthBatch.Draw();
   1.270 +   modelViewMatrix.PopMatrix();
   1.271 +
   1.272 +   // Begin Moon
   1.273 + 
   1.274 +
   1.275 +   // orbit the Earth
   1.276 +   modelViewMatrix.Rotate(MoonOrbitTilt, 0.0f, 0.0f, 1.0f);
   1.277 +   // NOrth is up!
   1.278 +   modelViewMatrix.Rotate(-90.0f - MoonAxialTilt, 0.0f, 1.0f, 0.0f);
   1.279 +
   1.280 +   float MoonRot = rotTimer.GetElapsedSeconds() * MoonOrbitSpeed * 10;
   1.281 +   modelViewMatrix.Rotate(MoonRot, 0.0f, 1.0f, 0.0f);
   1.282 +
   1.283 +   modelViewMatrix.Translate(0.5f, 0.0f, 0.0f);
   1.284 +
   1.285 +
   1.286 +   glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]);
   1.287 +   if (polymode == GL_FILL)
   1.288 +      {
   1.289 +      shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF,
   1.290 +   				   modelViewMatrix.GetMatrix(),
   1.291 +   				   transformPipeline.GetProjectionMatrix(),
   1.292 +   				   vLightTransformed,
   1.293 +   				   vWhite,
   1.294 +   				   0);
   1.295 +      }
   1.296 +   else 
   1.297 +      {
   1.298 +      shaderManager.UseStockShader(GLT_SHADER_FLAT,
   1.299 +   				   transformPipeline.GetModelViewProjectionMatrix(),
   1.300 +   				   vWhite);
   1.301 +      }
   1.302 +   moonBatch.Draw();
   1.303 +   modelViewMatrix.PopMatrix();
   1.304 +
   1.305 +
   1.306 +   modelViewMatrix.PopMatrix();
   1.307 +   // End Earth/Moon
   1.308 +   ////////////////////////////////
   1.309 +   
   1.310 +   }
   1.311 +
   1.312 +////////////////////////////////////////////////////////////////////////////////
   1.313 +void RenderScene(void)
   1.314 +   {
   1.315 +   static CStopWatch rotTimer;
   1.316 +   float yRot = - rotTimer.GetElapsedSeconds() * 60.0f;
   1.317 +
   1.318 +   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
   1.319 +
   1.320 +   // Begin Render
   1.321 +   modelViewMatrix.PushMatrix();
   1.322 +
   1.323 +   // Begin Camera position
   1.324 +   static M3DMatrix44f mCamera;
   1.325 +   cameraFrame.GetCameraMatrix(mCamera);
   1.326 +   modelViewMatrix.MultMatrix(mCamera);
   1.327 +   // End Camera position
   1.328 +
   1.329 +   DrawSolarSystem(yRot);
   1.330 +
   1.331 +   // End Render
   1.332 +   modelViewMatrix.PopMatrix();
   1.333 +
   1.334 +   glutSwapBuffers();
   1.335 +   glutPostRedisplay();
   1.336 +   }
   1.337 +
   1.338 +////////////////////////////////////////////////////////////////////////////////
   1.339 +void SetupRC()
   1.340 +   {
   1.341 +   shaderManager.InitializeStockShaders();
   1.342 +   glEnable(GL_DEPTH_TEST);
   1.343 +   glEnable(GL_CULL_FACE);
   1.344 +   glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
   1.345 +
   1.346 +   gltMakeSphere(jupiterBatch, 0.5f, 128, 128 );
   1.347 +   gltMakeSphere(earthBatch, 0.2f, 26, 26);
   1.348 +   gltMakeSphere(moonBatch, 0.1f, 26, 26);
   1.349 +   gltMakeSphere(sunBatch, 1.0f, 26, 26);
   1.350 +
   1.351 +   glGenTextures(NUM_TEXTURES, uiTextures);
   1.352 +
   1.353 +   glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_EARTH]);
   1.354 +   LoadTGATexture("../textures/earth.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
   1.355 +
   1.356 +   glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_MOON]);
   1.357 +   LoadTGATexture("../textures/moon.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
   1.358 +
   1.359 +   glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_JUPITER]);
   1.360 +   LoadTGATexture("../textures/jupiter.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
   1.361 +
   1.362 +   glBindTexture(GL_TEXTURE_2D, uiTextures[TEX_SUN]);
   1.363 +   LoadTGATexture("../textures/sun.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
   1.364 +
   1.365 +   static M3DMatrix44f mCamera;
   1.366 +   cameraFrame.GetCameraMatrix(mCamera);
   1.367 +   cameraFrame.MoveForward(-10.0);
   1.368 +//   cameraFrame.RotateWorld(15.0, 0.0f, 1.0f, 0.0f);
   1.369 +   }
   1.370 +
   1.371 +////////////////////////////////////////////////////////////////////////////////
   1.372 +void ShutDownRC(void)
   1.373 +   {
   1.374 +   glDeleteTextures(NUM_TEXTURES, uiTextures);
   1.375 +   }
   1.376 +
   1.377 +////////////////////////////////////////////////////////////////////////////////
   1.378 +void ChangeSize(int nWidth, int nHeight)
   1.379 +   {
   1.380 +   if (nHeight == 0)
   1.381 +      {
   1.382 +      nHeight = 1;
   1.383 +      }
   1.384 +   glViewport(0,0,nWidth, nHeight);
   1.385 +   viewFrustum.SetPerspective(35.0f, float(nWidth)/float(nHeight), 1.0f, 100.0f);
   1.386 +   projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
   1.387 +   transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
   1.388 +   }
   1.389 +
   1.390 +////////////////////////////////////////////////////////////////////////////////
   1.391 +void SpecialKeys(int key, int x, int y)
   1.392 +   {
   1.393 +   }
   1.394 +
   1.395 +////////////////////////////////////////////////////////////////////////////////
   1.396 +void KeyboardFunc(unsigned char key, int x, int y)
   1.397 +   {
   1.398 +   static int changed;
   1.399 +   static float linear = 0.1f;
   1.400 +
   1.401 +   changed = 0;
   1.402 +   
   1.403 +   if ('w' == key)
   1.404 +      {
   1.405 +      cameraFrame.MoveForward(linear);
   1.406 +      changed = 1;
   1.407 +      }
   1.408 +   else if ('W' == key)
   1.409 +      {
   1.410 +      cameraFrame.MoveForward(10*linear);
   1.411 +      changed = 1;
   1.412 +      }
   1.413 +   else if ('s' == key)
   1.414 +      {
   1.415 +      cameraFrame.MoveForward(-linear);
   1.416 +      changed = 1;
   1.417 +      }
   1.418 +   else if ('S' == key)
   1.419 +      {
   1.420 +      cameraFrame.MoveForward(-10*linear);
   1.421 +      changed = 1;
   1.422 +      }
   1.423 +   else if ('a' == key)
   1.424 +      {
   1.425 +      cameraFrame.MoveRight(linear);
   1.426 +      changed = 1;
   1.427 +      }
   1.428 +   else if ('A' == key)
   1.429 +      {
   1.430 +      cameraFrame.MoveRight(10*linear);
   1.431 +      changed = 1;
   1.432 +      }
   1.433 +   else if ('d' == key)
   1.434 +      {
   1.435 +      cameraFrame.MoveRight(-linear);
   1.436 +      changed = 1;
   1.437 +      }
   1.438 +   else if ('D' == key)
   1.439 +      {
   1.440 +      cameraFrame.MoveRight(-10*linear);
   1.441 +      changed = 1;
   1.442 +      }
   1.443 +
   1.444 +   else if ('q' == key)
   1.445 +      {
   1.446 +      exit(0);
   1.447 +      }
   1.448 +   else if ('f' == key)
   1.449 +      {
   1.450 +      if (fullscreen)
   1.451 +	 {
   1.452 +	 glutReshapeWindow(width, height);
   1.453 +	 fullscreen = 0;
   1.454 +	 }
   1.455 +      else
   1.456 +	 {
   1.457 + 	 width = glutGet(GLUT_WINDOW_WIDTH);
   1.458 +	 height = glutGet(GLUT_WINDOW_HEIGHT);
   1.459 +	 glutFullScreen();
   1.460 +	 fullscreen = 1;
   1.461 +	 } 
   1.462 +      }
   1.463 +   else if ('o' == key)
   1.464 +      {
   1.465 +      // 'o' for 'outline' - toggle wireframe rendering 
   1.466 +      polymode = (polymode == GL_FILL) ? GL_LINE : GL_FILL;
   1.467 +      glPolygonMode(GL_FRONT_AND_BACK, polymode);
   1.468 +      }
   1.469 +   else if ('p' == key)
   1.470 +      {
   1.471 +      // 'p' for 'print screen' - save a screenshot
   1.472 +      char filename[20];
   1.473 +      get_next_file_name(filename);
   1.474 +      
   1.475 +      gltGrabScreenTGA(filename);
   1.476 +      }
   1.477 +
   1.478 +   if (changed)
   1.479 +      {
   1.480 +      glutPostRedisplay();
   1.481 +      }
   1.482 +   }
   1.483 +
   1.484 +////////////////////////////////////////////////////////////////////////////////
   1.485 +void MouseMotionFunc (int x, int y)
   1.486 +   {
   1.487 +   static float angular = (float) m3dDegToRad(0.5f);
   1.488 +   static int xx = -1;
   1.489 +   static int yy = -1;
   1.490 +
   1.491 +   if (-1 == xx)
   1.492 +      {
   1.493 +      xx = x;
   1.494 +      yy = y;
   1.495 +      }
   1.496 +
   1.497 +   if ((0 == x) || (x < xx))
   1.498 +      {
   1.499 +      cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f);
   1.500 +      glutPostRedisplay();
   1.501 +      }
   1.502 +   else if ((x == glutGet(GLUT_WINDOW_WIDTH) -1) || (x > xx))
   1.503 +      {
   1.504 +      cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f);
   1.505 +      glutPostRedisplay();
   1.506 +      }
   1.507 +   // Hmm.  Need to transform normal vector, don't I?
   1.508 +   // if ((0 == y) || (y < yy))
   1.509 +   //    {
   1.510 +   //    cameraFrame.RotateWorld(angular, 1.0f, 0.0f, 0.0f);
   1.511 +   //    }
   1.512 +   // else if ((y == glutGet(GLUT_WINDOW_HEIGHT) -1) || (y > yy))
   1.513 +   //    {
   1.514 +   //    cameraFrame.RotateWorld(-angular, 1.0f, 0.0f, 0.0f);
   1.515 +   //    }
   1.516 +
   1.517 +   xx = x; 
   1.518 +   yy = y;
   1.519 +   }
   1.520 +
   1.521 +////////////////////////////////////////////////////////////////////////////////
   1.522 +int main (int argc, char * argv[])
   1.523 +   {
   1.524 +   gltSetWorkingDirectory(argv[0]);
   1.525 +   glutInit(&argc, argv);
   1.526 +   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
   1.527 +   glutInitWindowSize(800, 600);
   1.528 +   glutCreateWindow("OpenGL SphereWorld");
   1.529 +
   1.530 +   glutReshapeFunc(ChangeSize);
   1.531 +   glutDisplayFunc(RenderScene);
   1.532 +   glutSpecialFunc(SpecialKeys);
   1.533 +   glutKeyboardFunc(KeyboardFunc);
   1.534 +   glutMotionFunc(MouseMotionFunc);
   1.535 +   glutPassiveMotionFunc(MouseMotionFunc);
   1.536 +   glutSetCursor(GLUT_CURSOR_NONE);
   1.537 +
   1.538 +   GLenum err = glewInit();
   1.539 +   if (GLEW_OK != err)
   1.540 +      {
   1.541 +      fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
   1.542 +      return 1;
   1.543 +      }
   1.544 +
   1.545 +   // This enabled vertical sync on Linux
   1.546 +   // For full generality I should use Glew and check what OS I'm on.
   1.547 +   // Note that the ATI Catalyst driver exports the WGL_EXT_swap_control
   1.548 +   // extension name instead of SGI_swap_control as it should.
   1.549 +   // but nonetheless the actual function provided is glXSwapIntervalSGI
   1.550 +
   1.551 +   PFNGLXSWAPINTERVALSGIPROC SwapInterval;
   1.552 +   SwapInterval = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalSGI");
   1.553 +
   1.554 +   if (SwapInterval)
   1.555 +      SwapInterval(1);
   1.556 +
   1.557 +   SetupRC();
   1.558 +   glutMainLoop();
   1.559 +   ShutDownRC();
   1.560 +
   1.561 +   return 0;
   1.562 +   }
   1.563 +
   1.564 +
   1.565 +
   1.566 +
   1.567 +