(no commit message)
[atyndall/cits2231.git] / scene.c
diff --git a/scene.c b/scene.c
index 002fadc..5c9b14d 100644 (file)
--- a/scene.c
+++ b/scene.c
@@ -1,9 +1,7 @@
-// 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
@@ -105,13 +103,28 @@ const char *objectMenuEntries[NMESH] = {
 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
+/**\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
@@ -123,8 +136,11 @@ texture* loadTexture(char *fileName) {
     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
@@ -167,11 +183,13 @@ mesh* loadMesh(char* fileName) {
     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
@@ -184,13 +202,21 @@ void getMesh(int i) { // getMesh(i) loads mesh[i] if it isn't already loaded.
     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
@@ -216,6 +242,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\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
@@ -236,10 +266,10 @@ void processMainEvents(int id) {
   }\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
@@ -277,14 +307,10 @@ void processMaterialEvents(int id) {
   }\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
@@ -306,6 +332,30 @@ void processLightEvents(int id) {
   }\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
@@ -361,6 +411,9 @@ int makeSubmenuFromArray( const char *menuEntries[], unsigned int menuEntriesSiz
   return mainMenu;\r
 }\r
 \r
+/**\r
+ * Creates menu for program\r
+ */\r
 void makeMenu() {\r
   // Construct material menu\r
   int materialMenu = glutCreateMenu(processMaterialEvents);\r
@@ -390,29 +443,67 @@ void makeMenu() {
   int gTextureMenu = makeSubmenuFromArray( textureMenuEntries, textureMenuEntriesSize, processGTextureEvents );\r
 \r
   // Construct main menu\r
-  int mainMenu = 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
+  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("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 width New width\r
+ * @param height New height\r
+ */\r
+void windowReshape(int width, int height) {\r
+  glViewport(0, 0, (GLsizei)width, (GLsizei)height);\r
+  printf("Width: %d, height: %d\n", width, height);\r
+  glMatrixMode(GL_PROJECTION);\r
+  glLoadIdentity();\r
+  gluPerspective( (500* 60) / width, 1, 0.1, 1000.0);\r
+  glMatrixMode(GL_MODELVIEW);\r
+\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 btn, int state, int x, int y) {\r
+  \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
+\r
+   glTranslatef(0.0f,0.0f,-10.0f); // Move into the Screen 10.0\r
+\r
+   glutSolidTeapot(1);\r
+\r
+   glutSwapBuffers();\r
 }\r
 \r
-char *dirDefault1 = "models-textures"; \r
-char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures";\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
 \r
     if(argc>1)\r
@@ -426,12 +517,23 @@ int main(int argc, char **argv) {
     for(int i=0; i<NMESH; i++) meshes[i]=NULL;\r
     for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;\r
 \r
-    // The following is enough to run the program, but you'll\r
-    // need to change it significantly.\r
-\r
     glutInit(&argc, argv);\r
+\r
+    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);\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 Of Depth Test To Do\r
+\r
+    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);          // Really Nice Perspective Calculations\r
+\r
+    glutReshapeFunc(windowReshape);\r
     glutDisplayFunc(display);\r
+    glutMouseFunc(mouse);\r
 \r
     makeMenu();\r
     glutMainLoop();\r

UCC git Repository :: git.ucc.asn.au