-// compile this program using:\r
-// gcc -O3 -Wall -std=c99 -o scene scene.c bitmap.c -lglut\r
-// Or, with cygwin: (-mno-cygwin is only so the executable runs from windows)\r
-// gcc -mno-cygwin -O3 -Wall -std=c99 -o scene scene.c bitmap.c -lglut32 -lglu32 -lopengl32\r
-// Or, use make via the supplied Makefile: (For cygwin, install the package for make)\r
-// make\r
+/**\r
+ * CITS2231 Graphics Scene Editor\r
+ * @author Ashley Tyndall (20915779)\r
+ */\r
\r
#include <stdlib.h>\r
#include <stdio.h>\r
#include <math.h>\r
#include <GL/gl.h>\r
#include <GL/glut.h>\r
+#include <time.h>\r
\r
#include "bitmap.h"\r
\r
SceneObject sceneObjs[MAXOBJECTS]; // An array with details of the objects in a scene\r
int nObjects=0; // How many objects there are in the scene currently.\r
\r
+// Directories containing models\r
+char *dirDefault1 = "models-textures";\r
+char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures";\r
+\r
+char dataDir[200]; // Stores the directory name for the meshes and textures.\r
+\r
+static GLfloat lightColor[] = {1.0, 1.0, 1.0, 1.0}; // White light\r
+static GLfloat lightPosition[4];\r
+\r
+int moving, startx, starty;\r
+int lightMoving = 0, lightStartX, lightStartY;\r
+\r
+/* Time varying or user-controled variables. */\r
+static float jump = 0.0;\r
+static float lightAngle = 0.0, lightHeight = 5;\r
+GLfloat angle = -150; /* in degrees */\r
+GLfloat angle2 = 30; /* in degrees */\r
+\r
+/* Near and far parameters */\r
+GLfloat near = -100;\r
+GLfloat far = 100;\r
+\r
+/* Zoom factor for mouse movements */\r
+GLfloat zoomFactor = 1.0;\r
+\r
+/* Recursion level for floor drawing */\r
+int drawFloorRecurse = 5;\r
+\r
+/* Size of floor, from -n to n */\r
+int floorSize = 100;\r
+\r
+/* Light 0 parameters */\r
+GLfloat diffuse0[] = {1.0, 1.0, 1.0, 1.0};\r
+GLfloat ambient0[] = {1.0, 1.0, 1.0, 1.0};\r
+GLfloat specular0[] = {1.0, 1.0, 1.0, 1.0};\r
+GLfloat light0_pos[] ={ 1.0, 2.0, 3,0, 1.0};\r
+\r
+\r
+/**\r
+ * Prints out error message when file cannot be read\r
+ * @param fileName Name of file that could not be read\r
+ */\r
void fileErr(char* fileName) {\r
printf("Error reading file: %s\n", fileName);\r
printf("If not in the CSSE labs, you will need to include the directory containing\n");\r
printf("the models on the command line, or put it in the same folder as the exectutable.");\r
- exit(1);\r
+ exit(EXIT_FAILURE);\r
} \r
\r
+/**\r
+ * Reads .bmp texture files and converts them to a texture object\r
+ * @param fileName .bmp texture file\r
+ * @return texture object\r
+ */\r
texture* loadTexture(char *fileName) {\r
texture* t = malloc(sizeof (texture));\r
BITMAPINFO *info;\r
return t;\r
}\r
\r
-// The following works for the supplied .x files\r
-// but probably not for .x files from other sources.\r
+/**\r
+ * Reads .x files and converts them to a mesh object\r
+ * @param fileName .x mesh file\r
+ * @return mesh object\r
+ */\r
mesh* loadMesh(char* fileName) {\r
mesh* m = malloc(sizeof (mesh));\r
FILE* fp = fopen(fileName, "r");\r
return m;\r
}\r
\r
-char dataDir[200]; // Stores the directory name for the meshes and textures.\r
-\r
-// getMesh(i) loads mesh[i] if it isn't already loaded. \r
-// You must call getMesh(i) at least once before using mesh[i].\r
// [You may want to add to this function.]\r
+/**\r
+ * Loads mesh[i] if it isn't already loaded.\r
+ * You must call getMesh(i) at least once before using mesh[i].\r
+ *\r
+ * @param i Mesh ID\r
+ */\r
void getMesh(int i) { // getMesh(i) loads mesh[i] if it isn't already loaded. \r
char fileName[220];\r
if(i>=NMESH || i<0) {\r
meshes[i] = loadMesh(fileName);\r
}\r
\r
-// getTexture(i) loads texture i if it isn't already loaded.\r
-// After calling getTexture(i), you can make texture i the current texture using\r
-// glBindTexture(GL_TEXTURE_2D, i); (Use i=0 to return to the default plain texture.)\r
-// You can then scale the texture via: (See the textbook, section 8.8.3.)\r
-// glMatrixMode(GL_TEXTURE); \r
-// You must call getTexture(i) at least once before using texture i.\r
-void getTexture(int i) { // getTexture(i) loads texture i if it isn't already loaded.\r
+/**\r
+ * Loads texture i if it isn't already loaded\r
+ *\r
+ * After calling getTexture(i), you can make texture i the current texture using\r
+ * glBindTexture(GL_TEXTURE_2D, i);\r
+ * Use i=0 to return to the default plain texture.\r
+ *\r
+ * You can then scale the texture via:\r
+ * glMatrixMode(GL_TEXTURE);\r
+ * See the textbook, section 8.8.3.\r
+ *\r
+ * You must call getTexture(i) at least once before using texture i.\r
+ * @param i Texture ID\r
+ */\r
+void getTexture(int i) {\r
char fileName[220];\r
if(i<1 || i>NTEXTURE) {\r
printf("Error in getTexture - wrong texture number");\r
glBindTexture(GL_TEXTURE_2D, 0); // Back to default texture\r
}\r
\r
+/**\r
+ * Event hander for main menu events\r
+ * @param id ID of menu item selected\r
+ */\r
void processMainEvents(int id) {\r
switch (id) {\r
case ROTATE_MOVE_CAMERA:\r
}\r
}\r
\r
-void processObjectEvents(int id) {\r
-\r
-}\r
-\r
+/**\r
+ * Event hander for materials menu events\r
+ * @param id ID of menu item selected\r
+ */\r
void processMaterialEvents(int id) {\r
switch (id) {\r
case MATERIAL_ALL_RGB:\r
}\r
}\r
\r
-void processTextureEvents(int id) {\r
-\r
-}\r
-\r
-void processGTextureEvents(int id) {\r
-\r
-}\r
-\r
+/**\r
+ * Event hander for light menu events\r
+ * @param id ID of menu item selected\r
+ */\r
void processLightEvents(int id) {\r
switch (id) {\r
case LIGHT_MOVE_LIGHT_1:\r
}\r
}\r
\r
+/**\r
+ * Event hander for object menu events\r
+ * @param id ID of object selected\r
+ */\r
+void processObjectEvents(int id) {\r
+\r
+}\r
+\r
+/**\r
+ * Event hander for texture menu events\r
+ * @param id ID of texutre selected\r
+ */\r
+void processTextureEvents(int id) {\r
+\r
+}\r
+\r
+/**\r
+ * Event hander for ground texture menu events\r
+ * @param id ID of ground texture selected\r
+ */\r
+void processGTextureEvents(int id) {\r
+\r
+}\r
+\r
/**\r
* Rounds up numbers, from http://stackoverflow.com/questions/3407012/c-rounding-up-to-the-nearest-multiple-of-a-number\r
* @param numToRound Number to round\r
return mainMenu;\r
}\r
\r
+/**\r
+ * Creates menu for program\r
+ */\r
void makeMenu() {\r
// Construct material menu\r
int materialMenu = glutCreateMenu(processMaterialEvents);\r
\r
// Construct main menu\r
glutCreateMenu(processMainEvents);\r
- glutAddMenuEntry("Rotate/Move Camera", ROTATE_MOVE_CAMERA);\r
- glutAddSubMenu("Add object", objectMenu);\r
- glutAddMenuEntry("Position/Scale", POSITION_SCALE);\r
- glutAddMenuEntry("Rotation/Texture Scale", ROTATION_TEXTURE_SCALE);\r
- glutAddSubMenu("Material", materialMenu);\r
- glutAddSubMenu("Texture", textureMenu);\r
- glutAddSubMenu("Ground texture", gTextureMenu);\r
- glutAddSubMenu("Lights", lightMenu);\r
+ //glutAddMenuEntry("Rotate/Move Camera", ROTATE_MOVE_CAMERA);\r
+ //glutAddSubMenu("Add object", objectMenu);\r
+ //glutAddMenuEntry("Position/Scale", POSITION_SCALE);\r
+ //glutAddMenuEntry("Rotation/Texture Scale", ROTATION_TEXTURE_SCALE);\r
+ //glutAddSubMenu("Material", materialMenu);\r
+ //glutAddSubMenu("Texture", textureMenu);\r
+ //glutAddSubMenu("Ground texture", gTextureMenu);\r
+ //glutAddSubMenu("Lights", lightMenu);\r
glutAddMenuEntry("Exit", EXIT);\r
\r
// Bind to right mouse button\r
glutAttachMenu(GLUT_RIGHT_BUTTON);\r
}\r
\r
+/**\r
+ * Called when window is resized\r
+ * @param w New width\r
+ * @param h New height\r
+ */\r
+void windowReshape(int w, int h) {\r
+ glViewport(0, 0, (GLsizei) w, (GLsizei) h);\r
+ glMatrixMode(GL_PROJECTION);\r
+ glLoadIdentity();\r
+ if (w <= h) \r
+ glOrtho(near, far, near*(GLfloat)h/(GLfloat)w,\r
+ far*(GLfloat)h/(GLfloat)w, near, far);\r
+ else\r
+ glOrtho(near*(GLfloat)w/(GLfloat)h,\r
+ far*(GLfloat)w/(GLfloat)h, near, far, near, far);\r
+ glMatrixMode(GL_MODELVIEW); \r
+ glLoadIdentity();\r
+}\r
+\r
+/**\r
+ * Called when mouse event occurs\r
+ * @param btn Mouse button\r
+ * @param state State of mouse button\r
+ * @param x Mouse x position\r
+ * @param y Mouse y position\r
+ */\r
+void mouse(int button, int state, int x, int y) {\r
+ if (button == GLUT_LEFT_BUTTON) {\r
+ if (state == GLUT_DOWN) {\r
+ moving = 1;\r
+ startx = x;\r
+ starty = y;\r
+ }\r
+ if (state == GLUT_UP) {\r
+ moving = 0;\r
+ }\r
+ }\r
+ if (button == GLUT_MIDDLE_BUTTON) {\r
+ if (state == GLUT_DOWN) {\r
+ lightMoving = 1;\r
+ lightStartX = x;\r
+ lightStartY = y;\r
+ }\r
+ if (state == GLUT_UP) {\r
+ lightMoving = 0;\r
+ }\r
+ }\r
+}\r
+\r
+/**\r
+ * Called when motion event occurs\r
+ * @param x Mouse x position\r
+ * @param y Mouse y position\r
+ */\r
+void motion(int x, int y) {\r
+ if (moving) {\r
+ angle = angle + (x - startx);\r
+ angle2 = angle2 + (y - starty);\r
+ startx = x;\r
+ starty = y;\r
+ glutPostRedisplay();\r
+ }\r
+ if (lightMoving) {\r
+ lightAngle += (x - lightStartX)/40.0;\r
+ lightHeight += (lightStartY - y)/20.0;\r
+ lightStartX = x;\r
+ lightStartY = y;\r
+ glutPostRedisplay();\r
+ }\r
+}\r
+\r
+/**\r
+ * Recursive function to draw a square by drawing smaller and smaller\r
+ * divisions of the square, determined by drawFloorRecurse.\r
+ * @param recurseLevel Current level of recursion, only pass 0\r
+ * @param x1 top-left x\r
+ * @param z1 top-left z\r
+ * @param x2 bottom-left x\r
+ * @param z2 bottom-left z\r
+ */\r
+void drawSquare(int recurseLevel, float x1, float z1, float x2, float z2) {\r
+\r
+ if ( drawFloorRecurse != recurseLevel ) {\r
+ // Calculate middle points\r
+ float xm = (x1 + x2) / 2.0;\r
+ float zm = (z1 + z2) / 2.0;\r
+\r
+ // Increment recursion level\r
+ int rnew = recurseLevel + 1;\r
+\r
+ // Split into four sub-quads\r
+ drawSquare(rnew, x1, z1, xm, zm);\r
+ drawSquare(rnew, x1, zm, xm, z2);\r
+ drawSquare(rnew, xm, zm, x2, z2);\r
+ drawSquare(rnew, xm, z1, x2, zm);\r
+\r
+ } else {\r
+ // Draw square.\r
+ // **NOTE: Is the polygon facing in the right direction?\r
+ glBegin(GL_QUADS);\r
+ glVertex3f(x1, 0.0, z1);\r
+ glVertex3f(x1, 0.0, z2);\r
+ glVertex3f(x2, 0.0, z2);\r
+ glVertex3f(x2, 0.0, z1);\r
+ glEnd();\r
+ }\r
+\r
+}\r
+\r
+/**\r
+ * Draw a floor by calling the drawSquare recursion\r
+ */\r
+void drawFloor() {\r
+ drawSquare(0, -floorSize, -floorSize, floorSize, floorSize);\r
+}\r
+\r
+/**\r
+ * Display function\r
+ */\r
void display() {\r
- // You probably want to change both of the following.\r
- glClear(GL_COLOR_BUFFER_BIT);\r
- glFlush();\r
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\r
+ glLoadIdentity();\r
+ gluLookAt(\r
+ 0.0, 0.0, 10.0, /* eye is at (x,y,z) */\r
+ 0.0, 0.0, 0.0, /* center is at (x,y,z) */\r
+ 0.0, 1.0, 0.0 /* up is in postivie Y direction */\r
+ );\r
+\r
+ // **NOTE: Currently this rotation function is all that moves the camera off\r
+ // the flat surface. Need to integrate function into gluLookAt\r
+ glRotatef(30.0, 1.0, 0.0, 0.0);\r
+\r
+ /* Reposition the light source. */\r
+ lightPosition[0] = 12*cos(lightAngle);\r
+ lightPosition[1] = lightHeight;\r
+ lightPosition[2] = 12*sin(lightAngle);\r
+ lightPosition[3] = 0.0;\r
+\r
+ glPushMatrix();\r
+\r
+ /* Perform scene rotations based on user mouse input. */\r
+ glRotatef(angle, 0.0, 1.0, 0.0);\r
+ //glRotatef(angle2, 1.0, 0.0, 0.0); **NOTE: Only one degree of freedom\r
+\r
+ glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);\r
+\r
+ drawFloor();\r
+\r
+ // Draw teapot for a test object\r
+ glPushMatrix();\r
+ glTranslatef(0.0, 1.0, 0.0); // **NOTE: Teapot does not rest on surface\r
+ glutWireTeapot(1);\r
+ glPopMatrix();\r
+\r
+ // Draw a white ball over the light source\r
+ glPushMatrix();\r
+ glDisable(GL_LIGHTING);\r
+ glColor3f(1.0, 1.0, 1.0);\r
+ glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);\r
+ glutSolidSphere(1.0, 50, 50);\r
+ glEnable(GL_LIGHTING);\r
+ glPopMatrix();\r
+\r
+ glPopMatrix();\r
+\r
+ glutSwapBuffers();\r
}\r
\r
-char *dirDefault1 = "models-textures"; \r
-char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures";\r
+/**\r
+ * init function; sets initial OpenGL state\r
+ */\r
+void init() {\r
+ glMatrixMode(GL_PROJECTION);\r
+ glLoadIdentity();\r
+\r
+ gluPerspective(\r
+ 60.0, /* field of view in degree */\r
+ 1.0, /* aspect ratio */\r
+ near, /* Z near */\r
+ far /* Z far */\r
+ ); \r
+\r
+ glEnable(GL_LIGHT0);\r
+ glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);\r
+ glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);\r
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);\r
+ glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);\r
+ glEnable(GL_LIGHTING);\r
+\r
+ glMatrixMode(GL_MODELVIEW);\r
+ glLoadIdentity();\r
+}\r
+\r
+/**\r
+ * Main function\r
+ * @param argc Number of arguments\r
+ * @param argv Array of arguments\r
+ * @return Program exit code\r
+ */\r
int main(int argc, char **argv) {\r
+ if(argc>1)\r
+ strcpy(dataDir, argv[1]);\r
+ else if(opendir(dirDefault1))\r
+ strcpy(dataDir, dirDefault1);\r
+ else if(opendir(dirDefault2))\r
+ strcpy(dataDir, dirDefault2);\r
+ else fileErr(dirDefault1);\r
+\r
+ for(int i=0; i<NMESH; i++) meshes[i]=NULL;\r
+ for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;\r
+\r
+ glutInit(&argc, argv);\r
+\r
+ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);\r
+\r
+ glutInitWindowSize(500, 500);\r
+ glutCreateWindow("Scene Editor");\r
+\r
+ glShadeModel(GL_SMOOTH); // Enables Smooth Shading\r
+ glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background\r
+ glClearDepth(1.0f); // Depth Buffer Setup\r
+ glEnable(GL_DEPTH_TEST); // Enables Depth Testing\r
+ glDepthFunc(GL_LEQUAL); // the type\r
+ glEnable(GL_CULL_FACE);\r
+ glEnable(GL_TEXTURE_2D);\r
+ glLineWidth(1.0);\r
\r
- if(argc>1)\r
- strcpy(dataDir, argv[1]);\r
- else if(opendir(dirDefault1))\r
- strcpy(dataDir, dirDefault1);\r
- else if(opendir(dirDefault2))\r
- strcpy(dataDir, dirDefault2);\r
- else fileErr(dirDefault1);\r
+ glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);\r
\r
- for(int i=0; i<NMESH; i++) meshes[i]=NULL;\r
- for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;\r
+ glutReshapeFunc(windowReshape);\r
+ glutDisplayFunc(display);\r
+ glutMouseFunc(mouse);\r
+ glutMotionFunc(motion);\r
\r
- // The following is enough to run the program, but you'll\r
- // need to change it significantly.\r
+ makeMenu();\r
\r
- glutInit(&argc, argv);\r
- glutCreateWindow("Scene Editor");\r
- glutDisplayFunc(display);\r
+ init();\r
\r
- makeMenu();\r
- glutMainLoop();\r
+ glutMainLoop();\r
}\r