#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]; unsigned int _Example; bool _EnableBlending; public: SceneManager(void) : _DrawPrimitiveType(GL_POINTS),_Example(1),_EnableBlending(false) { _Rotation[0] = _Rotation[1] = _Rotation[2] = 0.0f; _Translation[0] = _Translation[1] = 0.0f; _Translation[2] = -4.0f; } void init(void) { glEnable(GL_DEPTH_TEST); //glEnable(GL_CULL_FACE); //glCullFace(GL_BACK); glLineWidth(3.0f); glPointSize(18.0f); glClearColor(0.0,0.0,0.0,0.0); } 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) { 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); switch(_Example) { case 4: antialiasPolygons(); break; case 3: antialiasPointsExample(); break; case 2: antialiasLinesExample(); break; case 1: default: transparencyExample(); break; } glPopMatrix(); glutSwapBuffers(); } void transparencyExample(void) { Vec3f origin(0.0,0.0,0.0); drawAxis(origin,Vec3f(1.0,0.0,0.001), Vec3f(1.0,0.0,0.001),2.0f); drawAxis(origin,Vec3f(0.0,1.0,0.001), Vec3f(0.0,1.0,0.001),2.0f); drawAxis(origin,Vec3f(0.0,0.0,1.0), Vec3f(0.0,0.0,1.0),2.0f); if(_EnableBlending) { glEnable(GL_BLEND); } else { glDisable(GL_BLEND); } glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); GLfloat size(1.0); glBegin(GL_QUADS); //Green Square glColor4f(0.0,1.0,0.0,0.5); glVertex3f(-size, size, -size); glVertex3f(-size, -size, -size); glVertex3f(size, -size, -size); glVertex3f(size, size, -size); //Blue Square glColor4f(0.0,0.0,1.0,0.5); glVertex3f(-size, size, 0.0f); glVertex3f(-size, -size, 0.0f); glVertex3f(size, -size, 0.0f); glVertex3f(size, size, 0.0f); //Red Square glColor4f(1.0,0.0,0.0,0.5); glVertex3f(-size, size, size); glVertex3f(-size, -size, size); glVertex3f(size, -size, size); glVertex3f(size, size, size); glEnd(); } void antialiasLinesExample(void) { glEnable(GL_LINE_SMOOTH); if(_EnableBlending) { glEnable(GL_BLEND); } else { glDisable(GL_BLEND); } glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); GLfloat size(1.0); glBegin(GL_LINES); //Green Square glColor4f(0.0,1.0,0.0,1.0); glVertex3f(-size, size, -size); glVertex3f(-size, -size, -size); glVertex3f(size, -size, -size); glVertex3f(size, size, -size); //Blue Square glColor4f(0.0,0.0,1.0,1.0); glVertex3f(-size, size, 0.0f); glVertex3f(-size, -size, 0.0f); glVertex3f(size, -size, 0.0f); glVertex3f(size, size, 0.0f); //Red Square glColor4f(1.0,0.0,0.0,1.0); glVertex3f(-size, size, size); glVertex3f(-size, -size, size); glVertex3f(size, -size, size); glVertex3f(size, size, size); glEnd(); } void antialiasPointsExample(void) { glEnable(GL_POINT_SMOOTH); if(_EnableBlending) { glEnable(GL_BLEND); } else { glDisable(GL_BLEND); } glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); GLfloat size(1.0); glBegin(GL_POINTS); //Green Square glColor4f(0.0,1.0,0.0,1.0); glVertex3f(-size, size, -size); glVertex3f(-size, -size, -size); glVertex3f(size, -size, -size); glVertex3f(size, size, -size); //Blue Square glColor4f(0.0,0.0,1.0,1.0); glVertex3f(-size, size, 0.0f); glVertex3f(-size, -size, 0.0f); glVertex3f(size, -size, 0.0f); glVertex3f(size, size, 0.0f); //Red Square glColor4f(1.0,0.0,0.0,1.0); glVertex3f(-size, size, size); glVertex3f(-size, -size, size); glVertex3f(size, -size, size); glVertex3f(size, size, size); glEnd(); } void antialiasPolygons(void) { if(_EnableBlending) { glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); } else { glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); } glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST); glEnable(GL_POLYGON_SMOOTH); glBlendFunc(GL_SRC_ALPHA_SATURATE,GL_ONE); GLfloat size(1.0); glBegin(GL_QUADS); //Green Square glColor4f(0.0,1.0,0.0,1.0); glVertex3f(-size, size, -size); glVertex3f(-size, -size, -size); glVertex3f(size, -size, -size); glVertex3f(size, size, -size); //Blue Square glColor4f(0.0,0.0,1.0,1.0); glVertex3f(-size, size, 0.0f); glVertex3f(-size, -size, 0.0f); glVertex3f(size, -size, 0.0f); glVertex3f(size, size, 0.0f); //Red Square glColor4f(1.0,0.0,0.0,1.0); glVertex3f(-size, size, size); glVertex3f(-size, -size, size); glVertex3f(size, -size, size); glVertex3f(size, size, size); glEnd(); } 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': _EnableBlending = !_EnableBlending; break; case '1': _Example = 1; break; case '2': _Example = 2; break; case '3': _Example = 3; break; case '4': _Example = 4; 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 | GLUT_ALPHA); glutCreateWindow("Double Buffering"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutReshapeWindow(800,600); mgr.init(); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }