业精于勤,荒于嬉
全部博文(763)
分类: C/C++
2010-05-23 23:01:04
#include #include void background(void) { glClearColor(0.0,0.0,0.0,0.0);//设置背景颜色为黑色 } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT);//buffer设置为颜色可写 glBegin(GL_TRIANGLES);//开始画三角形 glShadeModel(GL_SMOOTH);//设置为光滑明暗模式 glColor3f(1.0,0.0,0.0);//设置第一个顶点为红色 glVertex2f(-1.0,-1.0);//设置第一个顶点的坐标为(-1.0,-1.0) glColor3f(0.0,1.0,0.0);//设置第二个顶点为绿色 glVertex2f(0.0,-1.0);//设置第二个顶点的坐标为(0.0,-1.0) glColor3f(0.0,0.0,1.0);//设置第三个顶点为蓝色 glVertex2f(-0.5,1.0);//设置第三个顶点的坐标为(-0.5,1.0) glEnd();//三角形结束 glFlush();//强制OpenGL函数在有限时间内运行 } void myReshape(GLsizei w,GLsizei h) { glViewport(0,0,w,h);//设置视口 glMatrixMode(GL_PROJECTION);//指明当前矩阵为GL_PROJECTION glLoadIdentity();//将当前矩阵置换为单位阵 if(w <= h) gluOrtho2D(-1.0,1.5,-1.5,1.5*(GLfloat)h/(GLfloat)w);//定义二维正视投影矩阵 else gluOrtho2D(-1.0,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5); glMatrixMode(GL_MODELVIEW);//指明当前矩阵为GL_MODELVIEW } int main(int argc,char ** argv) { /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(200,200); /*创建窗口*/ glutCreateWindow("Triangle"); /*绘制与显示*/ background(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop(); return(0); } |
图一:一个彩色的三角形 |
例2:本例子使用颜色引索模式绘制8个不同颜色的球体,如图二所示。阅读此例时,请主要关注函数palette和DrawColotFans。
glIndex设置当前颜色索引。参数为当前颜色索引。本例中glIndexd 函数的参数j+1对应palette中auxSetOneColor函数中的i+1,auxSetOneColor函数的后三个函数制定对应的颜色,颜色值由变量rgb[8][3]定义。
#include #include void init(void) { glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glShadeModel(GL_SMOOTH); } void palette(void) { GLint i; static GLfloat rgb[8][3]={{1,0,0},{1,0,0.5},{1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0.5,0}}; for(i = 0;i<8;i++) { auxSetOneColor(i+1,rgb[i][0],rgb[i][1],rgb[i][2]);//设置颜色 } } void DrawColorFans(void) { GLint j; glTranslatef(-15,-15,0); for(j = 0;j<8;j++) { glIndexd(j+1);//设置当前颜色索引 /*在不同位置绘制球体*/ glTranslatef(j,j-1,0); glutSolidSphere(1,20,20); } } void CALLBACK display(void) { palette(); DrawColorFans(); glFlush(); } void CALLBACK reshape(GLsizei w,GLsizei h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(100,1,1,20); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-15); } void main() { auxInitDisplayMode(AUX_SINGLE|AUX_INDEX); auxInitPosition(100,100,500,500); auxInitWindow("draw the color sphere"); init(); auxReshapeFunc(reshape); auxMainLoop(display); } |
图二:8个不同颜色的球体 |
例3:下面这个例子不仅说明了附加裁剪函数的用法,而且调用了gluPerspective()透视投影函数,读者可以细细体会其中的用法。例程如下:
#include "glos.h" #include #include #include void myinit(void); void CALLBACK myReshape(GLsizei w, GLsizei h); void CALLBACK display(void); void CALLBACK display(void) { GLdouble eqn[4] = {1.0, 0.0, 0.0, 0.0}; glClear(GL_COLOR_BUFFER_BIT); glColor3f (1.0, 0.0, 1.0); glPushMatrix(); glTranslatef (0.0, 0.0, -5.0); /* clip the left part of wire_sphere : x<0 */ glClipPlane (GL_CLIP_PLANE0, eqn); glEnable (GL_CLIP_PLANE0); glRotatef (-90.0, 1.0, 0.0, 0.0); auxWireSphere(1.0); glPopMatrix(); glFlush(); } void myinit (void) { glShadeModel (GL_FLAT); } void CALLBACK myReshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); } void main(void) { auxInitDisplayMode (AUX_SINGLE | AUX_RGB); auxInitPosition (0, 0, 500, 500); auxInitWindow ("Arbitrary Clipping Planes"); myinit (); auxReshapeFunc (myReshape); auxMainLoop(display); } |
例4:利用堆栈绘制三个物体--一个绿色的茶壶,一个蓝色的茶壶,一个红色的立方体(部分代码)
void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); /*蓝色茶壶*/ glPushMatrix(); glColor3f(0.0,0.0,1.0); glutSolidTeapot(1.5); glPopMatrix(); /*红色立方体*/ glPushMatrix(); glTranslatef(5.0,0.0,0.0);//坐标变换 glColor3f(1.0,0.0,0.0); glutSolidCube(1.0); glPopMatrix(); /*绿色茶壶*/ glPushMatrix(); glTranslatef(-5.0,0.0,0.0);//坐标变换 glColor3f(0.0,1.0,0.0); glutSolidTeapot(1.0); glPopMatrix(); glFlush(); } void myReshape(GLsizei w,GLsizei h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(80.0,(GLdouble)w/(GLdouble)h,1.0,20.0);//创建透视投影矩阵 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0,0.0,-8.0); } |
例5:绘制六个彩色的三角形,如图六。
#include #include GLuint listName = 1; void myInit(void) { glClearColor(0.0,0.0,0.0,0.0);//设置背景为黑色 glNewList(listName,GL_COMPILE);//创建显示列表 /*画一个彩色的多边形*/ glBegin(GL_POLYGON); glColor3f(1.0,0.0,0.0); glVertex2f(1.0,1.0); glColor3f(0.0,1.0,0.0); glVertex2f(2.0,2.0); glColor3f(0.0,0.0,1.0); glVertex2f(1.5,2.5); glTranslatef(0.5,-0.5,0.0);//坐标转换 glEnd(); glEndList();//结束显示列表 glShadeModel(GL_SMOOTH); } void myDisplay(void) { GLuint i; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); for(i = 0;i<6;i++) glCallList(listName); glFlush(); } void myReshape(GLsizei w,GLsizei h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<=h) glOrtho(-4.0,4.0,-4.0*(GLfloat)h/(GLfloat)w,4.0*(GLfloat)h/(GLfloat)w,-8.0,8.0); else glOrtho(-4.0,4.0*(GLfloat)w/(GLfloat)h,-4.0,4.0,-8.0,8.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(-4.0,0.0,-3.0); } int main(int argc,char ** argv) { /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(500,500); glutInitWindowPosition(100,200); /*创建窗口*/ glutCreateWindow("display list"); /*绘制与显示*/ myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); /*进入GLUT事件处理循环*/ glutMainLoop(); return(0); } |
例6:绘制三个使用不同材质的球体。
#include #include GLfloat light_position[] = {0.0,3.0,6.0,0.0}; GLfloat no_mat[] = {0.0,0.0,0.0,1.0}; GLfloat mat_grey_ambient[] = {0.5,0.5,0.5,1.0}; GLfloat mat_red_ambient[] = {0.0,0.0,1.0,1.0}; GLfloat mat_diffuse[] = {0.8,0.2,0.5,1.0}; GLfloat mat_specular[] = {1.0,1.0,1.0,1.0}; GLfloat no_shininess[] = {0.0}; GLfloat low_shininess[] = {5.0}; GLfloat high_shininess[] = {100.0}; GLfloat mat_emission[] = {0.3,0.2,0.2,0.0}; void myInit(void) { glLightfv(GL_LIGHT2,GL_POSITION,light_position);//设置光源 glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS);//指定深度比较中使用的数值 glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glShadeModel(GL_SMOOTH); } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); /*为光照模型指定材质参数*/ glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); glMaterialfv(GL_FRONT,GL_SHININESS,high_shininess); glMaterialfv(GL_FRONT,GL_EMISSION,no_mat); glColorMaterial(GL_FRONT,GL_AMBIENT);//使材质色跟踪当前颜色 glEnable(GL_COLOR_MATERIAL); /*第一个球形*/ glPushMatrix(); glColor3fv(no_mat); glTranslatef(-2.5,1.5,0.0); glRotatef(15.0,1.0,0.0,0.0); glutSolidSphere(1.2,20.0,20.0); glPopMatrix(); /*第二个球形*/ glPushMatrix(); glColor3fv(mat_grey_ambient); glRotatef(15.0,1.0,0.0,0.0); glutSolidSphere(1.2,20.0,20.0); glPopMatrix(); /*第三个球形*/ glPushMatrix(); glColor3fv(mat_red_ambient); glTranslatef(2.5,-1.5,0.0); glRotatef(15.0,1.0,0.0,0.0); glutSolidSphere(1.2,20.0,20.0); glPopMatrix(); glDisable(GL_COLOR_MATERIAL); glPopMatrix(); glFlush(); } void myReshape(int w,int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w <= h) glOrtho(-5.5,5.5,-5.5*(GLfloat)h/(GLfloat)w,5.5*(GLfloat)h/(GLfloat)w,-5.5,5.5); else glOrtho(-5.5*(GLfloat)w/(GLfloat)h,5.5*(GLfloat)w/(GLfloat)h,-5.5,5.5,-5.5,5.5); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc,char ** argv) { /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); /*创建窗口*/ glutCreateWindow(" Light&Material "); /*绘制与显示*/ myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutMainLoop(); return 0; } |
例7:绘制一个简单的二维纹理图,并将该图像映射到一个四边形上。
#include #include #define imageWidth 64 #define imageHeight 64 GLubyte image[imageWidth][imageHeight][3]; /*绘制一个简单的二维纹理图*/ void makeImage(void) { int i,j,r,g,b; /*根据点的位置设置不同的颜色*/ for(i = 0;i < imageWidth;i++) { for(j = 0;j r = (i*j)%255; g = (i*i)%255; b = (j*j)%255; image[i][j][0] = (GLubyte)r; image[i][j][1] = (GLubyte)g; image[i][j][2] = (GLubyte)b; } } } void myInit(void) { glClearColor(0.0,0.0,0.0,0.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); makeImage(); glPixelStorei(GL_UNPACK_ALIGNMENT,1); /*指定二维纹理映射*/ glTexImage2D(GL_TEXTURE_2D,0,3,imageWidth,imageHeight,0,GL_RGB,GL_UNSIGNED_BYTE,&image[0][0][0]); /*设置纹理参数*/ glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); /*设置纹理环境参数*/ glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL); glEnable(GL_TEXTURE_2D); glShadeModel(GL_FLAT); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); /*将纹理映射到四边形上*/ glBegin(GL_QUADS); /*纹理的坐标和四边形顶点的对应*/ glTexCoord2f(0.0,0.0); glVertex3f(-1.0,1.0,0.0); glTexCoord2f(0.0,1.0); glVertex3f(-1.0,-1.0,0.0); glTexCoord2f(1.0,1.0); glVertex3f(1.0,-1.0,0.0); glTexCoord2f(1.0,0.0); glVertex3f(1.0,1.0,0.0); glEnd(); glFlush(); } void myReshape(int w,int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(80.0,1.0-(GLfloat)w/(GLfloat)h,1.0,30.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc,char **argv) { /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(300,300); glutInitWindowPosition(200,200); /*创建窗口*/ glutCreateWindow(" Texture "); /*绘制与显示*/ myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop(); return 0; } |
例8:绘制三个互相重叠的矩形,这三个矩形从左到右分别为绿、红、蓝。其中红色、蓝色矩形的透明度分别为50%的透明度,即alpha值为0.5,如图九所示。
#include #include void myInit(void) { glClearColor(0.2,0.8,0.8,0.0);//将背景设置为湖蓝色 glEnable(GL_BLEND);//激活GL_BLEND glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);//指定像素的数学算法 glEnable(GL_FLAT); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); /*绘制一个绿色的矩形 alpha = 1.0*/ glColor4f(0.0,1.0,0.0,1.0); glBegin(GL_POLYGON); glVertex3f(-0.75,0.5,0.0); glVertex3f(-0.75,-0.75,0.0); glVertex3f(0.5,-0.75,0.0); glVertex3f(0.5,0.5,0.0); glEnd(); /*绘制一个红色的矩形 alpha = 0.5*/ glColor4f(1.0,0.0,0.0,0.5); glBegin(GL_POLYGON); glVertex3f(-0.25,1.0,0.0); glVertex3f(-0.25,-0.25,0.0); glVertex3f(1.0,-0.25,0.0); glVertex3f(1.0,1.0,0.0); glEnd(); /*绘制一个蓝色的矩形 alpha = 0.5*/ glColor4f(0.0,0.0,1.0,0.5); glBegin(GL_POLYGON); glVertex3f(0.25,1.5,0.0); glVertex3f(0.25,0.25,0.0); glVertex3f(1.5,0.25,0.0); glVertex3f(1.5,1.5,0.0); glEnd(); glFlush(); } void myReshape(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w else glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-1.5,1.5); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(-0.4,0.0,0.0); } int main(int argc,char ** argv) { /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(300,400); glutInitWindowPosition(200,200); /*创建窗口*/ glutCreateWindow("BLEND POLYGON"); /*绘制与显示*/ myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); /*进入GLUT事件处理循环*/ glutMainLoop(); return(0); } |
图九:三个互相重叠的矩形 |
例9:绘制一个被雾化的圆锥体,如图十所示,为了观察不同的雾化参数,程序中加入了键盘操作。"shift+1"," shift+2"键分别增加和减弱雾化浓度,"shift+3"设置雾化的起始点和终止点,"shift+4"和"shift+5"键改变雾化方程,"shift+6"将雾化颜色由白改为绿色,如图十一所示。
#include #include GLfloat light_ambient[] = {0.1,0.1,0.1,0.0}; GLfloat light_diffuse[] = {1.0,1.0,1.0,0.0}; GLfloat light_specular[] = {1.0,1.0,1.0,0.0}; GLfloat light_position[] = {-10.0,0.0,5.0,0.0}; GLfloat material_ambient[] = {0.1745,0.01175,0.01175}; GLfloat material_diffuse[] = {0.61424,0.04136,0.04136}; GLfloat material_specular[] = {0.727811,0.626959,0.62659}; GLfloat fogColorWhite[] = {1.0,1.0,1.0,1.0}; GLfloat fogColorGreen[] = {0.0,1.0,0.0,1.0}; float fogDensity = 0.02; void myInit(void) { /*设置背景色*/ glClearColor(0.5,0.5,0.5,1.0); /*设置光照*/ glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse); glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular); glLightfv(GL_LIGHT0,GL_POSITION,light_position); /*设置材质*/ glMaterialfv(GL_FRONT,GL_AMBIENT,material_ambient); glMaterialfv(GL_FRONT,GL_DIFFUSE,material_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,material_specular); glMaterialf(GL_FRONT,GL_SHININESS,0.6*128); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glFrontFace(GL_CW); /*设置雾化*/ glEnable(GL_FOG); glFogi(GL_FOG_MODE,GL_LINEAR); glFogfv(GL_FOG_COLOR,fogColorWhite); glFogf(GL_FOG_DENSITY,fogDensity); glFogf(GL_FOG_START,0.0); glFogf(GL_FOG_END,15.0); glHint(GL_FOG_HINT,GL_DONT_CARE); } /*根据不同的键值设置不同的雾化效果*/ static void myKey(unsigned char key,int x,int y) { switch(key) { case 33: //shift+1 fogDensity *= 1.10; glFogi(GL_FOG_MODE,GL_EXP); glFogf(GL_FOG_DENSITY,fogDensity); glutPostRedisplay(); break; case 64: //shift+2 fogDensity /= 1.10; glFogi(GL_FOG_MODE,GL_EXP); glFogf(GL_FOG_DENSITY,fogDensity); glutPostRedisplay(); break; case 35: //shift+3 glFogi(GL_FOG_START,0.0); glFogi(GL_FOG_END,8.0); glutPostRedisplay(); break; case 36: //shift+4 glFogi(GL_FOG_MODE,GL_EXP2); glutPostRedisplay(); break; case 37: //shift+5 glFogi(GL_FOG_MODE,GL_LINEAR); glutPostRedisplay(); break; case 94: //shift+6 glFogfv(GL_FOG_COLOR,fogColorGreen); glutPostRedisplay(); break; case 27: //Esc exit(0); default: break; } } /*绘制圆锥体*/ void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(-4.0,4.0,0.0); glRotatef(30.0,1.0,1.0,0.0); glutSolidCone(1.0,50.0,20.0,20.0); glPopMatrix(); glutSwapBuffers(); } void myReshape(int w,int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(100.0,1.0,1.0,20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0,0.0,-15.5); } int main(int argc,char ** argv) { /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(300,300); glutInitWindowPosition(100,100); /*创建窗口*/ glutCreateWindow(" FOG "); /*绘制与显示*/ myInit(); glutKeyboardFunc(myKey); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); /*进入GLUT事件处理循环*/ glutMainLoop(); return 0; } |
图十:被雾化的圆锥体 |