您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 资本运营 > 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
本文标题:Directx11教程 lesson1
链接地址:https://www.777doc.com/doc-3300648 .html