CamelotClient.cpp 9.7 KB

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