CamelotClient.cpp 10 KB

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