#include #include #include #include #include #include #include
收藏 分销(赏)

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

上传人:a199****6536 文档编号:3242758 上传时间:2024-06-26 格式:DOC 页数:9 大小:38KB 下载积分:6 金币
下载 相关 举报
opengl-3D迷宫-C--实现-源代码.doc_第1页
第1页 / 共9页
opengl-3D迷宫-C--实现-源代码.doc_第2页
第2页 / 共9页


点击查看更多>>
资源描述
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> #include <math.h> #include <iostream> using namespace std; void drawwalls(void); void drawtop(void); void drawball(void); #define IDM_APPLICATION_EXIT (101) #define IDM_APPLICATION_TEXTURE (102) #define IDM_APPLICATION_BANK (103) #define MAZE_HEIGHT (16) #define MAZE_WIDTH (16) #define STARTING_POINT_X (13.5f); #define STARTING_POINT_Y (1.5f); #define STARTING_HEADING (90.0f); float player_x = STARTING_POINT_X ; float player_y = STARTING_POINT_Y ; float player_h = STARTING_HEADING ; // player's heading float player_s = 0.0f; // forward speed of the player float player_m = 1.0f; // speed multiplier of the player float player_t = 0.0f; // player's turning (change in heading) float player_b = 0.0f; // viewpoint bank (roll) static float texcoordX=0.0f; int walllist=0; int mazelist=0; int balllist=0; int status=1; bool searchroute=false; bool keystate[4]={false,false,false,false}; char mazedata[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'}, }; void myinit() { 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);//Ê¹ÎÆÀí²»±äÐÎ } bool wall(int x,int y) { return (x>=0 && y>=0 && x<MAZE_WIDTH && y<MAZE_HEIGHT && mazedata[y][x]!=' '); } bool onopen(int x,int y){ if(wall(x,y)){ return(mazedata[y][x]=='H'); } } void closeit(int x,int y) { if(onopen(x,y)) { mazedata[y][x]= 'X'; } } bool neighbor(int x,int y,int w,int *nx,int *ny){ switch(w) { case 0: *nx = x-1; *ny=y; break; case 1: *nx = x; *ny=y+1; break; case 2: *nx = x+1; *ny=y; break; case 3: *nx = x; *ny=y-1; break; default: break; } return wall(*nx,*ny); } bool diagnal(int x,int y,int w,int *nx,int *ny){ switch(w) { case 0: *nx = x-1; *ny=y-1; break; case 1: *nx = x-1; *ny=y+1; break; case 2: *nx = x+1; *ny=y+1; break; case 3: *nx = x+1; *ny=y-1; break; default: break; } return wall(*nx,*ny); } void dw(int x,int y,int p) { int w=p; closeit(x,y); do{ int x2=0; int y2=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 { float fx; float fy; 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 ; } void drawwalls() { 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(); } void drawtop() { int x,y; glBegin(GL_QUADS); for(y=0;y<MAZE_HEIGHT;y++) { for(x=0;x<MAZE_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(); } void forward(float px,float py,float bf) { int x = ((int)player_x); int y = ((int)player_y); int h=0; if((px> x+1.0f - bf) && wall(x+1,y)) { px = (float)(x)+1.0f-bf; h++; } if(py> y+1.0f-bf && wall(x,y+1)) { py = (float)(y)+1.0f-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; } void drawball() { glDisable(GL_TEXTURE_2D); glColor3f(1.0,0.0,0.0); glutSolidSphere(0.2f,15,15); } void navmaze1() { 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; 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(); } void navmaze2() { 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; player_b = 3*player_b/4 + player_t/4; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glOrtho(-16.0,16.0,-16.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(); } void myDisplay() { if(status==1) { if(searchroute==true) { } else navmaze1(); } if(status==3) { if(searchroute==true) { } else navmaze2(); } glFlush(); glutSwapBuffers(); } void myReshape(int w, int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glutPostRedisplay(); } void specialKeys(int key,int x,int y) { switch (key) { case GLUT_KEY_LEFT: keystate[2] = true; player_t = -2.0f; break; case GLUT_KEY_RIGHT: keystate[3] = true; player_t = 2.0f; break; case GLUT_KEY_UP: keystate[0] = true; player_s = 0.01f; break; case GLUT_KEY_DOWN: keystate[1] = true; player_s = -0.01f; break; default:break; } } void keyboard(unsigned char key,int x,int y) { switch (key) { case '1': status=1; break; case '3': status=3; break; default:break; } glutPostRedisplay(); } void upSpecialKeyboard(int key,int x,int y) { switch (key) { case GLUT_KEY_LEFT: keystate[2] = false; player_t = 0.0f; break; case GLUT_KEY_RIGHT: keystate[3] = false; player_t = 0.0f; break; case GLUT_KEY_UP: keystate[0] = false; player_s = 0.0f; break; case GLUT_KEY_DOWN: keystate[1] = false; player_s = 0.0f; break; default:break; } // glutPostRedisplay(); } void idle() { if(keystate[0]||keystate[1]||keystate[2]||keystate[3]) glutPostRedisplay(); else{} }//ÊͷŰ´¼üºó¾Í½øÈë¿ÕÏÐ״̬£¬Èç¹û¿ÕÏÐ״̬²»Ò»Ö±Öظ´»æÍ¼£¬¾Í»á»­ÃæÍ£ÖÍ¡£´Î·½·¨È±µãÊÇ¿ªÏúºÜ´ó void main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow("polygon modeler"); glutDisplayFunc(myDisplay); myinit (); glutSpecialFunc(specialKeys); glutKeyboardFunc(keyboard); glutSpecialUpFunc(upSpecialKeyboard); glutIdleFunc(idle); glutMainLoop(); }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服