(no commit message)
authorAsh Tyndall <[email protected]>
Sat, 22 Oct 2011 06:51:59 +0000 (14:51 +0800)
committerAsh Tyndall <[email protected]>
Sat, 22 Oct 2011 06:51:59 +0000 (14:51 +0800)
globals.c
globals.h
helper.c
helper.h
scene.c
scene.h
types.h

index cfc021f..8bd2cdf 100644 (file)
--- a/globals.c
+++ b/globals.c
@@ -40,6 +40,30 @@ int curObject = -1;                 // The scene object that is currently select
 int buttonSelected = -1;                 // Either GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or -1 (no button)
 int manipulateState = STATE_CAMERA_ROTATE_MOVE; // See manipulateStates enum
 
 int buttonSelected = -1;                 // Either GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or -1 (no button)
 int manipulateState = STATE_CAMERA_ROTATE_MOVE; // See manipulateStates enum
 
+/* Light object arrays and default values */
+LightObject lightObjs[LIGHTS];
+
+GLfloat defaultPosition[] = {0.0, 0.0, 0.0, 0.0};
+
+GLfloat defaultAmbient[] = {0.0, 1.0, 0.0, 1.0};
+GLfloat defaultDiffuse[] = {0.0, 1.0, 0.0, 1.0};
+GLfloat defaultSpecular[] = {1.0, 0.0, 0.0, 1.0};
+GLfloat defaultDirection[] = {0.0, 0.0, 0.0};
+
+GLfloat defaultCutoff = 0;
+GLfloat defaultExponent = 0;
+
+/* Material types */
+GLfloat materialAmbient[] = {0.2, 0.2, 0.2, 1.0};
+GLfloat materialDiffuse[] = {1.0, 0.8, 0.0, 1.0};
+GLfloat materialSpecular[] = {1.0, 1.0, 1.0, 1.0};
+
+/* Light parameters */
+GLfloat lightShine = 100.0;
+GLfloat lightGlobalModel[] = {0.2,0.2,0.2,1};
+GLfloat lightGlobalEmission[] = {0.0, 0.3, 0.3, 1.0};
+GLfloat lightBallSize = 0.2;
+
 // Directories containing models
 char *dirDefault1 = "models-textures";
 char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures";
 // Directories containing models
 char *dirDefault1 = "models-textures";
 char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures";
@@ -59,38 +83,6 @@ GLfloat factor = 1.0;
 /* Length of axis lines */
 GLfloat lineLength = 10;
 
 /* Length of axis lines */
 GLfloat lineLength = 10;
 
-/* Light 0 parameters */
-GLfloat diffuse0[]={1.0, 0.0, 0.0, 1.0};
-GLfloat ambient0[]={1.0, 0.0, 0.0, 1.0};
-GLfloat specular0[]={1.0, 0.0, 0.0, 1.0};
-GLfloat direction0[] = {0.0, 0.0, 0.0};
-
-GLfloat lightPosition0[4];
-
-float lightAngle0 = 0.0, lightHeight0 = 5;
-int lightMoving0 = 0, lightStartX0, lightStartY0;
-
-/* Light 1 parameters */
-GLfloat diffuse1[]={0.0, 1.0, 0.0, 1.0};
-GLfloat ambient1[]={0.0, 1.0, 0.0, 1.0};
-GLfloat specular1[]={0.0, 1.0, 0.0, 1.0};
-GLfloat direction1[] = {0.0, 0.0, 0.0};
-
-GLfloat lightPosition1[4];
-
-float lightAngle1 = 360.0, lightHeight1 = 5;
-int lightMoving1 = 0, lightStartX1, lightStartY1;
-
-/* Material types */
-GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
-GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0};
-GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
-GLfloat shine = 100.0;
-GLfloat glightmodel[] = {0.2,0.2,0.2,1};
-GLfloat emission[] = {0.0, 0.3, 0.3, 1.0};
-
-GLfloat lightBallSize = 0.2;
-
 /* Zoom and rotate tracking */
 GLfloat zoom = 0.0, rotate = 0.0, camAngle = 40.0;
 GLfloat zoomFactor = 0.2, camRotateFactor = 0.5, camAngleFactor = 0.5;
 /* Zoom and rotate tracking */
 GLfloat zoom = 0.0, rotate = 0.0, camAngle = 40.0;
 GLfloat zoomFactor = 0.2, camRotateFactor = 0.5, camAngleFactor = 0.5;
index e9e42f2..a849e05 100644 (file)
--- a/globals.h
+++ b/globals.h
@@ -9,10 +9,12 @@
 #define        GLOBALS_H
 
 // Defined values
 #define        GLOBALS_H
 
 // Defined values
