From: Ash Tyndall Date: Sat, 22 Oct 2011 06:51:59 +0000 (+0800) Subject: (no commit message) X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=cc01320b8824eb601505dc5cdb101cfe8b7d1271;p=atyndall%2Fcits2231.git --- diff --git a/globals.c b/globals.c index cfc021f..8bd2cdf 100644 --- 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 +/* 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"; @@ -59,38 +83,6 @@ GLfloat factor = 1.0; /* 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; diff --git a/globals.h b/globals.h index e9e42f2..a849e05 100644 --- a/globals.h +++ b/globals.h @@ -9,10 +9,12 @@ #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 @@ -32,6 +34,30 @@ extern int curObject; 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; @@ -52,38 +78,6 @@ extern GLfloat factor; /* 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; diff --git a/helper.c b/helper.c index ab96c88..5ebcc09 100644 --- a/helper.c +++ b/helper.c @@ -316,4 +316,50 @@ int addSceneObject(int id) { 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 diff --git a/helper.h b/helper.h index c0c2462..8c55595 100644 --- a/helper.h +++ b/helper.h @@ -26,4 +26,7 @@ void drawAxisLines(); int addSceneObject(int id); +void initializeLights(); +void updateLights(); + #endif /* HELPER_H */ diff --git a/scene.c b/scene.c index 9a803cb..b56b518 100644 --- a/scene.c +++ b/scene.c @@ -154,8 +154,8 @@ void makeMenu() { int lightMenu = glutCreateMenu(processLightEvents); glutAddMenuEntry("Move Light 1", M_LIGHT_MOVE_LIGHT_1); //glutAddMenuEntry("R/G/B/All Light 1", M_LIGHT_RGBALL_LIGHT_1); - //glutAddMenuEntry("Move Light 2", M_LIGHT_MOVE_LIGHT_2); - glutAddMenuEntry("R/G/B/All Light 2", M_LIGHT_RGBALL_LIGHT_2); + glutAddMenuEntry("Move Light 2", M_LIGHT_MOVE_LIGHT_2); + //glutAddMenuEntry("R/G/B/All Light 2", M_LIGHT_RGBALL_LIGHT_2); // Construct object menu int objectMenu = makeSubmenuFromArray( objectMenuEntries, NMESH, processObjectEvents ); @@ -390,22 +390,13 @@ void display() { glRotatef(camAngle*camAngleFactor, 1.0, 0.0, 0.0); // Set camera angle upward /* Reposition the light source 0. */ - lightPosition0[0] = 12*cos(lightAngle0); - lightPosition0[1] = lightHeight0; - lightPosition0[2] = 12*sin(lightAngle0); - lightPosition0[3] = 0.0; + lightObjs[0].position[0] = 12; + lightObjs[0].position[1] = 5; + lightObjs[0].position[2] = 12; + lightObjs[0].position[3] = 0.0; - direction0[0] = lightPosition0[0]; - direction0[2] = lightPosition0[2]; - - /* Reposition the light source 1. */ - lightPosition1[0] = 12*cos(lightAngle1); - lightPosition1[1] = lightHeight1; - lightPosition1[2] = 12*sin(lightAngle1); - lightPosition1[3] = 0.0; - - direction1[0] = lightPosition1[0]; - direction1[2] = lightPosition1[2]; + //direction0[0] = lightPosition0[0]; + //direction0[2] = lightPosition0[2]; glPushMatrix(); @@ -414,9 +405,6 @@ void display() { glTranslatef(camx, camy, camz); glRotatef(keyrot, 1.0, 0.0, 0.0); - glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0); - glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1); - drawFloor(); // Draw sceneObjs array @@ -460,21 +448,7 @@ void display() { glPopMatrix(); } - // Draw a white ball over the light sources - glDisable(GL_LIGHTING); - glColor3f(1.0, 1.0, 1.0); - - glPushMatrix(); - glTranslatef(lightPosition0[0], lightPosition0[1], lightPosition0[2]); - glutSolidSphere(lightBallSize, 20, 20); - glPopMatrix(); - - glPushMatrix(); - glTranslatef(lightPosition1[0], lightPosition1[1], lightPosition1[2]); - glutSolidSphere(lightBallSize, 20, 20); - glPopMatrix(); - - glEnable(GL_LIGHTING); + updateLights(); drawAxisLines(); @@ -483,28 +457,6 @@ void display() { glutSwapBuffers(); } -/** - * init function; sets initial OpenGL state - */ -void init() { - glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0); - glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0); - glLightfv(GL_LIGHT0, GL_SPECULAR, specular0); - glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0); - - glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1); - glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1); - glLightfv(GL_LIGHT1, GL_SPECULAR, specular1); - glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, direction1); - - glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel); - glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); - - glEnable(GL_LIGHT0); - glEnable(GL_LIGHT1); - glEnable(GL_LIGHTING); -} - /** * Main function * @param argc Number of arguments @@ -549,7 +501,7 @@ int main(int argc, char **argv) { makeMenu(); - init(); + initializeLights(); glutMainLoop(); } \ No newline at end of file diff --git a/scene.h b/scene.h index 33955b4..1f9abfc 100644 --- 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 init(); int main(int argc, char **argv); #endif /* SCENE_H */ diff --git a/types.h b/types.h index 0d7110e..56cb1fa 100644 --- 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; +// 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