Skip to content

Commit

Permalink
Add built-in BasicEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
mogemimi committed Nov 2, 2019
1 parent 360b058 commit c116637
Show file tree
Hide file tree
Showing 16 changed files with 1,426 additions and 0 deletions.
8 changes: 8 additions & 0 deletions build/pomdog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@
D719A53F23497EB600C1868B /* PNM.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D719A53923497EB600C1868B /* PNM.cpp */; };
D719A55F2349868200C1868B /* TrueTypeFontLoader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D719A55E2349868200C1868B /* TrueTypeFontLoader.cpp */; };
D719A5602349868200C1868B /* TrueTypeFontLoader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D719A55E2349868200C1868B /* TrueTypeFontLoader.cpp */; };
D7255BA6236D59AB00B8E4A0 /* BasicEffect.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D7255BA5236D59AB00B8E4A0 /* BasicEffect.cpp */; };
D7255BA7236D59AB00B8E4A0 /* BasicEffect.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D7255BA5236D59AB00B8E4A0 /* BasicEffect.cpp */; };
D7334DC8228AC80200232225 /* PolylineBatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D7334DC6228AC80200232225 /* PolylineBatch.cpp */; };
D7334DC9228AC80200232225 /* PolylineBatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D7334DC6228AC80200232225 /* PolylineBatch.cpp */; };
D7334DCA228AC80200232225 /* PrimitiveBatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D7334DC7228AC80200232225 /* PrimitiveBatch.cpp */; };
Expand Down Expand Up @@ -883,6 +885,8 @@
D719A55C2349864500C1868B /* SpriteFont.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = SpriteFont.hpp; sourceTree = "<group>"; };
D719A55D2349864500C1868B /* SpriteFontLoader.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = SpriteFontLoader.hpp; sourceTree = "<group>"; };
D719A55E2349868200C1868B /* TrueTypeFontLoader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TrueTypeFontLoader.cpp; sourceTree = "<group>"; };
D7255BA4236D598C00B8E4A0 /* BasicEffect.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = BasicEffect.hpp; sourceTree = "<group>"; };
D7255BA5236D59AB00B8E4A0 /* BasicEffect.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BasicEffect.cpp; sourceTree = "<group>"; };
D7334DC6228AC80200232225 /* PolylineBatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PolylineBatch.cpp; sourceTree = "<group>"; };
D7334DC7228AC80200232225 /* PrimitiveBatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PrimitiveBatch.cpp; sourceTree = "<group>"; };
D7334DCC228AC81F00232225 /* SpriteLine.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SpriteLine.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1739,6 +1743,7 @@
A9FC99031DC3D86C00C78D63 /* Graphics */ = {
isa = PBXGroup;
children = (
D7255BA5236D59AB00B8E4A0 /* BasicEffect.cpp */,
A98F5917211B2B89002960C1 /* LineBatch.cpp */,
A98F5918211B2B89002960C1 /* PolygonShapeBuilder.cpp */,
D7334DC6228AC80200232225 /* PolylineBatch.cpp */,
Expand All @@ -1757,6 +1762,7 @@
A9FC99081DC3D8E100C78D63 /* Graphics */ = {
isa = PBXGroup;
children = (
D7255BA4236D598C00B8E4A0 /* BasicEffect.hpp */,
D719A55A2349864500C1868B /* FontGlyph.hpp */,
D719A5572349864500C1868B /* LineBatch.hpp */,
D719A5592349864500C1868B /* PolygonShapeBuilder.hpp */,
Expand Down Expand Up @@ -2531,6 +2537,7 @@
A98F5926211B2B9A002960C1 /* SpriteFontLoader.cpp in Sources */,
D7334E17228AEC1300232225 /* PostProcessCompositor.cpp in Sources */,
03F46EF697B70E1EE9B2533B /* CocoaWindowDelegate.mm in Sources */,
D7255BA6236D59AB00B8E4A0 /* BasicEffect.cpp in Sources */,
2A5B52A528596019ABF40617 /* GameHostCocoa.mm in Sources */,
E6DAE91B1C77C1736DB66CD7 /* GameWindowCocoa.mm in Sources */,
E9DF3779DAEBF65FC42EA8CF /* KeyboardCocoa.cpp in Sources */,
Expand Down Expand Up @@ -2701,6 +2708,7 @@
A98F5927211B2B9A002960C1 /* SpriteFontLoader.cpp in Sources */,
D7334E18228AEC1300232225 /* PostProcessCompositor.cpp in Sources */,
E8ACB94BC7A78F57A854EC2C /* CocoaWindowDelegate.mm in Sources */,
D7255BA7236D59AB00B8E4A0 /* BasicEffect.cpp in Sources */,
0AECD9F61617195B0205EFEF /* GameHostCocoa.mm in Sources */,
71E352760BC127DCF199C59B /* GameWindowCocoa.mm in Sources */,
94FC0ADEB7D7E0AE9084A692 /* KeyboardCocoa.cpp in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions build/pomdog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ set(POMDOG_SOURCES_CORE
)

set(POMDOG_SOURCES_EXPERIMENTAL
${POMDOG_DIR}/include/Pomdog/Experimental/Graphics/BasicEffect.hpp
${POMDOG_DIR}/include/Pomdog/Experimental/Graphics/FontGlyph.hpp
${POMDOG_DIR}/include/Pomdog/Experimental/Graphics/LineBatch.hpp
${POMDOG_DIR}/include/Pomdog/Experimental/Graphics/PolylineBatch.hpp
Expand Down Expand Up @@ -441,6 +442,7 @@ set(POMDOG_SOURCES_EXPERIMENTAL
${POMDOG_DIR}/include/Pomdog/Experimental/TexturePacker/TextureAtlasLoader.hpp
${POMDOG_DIR}/include/Pomdog/Experimental/TexturePacker/TextureRegion.hpp
${POMDOG_DIR}/include/Pomdog/Experimental/Tween/EasingHelper.hpp
${POMDOG_DIR}/src/Experimental/Graphics/BasicEffect.cpp
${POMDOG_DIR}/src/Experimental/Graphics/LineBatch.cpp
${POMDOG_DIR}/src/Experimental/Graphics/PolylineBatch.cpp
${POMDOG_DIR}/src/Experimental/Graphics/PolygonShapeBuilder.cpp
Expand Down
3 changes: 3 additions & 0 deletions examples/FeatureShowcase/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(CMAKE_CONFIGURATION_TYPES Debug Release)

source_group(Source REGULAR_EXPRESSION "Source/*")
source_group(Source\\AudioClipTest REGULAR_EXPRESSION "Source/AudioClipTest/*")
source_group(Source\\BasicEffectTest REGULAR_EXPRESSION "Source/BasicEffectTest/*")
source_group(Source\\Beam2DTest REGULAR_EXPRESSION "Source/Beam2DTest/*")
source_group(Source\\GamepadTest REGULAR_EXPRESSION "Source/GamepadTest/*")
source_group(Source\\HTTPClientTest REGULAR_EXPRESSION "Source/HTTPClientTest/*")
Expand Down Expand Up @@ -44,6 +45,8 @@ add_executable(${PRODUCT_NAME} WIN32
Source/FeatureShowcaseGame.hpp
Source/AudioClipTest/AudioClipTest.cpp
Source/AudioClipTest/AudioClipTest.hpp
Source/BasicEffectTest/BasicEffectTest.cpp
Source/BasicEffectTest/BasicEffectTest.hpp
Source/Beam2DTest/Beam2DTest.cpp
Source/Beam2DTest/Beam2DTest.hpp
Source/GamepadTest/GamepadTest.cpp
Expand Down
286 changes: 286 additions & 0 deletions examples/FeatureShowcase/Source/BasicEffectTest/BasicEffectTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
#include "BasicEffectTest.hpp"
#include <Pomdog/Experimental/Graphics/BasicEffect.hpp>
#include <cmath>

namespace FeatureShowcase {

BasicEffectTest::BasicEffectTest(const std::shared_ptr<GameHost>& gameHostIn)
: gameHost(gameHostIn)
, graphicsDevice(gameHostIn->GetGraphicsDevice())
, commandQueue(gameHostIn->GetGraphicsCommandQueue())
{
}

void BasicEffectTest::Initialize()
{
auto assets = gameHost->GetAssetManager();
auto clock = gameHost->GetClock();
commandList = std::make_shared<GraphicsCommandList>(*graphicsDevice);

// NOTE: Load texture from image file
if (auto[res, err] = assets->Load<Texture2D>("Textures/pomdog.png"); err != nullptr) {
Log::Verbose("failed to load texture: " + err->ToString());
}
else {
texture = std::move(res);
}

{
using VertexCombined = BasicEffect::VertexPositionNormalTexture;

// NOTE: Create vertex buffer
std::array<VertexCombined, 20> verticesCombo = {{
// top
{Vector3{0.0f, 1.0f, 0.0f}, Vector3{0.0f, 1.0f, 0.0f}, Vector2{0.0f, 1.0f}},
{Vector3{1.0f, 1.0f, 0.0f}, Vector3{0.0f, 1.0f, 0.0f}, Vector2{1.0f, 1.0f}},
{Vector3{1.0f, 1.0f, 1.0f}, Vector3{0.0f, 1.0f, 0.0f}, Vector2{1.0f, 0.0f}},
{Vector3{0.0f, 1.0f, 1.0f}, Vector3{0.0f, 1.0f, 0.0f}, Vector2{0.0f, 0.0f}},

// left
{Vector3{0.0f, 0.0f, 1.0f}, Vector3{-1.0f, 0.0f, 0.0f}, Vector2{0.0f, 1.0f}},
{Vector3{0.0f, 0.0f, 0.0f}, Vector3{-1.0f, 0.0f, 0.0f}, Vector2{1.0f, 1.0f}},
{Vector3{0.0f, 1.0f, 0.0f}, Vector3{-1.0f, 0.0f, 0.0f}, Vector2{1.0f, 0.0f}},
{Vector3{0.0f, 1.0f, 1.0f}, Vector3{-1.0f, 0.0f, 0.0f}, Vector2{0.0f, 0.0f}},

// right
{Vector3{1.0f, 0.0f, 1.0f}, Vector3{1.0f, 0.0f, 0.0f}, Vector2{0.0f, 0.0f}},
{Vector3{1.0f, 0.0f, 0.0f}, Vector3{1.0f, 0.0f, 0.0f}, Vector2{1.0f, 0.0f}},
{Vector3{1.0f, 1.0f, 0.0f}, Vector3{1.0f, 0.0f, 0.0f}, Vector2{1.0f, 1.0f}},
{Vector3{1.0f, 1.0f, 1.0f}, Vector3{1.0f, 0.0f, 0.0f}, Vector2{0.0f, 1.0f}},

// front
{Vector3{0.0f, 0.0f, 0.0f}, Vector3{0.0f, 0.0f, -1.0f}, Vector2{0.0f, 0.0f}},
{Vector3{1.0f, 0.0f, 0.0f}, Vector3{0.0f, 0.0f, -1.0f}, Vector2{1.0f, 0.0f}},
{Vector3{1.0f, 1.0f, 0.0f}, Vector3{0.0f, 0.0f, -1.0f}, Vector2{1.0f, 1.0f}},
{Vector3{0.0f, 1.0f, 0.0f}, Vector3{0.0f, 0.0f, -1.0f}, Vector2{0.0f, 1.0f}},

// back
{Vector3{0.0f, 0.0f, 1.0f}, Vector3{0.0f, 0.0f, 1.0f}, Vector2{0.0f, 0.0f}},
{Vector3{1.0f, 0.0f, 1.0f}, Vector3{0.0f, 0.0f, 1.0f}, Vector2{1.0f, 0.0f}},
{Vector3{1.0f, 1.0f, 1.0f}, Vector3{0.0f, 0.0f, 1.0f}, Vector2{1.0f, 1.0f}},
{Vector3{0.0f, 1.0f, 1.0f}, Vector3{0.0f, 0.0f, 1.0f}, Vector2{0.0f, 1.0f}},
}};

vertexBuffer1 = std::make_shared<VertexBuffer>(
graphicsDevice,
verticesCombo.data(),
verticesCombo.size(),
sizeof(VertexCombined),
BufferUsage::Immutable);
}
{
using VertexCombined = BasicEffect::VertexPositionColor;

// NOTE: Create vertex buffer
std::array<VertexCombined, 20> verticesCombo = {{
// top
{Vector3{0.0f, 1.0f, 0.0f}, Color::White.ToVector4()},
{Vector3{1.0f, 1.0f, 0.0f}, Color::White.ToVector4()},
{Vector3{1.0f, 1.0f, 1.0f}, Color::White.ToVector4()},
{Vector3{0.0f, 1.0f, 1.0f}, Color::White.ToVector4()},

// left
{Vector3{0.0f, 0.0f, 1.0f}, Color::Yellow.ToVector4()},
{Vector3{0.0f, 0.0f, 0.0f}, Color::Yellow.ToVector4()},
{Vector3{0.0f, 1.0f, 0.0f}, Color::Yellow.ToVector4()},
{Vector3{0.0f, 1.0f, 1.0f}, Color::Yellow.ToVector4()},

// right
{Vector3{1.0f, 0.0f, 1.0f}, Color::Red.ToVector4()},
{Vector3{1.0f, 0.0f, 0.0f}, Color::Red.ToVector4()},
{Vector3{1.0f, 1.0f, 0.0f}, Color::Red.ToVector4()},
{Vector3{1.0f, 1.0f, 1.0f}, Color::Red.ToVector4()},

// front
{Vector3{0.0f, 0.0f, 0.0f}, Color::Blue.ToVector4()},
{Vector3{1.0f, 0.0f, 0.0f}, Color::Blue.ToVector4()},
{Vector3{1.0f, 1.0f, 0.0f}, Color::Blue.ToVector4()},
{Vector3{0.0f, 1.0f, 0.0f}, Color::Blue.ToVector4()},

// back
{Vector3{0.0f, 0.0f, 1.0f}, Color::Red.ToVector4()},
{Vector3{1.0f, 0.0f, 1.0f}, Color::Yellow.ToVector4()},
{Vector3{1.0f, 1.0f, 1.0f}, Color::Blue.ToVector4()},
{Vector3{0.0f, 1.0f, 1.0f}, Color::White.ToVector4()},
}};

vertexBuffer2 = std::make_shared<VertexBuffer>(
graphicsDevice,
verticesCombo.data(),
verticesCombo.size(),
sizeof(VertexCombined),
BufferUsage::Immutable);
}
{
// NOTE: Create index buffer
std::array<std::uint16_t, 30> indices = {{
3, 1, 0,
2, 1, 3,

7, 5, 4,
6, 5, 7,

10, 8, 9,
11, 8, 10,

15, 13, 12,
14, 13, 15,

18, 16, 17,
19, 16, 18,
}};

indexBuffer = std::make_shared<IndexBuffer>(
graphicsDevice,
IndexElementSize::SixteenBits,
indices.data(),
indices.size(),
BufferUsage::Immutable);
}
{
// NOTE: Create constant buffer
modelConstantBuffer = std::make_shared<ConstantBuffer>(
graphicsDevice,
sizeof(BasicEffect::ModelConstantBuffer),
BufferUsage::Dynamic);

worldConstantBuffer = std::make_shared<ConstantBuffer>(
graphicsDevice,
sizeof(BasicEffect::WorldConstantBuffer),
BufferUsage::Dynamic);
}
{
// NOTE: Create sampler state
sampler = std::make_shared<SamplerState>(
graphicsDevice,
SamplerDescription::CreateLinearClamp());
}
{
auto presentationParameters = graphicsDevice->GetPresentationParameters();

BasicEffect::BasicEffectDescription effectDesc;
effectDesc.LightingEnabled = true;
effectDesc.TextureEnabled = true;
effectDesc.VertexColorEnabled = false;

// NOTE: Create pipeline state
pipelineState1 = BasicEffect::CreateBasicEffect(*assets, effectDesc)
.SetRenderTargetViewFormat(presentationParameters.BackBufferFormat)
.SetDepthStencilViewFormat(presentationParameters.DepthStencilFormat)
.SetDepthStencilState(DepthStencilDescription::CreateDefault())
.SetBlendState(BlendDescription::CreateNonPremultiplied())
.SetRasterizerState(RasterizerDescription::CreateDefault())
.Build();
}
{
auto presentationParameters = graphicsDevice->GetPresentationParameters();

BasicEffect::BasicEffectDescription effectDesc;
effectDesc.LightingEnabled = false;
effectDesc.TextureEnabled = false;
effectDesc.VertexColorEnabled = true;

// NOTE: Create pipeline state
pipelineState2 = BasicEffect::CreateBasicEffect(*assets, effectDesc)
.SetRenderTargetViewFormat(presentationParameters.BackBufferFormat)
.SetDepthStencilViewFormat(presentationParameters.DepthStencilFormat)
.SetDepthStencilState(DepthStencilDescription::CreateDefault())
.SetBlendState(BlendDescription::CreateNonPremultiplied())
.SetRasterizerState(RasterizerDescription::CreateDefault())
.Build();
}
}

void BasicEffectTest::Update()
{
auto presentationParameters = graphicsDevice->GetPresentationParameters();

constexpr float rotateSpeed = 0.5f;

auto projectionMatrix = Matrix4x4::CreatePerspectiveFieldOfViewLH(
MathHelper::ToRadians(45.0f),
static_cast<float>(presentationParameters.BackBufferWidth) / presentationParameters.BackBufferHeight,
0.01f,
1000.0f);

auto cameraPosition = Vector3{0.0f, 6.0f, 0.0f};
auto cameraDirection = Vector3{0.0f, -1.0f, 1.0f};
auto viewMatrix = Matrix4x4::CreateLookAtLH(cameraPosition, cameraPosition + cameraDirection, Vector3::UnitY);

auto lightDirection = Vector3::Normalize(Vector3{-0.5f, -1.0f, 0.5f});

// NOTE: Update constant buffer for world
BasicEffect::WorldConstantBuffer worldConstants;
worldConstants.ViewProjection = viewMatrix * projectionMatrix;
worldConstants.InverseView = Matrix4x4::Invert(viewMatrix);
worldConstants.LightDirection = Vector4{lightDirection, 0.0f};
worldConstantBuffer->SetValue(worldConstants);

auto time = static_cast<float>(gameHost->GetClock()->GetTotalGameTime().count());
auto rotateY = Math::TwoPi<float> * rotateSpeed * time;

auto mouse = gameHost->GetMouse()->GetState();
if (mouse.LeftButton == ButtonState::Pressed) {
rotateY = -Math::TwoPi<float> * (static_cast<float>(mouse.Position.X) / static_cast<float>(presentationParameters.BackBufferWidth));
}

auto modelMatrix = Matrix4x4::CreateTranslation(Vector3{-0.5f, -0.5f, -0.5f})
* Matrix4x4::CreateScale(1.0f + std::cos(time * 5.0f) * 0.1f)
* Matrix4x4::CreateRotationY(rotateY)
* Matrix4x4::CreateTranslation(Vector3{0.0f, 0.0f, 6.0f});

constexpr float metalness = 0.1f;

// NOTE: Update constant buffer for model
BasicEffect::ModelConstantBuffer modelConstants;
modelConstants.Model = modelMatrix;
modelConstants.Material = Vector4{metalness, 0.0f, 0.0f, 0.0f};
modelConstants.Color = Vector4{1.0f, 1.0f, 1.0f, 1.0f};
modelConstantBuffer->SetValue(modelConstants);
}

void BasicEffectTest::Draw()
{
auto presentationParameters = graphicsDevice->GetPresentationParameters();

Viewport viewport = {0, 0, presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight};
RenderPass pass;
pass.RenderTargets.emplace_back(nullptr, Color::CornflowerBlue.ToVector4());
pass.ClearDepth = 1.0f;
pass.ClearStencil = 0;
pass.Viewport = viewport;
pass.ScissorRect = viewport.GetBounds();

auto mouse = gameHost->GetMouse()->GetState();

commandList->Reset();
commandList->SetRenderPass(std::move(pass));
commandList->SetConstantBuffer(0, modelConstantBuffer);
commandList->SetConstantBuffer(1, worldConstantBuffer);
commandList->SetSamplerState(0, sampler);
commandList->SetTexture(0, texture);
if (mouse.RightButton == ButtonState::Pressed) {
commandList->SetVertexBuffer(vertexBuffer2);
commandList->SetPipelineState(pipelineState2);
}
else {
commandList->SetVertexBuffer(vertexBuffer1);
commandList->SetPipelineState(pipelineState1);
}
commandList->SetPrimitiveTopology(PrimitiveTopology::TriangleList);
commandList->DrawIndexed(indexBuffer, indexBuffer->GetIndexCount(), 0);
commandList->Close();

constexpr bool isStandalone = false;
if constexpr (isStandalone) {
commandQueue->Reset();
commandQueue->PushbackCommandList(commandList);
commandQueue->ExecuteCommandLists();
commandQueue->Present();
}
else {
commandQueue->PushbackCommandList(commandList);
}
}

} // namespace FeatureShowcase
Loading

0 comments on commit c116637

Please sign in to comment.