2
0

CmApplication.cpp 13 KB

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