+/**\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