Parallel Programming - Improve camera UI
authorSam Moore <sam@daedalus.(none)>
Mon, 17 Sep 2012 11:55:36 +0000 (19:55 +0800)
committerSam Moore <sam@daedalus.(none)>
Mon, 17 Sep 2012 11:55:36 +0000 (19:55 +0800)
Can now fly around (sort of) like Descent games.
Except glut can only handle one key press at a time... pretty terrible
Maybe I'll change it to SDL... But I think I'm getting side tracked

course/semester2/pprog/assignment1/single-thread/graphics.c
course/semester2/pprog/assignment1/single-thread/graphics.h

index 6fdb581..32ca080 100644 (file)
@@ -22,6 +22,9 @@ float look[3];
 int windowWidth, windowHeight, upY;
 double scale = 1.0;
 
+#ifdef FLYING_CAMERA
+Camera eye;
+#endif //FLYING_CAMERA
 
 /**
  * @function Graphics_Run
@@ -95,6 +98,12 @@ void Graphics_Run(int argc, char ** argv)
        look[2] = 0;
        gluPerspective(VIEW_ANGLE, (double)WIDTH/(double)HEIGHT, WORLD_NEAR, WORLD_FAR);  
 
+       #ifdef FLYING_CAMERA
+       eye.x[0] = 1; eye.x[1] = 0; eye.x[2] = 0;
+       eye.y[0] = 0; eye.y[1] = 1; eye.y[2] = 0;
+       eye.z[0] = 0; eye.z[1] = 0; eye.z[2] = 1;
+       #endif //FLYING_CAMERA
+
        glutMainLoop();   
 }
 
@@ -124,8 +133,15 @@ void Graphics_Display()
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
+
+       #ifdef FLYING_CAMERA
+       gluLookAt(eye.p[0], eye.p[1], eye.p[2], 
+               eye.p[0] + eye.x[0], eye.p[1] + eye.x[1], eye.p[2] + eye.x[2],
+               eye.z[0], eye.z[1], eye.z[2]);
+       #else
        gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
                eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
+       #endif //FLYING_CAMERA
 
        BeforeDraw(); // Stuff to do before graphics is allowed to draw
                        // Single-thread - perform a computation step, obviously! It's not done anywhere else
@@ -164,6 +180,149 @@ void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
                return;
        }
 
+       #ifdef FLYING_CAMERA
+       switch (theKey)
+       {
+               case 'W':
+               case 'w':
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                               eye.p[i] += eye.x[i];
+                       break;
+               case 'S':
+               case 's':
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                               eye.p[i] -= eye.x[i];
+                       break;
+               case 'A':
+               case 'a':
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                               eye.p[i] += eye.y[i];
+                       break;
+               case 'D':
+               case 'd':
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                               eye.p[i] -= eye.y[i];
+                       break;
+               case 'Z':
+               case 'z':
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                               eye.p[i] += eye.z[i];
+                       break;
+               case 'C':
+               case 'c':
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                               eye.p[i] -= eye.z[i];
+                       break;
+
+               case 'I':
+               case 'i':
+               {
+                       float theta = M_PI/80.0;
+                       Camera old = eye;
+
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                       {
+                               eye.z[i] = old.z[i] * cos(theta) + old.x[i] * sin(theta);
+                               eye.x[i] = old.x[i] * cos(theta) - old.z[i] * sin(theta);
+                       }
+                       break;
+               }
+               case 'K':
+               case 'k':
+               {
+                       float theta = -M_PI/80.0;
+                       Camera old = eye;
+
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                       {
+                               eye.z[i] = old.z[i] * cos(theta) + old.x[i] * sin(theta);
+                               eye.x[i] = old.x[i] * cos(theta) - old.z[i] * sin(theta);
+                       }
+                       break;
+               }
+               
+               case 'J':
+               case 'j':
+               {
+                       float theta = -M_PI/80.0;
+                       Camera old = eye;
+
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                       {
+                               eye.y[i] = old.y[i] * cos(theta) + old.x[i] * sin(theta);
+                               eye.x[i] = old.x[i] * cos(theta) - old.y[i] * sin(theta);
+                       }
+                       break;
+               }
+
+               case 'L':
+               case 'l':
+               {
+                       float theta = M_PI/80.0;
+                       Camera old = eye;
+
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                       {
+                               eye.y[i] = old.y[i] * cos(theta) + old.x[i] * sin(theta);
+                               eye.x[i] = old.x[i] * cos(theta) - old.y[i] * sin(theta);
+                       }
+                       break;
+               }
+
+               case 'Q':
+               case 'q':
+               {
+                       float theta = M_PI/80.0;
+                       Camera old = eye;
+
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                       {
+                               eye.z[i] = old.z[i] * cos(theta) + old.y[i] * sin(theta);
+                               eye.y[i] = old.y[i] * cos(theta) - old.z[i] * sin(theta);
+                       }
+                       break;
+               }
+               case 'E':
+               case 'e':
+               {
+                       float theta = -M_PI/80.0;
+                       Camera old = eye;
+
+                       for (unsigned i = 0; i < DIMENSIONS; ++i)
+                       {
+                               eye.z[i] = old.z[i] * cos(theta) + old.y[i] * sin(theta);
+                               eye.y[i] = old.y[i] * cos(theta) - old.z[i] * sin(theta);
+                       }
+                       break;
+               }       
+       }
+
+       /*
+       float x = 0; float y = 0; float z = 0;
+       for (unsigned i = 0; i < DIMENSIONS; ++i)
+       {
+               x += eye.x[i] * eye.x[i];
+               y += eye.y[i] * eye.y[i];
+               z += eye.z[i] * eye.z[i];
+       }
+       for (unsigned i = 0; i < DIMENSIONS; ++i)
+       {
+               eye.x[i] /= sqrt(x);
+               eye.y[i] /= sqrt(y);
+               eye.z[i] /= sqrt(z);
+       }
+       */
+       system("clear");
+       printf("Camera status:\n");
+       printf("Position: %f %f %f\n", eye.p[0], eye.p[1], eye.p[2]);
+       printf("x: %f %f %f (%f)\n", eye.x[0], eye.x[1], eye.x[2], sqrt(eye.x[0]*eye.x[0] + eye.x[1]*eye.x[1] + eye.x[2]*eye.x[2]));
+       printf("y: %f %f %f (%f)\n", eye.y[0], eye.y[1], eye.y[2], sqrt(eye.y[0]*eye.y[0] + eye.y[1]*eye.y[1] + eye.y[2]*eye.y[2]));
+       printf("z: %f %f %f (%f)\n", eye.z[0], eye.z[1], eye.z[2], sqrt(eye.z[0]*eye.z[0] + eye.z[1]*eye.z[1] + eye.z[2]*eye.z[2]));
+
+       #else
+       
+       
+
                if (theKey == 'i' || theKey == 'I') {
                        eyePhi -= M_PI / 20;
                if (eyePhi == 0)
@@ -193,6 +352,7 @@ void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
                }
                if (sin(eyePhi) > 0) upY = 1;
                else upY = 1;
