Successful DirectX context through GLFW, praise Gaben!
This commit is contained in:
@@ -104,7 +104,7 @@ set(SOURCE_FILES
|
||||
${SOURCE_FILES_Core_Util}
|
||||
${SOURCE_FILES_Input}
|
||||
${SOURCE_FILES_Rendering}
|
||||
${SOURCE_FILES_Rendering_OpenGL}
|
||||
#${SOURCE_FILES_Rendering_OpenGL}
|
||||
${SOURCE_FILES_Rendering_DirectX}
|
||||
${SOURCE_FILES_Transform}
|
||||
${SOURCE_FILES_Physics}
|
||||
@@ -131,6 +131,7 @@ target_link_libraries(game
|
||||
${X11_LIBRARIES}
|
||||
${OPENAL_LIBRARY}
|
||||
${liquidfun_LIBRARIES}
|
||||
${CMAKE_LIBRARY_PATH}/DirectXTK.lib
|
||||
)
|
||||
|
||||
add_executable(breakout
|
||||
@@ -147,5 +148,6 @@ target_link_libraries(breakout
|
||||
${X11_LIBRARIES}
|
||||
${OPENAL_LIBRARY}
|
||||
${liquidfun_LIBRARIES}
|
||||
${CMAKE_LIBRARY_PATH}/DirectXTK.lib
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Core/StaticSystem.h"
|
||||
|
||||
std::unordered_map<size_t, void*> dd::StaticSystem::Instances;
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Rendering/Model.h"
|
||||
|
||||
dd::Model::Model(std::string fileName)
|
||||
: dd::RawModel(fileName)
|
||||
{
|
||||
D3D11_BUFFER_DESC vbd = { 0 };
|
||||
vbd.ByteWidth = sizeof(RawModel::Vertex) * m_Vertices.size();
|
||||
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
||||
D3D11_SUBRESOURCE_DATA vsrd = { m_Vertices.data(), 0, 0 };
|
||||
StaticSystem::Get<Renderer>()->m_Device->CreateBuffer(&vbd, &vsrd, &VertexBuffer);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Rendering/Renderer.h"
|
||||
#include "Rendering/ShaderProgram.h"
|
||||
|
||||
void dd::Renderer::Initialize()
|
||||
{
|
||||
StaticSystem::Set(this);
|
||||
|
||||
// Initialize GLFW
|
||||
if (!glfwInit()) {
|
||||
LOG_ERROR("GLFW: Initialization failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Create a window
|
||||
GLFWmonitor* monitor = nullptr;
|
||||
if (m_Fullscreen) {
|
||||
monitor = glfwGetPrimaryMonitor();
|
||||
}
|
||||
glfwWindowHint(GLFW_SAMPLES, 8);
|
||||
m_Window = glfwCreateWindow(m_Resolution.Width, m_Resolution.Height, "daydream", monitor, nullptr);
|
||||
if (!m_Window) {
|
||||
LOG_ERROR("GLFW: Failed to create window");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
//glfwMakeContextCurrent(m_Window);
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC scd = { 0 };
|
||||
scd.BufferCount = 1;
|
||||
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
scd.OutputWindow = glfwGetWin32Window(m_Window);
|
||||
scd.SampleDesc.Count = 4;
|
||||
scd.Windowed = TRUE;
|
||||
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
|
||||
|
||||
D3D11CreateDeviceAndSwapChain(
|
||||
NULL,
|
||||
D3D_DRIVER_TYPE_HARDWARE,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
D3D11_SDK_VERSION,
|
||||
&scd,
|
||||
&m_SwapChain,
|
||||
&m_Device,
|
||||
NULL,
|
||||
&m_DeviceContext);
|
||||
|
||||
// Set the back buffer render target
|
||||
ID3D11Texture2D* pBackBuffer;
|
||||
m_SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&pBackBuffer);
|
||||
m_Device->CreateRenderTargetView(pBackBuffer, NULL, &m_BackBuffer);
|
||||
pBackBuffer->Release();
|
||||
// Depth buffer
|
||||
D3D11_TEXTURE2D_DESC texd = { };
|
||||
texd.Width = m_Resolution.Width;
|
||||
texd.Height = m_Resolution.Height;
|
||||
texd.ArraySize = 1;
|
||||
texd.MipLevels = 1;
|
||||
texd.SampleDesc.Count = 4;
|
||||
texd.Format = DXGI_FORMAT_D32_FLOAT;
|
||||
texd.BindFlags = D3D11_BIND_DEPTH_STENCIL;
|
||||
ID3D11Texture2D* pDepthBuffer;
|
||||
m_Device->CreateTexture2D(&texd, NULL, &pDepthBuffer);
|
||||
D3D11_DEPTH_STENCIL_VIEW_DESC dsvd = { };
|
||||
dsvd.Format = DXGI_FORMAT_D32_FLOAT;
|
||||
dsvd.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
|
||||
m_Device->CreateDepthStencilView(pDepthBuffer, &dsvd, &m_DepthBuffer);
|
||||
pDepthBuffer->Release();
|
||||
// Set back and depth buffer
|
||||
m_DeviceContext->OMSetRenderTargets(1, &m_BackBuffer, m_DepthBuffer);
|
||||
|
||||
// Rasterizer settings
|
||||
D3D11_RASTERIZER_DESC rd;
|
||||
memset(&rd, 0, sizeof(D3D11_RASTERIZER_DESC));
|
||||
rd.AntialiasedLineEnable = false;
|
||||
rd.CullMode = D3D11_CULL_BACK;
|
||||
rd.DepthBias = 0;
|
||||
rd.DepthBiasClamp = 0.0f;
|
||||
rd.DepthClipEnable = true;
|
||||
rd.FillMode = D3D11_FILL_SOLID;
|
||||
rd.FrontCounterClockwise = true;
|
||||
rd.MultisampleEnable = false;
|
||||
rd.ScissorEnable = false;
|
||||
rd.SlopeScaledDepthBias = 0.0f;
|
||||
ID3D11RasterizerState* rasterState;
|
||||
m_Device->CreateRasterizerState(&rd, &rasterState);
|
||||
m_DeviceContext->RSSetState(rasterState);
|
||||
|
||||
// Set the viewport
|
||||
D3D11_VIEWPORT viewport = { 0 };
|
||||
viewport.TopLeftX = 0;
|
||||
viewport.TopLeftY = 0;
|
||||
viewport.Width = m_Resolution.Width;
|
||||
viewport.Height = m_Resolution.Height;
|
||||
viewport.MinDepth = 0;
|
||||
viewport.MaxDepth = 1;
|
||||
m_DeviceContext->RSSetViewports(1, &viewport);
|
||||
|
||||
|
||||
testModel = ResourceManager::Load<Model>("Models/Core/UnitSphere.obj");
|
||||
|
||||
shaderProgram = new ShaderProgram();
|
||||
shaderProgram->CompileVertexShader(L"vertex.hlsl");
|
||||
shaderProgram->CompilePixelShader(L"pixel.hlsl");
|
||||
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},
|
||||
};
|
||||
shaderProgram->CreateInputLayout(VertexIED, sizeof(VertexIED) / sizeof(VertexIED[0]));
|
||||
shaderProgram->Bind();
|
||||
|
||||
// Create constant buffer
|
||||
// TODO: Put this in shader
|
||||
D3D11_BUFFER_DESC bd = { };
|
||||
bd.ByteWidth = 64*6;
|
||||
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
m_Device->CreateBuffer(&bd, NULL, &constantBuffer);
|
||||
m_DeviceContext->VSSetConstantBuffers(0, 1, &constantBuffer);
|
||||
m_DeviceContext->PSSetConstantBuffers(0, 1, &constantBuffer);
|
||||
}
|
||||
|
||||
void dd::Renderer::Draw(RenderQueueCollection& rq)
|
||||
{
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
for (auto matGroup : testModel->TextureGroups) {
|
||||
//m_DeviceContext->PSSetShaderResources(0, 1, &matGroup.Texture);
|
||||
m_DeviceContext->Draw(matGroup.EndIndex - matGroup.StartIndex, matGroup.StartIndex);
|
||||
}
|
||||
|
||||
m_SwapChain->Present(0, 0);
|
||||
}
|
||||
|
||||
//void dd::Renderer::Resize(int width, int height)
|
||||
//{
|
||||
// m_DeviceContext->OMSetRenderTargets(0, 0, 0);
|
||||
// m_BackBuffer->Release();
|
||||
//
|
||||
// HRESULT hr; // TODO: Error handling
|
||||
// // Preserve the existing buffer count and format.
|
||||
// hr = m_SwapChain->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0);
|
||||
//
|
||||
// ID3D11Texture2D* pBuffer;
|
||||
// hr = m_SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&pBuffer);
|
||||
// hr = m_Device->CreateRenderTargetView(pBuffer, NULL, &m_BackBuffer);
|
||||
// pBuffer->Release();
|
||||
// m_DeviceContext->OMSetRenderTargets(1, &BackBuffer, NULL);
|
||||
//
|
||||
// // Set the viewport
|
||||
// D3D11_VIEWPORT viewport = { 0 };
|
||||
// viewport.TopLeftX = 0;
|
||||
// viewport.TopLeftY = 0;
|
||||
// viewport.Width = width;
|
||||
// viewport.Height = height;
|
||||
// m_DeviceContext->RSSetViewports(1, &viewport);
|
||||
//}
|
||||
|
||||
dd::Renderer::~Renderer()
|
||||
{
|
||||
m_SwapChain->Release();
|
||||
m_BackBuffer->Release();
|
||||
m_Device->Release();
|
||||
m_DeviceContext->Release();
|
||||
}
|
||||
|
||||
DirectX::SimpleMath::Matrix dd::Renderer::GLMtoDX(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;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Rendering/ShaderProgram.h"
|
||||
|
||||
dd::ShaderProgram::ShaderProgram()
|
||||
{
|
||||
m_VertexShaderBlob = nullptr;
|
||||
m_VertexShader = nullptr;
|
||||
m_PixelShaderBlob = nullptr;
|
||||
m_PixelShader = nullptr;
|
||||
m_InputLayout = nullptr;
|
||||
}
|
||||
|
||||
dd::ShaderProgram::~ShaderProgram()
|
||||
{
|
||||
if (m_VertexShader != nullptr)
|
||||
m_VertexShader->Release();
|
||||
if (m_VertexShaderBlob != nullptr)
|
||||
m_VertexShaderBlob->Release();
|
||||
if (m_PixelShader != nullptr)
|
||||
m_PixelShader->Release();
|
||||
if (m_PixelShaderBlob != nullptr)
|
||||
m_PixelShaderBlob->Release();
|
||||
if (m_InputLayout != nullptr)
|
||||
m_InputLayout->Release();
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
StaticSystem::Get<Renderer>()->m_Device->CreatePixelShader(m_PixelShaderBlob->GetBufferPointer(), m_PixelShaderBlob->GetBufferSize(), NULL, &m_PixelShader);
|
||||
}
|
||||
|
||||
void dd::ShaderProgram::Bind()
|
||||
{
|
||||
if (m_VertexShader != nullptr)
|
||||
StaticSystem::Get<Renderer>()->m_DeviceContext->VSSetShader(m_VertexShader, 0, 0);
|
||||
if (m_PixelShader != nullptr)
|
||||
StaticSystem::Get<Renderer>()->m_DeviceContext->PSSetShader(m_PixelShader, 0, 0);
|
||||
if (m_InputLayout != nullptr)
|
||||
StaticSystem::Get<Renderer>()->m_DeviceContext->IASetInputLayout(m_InputLayout);
|
||||
}
|
||||
|
||||
void dd::ShaderProgram::Unbind()
|
||||
{
|
||||
if (m_VertexShader != nullptr) {
|
||||
StaticSystem::Get<Renderer>()->m_DeviceContext->VSSetShader(NULL, 0, 0);
|
||||
}
|
||||
if (m_PixelShader != nullptr) {
|
||||
StaticSystem::Get<Renderer>()->m_DeviceContext->PSSetShader(NULL, 0, 0);
|
||||
}
|
||||
//if (m_InputLayout != nullptr)
|
||||
// dd::RENDERER->DeviceContext->IASetInputLayout(NULL);
|
||||
}
|
||||
|
||||
HRESULT dd::ShaderProgram::CreateInputLayout(D3D11_INPUT_ELEMENT_DESC* ied, UINT numElements)
|
||||
{
|
||||
return StaticSystem::Get<Renderer>()->m_Device->CreateInputLayout(ied, numElements, m_VertexShaderBlob->GetBufferPointer(), m_VertexShaderBlob->GetBufferSize(), &m_InputLayout);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Rendering/Texture.h"
|
||||
|
||||
dd::Texture::Texture(std::string path)
|
||||
{
|
||||
}
|
||||
|
||||
dd::Texture::~Texture()
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user