CmApplication.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "CmHighLevelGpuProgram.h"
  10. #include "CmHighLevelGpuProgramManager.h"
  11. #include "CmDynLib.h"
  12. #include "CmDynLibManager.h"
  13. #include "CmImporter.h"
  14. #include "CmResources.h"
  15. namespace CamelotEngine
  16. {
  17. Application::Application()
  18. :mRenderWindow(nullptr), mViewport(nullptr), mCamera(nullptr)
  19. { }
  20. void Application::startUp(String renderSystemDll)
  21. {
  22. DynLibManager::startUp(new DynLibManager());
  23. HighLevelGpuProgramManager::startUp(new HighLevelGpuProgramManager());
  24. RenderSystemManager::initialize(renderSystemDll);
  25. RenderSystem* renderSystem = RenderSystemManager::getActive();
  26. renderSystem->_initialise(false, "Camelot Renderer");
  27. mRenderWindow = renderSystem->_createRenderWindow("Camelot Renderer", 800, 600, false);
  28. //renderSystem->setAmbientLight(1.0f, 1.0f, 1.0f);
  29. renderSystem->setLightingEnabled(false);
  30. mCamera = new Camera("SimpleCam");
  31. mCamera->setPosition(Vector3(0,0,40));
  32. mCamera->lookAt(Vector3(0,0,-300));
  33. mCamera->setNearClipDistance(5);
  34. mCamera->setAspectRatio(600.0f / 800.0f);
  35. mViewport = mRenderWindow->addViewport();
  36. /////////////////// HLSL SHADERS //////////////////////////
  37. //String fragShaderCode = "float4 ps_main() : COLOR0 \
  38. //{ \
  39. // float4 color = float4(0, 0, 0, 0); \
  40. // color.r = 1.0f; \
  41. // color.a = 1.0f; \
  42. // return color; \
  43. //}";
  44. //mFragProg = mGpuProgramManager->createProgram(fragShaderCode, "ps_main", "hlsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  45. //mFragProg->load();
  46. //String vertShaderCode = "float4x4 matViewProjection; \
  47. //float4 vs_main(float4 inPos : POSITION) : POSITION \
  48. //{ \
  49. // return mul(matViewProjection, inPos); \
  50. //}";
  51. //mVertProg = mGpuProgramManager->createProgram(vertShaderCode, "vs_main", "hlsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  52. //mVertProg->load();
  53. ///////////////// GLSL SHADERS ////////////////////////////
  54. String fragShaderCode = "uniform sampler2D tex; \
  55. void main() \
  56. {\
  57. vec4 texColor = texture2D(tex,gl_TexCoord[0].st);\
  58. gl_FragColor = texColor; \
  59. }";
  60. mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "main", "glsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  61. mFragProg->load();
  62. // TODO - Ogres GLSL parsing requires some strict parameter naming, can that be avoided?
  63. String vertShaderCode = "uniform mat4 matViewProjection; \
  64. attribute vec4 vertex; \
  65. void main() \
  66. { \
  67. gl_TexCoord[0] = gl_MultiTexCoord0; \
  68. gl_Position = matViewProjection * vertex; \
  69. }";
  70. mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "main", "glsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  71. mVertProg->load();
  72. // IMPORTER TEST
  73. Importer::startUp(new Importer());
  74. DynLib* loadedLibrary = gDynLibManager().load("CamelotFreeImgImporter.dll"); // TODO - Load this automatically somehow
  75. if(loadedLibrary != nullptr)
  76. {
  77. typedef const void (*LoadPluginFunc)();
  78. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  79. loadPluginFunc();
  80. }
  81. //mDbgTexture = std::static_pointer_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
  82. TexturePtr testTex = std::static_pointer_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
  83. Resources::startUp(new Resources());
  84. gResources().save(testTex, "C:\\ExportTest.tex");
  85. mDbgTexture = std::static_pointer_cast<Texture>(gResources().load("C:\\ExportTest.tex"));
  86. mDbgTexture = testTex;
  87. // TODO - Compare first few bytes of TextureData before and after save
  88. }
  89. void Application::runMainLoop()
  90. {
  91. while(true)
  92. {
  93. WindowEventUtilities::messagePump();
  94. DBG_renderSimpleFrame();
  95. }
  96. }
  97. void Application::shutDown()
  98. {
  99. if(RenderSystemManager::getActive() != nullptr)
  100. RenderSystemManager::getActive()->shutdown();
  101. HighLevelGpuProgramManager::shutDown();
  102. DynLibManager::shutDown();
  103. Resources::shutDown();
  104. }
  105. void Application::DBG_renderSimpleFrame()
  106. {
  107. RenderOperation ro;
  108. IndexData* indexData = new IndexData();
  109. indexData->indexCount = 36;
  110. indexData->indexBuffer = HardwareBufferManager::instance().createIndexBuffer(
  111. HardwareIndexBuffer::IT_16BIT,
  112. 36,
  113. HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  114. unsigned short* idxData = static_cast<unsigned short*>(indexData->indexBuffer->lock(HardwareBuffer::HBL_NORMAL));
  115. idxData[0] = 0; idxData[1] = 1; idxData[2] = 2;
  116. idxData[3] = 2; idxData[4] = 3; idxData[5] = 0;
  117. idxData[6] = 4; idxData[7] = 5; idxData[8] = 6;
  118. idxData[9] = 6; idxData[10] = 7; idxData[11] = 4;
  119. idxData[12] = 0; idxData[13] = 3; idxData[14] = 5;
  120. idxData[15] = 5; idxData[16] = 4; idxData[17] = 0;
  121. idxData[18] = 3; idxData[19] = 2; idxData[20] = 6;
  122. idxData[21] = 6; idxData[22] = 5; idxData[23] = 3;
  123. idxData[24] = 2; idxData[25] = 1; idxData[26] = 7;
  124. idxData[27] = 7; idxData[28] = 6; idxData[29] = 2;
  125. idxData[30] = 1; idxData[31] = 0; idxData[32] = 4;
  126. idxData[33] = 4; idxData[34] = 7; idxData[35] = 1;
  127. indexData->indexBuffer->unlock();
  128. VertexData* vertexData = new VertexData();
  129. vertexData->vertexStart = 0;
  130. vertexData->vertexCount = 8;
  131. VertexDeclaration* decl = vertexData->vertexDeclaration;
  132. decl->removeAllElements();
  133. size_t offset = 0;
  134. decl->addElement(0, offset, VET_FLOAT3, VES_POSITION);
  135. offset += VertexElement::getTypeSize(VET_FLOAT3);
  136. decl->addElement(0, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES);
  137. offset += VertexElement::getTypeSize(VET_FLOAT2);
  138. //decl->addElement(0, offset, VET_COLOUR, VES_DIFFUSE);
  139. //offset += VertexElement::getTypeSize(VET_COLOUR);
  140. HardwareVertexBufferPtr vertexBuffer = HardwareBufferManager::instance().createVertexBuffer(
  141. vertexData->vertexDeclaration->getVertexSize(0),
  142. vertexData->vertexCount,
  143. HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  144. vertexData->vertexBufferBinding->setBinding(0, vertexBuffer);
  145. size_t vertexSize = vertexBuffer->getVertexSize();
  146. char* vertBufferData = static_cast<char*>(vertexBuffer->lock(HardwareBuffer::HBL_NORMAL));
  147. size_t posSize = VertexElement::getTypeSize(VET_FLOAT3);
  148. size_t uvSize = VertexElement::getTypeSize(VET_FLOAT2);
  149. Vector3 position(-5.0f, -5.0f, -5.0f);
  150. memcpy(vertBufferData, &position, posSize);
  151. vertBufferData += posSize;
  152. Vector2 uv(0.0f, 0.0f);
  153. memcpy(vertBufferData, &uv, uvSize);
  154. vertBufferData += uvSize;
  155. position = Vector3(-5.0f, 5.0f, -5.0f);
  156. memcpy(vertBufferData, &position, posSize);
  157. vertBufferData += posSize;
  158. uv = Vector2(0.0f, 1.0f);
  159. memcpy(vertBufferData, &uv, uvSize);
  160. vertBufferData += uvSize;
  161. position = Vector3(5.0f, 5.0f, -5.0f);
  162. memcpy(vertBufferData, &position, posSize);
  163. vertBufferData += posSize;
  164. uv = Vector2(1.0f, 1.0f);
  165. memcpy(vertBufferData, &uv, uvSize);
  166. vertBufferData += uvSize;
  167. position = Vector3(5.0f, -5.0f, -5.0f);
  168. memcpy(vertBufferData, &position, posSize);
  169. vertBufferData += posSize;
  170. uv = Vector2(1.0f, 0.0f);
  171. memcpy(vertBufferData, &uv, uvSize);
  172. vertBufferData += uvSize;
  173. position = Vector3(-5.0f, -5.0f, 5.0f);
  174. memcpy(vertBufferData, &position, posSize);
  175. vertBufferData += posSize;
  176. uv = Vector2(0.0f, 0.0f);
  177. memcpy(vertBufferData, &uv, uvSize);
  178. vertBufferData += uvSize;
  179. position = Vector3(5.0f, -5.0f, 5.0f);
  180. memcpy(vertBufferData, &position, posSize);
  181. vertBufferData += posSize;
  182. uv = Vector2(1.0f, 0.0f);
  183. memcpy(vertBufferData, &uv, uvSize);
  184. vertBufferData += uvSize;
  185. position = Vector3(5.0f, 5.0f, 5.0f);
  186. memcpy(vertBufferData, &position, posSize);
  187. vertBufferData += posSize;
  188. uv = Vector2(1.0f, 1.0f);
  189. memcpy(vertBufferData, &uv, uvSize);
  190. vertBufferData += uvSize;
  191. position = Vector3(-5.0f, 5.0f, 5.0f);
  192. memcpy(vertBufferData, &position, posSize);
  193. vertBufferData += posSize;
  194. uv = Vector2(0.0f, 1.0f);
  195. memcpy(vertBufferData, &uv, uvSize);
  196. vertBufferData += uvSize;
  197. vertexBuffer->unlock();
  198. ro.indexData = indexData;
  199. ro.vertexData = vertexData;
  200. ro.useIndexes = true;
  201. ro.operationType = RenderOperation::OT_TRIANGLE_LIST;
  202. RenderSystem* renderSystem = RenderSystemManager::getActive();
  203. renderSystem->_setViewport(mViewport);
  204. //Matrix4 projMatrix = mCamera->getProjectionMatrixRS();
  205. //renderSystem->_setProjectionMatrix(projMatrix);
  206. //Matrix4 viewMatrix = mCamera->getViewMatrix(true);
  207. //renderSystem->_setViewMatrix(viewMatrix);
  208. Matrix4 projMatrixCstm = mCamera->getProjectionMatrix();
  209. Matrix4 viewMatrixCstm = mCamera->getViewMatrix(true);
  210. Matrix4 viewProjMatrix = projMatrixCstm * viewMatrixCstm;
  211. renderSystem->setInvertVertexWinding(false);
  212. renderSystem->_setDepthBufferParams();
  213. renderSystem->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH, Color::Blue);
  214. renderSystem->_beginFrame();
  215. mVertProg->getDefaultParameters()->setNamedConstant("matViewProjection", viewProjMatrix);
  216. //renderSystem->bindGpuProgramParameters(GPT_VERTEX_PROGRAM, mVertProg->getDefaultParameters(), GPV_ALL);
  217. renderSystem->_setTexture(0, true, mDbgTexture);
  218. renderSystem->bindGpuProgram(mFragProg->_getBindingDelegate()); // TODO - I don't like this. Shader should be able to be bound directly!
  219. renderSystem->bindGpuProgram(mVertProg->_getBindingDelegate()); // TODO - I don't like this. Shader should be able to be bound directly!
  220. // 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
  221. 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
  222. renderSystem->bindGpuProgramParameters(GPT_VERTEX_PROGRAM, mVertProg->getDefaultParameters(), GPV_ALL);
  223. renderSystem->_render(ro);
  224. renderSystem->_endFrame();
  225. renderSystem->_swapAllRenderTargetBuffers(false);
  226. }
  227. Application& gApplication()
  228. {
  229. static Application application;
  230. return application;
  231. }
  232. }