您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 造纸印刷 > directX综合实验
DirectX角色游戏编程实验指导书信息电子学院实验教学中心学号:E09700227姓名:曾焕凯班级:09数字媒体技术2班实验名称:DirectX三维文字及地形场景实验实验目的通过实验,运用DirectX三维图形编程的知识、原理和技术;掌握在综合三维场景的基础上,设计一个三维文字及地形的实现方法;通过使用VC++编程环境实现三维场景中的三维文字及地形的设计;算法实现的设计方法及程序流程图(一)在VC++编程环境下建立三维场景,场景中包括:茶壶模型体模型。茶壶模型使用黄色材质。圆柱体模型使用纹理贴图材质。设置一个电光源灯光系统。流程:代码主要在原有代码基础上修改得到,先将需要的头文件包含进来#includecube.h#includevertex.h之后对照两个例子的代码,将两个代码结合到一起即可。具体见下面的代码。(二)在VC++编程环境下建立一个三维文字及地形的设计。1.在三维场景中,使用地形文件建立一个三维地形。2.同时输出三维文字,文字内容包括:学生名字、学号。流程:代码主要参考十三章以及第九章代码,将两者融合(主要在terrainDriver.cpp这个文件中进行修改)。期间注意:HDChdc=CreateCompatibleDC(0);HFONThFont;HFONThFontOld;这段代码从d3dxcreatetext.cpp中引入到terrainDriver.cpp时会导致程序报错,经测试主要是因为在之前源文件夹中的d3dfont.cpp中已经定义过了。解决方案是:去掉原来程序左上角的fps显示,以解决冲突。具体方法:删除工程中d3dfont.cpp,d3dfont.h,以及其他无用的文件fps.h,fps.cpp.再在d3dxcreatetext.cpp中删除有关fps显示的响应语句:#includefps.hFPSCounter*FPS=0;d3d::DeleteFPSCounter*(FPS);FPS=newFPSCounter(Device);if(FPS)FPS-render(0xffffffff,timeDelta);等语句。Fps显示功能即可去除,最后拼合两段代码即可,具体代码见下方。(下面的代码去掉了文字旋转功能,并将地形修改的相对简单,以方便字体显示。)(三)建立一个三维文字及地形的设计并加入(一)中内容再加入粒子系统。结合一,二两个实验。先将代码2中缺少的头文件移入实验2相应文件夹,托入vc头文件。打开terrainDriver.cpp,包含相应头文件#includecube.h#includevertex.h#includepSystem.h#includecstdlib#includectime其次对照两个实验的代码,通过比较分析,将实验一中的茶壶及已赋予材质的立方体加入地形中。然后为该场景再加入粒子系统。具体见下面代码。实验中途遇到过几个问题:1.文字无法显示,及显示颜色不正确。经测试在绘制场景时加入Device-SetMaterial(&d3d::GREEN_MTRL);Text-DrawSubset(0);2.程序运行退出时出现上述错误,测试发现是Cleanup()中多了一个d3d::ReleaseID3DXMesh*(Text);即可。程序结果分析主要代码(一)#included3dUtility.h#includecube.h#includevertex.h//GlobalsIDirect3DDevice9*Device=0;constintWidth=640;constintHeight=480;Cube*Box=0;IDirect3DTexture9*Tex=0;ID3DXMesh*Objects[4]={0,0,0,0};D3DXMATRIXWorlds[4];D3DMATERIAL9Mtrls[4];//FrameworkFunctionsboolSetup(){//Createobjects.D3DXCreateTeapot(Device,&Objects[0],0);Box=newCube(Device);//Buildworldmatrices-positiontheobjectsinworldspace.D3DXMatrixTranslation(&Worlds[0],0.0f,0.0f,0.0f);D3DXMatrixTranslation(&Worlds[1],0.0f,-2.0f,0.0f);//Setuptheobject'smaterials.Mtrls[0]=d3d::YELLOW_MTRL;Mtrls[1]=d3d::WHITE_MTRL;//Setupadirectionallight.D3DXVECTOR3dir(1.0f,-0.0f,0.25f);D3DXCOLORc=d3d::WHITE;D3DLIGHT9dirLight=d3d::InitDirectionalLight(&dir,&c);//SetandEnablethelight.Device-SetLight(0,&dirLight);Device-LightEnable(0,true);//Setlightingrelatedrenderstates.Device-SetRenderState(D3DRS_NORMALIZENORMALS,true);Device-SetRenderState(D3DRS_SPECULARENABLE,false);//Createtexture.D3DXCreateTextureFromFile(Device,crate.jpg,&Tex);//SetTextureFilterStates.Device-SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);Device-SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);Device-SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);//Settheprojectionmatrix.D3DXMATRIXproj;D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI*0.25f,//45-degree(float)Width/(float)Height,1.0f,1000.0f);Device-SetTransform(D3DTS_PROJECTION,&proj);returntrue;}voidCleanup(){for(inti=0;i1;i++)d3d::ReleaseID3DXMesh*(Objects[i]);d3d::DeleteCube*(Box);d3d::ReleaseIDirect3DTexture9*(Tex);}boolDisplay(floattimeDelta){if(Device){//Updatethescene:updatecameraposition.staticfloatangle=(3.0f*D3DX_PI)/2.0f;staticfloatheight=5.0f;if(::GetAsyncKeyState(VK_LEFT)&0x8000f)angle-=0.5f*timeDelta;if(::GetAsyncKeyState(VK_RIGHT)&0x8000f)angle+=0.5f*timeDelta;if(::GetAsyncKeyState(VK_UP)&0x8000f)height+=5.0f*timeDelta;if(::GetAsyncKeyState(VK_DOWN)&0x8000f)height-=5.0f*timeDelta;D3DXVECTOR3position(cosf(angle)*7.0f,height,sinf(angle)*7.0f);D3DXVECTOR3target(0.0f,0.0f,0.0f);D3DXVECTOR3up(0.0f,1.0f,0.0f);D3DXMATRIXV;D3DXMatrixLookAtLH(&V,&position,&target,&up);Device-SetTransform(D3DTS_VIEW,&V);//Drawthescene:Device-Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0x00000000,1.0f,0);Device-BeginScene();for(inti=0;i1;i++){//setmaterialandworldmatrixforithobject,thenrender//theithobject.Device-SetMaterial(&Mtrls[i]);Device-SetTransform(D3DTS_WORLD,&Worlds[i]);Objects[i]-DrawSubset(0);}Device-SetMaterial(&d3d::WHITE_MTRL);Device-SetTexture(0,Tex);Box-draw(&Worlds[1],0,0);Device-SetTexture(0,NULL);Device-EndScene();Device-Present(0,0,0,0);}returntrue;}//WndProcLRESULTCALLBACKd3d::WndProc(HWNDhwnd,UINTmsg,WPARAMwParam,LPARAMlParam){switch(msg){caseWM_DESTROY:::PostQuitMessage(0);break;caseWM_KEYDOWN:if(wParam==VK_ESCAPE)::DestroyWindow(hwnd);break;}return::DefWindowProc(hwnd,msg,wParam,lParam);}//WinMain//intWINAPIWinMain(HINSTANCEhinstance,HINSTANCEprevInstance,PSTRcmdLine,intshowCmd){if(!d3d::InitD3D(hinstance,Width,Height,true,D3DDEVTYPE_HAL,&Device)){::MessageBox(0,InitD3D()-FAILED,0,0);return0;}if(!Setup()){::MessageBox(0,Setup()-FAILED,0,0);return0;}d3d::EnterMsgLoop(Display);Cleanup();Device-Release();return0;}(二)#included3dUtility.h#includeterrain.h#includecamera.hIDirect3DDevice9*Device=0;constintWidth=1000;constintHeight=700;Terrain*TheTerrain=0;CameraTheCamera(Camera::LANDOBJECT);ID3DXMesh*Text=0;boolSetup(){D3DXVECTOR3lightDirection(0.0f,1.0f,0.0f);TheTerrain=newTerrain(Device,coastMountain641.raw,64,64,10,0.5f);TheTerrain-genTexture(&lightDirection);HDChdc=CreateCompatibleDC(0);HFONThFont;HFONThFontOld;LOGFONTlf;ZeroMemory(&lf,sizeof(LOGFONT));
本文标题:directX综合实验
链接地址:https://www.777doc.com/doc-3542238 .html