Files
bluflame/aiwaz/Demo/main.cpp
2026-04-18 22:31:51 +02:00

296 lines
11 KiB
C++

#pragma comment(lib, "Aiwaz.lib")
#include "stdafx.h"
#include "../Aiwaz/Aiwaz.h"
#include "BLUImporter/BLUImporter.h"
#define WINDOW_TITLE "BluFlame - Aiwaz"
bool WINDOW_FULLSCREEN = false;
bool START_INTRO = true;
int WINDOW_WIDTH = 1024;
int WINDOW_HEIGHT = 768;
int _tmain(int argc, TCHAR* argv[])
{
MSG message;
LARGE_INTEGER oldTime;
LARGE_INTEGER ticksPerSecond;
bool done = false;
IEngine* engine = NULL;
IWindow* window = NULL;
float elapsedTime = 0.0f;
try
{
engine = Aiwaz::CreateEngine();
engine->Initialize();
IResourceFactory& resFactory = engine->get_ResourceFactory();
engine->get_FileSystem().InitializeFromDirectory(L"");
BluModel chamferBox = BluImporter::Load(*engine, L"Data/ChamferBox.blu");
chamferBox.RootTransformation->set_LocalScale(D3DXVECTOR3(0.1f, 0.1f, 0.1f));
chamferBox.RootTransformation->set_LocalPosition(D3DXVECTOR3(1.0f, 0.0f, 2.0f));
window = &resFactory.CreateSwapChainWindow();
window->Initialize(std::to_string16(WINDOW_TITLE), WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_FULLSCREEN);
window->set_VSync(false);
window->get_SwapChain().get_Base().set_ClearColor(0xFF00AAFF);
window->get_SwapChain().get_Base().set_HasDepthStencilBuffer(true);
IPerspectiveCamera& currentCamera = resFactory.CreateOrFindPerspectiveCamera();
currentCamera.get_Base().set_AspectRatio((float)WINDOW_WIDTH / WINDOW_HEIGHT);
IShader& normalShader = resFactory.CreateOrFindShader("NormalOutput");
normalShader.LoadFromFile(L"Data/NormalOutput.fxo");
normalShader.set_TechniqueName("NormalMapping");
IShader& diffuseShader = resFactory.CreateOrFindShader("DiffuseOutput");
diffuseShader.LoadFromFile(L"Data/NormalOutput.fxo");
IShader& lightShader = resFactory.CreateOrFindShader("LightShader");
lightShader.LoadFromFile(L"Data/NormalOutput.fxo");
lightShader.set_TechniqueName("Light");
ITexture& wallTexture = resFactory.CreateOrFindTexture("Wall");
wallTexture.LoadFromFile(L"Data/Wall.jpg");
ITexture& wallNormalTexture = resFactory.CreateOrFindTexture("WallNormal");
wallNormalTexture.LoadFromFile(L"Data/WallNormal.jpg");
wallNormalTexture.set_BindingName("Normal");
ITexture& wall2Texture = resFactory.CreateOrFindTexture("Wall2");
wall2Texture.LoadFromFile(L"Data/Wall2.jpg");
ITexture& wall2NormalTexture = resFactory.CreateOrFindTexture("Wall2Normal");
wall2NormalTexture.LoadFromFile(L"Data/Wall2Normal.jpg");
wall2NormalTexture.set_BindingName("Normal");
IGeometryBuffer& cubeBuffer = *engine->get_CommonObjectFactory().CreateCube(1.0f, 1.0f, 1.0f, false, "Cube");
cubeBuffer.ConvertToAdjacency();
IGeometryBuffer& roomBuffer = *engine->get_CommonObjectFactory().CreateCube(10.0f, 10.0f, 10.0f, true, "RoomCube");
IGeometryBuffer& lightCubeBuffer = *engine->get_CommonObjectFactory().CreateCube(0.1f, 0.1f, 0.1f, false, "LightCube");
ITransformation& cubeTransformation = resFactory.CreateOrFindTransformation("CubeTransformation");
cubeTransformation.set_TransformationBindings(TransformationBindings::CreateDefault());
cubeTransformation.set_LocalPosition(D3DXVECTOR3(-1.0f, 0.0f, 2.0f));
ITransformation& roomTransformation = resFactory.CreateOrFindTransformation("RoomTransformation");
roomTransformation.set_TransformationBindings(TransformationBindings::CreateDefault());
roomTransformation.set_LocalPosition(D3DXVECTOR3(0.0f, 0.0f, 2.0f));
ITransformation& lightCubeTransformation = resFactory.CreateOrFindTransformation("LightCubeTransformation");
lightCubeTransformation.set_TransformationBindings(TransformationBindings::CreateDefault());
lightCubeTransformation.set_LocalPosition(D3DXVECTOR3(0.0f, 0.0f, 2.0f));
IShaderParameterCollection& lightParameters = resFactory.CreateOrFindShaderParameterCollection("Light");
D3DXVECTOR3 lightPosition = D3DXVECTOR3(-1.0f, 1.0f, -1.0f);
lightPosition = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXVECTOR4 lightColor = D3DXVECTOR4(2.0f, 2.0f, 1.0f, 1.0f);
lightParameters.set_IsPreconditionForFollowingShaders(true);
lightParameters.SetParameter("LightPosition", &lightPosition);
lightParameters.SetParameter("LightColor", &lightColor);
IRenderTargetTexture& colorMap = resFactory.CreateOrFindRenderTargetTexture();
colorMap.SetTextureParameters(
window->get_SwapChain().get_Base().get_RenderTargetWidth(),
window->get_SwapChain().get_Base().get_RenderTargetHeight(),
window->get_SwapChain().get_Base().get_RenderTargetFormat());
colorMap.get_TextureResource().set_BindingName("ColorMap");
colorMap.get_Base().set_ClearColor(D3DXCOLOR(0.0f, 0.2f, 0.0f, 1.0f));
colorMap.get_Base().set_HasDepthStencilBuffer(true);
IShader& shadowReceiverShader = resFactory.CreateOrFindShader();
shadowReceiverShader.LoadFromFile(L"Data/ShadowVolume.fxo");
shadowReceiverShader.set_TechniqueName("ReceiveShadows");
shadowReceiverShader.set_Priority(2);
IShader& shadowCasterShader = resFactory.CreateOrFindShader();
shadowCasterShader.LoadFromFile(L"Data/ShadowVolume.fxo");
shadowCasterShader.set_TechniqueName("CastShadows");
shadowCasterShader.set_Priority(1);
// PingPong stuff
PingPongBufferDescription pingPongDescriptor;
pingPongDescriptor.ShaderFileName = L"Data/PostProcess.fxo";
pingPongDescriptor.ShaderTechniqueA = "Blur";
pingPongDescriptor.ShaderTechniqueB = "Blur";
pingPongDescriptor.PreProcessShaderTechnique = "ShowTexture";
pingPongDescriptor.LoopCount = 4;
pingPongDescriptor.TextureWidth = window->get_SwapChain().get_Base().get_RenderTargetWidth() / 4;
pingPongDescriptor.TextureHeight = window->get_SwapChain().get_Base().get_RenderTargetHeight() / 4;
pingPongDescriptor.TextureFormat = window->get_SwapChain().get_Base().get_RenderTargetFormat();
IPingPongBuffer* blurBuffer = engine->get_CommonObjectFactory().CreatePingPongBuffer(colorMap.get_TextureResource(), pingPongDescriptor);
IShader& composeShaderResource = resFactory.CreateOrFindShader();
composeShaderResource.LoadFromFile(L"Data/PostProcess.fxo");
composeShaderResource.set_TechniqueName("Compose");
IShaderParameterCollection& shaderParameterCollection = resFactory.CreateOrFindShaderParameterCollection();
shaderParameterCollection.SetParameter("Time", &elapsedTime);
ITexture& distortTexture = resFactory.CreateOrFindTexture();
distortTexture.LoadFromFile(L"Data/Distortion.jpg");
distortTexture.set_BindingName("DistortMap");
IGeometryBuffer& quadGeometry = *engine->get_CommonObjectFactory().CreateQuad(2.0f, 2.0f, false);
IRenderCommandNode& rootNode = resFactory.CreateRenderCommandNode();
IRenderCommandNode& colorNode = resFactory.CreateRenderCommandNode();
IRenderCommandNode& intensityNode = resFactory.CreateRenderCommandNode();
IRenderCommandNode& composingNode = resFactory.CreateRenderCommandNode();
IRenderCommandNode& cubeNode = resFactory.CreateRenderCommandNode();
IRenderCommandNode& roomNode = resFactory.CreateRenderCommandNode();
IRenderCommandNode& roomShadowReceiverNode = resFactory.CreateRenderCommandNode();
IRenderCommandNode& lightNode = resFactory.CreateRenderCommandNode();
{
// create light node
AddToNode(lightNode, lightShader);
AddToNode(lightNode, lightCubeTransformation);
AddToNode(lightNode, lightCubeBuffer);
// create cube node
AddToNode(cubeNode, normalShader);
AddToNode(cubeNode, wallTexture);
AddToNode(cubeNode, wallNormalTexture);
AddToNode(cubeNode, cubeTransformation);
AddToNode(cubeNode, cubeBuffer);
// render shadow caster
AddToNode(cubeNode, shadowCasterShader);
AddToNode(cubeNode, cubeTransformation);
AddToNode(cubeNode, cubeBuffer);
AddToNode(roomNode, normalShader);
AddToNode(roomNode, wall2Texture);
AddToNode(roomNode, wall2NormalTexture);
AddToNode(roomNode, roomTransformation);
AddToNode(roomNode, roomBuffer);
// render shadow receiver
AddToNode(roomShadowReceiverNode, shadowReceiverShader);
AddToNode(roomShadowReceiverNode, roomTransformation);
AddToNode(roomShadowReceiverNode, roomBuffer);
// render colored scene to texture
AddToNode(rootNode, colorNode);
AddToNode(colorNode, colorMap);
AddToNode(colorNode, currentCamera);
AddToNode(colorNode, lightParameters);
AddToNode(colorNode, roomNode);
AddToNode(colorNode, chamferBox.RootNode);
AddToNode(colorNode, cubeNode);
AddToNode(colorNode, roomShadowReceiverNode);
AddToNode(colorNode, lightNode);
// create intensity map from colored scene
AddToNode(rootNode, intensityNode);
AddToNode(intensityNode, blurBuffer->get_RootNode());
// compose final scene
AddToNode(rootNode, composingNode);
AddToNode(composingNode, window->get_SwapChain());
AddToNode(composingNode, currentCamera);
AddToNode(composingNode, composeShaderResource);
AddToNode(composingNode, shaderParameterCollection);
AddToNode(composingNode, distortTexture);
AddToNode(composingNode, colorMap.get_TextureResource());
AddToNode(composingNode, blurBuffer->get_OutputTexture());
AddToNode(composingNode, quadGeometry);
}
IUpdatable& updatableRootNode = dynamic_cast<IUpdatable&>(rootNode);
QueryPerformanceCounter(&oldTime);
float refreshTime = 0.0f;
while (!done)
{
if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
if (message.message == WM_QUIT)
{
done = TRUE;
}
else
{
TranslateMessage(&message);
DispatchMessage(&message);
}
}
else
{
if (window->get_Active())
{
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
QueryPerformanceFrequency(&ticksPerSecond);
time.QuadPart -= oldTime.QuadPart;
double timeValue = double(time.QuadPart) / (ticksPerSecond.QuadPart);
QueryPerformanceCounter(&oldTime);
elapsedTime += (float)timeValue;
D3DXVECTOR3 rotationYPR = chamferBox.RootTransformation->get_LocalRotationYPR();
const float timefactor = 0.4f;
rotationYPR.x += (float)timeValue * timefactor;
rotationYPR.y += (float)timeValue * timefactor;
rotationYPR.z += (float)timeValue * timefactor;
chamferBox.RootTransformation->set_LocalRotationYPR(rotationYPR);
rotationYPR = cubeTransformation.get_LocalRotationYPR();
const float timeCubefactor = 0.2f;
rotationYPR.x += (float)timeValue * timeCubefactor;
rotationYPR.y += (float)timeValue * timeCubefactor;
rotationYPR.z += (float)timeValue * timeCubefactor;
cubeTransformation.set_LocalRotationYPR(rotationYPR);
rotationYPR = roomTransformation.get_LocalRotationYPR();
const float timeRoomFactor = 0.05f;
rotationYPR.x += (float)timeValue * timeRoomFactor;
rotationYPR.y += (float)timeValue * timeRoomFactor;
rotationYPR.z += (float)timeValue * timeRoomFactor;
roomTransformation.set_LocalRotationYPR(rotationYPR);
//lightPosition.x = sin(elapsedTime * 0.5) * 2.0f;
lightPosition.y = cos(elapsedTime * 0.5f) * 2.0f;
lightCubeTransformation.set_LocalPosition(lightPosition);
//#ifdef _DEBUG
{
refreshTime -= (float)timeValue;
if (refreshTime < 0.0f)
{
refreshTime = 0.5f;
char title[255];
sprintf_s(title, 255, "%s - %.4f ms %.2f FPS", WINDOW_TITLE, float(timeValue * 1000), float(1.0 / timeValue));
window->set_Title(std::to_string16(title));
}
}
//#endif
updatableRootNode.Update(false);
rootNode.ProcessCommands();
window->SwapBuffers();
}
}
}
}
catch (LPCTSTR exception)
{
HWND windowHandle = 0x0;
if (window != NULL)
windowHandle = window->get_Handle();
::MessageBox(windowHandle, exception, _T("BluFlame demo system"), MB_OK);
::ExitProcess(0);
}
delete engine;
return 0;
}