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";
/* 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;
#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 LIGHTS 2 // Number of lights in program
+
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
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;
/* 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;
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
int addSceneObject(int id);
+void initializeLights();
+void updateLights();
+
#endif /* HELPER_H */
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
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
- 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
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
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
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
makeMenu();\r
\r
- init();\r
+ initializeLights();\r
\r
glutMainLoop();\r
}
\ No newline at end of file
void motion(int x, int y);
void display();
-void init();
int main(int argc, char **argv);
#endif /* SCENE_H */
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
-} transform;
+} point;
+// A type for texture id/scale data
typedef struct {
int id;
GLfloat scale;
} texturedat;
+// A type to maintain the state of an object in the scene
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;
+// 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