CmApplication.cpp 9.9 KB

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