X-Git-Url: https://git.ucc.asn.au/?p=atyndall%2Fcits2231.git;a=blobdiff_plain;f=helper.c;h=8135854e751bdd83c6c4f770ab6480ffa2a71e00;hp=ad20eb7bbd10981df1bfdd13734cdcc68366022b;hb=5b211bce232b1e75aa7cd3a08c533a8dd446f2fa;hpb=74c5d360c0a9fa9e125d4d2098fe604fb7d5e767;ds=inline diff --git a/helper.c b/helper.c index ad20eb7..8135854 100644 --- a/helper.c +++ b/helper.c @@ -207,79 +207,180 @@ int makeSubmenuFromArray( const char *menuEntries[], unsigned int menuEntriesSiz } /** - * Recursive function to draw a square by drawing smaller and smaller - * divisions of the square, determined by drawFloorRecurse. - * @param recurseLevel Current level of recursion, only pass 0 - * @param x1 top-left x - * @param z1 top-left z - * @param x2 bottom-left x - * @param z2 bottom-left z + * Draw a floor by looping over the floorSize and squareSize variables */ -void drawSquare(int recurseLevel, float x1, float z1, float x2, float z2) { +void drawFloor() { + if ( currentGroundTexture == 0 ) { + getTexture(1); + glBindTexture(GL_TEXTURE_2D, 1); + } else { + getTexture(currentGroundTexture); + glBindTexture(GL_TEXTURE_2D, currentGroundTexture); + } - if ( drawFloorRecurse != recurseLevel ) { - // Calculate middle points - float xm = (x1 + x2) / 2.0; - float zm = (z1 + z2) / 2.0; + glBegin(GL_QUADS); - // Increment recursion level - int rnew = recurseLevel + 1; + for ( int x = -floorSize; x < floorSize; x++ ) { + for ( int z = -floorSize; z < floorSize; z++ ) { + glColor3f( 1.0, 1.0, 1.0 ); - // Split into four sub-quads - drawSquare(rnew, x1, z1, xm, zm); - drawSquare(rnew, x1, zm, xm, z2); - drawSquare(rnew, xm, zm, x2, z2); - drawSquare(rnew, xm, z1, x2, zm); + glNormal3i( 0, 1, 0 ); + glTexCoord2f( 1.0, 1.0 ); + glVertex3f ( (x+1)*squareSize, 0.0, (z+1)*squareSize ); - } else { - // Draw square. - // **NOTE: Is the polygon facing in the right direction? - srand ( time(NULL) ); - if ( rand() % 10 < 5 ) { - printf("Random don't draw\n"); - return; + glNormal3i( 0, 1, 0 ); + glTexCoord2f( 1.0, 0.0 ); + glVertex3f ( (x+1)*squareSize, 0.0, z*squareSize ); + + glNormal3i( 0, 1, 0 ); + glTexCoord2f( 0.0, 0.0 ); + glVertex3f ( x*squareSize, 0.0, z*squareSize ); + + glNormal3i( 0, 1, 0 ); + glTexCoord2f( 0.0, 1.0 ); + glVertex3f ( x*squareSize, 0.0, (z+1)*squareSize ); + } } - //printf("Drawing square (%f, %f), (%f, %f), (%f, %f), (%f, %f)\n", x1, z1, x1, z2, x2, z2, x2, z1); - glBegin(GL_QUADS); - glNormal3f(0,1,0); - glVertex3f(x1, 0.0, z1); - glVertex3f(x1, 0.0, z2); - glVertex3f(x2, 0.0, z2); - glVertex3f(x2, 0.0, z1); - glEnd(); - } -} + glEnd(); -/** - * Draw a floor by calling the drawSquare recursion - */ -void drawFloor() { - drawSquare(0, -floorSize, -floorSize, floorSize, floorSize); + if ( currentGroundTexture != 0 ) { + glBindTexture(GL_TEXTURE_2D, 0); + } } /** * Draw x, z axis on floor */ -void drawLine() { - // **NOTE: fix function - glDisable(GL_TEXTURE_2D); - glDisable(GL_LIGHTING); - //glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glColor3f( 0.0, 0.0, 0.0 ); - - glBegin(GL_LINES); +void drawAxisLines() { + // **NOTE: Function does not currently draw arrow-heads + + glDisable(GL_TEXTURE_2D); + glDisable(GL_LIGHTING); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_LINE_SMOOTH); + glColor3f( 0.0, 0.0, 0.0 ); + + float arrowLength = 1.0; + + glBegin(GL_LINES); + // x arrow glVertex3i( lineLength, 0.0, 0.0 ); glVertex3i( -lineLength, 0.0, 0.0 ); - glEnd(); + + // x arrowheads + glVertex3i( lineLength, 0.0, 0.0 ); + glVertex3i( lineLength - arrowLength, 0.0, 1.0 ); + + glVertex3i( lineLength, 0.0, 0.0 ); + glVertex3i( lineLength - arrowLength, 0.0, -1.0 ); + + // z arrow + glVertex3i( 0.0, 0.0, lineLength ); + glVertex3i( 0.0, 0.0, -lineLength ); + + // z arrowheads + glVertex3i( 0.0, 0.0, lineLength ); + glVertex3i( 1.0, 0.0, lineLength - arrowLength ); + + glVertex3i( 0.0, 0.0, lineLength ); + glVertex3i( -1.0, 0.0, lineLength - arrowLength ); + glEnd(); + + glDisable(GL_BLEND); + glEnable(GL_LIGHTING); + glEnable(GL_TEXTURE_2D); +} + +int addSceneObject(int id) { + // **NOTE: Currently only adds the teapot - glBegin(GL_LINES); - glVertex3i( 0.0, 0.0, lineLength ); - glVertex3i( 0.0, 0.0, -lineLength ); - glEnd(); + curObject = nObjects; - //glDisable(GL_BLEND); - glEnable(GL_LIGHTING); - glEnable(GL_TEXTURE_2D); + sceneObjs[nObjects].mesh = -1; // Teapot is -1 mesh + sceneObjs[nObjects].texture.id = -1; // No texture + sceneObjs[nObjects].texture.scale = 1; + sceneObjs[nObjects].x = 0; + sceneObjs[nObjects].y = 0; + sceneObjs[nObjects].z = 0; + + sceneObjs[nObjects].scale[0] = 1; + sceneObjs[nObjects].scale[1] = 1; + sceneObjs[nObjects].scale[2] = 1; + + sceneObjs[nObjects].rotation.x = 0; + sceneObjs[nObjects].rotation.y = 0; + sceneObjs[nObjects].rotation.z = 0; + + 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 ); + + glEnable(GL_LIGHT0 + i); + + // Draw light ball for light + glPushMatrix(); + glColor3f(0.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_LIGHTING); +} + +/** + * Sets variables to the state they are in to begin with. + */ +void initializeState() { + /* Initial light 0 position. */ + lightObjs[0].position[0] = 4; + lightObjs[0].position[1] = 5; + lightObjs[0].position[2] = 6; + lightObjs[0].position[3] = 0.0; + + /* Initial light 1 position. */ + lightObjs[1].position[0] = -4; + lightObjs[1].position[1] = 5; + lightObjs[1].position[2] = -6; + lightObjs[1].position[3] = 0.0; + + /* Initial floor texture */ + currentGroundTexture = 5; } \ No newline at end of file