Working DirectX textures

This commit is contained in:
2015-10-13 17:33:12 +02:00
parent a782e6b40a
commit e195e69408
18 changed files with 215 additions and 3856 deletions
-12
View File
@@ -1,12 +0,0 @@
# Blender MTL File: 'submarine.blend'
# Material Count: 1
newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Kd SubmarineTexture.png
File diff suppressed because it is too large Load Diff
-12
View File
@@ -1,12 +0,0 @@
# Blender MTL File: 'submarine.blend'
# Material Count: 1
newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Kd SubmarineTexture.png
File diff suppressed because it is too large Load Diff
Binary file not shown.

Before

Width:  |  Height:  |  Size: 736 KiB

+1
View File
@@ -20,6 +20,7 @@
#define DD_BASERENDERER_H__
#include "Core/Util/Rectangle.h"
#include "Core/ResourceManager.h"
#include "Rendering/Camera.h"
#include "Rendering/RenderQueue.h"
+45
View File
@@ -0,0 +1,45 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BaseTexture_h__
#define BaseTexture_h__
#include <string>
#include <unordered_map>
#include <cstdio>
#include "Core/ResourceManager.h"
#include "Rendering/PNG.h"
namespace dd
{
class BaseTexture : public Resource
{
friend class ResourceManager;
public:
unsigned int Width = 0;
unsigned int Height = 0;
};
}
#endif
+4 -2
View File
@@ -4,9 +4,10 @@
#include <string>
#include <d3d11.h>
#include "Rendering/RawModel.h"
#include "Rendering/Renderer.h"
#include "Core/StaticSystem.h"
#include "Rendering/Renderer.h"
namespace dd
{
@@ -21,7 +22,8 @@ private:
public:
~Model();
ID3D11Buffer* VertexBuffer;
ID3D11Buffer* VertexBuffer = nullptr;
ID3D11Buffer* IndexBuffer = nullptr;
unsigned int NumVertices;
};
@@ -26,7 +26,6 @@ using namespace DirectX::SimpleMath;
//#pragma comment (lib, "DirectXTK.lib")
#include "Rendering/BaseRenderer.h"
#include "Core/StaticSystem.h"
namespace dd
{
@@ -47,6 +46,7 @@ public:
ID3D11DeviceContext* m_DeviceContext = nullptr;
ID3D11RenderTargetView* m_BackBuffer = nullptr;
ID3D11DepthStencilView* m_DepthBuffer = nullptr;
ID3D11SamplerState* m_SamplerState = nullptr;
private:
struct ConstantBufferStruct
@@ -63,7 +63,8 @@ private:
const Model* testModel = nullptr;
ID3D11Buffer* constantBuffer = nullptr;
DirectX::SimpleMath::Matrix GLMtoDX(glm::mat4 m);
static DirectX::SimpleMath::Matrix GLMtoDXModelView(glm::mat4 m);
static DirectX::SimpleMath::Matrix GLMtoDXProjection(glm::mat4 m);
};
}
@@ -6,6 +6,7 @@
#include <d3d11.h>
#include <d3dcompiler.h>
#pragma comment (lib, "D3DCompiler.lib")
#include "Core/StaticSystem.h"
#include "Rendering/Renderer.h"
+11 -10
View File
@@ -19,17 +19,16 @@
#ifndef Texture_h__
#define Texture_h__
#include <string>
#include <unordered_map>
#include <cstdio>
#include <d3d11.h>
#include "Core/ResourceManager.h"
#include "Rendering/PNG.h"
#include "Rendering/BaseTexture.h"
#include "Core/StaticSystem.h"
#include "Rendering/Renderer.h"
namespace dd
{
class Texture : public Resource
class Texture : public BaseTexture
{
friend class ResourceManager;
@@ -39,11 +38,13 @@ private:
public:
~Texture();
unsigned int Width = 0;
unsigned int Height = 0;
};
operator ID3D11Texture2D*() { return m_Texture; }
ID3D11Texture2D* m_Texture = nullptr;
ID3D11ShaderResourceView* m_ShaderResourceView = nullptr;
private:
};
}
#endif // Texture_h__
#endif
+2 -11
View File
@@ -19,17 +19,12 @@
#ifndef Texture_h__
#define Texture_h__
#include <string>
#include <unordered_map>
#include <cstdio>
#include "Core/ResourceManager.h"
#include "Rendering/PNG.h"
#include "Rendering/BaseTexture.h"
namespace dd
{
class Texture : public Resource
class Texture : public BaseTexture
{
friend class ResourceManager;
@@ -43,14 +38,10 @@ public:
operator GLuint() const { return m_Texture; }
unsigned int Width = 0;
unsigned int Height = 0;
private:
GLuint m_Texture = 0;
};
}
#endif // Texture_h__
+11 -17
View File
@@ -23,8 +23,6 @@
#include <forward_list>
#include "Core/Util/Rectangle.h"
#include "Rendering/Texture.h"
#include "Rendering/Model.h"
namespace dd
{
@@ -52,20 +50,18 @@ protected:
struct ModelJob : RenderJob
{
unsigned int ShaderID;
unsigned int TextureID;
unsigned int ShaderID = 0;
unsigned int TextureID = 0;
glm::mat4 ProjectionMatrix;
glm::mat4 ViewMatrix;
glm::mat4 ModelMatrix;
const Texture* DiffuseTexture;
const Texture* NormalTexture;
const Texture* SpecularTexture;
float Shininess;
float Shininess = 0.f;
glm::vec4 Color;
const dd::Model* Model;
unsigned int StartIndex;
unsigned int EndIndex;
const dd::Model* Model = nullptr;
unsigned int StartIndex = 0;
unsigned int EndIndex = 0;
void CalculateHash() override
{
@@ -75,15 +71,13 @@ struct ModelJob : RenderJob
struct SpriteJob : RenderJob
{
unsigned int ShaderID;
unsigned int TextureID;
unsigned int ShaderID = 0;
unsigned int TextureID = 0;
glm::mat4 ProjectionMatrix;
glm::mat4 ViewMatrix;
glm::mat4 ModelMatrix;
const Texture* DiffuseTexture;
const Texture* NormalTexture;
const Texture* SpecularTexture;
const Texture* DiffuseTexture = nullptr;
const Texture* NormalTexture = nullptr;
const Texture* SpecularTexture = nullptr;
glm::vec4 Color;
void CalculateHash() override
+2 -6
View File
@@ -85,12 +85,8 @@ void dd::Camera::UpdateProjectionMatrix()
// m_NearClip,
// m_FarClip
// );
m_ProjectionMatrix = glm::perspective(
m_FOV,
m_AspectRatio,
m_NearClip,
m_FarClip
);
m_ProjectionMatrix = glm::perspective(m_FOV, m_AspectRatio, m_NearClip, m_FarClip);
}
void dd::Camera::UpdateViewMatrix()
+7
View File
@@ -9,5 +9,12 @@ dd::Model::Model(std::string fileName)
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
D3D11_SUBRESOURCE_DATA vsrd = { m_Vertices.data(), 0, 0 };
StaticSystem::Get<Renderer>()->m_Device->CreateBuffer(&vbd, &vsrd, &VertexBuffer);
D3D11_BUFFER_DESC ibd = { 0 };
ibd.ByteWidth = sizeof(unsigned int) * m_Indices.size();
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
D3D11_SUBRESOURCE_DATA idata = { 0 };
idata.pSysMem = m_Indices.data();
StaticSystem::Get<Renderer>()->m_Device->CreateBuffer(&ibd, &idata, &IndexBuffer);
}
+74 -20
View File
@@ -1,5 +1,7 @@
#include "PrecompiledHeader.h"
#include "Rendering/Renderer.h"
#include "Rendering/Model.h"
#include "Rendering/ShaderProgram.h"
void dd::Renderer::Initialize()
@@ -18,13 +20,20 @@ void dd::Renderer::Initialize()
monitor = glfwGetPrimaryMonitor();
}
glfwWindowHint(GLFW_SAMPLES, 8);
m_Window = glfwCreateWindow(m_Resolution.Width, m_Resolution.Height, "daydream", monitor, nullptr);
m_Window = glfwCreateWindow(m_Resolution.Width, m_Resolution.Height, "DirectX 11", monitor, nullptr);
if (!m_Window) {
LOG_ERROR("GLFW: Failed to create window");
exit(EXIT_FAILURE);
}
//glfwMakeContextCurrent(m_Window);
// Create default camera
m_DefaultCamera = std::unique_ptr<dd::Camera>(new dd::Camera((float)m_Resolution.Width / m_Resolution.Height, 45.f, 0.01f, 5000.f));
m_DefaultCamera->SetPosition(glm::vec3(0, 0, 10));
if (m_Camera == nullptr) {
m_Camera = m_DefaultCamera.get();
}
DXGI_SWAP_CHAIN_DESC scd = { 0 };
scd.BufferCount = 1;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
@@ -100,7 +109,7 @@ void dd::Renderer::Initialize()
m_DeviceContext->RSSetViewports(1, &viewport);
testModel = ResourceManager::Load<Model>("Models/Core/UnitSphere.obj");
testModel = ResourceManager::Load<Model>("Models/Ship/Ship.obj");
shaderProgram = new ShaderProgram();
shaderProgram->CompileVertexShader(L"vertex.hlsl");
@@ -108,8 +117,8 @@ void dd::Renderer::Initialize()
D3D11_INPUT_ELEMENT_DESC VertexIED[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, sizeof(glm::vec3) * 4, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
shaderProgram->CreateInputLayout(VertexIED, sizeof(VertexIED) / sizeof(VertexIED[0]));
shaderProgram->Bind();
@@ -122,33 +131,66 @@ void dd::Renderer::Initialize()
m_Device->CreateBuffer(&bd, NULL, &constantBuffer);
m_DeviceContext->VSSetConstantBuffers(0, 1, &constantBuffer);
m_DeviceContext->PSSetConstantBuffers(0, 1, &constantBuffer);
D3D11_SAMPLER_DESC samplerDesc;
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
samplerDesc.BorderColor[0] = 1.f;
samplerDesc.BorderColor[1] = 0;
samplerDesc.BorderColor[2] = 1.f;
samplerDesc.BorderColor[3] = 0;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
m_Device->CreateSamplerState(&samplerDesc, &m_SamplerState);
}
void dd::Renderer::Draw(RenderQueueCollection& rq)
{
using namespace DirectX::SimpleMath;
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);
constantBufferStruct.MVP = GLMtoDX(glm::mat4(1.f));
constantBufferStruct.ModelMatrix = GLMtoDX(glm::mat4(1.f));
constantBufferStruct.ViewMatrix = GLMtoDX(glm::mat4(1.f));
constantBufferStruct.ProjectionMatrix = GLMtoDX(glm::mat4(1.f));
constantBufferStruct.InverseTransposeViewModel = GLMtoDX(glm::mat4(1.f));
constantBufferStruct.LightPosition = DirectX::SimpleMath::Vector3(0.f, 0.f, 100.f);
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));
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;
m_DeviceContext->UpdateSubresource(constantBuffer, 0, 0, &constantBufferStruct, 0, 0);
m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
UINT stride = sizeof(Model::Vertex);
UINT offset = 0;
m_DeviceContext->IASetVertexBuffers(0, 1, &testModel->VertexBuffer, &stride, &offset);
m_DeviceContext->UpdateSubresource(constantBuffer, 0, 0, &constantBufferStruct, 0, 0);
m_DeviceContext->PSSetSamplers(0, 1, &m_SamplerState);
for (auto matGroup : testModel->TextureGroups) {
//m_DeviceContext->PSSetShaderResources(0, 1, &matGroup.Texture);
m_DeviceContext->Draw(matGroup.EndIndex - matGroup.StartIndex, matGroup.StartIndex);
m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
UINT stride = sizeof(Model::Vertex);
UINT offset = 0;
m_DeviceContext->IASetVertexBuffers(0, 1, &modelJob->Model->VertexBuffer, &stride, &offset);
m_DeviceContext->IASetIndexBuffer(modelJob->Model->IndexBuffer, DXGI_FORMAT_R32_UINT, 0);
if (modelJob->DiffuseTexture != nullptr) {
m_DeviceContext->PSSetShaderResources(0, 1, &modelJob->DiffuseTexture->m_ShaderResourceView);
}
//m_DeviceContext->Draw(modelJob->EndIndex - modelJob->StartIndex + 1, modelJob->StartIndex);
m_DeviceContext->DrawIndexed(modelJob->EndIndex - modelJob->StartIndex + 1, modelJob->StartIndex, 0);
continue;
}
}
//constantBufferStruct.ModelMatrix = GLMtoDX(glm::mat4(1.f));
//constantBufferStruct.ViewMatrix = GLMtoDX(m_Camera->ViewMatrix());
//constantBufferStruct.ProjectionMatrix = proj;
//constantBufferStruct.InverseTransposeViewModel = GLMtoDX(glm::mat4(1.f));
m_SwapChain->Present(0, 0);
//glfwSwapBuffers(m_Window);
}
//void dd::Renderer::Resize(int width, int height)
@@ -183,13 +225,25 @@ dd::Renderer::~Renderer()
m_DeviceContext->Release();
}
DirectX::SimpleMath::Matrix dd::Renderer::GLMtoDX(glm::mat4 gm)
DirectX::SimpleMath::Matrix dd::Renderer::GLMtoDXModelView(glm::mat4 gm)
{
gm = glm::transpose(gm);
DirectX::SimpleMath::Matrix dm(
gm[0][0], gm[0][1], -gm[0][2], gm[0][3],
gm[1][0], gm[1][1], -gm[1][2], gm[1][3],
-gm[2][0], -gm[2][1], gm[2][2], gm[2][3],
gm[3][0], gm[3][1], -gm[3][2], gm[3][3]
);
return dm;
}
DirectX::SimpleMath::Matrix dd::Renderer::GLMtoDXProjection(glm::mat4 gm)
{
gm = glm::transpose(gm);
DirectX::SimpleMath::Matrix dm(
gm[0][0], gm[0][1], gm[0][2], gm[0][3],
gm[1][0], gm[1][1], gm[1][2], gm[1][3],
gm[2][0], gm[2][1], gm[2][2], gm[2][3],
-gm[2][0], -gm[2][1], -gm[2][2], -gm[2][3],
gm[3][0], gm[3][1], gm[3][2], gm[3][3]
);
return dm;
+14 -2
View File
@@ -27,14 +27,26 @@ dd::ShaderProgram::~ShaderProgram()
void dd::ShaderProgram::CompileVertexShader(std::wstring filename)
{
//D3DX11CompileFromFile(filename.c_str(), 0, 0, "main", "vs_5_0", 0, 0, 0, &m_VertexShaderBlob, 0, 0);
D3DCompileFromFile(filename.c_str(), nullptr, nullptr, "main", "vs_5_0", 0, 0, &m_VertexShaderBlob, nullptr);
ID3DBlob* blob = nullptr;
HRESULT result = D3DCompileFromFile(filename.c_str(), nullptr, nullptr, "main", "vs_5_0", 0, 0, &m_VertexShaderBlob, nullptr);
if (result != S_OK) {
std::wstring blobString((wchar_t*)blob->GetBufferPointer(), blob->GetBufferSize());
LOG_ERROR("Failed to compile vertex shader!\n\n%s", blobString.c_str());
return;
}
StaticSystem::Get<Renderer>()->m_Device->CreateVertexShader(m_VertexShaderBlob->GetBufferPointer(), m_VertexShaderBlob->GetBufferSize(), NULL, &m_VertexShader);
}
void dd::ShaderProgram::CompilePixelShader(std::wstring filename)
{
//D3DX11CompileFromFile(filename.c_str(), 0, 0, "main", "ps_5_0", 0, 0, 0, &m_PixelShaderBlob, 0, 0);
D3DCompileFromFile(filename.c_str(), nullptr, nullptr, "main", "ps_5_0", 0, 0, &m_PixelShaderBlob, nullptr);
ID3DBlob* blob = nullptr;
HRESULT result = D3DCompileFromFile(filename.c_str(), nullptr, nullptr, "main", "ps_5_0", 0, 0, &m_PixelShaderBlob, &blob);
if (result != S_OK) {
std::wstring blobString((wchar_t*)blob->GetBufferPointer(), blob->GetBufferSize());
LOG_ERROR("Failed to compile pixel shader!\n\n%s", blobString.c_str());
return;
}
StaticSystem::Get<Renderer>()->m_Device->CreatePixelShader(m_PixelShaderBlob->GetBufferPointer(), m_PixelShaderBlob->GetBufferSize(), NULL, &m_PixelShader);
}
+40
View File
@@ -21,6 +21,46 @@
dd::Texture::Texture(std::string path)
{
std::unique_ptr<Image> image = std::make_unique<PNG>(path);
if (image->Width == 0 && image->Height == 0 || image->Format == Image::ImageFormat::Unknown) {
image = std::make_unique<PNG>("Textures/Core/ErrorTexture.png");
if (image->Width == 0 && image->Height == 0 || image->Format == Image::ImageFormat::Unknown) {
LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed.");
return;
}
}
this->Width = image->Width;
this->Height = image->Height;
D3D11_TEXTURE2D_DESC desc;
desc.Width = this->Width;
desc.Height = this->Height;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA data;
data.pSysMem = image->Data;
data.SysMemPitch = image->Width * 4;
data.SysMemSlicePitch = image->Width * image->Height * 4;
HRESULT result;
result = StaticSystem::Get<Renderer>()->m_Device->CreateTexture2D(&desc, &data, &m_Texture);
if (result != S_OK) {
LOG_ERROR("DirectX failed to create texture \"%s\"", path.c_str());
}
result = StaticSystem::Get<Renderer>()->m_Device->CreateShaderResourceView(m_Texture, nullptr, &m_ShaderResourceView);
if (result != S_OK) {
LOG_ERROR("DirectX failed to create shader resource view for texture \"%s\"", path.c_str());
}
}
dd::Texture::~Texture()