CmApplication.cpp 8.7 KB

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