X-Git-Url: https://git.ucc.asn.au/?p=atyndall%2Fcits2231.git;a=blobdiff_plain;f=scene.c;h=19cc58aaae7b84e6a0d7c0e998bd92d0342e1f3c;hp=002fadc3a7e22505b7a3aa50a9602b6c27109002;hb=94b530f4dea0bf804fca4ca966a37a28ffd0d2f6;hpb=19fa399bf0c4b47f6f23a320627533f0c902bca8 diff --git a/scene.c b/scene.c index 002fadc..19cc58a 100644 --- a/scene.c +++ b/scene.c @@ -1,9 +1,7 @@ -// compile this program using: -// gcc -O3 -Wall -std=c99 -o scene scene.c bitmap.c -lglut -// Or, with cygwin: (-mno-cygwin is only so the executable runs from windows) -// gcc -mno-cygwin -O3 -Wall -std=c99 -o scene scene.c bitmap.c -lglut32 -lglu32 -lopengl32 -// Or, use make via the supplied Makefile: (For cygwin, install the package for make) -// make +/** + * CITS2231 Graphics Scene Editor + * @author Ashley Tyndall (20915779) + */ #include #include @@ -105,13 +103,39 @@ 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. +// Directories containing models +char *dirDefault1 = "models-textures"; +char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures"; + +char dataDir[200]; // Stores the directory name for the meshes and textures. + +static GLfloat floorVertices[4][3] = { + { -1000.0, 0.0, 1000.0 }, + { 1000.0, 0.0, 1000.0 }, + { 1000.0, 0.0, -1000.0 }, + { -1000.0, 0.0, -1000.0 }, +}; + +static GLfloat lightColor[] = {1.0, 1.0, 1.0, 1.0}; // White light +static GLfloat lightPosition[4]; +static float lightAngle = 10.0, lightHeight = 20; + +/** + * Prints out error message when file cannot be read + * @param fileName Name of file that could not be read + */ void fileErr(char* fileName) { printf("Error reading file: %s\n", fileName); printf("If not in the CSSE labs, you will need to include the directory containing\n"); printf("the models on the command line, or put it in the same folder as the exectutable."); - exit(1); + exit(EXIT_FAILURE); } +/** + * Reads .bmp texture files and converts them to a texture object + * @param fileName .bmp texture file + * @return texture object + */ texture* loadTexture(char *fileName) { texture* t = malloc(sizeof (texture)); BITMAPINFO *info; @@ -123,8 +147,11 @@ texture* loadTexture(char *fileName) { return t; } -// The following works for the supplied .x files -// but probably not for .x files from other sources. +/** + * Reads .x files and converts them to a mesh object + * @param fileName .x mesh file + * @return mesh object + */ mesh* loadMesh(char* fileName) { mesh* m = malloc(sizeof (mesh)); FILE* fp = fopen(fileName, "r"); @@ -167,11 +194,13 @@ mesh* loadMesh(char* fileName) { return m; } -char dataDir[200]; // Stores the directory name for the meshes and textures. - -// getMesh(i) loads mesh[i] if it isn't already loaded. -// You must call getMesh(i) at least once before using mesh[i]. // [You may want to add to this function.] +/** + * Loads mesh[i] if it isn't already loaded. + * You must call getMesh(i) at least once before using mesh[i]. + * + * @param i Mesh ID + */ void getMesh(int i) { // getMesh(i) loads mesh[i] if it isn't already loaded. char fileName[220]; if(i>=NMESH || i<0) { @@ -184,13 +213,21 @@ void getMesh(int i) { // getMesh(i) loads mesh[i] if it isn't already loaded. meshes[i] = loadMesh(fileName); } -// getTexture(i) loads texture i if it isn't already loaded. -// After calling getTexture(i), you can make texture i the current texture using -// glBindTexture(GL_TEXTURE_2D, i); (Use i=0 to return to the default plain texture.) -// You can then scale the texture via: (See the textbook, section 8.8.3.) -// glMatrixMode(GL_TEXTURE); -// You must call getTexture(i) at least once before using texture i. -void getTexture(int i) { // getTexture(i) loads texture i if it isn't already loaded. +/** + * Loads texture i if it isn't already loaded + * + * After calling getTexture(i), you can make texture i the current texture using + * glBindTexture(GL_TEXTURE_2D, i); + * Use i=0 to return to the default plain texture. + * + * You can then scale the texture via: + * glMatrixMode(GL_TEXTURE); + * See the textbook, section 8.8.3. + * + * You must call getTexture(i) at least once before using texture i. + * @param i Texture ID + */ +void getTexture(int i) { char fileName[220]; if(i<1 || i>NTEXTURE) { printf("Error in getTexture - wrong texture number"); @@ -216,6 +253,10 @@ void getTexture(int i) { // getTexture(i) loads texture i if it isn't already lo glBindTexture(GL_TEXTURE_2D, 0); // Back to default texture } +/** + * Event hander for main menu events + * @param id ID of menu item selected + */ void processMainEvents(int id) { switch (id) { case ROTATE_MOVE_CAMERA: @@ -236,10 +277,10 @@ void processMainEvents(int id) { } } -void processObjectEvents(int id) { - -} - +/** + * Event hander for materials menu events + * @param id ID of menu item selected + */ void processMaterialEvents(int id) { switch (id) { case MATERIAL_ALL_RGB: @@ -277,14 +318,10 @@ void processMaterialEvents(int id) { } } -void processTextureEvents(int id) { - -} - -void processGTextureEvents(int id) { - -} - +/** + * Event hander for light menu events + * @param id ID of menu item selected + */ void processLightEvents(int id) { switch (id) { case LIGHT_MOVE_LIGHT_1: @@ -306,6 +343,30 @@ void processLightEvents(int id) { } } +/** + * Event hander for object menu events + * @param id ID of object selected + */ +void processObjectEvents(int id) { + +} + +/** + * Event hander for texture menu events + * @param id ID of texutre selected + */ +void processTextureEvents(int id) { + +} + +/** + * Event hander for ground texture menu events + * @param id ID of ground texture selected + */ +void processGTextureEvents(int id) { + +} + /** * Rounds up numbers, from http://stackoverflow.com/questions/3407012/c-rounding-up-to-the-nearest-multiple-of-a-number * @param numToRound Number to round @@ -361,6 +422,9 @@ int makeSubmenuFromArray( const char *menuEntries[], unsigned int menuEntriesSiz return mainMenu; } +/** + * Creates menu for program + */ void makeMenu() { // Construct material menu int materialMenu = glutCreateMenu(processMaterialEvents); @@ -390,49 +454,198 @@ void makeMenu() { int gTextureMenu = makeSubmenuFromArray( textureMenuEntries, textureMenuEntriesSize, processGTextureEvents ); // Construct main menu - int mainMenu = glutCreateMenu(processMainEvents); - glutAddMenuEntry("Rotate/Move Camera", ROTATE_MOVE_CAMERA); - glutAddSubMenu("Add object", objectMenu); - glutAddMenuEntry("Position/Scale", POSITION_SCALE); - glutAddMenuEntry("Rotation/Texture Scale", ROTATION_TEXTURE_SCALE); - glutAddSubMenu("Material", materialMenu); - glutAddSubMenu("Texture", textureMenu); - glutAddSubMenu("Ground texture", gTextureMenu); - glutAddSubMenu("Lights", lightMenu); + glutCreateMenu(processMainEvents); + //glutAddMenuEntry("Rotate/Move Camera", ROTATE_MOVE_CAMERA); + //glutAddSubMenu("Add object", objectMenu); + //glutAddMenuEntry("Position/Scale", POSITION_SCALE); + //glutAddMenuEntry("Rotation/Texture Scale", ROTATION_TEXTURE_SCALE); + //glutAddSubMenu("Material", materialMenu); + //glutAddSubMenu("Texture", textureMenu); + //glutAddSubMenu("Ground texture", gTextureMenu); + //glutAddSubMenu("Lights", lightMenu); glutAddMenuEntry("Exit", EXIT); // Bind to right mouse button glutAttachMenu(GLUT_RIGHT_BUTTON); } +/** + * Called when window is resized + * @param w New width + * @param h New height + */ +void windowReshape(int w, int h) { + GLdouble near = -10.0; + GLdouble far = 10.0; + + glViewport(0, 0, (GLsizei) w, (GLsizei) h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + if (w <= h) + glOrtho(near, far, near*(GLfloat)h/(GLfloat)w, + far*(GLfloat)h/(GLfloat)w, near, far); + else + glOrtho(near*(GLfloat)w/(GLfloat)h, + far*(GLfloat)w/(GLfloat)h, near, far, near, far); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +/** + * Called when mouse event occurs + * @param btn Mouse button + * @param state State of mouse button + * @param x Mouse x position + * @param y Mouse y position + */ +void mouse(int btn, int state, int x, int y) { + +} + +/** + * Draw a floor. + */ +void drawFloor() { + glDisable(GL_LIGHTING); + + //if (useTexture) { + // glEnable(GL_TEXTURE_2D); + //} + + glBegin(GL_QUADS); + glTexCoord2f(0.0, 0.0); + glVertex3fv(floorVertices[0]); + glTexCoord2f(0.0, 16.0); + glVertex3fv(floorVertices[1]); + glTexCoord2f(16.0, 16.0); + glVertex3fv(floorVertices[2]); + glTexCoord2f(16.0, 0.0); + glVertex3fv(floorVertices[3]); + glEnd(); + + /*if (useTexture) { + glDisable(GL_TEXTURE_2D); + }*/ + + glEnable(GL_LIGHTING); +} + +/** + * Display function + */ void display() { - // You probably want to change both of the following. - glClear(GL_COLOR_BUFFER_BIT); - glFlush(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); + gluLookAt( + 0.0, 180.0, 60.0, /* eye is at (x,y,z) */ + 0.0, 0.0, 0.0, /* center is at (x,y,z) */ + 0.0, 1.0, 0.0 /* up is in postivie Y direction */ + ); + + /* Reposition the light source. */ + lightPosition[0] = 12*cos(lightAngle); + lightPosition[1] = lightHeight; + lightPosition[2] = 12*sin(lightAngle); + lightPosition[3] = 0.0; + + glPushMatrix(); + glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + drawFloor(); + glDisable(GL_BLEND); + + glPushMatrix(); + glLoadIdentity(); + glTranslatef(0.0, 0.0, 0.0); + + glutSolidTeapot(2); // Draw teapot for test + glPopMatrix(); + + glPushMatrix(); + glDisable(GL_LIGHTING); + glLoadIdentity(); + glColor3f(1.0, 1.0, 1.0); + + /* Draw a yellow ball at the light source. */ + glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]); + glutSolidSphere(1.0, 5, 5); + + glEnable(GL_LIGHTING); + glPopMatrix(); + + glPopMatrix(); + + glutSwapBuffers(); } -char *dirDefault1 = "models-textures"; -char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures"; +/** + * init function; sets initial OpenGL state + */ +void init() { + glMatrixMode(GL_PROJECTION); + + gluPerspective( + 60.0, /* field of view in degree */ + 1.0, /* aspect ratio */ + 0.0, /* Z near */ + 900.0 /* Z far */ + ); + + glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1); + glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor); + glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1); + glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05); + glEnable(GL_LIGHT0); + glEnable(GL_LIGHTING); + + glMatrixMode(GL_MODELVIEW); +} + +/** + * Main function + * @param argc Number of arguments + * @param argv Array of arguments + * @return Program exit code + */ int main(int argc, char **argv) { + if(argc>1) + strcpy(dataDir, argv[1]); + else if(opendir(dirDefault1)) + strcpy(dataDir, dirDefault1); + else if(opendir(dirDefault2)) + strcpy(dataDir, dirDefault2); + else fileErr(dirDefault1); + + for(int i=0; i1) - strcpy(dataDir, argv[1]); - else if(opendir(dirDefault1)) - strcpy(dataDir, dirDefault1); - else if(opendir(dirDefault2)) - strcpy(dataDir, dirDefault2); - else fileErr(dirDefault1); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); - for(int i=0; i