Danbias/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp

302 lines
7.3 KiB
C++
Raw Normal View History

2013-11-26 13:44:58 +01:00
#include "GFXAPI.h"
#include "../Core/Core.h"
2014-02-07 15:52:07 +01:00
#include "../Render/Resources.h"
#include "../Render/DefaultRenderer.h"
2013-11-26 15:33:05 +01:00
#include "../FileLoader/ObjReader.h"
2014-02-17 10:38:11 +01:00
#include "Resource/ResourceManager.h"
#include "../FileLoader/GeneralLoader.h"
#include "../Model/ModelInfo.h"
2014-02-07 15:52:07 +01:00
#include "../Render/GuiRenderer.h"
2014-01-08 07:01:59 +01:00
#include <vld.h>
2013-11-26 13:44:58 +01:00
namespace Oyster
{
namespace Graphics
{
namespace
{
Math::Float4x4 View;
Math::Float4x4 Projection;
std::vector<Definitions::Pointlight> Lights;
2014-02-10 13:59:45 +01:00
float deltaTime;
#ifdef _DEBUG
Model::Model* cube;
Model::Model* sphere;
2014-02-18 09:56:28 +01:00
ID3D11RasterizerState* wire;
2014-02-18 15:20:53 +01:00
ID3D11ShaderResourceView* debugSRV;
#endif
}
API::State API::Init(HWND Window, bool MSAA_Quality, bool Fullscreen, API::Option o)
2013-11-26 13:44:58 +01:00
{
Core::resolution = o.Resolution;
Core::modelPath = o.modelPath;
Core::texturePath = o.texturePath;
2013-11-26 15:33:05 +01:00
2013-11-26 13:44:58 +01:00
if(Core::Init::FullInit(Window, MSAA_Quality, Fullscreen) == Core::Init::Fail)
{
return API::Fail;
}
2014-02-11 13:29:19 +01:00
Render::Resources::Gui::Text::Font = (ID3D11ShaderResourceView*)API::CreateTexture(L"font_generic.png");
2014-02-07 15:52:07 +01:00
Render::Resources::Init();
2013-11-26 15:33:05 +01:00
Definitions::PostData pd;
pd.Amb = o.AmbientValue;
void* data = Render::Resources::Post::Data.Map();
memcpy(data,&pd,sizeof(Definitions::PostData));
Render::Resources::Post::Data.Unmap();
2013-11-26 15:33:05 +01:00
Render::Preparations::Basic::SetViewPort();
#ifdef _DEBUG
//fix load model
2014-02-18 15:20:53 +01:00
debugSRV = (ID3D11ShaderResourceView*)API::CreateTexture(L"color_white.png");
debugSRV = (ID3D11ShaderResourceView*)API::CreateTexture(L"color_white.png");
cube = CreateModel(L"generic_cube.dan");
cube->Tint = Math::Float3(0.0f,0.0f,1.0f);
sphere = CreateModel(L"generic_sphere.dan");
sphere->Tint = Math::Float3(1.0f,0.5f,182/255.0f);
2014-02-18 15:20:53 +01:00
2014-02-18 09:56:28 +01:00
D3D11_RASTERIZER_DESC desc;
desc.CullMode = D3D11_CULL_BACK;
desc.FillMode = D3D11_FILL_WIREFRAME;
desc.FrontCounterClockwise = false;
desc.DepthBias = 0;
desc.DepthBiasClamp = 0;
desc.DepthClipEnable = true;
desc.SlopeScaledDepthBias = 0;
desc.ScissorEnable = false;
desc.MultisampleEnable = false;
desc.AntialiasedLineEnable = false;
Core::device->CreateRasterizerState(&desc,&wire);
#endif
2013-11-26 13:44:58 +01:00
return API::Sucsess;
}
2014-02-10 16:22:23 +01:00
void API::SetProjection(const Math::Float4x4& projection)
{
Projection = projection;
}
2014-02-10 16:22:23 +01:00
void API::SetView(const Math::Float4x4& view)
{
View = view;
}
void API::NewFrame()
2013-11-26 13:44:58 +01:00
{
if(Lights.size())
{
Render::DefaultRenderer::NewFrame(View, Projection, &Lights[0], (int)Lights.size());
}
else
{
Render::DefaultRenderer::NewFrame(View, Projection, NULL, 0);
}
2013-11-26 13:44:58 +01:00
}
void API::RenderScene(Model::Model models[], int count)
2013-11-26 13:44:58 +01:00
{
2014-02-10 13:59:45 +01:00
Render::DefaultRenderer::RenderScene(models,count, View, Projection, deltaTime);
2013-11-26 13:44:58 +01:00
}
2014-02-10 13:59:45 +01:00
void API::RenderModel(Model::Model* m)
{
2014-02-10 13:59:45 +01:00
Render::DefaultRenderer::RenderScene(m,1, View, Projection, deltaTime);
}
2013-11-26 15:33:05 +01:00
void API::EndFrame()
{
2014-02-07 15:52:07 +01:00
Render::DefaultRenderer::EndFrame();
2013-11-26 15:33:05 +01:00
}
API::State API::SetOptions(API::Option option)
{
2014-01-31 16:29:50 +01:00
Core::modelPath = option.modelPath;
Core::texturePath = option.texturePath;
Definitions::PostData pd;
pd.Amb = option.AmbientValue;
void* data = Render::Resources::Post::Data.Map();
memcpy(data,&pd,sizeof(Definitions::PostData));
Render::Resources::Post::Data.Unmap();
2013-11-26 15:33:05 +01:00
return API::Sucsess;
}
//returns null for invalid filenames
2013-11-26 15:33:05 +01:00
Model::Model* API::CreateModel(std::wstring filename)
{
Model::Model* m = new Model::Model();
m->WorldMatrix = Oyster::Math::Float4x4::identity;
m->Visible = true;
2014-02-10 13:59:45 +01:00
m->Animation.AnimationPlaying = NULL;
2014-02-12 16:21:46 +01:00
m->Tint = Math::Float3(1);
2014-02-20 12:07:53 +01:00
m->GlowTint = Math::Float3(1);
2014-01-31 16:29:50 +01:00
m->info = (Model::ModelInfo*)Core::loader.LoadResource((Core::modelPath + filename).c_str(),Oyster::Graphics::Loading::LoadDAN, Oyster::Graphics::Loading::UnloadDAN);
2013-11-26 15:33:05 +01:00
Model::ModelInfo* mi = (Model::ModelInfo*)m->info;
2014-01-20 16:36:25 +01:00
if(!mi || mi->Vertices->GetBufferPointer() == NULL)
{
delete m;
2014-02-14 14:27:38 +01:00
Core::loader.ReleaseResource(mi);
delete mi;
return NULL;
}
2013-11-26 15:33:05 +01:00
return m;
}
void API::DeleteModel(Model::Model* model)
{
if(model==NULL)
return;
Model::ModelInfo* info = (Model::ModelInfo*)model->info;
2013-11-26 15:33:05 +01:00
delete model;
2014-01-31 16:29:50 +01:00
Core::loader.ReleaseResource(info);
}
void API::Clean()
{
2014-02-18 09:56:28 +01:00
#ifdef _DEBUG
DeleteModel(cube);
DeleteModel(sphere);
SAFE_RELEASE(wire);
#endif
2014-02-11 13:29:19 +01:00
DeleteTexture(Render::Resources::Gui::Text::Font);
SAFE_DELETE(Core::viewPort);
2014-01-31 16:29:50 +01:00
Core::loader.Clean();
Oyster::Graphics::Core::PipelineManager::Clean();
2014-02-07 15:52:07 +01:00
Oyster::Graphics::Render::Resources::Clean();
SAFE_RELEASE(Core::depthStencil);
SAFE_RELEASE(Core::depthStencilUAV);
SAFE_RELEASE(Core::backBufferRTV);
SAFE_RELEASE(Core::backBufferUAV);
SAFE_RELEASE(Core::swapChain);
SAFE_RELEASE(Core::deviceContext);
SAFE_RELEASE(Core::device);
2014-02-18 09:56:28 +01:00
2013-11-26 15:33:05 +01:00
}
void API::AddLight(Definitions::Pointlight light)
{
Lights.push_back(light);
}
void API::ClearLights()
{
Lights.clear();
}
2014-01-08 07:01:59 +01:00
#ifdef _DEBUG
API::State API::ReloadShaders()
{
2014-02-07 15:52:07 +01:00
Render::Resources::InitShaders();
2014-01-08 07:01:59 +01:00
return State::Sucsess;
}
void API::StartRenderWireFrame()
{
2014-02-19 15:57:52 +01:00
Core::deviceContext->OMSetRenderTargets((UINT)Render::Resources::Gather::Pass.RTV.size(),&Render::Resources::Gather::Pass.RTV[0],NULL);
2014-02-18 09:56:28 +01:00
Core::deviceContext->RSSetState(wire);
2014-02-19 15:57:52 +01:00
Core::deviceContext->OMSetRenderTargets((UINT)Render::Resources::Gather::Pass.RTV.size(),&Render::Resources::Gather::Pass.RTV[0],NULL);
2014-02-18 09:56:28 +01:00
}
void API::RenderDebugCube(Math::Matrix world)
{
cube->WorldMatrix = world;
Render::DefaultRenderer::RenderScene(cube,1,View,Projection);
}
void API::RenderDebugSphere(Math::Matrix world)
{
sphere->WorldMatrix = world;
Render::DefaultRenderer::RenderScene(sphere,1,View,Projection);
}
#else
API::State API::ReloadShaders()
{
2014-02-20 09:23:09 +01:00
return API::State::Sucsess;
}
void API::StartRenderWireFrame()
{
}
void API::RenderDebugCube(Math::Matrix world)
{
}
void API::RenderDebugSphere(Math::Matrix world)
{
}
2014-01-08 07:01:59 +01:00
#endif
2014-02-05 16:54:57 +01:00
API::Option API::GetOption()
{
Option o;
o.BytesUsed = Core::UsedMem;
o.modelPath = Core::modelPath;
o.texturePath = Core::texturePath;
o.Resolution = Core::resolution;
2014-02-05 16:54:57 +01:00
return o;
}
2014-02-07 11:52:51 +01:00
void API::StartGuiRender()
{
2014-02-11 13:29:19 +01:00
Render::Gui::Begin2DRender();
2014-02-07 11:52:51 +01:00
}
2014-02-17 13:05:35 +01:00
void API::RenderGuiElement(API::Texture tex, Math::Float3 pos, Math::Float2 size, Math::Float4 color)
2014-02-07 11:52:51 +01:00
{
2014-02-12 09:24:37 +01:00
Render::Gui::Render((ID3D11ShaderResourceView*)tex,pos,size,color);
2014-02-07 11:52:51 +01:00
}
2014-02-07 13:15:38 +01:00
API::Texture API::CreateTexture(std::wstring filename)
{
return Core::loader.LoadResource((Core::texturePath + filename).c_str(),Oyster::Graphics::Loading::LoadTexture, Oyster::Graphics::Loading::UnloadTexture);
}
void API::DeleteTexture(API::Texture tex)
{
Core::loader.ReleaseResource(tex);
}
float API::PlayAnimation(Model::Model* m, std::wstring name,bool looping)
{
2014-02-14 14:27:38 +01:00
if(m==NULL)
return 0;
2014-02-10 13:59:45 +01:00
m->Animation.AnimationPlaying = &(*m->info->Animations.find(name)).second;
m->Animation.AnimationTime=0;
m->Animation.LoopAnimation = looping;
return (float)m->Animation.AnimationPlaying->duration;
2014-02-10 13:59:45 +01:00
}
void API::Update(float dt)
{
deltaTime = dt;
}
2014-02-11 13:29:19 +01:00
void API::StartTextRender()
{
Render::Gui::Begin2DTextRender();
}
2014-02-17 13:05:35 +01:00
void API::RenderText(std::wstring text, Math::Float3 Pos, Math::Float2 Size, float FontSize, Math::Float4 color)
2014-02-11 13:29:19 +01:00
{
2014-02-13 16:27:53 +01:00
Render::Gui::RenderText(text, Pos, Size, FontSize, color);
2014-02-11 13:29:19 +01:00
}
2013-11-26 13:44:58 +01:00
}
}