CmApplication.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include "CmApplication.h"
  2. #include "CmRenderSystem.h"
  3. #include "CmRenderSystemManager.h"
  4. #include "CmWindowEventUtilities.h"
  5. #include "CmHardwareBufferManager.h"
  6. #include "CmRenderWindow.h"
  7. #include "CmCamera.h"
  8. #include "CmViewport.h"
  9. #include "CmVector2.h"
  10. #include "CmHighLevelGpuProgram.h"
  11. #include "CmHighLevelGpuProgramManager.h"
  12. #include "CmDynLib.h"
  13. #include "CmDynLibManager.h"
  14. #include "CmSceneManager.h"
  15. #include "CmImporter.h"
  16. #include "CmResources.h"
  17. #include "CmMesh.h"
  18. #include "CmGameObject.h"
  19. namespace CamelotEngine
  20. {
  21. Application::Application()
  22. :mRenderWindow(nullptr), mCamera(nullptr)
  23. { }
  24. void Application::startUp(String renderSystemDll)
  25. {
  26. DynLibManager::startUp(new DynLibManager());
  27. HighLevelGpuProgramManager::startUp(new HighLevelGpuProgramManager());
  28. RenderSystemManager::initialize(renderSystemDll);
  29. RenderSystem* renderSystem = RenderSystemManager::getActive();
  30. renderSystem->_initialise(false, "Camelot Renderer");
  31. SceneManager::startUp(new SceneManager());
  32. mRenderWindow = renderSystem->_createRenderWindow("Camelot Renderer", 800, 600, false);
  33. //renderSystem->setAmbientLight(1.0f, 1.0f, 1.0f);
  34. renderSystem->setLightingEnabled(false);
  35. mCameraGO = GameObject::create("MainCamera");
  36. mCamera = mCameraGO->addComponent<Camera>();
  37. mCamera->init(mRenderWindow, 0.0f, 0.0f, 1.0f, 1.0f, 0);
  38. mCameraGO->setPosition(Vector3(0,50,1240));
  39. mCameraGO->lookAt(Vector3(0,50,-300));
  40. mCamera->setNearClipDistance(5);
  41. mCamera->setAspectRatio(800.0f / 600.0f);
  42. /////////////////// HLSL SHADERS //////////////////////////
  43. //String fragShaderCode = "sampler2D diffuseMap; \
  44. // float4 ps_main(float2 uv : TEXCOORD0) : COLOR0 \
  45. //{ \
  46. // float4 color = tex2D(diffuseMap, uv); \
  47. // return color; \
  48. //}";
  49. //mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "ps_main", "hlsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  50. //mFragProg->load();
  51. //String vertShaderCode = "float4x4 matViewProjection; \
  52. // void vs_main( \
  53. // float4 inPos : POSITION, \
  54. // float2 uv : TEXCOORD0, \
  55. // out float4 oPosition : POSITION, \
  56. // out float2 oUv : TEXCOORD0) \
  57. //{ \
  58. // oPosition = mul(matViewProjection, inPos); \
  59. // oUv = uv; \
  60. //}";
  61. //mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "vs_main", "hlsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  62. //mVertProg->load();
  63. /////////////////// CG SHADERS //////////////////////////
  64. String fragShaderCode = "sampler2D diffuseMap; \
  65. float4 ps_main(float2 uv : TEXCOORD0) : COLOR0 \
  66. { \
  67. float4 color = tex2D(diffuseMap, uv); \
  68. return color; \
  69. }";
  70. mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "ps_main", "cg", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  71. mFragProg->load();
  72. String vertShaderCode = "float4x4 matViewProjection; \
  73. void vs_main( \
  74. float4 inPos : POSITION, \
  75. float2 uv : TEXCOORD0, \
  76. out float4 oPosition : POSITION, \
  77. out float2 oUv : TEXCOORD0) \
  78. { \
  79. oPosition = mul(matViewProjection, inPos); \
  80. oUv = uv; \
  81. }";
  82. mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "vs_main", "cg", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  83. mVertProg->load();
  84. ///////////////// GLSL SHADERS ////////////////////////////
  85. //String fragShaderCode = "uniform sampler2D tex; \
  86. // void main() \
  87. // {\
  88. // vec4 texColor = texture2D(tex,gl_TexCoord[0].st);\
  89. // gl_FragColor = texColor; \
  90. // }";
  91. //mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "main", "glsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  92. //mFragProg->load();
  93. //// TODO - Ogres GLSL parsing requires some strict parameter naming, can that be avoided?
  94. //String vertShaderCode = "uniform mat4 matViewProjection; \
  95. // attribute vec4 vertex; \
  96. // void main() \
  97. // { \
  98. // gl_TexCoord[0] = gl_MultiTexCoord0; \
  99. // gl_Position = matViewProjection * vertex; \
  100. // }";
  101. //mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "main", "glsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  102. //mVertProg->load();
  103. // IMPORTER TEST
  104. Importer::startUp(new Importer());
  105. DynLib* freeImgLibrary = gDynLibManager().load("CamelotFreeImgImporter.dll"); // TODO - Load this automatically somehow
  106. if(freeImgLibrary != nullptr)
  107. {
  108. typedef const void (*LoadPluginFunc)();
  109. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)freeImgLibrary->getSymbol("loadPlugin");
  110. loadPluginFunc();
  111. }
  112. DynLib* fbxLibrary = gDynLibManager().load("CamelotFBXImporter.dll"); // TODO - Load this automatically somehow
  113. if(fbxLibrary != nullptr)
  114. {
  115. typedef const void (*LoadPluginFunc)();
  116. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)fbxLibrary->getSymbol("loadPlugin");
  117. loadPluginFunc();
  118. }
  119. //mDbgTexture = std::static_pointer_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
  120. TexturePtr testTex = std::static_pointer_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
  121. mDbgMesh = std::static_pointer_cast<Mesh>(Importer::instance().import("C:\\X_Arena_Tower.FBX"));
  122. //mDbgMesh = std::static_pointer_cast<Mesh>(Importer::instance().import("C:\\TestFBX.fbx"));
  123. Resources::startUp(new Resources());
  124. gResources().save(testTex, "C:\\ExportTest.tex");
  125. gResources().save(mDbgMesh, "C:\\ExportMesh.mesh");
  126. mDbgTexture = std::static_pointer_cast<Texture>(gResources().load("C:\\ExportTest.tex"));
  127. mDbgMesh = std::static_pointer_cast<Mesh>(gResources().load("C:\\ExportMesh.mesh"));
  128. mDbgTexture = testTex;
  129. }
  130. void Application::runMainLoop()
  131. {
  132. while(true)
  133. {
  134. WindowEventUtilities::messagePump();
  135. DBG_renderSimpleFrame();
  136. }
  137. }
  138. void Application::shutDown()
  139. {
  140. SceneManager::shutDown();
  141. if(RenderSystemManager::getActive() != nullptr)
  142. RenderSystemManager::getActive()->shutdown();
  143. HighLevelGpuProgramManager::shutDown();
  144. DynLibManager::shutDown();
  145. Resources::shutDown();
  146. }
  147. void Application::DBG_renderSimpleFrame()
  148. {
  149. RenderOperation ro;
  150. IndexData* indexData = new IndexData();
  151. indexData->indexCount = 36;
  152. indexData->indexBuffer = HardwareBufferManager::instance().createIndexBuffer(
  153. HardwareIndexBuffer::IT_16BIT,
  154. 36,
  155. HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  156. unsigned short* idxData = static_cast<unsigned short*>(indexData->indexBuffer->lock(HardwareBuffer::HBL_NORMAL));
  157. idxData[0] = 0; idxData[1] = 1; idxData[2] = 2;
  158. idxData[3] = 2; idxData[4] = 3; idxData[5] = 0;
  159. idxData[6] = 4; idxData[7] = 5; idxData[8] = 6;
  160. idxData[9] = 6; idxData[10] = 7; idxData[11] = 4;
  161. idxData[12] = 0; idxData[13] = 3; idxData[14] = 5;
  162. idxData[15] = 5; idxData[16] = 4; idxData[17] = 0;
  163. idxData[18] = 3; idxData[19] = 2; idxData[20] = 6;
  164. idxData[21] = 6; idxData[22] = 5; idxData[23] = 3;
  165. idxData[24] = 2; idxData[25] = 1; idxData[26] = 7;
  166. idxData[27] = 7; idxData[28] = 6; idxData[29] = 2;
  167. idxData[30] = 1; idxData[31] = 0; idxData[32] = 4;
  168. idxData[33] = 4; idxData[34] = 7; idxData[35] = 1;
  169. indexData->indexBuffer->unlock();
  170. VertexData* vertexData = new VertexData();
  171. vertexData->vertexStart = 0;
  172. vertexData->vertexCount = 8;
  173. VertexDeclarationPtr decl = vertexData->vertexDeclaration;
  174. decl->removeAllElements();
  175. size_t offset = 0;
  176. decl->addElement(0, offset, VET_FLOAT3, VES_POSITION);
  177. offset += VertexElement::getTypeSize(VET_FLOAT3);
  178. decl->addElement(0, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES);
  179. offset += VertexElement::getTypeSize(VET_FLOAT2);
  180. //decl->addElement(0, offset, VET_COLOUR, VES_DIFFUSE);
  181. //offset += VertexElement::getTypeSize(VET_COLOUR);
  182. HardwareVertexBufferPtr vertexBuffer = HardwareBufferManager::instance().createVertexBuffer(
  183. vertexData->vertexDeclaration->getVertexSize(0),
  184. vertexData->vertexCount,
  185. HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  186. vertexData->vertexBufferBinding->setBinding(0, vertexBuffer);
  187. size_t vertexSize = vertexBuffer->getVertexSize();
  188. char* vertBufferData = static_cast<char*>(vertexBuffer->lock(HardwareBuffer::HBL_NORMAL));
  189. size_t posSize = VertexElement::getTypeSize(VET_FLOAT3);
  190. size_t uvSize = VertexElement::getTypeSize(VET_FLOAT2);
  191. Vector3 position(-5.0f, -5.0f, -5.0f);
  192. memcpy(vertBufferData, &position, posSize);
  193. vertBufferData += posSize;
  194. Vector2 uv(0.0f, 0.0f);
  195. memcpy(vertBufferData, &uv, uvSize);
  196. vertBufferData += uvSize;
  197. position = Vector3(-5.0f, 5.0f, -5.0f);
  198. memcpy(vertBufferData, &position, posSize);
  199. vertBufferData += posSize;
  200. uv = Vector2(0.0f, 1.0f);
  201. memcpy(vertBufferData, &uv, uvSize);
  202. vertBufferData += uvSize;
  203. position = Vector3(5.0f, 5.0f, -5.0f);
  204. memcpy(vertBufferData, &position, posSize);
  205. vertBufferData += posSize;
  206. uv = Vector2(1.0f, 1.0f);
  207. memcpy(vertBufferData, &uv, uvSize);
  208. vertBufferData += uvSize;
  209. position = Vector3(5.0f, -5.0f, -5.0f);
  210. memcpy(vertBufferData, &position, posSize);
  211. vertBufferData += posSize;
  212. uv = Vector2(1.0f, 0.0f);
  213. memcpy(vertBufferData, &uv, uvSize);
  214. vertBufferData += uvSize;
  215. position = Vector3(-5.0f, -5.0f, 5.0f);
  216. memcpy(vertBufferData, &position, posSize);
  217. vertBufferData += posSize;
  218. uv = Vector2(0.0f, 0.0f);
  219. memcpy(vertBufferData, &uv, uvSize);
  220. vertBufferData += uvSize;
  221. position = Vector3(5.0f, -5.0f, 5.0f);
  222. memcpy(vertBufferData, &position, posSize);
  223. vertBufferData += posSize;
  224. uv = Vector2(1.0f, 0.0f);
  225. memcpy(vertBufferData, &uv, uvSize);
  226. vertBufferData += uvSize;
  227. position = Vector3(5.0f, 5.0f, 5.0f);
  228. memcpy(vertBufferData, &position, posSize);
  229. vertBufferData += posSize;
  230. uv = Vector2(1.0f, 1.0f);
  231. memcpy(vertBufferData, &uv, uvSize);
  232. vertBufferData += uvSize;
  233. position = Vector3(-5.0f, 5.0f, 5.0f);
  234. memcpy(vertBufferData, &position, posSize);
  235. vertBufferData += posSize;
  236. uv = Vector2(0.0f, 1.0f);
  237. memcpy(vertBufferData, &uv, uvSize);
  238. vertBufferData += uvSize;
  239. vertexBuffer->unlock();
  240. ro.indexData = indexData;
  241. ro.vertexData = vertexData;
  242. ro.useIndexes = true;
  243. ro.operationType = RenderOperation::OT_TRIANGLE_LIST;
  244. RenderSystem* renderSystem = RenderSystemManager::getActive();
  245. renderSystem->_setViewport(mCamera->getViewport());
  246. //Matrix4 projMatrix = mCamera->getProjectionMatrixRS();
  247. //renderSystem->_setProjectionMatrix(projMatrix);
  248. //Matrix4 viewMatrix = mCamera->getViewMatrix(true);
  249. //renderSystem->_setViewMatrix(viewMatrix);
  250. Matrix4 projMatrixCstm = mCamera->getProjectionMatrix();
  251. Matrix4 viewMatrixCstm = mCamera->getViewMatrix();
  252. Matrix4 viewProjMatrix = projMatrixCstm * viewMatrixCstm;
  253. renderSystem->setInvertVertexWinding(false);
  254. renderSystem->_setDepthBufferParams();
  255. renderSystem->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH, Color::Blue);
  256. renderSystem->_beginFrame();
  257. mVertProg->getDefaultParameters()->setNamedConstant("matViewProjection", viewProjMatrix);
  258. //renderSystem->bindGpuProgramParameters(GPT_VERTEX_PROGRAM, mVertProg->getDefaultParameters(), GPV_ALL);
  259. renderSystem->_setTexture(0, true, mDbgTexture);
  260. renderSystem->bindGpuProgram(mFragProg->_getBindingDelegate()); // TODO - I don't like this. Shader should be able to be bound directly!
  261. renderSystem->bindGpuProgram(mVertProg->_getBindingDelegate()); // TODO - I don't like this. Shader should be able to be bound directly!
  262. // TODO - Shaders need to be bound and only then parameters can be set. I need to encapuslate this better because I can't expect users to know that
  263. renderSystem->bindGpuProgramParameters(GPT_FRAGMENT_PROGRAM, mFragProg->getDefaultParameters(), GPV_ALL); // TODO - If I dont call bind parameters before shader wont activate? I think I should handle that differently
  264. renderSystem->bindGpuProgramParameters(GPT_VERTEX_PROGRAM, mVertProg->getDefaultParameters(), GPV_ALL);
  265. /*renderSystem->_render(ro);*/
  266. renderSystem->_render(mDbgMesh->getRenderOperation());
  267. renderSystem->_endFrame();
  268. renderSystem->_swapAllRenderTargetBuffers(false);
  269. }
  270. Application& gApplication()
  271. {
  272. static Application application;
  273. return application;
  274. }
  275. }