void DXBox::BuildBuffers(ID3D11Device* md3dDevice) { GeometryGenerator::MeshData box; GeometryGenerator geoGen; geoGen.CreateBox(1.0f, 1.0f, 1.0f, box); // Cache the vertex offsets to each object in the concatenated vertex buffer. mBoxVertexOffset = 0; // Cache the index count of each object. mBoxIndexCount = box.Indices.size(); // Cache the starting index for each object in the concatenated index buffer. mBoxIndexOffset = 0; UINT totalVertexCount = box.Vertices.size(); UINT totalIndexCount = mBoxIndexCount; // Extract the vertex elements we are interested in and pack the // vertices of all the meshes into one vertex buffer. std::vector<Vertex::Basic32> vertices(totalVertexCount); UINT k = 0; for(size_t i = 0; i < box.Vertices.size(); ++i, ++k) { vertices[k].Pos = box.Vertices[i].Position; vertices[k].Normal = box.Vertices[i].Normal; vertices[k].Tex = box.Vertices[i].TexC; } D3D11_BUFFER_DESC vbd; vbd.Usage = D3D11_USAGE_IMMUTABLE; vbd.ByteWidth = sizeof(Vertex::Basic32) * totalVertexCount; vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = 0; vbd.MiscFlags = 0; D3D11_SUBRESOURCE_DATA vinitData; vinitData.pSysMem = &vertices[0]; HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mBoxVB)); // Pack the indices of all the meshes into one index buffer. std::vector<UINT> indices; indices.insert(indices.end(), box.Indices.begin(), box.Indices.end()); D3D11_BUFFER_DESC ibd; ibd.Usage = D3D11_USAGE_IMMUTABLE; ibd.ByteWidth = sizeof(UINT) * totalIndexCount; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; D3D11_SUBRESOURCE_DATA iinitData; iinitData.pSysMem = &indices[0]; HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mBoxIB)); }
void WavesCSApp::BuildBoxGeometry() { GeometryGenerator geoGen; GeometryGenerator::MeshData box = geoGen.CreateBox(8.0f, 8.0f, 8.0f, 3); std::vector<Vertex> vertices(box.Vertices.size()); for (size_t i = 0; i < box.Vertices.size(); ++i) { auto& p = box.Vertices[i].Position; vertices[i].Pos = p; vertices[i].Normal = box.Vertices[i].Normal; vertices[i].TexC = box.Vertices[i].TexC; } const UINT vbByteSize = (UINT)vertices.size() * sizeof(Vertex); std::vector<std::uint16_t> indices = box.GetIndices16(); const UINT ibByteSize = (UINT)indices.size() * sizeof(std::uint16_t); auto geo = std::make_unique<MeshGeometry>(); geo->Name = "boxGeo"; ThrowIfFailed(D3DCreateBlob(vbByteSize, &geo->VertexBufferCPU)); CopyMemory(geo->VertexBufferCPU->GetBufferPointer(), vertices.data(), vbByteSize); ThrowIfFailed(D3DCreateBlob(ibByteSize, &geo->IndexBufferCPU)); CopyMemory(geo->IndexBufferCPU->GetBufferPointer(), indices.data(), ibByteSize); geo->VertexBufferGPU = d3dUtil::CreateDefaultBuffer(md3dDevice.Get(), mCommandList.Get(), vertices.data(), vbByteSize, geo->VertexBufferUploader); geo->IndexBufferGPU = d3dUtil::CreateDefaultBuffer(md3dDevice.Get(), mCommandList.Get(), indices.data(), ibByteSize, geo->IndexBufferUploader); geo->VertexByteStride = sizeof(Vertex); geo->VertexBufferByteSize = vbByteSize; geo->IndexFormat = DXGI_FORMAT_R16_UINT; geo->IndexBufferByteSize = ibByteSize; SubmeshGeometry submesh; submesh.IndexCount = (UINT)indices.size(); submesh.StartIndexLocation = 0; submesh.BaseVertexLocation = 0; geo->DrawArgs["box"] = submesh; mGeometries["boxGeo"] = std::move(geo); }
void BlendApp::BuildCrateGeometryBuffers() { GeometryGenerator::MeshData box; GeometryGenerator geoGen; geoGen.CreateBox(1.0f, 1.0f, 1.0f, box); // // Extract the vertex elements we are interested in and pack the // vertices of all the meshes into one vertex buffer. // std::vector<Vertex> vertices(box.Vertices.size()); for(UINT i = 0; i < box.Vertices.size(); ++i) { vertices[i].Pos = box.Vertices[i].Position; vertices[i].Normal = box.Vertices[i].Normal; vertices[i].Tex = box.Vertices[i].TexC; } D3D11_BUFFER_DESC vbd; ZeroMemory(&vbd, sizeof(vbd)); vbd.Usage = D3D11_USAGE_IMMUTABLE; vbd.ByteWidth = sizeof(Vertex) * box.Vertices.size(); vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = 0; vbd.MiscFlags = 0; D3D11_SUBRESOURCE_DATA vinitData; vinitData.pSysMem = &vertices[0]; HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mBoxVB)); // // Pack the indices of all the meshes into one index buffer. // D3D11_BUFFER_DESC ibd; ZeroMemory(&ibd, sizeof(ibd)); ibd.Usage = D3D11_USAGE_IMMUTABLE; ibd.ByteWidth = sizeof(UINT) * box.Indices.size(); ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; D3D11_SUBRESOURCE_DATA iinitData; iinitData.pSysMem = &box.Indices[0]; HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mBoxIB)); }
void TreeBillboardApp::BuildCrateBuffers() { GeometryGenerator::MeshData box; GeometryGenerator geoGen; geoGen.CreateBox(1.0f, 1.0f, 1.0f, box); // Extract the vertex elements we are interested in and pack the // vertices of all the meshes into one vertex buffer. std::vector<Vertex::Basic32> vertices(box.vertices.size()); for(UINT i = 0; i < box.vertices.size(); ++i) { vertices[i].Pos = box.vertices[i].position; vertices[i].Normal = box.vertices[i].normal; vertices[i].Tex = box.vertices[i].texC; } D3D11_BUFFER_DESC vbd; vbd.Usage = D3D11_USAGE_IMMUTABLE; vbd.ByteWidth = sizeof(Vertex::Basic32) * box.vertices.size(); vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = 0; vbd.MiscFlags = 0; D3D11_SUBRESOURCE_DATA vinitData; vinitData.pSysMem = &vertices[0]; HR(m_dxDevice->CreateBuffer(&vbd, &vinitData, m_boxVB.GetAddressOf())); // Pack the indices of all the meshes into one index buffer. D3D11_BUFFER_DESC ibd; ibd.Usage = D3D11_USAGE_IMMUTABLE; ibd.ByteWidth = sizeof(UINT) * box.indices.size(); ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; D3D11_SUBRESOURCE_DATA iinitData; iinitData.pSysMem = &box.indices[0]; HR(m_dxDevice->CreateBuffer(&ibd, &iinitData, m_boxIB.GetAddressOf())); }
void TexWaves::BuildBoxGeometry() { GeometryGenerator geoGen; GeometryGenerator::MeshData box = geoGen.CreateBox(8.0f, 8.0f, 8.0f, 3); SubmeshGeometry boxSubmesh; boxSubmesh.IndexCount = (UINT)box.Indices32.size(); boxSubmesh.StartIndexLocation = 0; boxSubmesh.BaseVertexLocation = 0; std::vector<FrameResource::Vertex> vertices(box.Vertices.size()); for (size_t i = 0; i < box.Vertices.size(); i++) { vertices[i].Pos = box.Vertices[i].Position; vertices[i].Normal = box.Vertices[i].Normal; vertices[i].TexC = box.Vertices[i].TexCoord; } std::vector<uint16_t> indices = box.GetIndices16(); const UINT vbByteSize = sizeof(FrameResource::Vertex) * (UINT)vertices.size(); const UINT ibByteSize = sizeof(uint16_t) * (UINT)indices.size(); auto geo = std::make_unique<MeshGeometry>(); geo->Name = "boxGeo"; geo->VertexBufferGPU = D3DUtil::CreateDefaultBuffer(_device.Get(), _commandList.Get(), vertices.data(), vbByteSize, geo->VertexBufferUploader); geo->IndexBufferGPU = D3DUtil::CreateDefaultBuffer(_device.Get(), _commandList.Get(), indices.data(), ibByteSize, geo->IndexBufferUploader); geo->VertexByteStride = sizeof(FrameResource::Vertex); geo->VertexBufferByteSize = vbByteSize; geo->IndexFormat = DXGI_FORMAT_R16_UINT; geo->IndexBufferByteSize = ibByteSize; geo->DrawArgs["box"] = boxSubmesh; _geometries[geo->Name] = move(geo); }
void TexColumnApp::BuildShapeGeometryBuffers() { GeometryGenerator::MeshData box; GeometryGenerator::MeshData grid; GeometryGenerator::MeshData sphere; GeometryGenerator::MeshData cylinder; GeometryGenerator geoGen; geoGen.CreateBox(1.0f, 1.0f, 1.0f, box); geoGen.CreateGrid(20.0f, 30.0f, 60, 40, grid); geoGen.CreateSphere(0.5f, 20, 20, sphere); geoGen.CreateCylinder(0.5f, 0.3f, 3.0f, 20, 20, cylinder); // Cache the vertex offsets to each object in the concatenated vertex buffer. mBoxVertexOffset = 0; mGridVertexOffset = box.Vertices.size(); mSphereVertexOffset = mGridVertexOffset + grid.Vertices.size(); mCylinderVertexOffset = mSphereVertexOffset + sphere.Vertices.size(); // Cache the index count of each object. mBoxIndexCount = box.Indices.size(); mGridIndexCount = grid.Indices.size(); mSphereIndexCount = sphere.Indices.size(); mCylinderIndexCount = cylinder.Indices.size(); // Cache the starting index for each object in the concatenated index buffer. mBoxIndexOffset = 0; mGridIndexOffset = mBoxIndexCount; mSphereIndexOffset = mGridIndexOffset + mGridIndexCount; mCylinderIndexOffset = mSphereIndexOffset + mSphereIndexCount; UINT totalVertexCount = box.Vertices.size() + grid.Vertices.size() + sphere.Vertices.size() + cylinder.Vertices.size(); UINT totalIndexCount = mBoxIndexCount + mGridIndexCount + mSphereIndexCount + mCylinderIndexCount; // // Extract the vertex elements we are interested in and pack the // vertices of all the meshes into one vertex buffer. // std::vector<Vertex::Basic32> vertices(totalVertexCount); UINT k = 0; for(size_t i = 0; i < box.Vertices.size(); ++i, ++k) { vertices[k].Pos = box.Vertices[i].Position; vertices[k].Normal = box.Vertices[i].Normal; vertices[k].Tex = box.Vertices[i].TexC; } for(size_t i = 0; i < grid.Vertices.size(); ++i, ++k) { vertices[k].Pos = grid.Vertices[i].Position; vertices[k].Normal = grid.Vertices[i].Normal; vertices[k].Tex = grid.Vertices[i].TexC; } for(size_t i = 0; i < sphere.Vertices.size(); ++i, ++k) { vertices[k].Pos = sphere.Vertices[i].Position; vertices[k].Normal = sphere.Vertices[i].Normal; vertices[k].Tex = sphere.Vertices[i].TexC; } for(size_t i = 0; i < cylinder.Vertices.size(); ++i, ++k) { vertices[k].Pos = cylinder.Vertices[i].Position; vertices[k].Normal = cylinder.Vertices[i].Normal; vertices[k].Tex = cylinder.Vertices[i].TexC; } D3D11_BUFFER_DESC vbd; vbd.Usage = D3D11_USAGE_IMMUTABLE; vbd.ByteWidth = sizeof(Vertex::Basic32) * totalVertexCount; vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = 0; vbd.MiscFlags = 0; D3D11_SUBRESOURCE_DATA vinitData; vinitData.pSysMem = &vertices[0]; HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mShapesVB)); // // Pack the indices of all the meshes into one index buffer. // std::vector<UINT> indices; indices.insert(indices.end(), box.Indices.begin(), box.Indices.end()); indices.insert(indices.end(), grid.Indices.begin(), grid.Indices.end()); indices.insert(indices.end(), sphere.Indices.begin(), sphere.Indices.end()); indices.insert(indices.end(), cylinder.Indices.begin(), cylinder.Indices.end()); D3D11_BUFFER_DESC ibd; ibd.Usage = D3D11_USAGE_IMMUTABLE; ibd.ByteWidth = sizeof(UINT) * totalIndexCount; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; D3D11_SUBRESOURCE_DATA iinitData; iinitData.pSysMem = &indices[0]; HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mShapesIB)); }
void CameraAndDynamicIndexingApp::BuildShapeGeometry() { GeometryGenerator geoGen; GeometryGenerator::MeshData box = geoGen.CreateBox(1.0f, 1.0f, 1.0f, 3); GeometryGenerator::MeshData grid = geoGen.CreateGrid(20.0f, 30.0f, 60, 40); GeometryGenerator::MeshData sphere = geoGen.CreateSphere(0.5f, 20, 20); GeometryGenerator::MeshData cylinder = geoGen.CreateCylinder(0.5f, 0.3f, 3.0f, 20, 20); // // We are concatenating all the geometry into one big vertex/index buffer. So // define the regions in the buffer each submesh covers. // // Cache the vertex offsets to each object in the concatenated vertex buffer. UINT boxVertexOffset = 0; UINT gridVertexOffset = (UINT)box.Vertices.size(); UINT sphereVertexOffset = gridVertexOffset + (UINT)grid.Vertices.size(); UINT cylinderVertexOffset = sphereVertexOffset + (UINT)sphere.Vertices.size(); // Cache the starting index for each object in the concatenated index buffer. UINT boxIndexOffset = 0; UINT gridIndexOffset = (UINT)box.Indices32.size(); UINT sphereIndexOffset = gridIndexOffset + (UINT)grid.Indices32.size(); UINT cylinderIndexOffset = sphereIndexOffset + (UINT)sphere.Indices32.size(); SubmeshGeometry boxSubmesh; boxSubmesh.IndexCount = (UINT)box.Indices32.size(); boxSubmesh.StartIndexLocation = boxIndexOffset; boxSubmesh.BaseVertexLocation = boxVertexOffset; SubmeshGeometry gridSubmesh; gridSubmesh.IndexCount = (UINT)grid.Indices32.size(); gridSubmesh.StartIndexLocation = gridIndexOffset; gridSubmesh.BaseVertexLocation = gridVertexOffset; SubmeshGeometry sphereSubmesh; sphereSubmesh.IndexCount = (UINT)sphere.Indices32.size(); sphereSubmesh.StartIndexLocation = sphereIndexOffset; sphereSubmesh.BaseVertexLocation = sphereVertexOffset; SubmeshGeometry cylinderSubmesh; cylinderSubmesh.IndexCount = (UINT)cylinder.Indices32.size(); cylinderSubmesh.StartIndexLocation = cylinderIndexOffset; cylinderSubmesh.BaseVertexLocation = cylinderVertexOffset; // // Extract the vertex elements we are interested in and pack the // vertices of all the meshes into one vertex buffer. // auto totalVertexCount = box.Vertices.size() + grid.Vertices.size() + sphere.Vertices.size() + cylinder.Vertices.size(); std::vector<Vertex> vertices(totalVertexCount); UINT k = 0; for(size_t i = 0; i < box.Vertices.size(); ++i, ++k) { vertices[k].Pos = box.Vertices[i].Position; vertices[k].Normal = box.Vertices[i].Normal; vertices[k].TexC = box.Vertices[i].TexC; } for(size_t i = 0; i < grid.Vertices.size(); ++i, ++k) { vertices[k].Pos = grid.Vertices[i].Position; vertices[k].Normal = grid.Vertices[i].Normal; vertices[k].TexC = grid.Vertices[i].TexC; } for(size_t i = 0; i < sphere.Vertices.size(); ++i, ++k) { vertices[k].Pos = sphere.Vertices[i].Position; vertices[k].Normal = sphere.Vertices[i].Normal; vertices[k].TexC = sphere.Vertices[i].TexC; } for(size_t i = 0; i < cylinder.Vertices.size(); ++i, ++k) { vertices[k].Pos = cylinder.Vertices[i].Position; vertices[k].Normal = cylinder.Vertices[i].Normal; vertices[k].TexC = cylinder.Vertices[i].TexC; } std::vector<std::uint16_t> indices; indices.insert(indices.end(), std::begin(box.GetIndices16()), std::end(box.GetIndices16())); indices.insert(indices.end(), std::begin(grid.GetIndices16()), std::end(grid.GetIndices16())); indices.insert(indices.end(), std::begin(sphere.GetIndices16()), std::end(sphere.GetIndices16())); indices.insert(indices.end(), std::begin(cylinder.GetIndices16()), std::end(cylinder.GetIndices16())); const UINT vbByteSize = (UINT)vertices.size() * sizeof(Vertex); const UINT ibByteSize = (UINT)indices.size() * sizeof(std::uint16_t); auto geo = std::make_unique<MeshGeometry>(); geo->Name = "shapeGeo"; ThrowIfFailed(D3DCreateBlob(vbByteSize, &geo->VertexBufferCPU)); CopyMemory(geo->VertexBufferCPU->GetBufferPointer(), vertices.data(), vbByteSize); ThrowIfFailed(D3DCreateBlob(ibByteSize, &geo->IndexBufferCPU)); CopyMemory(geo->IndexBufferCPU->GetBufferPointer(), indices.data(), ibByteSize); geo->VertexBufferGPU = d3dUtil::CreateDefaultBuffer(md3dDevice.Get(), mCommandList.Get(), vertices.data(), vbByteSize, geo->VertexBufferUploader); geo->IndexBufferGPU = d3dUtil::CreateDefaultBuffer(md3dDevice.Get(), mCommandList.Get(), indices.data(), ibByteSize, geo->IndexBufferUploader); geo->VertexByteStride = sizeof(Vertex); geo->VertexBufferByteSize = vbByteSize; geo->IndexFormat = DXGI_FORMAT_R16_UINT; geo->IndexBufferByteSize = ibByteSize; geo->DrawArgs["box"] = boxSubmesh; geo->DrawArgs["grid"] = gridSubmesh; geo->DrawArgs["sphere"] = sphereSubmesh; geo->DrawArgs["cylinder"] = cylinderSubmesh; mGeometries[geo->Name] = std::move(geo); }
void TextureWave::BuildGeometries() { GeometryGenerator::MeshData meshData; GeometryGenerator generator; generator.CreateGrid(160.0f, 160.0f, 50, 50, meshData); m_wave.Init(160, 160, 1.0f, 0.03f, 5.0f, 0.3f); m_landIndexCount = static_cast<UINT>(meshData.Indices.size()); std::vector<D3DHelper::CustomVertexWithTexture> tempVerties(meshData.Vertices.size()); UINT vertexCount = static_cast<UINT>(meshData.Vertices.size()); for (UINT i = 0; i < vertexCount; ++i) { XMFLOAT3 pos = meshData.Vertices[i].Position; pos.y = GetHillHeight(pos.x, pos.z); tempVerties[i].Position = pos; tempVerties[i].Normal = GetHillNormal(pos.x, pos.z);; tempVerties[i].TexCoord = meshData.Vertices[i].TexC; } D3D11_BUFFER_DESC vbd; vbd.Usage = D3D11_USAGE_IMMUTABLE; vbd.ByteWidth = static_cast<UINT>(sizeof(D3DHelper::CustomVertexWithTexture) * tempVerties.size()); vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = 0; vbd.MiscFlags = 0; D3D11_SUBRESOURCE_DATA vinitData; vinitData.pSysMem = &tempVerties[0]; auto hr = m_d3dDevice->CreateBuffer(&vbd, &vinitData, &m_landVB); D3DHelper::ThrowIfFailed(hr); D3D11_BUFFER_DESC ibd; ibd.Usage = D3D11_USAGE_IMMUTABLE; ibd.ByteWidth = sizeof(UINT) * m_landIndexCount; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; D3D11_SUBRESOURCE_DATA iinitData; iinitData.pSysMem = &meshData.Indices[0]; hr = m_d3dDevice->CreateBuffer(&ibd, &iinitData, &m_landIB); D3DHelper::ThrowIfFailed(hr); meshData.Clear(); // *** start create wave m_waveIndexCount = m_wave.IndexCount(); vbd.Usage = D3D11_USAGE_DYNAMIC; vbd.ByteWidth = static_cast<UINT>(sizeof(D3DHelper::CustomVertexWithTexture) * m_wave.VertexCount()); vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; vbd.MiscFlags = 0; hr = m_d3dDevice->CreateBuffer(&vbd, nullptr, &m_waveVB); D3DHelper::ThrowIfFailed(hr); std::vector<UINT> indices(3 * m_wave.TriangleCount()); // 3 indices per face UINT m = m_wave.RowCount(); UINT n = m_wave.ColumnCount(); int k = 0; for (UINT i = 0; i < m - 1; ++i) { for (DWORD j = 0; j < n - 1; ++j) { indices[k] = i*n + j; indices[k + 1] = i*n + j + 1; indices[k + 2] = (i + 1)*n + j; indices[k + 3] = (i + 1)*n + j; indices[k + 4] = i*n + j + 1; indices[k + 5] = (i + 1)*n + j + 1; k += 6; // next quad } } ibd.Usage = D3D11_USAGE_IMMUTABLE; ibd.ByteWidth = sizeof(UINT) * m_waveIndexCount; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; iinitData; iinitData.pSysMem = &indices[0]; hr = m_d3dDevice->CreateBuffer(&ibd, &iinitData, &m_waveIB); D3DHelper::ThrowIfFailed(hr); // *** end of create wave meshData.Clear(); generator.CreateBox(4, 4, 4, meshData); tempVerties.clear(); tempVerties.resize(meshData.Vertices.size()); vertexCount = static_cast<UINT>(meshData.Vertices.size()); for (UINT i = 0; i < vertexCount; ++i) { tempVerties[i].Position = meshData.Vertices[i].Position; tempVerties[i].Normal = meshData.Vertices[i].Normal; tempVerties[i].TexCoord = meshData.Vertices[i].TexC; } vbd.Usage = D3D11_USAGE_IMMUTABLE; vbd.ByteWidth = static_cast<UINT>(sizeof(D3DHelper::CustomVertexWithTexture) * tempVerties.size()); vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = 0; vbd.MiscFlags = 0; vinitData.pSysMem = &tempVerties[0]; hr = m_d3dDevice->CreateBuffer(&vbd, &vinitData, &m_boxVB); D3DHelper::ThrowIfFailed(hr); m_boxIndexCount = static_cast<UINT>(meshData.Indices.size()); ibd.Usage = D3D11_USAGE_IMMUTABLE; ibd.ByteWidth = sizeof(UINT) * m_boxIndexCount; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; iinitData.pSysMem = &meshData.Indices[0]; hr = m_d3dDevice->CreateBuffer(&ibd, &iinitData, &m_boxIB); D3DHelper::ThrowIfFailed(hr); }
void DynamicIndexing::BuildShapeGeometry() { GeometryGenerator geoGen; GeometryGenerator::MeshData box = geoGen.CreateBox(1.0f, 1.0f, 1.0f, 3); GeometryGenerator::MeshData grid = geoGen.CreateGrid(20.0f, 30.0f, 60, 40); GeometryGenerator::MeshData sphere = geoGen.CreateSphere(0.5f, 20, 20); GeometryGenerator::MeshData cylinder = geoGen.CreateCylinder(0.5f, 0.3f, 3.0f, 20, 20); UINT boxVertexOffset = 0; UINT gridVertexOffset = (UINT)box.Vertices.size(); UINT sphereVertexOffset = gridVertexOffset + (UINT)grid.Vertices.size(); UINT cylinderVertexOffset = sphereVertexOffset + (UINT)sphere.Vertices.size(); UINT boxIndexOffset = 0; UINT gridIndexOffset = (UINT)box.Indices32.size(); UINT sphereIndexOffset = gridIndexOffset + (UINT)grid.Indices32.size(); UINT cylinderIndexOffset = sphereIndexOffset + (UINT)sphere.Indices32.size(); SubmeshGeometry boxSubmesh; boxSubmesh.IndexCount = (UINT)box.Indices32.size(); boxSubmesh.StartIndexLocation = boxIndexOffset; boxSubmesh.BaseVertexLocation = boxVertexOffset; SubmeshGeometry gridSubmesh; gridSubmesh.IndexCount = (UINT)grid.Indices32.size(); gridSubmesh.StartIndexLocation = gridIndexOffset; gridSubmesh.BaseVertexLocation = gridVertexOffset; SubmeshGeometry sphereSubmesh; sphereSubmesh.IndexCount = (UINT)sphere.Indices32.size(); sphereSubmesh.StartIndexLocation = sphereIndexOffset; sphereSubmesh.BaseVertexLocation = sphereVertexOffset; SubmeshGeometry cylinderSubmesh; cylinderSubmesh.IndexCount = (UINT)cylinder.Indices32.size(); cylinderSubmesh.StartIndexLocation = cylinderIndexOffset; cylinderSubmesh.BaseVertexLocation = cylinderVertexOffset; auto totalVertexCount = box.Vertices.size() + grid.Vertices.size() + sphere.Vertices.size() + cylinder.Vertices.size(); std::vector<Vertex> vertices(totalVertexCount); UINT k = 0; for (size_t i = 0; i < box.Vertices.size(); ++i, ++k) { vertices[k].Pos = box.Vertices[i].Position; vertices[k].Normal = box.Vertices[i].Normal; vertices[k].Uv = box.Vertices[i].TexCoord; } for (size_t i = 0; i < grid.Vertices.size(); ++i, ++k) { vertices[k].Pos = grid.Vertices[i].Position; vertices[k].Normal = grid.Vertices[i].Normal; vertices[k].Uv = grid.Vertices[i].TexCoord; } for (size_t i = 0; i < sphere.Vertices.size(); ++i, ++k) { vertices[k].Pos = sphere.Vertices[i].Position; vertices[k].Normal = sphere.Vertices[i].Normal; vertices[k].Uv = sphere.Vertices[i].TexCoord; } for (size_t i = 0; i < cylinder.Vertices.size(); ++i, ++k) { vertices[k].Pos = cylinder.Vertices[i].Position; vertices[k].Normal = cylinder.Vertices[i].Normal; vertices[k].Uv = cylinder.Vertices[i].TexCoord; } std::vector<uint16_t> indices; indices.insert(indices.end(), begin(box.GetIndices16()), end(box.GetIndices16())); indices.insert(indices.end(), begin(grid.GetIndices16()), end(grid.GetIndices16())); indices.insert(indices.end(), begin(sphere.GetIndices16()), end(sphere.GetIndices16())); indices.insert(indices.end(), begin(cylinder.GetIndices16()), end(cylinder.GetIndices16())); const UINT vbByteSize = (UINT)vertices.size() * sizeof(Vertex); const UINT ibByteSize = (UINT)indices.size() * sizeof(uint16_t); auto geo = std::make_unique<MeshGeometry>(); geo->Name = "shapeGeo"; ThrowIfFailed(D3DCreateBlob(vbByteSize, &geo->VertexBufferCPU)); CopyMemory(geo->VertexBufferCPU->GetBufferPointer(), vertices.data(), vbByteSize); ThrowIfFailed(D3DCreateBlob(ibByteSize, &geo->IndexBufferCPU)); CopyMemory(geo->IndexBufferCPU->GetBufferPointer(), indices.data(), ibByteSize); geo->VertexBufferGPU = D3DUtil::CreateDefaultBuffer(_device.Get(), _commandList.Get(), vertices.data(), vbByteSize, geo->VertexBufferUploader); geo->IndexBufferGPU = D3DUtil::CreateDefaultBuffer(_device.Get(), _commandList.Get(), indices.data(), ibByteSize, geo->IndexBufferUploader); geo->VertexByteStride = sizeof(Vertex); geo->VertexBufferByteSize = vbByteSize; geo->IndexFormat = DXGI_FORMAT_R16_UINT; geo->IndexBufferByteSize = ibByteSize; geo->DrawArgs["box"] = boxSubmesh; geo->DrawArgs["grid"] = gridSubmesh; geo->DrawArgs["sphere"] = sphereSubmesh; geo->DrawArgs["cylinder"] = cylinderSubmesh; _geometries[geo->Name] = move(geo); }
HRESULT LightSkull::BuildGeometries() { auto tempBoxWorld = XMMatrixTranslation(0.0f, 0.5f, 0.0f); auto tempBoxScale = XMMatrixScaling(3.0f, 1.0f, 3.0f); m_boxWorld = XMMatrixMultiply(tempBoxWorld, tempBoxScale); m_boxWorld = XMMatrixTranspose(m_boxWorld); auto tempCenterSphereOffset = XMMatrixTranslation(0.0f, 2.0f, 0.0f); auto tempCenterSphereScale = XMMatrixScaling(2.0f, 2.0f, 2.0f); m_centerSphere = XMMatrixMultiply(tempCenterSphereScale, tempCenterSphereOffset); m_centerSphere = XMMatrixTranspose(m_centerSphere); for (int i = 0; i < 5; ++i) { m_cylinderWorld[i * 2] = XMMatrixTranslation(-5.0f, 1.5f, -10.0f + i*5.0f); m_cylinderWorld[i * 2] = XMMatrixTranspose(m_cylinderWorld[i * 2]); m_cylinderWorld[i * 2 + 1] = XMMatrixTranslation(+5.0f, 1.5f, -10.0f + i*5.0f); m_cylinderWorld[i * 2 + 1] = XMMatrixTranspose(m_cylinderWorld[i * 2 + 1]); m_sphereWorld[i * 2] = XMMatrixTranslation(-5.0f, 3.5f, -10.0f + i*5.0f); m_sphereWorld[i * 2] = XMMatrixTranspose(m_sphereWorld[i * 2]); m_sphereWorld[i * 2 + 1] = XMMatrixTranslation(+5.0f, 3.5f, -10.0f + i*5.0f); m_sphereWorld[i * 2 + 1] = XMMatrixTranspose(m_sphereWorld[i * 2 + 1]); } HRESULT result = S_FALSE; GeometryGenerator::MeshData box; GeometryGenerator::MeshData grid; GeometryGenerator::MeshData sphere; GeometryGenerator::MeshData cylinder; GeometryGenerator g; g.CreateBox(1.0f, 1.0f, 1.0f, box); g.CreateCylinder(0.5f, 0.3f, 3.0f, 20, 20, cylinder); g.CreateGrid(20.0f, 30.0f, 60, 40, grid); g.CreateSphere(0.5f, 20, 20, sphere); m_boxVertexOffset = 0; m_gridVertexOffset = static_cast<int>(box.Vertices.size()); m_sphereVertexOffset = static_cast<int>(m_gridVertexOffset + grid.Vertices.size()); m_cylinderVertexOffset = static_cast<int>(m_sphereVertexOffset + sphere.Vertices.size()); m_boxIndexCount = static_cast<UINT>(box.Indices.size()); m_gridIndexCount = static_cast<UINT>(grid.Indices.size()); m_sphereIndexCount = static_cast<UINT>(sphere.Indices.size()); m_cylinderIndexCount = static_cast<UINT>(cylinder.Indices.size()); m_boxIndexOffset = 0; m_gridIndexOffset = m_boxIndexCount; m_sphereIndexOffset = m_gridIndexOffset + m_gridIndexCount; m_cylinderIndexOffset = m_sphereIndexOffset + m_sphereIndexCount; std::vector<CustomVertex> vertices; int j = 0; for (UINT i = 0; i < box.Vertices.size(); ++i, ++j) { CustomVertex newData; newData.Pos = box.Vertices[i].Position; newData.Normal = box.Vertices[i].Normal; vertices.push_back(newData); } for (UINT i = 0; i < grid.Vertices.size(); ++i, ++j) { CustomVertex newData; newData.Pos = grid.Vertices[i].Position; newData.Normal = grid.Vertices[i].Normal; vertices.push_back(newData); } for (UINT i = 0; i < sphere.Vertices.size(); ++i, ++j) { CustomVertex newData; newData.Pos = sphere.Vertices[i].Position; newData.Normal = sphere.Vertices[i].Normal; vertices.push_back(newData); } for (UINT i = 0; i < cylinder.Vertices.size(); ++i, ++j) { CustomVertex newData; newData.Pos = cylinder.Vertices[i].Position; newData.Normal = cylinder.Vertices[i].Normal; vertices.push_back(newData); } D3D11_BUFFER_DESC vbd; vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.ByteWidth = sizeof(CustomVertex) * static_cast<UINT>(vertices.size()); vbd.CPUAccessFlags = 0; vbd.MiscFlags = 0; vbd.StructureByteStride = 0; vbd.Usage = D3D11_USAGE_IMMUTABLE; D3D11_SUBRESOURCE_DATA vsd; vsd.pSysMem = &vertices[0]; result = m_d3dDevice->CreateBuffer(&vbd, &vsd, &m_vertexBuffer); if (FAILED(result)) { return result; } std::vector<UINT> indices; indices.insert(indices.end(), box.Indices.begin(), box.Indices.end()); indices.insert(indices.end(), grid.Indices.begin(), grid.Indices.end()); indices.insert(indices.end(), sphere.Indices.begin(), sphere.Indices.end()); indices.insert(indices.end(), cylinder.Indices.begin(), cylinder.Indices.end()); D3D11_BUFFER_DESC ibd; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.ByteWidth = sizeof(UINT) *static_cast<UINT>(indices.size()); ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; ibd.StructureByteStride = 0; ibd.Usage = D3D11_USAGE_IMMUTABLE; D3D11_SUBRESOURCE_DATA isb; isb.pSysMem = &indices[0]; result = m_d3dDevice->CreateBuffer(&ibd, &isb, &m_indexBuffer); D3DHelper::ThrowIfFailed(result); m_constantBuffer.Create(m_d3dDevice); m_constBufferPerframe.Create(m_d3dDevice); m_constMatPerObj.Create(m_d3dDevice); auto skullScale = XMMatrixScaling(0.5f, 0.5f, 0.5f); auto skullOffset = XMMatrixTranslation(0.0f, 1.0f, 0.0f); m_skullWorld = XMMatrixMultiply(skullScale, skullOffset); m_skullWorld = XMMatrixTranspose(m_skullWorld); m_skullMat.Ambient = XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f); m_skullMat.Diffuse = XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f); m_skullMat.Specular = XMFLOAT4(0.8f, 0.8f, 0.8f, 16.0f); m_gridMat.Ambient = XMFLOAT4(0.48f, 0.77f, 0.46f, 1.0f); m_gridMat.Diffuse = XMFLOAT4(0.48f, 0.77f, 0.46f, 1.0f); m_gridMat.Specular = XMFLOAT4(0.2f, 0.2f, 0.2f, 16.0f); m_cylinderMat.Ambient = XMFLOAT4(0.7f, 0.85f, 0.7f, 1.0f); m_cylinderMat.Diffuse = XMFLOAT4(0.7f, 0.85f, 0.7f, 1.0f); m_cylinderMat.Specular = XMFLOAT4(0.8f, 0.8f, 0.8f, 16.0f); m_sphereMat.Ambient = XMFLOAT4(0.1f, 0.2f, 0.3f, 1.0f); m_sphereMat.Diffuse = XMFLOAT4(0.2f, 0.4f, 0.6f, 1.0f); m_sphereMat.Specular = XMFLOAT4(0.9f, 0.9f, 0.9f, 16.0f); m_boxMat.Ambient = XMFLOAT4(0.651f, 0.5f, 0.392f, 1.0f); m_boxMat.Diffuse = XMFLOAT4(0.651f, 0.5f, 0.392f, 1.0f); m_boxMat.Specular = XMFLOAT4(0.2f, 0.2f, 0.2f, 16.0f); m_lightOne.Ambient = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f); m_lightOne.Diffuse = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f); m_lightOne.Specular = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f); m_lightOne.Direction = XMFLOAT3(0.57735f, -0.57735f, 0.57735f); return result; }
void LitSkullApp::BuildShapeGeometryBuffers() { GeometryGenerator::MeshData box; GeometryGenerator::MeshData grid; GeometryGenerator::MeshData sphere; GeometryGenerator::MeshData cylinder; GeometryGenerator geoGen; geoGen.CreateBox(1.0f, 1.0f, 1.0f, box); geoGen.CreateGrid(160.f, 160.f, 50, 50, grid); //geoGen.CreateSphere(0.5f, 20, 20, sphere); geoGen.CreateGeosphere(0.5f, 3, sphere); geoGen.CreateCylinder(0.5f, 0.3f, 3.0f, 20, 20, cylinder); UINT boxVertexCount = box.Vertices.size(); UINT gridVertexCount = grid.Vertices.size(); UINT sphereVertexCount = sphere.Vertices.size(); UINT cylinderVertexCount = cylinder.Vertices.size(); UINT totalVertexCount = boxVertexCount + gridVertexCount + sphereVertexCount + cylinderVertexCount; m_uBoxIndexCount = box.Indices.size(); m_uGridIndexCount = grid.Indices.size(); m_uSphereIndexCount = sphere.Indices.size(); m_uCylinderIndexCount = cylinder.Indices.size(); UINT totalIndexCount = m_uBoxIndexCount + m_uGridIndexCount + m_uSphereIndexCount + m_uCylinderIndexCount; m_uBoxVertexOffset = 0; m_uGridVertexOffset = m_uBoxVertexOffset + boxVertexCount; m_uSphereVertexOffset = m_uGridVertexOffset + gridVertexCount; m_uCylinderVertexOffset = m_uSphereVertexOffset + sphereVertexCount; m_uBoxIndexOffset = 0; m_uGridIndexOffset = m_uBoxIndexOffset + m_uBoxIndexCount; m_uSphereIndexOffset = m_uGridIndexOffset + m_uGridIndexCount; m_uCylinderIndexOffset = m_uSphereIndexOffset + m_uSphereIndexCount; std::vector<Vertex::PosNormal> vertices(totalVertexCount); XMFLOAT4 black(0.0f, 0.0f, 0.0f, 1.0f); for (UINT k = m_uBoxVertexOffset, i = 0; i < boxVertexCount; ++i) { vertices[k + i].Pos = box.Vertices[i].Position; vertices[k + i].Normal = box.Vertices[i].Normal; } for (UINT k = m_uGridVertexOffset, i = 0; i < gridVertexCount; ++i) { vertices[k + i].Pos = grid.Vertices[i].Position; vertices[k + i].Normal = grid.Vertices[i].Normal; } for (size_t k = m_uSphereVertexOffset, i = 0; i < sphereVertexCount; ++i) { vertices[k + i].Pos = sphere.Vertices[i].Position; vertices[k + i].Normal = sphere.Vertices[i].Normal; } for (size_t k = m_uCylinderVertexOffset, i = 0; i < cylinderVertexCount; ++i) { vertices[k + i].Pos = cylinder.Vertices[i].Position; vertices[k + i].Normal = cylinder.Vertices[i].Normal; } { D3D11_BUFFER_DESC vbd; vbd.Usage = D3D11_USAGE_IMMUTABLE;//jingz todo 为什么CPU要访问和修改?? vbd.ByteWidth = sizeof(Vertex::PosNormal) * totalVertexCount; vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = 0; vbd.MiscFlags = 0; vbd.StructureByteStride = 0; D3D11_SUBRESOURCE_DATA initDate; initDate.pSysMem = &vertices[0]; HR(m_pD3dDevice->CreateBuffer(&vbd, &initDate, &m_pShapesVB)); } { std::vector<UINT> indices; indices.insert(indices.end(), box.Indices.begin(), box.Indices.end()); indices.insert(indices.end(), grid.Indices.begin(), grid.Indices.end()); indices.insert(indices.end(), sphere.Indices.begin(), sphere.Indices.end()); indices.insert(indices.end(), cylinder.Indices.begin(), cylinder.Indices.end()); D3D11_BUFFER_DESC ibd; ibd.Usage = D3D11_USAGE_IMMUTABLE;//jingz todo 为什么CPU要访问和修改?? ibd.ByteWidth = sizeof(UINT) * totalIndexCount; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; ibd.StructureByteStride = 0; D3D11_SUBRESOURCE_DATA initDate; initDate.pSysMem = &indices[0]; HR(m_pD3dDevice->CreateBuffer(&ibd, &initDate, &m_pShapesIB)); } }
Box::Box() { GeometryGenerator gen; gen.CreateBox(*m_Data); }
void Shape::BuildGeometryBuffers() { GeometryGenerator::MeshData grid; GeometryGenerator::MeshData box; GeometryGenerator::MeshData sphere; GeometryGenerator::MeshData cylinder; GeometryGenerator geoGen; geoGen.CreateGrid(20.0f, 30.0f, 60, 40, grid); geoGen.CreateBox(1.0f, 1.0f, 1.0f, box); geoGen.CreateGeosphere(0.5f, 3, sphere); //geoGen.CreateSphere(1.0f, 30, 30, sphere); geoGen.CreateCylinder(0.5f, 0.3f, 3.0f, 30, 30, cylinder); mGridVertexOffset = 0; mBoxVertexOffset = grid.Vertices.size(); mSphereVertexOffset = mBoxVertexOffset + box.Vertices.size(); mCylinderVertexOffset = mSphereVertexOffset + sphere.Vertices.size(); mGridIndexCount = grid.Indices.size(); mBoxIndexCount = box.Indices.size(); mSphereIndexCount = sphere.Indices.size(); mCylinderIndexCount = cylinder.Indices.size(); mGridIndexOffset = 0; mBoxIndexOffset = mGridIndexCount; mSphereIndexOffset = mBoxIndexOffset + mBoxIndexCount; mCylinderIndexOffset = mSphereIndexOffset + mSphereIndexCount; UINT totalVertexCount = box.Vertices.size() + grid.Vertices.size() + sphere.Vertices.size() + cylinder.Vertices.size(); UINT totalIndexCount = mBoxIndexCount + mGridIndexCount + mSphereIndexCount + mCylinderIndexCount; #pragma region Create Vertices Buffer std::vector<Vertex> vertices(totalVertexCount); XMFLOAT4 color = *(XMFLOAT4*)&Colors::Red; UINT k = 0; for ( size_t i = 0; i < grid.Vertices.size(); ++i, ++k ) { vertices[k].Pos = grid.Vertices[i].Position; vertices[k].Color = *(XMFLOAT4*)&Colors::Blue; } for ( size_t i = 0; i < box.Vertices.size(); ++i, ++k ) { vertices[k].Pos = box.Vertices[i].Position; vertices[k].Color = *(XMFLOAT4*)&Colors::Magenta; } for ( size_t i = 0; i < sphere.Vertices.size(); ++i, ++k ) { vertices[k].Pos = sphere.Vertices[i].Position; vertices[k].Color = *(XMFLOAT4*)&Colors::Yellow; } for ( size_t i = 0; i < cylinder.Vertices.size(); ++i, ++k ) { vertices[k].Pos = cylinder.Vertices[i].Position; vertices[k].Color = *(XMFLOAT4*)&Colors::Red; } D3D11_BUFFER_DESC vbd; vbd.Usage = D3D11_USAGE_IMMUTABLE; vbd.ByteWidth = sizeof(Vertex)*totalVertexCount; vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = 0; vbd.MiscFlags = 0; vbd.StructureByteStride = 0; D3D11_SUBRESOURCE_DATA vinitData; vinitData.pSysMem = &vertices[0]; HR(mD3DDevice->CreateBuffer(&vbd, &vinitData, &mShapeVB)); #pragma endregion #pragma region Create Indices Buffer std::vector<UINT> indices; indices.clear(); indices.insert(indices.end(), grid.Indices.begin(), grid.Indices.end()); indices.insert(indices.end(), box.Indices.begin(), box.Indices.end()); indices.insert(indices.end(), sphere.Indices.begin(), sphere.Indices.end()); indices.insert(indices.end(), cylinder.Indices.begin(), cylinder.Indices.end()); D3D11_BUFFER_DESC ibd; ibd.Usage = D3D11_USAGE_IMMUTABLE; ibd.ByteWidth = sizeof(UINT)*totalIndexCount; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; ibd.StructureByteStride = 0; D3D11_SUBRESOURCE_DATA iinitData; iinitData.pSysMem = &indices[0]; HR(mD3DDevice->CreateBuffer(&ibd, &iinitData, &mShapeIB)); #pragma endregion }
//Makes a Square by default Entity::Entity(int type, std::string label, float width, float height, float depth) : mPosition(0.0f, 0.0f, 0.0f), mShadowScale(0.0f, 0.0f, 0.0f), mRight(1.0f, 0.0f, 0.0f), mUp(0.0f, 1.0f, 0.0f), mLook(0.0f, 0.0f, 1.0f), prevPitch(0.0f), rotationY(0.0f), prevRoll(0.0f), origTexScale(1.0f, 1.0f, 1.0f), texTrans(0.0f, 0.0f, 0.0f), texTransMult(0.0f, 0.0f, 0.0f), mGoToPos(0.0f, 0.0f, 0.0f), currProgress(0.0f), rotationZ(0.0f), mDistanceLeft(0.0f), mTexWidth(0.0f), mTexHeight(0.0f), mUpDown(false), mGrowing(false), mSquishX(false), mSquishY(false), mSquishZ(false), mOrigY(0.0f), mOrigX(0.0f), mOrigZ(0.0f), mGrowOut(false), mHeightToGo(0.0f), mScale(1.0f), mWidth(width), mHeight(height), mDepth(depth), hovering(false), useTexTrans(false), progressBar(false), goToPos(false), billboard(false), flipUpright(false), reverseLook(false), mDead(false), mSpinning(false), mExplode(false), mBasicTexTrans(false), mUseAnimation(false), mUseAAB(false), mUseAABOnce(false), mGoUp(true), mBackFaceCull(true), mGoDown(false), mSideToSide(false), mPulse(false), mOrbit(false), turnAngle(0.0f), explosionDist(0.0f), mAnim(0), movementMult(0.0f), mFlipping(false), mRolling(false), mBackAndForth(false), mGrow(true), mShrink(false), mGrowIn(false), mFlipTexture(false), mTexRotate(false), mLabel(label) { //SET MATERIAL mMat.Ambient = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); mMat.Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); mMat.Specular = XMFLOAT4(1.0f, 1.0f, 1.0f, 48.0f); GeometryGenerator geoGen; //FLOOR PLANE XMMATRIX I = XMMatrixIdentity(); XMStoreFloat4x4(&mWorld, I); XMStoreFloat4x4(&mShadowTrans, I); switch (type) { case 0: geoGen.CreateGrid(width, height, 2, 2, mGrid); break; case 1: geoGen.CreateSphere(width, height, height, mGrid);/*height is slice count .. width for radius*/ break; case 2: geoGen.CreateUprightSquare(width, height, mGrid); break; case 3: geoGen.CreateBox(width, height, depth, mGrid); break; case 4: geoGen.CreateFrontandBackFace(width, height, depth, mGrid); break; case 5: geoGen.CreateCylinder(width, depth, height, 15, 2, mGrid); break; case 6: geoGen.CreateBox2Tex(width, height, depth, mGrid); break; } mIndexCount = mGrid.Indices.size(); mMeshVertices.resize(mGrid.Vertices.size()); }
// @brief 生成天体对象的Buffers void SuperSolarSystemApp::BuildObjectBuffers( ) { GeometryGenerator geoGen; GeometryGenerator::MeshData starMash; GeometryGenerator::MeshData iceCubeMash; geoGen.CreateSphere( 1.0f, 40, 40, starMash ); geoGen.CreateBox( 1.0f, 1.0f, 1.0f, iceCubeMash ); // // 缓存偏移信息和数量信息, 以及冰立方的各个面的法向量 // mLTVertexOffset[LTVT_STAR] = 0; mLTVertexOffset[LTVT_ICECUBE] = starMash.Vertices.size( ); mLTIndexCount[LTVT_STAR] = starMash.Indices.size( ); mLTIndexCount[LTVT_ICECUBE] = iceCubeMash.Indices.size( ); mLTIndexOffset[LTVT_STAR] = 0; mLTIndexOffset[LTVT_ICECUBE] = starMash.Indices.size( ); for ( int i = 0; i < 6; i++ ) { mIceCubeNormals[i] = iceCubeMash.Vertices[i * 4].Normal; } // // 创建顶点缓冲 // UINT totVertexCount = starMash.Vertices.size( ) + iceCubeMash.Vertices.size( ); std::vector<Vertex::NormalObjVertex> vertices; vertices.resize( totVertexCount ); UINT k = 0; for ( size_t i = 0; i < starMash.Vertices.size( ); i++, k++ ) { vertices[k].Pos = starMash.Vertices[i].Position; vertices[k].Normal = starMash.Vertices[i].Normal; vertices[k].Tex = starMash.Vertices[i].TexC; } for ( size_t i = 0; i < iceCubeMash.Vertices.size( ); i++, k++ ) { vertices[k].Pos = iceCubeMash.Vertices[i].Position; vertices[k].Normal = iceCubeMash.Vertices[i].Normal; vertices[k].Tex = iceCubeMash.Vertices[i].TexC; } // 顶点缓冲创建第1步, 填充描述缓冲的 D3D11_BUFFER_DESC D3D11_BUFFER_DESC vbd; // Usage指定如何使用这个缓冲, 这里指定 D3D11_USAGE_IMMUTABLE, 表明缓冲一旦创建后便不再更改 vbd.Usage = D3D11_USAGE_IMMUTABLE; // 按字节衡量的顶点缓冲大小 vbd.ByteWidth = sizeof( Vertex::NormalObjVertex ) * totVertexCount; // 对于顶点缓冲, 指定 D3D11_BIND_VERTEX_BUFFER vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; // 指定CPU如何访问缓冲, 如果之前Usage指定了D3D11_USAGE_IMMUTABLE, 那就设为0 vbd.CPUAccessFlags = 0; // 在顶点缓冲中指定0 vbd.MiscFlags = 0; // 这个参数只对结构缓冲有效, 其余缓冲设为0 vbd.StructureByteStride = 0; // 顶点缓冲创建第2步, 填充 D3D11_SUBRESOURCE_DATA, 指定顶点数据 D3D11_SUBRESOURCE_DATA vinitData; vinitData.pSysMem = &vertices[0]; // 顶点缓冲创建第3步, 创建顶点缓冲 HR( mD3dDevice->CreateBuffer( &vbd, &vinitData, &mLightTexVB ) ); // // 创建索引缓冲 // std::vector<UINT> indices; indices.insert( indices.end( ), starMash.Indices.begin( ), starMash.Indices.end( ) ); indices.insert( indices.end( ), iceCubeMash.Indices.begin( ), iceCubeMash.Indices.end( ) ); UINT totIndexCount = starMash.Indices.size( ) + iceCubeMash.Indices.size( ); D3D11_BUFFER_DESC ibd; ibd.Usage = D3D11_USAGE_IMMUTABLE; ibd.ByteWidth = sizeof( UINT ) * totIndexCount; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; ibd.StructureByteStride = 0; D3D11_SUBRESOURCE_DATA iinitData; iinitData.pSysMem = &indices[0]; HR( mD3dDevice->CreateBuffer( &ibd, &iinitData, &mLightTexIB ) ); }
void ObjectsRenderer::InitBase() { // Init base objects BasicObjectData* objectData = new BasicObjectData(); objectData->UseIndex = true; objectData->UseEx = true; // Init shapes GeometryGenerator::MeshData box; GeometryGenerator::MeshData grid; GeometryGenerator::MeshData cylinder; GeometryGenerator geoGen; geoGen.CreateBox(1.0f, 1.0f, 1.0f, box); geoGen.CreateGrid(20.0f, 30.0f, 60, 40, grid); geoGen.CreateCylinder(0.5f, 0.3f, 3.0f, 20, 20, cylinder); // Cache the vertex offsets to each object in the concatenated vertex buffer. int boxVertexOffset = 0; int gridVertexOffset = box.Vertices.size(); int cylinderVertexOffset = gridVertexOffset + grid.Vertices.size(); // Cache the index count of each object. int boxIndexCount = box.Indices.size(); int gridIndexCount = grid.Indices.size(); int cylinderIndexCount = cylinder.Indices.size(); // Cache the starting index for each object in the concatenated index buffer. int boxIndexOffset = 0; int gridIndexOffset = boxIndexCount; int cylinderIndexOffset = gridIndexOffset + gridIndexCount; UINT totalVertexCount = box.Vertices.size() + grid.Vertices.size() + cylinder.Vertices.size(); UINT totalIndexCount = boxIndexCount + gridIndexCount + cylinderIndexCount; // Extract the vertex elements we are interested in and pack the // vertices of all the meshes into one vertex buffer. auto& vertices = objectData->VertexDataEx; vertices.resize(totalVertexCount); UINT k = 0; for (size_t i = 0; i < box.Vertices.size(); ++i, ++k) { vertices[k].Pos = box.Vertices[i].Position; vertices[k].Normal = box.Vertices[i].Normal; vertices[k].Tex = box.Vertices[i].TexC; vertices[k].TangentU = box.Vertices[i].TangentU; } for (size_t i = 0; i < grid.Vertices.size(); ++i, ++k) { vertices[k].Pos = grid.Vertices[i].Position; vertices[k].Normal = grid.Vertices[i].Normal; vertices[k].Tex = grid.Vertices[i].TexC; vertices[k].TangentU = grid.Vertices[i].TangentU; } for (size_t i = 0; i < cylinder.Vertices.size(); ++i, ++k) { vertices[k].Pos = cylinder.Vertices[i].Position; vertices[k].Normal = cylinder.Vertices[i].Normal; vertices[k].Tex = cylinder.Vertices[i].TexC; vertices[k].TangentU = cylinder.Vertices[i].TangentU; } // Pack the indices of all the meshes into one index buffer. auto& indices = objectData->IndexData; indices.insert(indices.end(), box.Indices.begin(), box.Indices.end()); indices.insert(indices.end(), grid.Indices.begin(), grid.Indices.end()); indices.insert(indices.end(), cylinder.Indices.begin(), cylinder.Indices.end()); // Set unit data XMFLOAT4X4 gridWorld, boxWorld, cylWorld[10]; XMMATRIX I = XMMatrixIdentity(); XMFLOAT4X4 One; XMStoreFloat4x4(&One, I); XMStoreFloat4x4(&gridWorld, I); XMMATRIX boxScale = XMMatrixScaling(3.0f, 1.0f, 3.0f); XMMATRIX boxOffset = XMMatrixTranslation(0.0f, 0.5f, 0.0f); XMStoreFloat4x4(&boxWorld, XMMatrixMultiply(boxScale, boxOffset)); for (int i = 0; i < 5; ++i) { XMStoreFloat4x4(&cylWorld[i * 2 + 0], XMMatrixTranslation(-5.0f, 1.5f, -10.0f + i*5.0f)); XMStoreFloat4x4(&cylWorld[i * 2 + 1], XMMatrixTranslation(+5.0f, 1.5f, -10.0f + i*5.0f)); } Material gridMat, cylinderMat, boxMat; gridMat.Ambient = XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f); gridMat.Diffuse = XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f); gridMat.Specular = XMFLOAT4(0.8f, 0.8f, 0.8f, 16.0f); gridMat.Reflect = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f); cylinderMat.Ambient = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); cylinderMat.Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); cylinderMat.Specular = XMFLOAT4(0.8f, 0.8f, 0.8f, 16.0f); cylinderMat.Reflect = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f); boxMat.Ambient = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); boxMat.Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); boxMat.Specular = XMFLOAT4(0.8f, 0.8f, 0.8f, 16.0f); boxMat.Reflect = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f); objectData->Units.resize(3); // box auto& unit0 = objectData->Units[0]; unit0.VCount = box.Vertices.size(); unit0.Base = boxVertexOffset; unit0.Count = boxIndexCount; unit0.Start = boxIndexOffset; unit0.Worlds.push_back(boxWorld); unit0.TextureFileNames.push_back(L"Media\\Textures\\stone.dds"); unit0.NorTextureFileNames.push_back(L"Media\\Textures\\stones_nmap.dds"); unit0.TextureTransform.push_back(One); unit0.Material.push_back(boxMat); // grid auto& unit1 = objectData->Units[1]; unit1.VCount = grid.Vertices.size(); unit1.Base = gridVertexOffset; unit1.Count = gridIndexCount; unit1.Start = gridIndexOffset; unit1.Worlds.push_back(gridWorld); unit1.TextureFileNames.push_back(L"Media\\Textures\\floor.dds"); unit1.NorTextureFileNames.push_back(L"Media\\Textures\\floor_nmap.dds"); XMFLOAT4X4 texTransform; XMStoreFloat4x4(&texTransform, XMMatrixScaling(6.0f, 8.0f, 1.0f)); unit1.TextureTransform.push_back(texTransform); unit1.Material.push_back(gridMat); // cylinder auto& unit2 = objectData->Units[2]; unit2.VCount = cylinder.Vertices.size(); unit2.Base = cylinderVertexOffset; unit2.Count = cylinderIndexCount; unit2.Start = cylinderIndexOffset; unit2.Worlds.assign(cylWorld, cylWorld + 10); unit2.TextureFileNames.push_back(L"Media\\Textures\\bricks.dds"); unit2.TextureStepRate = 10; unit2.NorTextureFileNames.push_back(L"Media\\Textures\\bricks_nmap.dds"); unit2.NorTextureStepRate = 10; unit2.TextureTransform.push_back(One); unit2.TextureTransformStepRate = 10; unit2.Material.push_back(cylinderMat); unit2.MaterialStepRate = 10; BasicFeatureConfigure objectFeature = { 0 }; objectFeature.LightCount = 3; objectFeature.TextureEnable = true; objectFeature.NormalEnable = true; /*objectFeature.TessEnable = true; objectFeature.TessDesc.HeightScale = 0.07f; objectFeature.TessDesc.MaxTessDistance = 1.0f; objectFeature.TessDesc.MaxTessFactor = 5.0f; objectFeature.TessDesc.MinTessDistance = 25.0f; objectFeature.TessDesc.MinTessFactor = 1.0f;*/ m_base->Initialize(objectData, objectFeature); }
void TextureDemo::BuildGeometries() { GeometryGenerator g; GeometryGenerator::MeshData data; g.CreateBox(8, 8, 8, data); m_indexTotalCount = static_cast<UINT>(data.Indices.size()); std::vector<CustomVertex> vertexData; UINT dataVertexCount = static_cast<UINT>(data.Vertices.size()); for (UINT i = 0; i < dataVertexCount; ++i) { CustomVertex customData; customData.Normal = data.Vertices[i].Normal; customData.Pos = data.Vertices[i].Position; vertexData.push_back(customData); } UINT vertexDataCount = static_cast<UINT>(vertexData.size()); for (UINT i = 0; i < vertexDataCount / 4; ++i) { vertexData[i * 4 + 0].Tex = { -1,2 }; vertexData[i * 4 + 1].Tex = { -1,-1 }; vertexData[i * 4 + 2].Tex = { 2,-1 }; vertexData[i * 4 + 3].Tex = { 2,2 }; } D3D11_BUFFER_DESC bd; ZeroMemory(&bd, sizeof(D3D11_BUFFER_DESC)); bd.Usage = D3D11_USAGE_IMMUTABLE; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; bd.ByteWidth = sizeof(CustomVertex) * static_cast<UINT>(data.Vertices.size()); bd.CPUAccessFlags = 0; bd.MiscFlags = 0; bd.StructureByteStride = 0; D3D11_SUBRESOURCE_DATA vertexSubResource; ZeroMemory(&vertexSubResource, sizeof(D3D11_SUBRESOURCE_DATA)); vertexSubResource.pSysMem = &vertexData[0]; HRESULT hr = S_FALSE; hr = m_d3dDevice->CreateBuffer(&bd, &vertexSubResource, &m_vertexBuffer); D3DHelper::ThrowIfFailed(hr); D3D11_BUFFER_DESC indexBD; ZeroMemory(&indexBD, sizeof(D3D11_BUFFER_DESC)); indexBD.BindFlags = D3D11_BIND_INDEX_BUFFER; indexBD.ByteWidth = sizeof(UINT) * m_indexTotalCount; indexBD.CPUAccessFlags = 0; indexBD.MiscFlags = 0; indexBD.StructureByteStride = 0; indexBD.Usage = D3D11_USAGE_IMMUTABLE; D3D11_SUBRESOURCE_DATA indexSubResource; ZeroMemory(&indexSubResource, sizeof(D3D11_SUBRESOURCE_DATA)); indexSubResource.pSysMem = &data.Indices[0]; hr = m_d3dDevice->CreateBuffer(&indexBD, &indexSubResource, &m_indexBuffer); D3DHelper::ThrowIfFailed(hr); }