CmApplication.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. #include "CmMaterial.h"
  22. #include "CmShader.h"
  23. #include "CmTechnique.h"
  24. #include "CmPass.h"
  25. namespace CamelotEngine
  26. {
  27. Application::Application()
  28. :mRenderWindow(nullptr), mCamera(nullptr)
  29. { }
  30. void Application::startUp(String renderSystemDll)
  31. {
  32. Time::startUp(new Time());
  33. Input::startUp(new Input());
  34. DynLibManager::startUp(new DynLibManager());
  35. HighLevelGpuProgramManager::startUp(new HighLevelGpuProgramManager());
  36. RenderSystemManager::initialize(renderSystemDll);
  37. RenderSystem* renderSystem = RenderSystemManager::getActive();
  38. renderSystem->_initialise(false, "Camelot Renderer");
  39. SceneManager::startUp(new SceneManager());
  40. mRenderWindow = renderSystem->_createRenderWindow("Camelot Renderer", 800, 600, false);
  41. //renderSystem->setAmbientLight(1.0f, 1.0f, 1.0f);
  42. renderSystem->setLightingEnabled(false);
  43. mCameraGO = GameObject::create("MainCamera");
  44. mCamera = mCameraGO->addComponent<Camera>();
  45. mCamera->init(mRenderWindow, 0.0f, 0.0f, 1.0f, 1.0f, 0);
  46. mCameraGO->setPosition(Vector3(0,50,1240));
  47. mCameraGO->lookAt(Vector3(0,50,-300));
  48. mCamera->setNearClipDistance(5);
  49. mCamera->setAspectRatio(800.0f / 600.0f);
  50. /////////////////// HLSL SHADERS //////////////////////////
  51. String fragShaderCode = "sampler2D tex; \
  52. float4 ps_main(float2 uv : TEXCOORD0) : COLOR0 \
  53. { \
  54. float4 color = tex2D(tex, uv); \
  55. return color; \
  56. }";
  57. mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "ps_main", "hlsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  58. mFragProg->load();
  59. String vertShaderCode = "float4x4 matViewProjection; \
  60. void vs_main( \
  61. float4 inPos : POSITION, \
  62. float2 uv : TEXCOORD0, \
  63. out float4 oPosition : POSITION, \
  64. out float2 oUv : TEXCOORD0) \
  65. { \
  66. oPosition = mul(matViewProjection, inPos); \
  67. oUv = uv; \
  68. }";
  69. mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "vs_main", "hlsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  70. mVertProg->load();
  71. /////////////////// CG SHADERS //////////////////////////
  72. //String fragShaderCode = "sampler2D diffuseMap; \
  73. // float4 ps_main(float2 uv : TEXCOORD0) : COLOR0 \
  74. //{ \
  75. // float4 color = tex2D(diffuseMap, uv); \
  76. // return color; \
  77. //}";
  78. //mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "ps_main", "cg", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  79. //mFragProg->load();
  80. //String vertShaderCode = "float4x4 matViewProjection; \
  81. // void vs_main( \
  82. // float4 inPos : POSITION, \
  83. // float2 uv : TEXCOORD0, \
  84. // out float4 oPosition : POSITION, \
  85. // out float2 oUv : TEXCOORD0) \
  86. // { \
  87. // oPosition = mul(matViewProjection, inPos); \
  88. // oUv = uv; \
  89. // }";
  90. //mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "vs_main", "cg", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  91. //mVertProg->load();
  92. ///////////////// GLSL SHADERS ////////////////////////////
  93. //String fragShaderCode = "uniform sampler2D tex; \
  94. // void main() \
  95. // {\
  96. // vec4 texColor = texture2D(tex,gl_TexCoord[0].st);\
  97. // gl_FragColor = texColor; \
  98. // }";
  99. //mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "main", "glsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  100. //mFragProg->load();
  101. //// TODO - Ogres GLSL parsing requires some strict parameter naming, can that be avoided?
  102. //String vertShaderCode = "uniform mat4 matViewProjection; \
  103. // attribute vec4 vertex; \
  104. // void main() \
  105. // { \
  106. // gl_TexCoord[0] = gl_MultiTexCoord0; \
  107. // gl_Position = matViewProjection * vertex; \
  108. // }";
  109. //mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "main", "glsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  110. //mVertProg->load();
  111. //ShaderPtr newShader(new Shader("TestShader"));
  112. //TechniquePtr newTechnique = newShader->addTechnique("GLRenderSystem", "ForwardRenderer");
  113. //PassPtr newPass = newTechnique->addPass();
  114. //newPass->setVertexProgram(mVertProg);
  115. //newPass->setFragmentProgram(mFragProg);
  116. //Material newMat;
  117. //newMat.setShader(newShader);
  118. //newShader.
  119. // IMPORTER TEST
  120. Importer::startUp(new Importer());
  121. loadPlugin("CamelotFreeImgImporter"); // TODO - Load this automatically somehow
  122. loadPlugin("CamelotFBXImporter"); // TODO - Load this automatically somehow
  123. //mDbgTexture = std::static_pointer_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
  124. TextureRef testTex = static_resource_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
  125. mDbgMesh = static_resource_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. loadPlugin("CamelotOISInput"); // TODO - Load this automatically somehow
  134. }
  135. void Application::runMainLoop()
  136. {
  137. while(true)
  138. {
  139. WindowEventUtilities::messagePump();
  140. DBG_renderSimpleFrame();
  141. gTime().update();
  142. gInput().update();
  143. }
  144. }
  145. void Application::shutDown()
  146. {
  147. SceneManager::shutDown();
  148. if(RenderSystemManager::getActive() != nullptr)
  149. RenderSystemManager::getActive()->shutdown();
  150. HighLevelGpuProgramManager::shutDown();
  151. DynLibManager::shutDown();
  152. Resources::shutDown();
  153. Input::shutDown();
  154. Time::shutDown();
  155. }
  156. void Application::loadPlugin(const String& pluginName)
  157. {
  158. String name = pluginName;
  159. #if CM_PLATFORM == CM_PLATFORM_LINUX
  160. // dlopen() does not add .so to the filename, like windows does for .dll
  161. if (name.substr(name.length() - 3, 3) != ".so")
  162. name += ".so";
  163. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  164. // dlopen() does not add .dylib to the filename, like windows does for .dll
  165. if (name.substr(name.length() - 6, 6) != ".dylib")
  166. name += ".dylib";
  167. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  168. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  169. // if you include a relative path then it does not. So, add it to be sure.
  170. if (name.substr(name.length() - 4, 4) != ".dll")
  171. name += ".dll";
  172. #endif
  173. DynLib* library = gDynLibManager().load(name);
  174. if(library != nullptr)
  175. {
  176. typedef const void (*LoadPluginFunc)();
  177. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  178. loadPluginFunc();
  179. }
  180. }
  181. UINT32 Application::getAppWindowId()
  182. {
  183. if(!mRenderWindow)
  184. {
  185. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window is set!");
  186. }
  187. UINT32 windowId;
  188. mRenderWindow->getCustomAttribute("WINDOW", &windowId);
  189. return windowId;
  190. }
  191. void Application::DBG_renderSimpleFrame()
  192. {
  193. RenderOperation ro;
  194. IndexData* indexData = new IndexData();
  195. indexData->indexCount = 36;
  196. indexData->indexBuffer = HardwareBufferManager::instance().createIndexBuffer(
  197. HardwareIndexBuffer::IT_16BIT,
  198. 36,
  199. HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  200. unsigned short* idxData = static_cast<unsigned short*>(indexData->indexBuffer->lock(HardwareBuffer::HBL_NORMAL));
  201. idxData[0] = 0; idxData[1] = 1; idxData[2] = 2;
  202. idxData[3] = 2; idxData[4] = 3; idxData[5] = 0;
  203. idxData[6] = 4; idxData[7] = 5; idxData[8] = 6;
  204. idxData[9] = 6; idxData[10] = 7; idxData[11] = 4;
  205. idxData[12] = 0; idxData[13] = 3; idxData[14] = 5;
  206. idxData[15] = 5; idxData[16] = 4; idxData[17] = 0;
  207. idxData[18] = 3; idxData[19] = 2; idxData[20] = 6;
  208. idxData[21] = 6; idxData[22] = 5; idxData[23] = 3;
  209. idxData[24] = 2; idxData[25] = 1; idxData[26] = 7;
  210. idxData[27] = 7; idxData[28] = 6; idxData[29] = 2;
  211. idxData[30] = 1; idxData[31] = 0; idxData[32] = 4;
  212. idxData[33] = 4; idxData[34] = 7; idxData[35] = 1;
  213. indexData->indexBuffer->unlock();
  214. VertexData* vertexData = new VertexData();
  215. vertexData->vertexStart = 0;
  216. vertexData->vertexCount = 8;
  217. VertexDeclarationPtr decl = vertexData->vertexDeclaration;
  218. decl->removeAllElements();
  219. size_t offset = 0;
  220. decl->addElement(0, offset, VET_FLOAT3, VES_POSITION);
  221. offset += VertexElement::getTypeSize(VET_FLOAT3);
  222. decl->addElement(0, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES);
  223. offset += VertexElement::getTypeSize(VET_FLOAT2);
  224. //decl->addElement(0, offset, VET_COLOUR, VES_DIFFUSE);
  225. //offset += VertexElement::getTypeSize(VET_COLOUR);
  226. HardwareVertexBufferPtr vertexBuffer = HardwareBufferManager::instance().createVertexBuffer(
  227. vertexData->vertexDeclaration->getVertexSize(0),
  228. vertexData->vertexCount,
  229. HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  230. vertexData->vertexBufferBinding->setBinding(0, vertexBuffer);
  231. size_t vertexSize = vertexBuffer->getVertexSize();
  232. char* vertBufferData = static_cast<char*>(vertexBuffer->lock(HardwareBuffer::HBL_NORMAL));
  233. size_t posSize = VertexElement::getTypeSize(VET_FLOAT3);
  234. size_t uvSize = VertexElement::getTypeSize(VET_FLOAT2);
  235. Vector3 position(-5.0f, -5.0f, -5.0f);
  236. memcpy(vertBufferData, &position, posSize);
  237. vertBufferData += posSize;
  238. Vector2 uv(0.0f, 0.0f);
  239. memcpy(vertBufferData, &uv, uvSize);
  240. vertBufferData += uvSize;
  241. position = Vector3(-5.0f, 5.0f, -5.0f);
  242. memcpy(vertBufferData, &position, posSize);
  243. vertBufferData += posSize;
  244. uv = Vector2(0.0f, 1.0f);
  245. memcpy(vertBufferData, &uv, uvSize);
  246. vertBufferData += uvSize;
  247. position = Vector3(5.0f, 5.0f, -5.0f);
  248. memcpy(vertBufferData, &position, posSize);
  249. vertBufferData += posSize;
  250. uv = Vector2(1.0f, 1.0f);
  251. memcpy(vertBufferData, &uv, uvSize);
  252. vertBufferData += uvSize;
  253. position = Vector3(5.0f, -5.0f, -5.0f);
  254. memcpy(vertBufferData, &position, posSize);
  255. vertBufferData += posSize;
  256. uv = Vector2(1.0f, 0.0f);
  257. memcpy(vertBufferData, &uv, uvSize);
  258. vertBufferData += uvSize;
  259. position = Vector3(-5.0f, -5.0f, 5.0f);
  260. memcpy(vertBufferData, &position, posSize);
  261. vertBufferData += posSize;
  262. uv = Vector2(0.0f, 0.0f);
  263. memcpy(vertBufferData, &uv, uvSize);
  264. vertBufferData += uvSize;
  265. position = Vector3(5.0f, -5.0f, 5.0f);
  266. memcpy(vertBufferData, &position, posSize);
  267. vertBufferData += posSize;
  268. uv = Vector2(1.0f, 0.0f);
  269. memcpy(vertBufferData, &uv, uvSize);
  270. vertBufferData += uvSize;
  271. position = Vector3(5.0f, 5.0f, 5.0f);
  272. memcpy(vertBufferData, &position, posSize);
  273. vertBufferData += posSize;
  274. uv = Vector2(1.0f, 1.0f);
  275. memcpy(vertBufferData, &uv, uvSize);
  276. vertBufferData += uvSize;
  277. position = Vector3(-5.0f, 5.0f, 5.0f);
  278. memcpy(vertBufferData, &position, posSize);
  279. vertBufferData += posSize;
  280. uv = Vector2(0.0f, 1.0f);
  281. memcpy(vertBufferData, &uv, uvSize);
  282. vertBufferData += uvSize;
  283. vertexBuffer->unlock();
  284. ro.indexData = indexData;
  285. ro.vertexData = vertexData;
  286. ro.useIndexes = true;
  287. ro.operationType = RenderOperation::OT_TRIANGLE_LIST;
  288. RenderSystem* renderSystem = RenderSystemManager::getActive();
  289. renderSystem->_setViewport(mCamera->getViewport());
  290. //Matrix4 projMatrix = mCamera->getProjectionMatrixRS();
  291. //renderSystem->_setProjectionMatrix(projMatrix);
  292. //Matrix4 viewMatrix = mCamera->getViewMatrix(true);
  293. //renderSystem->_setViewMatrix(viewMatrix);
  294. Matrix4 projMatrixCstm = mCamera->getProjectionMatrix();
  295. Matrix4 viewMatrixCstm = mCamera->getViewMatrix();
  296. Matrix4 viewProjMatrix = projMatrixCstm * viewMatrixCstm;
  297. renderSystem->setInvertVertexWinding(false);
  298. renderSystem->_setDepthBufferParams();
  299. renderSystem->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH, Color::Blue);
  300. renderSystem->_beginFrame();
  301. mVertProg->getDefaultParameters()->setNamedConstant("matViewProjection", viewProjMatrix);
  302. mFragProg->getDefaultParameters()->setNamedConstant("tex", mDbgTexture);
  303. //renderSystem->bindGpuProgramParameters(GPT_VERTEX_PROGRAM, mVertProg->getDefaultParameters(), GPV_ALL);
  304. //renderSystem->_setTexture(0, true, mDbgTexture);
  305. renderSystem->bindGpuProgram(mFragProg->_getBindingDelegate()); // TODO - I don't like this. Shader should be able to be bound directly!
  306. renderSystem->bindGpuProgram(mVertProg->_getBindingDelegate()); // TODO - I don't like this. Shader should be able to be bound directly!
  307. // 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
  308. 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
  309. renderSystem->bindGpuProgramParameters(GPT_VERTEX_PROGRAM, mVertProg->getDefaultParameters(), GPV_ALL);
  310. /*renderSystem->_render(ro);*/
  311. renderSystem->_render(mDbgMesh->getRenderOperation());
  312. renderSystem->_endFrame();
  313. renderSystem->_swapAllRenderTargetBuffers(false);
  314. }
  315. Application& gApplication()
  316. {
  317. static Application application;
  318. return application;
  319. }
  320. }