From 751a7ad384e80669e71e8e5613227ac450c71944 Mon Sep 17 00:00:00 2001 From: Ash Tyndall Date: Fri, 21 Oct 2011 11:36:16 +0800 Subject: [PATCH] --- globals.c | 30 ++++++++++++++++++----------- globals.h | 29 ++++++++++++++++++---------- scene.c | 57 ++++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 82 insertions(+), 34 deletions(-) diff --git a/globals.c b/globals.c index f8ba810..d1feddd 100644 --- a/globals.c +++ b/globals.c @@ -43,17 +43,10 @@ char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures"; char dataDir[200]; // Stores the directory name for the meshes and textures. -GLfloat lightColor[] = {1.0, 1.0, 1.0, 1.0}; // White light -GLfloat lightPosition[4]; - int moving, startx, starty; -int lightMoving = 0, lightStartX, lightStartY; /* Time varying or user-controled variables. */ float jump = 0.0; -float lightAngle = 0.0, lightHeight = 5; -GLfloat angle = -150; /* in degrees */ -GLfloat angle2 = 30; /* in degrees */ /* Near and far parameters - calculates scene size */ GLfloat near = -30; @@ -80,18 +73,33 @@ GLfloat lineLength = 10; 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 emission0[] = {0.0, 0.3, 0.3, 1.0}; - GLfloat direction0[] = {0.0, 0.0, 0.0}; -GLfloat shine = 100.0; +GLfloat lightColor0[] = {1.0, 1.0, 1.0, 1.0}; // White light +GLfloat lightPosition0[4]; -GLfloat glightmodel[] = {0.2,0.2,0.2,1}; +float lightAngle0 = 0.0, lightHeight0 = 5; +int lightMoving0 = 0, lightStartX0, lightStartY0; + +/* Light 1 parameters */ +GLfloat diffuse1[]={1.0, 0.0, 0.0, 1.0}; +GLfloat ambient1[]={1.0, 0.0, 0.0, 1.0}; +GLfloat specular1[]={1.0, 0.0, 0.0, 1.0}; +GLfloat direction1[] = {0.0, 0.0, 0.0}; + +GLfloat lightColor1[] = {1.0, 1.0, 1.0, 1.0}; // White light +GLfloat lightPosition1[4]; + +float lightAngle1 = 180.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}; /* Zoom and rotate tracking */ GLfloat zoom = 0.0, rotate = 0.0; diff --git a/globals.h b/globals.h index b6e0454..640c323 100644 --- a/globals.h +++ b/globals.h @@ -32,12 +32,7 @@ extern char *dirDefault2; // Stores the directory name for the meshes and textures. extern char dataDir[]; -// Lighting -extern GLfloat lightColor[]; -extern GLfloat lightPosition[]; - extern int moving, startx, starty; -extern int lightMoving, lightStartX, lightStartY; /* Time varying or user-controled variables. */ extern float jump; @@ -70,19 +65,33 @@ extern GLfloat lineLength; extern GLfloat diffuse0[]; extern GLfloat ambient0[]; extern GLfloat specular0[]; -extern GLfloat emission0[]; - extern GLfloat direction0[]; -extern GLfloat light0_pos[]; -extern GLfloat shine; +extern GLfloat lightColor0[]; +extern GLfloat lightPosition0[]; -extern GLfloat glightmodel[]; +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 lightColor1[]; +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[]; /* Beginning width, height */ extern int width, height; diff --git a/scene.c b/scene.c index 395a725..19d74af 100644 --- a/scene.c +++ b/scene.c @@ -281,11 +281,18 @@ void motion(int x, int y) { starty = y; glutPostRedisplay(); } - if (lightMoving) { - lightAngle += (x - lightStartX)/40.0; - lightHeight += (lightStartY - y)/20.0; - lightStartX = x; - lightStartY = y; + if (lightMoving0) { + lightAngle0 += (x - lightStartX0)/40.0; + lightHeight0 += (lightStartY0 - y)/20.0; + lightStartX0 = x; + lightStartY0 = y; + glutPostRedisplay(); + } + if (lightMoving1) { + lightAngle1 += (x - lightStartX1)/40.0; + lightHeight1 += (lightStartY1 - y)/20.0; + lightStartX1 = x; + lightStartY1 = y; glutPostRedisplay(); } } @@ -325,11 +332,17 @@ void display() { glRotatef(40.0, 1.0, 0.0, 0.0); - /* Reposition the light source. */ - lightPosition[0] = 12*cos(lightAngle); - lightPosition[1] = lightHeight; - lightPosition[2] = 12*sin(lightAngle); - lightPosition[3] = 0.0; + /* Reposition the light source 0. */ + lightPosition0[0] = 12*cos(lightAngle0); + lightPosition0[1] = lightHeight0; + lightPosition0[2] = 12*sin(lightAngle0); + lightPosition0[3] = 0.0; + + /* Reposition the light source 1. */ + lightPosition1[0] = 12*cos(lightAngle1); + lightPosition1[1] = lightHeight1; + lightPosition1[2] = 12*sin(lightAngle1); + lightPosition1[3] = 0.0; glPushMatrix(); @@ -338,7 +351,8 @@ void display() { glTranslatef(camx, camy, camz); glRotatef(keyrot, 1.0, 0.0, 0.0); - glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); + glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0); + glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1); drawFloor(); @@ -357,7 +371,16 @@ void display() { glPushMatrix(); glDisable(GL_LIGHTING); glColor3f(1.0, 1.0, 0.0); - glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]); + glTranslatef(lightPosition0[0], lightPosition0[1], lightPosition0[2]); + glutSolidSphere(1.0, 50, 50); + glEnable(GL_LIGHTING); + glPopMatrix(); + + // Draw a white ball over the light source + glPushMatrix(); + glDisable(GL_LIGHTING); + glColor3f(1.0, 1.0, 0.0); + glTranslatef(lightPosition1[0], lightPosition1[1], lightPosition1[2]); glutSolidSphere(1.0, 50, 50); glEnable(GL_LIGHTING); glPopMatrix(); @@ -378,15 +401,23 @@ void init() { glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 90.0); + 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); + + glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 90.0); + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel); glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, specular); - glMaterialfv(GL_FRONT, GL_EMISSION, emission0); + glMaterialfv(GL_FRONT, GL_EMISSION, emission); glMaterialf(GL_FRONT, GL_SHININESS, shine); glEnable(GL_LIGHT0); + glEnable(GL_LIGHT1); glEnable(GL_LIGHTING); } -- 2.20.1