-#define NMESH 54       // The number of meshes (in the models-textures dir)
+#define NMESH 54        // The number of meshes (in the models-textures dir)
 #define NTEXTURE 30     // The number of textures (in the models-textures dir)
 #define MAXOBJECTS 256
 
 #define NTEXTURE 30     // The number of textures (in the models-textures dir)
 #define MAXOBJECTS 256
 
+#define LIGHTS 2        // Number of lights in program
+
 #ifndef M_PI
 #define M_PI 3.14159265358979323846
 #endif
 #ifndef M_PI
 #define M_PI 3.14159265358979323846
 #endif
@@ -32,6 +34,30 @@ extern int curObject;
 extern int buttonSelected;
 extern int manipulateState;
 
 extern int buttonSelected;
 extern int manipulateState;
 
+/* Light object arrays and default values */
+extern LightObject lightObjs[];
+
+extern GLfloat defaultPosition[];
+
+extern GLfloat defaultAmbient[];
+extern GLfloat defaultDiffuse[];
+extern GLfloat defaultSpecular[];
+extern GLfloat defaultDirection[];
+
+extern GLfloat defaultCutoff;
+extern GLfloat defaultExponent;
+
+/* Material types */
+extern GLfloat materialAmbient[];
+extern GLfloat materialDiffuse[];
+extern GLfloat materialSpecular[];
+
+/* Light parameters */
+extern GLfloat lightShine;
+extern GLfloat lightGlobalModel[];
+extern GLfloat lightGlobalEmission[];
+extern GLfloat lightBallSize;
+
 // Directories containing models
 extern char *dirDefault1;
 extern char *dirDefault2;
 // Directories containing models
 extern char *dirDefault1;
 extern char *dirDefault2;
@@ -52,38 +78,6 @@ extern GLfloat factor;
 /* Length of axis lines */
 extern GLfloat lineLength;
 
 /* Length of axis lines */
 extern GLfloat lineLength;
 
-/* Light 0 parameters */
-extern GLfloat diffuse0[];
-extern GLfloat ambient0[];
-extern GLfloat specular0[];
-extern GLfloat direction0[];
-
-extern GLfloat lightPosition0[];
-
-extern float lightAngle0, lightHeight0;
-extern int lightMoving0, lightStartX0, lightStartY0;
-
-/* Light 1 parameters */
-extern GLfloat diffuse1[];
-extern GLfloat ambient1[];
-extern GLfloat specular1[];
-extern GLfloat direction1[];
-
-extern GLfloat lightPosition1[];
-
-extern float lightAngle1, lightHeight1;
-extern int lightMoving1, lightStartX1, lightStartY1;
-
-/* Material types */
-extern GLfloat ambient[];
-extern GLfloat diffuse[];
-extern GLfloat specular[];
-extern GLfloat shine;
-extern GLfloat glightmodel[];
-extern GLfloat emission[];
-
-extern GLfloat lightBallSize;
-
 /* Beginning width, height */
 extern int width, height;
 
 /* Beginning width, height */
 extern int width, height;
 
index ab96c88..5ebcc09 100644 (file)
--- a/helper.c
+++ b/helper.c
@@ -316,4 +316,50 @@ int addSceneObject(int id) {
   nObjects += 1; // New object in scene
 
   return nObjects;
   nObjects += 1; // New object in scene
 
   return nObjects;
+}
+
+void initializeLights() {
+  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightGlobalModel);
+  glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
+  
+  for ( int i = 0; i < LIGHTS; i++ ) {
+
+    memcpy( lightObjs[i].position, defaultPosition, sizeof(GLfloat) * 4 );
+    memcpy( lightObjs[i].ambient, defaultAmbient, sizeof(GLfloat) * 4 );
+    memcpy( lightObjs[i].diffuse, defaultDiffuse, sizeof(GLfloat) * 4 );
+    memcpy( lightObjs[i].specular, defaultSpecular, sizeof(GLfloat) * 4 );
+    memcpy( lightObjs[i].direction, defaultDirection, sizeof(GLfloat) * 3 );
+
+    lightObjs[i].cutoff = defaultCutoff;
+    lightObjs[i].exponent = defaultExponent;
+  }
+
+}
+
+void updateLights() {
+  glDisable(GL_LIGHTING);
+
+  for ( int i = 0; i < LIGHTS; i++ ) {
+    glDisable(GL_LIGHT0 + i);
+
+    glLightfv(GL_LIGHT0 + i, GL_POSITION, lightObjs[i].position);
+    glLightfv(GL_LIGHT0 + i, GL_AMBIENT, lightObjs[i].ambient);
+    glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightObjs[i].diffuse);
+    glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lightObjs[i].specular);
+    glLightfv(GL_LIGHT0 + i, GL_SPOT_DIRECTION, lightObjs[i].direction);
+
+    glLightf(GL_LIGHT0 + i, GL_SPOT_CUTOFF, lightObjs[i].cutoff);
+    glLighti(GL_LIGHT0 + i, GL_SPOT_EXPONENT, (int)lightObjs[i].exponent );
+
+    // Draw light ball for light
+    glPushMatrix();
+      glColor3f(1.0, 1.0, 1.0);
+      glTranslatef(lightObjs[i].position[0], lightObjs[i].position[1], lightObjs[i].position[2]);
+      glutSolidSphere(lightBallSize, 20, 20);
+    glPopMatrix();
+
+    glEnable(GL_LIGHT0 + i);
+  }
+
+  glEnable(GL_LIGHTING);
 }