+       #endif //FLYING_CAMERA
 }
 
 /**
index c2b99f3..97ccf47 100644 (file)
@@ -14,7 +14,7 @@
 #include "nbody.h"
 
 #define WIDTH 800
-#define HEIGHT 800
+#define HEIGHT 640
 #define POINT_SIZE 1
 #define POSITION_X 112
 #define POSITION_Y 20
@@ -24,7 +24,7 @@
 #define WORLD_TOP 10000
 #define VIEW_ANGLE 45
 #define RHO 100
-#define WORLD_NEAR 0.1
+#define WORLD_NEAR 0.0001
 #define WORLD_FAR 1000000
 #define BALL_SIZE 0.5
 #define REFRESH_RATE 0.001
@@ -36,6 +36,20 @@ void Graphics_Keyboard(unsigned char key, int mouse_x, int mouse_y);
 void Graphics_Reshape(int width, int height);
 
 
+#define FLYING_CAMERA
+
+#ifdef FLYING_CAMERA
+typedef struct
+{
+       float p[DIMENSIONS]; // Translation position of the camera
+       
+       float x[DIMENSIONS];
+       float y[DIMENSIONS];
+       float z[DIMENSIONS];
+       
+} Camera;
+#endif //FLYING_CAMERA
+
 #endif //_GRAPHICS_H
 
 //EOF

UCC git Repository :: git.ucc.asn.au