void FogDemo::CreateVertexBuffer(ID3D11Device* device, const std::shared_ptr<Mesh>& mesh, ID3D11Buffer** vertexBuffer) const { const std::vector<XMFLOAT3>& sourceVertices = mesh->Vertices(); std::shared_ptr<std::vector<XMFLOAT3>> textureCoordinates = mesh->TextureCoordinates().at(0); assert(textureCoordinates->size() == sourceVertices.size()); const std::vector<XMFLOAT3>& normals = mesh->Normals(); assert(normals.size() == sourceVertices.size()); std::vector<VertexPositionTextureNormal> vertices; vertices.reserve(sourceVertices.size()); for (UINT i = 0; i < sourceVertices.size(); i++) { XMFLOAT3 position = sourceVertices.at(i); XMFLOAT3 uv = textureCoordinates->at(i); XMFLOAT3 normal = normals.at(i); vertices.push_back(VertexPositionTextureNormal(XMFLOAT4(position.x, position.y, position.z, 1.0f), XMFLOAT2(uv.x, uv.y), normal)); } D3D11_BUFFER_DESC vertexBufferDesc; ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc)); vertexBufferDesc.ByteWidth = (UINT)(sizeof(VertexPositionTextureNormal) * vertices.size()); vertexBufferDesc.Usage = D3D11_USAGE_IMMUTABLE; vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; D3D11_SUBRESOURCE_DATA vertexSubResourceData; ZeroMemory(&vertexSubResourceData, sizeof(vertexSubResourceData)); vertexSubResourceData.pSysMem = &vertices[0]; ThrowIfFailed(device->CreateBuffer(&vertexBufferDesc, &vertexSubResourceData, vertexBuffer), "ID3D11Device::CreateBuffer() failed."); }
void SpecularLightingDemo::CreateVertexBuffer(const Mesh& mesh, ID3D11Buffer** vertexBuffer) const { const vector<XMFLOAT3>& sourceVertices = mesh.Vertices(); const vector<XMFLOAT3>& sourceNormals = mesh.Normals(); const auto& sourceUVs = mesh.TextureCoordinates().at(0); vector<VertexPositionTextureNormal> vertices; vertices.reserve(sourceVertices.size()); for (UINT i = 0; i < sourceVertices.size(); i++) { const XMFLOAT3& position = sourceVertices.at(i); const XMFLOAT3& uv = sourceUVs->at(i); const XMFLOAT3& normal = sourceNormals.at(i); vertices.push_back(VertexPositionTextureNormal(XMFLOAT4(position.x, position.y, position.z, 1.0f), XMFLOAT2(uv.x, uv.y), normal)); } D3D11_BUFFER_DESC vertexBufferDesc = { 0 }; vertexBufferDesc.ByteWidth = sizeof(VertexPositionTextureNormal) * static_cast<UINT>(vertices.size()); vertexBufferDesc.Usage = D3D11_USAGE_IMMUTABLE; vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; D3D11_SUBRESOURCE_DATA vertexSubResourceData = { 0 }; vertexSubResourceData.pSysMem = &vertices[0]; ThrowIfFailed(mGame->Direct3DDevice()->CreateBuffer(&vertexBufferDesc, &vertexSubResourceData, vertexBuffer), "ID3D11Device::CreateBuffer() failed."); }