CamelotClient.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // CamelotClient.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include "CmApplication.h"
  6. #include "CmDynLibManager.h"
  7. #include "CmGameObject.h"
  8. #include "CmCamera.h"
  9. #include "CmHighLevelGpuProgramManager.h"
  10. #include "CmRenderSystem.h"
  11. #include "CmRenderWindow.h"
  12. #include "CmResources.h"
  13. #include "CmRenderable.h"
  14. #include "CmMaterial.h"
  15. #include "CmShader.h"
  16. #include "CmTechnique.h"
  17. #include "CmPass.h"
  18. #include "CmImporter.h"
  19. #include "CmMesh.h"
  20. #include "CmGpuProgInclude.h" // DEBUG ONLY
  21. #include "CmDebugCamera.h"
  22. using namespace CamelotEngine;
  23. int CALLBACK WinMain(
  24. _In_ HINSTANCE hInstance,
  25. _In_ HINSTANCE hPrevInstance,
  26. _In_ LPSTR lpCmdLine,
  27. _In_ int nCmdShow
  28. )
  29. {
  30. //gApplication().startUp("CamelotGLRenderSystem", "CamelotForwardRenderer");
  31. //gApplication().startUp("CamelotD3D9RenderSystem", "CamelotForwardRenderer");
  32. gApplication().startUp("CamelotD3D11RenderSystem", "CamelotForwardRenderer");
  33. RenderSystem* renderSystem = RenderSystem::instancePtr();
  34. RenderWindowPtr renderWindow = gApplication().getPrimaryRenderWindow();
  35. GameObjectPtr cameraGO = GameObject::create("MainCamera");
  36. CameraPtr camera = cameraGO->addComponent<Camera>();
  37. camera->init(renderWindow, 0.0f, 0.0f, 1.0f, 1.0f, 0);
  38. cameraGO->setPosition(Vector3(0,50,1240));
  39. cameraGO->lookAt(Vector3(0,50,-300));
  40. camera->setNearClipDistance(5);
  41. camera->setAspectRatio(800.0f / 600.0f);
  42. std::shared_ptr<DebugCamera> debugCamera = cameraGO->addComponent<DebugCamera>();
  43. GameObjectPtr testModelGO = GameObject::create("TestMesh");
  44. RenderablePtr testRenderable = testModelGO->addComponent<Renderable>();
  45. GpuProgIncludeHandle gpuProgInclude = Importer::instance().import("C:\\testInclude.gpuproginc");
  46. const String& debugString = gpuProgInclude->getString();
  47. /////////////////// HLSL 9 SHADERS //////////////////////////
  48. //String fragShaderCode = "sampler2D tex; \
  49. // float4 ps_main(float2 uv : TEXCOORD0) : COLOR0 \
  50. // { \
  51. // float4 color = tex2D(tex, uv); \
  52. // return color; \
  53. // }";
  54. //HighLevelGpuProgramHandle fragProgRef = HighLevelGpuProgram::create(fragShaderCode, "ps_main", "hlsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  55. //String vertShaderCode = "float4x4 matViewProjection; \
  56. // void vs_main( \
  57. // float4 inPos : POSITION, \
  58. // float2 uv : TEXCOORD0, \
  59. // out float4 oPosition : POSITION, \
  60. // out float2 oUv : TEXCOORD0) \
  61. // { \
  62. // oPosition = mul(matViewProjection, inPos); \
  63. // oUv = uv; \
  64. // }";
  65. //HighLevelGpuProgramHandle vertProgRef = HighLevelGpuProgram::create(vertShaderCode, "vs_main", "hlsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  66. /////////////////// HLSL 11 SHADERS //////////////////////////
  67. String fragShaderCode = "SamplerState samp : register(s0); \
  68. Texture2D tex : register(t0); \
  69. float4 ps_main(in float4 inPos : SV_Position, float2 uv : TEXCOORD0) : SV_Target \
  70. { \
  71. float4 color = tex.Sample(samp, uv); \
  72. return color; \
  73. }";
  74. HighLevelGpuProgramHandle fragProgRef = HighLevelGpuProgram::create(fragShaderCode, "ps_main", "hlsl", GPT_FRAGMENT_PROGRAM, GPP_PS_4_0);
  75. String vertShaderCode = "float4x4 matViewProjection; \
  76. void vs_main( \
  77. in float4 inPos : POSITION, \
  78. in float2 uv : TEXCOORD0, \
  79. out float4 oPosition : SV_Position, \
  80. out float2 oUv : TEXCOORD0) \
  81. { \
  82. oPosition = mul(matViewProjection, inPos); \
  83. oUv = uv; \
  84. }";
  85. HighLevelGpuProgramHandle vertProgRef = HighLevelGpuProgram::create(vertShaderCode, "vs_main", "hlsl", GPT_VERTEX_PROGRAM, GPP_VS_4_0);
  86. /////////////////// CG SHADERS //////////////////////////
  87. //String fragShaderCode = "sampler2D tex; \
  88. // float4 ps_main(float2 uv : TEXCOORD0) : COLOR0 \
  89. // { \
  90. // float4 color = tex2D(tex, uv); \
  91. // return color; \
  92. // }";
  93. //HighLevelGpuProgramHandle fragProgRef = HighLevelGpuProgram::create(fragShaderCode, "ps_main", "cg", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  94. //String vertShaderCode = "float4x4 matViewProjection; \
  95. // void vs_main( \
  96. // float4 cm_position : POSITION, \
  97. // float2 cm_texcoord0 : TEXCOORD0, \
  98. // out float4 oPosition : POSITION, \
  99. // out float2 oUv : TEXCOORD0) \
  100. // { \
  101. // oPosition = mul(matViewProjection, cm_position); \
  102. // oUv = cm_texcoord0; \
  103. // }";
  104. //HighLevelGpuProgramHandle vertProgRef = HighLevelGpuProgram::create(vertShaderCode, "vs_main", "cg", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  105. ///////////////// GLSL SHADERS ////////////////////////////
  106. //String fragShaderCode = " #version 400 \n \
  107. // uniform sampler2D tex; \
  108. // in vec2 texcoord0; \
  109. // out vec4 fragColor; \
  110. // void main() \
  111. // {\
  112. // vec4 texColor = texture2D(tex, texcoord0.st);\
  113. // fragColor = texColor; \
  114. // }";
  115. //HighLevelGpuProgramHandle fragProgRef = HighLevelGpuProgram::create(fragShaderCode, "main", "glsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  116. //// TODO - Make sure to document the strict input parameter naming. (Exact supported names are in GLSLParamParser)
  117. //String vertShaderCode = "#version 400 \n \
  118. // uniform mainFragBlock { mat4 matViewProjection; }; \
  119. // in vec4 cm_position; \
  120. // in vec2 cm_texcoord0; \
  121. // out vec2 texcoord0; \
  122. // void main() \
  123. // { \
  124. // texcoord0 = cm_texcoord0; \
  125. // gl_Position = cm_position * matViewProjection; \
  126. // }";
  127. //HighLevelGpuProgramHandle vertProgRef= HighLevelGpuProgram::create(vertShaderCode, "main", "glsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  128. gResources().create(vertProgRef, "C:\\vertProgCg.vprog", true);
  129. gResources().unload(vertProgRef);
  130. vertProgRef = gResources().load("C:\\vertProgCg.vprog");
  131. gResources().create(fragProgRef, "C:\\fragProgCg.vprog", true);
  132. gResources().unload(fragProgRef);
  133. fragProgRef = gResources().load("C:\\fragProgCg.vprog");
  134. ShaderPtr testShader = Shader::create("TestShader");
  135. testShader->addParameter("matViewProjection", "matViewProjection", GPDT_MATRIX_4X4);
  136. testShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
  137. testShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
  138. TechniquePtr newTechniqueGL = testShader->addTechnique("GLRenderSystem", "ForwardRenderer");
  139. PassPtr newPassGL = newTechniqueGL->addPass();
  140. newPassGL->setVertexProgram(vertProgRef);
  141. newPassGL->setFragmentProgram(fragProgRef);
  142. // TODO - I need to create different techniques for different render systems (and renderers, if there were any),
  143. // which is redundant as some techniques can be reused. I should add a functionality that supports multiple
  144. // render systems/renderers per technique
  145. TechniquePtr newTechniqueDX = testShader->addTechnique("D3D9RenderSystem", "ForwardRenderer");
  146. PassPtr newPassDX = newTechniqueDX->addPass();
  147. newPassDX->setVertexProgram(vertProgRef);
  148. newPassDX->setFragmentProgram(fragProgRef);
  149. TechniquePtr newTechniqueDX11 = testShader->addTechnique("D3D11RenderSystem", "ForwardRenderer");
  150. PassPtr newPassDX11 = newTechniqueDX11->addPass();
  151. newPassDX11->setVertexProgram(vertProgRef);
  152. newPassDX11->setFragmentProgram(fragProgRef);
  153. MaterialHandle testMaterial = Material::create();
  154. testMaterial.waitUntilLoaded(); // TODO - Material doesn't do anything GPU specific, so technically it should be possible to initialize on the spot
  155. // but is that a good idea?
  156. testMaterial->setShader(testShader);
  157. testMaterial->setMat4("matViewProjection", Matrix4::IDENTITY);
  158. //testMaterialRef = gResources().load("C:\\testMaterial.mat");
  159. //testMaterialRef.waitUntilLoaded();
  160. /*TextureRef testTex = static_resource_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));*/
  161. TextureHandle testTexRef = static_resource_cast<Texture>(Importer::instance().import("C:\\ArenaTowerDFS.psd"));
  162. MeshHandle dbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import("C:\\X_Arena_Tower.FBX"));
  163. //int tmpFlag = _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
  164. gResources().create(testTexRef, "C:\\ExportTest.tex", true);
  165. gResources().create(dbgMeshRef, "C:\\ExportMesh.mesh", true);
  166. gResources().unload(testTexRef);
  167. gResources().unload(dbgMeshRef);
  168. testTexRef = static_resource_cast<Texture>(gResources().load("C:\\ExportTest.tex"));
  169. dbgMeshRef = static_resource_cast<Mesh>(gResources().load("C:\\ExportMesh.mesh"));
  170. testMaterial->setTexture("tex", testTexRef);
  171. gResources().create(testMaterial, "C:\\ExportMaterial.mat", true);
  172. gResources().unload(testMaterial);
  173. testMaterial = gResources().load("C:\\ExportMaterial.mat");
  174. //_ASSERT(_CrtCheckMemory());
  175. //dbgMeshRef.waitUntilLoaded();
  176. testRenderable->setMesh(dbgMeshRef);
  177. testRenderable->setMaterial(testMaterial);
  178. //// Set the new state for the flag
  179. //_CrtSetDbgFlag( tmpFlag );
  180. gApplication().runMainLoop();
  181. // Release everything before shutdown
  182. //testMaterial->destroy();
  183. gpuProgInclude.reset();
  184. gResources().unload(testTexRef);
  185. gResources().unload(dbgMeshRef);
  186. gResources().unload(fragProgRef);
  187. gResources().unload(vertProgRef);
  188. gResources().unload(testMaterial);
  189. testMaterial.reset();
  190. testTexRef.reset();
  191. dbgMeshRef.reset();
  192. fragProgRef.reset();
  193. vertProgRef.reset();
  194. testModelGO->destroy();
  195. testModelGO = nullptr;
  196. testRenderable = nullptr;
  197. cameraGO->destroy();
  198. cameraGO = nullptr;
  199. camera = nullptr;
  200. debugCamera = nullptr;
  201. newPassGL = nullptr;
  202. newTechniqueGL = nullptr;
  203. newPassDX = nullptr;
  204. newTechniqueDX = nullptr;
  205. newPassDX11 = nullptr;
  206. newTechniqueDX11 = nullptr;
  207. testShader = nullptr;
  208. renderWindow = nullptr;
  209. gApplication().shutDown();
  210. return 0;
  211. }