摘录了计算机图形学书上的一个例子,嘿嘿,比想象的简单啊;
稍微修改下,算是3D入门吧;
#include "glut.h" void axis(double length) { glPushMatrix(); glBegin(GL_LINES); glVertex3d(0.0, 0.0, 0.0); glVertex3d(0.0, 0.0, length); glEnd(); glTranslated(0.0, 0.0, length-0.2) ; glutWireCone(0.04, 0.3, 8, 8) ; glPopMatrix(); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT) ; glMatrixMode(GL_PROJECTION) ; glLoadIdentity(); glOrtho(-2.0*64/48.0, 2.0*64/48.0, -2.0, 2.0, 0.1, 100) ; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(1.3,1.6,2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) ; // draw x-y-z coordinates axis(0.5); // z glPushMatrix(); glRotated(90.0,0, 1.0, 0); axis(0.5); //x glPopMatrix(); glPushMatrix(); glRotated(-90.0, 1.0, 0.0, 0.0); axis(0.5); //y glPopMatrix(); //draw cube glPushMatrix(); glTranslated(0.5,0.5,0.5); glutWireCube(1.0); glPopMatrix(); //draw teapot glPushMatrix(); glTranslated(1.0,1.0,0.0); glutWireTeapot(0.25); glPopMatrix(); //draw Sphere glPushMatrix(); glTranslated(1.0,0.0,0.0); glutWireSphere(0.25,12,8); glPopMatrix(); //draw Torus glPushMatrix(); glTranslated(0.0,1.0,0.0); glRotated(-90, 1.0, 0.0, 0.0); glutWireTorus(0.1,0.25,12,12); glPopMatrix(); //draw Dodecahedron glPushMatrix(); glTranslated(0.0,0.0,1.0); glScaled(0.577*0.25,0.577*0.25,0.577*0.25); glutWireDodecahedron(); glPopMatrix(); //draw cone glPushMatrix(); glTranslated(1.0,1.0,1.0); glRotated(-90,1.0, 0.0, 0.0); glutWireCone(0.15,0.3,8,8); glPopMatrix(); //draw cube glPushMatrix(); glTranslated(1.0,0.0,1.0); glutWireCube(0.3); glPopMatrix(); //draw cyliner glPushMatrix(); glTranslated(0.0, 1.0, 1.0); GLUquadricObj * quadricObj = gluNewQuadric(); gluQuadricDrawStyle(quadricObj,GLU_LINE); gluCylinder(quadricObj,0.2, 0.2, 0.3, 6, 6); glPopMatrix(); glFlush(); } void myInit(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glColor3f(0.0f, 1.0f, 0.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); } int main(int argc,char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640,480); glViewport(0,0, 640, 480) ; glutInitWindowPosition(100,150); glutCreateWindow("My3D"); glutDisplayFunc(myDisplay); myInit( ); glutMainLoop( ); }
上图: