Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

answer with C++ and make it clear with working code please ? Create a simple mes

ID: 3560843 • Letter: A

Question

answer with C++ and make it clear with working code please

? Create a simple mesh in a file using the following format: A line holding the word vertices, a colon, a space, and the number of vertices in your mesh A line for each vertex in your mesh, with each coordinate separated by commas A blank line o A line holding the word faces, a colon, a space, and the number of faces/triangles in your mesh A line for each face in your mesh, with each vertex index separated by a comma An example mesh file: Write a program that does the following: Loads the mesh into a custom mesh data structure in your code Draws the mesh to the screen using vertex and index buffers

Explanation / Answer

SimpleCubeVertex cubeVertices[] = { { float3(-0.5f, 0.5f, -0.5f), float3(0.0f, 1.0f, 0.0f) }, // +Y (top face) { float3( 0.5f, 0.5f, -0.5f), float3(1.0f, 1.0f, 0.0f) }, { float3( 0.5f, 0.5f, 0.5f), float3(1.0f, 1.0f, 1.0f) }, { float3(-0.5f, 0.5f, 0.5f), float3(0.0f, 1.0f, 1.0f) }, { float3(-0.5f, -0.5f, 0.5f), float3(0.0f, 0.0f, 1.0f) }, // -Y (bottom face) { float3( 0.5f, -0.5f, 0.5f), float3(1.0f, 0.0f, 1.0f) }, { float3( 0.5f, -0.5f, -0.5f), float3(1.0f, 0.0f, 0.0f) }, { float3(-0.5f, -0.5f, -0.5f), float3(0.0f, 0.0f, 0.0f) }, }; const D3D11_INPUT_ELEMENT_DESC basicVertexLayoutDesc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; ComPtr inputLayout; m_d3dDevice->CreateInputLayout( basicVertexLayoutDesc, ARRAYSIZE(basicVertexLayoutDesc), vertexShaderBytecode->Data, vertexShaderBytecode->Length, &inputLayout) ); truct ConstantBuffer { float4x4 model; float4x4 view; float4x4 projection; }; ComPtr m_constantBuffer; ConstantBuffer m_constantBufferData; // ... // Create a constant buffer for passing model, view, and projection matrices // to the vertex shader. This allows us to rotate the cube and apply // a perspective projection to it. D3D11_BUFFER_DESC constantBufferDesc = {0}; constantBufferDesc.ByteWidth = sizeof(m_constantBufferData); constantBufferDesc.Usage = D3D11_USAGE_DEFAULT; constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; constantBufferDesc.CPUAccessFlags = 0; constantBufferDesc.MiscFlags = 0; constantBufferDesc.StructureByteStride = 0; m_d3dDevice->CreateBuffer( &constantBufferDesc, nullptr, &m_constantBuffer ); m_constantBufferData.model = float4x4( // identity matrix, since you are not animating the object 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); ); // Specify the view (camera) transform corresponding to a camera position of // X = 0, Y = 1, Z = 2. m_constantBufferData.view = float4x4( -1.00000000f, 0.00000000f, 0.00000000f, 0.00000000f, 0.00000000f, 0.89442718f, 0.44721359f, 0.00000000f, 0.00000000f, 0.44721359f, -0.89442718f, -2.23606800f, 0.00000000f, 0.00000000f, 0.00000000f, 1.00000000f); // Finally, update the constant buffer perspective projection parameters // to account for the size of the application window. In this sample, // the parameters are fixed to a 70-degree field of view, with a depth // range of 0.01 to 100. float xScale = 1.42814801f; float yScale = 1.42814801f; if (backBufferDesc.Width > backBufferDesc.Height) { xScale = yScale * static_cast(backBufferDesc.Height) / static_cast(backBufferDesc.Width); } else { yScale = xScale * static_cast(backBufferDesc.Width) / static_cast(backBufferDesc.Height); } m_constantBufferData.projection = float4x4( xScale, 0.0f, 0.0f, 0.0f, 0.0f, yScale, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, -0.01f, 0.0f, 0.0f, -1.0f, 0.0f ); // Set the vertex and index buffers, and specify the way they define geometry. UINT stride = sizeof(SimpleCubeVertex); UINT offset = 0; m_d3dDeviceContext->IASetVertexBuffers( 0, 1, vertexBuffer.GetAddressOf(), &stride, &offset); m_d3dDeviceContext->IASetIndexBuffer( indexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0); m_d3dDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // Set the vertex and pixel shader stage state. m_d3dDeviceContext->VSSetShader( vertexShader.Get(), nullptr, 0); m_d3dDeviceContext->VSSetConstantBuffers( 0, 1, m_constantBuffer.GetAddressOf()); cbuffer simpleConstantBuffer : register( b0 ) { matrix model; matrix view; matrix projection; }; struct VertexShaderInput { float3 pos : POSITION; float3 color : COLOR; }; struct PixelShaderInput { float4 pos : SV_POSITION; float4 color : COLOR; }; PixelShaderInput SimpleVertexShader(VertexShaderInput input) { PixelShaderInput vertexShaderOutput; float4 pos = float4(input.pos, 1.0f); // Transform the vertex position into projection space. pos = mul(pos, model); pos = mul(pos, view); pos = mul(pos, projection); vertexShaderOutput.pos = pos; // Pass the vertex color through to the pixel shader. vertexShaderOutput.color = float4(input.color, 1.0f); return vertexShaderOutput; } m_d3dDeviceContext->PSSetShader( pixelShader.Get(), nullptr, 0 ); struct PixelShaderInput { float4 pos : SV_POSITION; }; float4 SimplePixelShader(PixelShaderInput input) : SV_TARGET { // Draw the entire triangle yellow. return float4(1.0f, 1.0f, 0.0f, 1.0f); } // Draw the cube. m_d3dDeviceContext->DrawIndexed( ARRAYSIZE(cubeIndices), 0, 0 ); // Present the rendered image to the window. Because the maximum frame latency is set to 1, // the render loop is generally throttled to the screen refresh rate, typically around // 60Hz, by sleeping the app on Present until the screen is refreshed. m_swapChain->Present(1, 0);