// Copyright (c) David Kabala // Fall 2009 #include class Vec3f{ public: Vec3f(GLfloat x, GLfloat y, GLfloat z) { _Data[0] = x; _Data[1] = y; _Data[2] = z; } const GLfloat *getdata(void) const { return _Data; } Vec3f operator+(const Vec3f& Right) const { return Vec3f(_Data[0] + Right._Data[0], _Data[1] + Right._Data[1], _Data[2] + Right._Data[2]); } Vec3f operator*(const GLfloat& Right) const { return Vec3f(_Data[0] * Right, _Data[1] * Right, _Data[2] * Right); } private: GLfloat _Data[3]; }; class SceneManager { protected: GLenum _DrawPrimitiveType; GLfloat _Translation[3]; GLfloat _Rotation[3]; bool _EnableDepthTest; public: SceneManager(void) : _DrawPrimitiveType(GL_POINTS), _EnableDepthTest(false) { _Rotation[0] = _Rotation[1] = _Rotation[2] = 0.0f; _Translation[0] = _Translation[1] = 0.0f; _Translation[2] = -4.0f; } void init(void) { glLineWidth(3.0f); } void reshape(int w, int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f,static_cast(w)/static_cast(h),.1f,100.0f); glMatrixMode(GL_MODELVIEW); glutPostRedisplay(); } void display(void) { if(_EnableDepthTest) { glEnable(GL_DEPTH_TEST); } else { glDisable(GL_DEPTH_TEST); } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(_Translation[0], _Translation[1], _Translation[2]); glRotatef(_Rotation[0],1.0f,0.0f,0.0f); glRotatef(_Rotation[1],0.0f,1.0f,0.0f); glRotatef(_Rotation[2],0.0f,0.0f,1.0f); GLfloat size(1.0); glBegin(GL_QUADS); //Red Square glColor3f(1.0,0.0,0.0); glVertex3f(-size, size, size); glVertex3f(-size, -size, size); glVertex3f(size, -size, size); glVertex3f(size, size, size); //Green Square glColor3f(0.0,1.0,0.0); glVertex3f(-size, size, -size); glVertex3f(-size, -size, -size); glVertex3f(size, -size, -size); glVertex3f(size, size, -size); //Blue Square glColor3f(0.0,0.0,1.0); glVertex3f(-size, size, 0.0f); glVertex3f(-size, -size, 0.0f); glVertex3f(size, -size, 0.0f); glVertex3f(size, size, 0.0f); glEnd(); //Vec3f origin(-size-.0001,-size-.0001,-size-.0001); Vec3f origin(0.0,0.0,0.0); drawAxis(origin,Vec3f(1.0,0.0,0.0), Vec3f(1.0,0.0,0.0),2.0f); drawAxis(origin,Vec3f(0.0,1.0,0.0), Vec3f(0.0,1.0,0.0),2.0f); drawAxis(origin,Vec3f(0.0,0.0,1.0), Vec3f(0.0,0.0,1.0),2.0f); glPopMatrix(); glutSwapBuffers(); } void drawAxis(Vec3f origin, Vec3f direction, Vec3f color, GLfloat Length) { glBegin(GL_LINES); glColor3fv(color.getdata()); glVertex3fv(origin.getdata()); Vec3f v(origin + (direction * Length)); glVertex3fv(v.getdata()); glEnd(); } void keyboard(unsigned char key, int x, int y) { switch(key) { case 'a': _Rotation[1] -= 3.5; break; case 'd': _Rotation[1] += 3.5; break; case 'w': _Rotation[0] -= 3.5; break; case 's': _Rotation[0] += 3.5; break; case 'z': //Toggle Depth Test _EnableDepthTest = !_EnableDepthTest; break; } glutPostRedisplay(); } }; SceneManager mgr; void reshape(int w, int h) { mgr.reshape(w,h); } void display(void) { mgr.display(); } void keyboard(unsigned char key, int x, int y) { mgr.keyboard(key,x,y); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Depth Test"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutReshapeWindow(800,600); mgr.init(); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }