1、DirectX 10 教程 7:绘制 3D 模型原文地址: Tutorial 7: 3D Model Rendering。本教程介绍在 DirectX 10 中使用 HLSL 绘制 3D 模型,代码基于上一个漫反射光照的教程。在上一个教程中我们绘制了一个 3D 模型,只是一个简单的三角形很容易理解。本教程我们要绘制一个更复杂的对象立方体,但在这之前需要讨论一下模型格式。有很多工具可以创建 3D 模型,Maya 和 3D Studio Max 是两个较为流行的建模工具,其他的工具功能弱一点,但都能完成基本的工作。无论使用的是何种工具,你总要将做好的模型导出为不同的格式。建议你定义自己的模型格式,
2、然后编写一个转换程序将输出格式转换为自己的格式。因为你有可能改变使用的 3D 建模工具,模型格式也会随之改变,因此会处理不同的格式。如果你使用自己的格式,并将不同的其他格式转换为自己的格式,那么你的程序就无需进行修改,只需修改你的格式转换程序即可。而且大多数 3D 建模工具会导出只适用于它们的无关数据,在你的模型格式中无需使用这些数据。使用自己的格式最重要的就是格式是否满足你的需要,使用起来是否简便。你也可以考虑对不同的对象使用不同的格式,例如有些模型包含动画,而有些模型是静态的。我要介绍的模型格式非常基本。每行对应一个顶点,包含位置矢量(x, y, z),纹理坐标(tu, tv)和法线矢量(
3、nx, ny, nz)。在文件顶部还包含顶点的数量,你可以在第一行就读取这个信息,这样在读取顶点数据之前就可以分配大小正确的内存用于数组。此文件格式每三行数据构成一个三角形,顶点的排列顺序是是顺时针方向。下面是模型文件的内容:Cube.txtVertex Count: 36Data:-1.0 1.0 -1.0 0.0 0.0 0.0 0.0 -1.01.0 1.0 -1.0 1.0 0.0 0.0 0.0 -1.0-1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.0-1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.01.0 1.0 -1.0 1.0 0.0
4、0.0 0.0 -1.01.0 -1.0 -1.0 1.0 1.0 0.0 0.0 -1.01.0 1.0 -1.0 0.0 0.0 1.0 0.0 0.01.0 1.0 1.0 1.0 0.0 1.0 0.0 0.01.0 -1.0 -1.0 0.0 1.0 1.0 0.0 0.01.0 -1.0 -1.0 0.0 1.0 1.0 0.0 0.01.0 1.0 1.0 1.0 0.0 1.0 0.0 0.01.0 -1.0 1.0 1.0 1.0 1.0 0.0 0.01.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0-1.0 1.0 1.0 1.0 0.0 0.0 0.0 1.
5、01.0 -1.0 1.0 0.0 1.0 0.0 0.0 1.01.0 -1.0 1.0 0.0 1.0 0.0 0.0 1.0-1.0 1.0 1.0 1.0 0.0 0.0 0.0 1.0-1.0 -1.0 1.0 1.0 1.0 0.0 0.0 1.0-1.0 1.0 1.0 0.0 0.0 -1.0 0.0 0.0-1.0 1.0 -1.0 1.0 0.0 -1.0 0.0 0.0-1.0 -1.0 1.0 0.0 1.0 -1.0 0.0 0.0-1.0 -1.0 1.0 0.0 1.0 -1.0 0.0 0.0-1.0 1.0 -1.0 1.0 0.0 -1.0 0.0 0.0-
6、1.0 -1.0 -1.0 1.0 1.0 -1.0 0.0 0.0-1.0 1.0 1.0 0.0 0.0 0.0 1.0 0.01.0 1.0 1.0 1.0 0.0 0.0 1.0 0.0-1.0 1.0 -1.0 0.0 1.0 0.0 1.0 0.0-1.0 1.0 -1.0 0.0 1.0 0.0 1.0 0.01.0 1.0 1.0 1.0 0.0 0.0 1.0 0.01.0 1.0 -1.0 1.0 1.0 0.0 1.0 0.0-1.0 -1.0 -1.0 0.0 0.0 0.0 -1.0 0.01.0 -1.0 -1.0 1.0 0.0 0.0 -1.0 0.0-1.0
7、-1.0 1.0 0.0 1.0 0.0 -1.0 0.0-1.0 -1.0 1.0 0.0 1.0 0.0 -1.0 0.01.0 -1.0 -1.0 1.0 0.0 0.0 -1.0 0.01.0 -1.0 1.0 1.0 1.0 0.0 -1.0 0.0你可以看到有 36 行 x,y,z,tu,tv,nx ,ny,nz 数据。每三行构成一个三角形,一共 12 个三角形构成一个立方体。数据格式非常简单,可以不加修改被读取到顶点缓存。值得注意的是,有些 3D 建模工具以不同的顺序输出数据,例如顺时针方向和逆时针方向坐标系统,你需要记住 DirectX 10 默认为左手坐标系,所以模型数据也必
8、须加以匹配。留心这些区别,确保你的转换程序将数据转换为正确的格式/顺序。Modelclass.h本教程中我们需要对 ModelClass 做一点小的修改使之可以绘制来自于文本文件中的 3D模型。/ Filename: modelclass.h/#ifndef _MODELCLASS_H_#define _MODELCLASS_H_需要包含 fstream 库处理模型文本文件的读取。/ INCLUDES /#include using namespace std;/ MY CLASS INCLUDES /#include “textureclass.h“/ Class name: ModelCl
9、ass/class ModelClassprivate:struct VertexTypeD3DXVECTOR3 position;D3DXVECTOR2 texture;D3DXVECTOR3 normal;下一个变化是添加了一个新结构表示模型格式,名称为 ModelType。它包含了位置,纹理坐标和法线矢量。struct ModelTypefloat x, y, z;float tu, tv;float nx, ny, nz;public:ModelClass();ModelClass(const ModelClassModelClass();Initialize 方法的一个参数为要加载的
10、模型的文件名称。bool Initialize(ID3D10Device*, char*, WCHAR*);void Shutdown();void Render(ID3D10Device*);int GetIndexCount();ID3D10ShaderResourceView* GetTexture();private:bool InitializeBuffers(ID3D10Device*);void ShutdownBuffers();void RenderBuffers(ID3D10Device*);bool LoadTexture(ID3D10Device*, WCHAR*);v
11、oid ReleaseTexture();还添加了两个新方法加载和卸载来自于文本文件的模型数据。bool LoadModel(char*);void ReleaseModel();private:ID3D10Buffer *m_vertexBuffer, *m_indexBuffer;int m_vertexCount, m_indexCount;TextureClass* m_Texture;最后的变化是一个叫做 m_model 的私有变量,对应新的结构体 ModelType 的数组。这个变量用来在发送到顶点缓存之前读取和保存模型数据。ModelType* m_model;#endifMod
12、elclass.cpp/ Filename: modelclass.cpp/#include “modelclass.h“ModelClass:ModelClass()m_vertexBuffer = 0;m_indexBuffer = 0;m_Texture = 0;在构造函数中将模型结构体设置为 null。m_model = 0;ModelClass:ModelClass(const ModelClass在 Initialize 方法中我们首先调用 LoadModel 方法,这个方法将模型数据加载到m_model 数组中。填充了这个数组后,我们就可以基于它创建顶点和索引缓存了。因为Init
13、ializeBuffers 方法需要使用模型数据,所以你需要确保以正确的顺序调用这个方法。/ Load in the model data.result = LoadModel(modelFilename);if(!result)return false;/ Initialize the vertex and index buffers.result = InitializeBuffers(device);if(!result)return false;/ Load the texture for this model.result = LoadTexture(device, textureF
14、ilename);if(!result)return false;return true;void ModelClass:Shutdown()/ Release the model texture.ReleaseTexture();/ Shutdown the vertex and index buffers.ShutdownBuffers();在 Shutdown 方法中,我们添加了调用 ReleaseModel 方法,用来清除 m_model 数组数据。/ Release the model data.ReleaseModel();return;void ModelClass:Render
15、(ID3D10Device* device)/ Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.RenderBuffers(device);return;int ModelClass:GetIndexCount()return m_indexCount;ID3D10ShaderResourceView* ModelClass:GetTexture()return m_Texture-GetTexture();bool ModelClass:InitializeBuffer
16、s(ID3D10Device* device)VertexType* vertices;unsigned long* indices;D3D10_BUFFER_DESC vertexBufferDesc, indexBufferDesc;D3D10_SUBRESOURCE_DATA vertexData, indexData;HRESULT result;int i;注意我们现在不需要手动设置顶点和索引数量,我们可以在 ModelClass:LoadModel 方法中读取顶点和索引数量。/ Create the vertex array.vertices = new VertexTypem_v
17、ertexCount;if(!vertices)return false;/ Create the index array.indices = new unsigned longm_indexCount;if(!indices)return false;加载顶点和索引数组有一点变化。现在不需要手动设置,而是遍历 m_model 数组中的数据,将它们复制到顶点数组中。索引数组很容易创建,因为每个顶点的索引值与数组中的索引值相同。/ Load the vertex array and index array with data.for(i=0; iCreateBuffer(if(FAILED(re
18、sult)return false;/ Set up the description of the index buffer.indexBufferDesc.Usage = D3D10_USAGE_DEFAULT;indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;indexBufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;indexBufferDesc.CPUAccessFlags = 0;indexBufferDesc.MiscFlags = 0;/ Give the
19、subresource structure a pointer to the index data.indexData.pSysMem = indices;/ Create the index buffer.result = device-CreateBuffer(if(FAILED(result)return false;/ Release the arrays now that the vertex and index buffers have been created and loaded.delete vertices;vertices = 0;delete indices;indic
20、es = 0;return true;void ModelClass:ShutdownBuffers()/ Release the index buffer.if(m_indexBuffer)m_indexBuffer-Release();m_indexBuffer = 0;/ Release the vertex buffer.if(m_vertexBuffer)m_vertexBuffer-Release();m_vertexBuffer = 0;return;void ModelClass:RenderBuffers(ID3D10Device* device)unsigned int s
21、tride;unsigned int offset;/ Set vertex buffer stride and offset.stride = sizeof(VertexType); offset = 0;/ Set the vertex buffer to active in the input assembler so it can be rendered.device-IASetVertexBuffers(0, 1, / Set the index buffer to active in the input assembler so it can be rendered.device-
22、IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);/ Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.device-IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);return;bool ModelClass:LoadTexture(ID3D10Device* device, WCHAR* filename)b
23、ool result;/ Create the texture object.m_Texture = new TextureClass;if(!m_Texture)return false;/ Initialize the texture object.result = m_Texture-Initialize(device, filename);if(!result)return false;return true;void ModelClass:ReleaseTexture()/ Release the texture object.if(m_Texture)m_Texture-Shutd
24、own();delete m_Texture;m_Texture = 0;return;新的 LoadModel 方法处理从文本文件中将模型数据加载到 m_model 数组变量中。首先打开文本文件读取顶点数量,之后创建 ModelType 数组并读取每一行的数据到这个数组中。顶点或索引数量也是在这个方法中设置的。bool ModelClass:LoadModel(char* filename)ifstream fin;char input;int i;/ Open the model file.fin.open(filename);/ If it could not open the file
25、 then exit.if(fin.fail()return false;/ Read up to the value of vertex count.fin.get(input);while(input != :)fin.get(input);/ Read in the vertex count.fin m_vertexCount;/ Set the number of indices to be the same as the vertex count.m_indexCount = m_vertexCount;/ Create the model using the vertex coun
26、t that was read in.m_model = new ModelTypem_vertexCount;if(!m_model)return false;/ Read up to the beginning of the data.fin.get(input);while(input != :)fin.get(input);fin.get(input);fin.get(input);/ Read in the vertex data.for(i=0; i m_modeli.x m_modeli.y m_modeli.z;fin m_modeli.tu m_modeli.tv;fin m
27、_modeli.nx m_modeli.ny m_modeli.nz;/ Close the model file.fin.close();return true;ReleaseModel 方法删除模型数据数组。void ModelClass:ReleaseModel()if(m_model)delete m_model;m_model = 0;return;Graphicsclass.hGraphicsClass 的头文件与上一个教程一样。/ Filename: graphicsclass.h/#ifndef _GRAPHICSCLASS_H_#define _GRAPHICSCLASS_H
28、_/ MY CLASS INCLUDES /#include “d3dclass.h“#include “cameraclass.h“#include “modelclass.h“#include “lightshaderclass.h“#include “lightclass.h“/ GLOBALS /const bool FULL_SCREEN = true;const bool VSYNC_ENABLED = true;const float SCREEN_DEPTH = 1000.0f;const float SCREEN_NEAR = 0.1f;/ Class name: Graph
29、icsClass/class GraphicsClasspublic:GraphicsClass();GraphicsClass(const GraphicsClassGraphicsClass();bool Initialize(int, int, HWND);void Shutdown();bool Frame();private:bool Render(float);private:D3DClass* m_D3D;CameraClass* m_Camera;ModelClass* m_Model;LightShaderClass* m_LightShader;LightClass* m_
30、Light;#endifGraphicsclass.cpp/ Filename: graphicsclass.cpp/#include “graphicsclass.h“GraphicsClass:GraphicsClass()m_D3D = 0;m_Camera = 0;m_Model = 0;m_LightShader = 0;m_Light = 0;GraphicsClass:GraphicsClass(const GraphicsClass/ Create the Direct3D object.m_D3D = new D3DClass;if(!m_D3D)return false;/
31、 Initialize the Direct3D object.result = m_D3D-Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR);if(!result)MessageBox(hwnd, L“Could not initialize Direct3D“, L“Error“, MB_OK);return false;/ Create the camera object.m_Camera = new CameraClass;if(!m_Ca
32、mera)return false;/ Create the model object.m_Model = new ModelClass;if(!m_Model)return false;模型的初始化方法的参数包含要加载的模型文件名称,本教程中我用的是 cube.txt。/ Initialize the model object.result = m_Model-Initialize(m_D3D-GetDevice(), “/Engine/data/cube.txt“, L“/Engine/data/seafloor.dds“);if(!result)MessageBox(hwnd, L“Co
33、uld not initialize the model object.“, L“Error“, MB_OK);return false;/ Create the light shader object.m_LightShader = new LightShaderClass;if(!m_LightShader)return false;/ Initialize the light shader object.result = m_LightShader-Initialize(m_D3D-GetDevice(), hwnd);if(!result)MessageBox(hwnd, L“Coul
34、d not initialize the light shader object.“, L“Error“, MB_OK);return false;/ Create the light object.m_Light = new LightClass;if(!m_Light)return false;本教程中我将光线颜色变为白色。/ Initialize the light object.m_Light-SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f);m_Light-SetDirection(0.0f, 0.0f, 1.0f);return true;void G
35、raphicsClass:Shutdown()/ Release the light object.if(m_Light)delete m_Light;m_Light = 0;/ Release the light shader object.if(m_LightShader)m_LightShader-Shutdown();delete m_LightShader;m_LightShader = 0;/ Release the model object.if(m_Model)m_Model-Shutdown();delete m_Model;m_Model = 0;/ Release the
36、 camera object.if(m_Camera)delete m_Camera;m_Camera = 0;/ Release the D3D object.if(m_D3D)m_D3D-Shutdown();delete m_D3D;m_D3D = 0;return;bool GraphicsClass:Frame()bool result;static float rotation = 0.0f;/ Update the rotation variable each frame.rotation += (float)D3DX_PI * 0.01f;if(rotation 360.0f)
37、rotation -= 360.0f;/ Render the graphics scene.result = Render(rotation);if(!result)return false;return true;bool GraphicsClass:Render(float rotation)D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix;/ Clear the buffers to begin the scene.m_D3D-BeginScene(0.0f, 0.0f, 0.0f, 1.0f);/ Generate the vi
38、ew matrix based on the cameras position.m_Camera-Render();/ Get the world, view, and projection matrices from the camera and d3d objects.m_Camera-GetViewMatrix(viewMatrix);m_D3D-GetWorldMatrix(worldMatrix);m_D3D-GetProjectionMatrix(projectionMatrix);/ Rotate the world matrix by the rotation value so
39、 that the triangle will spin.D3DXMatrixRotationY(/ Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.m_Model-Render(m_D3D-GetDevice();/ Render the model using the light shader.m_LightShader-Render(m_D3D-GetDevice(), m_Model-GetIndexCount(), worldMatrix, view
40、Matrix, projectionMatrix, m_Model-GetTexture(),m_Light-GetDirection(), m_Light-GetDiffuseColor();/ Present the rendered scene to the screen.m_D3D-EndScene();return true;总结通过修改 ModelClass,我们现在可以加载 3D 模型并进行绘制了。虽然本教程中使用的格式只能用于含有基本光照的静态对象,但这是一个理解模型格式非常好的一个入门教程。练习1编译代码并运行程序,你可以看到一个带有纹理的旋转立方体,按 escape 键退出。2找一个不错的 3D 建模工具( 希望是免费的)并创建一个简单的模型,导出后看看它们的格式。3编写一个简单的转换程序,将导出的模型转换为本教程使用的格式,替换掉cube.txt 看看运行效果。