opengl3d迷宫c实现源代码OpenGL三维迷宫C实现源代码.docx
- 文档编号:6747022
- 上传时间:2023-01-09
- 格式:DOCX
- 页数:13
- 大小:16.81KB
opengl3d迷宫c实现源代码OpenGL三维迷宫C实现源代码.docx
《opengl3d迷宫c实现源代码OpenGL三维迷宫C实现源代码.docx》由会员分享,可在线阅读,更多相关《opengl3d迷宫c实现源代码OpenGL三维迷宫C实现源代码.docx(13页珍藏版)》请在冰豆网上搜索。
opengl3d迷宫c实现源代码OpenGL三维迷宫C实现源代码
opengl3d迷宫c实现源代码(OpenGL三维迷宫C实现源代码)
opengl3d迷宫c++实现源代码(OpenGL三维迷宫C++实现源代码)
#include"stdafx.h"
#stdio.h>in<
#stdlib.h>in<
#include
#math.h>in<
#iostream>in<
usingnamespacestd;
drawwalls(void);void
drawtop(void);void
drawball(void);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'sheading
floatplayer_with=0.0f;//forwardspeedoftheplayer
floatplayer_m=1.0f;//speedmultiplieroftheplayer
floatplayer_t=0.0f;//player'schanging(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'},
};
myinit(void)
{
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_house_correction_hint,gl_nicest);//使纹理不变形
}
abolusofwall(intx,inty){
return(x>=0&&y>=0&&x height&&mazedata[y][x].=''); } abolusonopen(intx,inty){ if(wall(x,y)){ return(mazedata[y][x]=='h'); } } closeitvoid(intx,inty){ if(onopen(x,y)) { mazedata[y][x]='x'; } } abolusneighbor(intx,inty,intw,intnx*ny*,int){ switch(w){ case0: *nx=x-1;*ny=y;break; case1: nx*ny*=x=y+1;;break; case2: *nx=x+1;*ny=y;break; case3: *nx*ny==x;y-1;break; default: break; } returnwall(nx*ny*,); } abolusdiagnal(intx,inty,intw,intnx*ny*,int){ switch(w){ case0: *nx=x-1;*ny=y-1;break; case1: *nx*ny-1=x=y+1;;break; case2: *nx+1=x=y+1;*ny;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((x,y,wneighbor&,&x2,y2)){ if(onopen(x2,y2)){ dw(x2,y2,(w+3)%,(4); } else{ if((w+1)%,4==p) { return; } } } else{ floatfx; floatonthefy; if(diagnal(x,y,w,&x2,&y2)&&onopen(x2,y2)){ dw(x2,y2,(w+2)%,(4); } texcoordx=(texcoordx<0.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; } drawwalls(void){ 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(); } drawtop(void){ intx,y; glbegin(gl_quads); for(y=0,y for(x=0,x 如果(墙(x,y)){ mazedata[y][x]=“X”; glVertex3f(X+Y+0.0f,0.0f,1.0f); glVertex3f(X+Y+0.0f,1.0f,1.0f); glVertex3f(X+Y+1.0f,1.0f,1.0f); glVertex3f(X+Y+0.0f,1.0f,1.0f); } } } glend(); } 前向空(浮点数,浮点数,浮球){ intx=((int)player_x); y=((int)player_y); int,0; 如果((PX>x+1.0f-BF)和墙(x+1,y)){ PX=(浮动)(x)+1.0f-bf; H+; } 如果(Py>y+1.0f-bf&壁(x,y+1)){ py=(浮动)(Y)+1.0f-bf; H+; } 如果(PX,x+BF和墙(X-1,y)){ PX=(浮动)(x)+高炉; H+; } 如果(PY =(浮点)(y)+BF; H+; } player_x=PX; player_y=Py; } 无效drawball() { gldisable(gl_texture_2d); glColor3f(1.0,0.0,0.0); glutsolidsphere(0.2f,15,15); } 无效navmaze1() { 远期(player_x+player_s*(浮动)罪(player_h*3.14/180), player_y+player_s*(浮动)cos(player_h*3.14/180),0.2f); cout< player_h+=player_t; player_b=3*4+player_tplayer_b//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_xplayer_y,-,-0.5f); glcalllist(walllist); glpopmatrix(); } 无效navmaze2() { 远期(player_x+player_m*player_s*(浮动)罪(player_h*3.14/180), player_y+player_m*player_s*(浮动)cos(player_h*3.14 /180),0.2f); cout< player_h+=player_t; player_b=3*4+player_tplayer_b//4; glClear里(gl_color_buffer_bit|gl_depth_buffer_bit); glloadidentity(); glortho(-16.0,16.016.0,16.0,-,-2.0,20.0); glpushmatrix(); glRotatef(90.0f,0.0f,0.0f,1.0f); glTranslatef(-maze_width/2-maze_height/2-0.5f); glcalllist(walllist); glcalllist(mazelist); glpushmatrix(); glTranslatef(player_x,player_y,0.5f); glcalllist(balllist); glpopmatrix(); glpopmatrix(); } 无效mydisplay() { 如果(状态=1) { 如果(searchroute==真) { } 其他navmaze1(); } 如果(状态=3) { 如果(searchroute==真) { } 其他navmaze2(); } glflush(); glutswapbuffers(); } 无效myreshape(int,inth) { glviewport(0,0,W,H); glMatrixMode(gl_projection); glloadidentity(); glMatrixMode(gl_modelview); glloadidentity(); glutpostredisplay(); } 无效specialkeys(int,intx,y) { 开关(键) { 案例glut_key_left: 重点[2]=真; player_t=-2.0f; 打破; 案例glut_key_right: 重点[3]=真; player_t=2.0f; 打破; 案例glut_key_up: 重点[0]=真; player_s=0.01f; 打破; 案例glut_key_down: 重点[1]=真; player_s=-0.01f; 打破; 默认值: 中断; } } 无效键盘(无符号字符键,int,int) { 开关(键) { 案例1: 状态=1; 打破; 案例3: 状态=3; 打破; 默认值: 中断; } glutpostredisplay(); } 无效upspecialkeyboard(int,intx,y) { 开关(键) { 案例glut_key_left: 重点[2]=false; player_t=0.0f; 打破; 案例glut_key_right: 重点[3]=false; player_t=0.0f; 打破; 案例glut_key_up: 重点[0]=false; player_s=0.0f; 打破; 案例glut_key_down: 重点[1]=false; player_s=0.0f; 打破; 默认值: 中断; } //glutpostredisplay(); } 无效idle() { 如果(重点[0]||重点[1]||重点[2]||重点[3]) glutpostredisplay(); 别的{} }//释放按键后就进入空闲状态,如果空闲状态不一直重复绘图,就 会画面停滞。 次方法缺点是开销很大 voidmain(intargc、argvchar*) { glutInit(&argc、argv); glutinitdisplaymode(glut_double|glut_rgb); (500,500)glutinitwindowsize; glutcreatewindow(“多边形建模”); glutdisplayfunc(mydisplay); myinit(); glutspecialfunc(specialkeys); glutkeyboardfunc(键盘); glutspecialupfunc(upspecialkeyboard); glutidlefunc(空闲); glutmainloop(); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- opengl3d 迷宫 实现 源代码 OpenGL 三维
![提示](https://static.bdocx.com/images/bang_tan.gif)