Directx11教程 lesson1

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Tutorial1:GridandCameraMovementThistutorialwillcoverthebasicsofrenderingaterraingridusingDirectX10.Beforegettingintomoreadvancedterrainconceptsyoushouldbeabletorenderabasicgridandhavegoodcamerafunctionalityandthatwillbethepurposeofthistutorial.Withagoodcamerathatcanmovearoundtheterraineasilyitcanhelpdebuggingissuesthatoccurduringdevelopment.Havinggooddebuggingtoolsisalwaysthekeytospeedingupdevelopmentandensuringquality.AlsonotethatthistutorialisbasedontheknowledgeandframeworkoftheDirectX10tutorials.Thegridinthistutorialwillbeabasic100x100unitflatterrain.ItwillbecomposedofquadsmadeoutoflinesthatwillbeencapsulatedinanewclasscalledTerrainClass.ThecamerawillalsobebasedonanewclasscalledPositionClass(CameraClassisstillusedtomaketheviewmatrix,I'vejustseparatedthefunctionality).PositionClasswillmaintainthepositionandrotationofthecameraaswellastheaccelerationanddecelerationsothecameramovessmoothlyaroundtheterrain.AndfinallywewillreusetheTextClasstodisplaythefps,cpu,andposition/rotationofthecamera.FrameworkTheframeworkcontainsthenewTerrainClassandPositionClass.TherestoftheclasseshavealreadybeencoveredintheDirectX10tutorialssection.WewillstartthetutorialbylookingatthenewTerrainClass.Terrainclass.hTheterrainclasswillencapsulatethemodeldataandrenderingfunctionalityfordrawingthe100x100linegrid.Thisclasscontainsonlythebasicsfornowsincethistutorialisjustfocusedongettingaverybasicterraindrawnfirst./////////////////////////////////////////////////////////////////////////////////Filename:terrainclass.h////////////////////////////////////////////////////////////////////////////////#ifndef_TERRAINCLASS_H_#define_TERRAINCLASS_H_////////////////INCLUDES////////////////#included3d10.h#included3dx10math.h#includefstreamusingnamespacestd;//////////////////////////////////////////////////////////////////////////////////Classname:TerrainClass////////////////////////////////////////////////////////////////////////////////classTerrainClass{private:Thevertexstructurefortheterrainshaderwilljustbepositionandcolorasweareonlydrawingwhitelinestostartwith.structVertexType{D3DXVECTOR3position;D3DXVECTOR4color;};public:TerrainClass();TerrainClass(constTerrainClass&);~TerrainClass();boolInitialize(ID3D10Device*);voidShutdown();voidRender(ID3D10Device*);intGetIndexCount();private:boolInitializeBuffers(ID3D10Device*);voidShutdownBuffers();voidRenderBuffers(ID3D10Device*);private:ID3D10Buffer*m_vertexBuffer,*m_indexBuffer;intm_vertexCount,m_indexCount;intm_terrainWidth,m_terrainHeight;};#endifTerrainclass.cpp//////////////////////////////////////////////////////////////////////////////////Filename:terrainclass.cpp////////////////////////////////////////////////////////////////////////////////#includeterrainclass.hTheclassconstructorwillinitializethevertexandindexbufferpointerstonull.TerrainClass::TerrainClass(){m_vertexBuffer=0;m_indexBuffer=0;}TerrainClass::TerrainClass(constTerrainClass&other){}TerrainClass::~TerrainClass(){}TheInitializefunctionwillfirstsetthewidthandheightoftheterrainandthencallthefunctionforinitializingthevertexandindexbuffersthatwillholdtheterrainmodeldata.boolTerrainClass::Initialize(ID3D10Device*device){boolresult;//Setthewidthandheightoftheterrain.m_terrainWidth=100;m_terrainHeight=100;//Initializethevertexandindexbufferthatholdthegeometryfortheterrain.result=InitializeBuffers(device);if(!result){returnfalse;}returntrue;}ShutdowncallstheShutdownBuffersfunctiontoreleasethevertexandindexbuffersthatareholdingtheterrainmodeldata.voidTerrainClass::Shutdown(){//Releasethevertexandindexbuffers.ShutdownBuffers();return;}TheRenderfunctioncallsRenderBufferstoputtheterrainmodeldataonthegraphicspipelinesotheColorShaderClassobjectcanthenrenderit.ThisisdoneintheGraphicsClass::Renderfunction.voidTerrainClass::Render(ID3D10Device*device){//Putthevertexandindexbuffersonthegraphicspipelinetopreparethemfordrawing.RenderBuffers(device);return;}GetIndexCountreturnsthenumberofindexesintheterrainmodel.Thisiscalledbytheshadersthatrendertheterrainastheyrequirethisinformationtoperformtherender.intTerrainClass::GetIndexCount(){returnm_indexCount;}TheInitializeBuffersfunctioncreatesthevertexandindexbufferusedtoholdtheterraingriddata.Theterrainisgoingtobecomposedoflinesformingsquares.InDirectX10todrawalineyouneedtwopoints,andtodrawasquareyouneedeightpointstoformthefourlines.Sointhecodebelowyouwillseeaforloopthatcreateseachsquareinthe100x100gridusingeightpointstocreatefourlines.Itsnotefficientbutitisfinejusttoquicklyseesomethingworking.Sincewearen'tloadingamodelIuseavertexarraytocreatetheterraingridandthenIcreateavertexandindexbufferfromthatarray.boolTerrainClass::InitializeBuffers(ID3D10Device*device){VertexType*vertices;unsignedlong*indices;D3D10_BUFFER_DESCvertexBufferDesc,indexBufferDesc;D3D10_SUBRESOURCE_DATAvertexData,indexData;HRESULTresult;inti,j,index;floatpositionX,positionZ;Determinethenumberofverticesinthe100x100mesh.//Calculatethenumberofverticesintheterrainmesh.m_vertexCount=(m_terrainWidth-1)*(m_terrainHeight-1)*8;//Settheindexcount

1 / 36
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功