opengl 3D迷宫 C 实现 源代码_第1页
opengl 3D迷宫 C 实现 源代码_第2页
opengl 3D迷宫 C 实现 源代码_第3页
opengl 3D迷宫 C 实现 源代码_第4页
opengl 3D迷宫 C 实现 源代码_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#includenstdafx・h”#include<stdio.h>#include<stdlib.h>#include#include<GL/glut.h><math.h>#include<iostream>usingnamespacestd;voiddrawwalls(void);voiddrawtop(void);voiddrawball(void);d㊀fineIDM_APPLICATION_EXIT (101)ci㊀fin㊀工DM_APPL工CAT工ON_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.Of);floatplayer_x=STARTING_POINT_X;floatplayer_x=STARTING_POINT_X;floatplayer_y=floatplayer_h=floatplayer_s=floatplayer_m=floatplayer_t=floatplayer_b=STARTING_POINT_Y;STARTINGHEADING; //player1sleadingO.Of;floatplayer_y=floatplayer_h=floatplayer_s=floatplayer_m=floatplayer_t=floatplayer_b=STARTING_POINT_Y;STARTINGHEADING; //player1sleadingO.Of;l.Of;O.Of;O.Of;forwardspeedofspeedmultiplierplayer1sturningtheplayeroftheplayer(changeinheading)viewpointbank(roll)staticfloattexcoordX=0.Of;intwalllist=0;intmazelist=0;intballlist=0;intstatus=l;bools㊀archroute=fals㊀;boolkeystate[4]={false,false,falseffalse};charmazedata[MAZE_HEIGHT][MAZE_WIDTH]={,},fILJTfTITII IIIIIIT ITLJ»IIIIIITI TI I» ILJI1JL1,,,,,,,, 11 ,,,,,,, 11jffIuf! 1IUI!I !TJ1ITJIfTJ!I TIuIIIITJ!I!I! I! I! ITJ»11口, ,口, ,n,n,n,,ti,,n,,,,,ti),fIUT»1IUIIUIIII»,口,»»IU»।口,»»»LJIII1口,»>ILJ»1[tl,,,tl,,乜,,ti,,tl),fILJ!»»II»IIIIIILJI»I!I»IIIIfI!,口,IfILJ»111 ,,,,,, :1 ,,,,,,, 11 , , 11J,{,H一—H―H一H-H一H-H一H—H—一H-H一H--H,},fIUT» »I»I1IIIIIITITI» ! »I1 1ILTI!I»IILJI111 ,,,,,,,,,,,, 11Iff工1JfJITJ1 1 » !U> *u» »LJIInT!TJII fIuITuIILJ\ !1口, ,口,ti,ti,n,M,,口,口,口,一HIHJH-H,},{,H——H———L'H一一一L'H一———一口},f!U»»»IVIITU!fLJ»TUfITJ!!TJIITJT!rjIIITITT!!ITJIItH,, , ,H,H,H,H,H,H,H,, , ,,旦),{Hj'JH—一一一H一—'JIHJ-'JH—一H){HJ -H—H—HIHJ'JHIHJH-H—'JH-H},fILJT» I\ T»ITI!IIU!IITI»II \ITITTLJII IILJ»111 ,,,,,,-t-L,,,,,,,-LI, , 11j,fITJT I »ILJTIUI!!IUITLTTIU<»U»» TTLJIITJTITJI! IITJI1ttl,,,tl,H,,H,,H,tl,H,rn]f{'H— — ——H—一H—一—— —H一—一 IH},—H—H,},);voidmyinit()(glClearColor(0.5f,0.5fz0.5f,0.Of);glColor3f(1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);glEnable(GL_TEXTURE_2D);walllist=glGenLists(2);maze1ist=walllist+1;balllist=walllist+2;glNewList(walllistzGL_COMPILE);drawwalls();glEndList();glNewList(mazelist,GL_COMPILE);drawtop();glEndList();glNewList(balllist,GL_COMPILE);drawball();glEndList();glMatrixMode(GL_PROJECTION);glLoadldentity();gluPerspective(60.0,1.0,0.1,60.0);glMatrixMode(GL_MODELVIEW);glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//E1IffiAi2»±aDI)boolwall(intx,inty){return(x>=0&&y>=0&&x<MAZE_WIDTH&&y<MAZE_HEIGHT&&mazedata[y][x]!=*1);boolonopen(intx,inty){if(wall(x,y)){return(maz㊀data[y][x]==,H1;})voidcloseit(intx,inty){if(onopen(x,y))(mazedata[y][x]=1X!;))boolneighbor(intx,inty,intwzint*nx,int*ny){switch(w){case0:*nx=x-1;*ny=y;break;*nx=x;*ny=y+l;break;*nx=x+1;*ny=y;break;*nx=x;*ny=y-l;break;default:break;)returnwall(*nx,*ny);}booldiagnal(intx,inty,intw,int*nx,int*ny){switch(w){cas㊀0:*nx=x-1;*ny=y-l;break;*nx=x-1;*ny=y+l;break;*nx=x+1;*ny=y+l;break;*nx=x+1;*ny=y-l;break;default:break;)returnwall(*nx,*ny);}voiddw(intx,inty,intp){intw=p;closeit(x,y);do{intx2=0;inty2=0;if(naighbor(x,y,w,&x2,&y2)){if(onopen(x2,y2)){dw(x2ry2,(w+3)%4);)㊀Is㊀{if((w+1)%4==p){return;}else{floatfx;floatfy;if(diagnal(x,y,w,&x2z&y2)&&onopen(x2,y2)){dw(x2,y2,(w+2)%4);)texcoordX=(texcoordX<0.5)?1.Of:0.Of;fx=(float)x+((w==l||w==2)?1.Of:0.Of);fy=(float)y+((w==0||w==l)?1.Of:0.Of);glTexCoord2f(texcoordX,0.Of);glVertex3f(fx,fy,0.Of);glTexCoord2f(texcoordX,1.Of);glVertex3f(fx,fy,1.Of);)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.Of,0.Of,0.Of);glVertex3f(0.Of,0.Of,1.Of);dw(0,0,0);glEnd();}voiddrawtop(){intx,y;glBegin(GL_QUADS);for(y=O;y<MAZE_HEIGHT;y++){for(x=0;x<MAZE_WIDTH;x++){if(wall(x,y)){mazedata[y][x]=*X1;glVertex3f(x+0.Of,y+0.Of,1.Of);glVertex3f(x+1.Of,y+0.Of,1.Of);glVertex3f(x+1.Of,y+1.Of,1.Of);glVertex3f(x+0.Of,y+1.Of,1.Of);}))glEnd();}voidforward(floatpx,floatpy,floatbf)intx=((int)player_x);inty=((int)player_y);inth=0;if((px>x+1.Of-bf)&&wall(x+1,y))px=(float)(x)+1.Of-bf;h++;if(py>y+1.Of-bf&&wall(x,y+1))py=(float)(y)+1.Of-bf;h++;)if(px<x+bf&&wall(x-1,y)){px=(float)(x)+bf;h++;)if(py<y+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);}voidnavmazel()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);cout<<player_x<<player_y<<endl;play㊀r_h+=play㊀r_t;player_b=3*player_b/4+player_t/4;glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glLoadldentity();glPushMatrix();glRotatef(-90.0f,1.0f,0.0f,0.0f);glRotatef(player_h,0.Of,0.Of,1.Of);glTranslatef(-player_x,-player_yA-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);cout<<player_x<<player_x<<endl;player_h+=player_t;play㊀r_b=3*player_b/4+player_t/4;glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glLoadldentity();glOrtho(-16.0,16.0,-16.0,16.0,-2.0,20.0);glPushMatrix();glRotat㊀f(90.0f,0.0f,0.0f,1.0f);glTranslatef(-MAZE_WIDTH/2,-MAZE_HE工GHT/2,-0.5f);glCallList(walllist);glCallList(mazelist);glPushMatrix();glTranslatef(player_x,player_y,0.5f);glCallList(balllist);glPopMatrix();glPopMatrix();)voidmyDisplay()(if(status==l)if(searchroute==true)elsenavmazel();)if(status==3)(if(searchroute==true)()elsenavmaze2();)glFlush();glutSwapBuffers();}voidmyReshape(intw,inth)(glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadldentity();glMatrixMode(GL_MODELVIEW);glLoadldentity();glutPostRedisplay();)voidspecialKeys(intkey,intxzinty)(switch(key)(caseGLUT_KEY_LEFT:keystate[2]=true;player_t=-2.Of;break;caseGLUT_KEY_RIGHT:keystate[3]=true;player_t=2.Of;break;caseGLUT_KEY_UP:keystate[0]=true;player_s=0.Olf;break;caseGLUT_KEY_DOWN:k㊀ystat㊀[1]=true;player_s=-0.Olf;break;default:break;voidkeyboard(unsignedcharkey,intx,inty)switch(key)(case111:status=l;break;case131:status=3;break;default:break;)glutPostRedisplay();)voidupSpecialKeyboard(intkey,intx,inty)(switch(key)(caseGLUT_KEY_LEFT:keystate[2]=false;player_t=0.Of;br

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论