\ No newline at end of file
 }
\ No newline at end of file
index c0c2462..8c55595 100644 (file)
--- a/helper.h
+++ b/helper.h
@@ -26,4 +26,7 @@ void drawAxisLines();
 
 int addSceneObject(int id);
 
 
 int addSceneObject(int id);
 
+void initializeLights();
+void updateLights();
+
 #endif /* HELPER_H */
 #endif /* HELPER_H */
diff --git a/scene.c b/scene.c
index 9a803cb..b56b518 100644 (file)
--- a/scene.c
+++ b/scene.c
@@ -154,8 +154,8 @@ void makeMenu() {
   int lightMenu = glutCreateMenu(processLightEvents);\r
   glutAddMenuEntry("Move Light 1", M_LIGHT_MOVE_LIGHT_1);\r
   //glutAddMenuEntry("R/G/B/All Light 1", M_LIGHT_RGBALL_LIGHT_1);\r
   int lightMenu = glutCreateMenu(processLightEvents);\r
   glutAddMenuEntry("Move Light 1", M_LIGHT_MOVE_LIGHT_1);\r
   //glutAddMenuEntry("R/G/B/All Light 1", M_LIGHT_RGBALL_LIGHT_1);\r
-  //glutAddMenuEntry("Move Light 2", M_LIGHT_MOVE_LIGHT_2);\r
-  glutAddMenuEntry("R/G/B/All Light 2", M_LIGHT_RGBALL_LIGHT_2);\r
+  glutAddMenuEntry("Move Light 2", M_LIGHT_MOVE_LIGHT_2);\r
+  //glutAddMenuEntry("R/G/B/All Light 2", M_LIGHT_RGBALL_LIGHT_2);\r
 \r
   // Construct object menu\r
   int objectMenu = makeSubmenuFromArray( objectMenuEntries, NMESH, processObjectEvents );\r
 \r
   // Construct object menu\r
   int objectMenu = makeSubmenuFromArray( objectMenuEntries, NMESH, processObjectEvents );\r
@@ -390,22 +390,13 @@ void display() {
   glRotatef(camAngle*camAngleFactor, 1.0, 0.0, 0.0); // Set camera angle upward\r
 \r
   /* Reposition the light source 0. */\r
   glRotatef(camAngle*camAngleFactor, 1.0, 0.0, 0.0); // Set camera angle upward\r
 \r
   /* Reposition the light source 0. */\r
-  lightPosition0[0] = 12*cos(lightAngle0);\r
-  lightPosition0[1] = lightHeight0;\r
-  lightPosition0[2] = 12*sin(lightAngle0);\r
-  lightPosition0[3] = 0.0;\r
+  lightObjs[0].position[0] = 12;\r
+  lightObjs[0].position[1] = 5;\r
+  lightObjs[0].position[2] = 12;\r
+  lightObjs[0].position[3] = 0.0;\r
 \r
 \r
-  direction0[0] = lightPosition0[0];\r
-  direction0[2] = lightPosition0[2];\r
-\r
-  /* Reposition the light source 1. */\r
-  lightPosition1[0] = 12*cos(lightAngle1);\r
-  lightPosition1[1] = lightHeight1;\r
-  lightPosition1[2] = 12*sin(lightAngle1);\r
-  lightPosition1[3] = 0.0;\r
-\r
-  direction1[0] = lightPosition1[0];\r
-  direction1[2] = lightPosition1[2];\r
+  //direction0[0] = lightPosition0[0];\r
+  //direction0[2] = lightPosition0[2];\r
 \r
   glPushMatrix();\r
 \r
 \r
   glPushMatrix();\r
 \r
@@ -414,9 +405,6 @@ void display() {
     glTranslatef(camx, camy, camz);\r
     glRotatef(keyrot, 1.0, 0.0, 0.0);\r
 \r
     glTranslatef(camx, camy, camz);\r
     glRotatef(keyrot, 1.0, 0.0, 0.0);\r
 \r
-    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0);\r
-    glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1);\r
-\r
     drawFloor();\r
     \r
     // Draw sceneObjs array\r
     drawFloor();\r
     \r
     // Draw sceneObjs array\r
@@ -460,21 +448,7 @@ void display() {
       glPopMatrix();\r
     }\r
 \r
       glPopMatrix();\r
     }\r
 \r
