CmApplication.cpp 14 KB

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