(no commit message)
[atyndall/cits2231.git] / scene.c
diff --git a/scene.c b/scene.c
index c546975..1aef061 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
@@ -12,6 +10,7 @@
 #include <math.h>\r
 #include <GL/gl.h>\r
 #include <GL/glut.h>\r
+#include <time.h>\r
 \r
 #include "bitmap.h"\r
 \r
@@ -111,6 +110,25 @@ char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures";
 \r
 char dataDir[200];  // Stores the directory name for the meshes and textures.\r
 \r
+static GLfloat floorVertices[4][3] = {\r
+  { -1000.0, 0.0, 1000.0 },\r
+  { 1000.0, 0.0, 1000.0 },\r
+  { 1000.0, 0.0, -1000.0 },\r
+  { -1000.0, 0.0, -1000.0 },\r
+};\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 = 20;\r
+GLfloat angle = -150;   /* in degrees */\r
+GLfloat angle2 = 30;   /* in degrees */\r
+\r
 /**\r
  * Prints out error message when file cannot be read\r
  * @param fileName Name of file that could not be read\r
@@ -460,13 +478,228 @@ void makeMenu() {
   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
+  GLdouble near = -1000.0;\r
+  GLdouble far = 1000.0;\r
+\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 btn, int state, int x, int y) {\r
+  \r
+}*//*\r
+static void\r
+mouse(int button, int state, int x, int y)\r
+{\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
+static void\r
+motion(int x, int y)\r
+{\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
+void idle() {\r
+  angle = (int)(angle + 10) % 360;\r
+    //angle2 = (int)(angle2 + 10) % 360;\r
+    printf("Angle 1: %f, Angle 2: %f\n", angle, angle2);\r
+    sleep(25);\r
+    glutPostRedisplay();\r
+}\r
+\r
+/**\r
+ * Draw a floor.\r
+ */\r
+void drawFloor() {\r
+  glDisable(GL_LIGHTING);\r
+\r
+  //if (useTexture) {\r
+  //  glEnable(GL_TEXTURE_2D);\r
+  //}\r
+\r
+  /*glBegin(GL_QUADS);\r
+    glTexCoord2f(0.0, 0.0);\r
+    glVertex3fv(floorVertices[0]);\r
+    glTexCoord2f(0.0, 16.0);\r
+    glVertex3fv(floorVertices[1]);\r
+    glTexCoord2f(16.0, 16.0);\r
+    glVertex3fv(floorVertices[2]);\r
+    glTexCoord2f(16.0, 0.0);\r
+    glVertex3fv(floorVertices[3]);\r
+  glEnd();*/\r
+\r
+drawSquare(0, -1000, -1000, 1000, 1000);\r
+\r
+  /*if (useTexture) {\r
+    glDisable(GL_TEXTURE_2D);\r
+  }*/\r
+\r
+  glEnable(GL_LIGHTING);\r
+}\r
+\r
+int drawFloorRecurse = 2;\r
+/*void drawSquare(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) {\r
+  drawSquare(0, x1, y1, x2, y2);\r
+}*/\r
+\r
+void drawSquare(int recurseLevel, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) {\r
+\r
+  \r
+\r
+  if ( drawFloorRecurse != recurseLevel ) {\r
+    GLfloat xm = (x1 + x2) / 2.0;\r
+    GLfloat ym = (y1 + y2) / 2.0;\r
+    int rnew = recurseLevel + 1;\r
+    printf("Recursing to level %d\n", rnew);\r
+\r
+    // Split into four sub-quads\r
+    drawSquare(rnew, x1, y1, xm, ym);\r
+    drawSquare(rnew, x1, ym, xm, y2);\r
+    drawSquare(rnew, xm, ym, x2, y2);\r
+    drawSquare(rnew, xm, y1, x2, ym);\r
+    \r
+  } else {\r
+    printf("Drawing (%f.10, %f.10) -> (%f.10, %f.10)\n", x1, y2, x2, y2);\r
+    glBegin(GL_QUADS);\r
+      glVertex3f(x1, 0.0, y1);\r
+      glVertex3f(x1, 0.0, y2);\r
+      glVertex3f(x2, 0.0, y2);\r
+      glVertex3f(x2, 0.0, y1);\r
+    glEnd();\r
+  }\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
+  gluLookAt(\r
+    0.0, 0.0, 60.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
+  /* 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
+      /* Perform scene rotations based on user mouse input. */\r
+    glRotatef(angle2, 1.0, 0.0, 0.0);\r
+    glRotatef(angle, 0.0, 1.0, 0.0);\r
+\r
+    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);\r
+\r
+    glEnable(GL_BLEND);\r
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
+    drawFloor();\r
+    glDisable(GL_BLEND);\r
+\r
+    glPushMatrix();\r
+    \r
+      //glTranslatef(0.0, 0.0, 0.0);\r
+      glutWireTeapot(30); // Draw teapot for test\r
+    glPopMatrix();\r
+\r
+    glPushMatrix();\r
+      glDisable(GL_LIGHTING);\r
+      glColor3f(1.0, 1.0, 1.0);\r
+\r
+      /* Draw a yellow ball at the light source. */\r
+      glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);\r
+      glutSolidSphere(1.0, 5, 5);\r
+\r
+      glEnable(GL_LIGHTING);\r
+    glPopMatrix();\r
+\r
+  glPopMatrix();\r
+\r
+  glutSwapBuffers();\r
+}\r
+\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
+    -1000.0,  /* Z near */\r
+     1000.0   /* Z far */\r
+    );    \r
+\r
+  glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);\r
+  glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);\r
+  glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1);\r
+  glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);\r
+  glEnable(GL_LIGHT0);\r
+  glEnable(GL_LIGHTING);\r
+\r
+  glMatrixMode(GL_MODELVIEW);\r
+  glLoadIdentity();\r
 }\r
 \r
 /**\r
@@ -476,25 +709,44 @@ void display() {
  * @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
+  glutIdleFunc(idle);\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

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