From f1c86cda52b306cdfbe62e1776eb38cff811f47b Mon Sep 17 00:00:00 2001 From: Ash Tyndall Date: Fri, 21 Oct 2011 19:59:39 +0800 Subject: [PATCH] --- globals.c | 22 ++++++---- globals.h | 8 +++- helper.c | 8 +++- helper.h | 2 +- scene.c | 122 ++++++++++++++++++++++++++++++++++++++---------------- types.h | 10 ++++- 6 files changed, 120 insertions(+), 52 deletions(-) diff --git a/globals.c b/globals.c index ded55aa..4f7c69e 100644 --- a/globals.c +++ b/globals.c @@ -35,7 +35,10 @@ const char *objectMenuEntries[NMESH] = { }; SceneObject sceneObjs[MAXOBJECTS]; // An array with details of the objects in a scene -int nObjects=0; // How many objects there are in the scene currently. +int nObjects = 0; // How many objects there are in the scene currently. +int curObject = -1; // The scene object that is currently selected, (-1 is no object) +int buttonSelected = -1; // Either GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or -1 (no button) +int manipulateState = STATE_CAMERA_ROTATE_MOVE; // See manipulateStates enum // Directories containing models char *dirDefault1 = "models-textures"; @@ -60,9 +63,9 @@ GLfloat factor = 1.0; GLfloat lineLength = 10; /* Light 0 parameters */ -GLfloat diffuse0[]={1.0, 1.0, 1.0, 0.1}; -GLfloat ambient0[]={1.0, 1.0, 1.0, 0.1}; -GLfloat specular0[]={1.0, 1.0, 1.0, 0.1}; +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]; @@ -71,9 +74,9 @@ float lightAngle0 = 0.0, lightHeight0 = 5; int lightMoving0 = 0, lightStartX0, lightStartY0; /* Light 1 parameters */ -GLfloat diffuse1[]={1.0, 1.0, 1.0, 0.1}; -GLfloat ambient1[]={1.0, 1.0, 1.0, 0.1}; -GLfloat specular1[]={1.0, 1.0, 1.0, 0.1}; +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]; @@ -90,8 +93,9 @@ 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; -GLfloat zoomFactor = 0.2, rotateFactor = 0.5; +GLfloat zoom = 0.0, rotate = 0.0, camAngle = 40.0; +GLfloat zoomFactor = 0.2, rotateFactor = 0.5, camAngleFactor = 0.5; +GLfloat leftrightFactor = 0.5, nearfarFactor = 0.5, bigsmallFactor = 0.5, updownFactor = 0.5; /* Beginning width, height */ int width = 500, height = 500; diff --git a/globals.h b/globals.h index b3d0d6b..058e32c 100644 --- a/globals.h +++ b/globals.h @@ -24,6 +24,9 @@ extern const char *objectMenuEntries[]; // Scene object arrays extern SceneObject sceneObjs[]; extern int nObjects; +extern int curObject; +extern int buttonSelected; +extern int manipulateState; // Directories containing models extern char *dirDefault1; @@ -82,8 +85,9 @@ extern GLfloat emission[]; extern int width, height; /* Zoom and rotate tracking */ -extern GLfloat zoom, rotate; -extern GLfloat zoomFactor, rotateFactor; +extern GLfloat zoom, rotate, camAngle; +extern GLfloat zoomFactor, rotateFactor, camAngleFactor; +extern GLfloat leftrightFactor, nearfarFactor, bigsmallFactor, updownFactor; /* Texture state tracking */ extern int currentGroundTexture, currentMeshTexture; diff --git a/helper.c b/helper.c index ea80294..f6ecc8c 100644 --- a/helper.c +++ b/helper.c @@ -293,10 +293,10 @@ void drawAxisLines() { glEnable(GL_TEXTURE_2D); } -void addSceneObject(int id) { +int addSceneObject(int id) { // **NOTE: Currently only adds the teapot - nObjects += 1; // New object in scene + curObject = nObjects; sceneObjs[nObjects].mesh = -1; sceneObjs[nObjects].texture = -1; @@ -308,4 +308,8 @@ void addSceneObject(int id) { sceneObjs[nObjects].rotation.parameter = 0; sceneObjs[nObjects].rotation.vect = (vector*)calloc( 3, sizeof(vector[0]) ); + + nObjects += 1; // New object in scene + + return nObjects; } \ No newline at end of file diff --git a/helper.h b/helper.h index f6d86c8..c0c2462 100644 --- a/helper.h +++ b/helper.h @@ -24,6 +24,6 @@ void drawSquare(int recurseLevel, float x1, float z1, float x2, float z2); void drawFloor(); void drawAxisLines(); -void addSceneObject(int id); +int addSceneObject(int id); #endif /* HELPER_H */ diff --git a/scene.c b/scene.c index 6ae85a7..e2e8cd2 100644 --- a/scene.c +++ b/scene.c @@ -198,15 +198,15 @@ void windowReshape(int w, int h) { * @param y Mouse y position */ void mouse(int button, int state, int x, int y) { - if (button == GLUT_LEFT_BUTTON) { - if (state == GLUT_DOWN) { - moving = 1; - startx = x; - starty = y; - } - if (state == GLUT_UP) { - moving = 0; - } + switch(button) { + case GLUT_LEFT_BUTTON: + case GLUT_MIDDLE_BUTTON: + if ( state == GLUT_DOWN ) { + buttonSelected = button; + } else if ( state == GLUT_UP ) { + buttonSelected = -1; + } + break; } } @@ -276,27 +276,47 @@ void keyboard(unsigned char key, int x, int y) { * @param y Mouse y position */ void motion(int x, int y) { - if (moving) { - zoom += (y - starty); - rotate += (x - startx); - startx = x; - starty = y; - glutPostRedisplay(); - } - 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(); + if ( buttonSelected == -1 ) return; // No button selected, no action + + switch ( manipulateState ) { + case STATE_CAMERA_ROTATE_MOVE: + rotate += (x - startx); + + if ( buttonSelected == GLUT_LEFT_BUTTON ) { + // w: rotate, h: zoom + zoom += (y - starty); + } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) { + // w: rotate, h: tilt + camAngle += (y - starty); + } + + starty = y; + startx = x; + + break; + + case STATE_OBJECT_POSITION_SCALE: + //SceneObject co = sceneObjs[curObject]; + + if ( buttonSelected == GLUT_LEFT_BUTTON ) { + // w: left/right, h: near/far + + } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) { + // w: big/small, h: up/down + } + break; + + case STATE_OBJECT_ROTATION_TEXTURE_SCALE: + if ( buttonSelected == GLUT_LEFT_BUTTON ) { + // w: rotate on width, h: rotate on h + } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) { + // w & h, rotate on h + } + break; + } + + glutPostRedisplay(); } /** @@ -332,7 +352,7 @@ void display() { 0.0, 10.0, 0.0 /* up is in postivie Y direction */ ); - glRotatef(40.0, 1.0, 0.0, 0.0); + glRotatef(camAngle*camAngleFactor, 1.0, 0.0, 0.0); /* Reposition the light source 0. */ lightPosition0[0] = 12*cos(lightAngle0); @@ -364,13 +384,43 @@ void display() { drawFloor(); - // Draw teapot for a test object + // Draw sceneObjs array glPushMatrix(); - glTranslatef(0.0, 0.5, 0.0); // **NOTE: Teapot currently does not rest on surface - glColor3f(0.0, 0.0, 1.0); - glFrontFace(GL_CW); // The teapot does not obey the right-hand rule - glutSolidTeapot(1); - glFrontFace(GL_CCW); + for ( int i = 0; i < nObjects; i++ ) { + SceneObject so = sceneObjs[i]; + + // Apply rotation vector + vector* rv = so.rotation.vect; + glRotatef(so.rotation.parameter, *rv[0], *rv[1], *rv[2]); + + // Apply scaling vector + vector* sv = so.scale; + glScalef(*sv[0], *sv[1], *sv[2]); + + // Apply translation vector + glTranslatef(so.x, so.y, so.z); + + // Apply texture + if ( so.texture > 0 ) { + getTexture(so.texture); + glBindTexture(GL_TEXTURE_2D, so.texture); + } else { + getTexture(0); + glBindTexture(GL_TEXTURE_2D, 0); + } + + // Draw actual object + if ( so.mesh > 0 ) { + // drawMesh(); + } else if ( so.mesh == -1 ) { // a mesh of -1 draws the teapot + // The teapot does not obey the right-hand rule + glFrontFace(GL_CW); + glutSolidTeapot(1); + glFrontFace(GL_CCW); + } + + glBindTexture(GL_TEXTURE_2D, 0); + } glPopMatrix(); // Draw a white ball over the light source diff --git a/types.h b/types.h index 197eb03..91813ee 100644 --- a/types.h +++ b/types.h @@ -35,7 +35,7 @@ typedef struct { } texture; -typedef GLfloat vector[3]; // Vector datatyle +typedef GLfloat vector[3]; // Vector datatype typedef struct { GLfloat parameter; // Transform amount @@ -75,5 +75,11 @@ enum menu { M_LIGHT_RGBALL_LIGHT_2 }; -#endif /* TYPES_H */ +// Manipulation states +enum manipulateStates { + STATE_CAMERA_ROTATE_MOVE, + STATE_OBJECT_POSITION_SCALE, + STATE_OBJECT_ROTATION_TEXTURE_SCALE +}; +#endif /* TYPES_H */ \ No newline at end of file -- 2.20.1