CmApplication.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 = "sampler2D diffuseMap; \
  38. // float4 ps_main(float2 uv : TEXCOORD0) : COLOR0 \
  39. //{ \
  40. // float4 color = tex2D(diffuseMap, uv); \
  41. // return color; \
  42. //}";
  43. //mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "ps_main", "hlsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  44. //mFragProg->load();
  45. //String vertShaderCode = "float4x4 matViewProjection; \
  46. // void vs_main( \
  47. // float4 inPos : POSITION, \
  48. // float2 uv : TEXCOORD0, \
  49. // out float4 oPosition : POSITION, \
  50. // out float2 oUv : TEXCOORD0) \
  51. //{ \
  52. // oPosition = mul(matViewProjection, inPos); \
  53. // oUv = uv; \
  54. //}";
  55. //mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "vs_main", "hlsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  56. //mVertProg->load();
  57. /////////////////// CG SHADERS //////////////////////////
  58. String fragShaderCode = "sampler2D diffuseMap; \
  59. float4 ps_main(float2 uv : TEXCOORD0) : COLOR0 \
  60. { \
  61. float4 color = tex2D(diffuseMap, uv); \
  62. return color; \
  63. }";
  64. mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "ps_main", "cg", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  65. mFragProg->load();
  66. String vertShaderCode = "float4x4 matViewProjection; \
  67. void vs_main( \
  68. float4 inPos : POSITION, \
  69. float2 uv : TEXCOORD0, \
  70. out float4 oPosition : POSITION, \
  71. out float2 oUv : TEXCOORD0) \
  72. { \
  73. oPosition = mul(matViewProjection, inPos); \
  74. oUv = uv; \
  75. }";
  76. mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "vs_main", "cg", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  77. mVertProg->load();
  78. ///////////////// GLSL SHADERS ////////////////////////////
  79. //String fragShaderCode = "uniform sampler2D tex; \
  80. // void main() \
  81. // {\
  82. // vec4 texColor = texture2D(tex,gl_TexCoord[0].st);\
  83. // gl_FragColor = texColor; \
  84. // }";
  85. //mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "main", "glsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  86. //mFragProg->load();
  87. //// TODO - Ogres GLSL parsing requires some strict parameter naming, can that be avoided?
  88. //String vertShaderCode = "uniform mat4 matViewProjection; \
  89. // attribute vec4 vertex; \
  90. // void main() \
  91. // { \
  92. // gl_TexCoord[0] = gl_MultiTexCoord0; \
  93. // gl_Position = matViewProjection * vertex; \
  94. // }";
  95. //mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "main", "glsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  96. //mVertProg->load();
  97. // IMPORTER TEST
  98. Importer::startUp(new Importer());
  99. DynLib* loadedLibrary = gDynLibManager().load("CamelotFreeImgImporter.dll"); // TODO - Load this automatically somehow
  100. if(loadedLibrary != nullptr)
  101. {
  102. typedef const void (*LoadPluginFunc)();
  103. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  104. loadPluginFunc();
  105. }
  106. //mDbgTexture = std::static_pointer_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
  107. TexturePtr testTex = std::static_pointer_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
  108. Resources::startUp(new Resources());
  109. gResources().save(testTex, "C:\\ExportTest.tex");
  110. mDbgTexture = std::static_pointer_cast<Texture>(gResources().load("C:\\ExportTest.tex"));
  111. mDbgTexture = testTex;
  112. }
  113. void Application::runMainLoop()
  114. {
  115. while(true)
  116. {
  117. WindowEventUtilities::messagePump();
  118. DBG_renderSimpleFrame();
  119. }
  120. }
  121. void Application::shutDown()
  122. {
  123. if(RenderSystemManager::getActive() != nullptr)
  124. RenderSystemManager::getActive()->shutdown();
  125. HighLevelGpuProgramManager::shutDown();
  126. DynLibManager::shutDown();
  127. Resources::shutDown();
  128. }
  129. void Application::DBG_renderSimpleFrame()
  130. {
  131. RenderOperation ro;
  132. IndexData* indexData = new IndexData();
  133. indexData->indexCount = 36;
  134. indexData->indexBuffer = HardwareBufferManager::instance().createIndexBuffer(
  135. HardwareIndexBuffer::IT_16BIT,
  136. 36,
  137. HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  138. unsigned short* idxData = static_cast<unsigned short*>(indexData->indexBuffer->lock(HardwareBuffer::HBL_NORMAL));
  139. idxData[0] = 0; idxData[1] = 1; idxData[2] = 2;
  140. idxData[3] = 2; idxData[4] = 3; idxData[5] = 0;
  141. idxData[6] = 4; idxData[7] = 5; idxData[8] = 6;
  142. idxData[9] = 6; idxData[10] = 7; idxData[11] = 4;
  143. idxData[12] = 0; idxData[13] = 3; idxData[14] = 5;
  144. idxData[15] = 5; idxData[16] = 4; idxData[17] = 0;
  145. idxData[18] = 3; idxData[19] = 2; idxData[20] = 6;
  146. idxData[21] = 6; idxData[22] = 5; idxData[23] = 3;
  147. idxData[24] = 2; idxData[25] = 1; idxData[26] = 7;
  148. idxData[27] = 7; idxData[28] = 6; idxData[29] = 2;
  149. idxData[30] = 1; idxData[31] = 0; idxData[32] = 4;
  150. idxData[33] = 4; idxData[34] = 7; idxData[35] = 1;
  151. indexData->indexBuffer->unlock();
  152. VertexData* vertexData = new VertexData();
  153. vertexData->vertexStart = 0;
  154. vertexData->vertexCount = 8;
  155. VertexDeclaration* decl = vertexData->vertexDeclaration;
  156. decl->removeAllElements();
  157. size_t offset = 0;
  158. decl->addElement(0, offset, VET_FLOAT3, VES_POSITION);
  159. offset += VertexElement::getTypeSize(VET_FLOAT3);
  160. decl->addElement(0, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES);
  161. offset += VertexElement::getTypeSize(VET_FLOAT2);
  162. //decl->addElement(0, offset, VET_COLOUR, VES_DIFFUSE);
  163. //offset += VertexElement::getTypeSize(VET_COLOUR);
  164. HardwareVertexBufferPtr vertexBuffer = HardwareBufferManager::instance().createVertexBuffer(
  165. vertexData->vertexDeclaration->getVertexSize(0),
  166. vertexData->vertexCount,
  167. HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  168. vertexData->vertexBufferBinding->setBinding(0, vertexBuffer);
  169. size_t vertexSize = vertexBuffer->getVertexSize();
  170. char* vertBufferData = static_cast<char*>(vertexBuffer->lock(HardwareBuffer::HBL_NORMAL));
  171. size_t posSize = VertexElement::getTypeSize(VET_FLOAT3);
  172. size_t uvSize = VertexElement::getTypeSize(VET_FLOAT2);
  173. Vector3 position(-5.0f, -5.0f, -5.0f);
  174. memcpy(vertBufferData, &position, posSize);
  175. vertBufferData += posSize;
  176. Vector2 uv(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(0.0f, 1.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(1.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, 0.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, 0.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, 1.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, 1.0f);
  219. memcpy(vertBufferData, &uv, uvSize);
  220. vertBufferData += uvSize;
  221. vertexBuffer->unlock();
  222. ro.indexData = indexData;
  223. ro.vertexData = vertexData;
  224. ro.useIndexes = true;
  225. ro.operationType = RenderOperation::OT_TRIANGLE_LIST;
  226. RenderSystem* renderSystem = RenderSystemManager::getActive();
  227. renderSystem->_setViewport(mViewport);
  228. //Matrix4 projMatrix = mCamera->getProjectionMatrixRS();
  229. //renderSystem->_setProjectionMatrix(projMatrix);
  230. //Matrix4 viewMatrix = mCamera->getViewMatrix(true);
  231. //renderSystem->_setViewMatrix(viewMatrix);
  232. Matrix4 projMatrixCstm = mCamera->getProjectionMatrix();
  233. Matrix4 viewMatrixCstm = mCamera->getViewMatrix(true);
  234. Matrix4 viewProjMatrix = projMatrixCstm * viewMatrixCstm;
  235. renderSystem->setInvertVertexWinding(false);
  236. renderSystem->_setDepthBufferParams();
  237. renderSystem->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH, Color::Blue);
  238. renderSystem->_beginFrame();
  239. mVertProg->getDefaultParameters()->setNamedConstant("matViewProjection", viewProjMatrix);
  240. //renderSystem->bindGpuProgramParameters(GPT_VERTEX_PROGRAM, mVertProg->getDefaultParameters(), GPV_ALL);
  241. renderSystem->_setTexture(0, true, mDbgTexture);
  242. renderSystem->bindGpuProgram(mFragProg->_getBindingDelegate()); // TODO - I don't like this. Shader should be able to be bound directly!
  243. renderSystem->bindGpuProgram(mVertProg->_getBindingDelegate()); // TODO - I don't like this. Shader should be able to be bound directly!
  244. // 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
  245. 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
  246. renderSystem->bindGpuProgramParameters(GPT_VERTEX_PROGRAM, mVertProg->getDefaultParameters(), GPV_ALL);
  247. renderSystem->_render(ro);
  248. renderSystem->_endFrame();
  249. renderSystem->_swapAllRenderTargetBuffers(false);
  250. }
  251. Application& gApplication()
  252. {
  253. static Application application;
  254. return application;
  255. }
  256. }