D3D11顶点纹理创建
2024-08-01
10
0
在函数gs_vertex_buffer::BuildBuffers()中除了创建顶点坐标外,也有其它相关的坐标创建,但也有一个特殊的就是纹理坐标的创建。
void gs_vertex_buffer::BuildBuffers()
{
InitBuffer(sizeof(vec3), vbd.data->num, vbd.data->points, &vertexBuffer);
if (vbd.data->normals)
InitBuffer(sizeof(vec3), vbd.data->num, vbd.data->normals,&normalBuffer);
if (vbd.data->tangents)
InitBuffer(sizeof(vec3), vbd.data->num, vbd.data->tangents,&tangentBuffer);
if (vbd.data->colors)
InitBuffer(sizeof(uint32_t), vbd.data->num, vbd.data->colors,&colorBuffer);
for (size_t i = 0; i < vbd.data->num_tex; i++) {
struct gs_tvertarray *tverts = vbd.data->tvarray + i;
if (tverts->width != 2 && tverts->width != 4)
throw "Invalid texture vertex size specified";
if (!tverts->array)
throw "No texture vertices specified";
ComPtr<ID3D11Buffer> buffer;
InitBuffer(tverts->width * sizeof(float), vbd.data->num,tverts->array, &buffer);
uvBuffers.push_back(buffer);
uvSizes.push_back(tverts->width * sizeof(float));
}
}
纹理坐析的创建和顶点坐标一样,都是使用的函数InitBuffer实现的。
InitBuffer函数创建了:
- 顶点坐标
ComPtr<ID3D11Buffer> vertexBuffer
- 法线
ComPtr<ID3D11Buffer> normalBuffer
- 正切
ComPtr<ID3D11Buffer> tangentBuffer
- 颜色
ComPtr<ID3D11Buffer> colorBuffer
- 纹理向量
vector<ComPtr<ID3D11Buffer>> uvBuffers
void gs_vertex_buffer::InitBuffer(const size_t elementSize,
const size_t numVerts, void *array,
ID3D11Buffer **buffer)
{
D3D11_BUFFER_DESC bd;
D3D11_SUBRESOURCE_DATA srd;
HRESULT hr;
memset(&bd, 0, sizeof(bd));
memset(&srd, 0, sizeof(srd));
bd.Usage = dynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
bd.CPUAccessFlags = dynamic ? D3D11_CPU_ACCESS_WRITE : 0;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.ByteWidth = UINT(elementSize * numVerts);
srd.pSysMem = array;
hr = device->device->CreateBuffer(&bd, &srd, buffer);
if (FAILED(hr))
throw HRError("Failed to create buffer", hr);
}