DX sprites and stuff I guess

This commit is contained in:
2015-10-19 17:47:58 +02:00
parent 1abee730ec
commit 4a3575190f
6 changed files with 156 additions and 29 deletions
+2 -2
View File
@@ -225,7 +225,7 @@ public:
transform->Sticky = false;
auto model = m_World->AddComponent<Components::Model>(t_halfPipe);
model->ModelFile = "Models/Test/halfpipe/Halfpipe.obj";
model->Color = glm::vec4(0.8f, 0.8f, 0.8f, 0.3f);
model->Color = glm::vec4(0.8f, 0.8f, 0.8f, 1.f);
m_World->CommitEntity(t_halfPipe);
}
{
@@ -236,7 +236,7 @@ public:
transform->Sticky = false;
auto model = m_World->AddComponent<Components::Model>(t_halfPipe);
model->ModelFile = "Models/Test/halfpipe/Halfpipe.obj";
model->Color = glm::vec4(0.8f, 0.8f, 0.8f, 0.3f);
model->Color = glm::vec4(0.8f, 0.8f, 0.8f, 1.f);
m_World->CommitEntity(t_halfPipe);
}
+10 -1
View File
@@ -56,14 +56,23 @@ private:
Matrix ViewMatrix;
Matrix ProjectionMatrix;
Matrix InverseTransposeViewModel;
Vector3 LightPosition;
Vector4 Color;
Vector4 LightPositions[10];
Vector4 LightColors[10];
float LightRadii[10];
unsigned int NumLights;
float paddingBitch;
} constantBufferStruct;
ShaderProgram* shaderProgram = nullptr;
ID3D11Buffer* constantBuffer = nullptr;
Model* m_UnitQuad = nullptr;
Texture* m_WhiteSphereTexture = nullptr;
static DirectX::SimpleMath::Matrix GLMtoDXModelView(glm::mat4 m);
static DirectX::SimpleMath::Matrix GLMtoDXProjection(glm::mat4 m);
static bool DepthSort(const std::shared_ptr<RenderJob> &i, const std::shared_ptr<RenderJob> &j) { return (i->Depth < j->Depth); }
};
}
+8 -8
View File
@@ -549,14 +549,14 @@ void dd::Systems::LevelSystem::GetNextLevel()
// m is magenta.
// d is dark.
// Feel free to make your own.
glm::vec4 w = glm::vec4(1, 1, 1, 0);
glm::vec4 r = glm::vec4(1, 0, 0, 0);
glm::vec4 g = glm::vec4(0, 1, 0, 0);
glm::vec4 b = glm::vec4(0, 0, 1, 0);
glm::vec4 y = glm::vec4(1, 1, 0, 0);
glm::vec4 c = glm::vec4(0, 1, 1, 0);
glm::vec4 m = glm::vec4(1, 0, 1, 0);
glm::vec4 d = glm::vec4(0, 0, 0, 0);
glm::vec4 w = glm::vec4(1, 1, 1, 1);
glm::vec4 r = glm::vec4(1, 0, 0, 1);
glm::vec4 g = glm::vec4(0, 1, 0, 1);
glm::vec4 b = glm::vec4(0, 0, 1, 1);
glm::vec4 y = glm::vec4(1, 1, 0, 1);
glm::vec4 c = glm::vec4(0, 1, 1, 1);
glm::vec4 m = glm::vec4(1, 0, 1, 1);
glm::vec4 d = glm::vec4(0, 0, 0, 1);
//glm::vec4 p4 = glm::vec4(asd.f / 255.f, asd.f / 255.f, sad.f / 255.f, 1.f);
glm::vec4 br = glm::vec4(143.f / 255.f, 98.f / 255.f, 0.f / 255.f, 1.f);
+90 -1
View File
@@ -99,6 +99,21 @@ void dd::Renderer::Initialize()
m_Device->CreateRasterizerState(&rd, &rasterState);
m_DeviceContext->RSSetState(rasterState);
D3D11_BLEND_DESC blend = { 0 };
for (int i = 0; i < 1; i++) {
blend.RenderTarget[i].BlendEnable = true;
blend.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD;
blend.RenderTarget[i].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blend.RenderTarget[i].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blend.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blend.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE;
blend.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ZERO;
blend.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
}
ID3D11BlendState* blendState;
m_Device->CreateBlendState(&blend, &blendState);
m_DeviceContext->OMSetBlendState(blendState, nullptr, 0xffffffff);
// Set the viewport
D3D11_VIEWPORT viewport = { 0 };
viewport.TopLeftX = 0;
@@ -131,7 +146,7 @@ void dd::Renderer::Initialize()
// Create constant buffer
// TODO: Put this in shader
D3D11_BUFFER_DESC bd = { };
bd.ByteWidth = 64*6;
bd.ByteWidth = sizeof(ConstantBufferStruct);
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
m_Device->CreateBuffer(&bd, NULL, &constantBuffer);
m_DeviceContext->VSSetConstantBuffers(0, 1, &constantBuffer);
@@ -152,12 +167,17 @@ void dd::Renderer::Initialize()
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
m_Device->CreateSamplerState(&samplerDesc, &m_SamplerState);
m_UnitQuad = ResourceManager::Load<Model>("Models/Core/UnitQuad.obj");
m_WhiteSphereTexture = ResourceManager::Load<Texture>("Textures/Test/Water.png");
}
void dd::Renderer::Draw(RenderQueueCollection& rq)
{
using namespace DirectX::SimpleMath;
rq.Forward.Jobs.sort(dd::Renderer::DepthSort);
float color[] = { 0.f, 0.f, 0.f, 1.f };
m_DeviceContext->ClearRenderTargetView(m_BackBuffer, color);
m_DeviceContext->ClearDepthStencilView(m_DepthBuffer, D3D11_CLEAR_DEPTH, 1.0f, 0);
@@ -165,10 +185,31 @@ void dd::Renderer::Draw(RenderQueueCollection& rq)
Matrix proj = DirectX::SimpleMath::Matrix::CreatePerspectiveFieldOfView(m_Camera->m_FOV, m_Camera->m_AspectRatio, m_Camera->m_NearClip, m_Camera->m_FarClip); // Right-handed
Matrix view = Matrix::CreateTranslation(Vector3(m_Camera->Position().x, m_Camera->Position().y, -m_Camera->Position().z));
int i = 0;
constantBufferStruct.NumLights = 0;
for (auto &job : rq.Lights) {
auto pointLightJob = std::dynamic_pointer_cast<PointLightJob>(job);
if (pointLightJob) {
glm::vec3 p = pointLightJob->Position;
constantBufferStruct.LightPositions[i] = Vector4(p.r, p.g, p.b, 0.f);
glm::vec3 c = pointLightJob->DiffuseColor;
constantBufferStruct.LightColors[i] = Vector4(c.r, c.g, c.b, 1.f);
constantBufferStruct.LightRadii[i] = pointLightJob->Radius;
constantBufferStruct.NumLights++;
i++;
}
}
//constantBufferStruct.LightPositions[0] = Vector4(2.0f, 0.0f, 5.0f, 0.0f);
//constantBufferStruct.LightPositions[1] = Vector4(-2.0f, 0.0f, 5.0f, 0.0f);
for (auto &job : rq.Deferred) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
if (modelJob) {
constantBufferStruct.MVP = Matrix(glm::value_ptr(modelJob->ModelMatrix)) * view * proj;
constantBufferStruct.InverseTransposeViewModel = (Matrix(glm::value_ptr(modelJob->ModelMatrix)) * view).Transpose().Invert();
constantBufferStruct.Color = Vector4(modelJob->Color.r, modelJob->Color.g, modelJob->Color.b, modelJob->Color.a);
m_DeviceContext->UpdateSubresource(constantBuffer, 0, 0, &constantBufferStruct, 0, 0);
m_DeviceContext->PSSetSamplers(0, 1, &m_SamplerState);
@@ -189,6 +230,54 @@ void dd::Renderer::Draw(RenderQueueCollection& rq)
}
}
for (auto &job : rq.Forward) {
auto spriteJob = std::dynamic_pointer_cast<SpriteJob>(job);
if (spriteJob) {
constantBufferStruct.MVP = Matrix(glm::value_ptr(spriteJob->ModelMatrix)) * view * proj;
constantBufferStruct.InverseTransposeViewModel = (Matrix(glm::value_ptr(spriteJob->ModelMatrix)) * view).Transpose().Invert();
constantBufferStruct.Color = Vector4(glm::value_ptr(spriteJob->Color));
m_DeviceContext->UpdateSubresource(constantBuffer, 0, 0, &constantBufferStruct, 0, 0);
m_DeviceContext->PSSetSamplers(0, 1, &m_SamplerState);
m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
UINT stride = sizeof(Model::Vertex);
UINT offset = 0;
m_DeviceContext->IASetVertexBuffers(0, 1, &m_UnitQuad->VertexBuffer, &stride, &offset);
m_DeviceContext->IASetIndexBuffer(m_UnitQuad->IndexBuffer, DXGI_FORMAT_R32_UINT, 0);
if (spriteJob->DiffuseTexture != nullptr) {
m_DeviceContext->PSSetShaderResources(0, 1, &spriteJob->DiffuseTexture->m_ShaderResourceView);
}
//m_DeviceContext->Draw(modelJob->EndIndex - modelJob->StartIndex + 1, modelJob->StartIndex);
m_DeviceContext->DrawIndexed(m_UnitQuad->m_Indices.size(), 0, 0);
continue;
}
auto waterJob = std::dynamic_pointer_cast<WaterParticleJob>(job);
if (waterJob) {
constantBufferStruct.MVP = Matrix(glm::value_ptr(waterJob->ModelMatrix)) * view * proj;
constantBufferStruct.InverseTransposeViewModel = (Matrix(glm::value_ptr(waterJob->ModelMatrix)) * view).Transpose().Invert();
constantBufferStruct.Color = Vector4(glm::value_ptr(waterJob->Color));
m_DeviceContext->UpdateSubresource(constantBuffer, 0, 0, &constantBufferStruct, 0, 0);
m_DeviceContext->PSSetSamplers(0, 1, &m_SamplerState);
m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
UINT stride = sizeof(Model::Vertex);
UINT offset = 0;
m_DeviceContext->IASetVertexBuffers(0, 1, &m_UnitQuad->VertexBuffer, &stride, &offset);
m_DeviceContext->IASetIndexBuffer(m_UnitQuad->IndexBuffer, DXGI_FORMAT_R32_UINT, 0);
m_DeviceContext->PSSetShaderResources(0, 1, &m_WhiteSphereTexture->m_ShaderResourceView);
//m_DeviceContext->Draw(modelJob->EndIndex - modelJob->StartIndex + 1, modelJob->StartIndex);
m_DeviceContext->DrawIndexed(m_UnitQuad->m_Indices.size(), 0, 0);
continue;
}
}
//constantBufferStruct.ModelMatrix = GLMtoDX(glm::mat4(1.f));
//constantBufferStruct.ViewMatrix = GLMtoDX(m_Camera->ViewMatrix());
//constantBufferStruct.ProjectionMatrix = proj;
+42 -14
View File
@@ -5,7 +5,11 @@ cbuffer ConstantBuffer
float4x4 ViewMatrix;
float4x4 ProjectionMatrix;
float4x4 InverseTransposeViewModel;
float3 LightPosition;
float4 Color;
float4 LightPositions[10];
float4 LightColors[10];
float LightRadii[10];
uint NumLights;
}
Texture2D Texture;
@@ -27,22 +31,46 @@ struct VOut
float4 BoneWeights2 : BLENDWEIGHT1;
};
float4 phong(float3 lightPos, float lightRadius, float3 lightColor, float3 position, float3 normal, float3 specular, float specularExponent)
{
float3 LightSpecular = float3(1.0f, 1.0f, 1.0f) * 0.5f;
// Diffuse
lightPos = mul(ViewMatrix, float4(lightPos, 1.0f)).xyz;
float3 distanceToLight = lightPos - position;
float3 directionToLight = normalize(distanceToLight);
float dotProd = dot(directionToLight, normal);
float3 Idiffuse = lightColor * dotProd;
// Specular
float3 surfaceToViewer = normalize(-position);
float3 halfWay = normalize(surfaceToViewer + directionToLight);
float dotSpecular = max(dot(halfWay, normal), 0.0f);
float specularFactor = pow(dotSpecular, specularExponent);
float3 Ispecular = specular * LightSpecular * specularFactor;
// Attenuation
float dist = distance(lightPos, position);
float attenuation = pow(max(0.0f, 1.0f - (dist / lightRadius)), 2.0f);
return float4((Idiffuse + Ispecular) * attenuation, 1.0f);
}
float4 main(VOut input) : SV_TARGET
{
float4 ambientLight = float4(1.0f, 1.0f, 1.0f, 1.0f) * 0.1f;
////float4 lightPos = float4(0.0f, -1.0f, 2.0f, 0.0f);
float4 lightColor = float4(1.0f, 1.0f, 1.0f, 1.0f);
//float4 ambientLight = float4(1.0f, 1.0f, 1.0f, 1.0f) * 0.1f;
//float4 lightColor = float4(1.0f, 1.0f, 1.0f, 1.0f);
//float3 lightToPixel = normalize(LightPosition - mul(input.Position, transpose(ModelMatrix)));
//float diffuseBrightness = saturate(dot(lightToPixel, normalize(mul(input.Normal, transpose(ModelMatrix)))));
float4 diffuseTexture = Texture.Sample(samplerState, input.TextureCoord) * Color;
float4 lights = float4(1.f, 1.f, 1.f, 1.f);
float3 lightToPixel = normalize(LightPosition - mul(input.Position, transpose(ModelMatrix)));
for (uint i = 0; i < NumLights; i++) {
float4 light = phong(LightPositions[i], LightRadii[i], LightColors[i].rgb, input.Position, normalize(input.Normal.xyz), float3(1.0f, 1.0f, 1.0f), 1.0f);
lights = lights + light;
}
//float3 lightModelPos = mul(ModelMatrix, LightPosition);
float diffuseBrightness = saturate(dot(lightToPixel, normalize(mul(input.Normal, transpose(ModelMatrix)))));
//input.texcoord.y = 1 - input.texcoord.y;
//return float4(input.normal.xy, 0.0f, 0.0f);
return Texture.Sample(samplerState, input.TextureCoord);
//return float4(input.color.xy, 0.0f, 0.0f);
//return (ambientLight + lightColor * diffuseBrightness) * Texture.Sample(samplerState, input.texcoord);
//return float4(0.0f, 0.0f, 1.0f, 1.0f);
//return float4(1.f, 0.f, 0.f, 1.f);
return float4(diffuseTexture.rgb * 0.8f, diffuseTexture.a) * lights;
}
@@ -5,7 +5,8 @@ cbuffer ConstantBuffer
float4x4 ViewMatrix;
float4x4 ProjectionMatrix;
float4x4 InverseTransposeViewModel;
float3 LightPosition;
float4 Color;
float4 LightPositions[2];
}
struct VIn
@@ -50,8 +51,8 @@ VOut main(VIn input)
output.PixelPosition = mul(MVP, input.Position);
//TODO: Make sure that boneTransform works here.
//output.Position = (V * M * boneTransform * vec4(Position, 1.0)).xyz;
//output.Normal = (inverse(transpose(V * M)) * boneTransform * vec4(Normal, 0.0)).xyz;
output.Position = mul(ModelMatrix * ViewMatrix, float4(input.Position.xyz, 1.0f));
output.Normal = mul(InverseTransposeViewModel, float4(input.Normal.xyz, 0.0f));
output.Tangent = input.Tangent;
output.BiTangent = input.BiTangent;
output.TextureCoord = input.TextureCoord;