本文主要是介绍directx学习之在屏幕上画一个三角形,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前一张已经输出了一个背景为紫色的屏幕,这一节的目标是在该屏幕上输出一个三角形。下面将逐一介绍建立一个三角形的过程。
一个三角形有三个点,叫做顶点。三个点的不同的集合可以建立不同的三角形。能让GPU创建一个三角形,必须将三个顶点的位置告诉它。下面是一个2D的例子。
怎么将三个点的数据传给GPU?
在d3d10中,顶点数据被存在一个缓存资源中。但是应该申请多大的缓存,这就是接下来的问题。
一个顶点就是一个位置,通常也包含一些其他的属性,比如颜色,纹理协调等。顶点结构就定义了那些属性在内存中的位置。
类似C语言的结构体,一个顶点通常被一个结构体来表示。顶点的大小就是结构体的大小。
在这里,我们只关心顶点的位置,因此我们定义我们自己的结构体仅有一个D3DXVECTOR3。整个类型是3个float点的容器,代表位置。
struct SimpleVertex
{
D3DXVECTOR3 pos;//position
};
现在如果将上面的结构传给GPU,对它来讲,这就是一块内存区域。为了从缓存中提取有用的属性信息,GPU必须知道我们设置的结构体的布局,为了达到这个目的,我们使用输入布局。
在D3D10中,输入布局也是一个描述结构体的D3D10对象。
每一个顶点的属性可以被D3D10_INPUT_ELEMENT_DESC来描述,应用程序定义一个数组包含一个或者多个D3D10_INPUT_ELEMENT_DESC,然后用该数组作为输入布局来描述顶点。
<span style="font-family:KaiTi_GB2312;font-size:18px;">typedef struct D3D10_INPUT_ELEMENT_DESC {LPCSTR SemanticName;UINT SemanticIndex;DXGI_FORMAT Format;UINT InputSlot;UINT AlignedByteOffset;D3D10_INPUT_CLASSIFICATION InputSlotClass;UINT InstanceDataStepRate;
} D3D10_INPUT_ELEMENT_DESC;</span>
Semant icName | Semantic name is a string containing a word that describes the nature or purpose (or semantics) of this element. The word can be in any form that a C identifier can, and can be anything that we choose. For instance, a good semantic name for the vertex's position is POSITION. Semantic names are not case-sensitive. |
SemanticIndex | Semantic index supplements semantic name. A vertex may have multiple attributes of the same nature. For example, it may have 2 sets of texture coordinates or 2 sets of colors. Instead of using semantic names that have numbers appended, such as "COLOR0" and "COLOR1", the two elements can share a single semantic name, "COLOR", with different semantic indices 0 and 1. |
Format | Format defines the data type to be used for this element. For instance, a format of DXGI_FORMAT_R32G32B32_FLOAT has three 32-bit floating point numbers, making the element 12-byte long. A format of DXGI_FORMAT_R16G16B16A16_UINT has four 16-bit unsigned integers, making the element 8 bytes long. |
InputSlot | As mentioned previously, a Direct3D 10 application passes vertex data to the GPU via the use of vertex buffer. In Direct3D 10, multiple vertex buffers can be fed to the GPU simultaneously, 16 to be exact. Each vertex buffer is bound to an input slot number ranging from 0 to 15. The InputSlot field tells the GPU which vertex buffer it should fetch for this element. |
AlignedByteOffset | A vertex is stored in a vertex buffer, which is simply a chunk of memory. The AlignedByteOffset field tells the GPU the memory location to start fetching the data for this element. |
InputSlotClass | This field usually has the value D3D10_INPUT_PER_VERTEX_DATA. When an application uses instancing, it can set an input layout's InputSlotClass to D3D10_INPUT_PER_INSTANCE_DATA to work with vertex buffer containing instance data. Instancing is an advanced Direct3D topic and will not be discussed here. For our tutorial, we will use D3D10_INPUT_PER_VERTEX_DATA exclusively. |
// Define the input layout D3D10_INPUT_ELEMENT_DESC layout[] = {{ L"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = sizeof(layout)/sizeof(layout[0]);
顶点布局:
// Create the input layout D3D10_PASS_DESC PassDesc; g_pTechnique->GetPassByIndex( 0
这篇关于directx学习之在屏幕上画一个三角形的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!