www.pudn.com > fish_openGL.rar > GLBLEND.cpp
// GLBLEND.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include#include #include #include #include #include #ifndef WIN32 #define CALLBACK #endif #define INREAL float #define S_NUMPOINTS 3 //#define S_NUMPOINTS 4 #define S_ORDER 3 #define S_NUMKNOTS (S_NUMPOINTS + S_ORDER) #define T_NUMPOINTS 3 #define T_ORDER 3 #define T_NUMKNOTS (T_NUMPOINTS + T_ORDER) //#define SQRT_TWO 1.41421356237309504880 typedef INREAL Point[4]; GLenum doubleBuffer; GLenum expectedError; GLint rotX =0, rotY = 0; INREAL sknots[S_NUMKNOTS] = { -1.0, -1.0, -1.0, 1.0, 1.0, 2.0//, 3.0 }; INREAL glutnots[T_NUMKNOTS] = { 1.0, 1.0, 2.0, 3.0, 4.0, 10.0 }; Point ctlpoints_f[S_NUMPOINTS][T_NUMPOINTS]= { {{0.0, 0.0, 0.0, 1.0},{0.0, 0.0, 0.0, 1.0},{0.0, 0.0, 0.0, 1.0}},//鱼嘴尖端,三合一点 {{0.3, -0.5, -0.5, 1.0},{0.3, 0.0, 0.5, 1.0},{0.3, 1.3, -0.5, 1.0}}, {{1.0, -1.0, 0.0, 1.0},{0.4, 0.0, 0.0, 1.0},{1.6, 1.8, 0.0, 1.0}}, //{{1.5, -2.0, 0.0, 1.0},{1.5, 0.0, 0.0, 1.0},{1.5, 4.0, 0.0, 1.0}}, }; Point ctlpoints_b[S_NUMPOINTS][T_NUMPOINTS]= { {{0.0, 0.0, 0.0, 1.0},{0.0, 0.0, 0.0, 1.0},{0.0, 0.0, 0.0, 1.0}}, {{0.3, -0.5, 0.5, 1.0},{0.3, 0.0, -0.5, 1.0},{0.3, 1.3, 0.5, 1.0}}, {{1.0, -1.0, 0.0, 1.0},{0.4, -0.0, 0.0, 1.0},{1.6, 1.8, 0.0, 1.0}}, //{{1.5, 0.0, 0.0, 1.0},{1.5, 0.0, 0.0, 1.0},{1.5, 0.0, 0.0, 1.0}}, }; Point ctlpoints_t[S_NUMPOINTS][T_NUMPOINTS]= { {{0.5, -0.02, 0.0, 1.0},{0.5, 0.0, 0.0, 1.0},{0.5, 0.02, 0.0, 1.0}}, {{0.6, -0.2, 0.0, 1.0},{0.6, 0.0, 0.05, 1.0},{0.6, 0.0, 0.0, 1.0}}, {{1.2, -0.6, 0.0, 1.0},{0.6, 0.0, 0.0, 1.0},{1.5, 0.8, 0.0, 1.0}}, }; /*Point ctlpoints_l[S_NUMPOINTS][T_NUMPOINTS]= { //{{0.0, 0.0, 0.0, 1.0},{0.0, 0.0, 0.0, 1.0},{0.0, 0.0, 0.0, 1.0}}, //{{0.5, -1.0, 0.0, 1.0},{0.5, 0.0, -1.0, 1.0},{0.5, 1.0, 0.0, 1.0}}, //{{1.0, -2.0, 0.0, 1.0},{1.0, -1.0, 0.0, 1.0},{1.0, 2.0, 0.0, 1.0}}, {{4.0, 0.0, 0.0, 1.0},{4.0, 0.0, 0.0, 1.0},{4.0, 0.0, 0.0, 1.0}},//花瓣 {{4.2, -0.5, -0.8, 1.0},{4.2, 0.0, 0.8, 1.0},{4.2, 0.5, -0.8, 1.0}}, {{4.0, 0.0, 0.0, 1.0},{4.0, 0.0, 0.0, 1.0},{4.0, 0.0, 0.0, 1.0}}, }; */ Point ctlpoints_l[S_NUMPOINTS][T_NUMPOINTS]= { {{3.7, -2.0, -1.0, 1.0},{3.7, -2.0, -1.0, 1.0},{3.7, -2.0, -1.0, 1.0}},//ye {{0.5, -2.3, 3.5, 1.0},{0.5, -2.0, 3.5, 1.0},{0.5, -1.7, 3.5, 1.0}}, {{0.0, -2.0, 3.0, 1.0},{-0.0, -2.0, 3.0, 1.0},{-0.0, -2.0, 3.0, 1.0}}, }; Point ctlpoints_l1[S_NUMPOINTS][T_NUMPOINTS]= { {{3.7, -2.0, -1.0, 1.0},{3.7, -2.0, -1.0, 1.0},{3.7, -2.0, -1.0, 1.0}},//ye1 {{0.5, -2.3, 3.5, 1.0},{0.5, -2.0, 3.5, 1.0},{0.5, -1.7, 3.5, 1.0}}, {{0.0, -2.0, 5.0, 1.0},{0.0, -2.0, 5.0, 1.0},{0.0, -2.0, 5.0, 1.0}}, }; Point ctlpoints_p[S_NUMPOINTS][T_NUMPOINTS]= { {{0.8, -2.0, 3.0, 1.0},{0.8, -2.0, 3.0, 1.0},{0.8, -2.0, 3.0, 1.0}},//ban {{0.5, -1.7, 3.2, 1.0},{0.8, -2.0, 3.3, 1.0},{1.1, -1.7, 3.4, 1.0}}, {{0.6, -2.0, 3.4, 1.0},{0.6, -2.0, 3.4, 1.0},{0.6, -2.0, 3.4, 1.0}}, }; Point ctlpoints_p1[S_NUMPOINTS][T_NUMPOINTS]= { {{0.8, -2.0, 3.0, 1.0},{0.8, -2.0, 3.0, 1.0},{0.8, -2.0, 3.0, 1.0}},//ban {{0.5, -1.5, 3.2, 1.0},{0.7, -2.0, 3.3, 1.0},{1.1, -1.5, 3.4, 1.0}}, {{0.3, -2.0, 3.4, 1.0},{0.3, -2.0, 3.4, 1.0},{0.3, -2.0, 3.4, 1.0}}, }; Point ctlpoints_p2[S_NUMPOINTS][T_NUMPOINTS]= { {{0.8, -2.0, 3.0, 1.0},{0.8, -2.0, 3.0, 1.0},{0.8, -2.0, 3.0, 1.0}},//ban {{0.5, -1.7, 3.1, 1.0},{0.7, -2.0, 3.2, 1.0},{1.1, -1.7, 3.3, 1.0}}, {{0.3, -2.0, 3.2, 1.0},{0.3, -2.0, 3.2, 1.0},{0.3, -2.0, 3.2, 1.0}}, }; /* Point ctlpoints_f[S_NUMPOINTS][T_NUMPOINTS]= { {{0.0, 0.0, 0.0, 1.0},{0.0, 0.0, 0.0, 1.0},{0.0, 0.0, 0.0, 1.0}},//鱼嘴尖端,三合一点 {{-0.5, -0.5, 0.3, 1.0},{0.5, 0.0, 0.3, 1.0},{-0.5, 1.3, 0.3, 1.0}}, {{0.0, -1.0, 0.0, 1.0},{0.0, 0.0, 0.4, 1.0},{0.0, 2.0, 1.6, 1.0}}, //{{1.5, -2.0, 0.0, 1.0},{1.5, 0.0, 0.0, 1.0},{1.5, 4.0, 0.0, 1.0}}, }; */ #pragma warning( disable : 4305 ) float view_rotx=0;//0 float view_roty=0;//7 float view_rotz=8;//8 GLfloat lev,y_bottom,Bubble_Radius=0.05; GLfloat lev1,Bubble1_Radius=0.03; GLenum bottle_tex=TRUE; ////////////////////////////////////////////////////////////////////////////////////// //纹理映射部分 AUX_RGBImageRec *image; unsigned char *pdata; void FishBodyTexture1(void)//fishbody_f { image=auxDIBImageLoad("fishbodyeye.bmp"); if(pdata!=NULL)free(pdata); pdata=(BYTE*)malloc(256*256*3*sizeof(BYTE)); if(!pdata)exit(0); glPixelStorei(GL_UNPACK_ALIGNMENT,1); gluScaleImage(GL_RGB,image->sizeX,image->sizeY,GL_UNSIGNED_BYTE,image->data, 256,256,GL_UNSIGNED_BYTE,pdata); glTexImage2D(GL_TEXTURE_2D,0,3,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,pdata); } void FishBodyTexture2(void)//fishbody_b { image=auxDIBImageLoad("fishbodyeye.bmp"); if(pdata!=NULL)free(pdata); pdata=(BYTE*)malloc(256*256*3*sizeof(BYTE)); if(!pdata)exit(0); glPixelStorei(GL_UNPACK_ALIGNMENT,1); gluScaleImage(GL_RGB,image->sizeX,image->sizeY,GL_UNSIGNED_BYTE,image->data, 256,256,GL_UNSIGNED_BYTE,pdata); glTexImage2D(GL_TEXTURE_2D,0,3,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,pdata); } void FishTailTexture(void)//fishbody_t { image=auxDIBImageLoad("fishbody5.bmp"); if(pdata!=NULL)free(pdata); pdata=(BYTE*)malloc(256*256*3*sizeof(BYTE)); if(!pdata)exit(0); glPixelStorei(GL_UNPACK_ALIGNMENT,1); gluScaleImage(GL_RGB,image->sizeX,image->sizeY,GL_UNSIGNED_BYTE,image->data, 256,256,GL_UNSIGNED_BYTE,pdata); glTexImage2D(GL_TEXTURE_2D,0,3,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,pdata); } void DeskTexture(void)//desktop { image=auxDIBImageLoad("Soap Bubbles.bmp"); if(pdata!=NULL)free(pdata); pdata=(BYTE*)malloc(256*256*3*sizeof(BYTE)); if(!pdata)exit(0); glPixelStorei(GL_UNPACK_ALIGNMENT,1); gluScaleImage(GL_RGB,image->sizeX,image->sizeY,GL_UNSIGNED_BYTE,image->data, 256,256,GL_UNSIGNED_BYTE,pdata); glTexImage2D(GL_TEXTURE_2D,0,3,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,pdata); } void DeskTexture1(void)//desktop { image=auxDIBImageLoad("Soap Bubbles1.bmp"); if(pdata!=NULL)free(pdata); pdata=(BYTE*)malloc(256*256*3*sizeof(BYTE)); if(!pdata)exit(0); glPixelStorei(GL_UNPACK_ALIGNMENT,1); gluScaleImage(GL_RGB,image->sizeX,image->sizeY,GL_UNSIGNED_BYTE,image->data, 256,256,GL_UNSIGNED_BYTE,pdata); glTexImage2D(GL_TEXTURE_2D,0,3,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,pdata); } //////////////////////////////////////////////////////////////////////////////////// //NURBS曲面相关部分 GLUnurbsObj *theNurbs;//创建一个指针 /*static void CALLBACK ErrorCallback(GLenum which) { if (which != expectedError) { fprintf(stderr, "Unexpected error occured (%d):\n", which); fprintf(stderr, " %s\n", gluErrorString(which)); } } */ ////////////////////////////////////////////////////////////////////////////////////// //初始化部分 ////////////////////////////////////////////////////////////////////////////////////// void init(void) { theNurbs = gluNewNurbsRenderer();// 创建一个NURBS对象,返回指针 // gluNurbsCallback(theNurbs, GLU_ERROR, ErrorCallback); gluNurbsCallback(theNurbs, GLU_ERROR, NULL);//定义回调函数 gluNurbsProperty(theNurbs, GLU_SAMPLING_TOLERANCE, 15.0);// 定义NURBRS对象的属性 // gluNurbsProperty(theNurbs, GLU_DISPLAY_MODE, GLU_OUTLINE_PATCH); gluNurbsProperty(theNurbs, GLU_DISPLAY_MODE, GLU_FILL); expectedError = GLU_INVALID_ENUM; gluNurbsProperty(theNurbs, ~0, 15.0); expectedError = GLU_NURBS_ERROR13; gluEndSurface(theNurbs); expectedError = 0; glClearColor(1.0, 1.0, 1.0, 0.0);//背景颜色 glShadeModel(GL_SMOOTH); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND);//打开融合 glEnable(GL_DEPTH_TEST);//打开深度测试 glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glTexImage2D(GL_TEXTURE_2D,0,3,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,pdata); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexEnvi(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_DECAL); } //////////////////////////////////////////////////////////////////////////////////////////// //绘图部分 ///////////////////////////////////////////////////////////////////////////////////////////// void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(view_rotx, view_roty, view_rotz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //画鱼缸及水//////////////////////////////////////////////////////////////////////////////// glDisable(GL_DEPTH_TEST);//关闭深度缓存为了融合鱼缸壁 GLfloat RAD = 3.1415926 / 180 ; GLfloat Radius=3.0;//鱼缸半径 GLfloat x, y, z, r,x_next,y_next,z_next, r_next; int i, j; glColor4f(0.9, 0.9, 0.95, 0.3); for(i = 40 ; i <=140 ; i += 5)//画缸壁 { glBegin(GL_QUAD_STRIP) ; r = Radius * sin(i * RAD) ; r_next=Radius * sin((i+5) * RAD) ; y =0.8*Radius * cos(i * RAD) ; y_next=0.8*Radius * cos((i+5) * RAD) ; for(j = 0 ; j <=360 ; j += 10) { x =r * cos(j * RAD) ; z =r * sin(j * RAD) ; glVertex3f(x, y, z) ; x_next =r_next * cos(j * RAD) ; z_next =r_next * sin(j * RAD) ; glVertex3f(x_next, y_next, z_next) ; } glEnd() ; } glBegin(GL_POLYGON) ;//画缸底 for(j = 0 ; j <=360 ; j += 10) { x =r_next * cos(j * RAD) ; z =r_next * sin(j * RAD) ; glVertex3f(x, y_next, z) ; } glEnd() ; /* glColor3f(0.8,0.8,0.95); glBegin(GL_LINE_LOOP) ;//画缸底line for(j = 0 ; j <=360 ; j += 10) { x =r_next * cos(j * RAD) ; z =r_next * sin(j * RAD) ; glVertex3f(x, y_next, z) ; } glEnd() ; */ y_bottom=y_next; Radius-=0.1;//画水壁 glColor4f(0.8,0.8,0.95,0.3); for(i = 60 ; i <=140 ; i += 5) { glBegin(GL_QUAD_STRIP) ; r = Radius * sin(i * RAD) ; r_next=Radius * sin((i+5) * RAD) ; y =0.8*Radius * cos(i * RAD) ; y_next=0.8*Radius * cos((i+5) * RAD) ; for(j = 0 ; j <=360 ; j += 10) { x =r * cos(j * RAD) ; z =r * sin(j * RAD) ; glVertex3f(x, y, z) ; x_next =r_next * cos(j * RAD) ; z_next =r_next * sin(j * RAD) ; glVertex3f(x_next, y_next, z_next) ; } glEnd() ; } glColor4f(0.9,0.8,0.95,0.3);//画水面 float r_top=Radius * sin(60 * RAD) ; float y_top=0.8*Radius * cos(60 * RAD) ; glBegin(GL_POLYGON) ; for(j = 0 ; j <=360 ; j += 10) { x =r_top * cos(j * RAD) ; z =r_top * sin(j * RAD) ; glVertex3f(x, y_top, z) ; } glEnd() ; glColor4f(0.7,0.8,0.95,0.5);//画水底 glBegin(GL_POLYGON) ; for(j = 0 ; j <=360 ; j += 10) { x =r_next * cos(j * RAD) ; z =r_next * sin(j * RAD) ; glVertex3f(x, y_next+0.02, z) ; } glEnd() ; //画泡泡////////////////////////////////////////////////////////////////////////////////////// GLfloat xb, yb, zb, rb,xb_next,yb_next,zb_next, rb_next; glEnable(GL_DEPTH_TEST);//打开深度,使泡泡与鱼交互时产生深度效果 glColor4f(0.2+Bubble_Radius*5, 0.5+Bubble_Radius*5, 0.9+Bubble_Radius*5, 0.3);//控制泡泡颜色使从底到上由蓝变白 lev=y_bottom+10*Bubble_Radius; for(i = 0 ; i <=180 ; i += 5) { glBegin(GL_QUAD_STRIP) ; rb =Bubble_Radius * sin(i * RAD) ; rb_next=Bubble_Radius * sin((i+5) * RAD) ; yb =lev+Bubble_Radius * cos(i * RAD) ; yb_next=lev+Bubble_Radius * cos((i+5) * RAD) ; for(j = 0 ; j <=360 ; j += 10) { xb =rb * cos(j * RAD) ; zb =rb * sin(j * RAD) ; glVertex3f(xb, yb, zb) ; xb_next =rb_next * cos(j * RAD) ; zb_next =rb_next * sin(j * RAD) ; glVertex3f(xb_next, yb_next, zb_next) ; } glEnd() ; } glColor4f(0.2+Bubble1_Radius*5, 0.5+Bubble1_Radius*5, 0.9+Bubble1_Radius*5, 0.3); lev1=y_bottom+10*Bubble1_Radius+1.0; for(i = 0 ; i <=180 ; i += 5) { glBegin(GL_QUAD_STRIP) ; rb =Bubble1_Radius * sin(i * RAD) ; rb_next=Bubble1_Radius * sin((i+5) * RAD) ; yb =lev1+Bubble1_Radius * cos(i * RAD) ; yb_next=lev1+Bubble1_Radius * cos((i+5) * RAD) ; for(j = 0 ; j <=360 ; j += 10) { xb =0.3+rb * cos(j * RAD) ; zb =0.3+rb * sin(j * RAD) ; glVertex3f(xb, yb, zb) ; xb_next =0.3+rb_next * cos(j * RAD) ; zb_next =0.3+rb_next * sin(j * RAD) ; glVertex3f(xb_next, yb_next, zb_next) ; } glEnd() ; } //画花束////////////////////////////////////////////////////////////////////////////////// glColor4f(0.8,0.9,0.5,0.5);//leaf_flat gluBeginSurface(theNurbs);//leaf_left gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_l[0][0][0], S_ORDER, T_ORDER, GL_MAP2_VERTEX_4); gluEndSurface(theNurbs); gluBeginSurface(theNurbs);//leaf_right gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_l1[0][0][0], S_ORDER, T_ORDER, GL_MAP2_VERTEX_4); gluEndSurface(theNurbs); glColor4f(0.9,0.5,0.5,0.5);//leaf_flat gluBeginSurface(theNurbs);//flower_left gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_p[0][0][0], S_ORDER, T_ORDER, GL_MAP2_VERTEX_4); gluEndSurface(theNurbs); gluBeginSurface(theNurbs);//flower_right gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_p1[0][0][0], S_ORDER, T_ORDER, GL_MAP2_VERTEX_4); gluEndSurface(theNurbs); gluBeginSurface(theNurbs);//flower_right gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_p2[0][0][0], S_ORDER, T_ORDER, GL_MAP2_VERTEX_4); gluEndSurface(theNurbs); //画小鱼/////////////////////////////////////////////////////////////////// glEnable(GL_DEPTH_TEST); glPushMatrix(); glTranslatef(2.0*sin(rotY*3.14/180), 0.0,2.0*cos(rotY*3.14/180));//使小鱼绕鱼缸旋转 glRotatef(rotX, 1, 0, 0); glRotatef(rotY, 0, 1, 0); glEnable(GL_TEXTURE_2D); FishBodyTexture1(); glColor4f(1.0,0.0,1.0,1.0);// 绘制NURBS曲面 gluBeginSurface(theNurbs);//body_front gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_f[0][0][0], S_ORDER, T_ORDER, GL_MAP2_TEXTURE_COORD_2); gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_f[0][0][0], S_ORDER, T_ORDER, GL_MAP2_NORMAL); gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_f[0][0][0], S_ORDER, T_ORDER, GL_MAP2_VERTEX_4); gluEndSurface(theNurbs); FishBodyTexture2(); gluBeginSurface(theNurbs);//body_back gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_b[0][0][0], S_ORDER, T_ORDER, GL_MAP2_TEXTURE_COORD_2); gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_b[0][0][0], S_ORDER, T_ORDER, GL_MAP2_NORMAL); gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_b[0][0][0], S_ORDER, T_ORDER, GL_MAP2_VERTEX_4); gluEndSurface(theNurbs); FishTailTexture(); gluBeginSurface(theNurbs);//tail gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_t[0][0][0], S_ORDER, T_ORDER, GL_MAP2_TEXTURE_COORD_2); gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_t[0][0][0], S_ORDER, T_ORDER, GL_MAP2_NORMAL); gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots, 4*T_NUMPOINTS, 4, &ctlpoints_t[0][0][0], S_ORDER, T_ORDER, GL_MAP2_VERTEX_4); gluEndSurface(theNurbs); glPopMatrix(); glDisable(GL_TEXTURE_2D); //画圆形桌子///////////////////////////////////////// glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST); // glEnable(GL_POLYGON_SMOOTH);//反走样,速度太慢 glEnable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); if(!bottle_tex)//纹理动态 DeskTexture1(); else DeskTexture();//画上面 glBegin(GL_POLYGON); glColor4f(1.0,1.0,1.0,0.5); glNormal3f(0.0,1.0,0.0); for( i=0;i<360;i+=5) { glTexCoord2d(1.0*sin(i*3.14/180),1.0*cos(i*3.14/180)); glVertex3f(1.4*sin(i*3.14/180),y_bottom,1.4*cos(i*3.14/180)); } glEnd(); glBegin(GL_POLYGON);//画下面 glColor3f(0.0,0.0,0.0); glNormal3f(0.0,-1.0,0.0); for(i=0;i<360;i+=5) { glTexCoord2d(0.5*sin(i*3.14/180),0.5*cos(i*3.14/180)); glVertex3f(1.4*cos(i*3.14/180),y_bottom-0.6,1.4*sin(i*3.14/180)); } glEnd(); glColor3f(1.0,1.0,1.0);//画侧面 for(i=0;i<360;i+=5) { glNormal3f(cos((i+2.5)*3.14/180),0.0,sin((i+2.5)*3.14/180)); glBegin(GL_POLYGON); glTexCoord2d(0.0+i/100,0.0);glVertex3f(1.4*cos(i*3.14/180),y_bottom-0.2,1.4*sin(i*3.14/180)); glTexCoord2d(0.0+i/100,0.5);glVertex3f(1.4*cos((i+5)*3.14/180),y_bottom-0.2,1.4*sin((i+5)*3.14/180)); glTexCoord2d(0.1+i/100,0.5);glVertex3f(1.4*cos((i+5)*3.14/180),y_bottom,1.4*sin((i+5)*3.14/180)); glTexCoord2d(0.1+i/100,0.0);glVertex3f(1.4*cos(i*3.14/180),y_bottom,1.4*sin(i*3.14/180)); glEnd(); } glDisable(GL_TEXTURE_2D); glDisable(GL_POLYGON_SMOOTH); glColor4f(0.8,0.8,0.95,0.25);//画玻璃托盘 glBegin(GL_POLYGON) ; for(j = 0 ; j <=360 ; j += 10) { x =4.0 * cos(j * RAD) ; z =4.0 * sin(j * RAD) ; glVertex3f(x, y_bottom-0.02, z) ; } glEnd() ; //交换/////////////////////////////////////////////////////////////////////////////////////// glutSwapBuffers(); } ///////////////////////////////////////////////////////////////////////////////////////////// //设置视口部分 /////////////////////////////////////////////////////////////////////////////////////////// void reshape (int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei) h); //以下语句放在reshape中可以使屏幕改变时保持图像比例 //但放在init中图像变形 glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.5, 100.0); // glMatrixMode(GL_MODELVIEW);//已放在init中 // glLoadIdentity(); // gluLookAt(view_rotx, view_roty, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } //键盘控制视角 void keyboard (unsigned char key, int x, int y) { switch (key) { case 'Y': // view_roty += 5.0; view_roty += 0.1; break; case 'y': // view_roty -= 5.0; view_roty -= 0.1; break; case 'X': // view_rotx += 5.0; view_rotx += 0.1; break; case 'x': // view_rotx -= 5.0; view_rotx -= 0.1; break; case 'Z': // view_rotx += 5.0; view_rotz += 0.1; break; case 'z': // view_rotx -= 5.0; view_rotz -= 0.1; break; case 27: // 退出程序 case 'Q': case 'q': exit(0); break; default: return; } glutPostRedisplay(); } //键盘控制小鱼 static void SpecialKey(int key, int x, int y) { switch (key) { case GLUT_KEY_DOWN: rotX -= 5; glutPostRedisplay(); break; case GLUT_KEY_UP: rotX += 5; glutPostRedisplay(); break; case GLUT_KEY_LEFT: rotY -= 5; glutPostRedisplay(); break; case GLUT_KEY_RIGHT: rotY += 5; glutPostRedisplay(); break; } } /* static void Args(int argc, char **argv) { GLint i; doubleBuffer = GL_FALSE; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-sb") == 0) { doubleBuffer = GL_FALSE; } else if (strcmp(argv[i], "-db") == 0) { doubleBuffer = GL_TRUE; } } } */ ////////////////////////////////////////////////////////////////////////////////////////////// //空闲相应部分 ////////////////////////////////////////////////////////////////////////////////////////////// void idle(void) { Bubble_Radius+=0.002; if(Bubble_Radius>=0.19) Bubble_Radius=0.05; Bubble1_Radius+=0.002; if(Bubble1_Radius>=0.15) Bubble1_Radius=0.03; rotY+=-2; if( rotY <=-360.0 ) rotY += 360.0; bottle_tex=!bottle_tex; glutPostRedisplay(); } /////////////////////////////////////////////////////////////////////////////////////////////// //主函数部分 /////////////////////////////////////////////////////////////////////////////////////////////// int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("fish!!"); init(); glutKeyboardFunc(keyboard); glutSpecialFunc(SpecialKey); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(idle); glutMainLoop(); return 0; }