opengl 3D迷宮 C 實現(xiàn) 源代碼_第1頁
opengl 3D迷宮 C 實現(xiàn) 源代碼_第2頁
opengl 3D迷宮 C 實現(xiàn) 源代碼_第3頁
opengl 3D迷宮 C 實現(xiàn) 源代碼_第4頁
opengl 3D迷宮 C 實現(xiàn) 源代碼_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

#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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論