void WavesCSApp::BuildLandGeometry() { GeometryGenerator geoGen; GeometryGenerator::MeshData grid = geoGen.CreateGrid(160.0f, 160.0f, 50, 50); // // Extract the vertex elements we are interested and apply the height function to // each vertex. In addition, color the vertices based on their height so we have // sandy looking beaches, grassy low hills, and snow mountain peaks. // std::vector<Vertex> vertices(grid.Vertices.size()); for(size_t i = 0; i < grid.Vertices.size(); ++i) { auto& p = grid.Vertices[i].Position; vertices[i].Pos = p; vertices[i].Pos.y = GetHillsHeight(p.x, p.z); vertices[i].Normal = GetHillsNormal(p.x, p.z); vertices[i].TexC = grid.Vertices[i].TexC; } const UINT vbByteSize = (UINT)vertices.size() * sizeof(Vertex); std::vector<std::uint16_t> indices = grid.GetIndices16(); const UINT ibByteSize = (UINT)indices.size() * sizeof(std::uint16_t); auto geo = std::make_unique<MeshGeometry>(); geo->Name = "landGeo"; 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["grid"] = submesh; mGeometries["landGeo"] = std::move(geo); }
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 TexWaves::BuildLandGeometry() { GeometryGenerator geoGen; GeometryGenerator::MeshData grid = geoGen.CreateGrid(160.0f, 160.0f, 50, 50); std::vector<FrameResource::Vertex> vertices(grid.Vertices.size()); for (size_t i = 0; i < grid.Vertices.size(); i++) { auto& p = grid.Vertices[i].Position; vertices[i].Pos = p; vertices[i].Pos.y = GetHillsHeight(p.x, p.z); vertices[i].Normal = GetHillsNormal(p.z, p.z); vertices[i].TexC = grid.Vertices[i].TexCoord; } const UINT vbByteSize = (UINT)vertices.size() * sizeof(FrameResource::Vertex); std::vector<std::uint16_t> indices = grid.GetIndices16(); const UINT ibByteSize = (UINT)indices.size() * sizeof(uint16_t); auto geo = std::make_unique<MeshGeometry>(); geo->Name = "landGeo"; 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; SubmeshGeometry submesh; submesh.IndexCount = (UINT)indices.size(); submesh.StartIndexLocation = 0; submesh.BaseVertexLocation = 0; geo->DrawArgs["grid"] = submesh; _geometries["landGeo"] = std::move(geo); }
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 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 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); }