-    // Draw a white ball over the light sources\r
-    glDisable(GL_LIGHTING);\r
-    glColor3f(1.0, 1.0, 1.0);\r
-\r
-    glPushMatrix();\r
-      glTranslatef(lightPosition0[0], lightPosition0[1], lightPosition0[2]);\r
-      glutSolidSphere(lightBallSize, 20, 20);\r
-    glPopMatrix();\r
-\r
-    glPushMatrix();\r
-      glTranslatef(lightPosition1[0], lightPosition1[1], lightPosition1[2]);\r
-      glutSolidSphere(lightBallSize, 20, 20);\r
-    glPopMatrix();\r
-\r
-    glEnable(GL_LIGHTING);\r
+    updateLights();\r
 \r
     drawAxisLines();\r
 \r
 \r
     drawAxisLines();\r
 \r
@@ -483,28 +457,6 @@ void display() {
   glutSwapBuffers();\r
 }\r
 \r
   glutSwapBuffers();\r
 }\r
 \r
-/**\r
- * init function; sets initial OpenGL state\r
- */\r
-void init() {\r
-  glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);\r
-  glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);\r
-  glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);\r
-  glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0);\r
-\r
-  glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);\r
-  glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);\r
-  glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);\r
-  glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, direction1);\r
-\r
-  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel);\r
-  glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);\r
-\r
-  glEnable(GL_LIGHT0);\r
-  glEnable(GL_LIGHT1);\r
-  glEnable(GL_LIGHTING);\r
-}\r
-\r
 /**\r
  * Main function\r
  * @param argc Number of arguments\r
 /**\r
  * Main function\r
  * @param argc Number of arguments\r
@@ -549,7 +501,7 @@ int main(int argc, char **argv) {
 \r
   makeMenu();\r
 \r
 \r
   makeMenu();\r
 \r
-  init();\r
+  initializeLights();\r
 \r
   glutMainLoop();\r
 }
\ No newline at end of file
 \r
   glutMainLoop();\r
 }
\ No newline at end of file
diff --git a/scene.h b/scene.h
index 33955b4..1f9abfc 100644 (file)
--- a/scene.h
+++ b/scene.h
@@ -21,7 +21,6 @@ void keyboard(unsigned char key, int x, int y);
 void motion(int x, int y);
 
 void display();
 void motion(int x, int y);
 
 void display();
-void init();
 int main(int argc, char **argv);
 
 #endif /* SCENE_H */
 int main(int argc, char **argv);
 
 #endif /* SCENE_H */
diff --git a/types.h b/types.h
index 0d7110e..56cb1fa 100644 (file)
--- a/types.h
+++ b/types.h
@@ -34,23 +34,37 @@ typedef struct {
     GLubyte *rgbData;   // Array of bytes with the colour data for the texture
 } texture;
 
     GLubyte *rgbData;   // Array of bytes with the colour data for the texture
 } texture;
 
+// A type for point data
 typedef struct {
     GLfloat x, y, z;    // Amount of rotation on axis
 typedef struct {
     GLfloat x, y, z;    // Amount of rotation on axis
-} transform;
+} point;
 
 
+// A type for texture id/scale data
 typedef struct {
     int id;
     GLfloat scale;
 } texturedat;
 
 typedef struct {
     int id;
     GLfloat scale;
 } texturedat;
 
+// A type to maintain the state of an object in the scene
 typedef struct {
 typedef struct {
-    int mesh;           // Mesh index number
-    texturedat texture; // Texture index number
-    GLfloat x,y,z;      // Scene position
-    GLfloat scale[3];   // Scale vector
-    transform rotation; // Rotation transformation
+    int mesh;             // Mesh index number
+    texturedat texture;   // Texture index number
+    GLfloat x,y,z;        // Scene position
+    GLfloat scale[3];     // Scale vector
+    point rotation;       // Rotation transformation
 } SceneObject;
 
 } SceneObject;
 
+// A type to maintain the state of a light in the scene
+typedef struct {
+    GLfloat position[4];  // Light position
+    GLfloat ambient[4];   // Ambient parameter
+    GLfloat diffuse[4];   // Diffuse parameter
+    GLfloat specular[4];  // Specular parameter
+    GLfloat direction[3]; // Direction parameter
+    GLfloat cutoff;       // Cutoff of light
+    char exponent;        // Light exponent value, 0 - 128
+} LightObject;
+
 // Menu enum
 enum menu {
   // Main menu
 // Menu enum
 enum menu {
   // Main menu

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