| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- #include "stdafx.h"
- #include <windows.h>
- #include "CmApplication.h"
- #include "CmDynLibManager.h"
- #include "CmGameObject.h"
- #include "CmCamera.h"
- #include "CmHighLevelGpuProgramManager.h"
- #include "CmRenderSystem.h"
- #include "CmRenderWindow.h"
- #include "CmResources.h"
- #include "CmRenderable.h"
- #include "CmMaterial.h"
- #include "CmShader.h"
- #include "CmTechnique.h"
- #include "CmPass.h"
- #include "CmImporter.h"
- #include "CmMesh.h"
- #include "CmGpuProgInclude.h" // DEBUG ONLY
- #include "CmGpuProgramImportOptions.h"
- #include "CmFontImportOptions.h"
- #include "CmCommandQueue.h"
- #include "CmBlendState.h"
- #include "CmDebugCamera.h"
- #include "CmTestTextSprite.h"
- #define DX11
- //#define DX9
- //#define GL
- using namespace CamelotEngine;
- MaterialHandle createTextMaterial()
- {
- #if defined DX9
- // TODO
- #elif defined DX11
- String textShader_dx11psLoc = "C:\\Projects\\CamelotEngine\\Data\\textShader_hlsl11_ps.gpuprog";
- String textShader_dx11vsLoc = "C:\\Projects\\CamelotEngine\\Data\\textShader_hlsl11_vs.gpuprog";
- ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(textShader_dx11psLoc);
- if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
- {
- GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
- importOptions->setEntryPoint("ps_main");
- importOptions->setLanguage("hlsl");
- importOptions->setProfile(GPP_PS_4_0);
- importOptions->setType(GPT_FRAGMENT_PROGRAM);
- }
- HighLevelGpuProgramHandle textShaderFragProgRef = Importer::instance().import(textShader_dx11psLoc, gpuProgImportOptions);
- gpuProgImportOptions = Importer::instance().createImportOptions(textShader_dx11vsLoc);
- if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
- {
- GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
- importOptions->setEntryPoint("vs_main");
- importOptions->setLanguage("hlsl");
- importOptions->setProfile(GPP_VS_4_0);
- importOptions->setType(GPT_VERTEX_PROGRAM);
- }
- HighLevelGpuProgramHandle textShaderVertProgRef = Importer::instance().import(textShader_dx11vsLoc, gpuProgImportOptions);
- #else
- // TODO
- #endif
- ShaderPtr textShader = Shader::create("TextShader");
- textShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
- textShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
- //TechniquePtr newTechniqueGL = textShader->addTechnique("GLRenderSystem", "ForwardRenderer");
- //PassPtr newPassGL = newTechniqueGL->addPass();
- //newPassGL->setVertexProgram(textShaderVertProgRef);
- //newPassGL->setFragmentProgram(textShaderFragProgRef);
- //TechniquePtr newTechniqueDX = textShader->addTechnique("D3D9RenderSystem", "ForwardRenderer");
- //PassPtr newPassDX = newTechniqueDX->addPass();
- //newPassDX->setVertexProgram(textShaderVertProgRef);
- //newPassDX->setFragmentProgram(textShaderFragProgRef);
- TechniquePtr newTechniqueDX11 = textShader->addTechnique("D3D11RenderSystem", "ForwardRenderer");
- PassPtr newPassDX11 = newTechniqueDX11->addPass();
- newPassDX11->setVertexProgram(textShaderVertProgRef);
- newPassDX11->setFragmentProgram(textShaderFragProgRef);
- BLEND_STATE_DESC desc;
- desc.renderTargetDesc[0].blendEnable = true;
- desc.renderTargetDesc[0].srcBlend = BF_SOURCE_ALPHA;
- desc.renderTargetDesc[0].dstBlend = BF_INV_SOURCE_ALPHA;
- desc.renderTargetDesc[0].blendOp = BO_ADD;
- BlendStateHandle blendState = BlendState::create(desc);
- newPassDX11->setBlendState(blendState);
- MaterialHandle textMaterial = Material::create();
- textMaterial->setShader(textShader);
- return textMaterial;
- }
- int CALLBACK WinMain(
- _In_ HINSTANCE hInstance,
- _In_ HINSTANCE hPrevInstance,
- _In_ LPSTR lpCmdLine,
- _In_ int nCmdShow
- )
- {
- #ifdef DX11
- gApplication().startUp("CamelotD3D11RenderSystem", "CamelotForwardRenderer");
- #elif defined DX9
- gApplication().startUp("CamelotD3D9RenderSystem", "CamelotForwardRenderer");
- #else
- gApplication().startUp("CamelotGLRenderSystem", "CamelotForwardRenderer");
- #endif
- //CommandQueue::addBreakpoint(1, 11);
- RenderSystem* renderSystem = RenderSystem::instancePtr();
- RenderWindowPtr renderWindow = gApplication().getPrimaryRenderWindow();
- GameObjectPtr cameraGO = GameObject::create("MainCamera");
- CameraPtr camera = cameraGO->addComponent<Camera>();
- camera->init(renderWindow, 0.0f, 0.0f, 1.0f, 1.0f, 0);
- cameraGO->setPosition(Vector3(0,50,1240));
- cameraGO->lookAt(Vector3(0,50,-300));
- camera->setNearClipDistance(5);
- camera->setAspectRatio(800.0f / 600.0f);
- std::shared_ptr<DebugCamera> debugCamera = cameraGO->addComponent<DebugCamera>();
- GameObjectPtr testModelGO = GameObject::create("TestMesh");
- RenderablePtr testRenderable = testModelGO->addComponent<Renderable>();
- GameObjectPtr testTextGO = GameObject::create("TestText");
- std::shared_ptr<TestTextSprite> textSprite = testTextGO->addComponent<TestTextSprite>();
- // Debug test fonts
- ImportOptionsPtr fontImportOptions = Importer::instance().createImportOptions("C:\\arial.ttf");
- if(rtti_is_of_type<FontImportOptions>(fontImportOptions))
- {
- FontImportOptions* importOptions = static_cast<FontImportOptions*>(fontImportOptions.get());
- vector<CamelotEngine::UINT32>::type fontSizes;
- fontSizes.push_back(12);
- importOptions->setFontSizes(fontSizes);
- }
- FontHandle font = Importer::instance().import("C:\\arial.ttf", fontImportOptions);
- MaterialHandle textMaterial = createTextMaterial();
- textSprite->setText("TESTfAV", font, 12, textMaterial);
- #if defined DX9
- ///////////////// HLSL 9 SHADERS //////////////////////////
- String dx9psLoc = "C:\\Projects\\CamelotEngine\\Data\\hlsl9_ps.gpuprog";
- String dx9vsLoc = "C:\\Projects\\CamelotEngine\\Data\\hlsl9_vs.gpuprog";
- ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(dx9psLoc);
- if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
- {
- GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
- importOptions->setEntryPoint("ps_main");
- importOptions->setLanguage("hlsl");
- importOptions->setProfile(GPP_PS_2_0);
- importOptions->setType(GPT_FRAGMENT_PROGRAM);
- }
- HighLevelGpuProgramHandle fragProgRef = Importer::instance().import(dx9psLoc, gpuProgImportOptions);
- gpuProgImportOptions = Importer::instance().createImportOptions(dx9vsLoc);
- if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
- {
- GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
- importOptions->setEntryPoint("vs_main");
- importOptions->setLanguage("hlsl");
- importOptions->setProfile(GPP_VS_2_0);
- importOptions->setType(GPT_VERTEX_PROGRAM);
- }
- HighLevelGpuProgramHandle vertProgRef = Importer::instance().import(dx9vsLoc, gpuProgImportOptions);
- #elif defined DX11
- GpuProgIncludeHandle gpuProgInclude = Importer::instance().import("C:\\testInclude.gpuproginc");
- const String& debugString = gpuProgInclude->getString();
-
- /////////////////// HLSL 11 SHADERS //////////////////////////
- String dx11psLoc = "C:\\Projects\\CamelotEngine\\Data\\hlsl11_ps.gpuprog";
- String dx11vsLoc = "C:\\Projects\\CamelotEngine\\Data\\hlsl11_vs.gpuprog";
- ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(dx11psLoc);
- if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
- {
- GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
- importOptions->setEntryPoint("ps_main");
- importOptions->setLanguage("hlsl");
- importOptions->setProfile(GPP_PS_4_0);
- importOptions->setType(GPT_FRAGMENT_PROGRAM);
- }
- HighLevelGpuProgramHandle fragProgRef = Importer::instance().import(dx11psLoc, gpuProgImportOptions);
- gpuProgImportOptions = Importer::instance().createImportOptions(dx11vsLoc);
- if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
- {
- GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
- importOptions->setEntryPoint("vs_main");
- importOptions->setLanguage("hlsl");
- importOptions->setProfile(GPP_VS_4_0);
- importOptions->setType(GPT_VERTEX_PROGRAM);
- }
- HighLevelGpuProgramHandle vertProgRef = Importer::instance().import(dx11vsLoc, gpuProgImportOptions);
-
- #else
- ///////////////// GLSL SHADERS ////////////////////////////
- String glslpsLoc = "C:\\Projects\\CamelotEngine\\Data\\glsl_ps.gpuprog";
- String glslvsLoc = "C:\\Projects\\CamelotEngine\\Data\\glsl_vs.gpuprog";
- ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(glslpsLoc);
- if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
- {
- GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
- importOptions->setEntryPoint("main");
- importOptions->setLanguage("glsl");
- importOptions->setProfile(GPP_PS_2_0);
- importOptions->setType(GPT_FRAGMENT_PROGRAM);
- }
- HighLevelGpuProgramHandle fragProgRef = Importer::instance().import(glslpsLoc, gpuProgImportOptions);
- gpuProgImportOptions = Importer::instance().createImportOptions(glslvsLoc);
- if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
- {
- GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
- importOptions->setEntryPoint("main");
- importOptions->setLanguage("glsl");
- importOptions->setProfile(GPP_VS_2_0);
- importOptions->setType(GPT_VERTEX_PROGRAM);
- }
- HighLevelGpuProgramHandle vertProgRef = Importer::instance().import(glslvsLoc, gpuProgImportOptions);
- #endif
- gResources().create(vertProgRef, "C:\\vertProgCg.vprog", true);
- gResources().unload(vertProgRef);
- vertProgRef = gResources().load("C:\\vertProgCg.vprog");
- gResources().create(fragProgRef, "C:\\fragProgCg.vprog", true);
- gResources().unload(fragProgRef);
- fragProgRef = gResources().load("C:\\fragProgCg.vprog");
- ShaderPtr testShader = Shader::create("TestShader");
- testShader->addParameter("matViewProjection", "matViewProjection", GPDT_MATRIX_4X4);
- #if defined DX11
- testShader->addParameter("input", "input", GPDT_STRUCT, 2, 8);
- #endif
- testShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
- testShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
- TechniquePtr newTechniqueGL = testShader->addTechnique("GLRenderSystem", "ForwardRenderer");
- PassPtr newPassGL = newTechniqueGL->addPass();
- newPassGL->setVertexProgram(vertProgRef);
- newPassGL->setFragmentProgram(fragProgRef);
- // TODO - I need to create different techniques for different render systems (and renderers, if there were any),
- // which is redundant as some techniques can be reused. I should add a functionality that supports multiple
- // render systems/renderers per technique
- TechniquePtr newTechniqueDX = testShader->addTechnique("D3D9RenderSystem", "ForwardRenderer");
- PassPtr newPassDX = newTechniqueDX->addPass();
- newPassDX->setVertexProgram(vertProgRef);
- newPassDX->setFragmentProgram(fragProgRef);
- TechniquePtr newTechniqueDX11 = testShader->addTechnique("D3D11RenderSystem", "ForwardRenderer");
- PassPtr newPassDX11 = newTechniqueDX11->addPass();
- newPassDX11->setVertexProgram(vertProgRef);
- newPassDX11->setFragmentProgram(fragProgRef);
- MaterialHandle testMaterial = Material::create();
- testMaterial->setShader(testShader);
- testMaterial->setMat4("matViewProjection", Matrix4::IDENTITY);
- #if defined DX11
- float dbgMultipliers1[2];
- dbgMultipliers1[0] = 0.0f;
- dbgMultipliers1[1] = 0.0f;
- float dbgMultipliers2[2];
- dbgMultipliers2[0] = 1.0f;
- dbgMultipliers2[1] = 1.0f;
- testMaterial->setStructData("input", dbgMultipliers1, sizeof(dbgMultipliers1), 0);
- testMaterial->setStructData("input", dbgMultipliers2, sizeof(dbgMultipliers2), 1);
- #endif
- //testMaterialRef = gResources().load("C:\\testMaterial.mat");
- //testMaterialRef.waitUntilLoaded();
- /*TextureRef testTex = static_resource_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));*/
- TextureHandle testTexRef = static_resource_cast<Texture>(Importer::instance().import("C:\\ArenaTowerDFS.psd"));
- MeshHandle dbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import("C:\\X_Arena_Tower.FBX"));
- //int tmpFlag = _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
- gResources().create(testTexRef, "C:\\ExportTest.tex", true);
- gResources().create(dbgMeshRef, "C:\\ExportMesh.mesh", true);
- gResources().unload(testTexRef);
- gResources().unload(dbgMeshRef);
- testTexRef = static_resource_cast<Texture>(gResources().loadAsync("C:\\ExportTest.tex"));
- dbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync("C:\\ExportMesh.mesh"));
-
- dbgMeshRef.waitUntilLoaded();
- testTexRef.waitUntilLoaded();
- testMaterial->setTexture("tex", testTexRef);
- gResources().create(testMaterial, "C:\\ExportMaterial.mat", true);
- gResources().unload(testMaterial);
- testMaterial = gResources().load("C:\\ExportMaterial.mat");
- //_ASSERT(_CrtCheckMemory());
- testRenderable->setMesh(dbgMeshRef);
- testRenderable->setMaterial(testMaterial);
- //// Set the new state for the flag
- //_CrtSetDbgFlag( tmpFlag );
- gApplication().runMainLoop();
- // Release everything before shutdown
-
- //testMaterial->destroy();
- #ifdef DX11
- gpuProgInclude.reset();
- #endif
- gResources().unload(testTexRef);
- gResources().unload(dbgMeshRef);
- gResources().unload(fragProgRef);
- gResources().unload(vertProgRef);
- gResources().unload(testMaterial);
- testMaterial.reset();
- testTexRef.reset();
- dbgMeshRef.reset();
- fragProgRef.reset();
- vertProgRef.reset();
- testModelGO->destroy();
- testModelGO = nullptr;
- testRenderable = nullptr;
- cameraGO->destroy();
- cameraGO = nullptr;
- camera = nullptr;
- debugCamera = nullptr;
- newPassGL = nullptr;
- newTechniqueGL = nullptr;
- newPassDX = nullptr;
- newTechniqueDX = nullptr;
- newPassDX11 = nullptr;
- newTechniqueDX11 = nullptr;
- testShader = nullptr;
-
- renderWindow = nullptr;
- gApplication().shutDown();
- return 0;
- }
|