3d shape in google drawing
Getting Started |
---|
This lesson draws uncomplicated shapes in 3D.
Setup
Get-go create a new project using the instructions from the earlier lessons: Using DeviceResources and Adding the DirectX Tool Kit which we will utilize for this lesson.
Background
In the previous lesson, we generated geometry with code using PrimitiveBatch to depict simple shapes. Here we make use of GeometricPrimitive which procedurally generates shapes like spheres, cubes, etc. These 3D shapes are more than efficient to render because they brand use of indexed primitives, and because they brand utilize of a static rather than dynamic vertex buffer and alphabetize buffer.
Cartoon a sphere
In the Game.h file, add the following variables to the bottom of the Game class'south private declarations:
DirectX::SimpleMath::Matrix m_world; DirectX::SimpleMath::Matrix m_view; DirectX::SimpleMath::Matrix m_proj; std::unique_ptr<DirectX::GeometricPrimitive> m_shape;
In Game.cpp, add to the TODO of CreateDeviceDependentResources:
auto context = m_deviceResources->GetD3DDeviceContext(); m_shape = GeometricPrimitive::CreateSphere(context); m_world = Matrix::Identity;
In Game.cpp, add to the TODO of CreateWindowSizeDependentResources:
auto size = m_deviceResources->GetOutputSize(); m_view = Matrix::CreateLookAt(Vector3(two.f, 2.f, 2.f), Vector3::Naught, Vector3::UnitY); m_proj = Matrix::CreatePerspectiveFieldOfView(XM_PI / 4.f, float(size.right) / bladder(size.bottom), 0.1f, ten.f);
In Game.cpp, add to the TODO of OnDeviceLost:
In Game.cpp, add to the TODO of Return:
m_shape->Draw(m_world, m_view, m_proj);
In Game.cpp, add together to the TODO of Update:
auto time = static_cast<bladder>(timer.GetTotalSeconds()); m_world = Matrix::CreateRotationZ(cosf(time) * 2.f);
Build and run, and you'll see a white lit sphere.
Drawing other built-in shapes
In Game.cpp modify the TODO of CreateDeviceDependentResources:
m_shape = GeometricPrimitive::CreateTorus(context);
Build and run to encounter a torus instead of a sphere.
Yous can try out other shapes like a cube, cone, cylinder, dodecahedron, or the classic Utah teapot.
m_shape = GeometricPrimitive::CreateCube(context); m_shape = GeometricPrimitive::CreateCone(context); m_shape = GeometricPrimitive::CreateCylinder(context); m_shape = GeometricPrimitive::CreateDodecahedron(context); m_shape = GeometricPrimitive::CreateTeapot(context);
Adding textures to 3D shapes
Start past saving earth.bmp into your new project's directory, then from the top menu select Project / Add Existing Item.... Select "globe.bmp" and click "OK".
In the Game.h file, add the post-obit variable to the bottom of the Game class's private declarations:
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_texture;
In Game.cpp, add together to the TODO of CreateDeviceDependentResources:
DX::ThrowIfFailed( CreateWICTextureFromFile(device, L"earth.bmp" , nullptr, m_texture.ReleaseAndGetAddressOf()));
In Game.cpp, add to the TODO of OnDeviceLost:
In Game.cpp modify the TODO of CreateDeviceDependentResources:
m_shape = GeometricPrimitive::CreateSphere(context);
In Game.cpp, modify to the TODO of Render:
m_shape->Describe(m_world, m_view, m_proj, Colors::White, m_texture.Get());
In Game.cpp, modify to the TODO of Update:
automobile fourth dimension = static_cast<float>(timer.GetTotalSeconds()); m_world = Matrix::CreateRotationY(time);
Build and you'll encounter planet earth spinning.
Using custom lighting and effects
By default the geometric primitive renderer uses a simple BasicEffect with default lighting settings. To get more control over the rendering, you can use your own effect.
In the Game.h file, add the following variables to the bottom of the Game class'due south private declarations:
std::unique_ptr<DirectX::BasicEffect> m_effect; Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;
In Game.cpp modify the TODO of CreateDeviceDependentResources:
m_effect = std::make_unique<BasicEffect>(device); m_effect->SetTextureEnabled(true); m_effect->SetPerPixelLighting(true); m_effect->SetLightingEnabled(true); m_effect->SetLightEnabled(0, true); m_effect->SetLightDiffuseColor(0, Colors::White); m_effect->SetLightDirection(0, -Vector3::UnitZ); m_shape = GeometricPrimitive::CreateSphere(context); m_shape->CreateInputLayout(m_effect.get(), m_inputLayout.ReleaseAndGetAddressOf()); DX::ThrowIfFailed( CreateWICTextureFromFile(device, L"globe.bmp" , nullptr, m_texture.ReleaseAndGetAddressOf())); m_effect->SetTexture(m_texture.Get()); m_world = Matrix::Identity;
In Game.cpp, add to the TODO of CreateWindowSizeDependentResources:
m_effect->SetView(m_view); m_effect->SetProjection(m_proj);
In Game.cpp, add to the TODO of OnDeviceLost:
m_effect.reset(); m_inputLayout.Reset();
In Game.cpp, modify to the TODO of Render:
m_effect->SetWorld(m_world); m_shape->Draw(m_effect.get(), m_inputLayout.Get());
Build and run to see earth with more 'space-similar' lighting.
More to explore
-
The
GeometricPrimitive
grade is designed for uncomplicated rendering, so it always uses theVertexPositionNormalTexture
vertex format. For details on how to utilize the congenital-in shape generation for other vertex formats, meet Custom vertex format. -
Using a custom shader, y'all can apply the basic box or sphere primitive as a "skybox". See Authoring an Effect.
-
Methods are provided to become access to the shape data as
std::vector
southward ofVertexType
anduint16_t
, andGeometricPrimitive
also allows yous to create drawable instances from such data. See Custom geometry
Next lesson: Rendering a model
Further reading
DirectX Tool Kit docs Effects, GeometricPrimitive
Source: https://github.com/microsoft/DirectXTK/wiki/3D-shapes
Post a Comment for "3d shape in google drawing"