opengl-3D迷宫-C--实现-源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includestdafx.h#includestdio.h#includestdlib.h#includeGL/glut.h#includemath.h#includeiostreamusingnamespacestd;voiddrawwalls(void);voiddrawtop(void);voiddrawball(void);#defineIDM_APPLICATION_EXIT(101)#defineIDM_APPLICATION_TEXTURE(102)#defineIDM_APPLICATION_BANK(103)#defineMAZE_HEIGHT(16)#defineMAZE_WIDTH(16)#defineSTARTING_POINT_X(13.5f);#defineSTARTING_POINT_Y(1.5f);#defineSTARTING_HEADING(90.0f);floatplayer_x=STARTING_POINT_X;floatplayer_y=STARTING_POINT_Y;floatplayer_h=STARTING_HEADING;//player'sheadingfloatplayer_s=0.0f;//forwardspeedoftheplayerfloatplayer_m=1.0f;//speedmultiplieroftheplayerfloatplayer_t=0.0f;//player'sturning(changeinheading)floatplayer_b=0.0f;//viewpointbank(roll)staticfloattexcoordX=0.0f;intwalllist=0;intmazelist=0;intballlist=0;intstatus=1;boolsearchroute=false;boolkeystate[4]={false,false,false,false};charmazedata[MAZE_HEIGHT][MAZE_WIDTH]={{'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H'},{'H','','','','','','','','H','','','','','','','H'},{'H','','H','','H','H','H','','H','','H','','','','','H'},{'H','','H','H','','','H','','H','H','','H','','H','','H'},{'H','','','','','','H','','','','','','','H','','H'},{'H','','H','H','H','H','H','H','H','H','','H','H','H','','H'},{'H','','','','','','','','','','','','H','','','H'},{'H','','H','H','H','H','H','','H','H','H','','H','H','H','H'},{'H','','H','','','','H','','','','H','','','','','H'},{'H','','','','H','H','H','H','H','H','H','','','','','H'},{'H','','H','','','','H','','','','H','','','H','','H'},{'H','','H','H','H','H','H','','H','H','H','H','','H','','H'},{'H','','','','','','H','','','','','','','H','','H'},{'H','','','H','H','','H','H','H','H','','H','H','H','','H'},{'H','','','','H','','H','','','','','H','','','','H'},{'H','H','H','H','H','H','H','H','H','H','H','H','H','','H','H'},};voidmyinit(){glClearColor(0.5f,0.5f,0.5f,0.0f);glColor3f(1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);glEnable(GL_TEXTURE_2D);walllist=glGenLists(2);mazelist=walllist+1;balllist=walllist+2;glNewList(walllist,GL_COMPILE);drawwalls();glEndList();glNewList(mazelist,GL_COMPILE);drawtop();glEndList();glNewList(balllist,GL_COMPILE);drawball();glEndList();glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0,1.0,0.1,60.0);glMatrixMode(GL_MODELVIEW);glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ʹÎÆÀí²»±äÐÎ}boolwall(intx,inty){return(x=0&&y=0&&xMAZE_WIDTH&&yMAZE_HEIGHT&&mazedata[y][x]!='');}boolonopen(intx,inty){if(wall(x,y)){return(mazedata[y][x]=='H');}}voidcloseit(intx,inty){if(onopen(x,y)){mazedata[y][x]='X';}}boolneighbor(intx,inty,intw,int*nx,int*ny){switch(w){case0:*nx=x-1;*ny=y;break;case1:*nx=x;*ny=y+1;break;case2:*nx=x+1;*ny=y;break;case3:*nx=x;*ny=y-1;break;default:break;}returnwall(*nx,*ny);}booldiagnal(intx,inty,intw,int*nx,int*ny){switch(w){case0:*nx=x-1;*ny=y-1;break;case1:*nx=x-1;*ny=y+1;break;case2:*nx=x+1;*ny=y+1;break;case3:*nx=x+1;*ny=y-1;break;default:break;}returnwall(*nx,*ny);}voiddw(intx,inty,intp){intw=p;closeit(x,y);do{intx2=0;inty2=0;if(neighbor(x,y,w,&x2,&y2)){if(onopen(x2,y2)){dw(x2,y2,(w+3)%4);}else{if((w+1)%4==p){return;}}}else{floatfx;floatfy;if(diagnal(x,y,w,&x2,&y2)&&onopen(x2,y2)){dw(x2,y2,(w+2)%4);}texcoordX=(texcoordX0.5)?1.0f:0.0f;fx=(float)x+((w==1||w==2)?1.0f:0.0f);fy=(float)y+((w==0||w==1)?1.0f:0.0f);glTexCoord2f(texcoordX,0.0f);glVertex3f(fx,fy,0.0f);glTexCoord2f(texcoordX,1.0f);glVertex3f(fx,fy,1.0f);}w++;w%=4;}while(w!=p);return;}voiddrawwalls(){glEnable(GL_TEXTURE_2D);glBegin(GL_QUAD_STRIP);glColor3f(1.0,1.0,1.0);glVertex3f(0.0f,0.0f,0.0f);glVertex3f(0.0f,0.0f,1.0f);dw(0,0,0);glEnd();}voiddrawtop(){intx,y;glBegin(GL_QUADS);for(y=0;yMAZE_HEIGHT;y++){for(x=0;xMAZE_WIDTH;x++){if(wall(x,y)){mazedata[y][x]='X';glVertex3f(x+0.0f,y+0.0f,1.0f);glVertex3f(x+1.0f,y+0.0f,1.0f);glVertex3f(x+1.0f,y+1.0f,1.0f);glVertex3f(x+0.0f,y+1.0f,1.0f);}}}glEnd();}voidforward(floatpx,floatpy,floatbf){intx=((int)player_x);inty=((int)player_y);inth=0;if((pxx+1.0f-bf)&&wall(x+1,y)){px=(float)(x)+1.0f-bf;h++;}if(pyy+1.0f-bf&&wall(x,y+1)){py=(float)(y)+1.0f-bf;h++;}if(pxx+bf&&wall(x-1,y)){px=(float)(x)+bf;h++;}if(pyy+bf&&wall(x,y-1)){py=(float)(y)+bf;h++;}player_x=px;player_y=py;}voiddrawball(){glDisable(GL_TEXTURE_2D);glColor3f(1.0,0.0,0.0);glutSolidSphere(0.2f,15,15);}voidnavmaze1(){forward(player_x+player_s*(float)sin(player_h*3.14/180),player_y+player_s*(float)cos(player_h*3.14/180),0.2f);coutplayer_xplayer_yendl;player_h+=player_t;player_b=3*player_b/4+player_t/4;glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glLoadIdentity();glPushMatrix();glRotatef(-90.0f,1.0f,0.0f,0.0f);glRotatef(player_h,0.0f,0.0f,1.0f);glTranslatef(-player_x,-player_y,-0.5f);glCallList(walllist);glPopMatrix();}voidnavmaze2(){forward(player_x+player_m*player_s*(float)sin(player_h*3.14/180),player_y+player_m*player_s*(float)cos(player_h*3.14/180),0.2f);coutplayer_xplayer_xendl;player_h+=player_t;player_b=3*player_b/4+player_t/4;glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glLoadIdentity();glOrtho(-

1 / 9
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功