第一个opengl程序
程序代码:
#include <GL/glut.h> GLfloat thera=0; GLfloat t=360; void myDisplay() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glutSolidCube(0.5); //glut中的二次曲面 //glutWireSphere(10,20,20); //glutWireTorus(3,10,20,20); //任意固定点的旋转 //glTranslatef(3,0,0); //glRotatef(90,0,1,0); //glTranslatef(-3,0,0); //glutWireTeapot(3); //旋转动画生成 glPushMatrix(); glRotatef(thera,0.0,1.0,0.0); //缩放 //glScalef(1.0,1.0,1.0); glutWireCube(2); glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,0.6); glRotatef(t,0.0,0.5,0.5); //缩放 //glScalef(1.0,1.0,1.0); glutWireCube(2); glPopMatrix(); glColor3f(0.0,0.4,0.2); glBegin(GL_LINES); glVertex3i(0,-5,0); glVertex3i(0,5,0); glEnd(); //二次曲面 //GLUquadricObj *obj=gluNewQuadric(); //gluQuadricDrawStyle(obj,GLU_LINE); //gluSphere(obj,10,20,20); //gluCylinder(obj,10,5,7,15,15); //gluDisk(obj,3,10,20,15); //glFlush(); glutSwapBuffers(); } void init() { glEnable(GL_DEPTH_TEST); glClearColor(1.0,1.0,1.0,0.0); glColor3f(0.0,0.0,0.0); /*glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-10.0,10.0,-10.0,10.0,-10.0,50.0);*/ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-5.0,5.0,-5.0,5.0,12,20.0); //gluPerspective(90,1,1.0,50.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(10.0,10.0,10.0,0.0,0.0,0.0,0.0,1.0,0.0); } /*void lineSegment(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0,0.4,0.2); glBegin(GL_LINES); glVertex2i(180,15); glVertex2i(10,145); glEnd(); glFlush(); }*/ void idle() { if (thera<360) thera+=0.1; else thera-=360; if (t>0) t-=0.1; else t+=360; glutPostRedisplay(); } //在指定区域显示完整图形 /*void reshape(int w,int h) { glViewport(0,0,500,500); }*/ void main(int argc,char**argv) { glutInit(&argc,argv); //GLUT初始化 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //初始化显示模式 glutInitWindowPosition(50,50); //初始化windows窗口位置 glutInitWindowSize(500,500); //初始化windows窗口尺寸 glutCreateWindow("simple"); //创建窗口 //glutReshapeFunc(reshape); glutIdleFunc(idle); glutDisplayFunc(myDisplay); //注册绘制函数 init(); glutMainLoop(); //消息处理循环 }