Main.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. #include "BsApplication.h"
  2. #include "Material/BsMaterial.h"
  3. #include "CoreThread/BsCoreThread.h"
  4. #include "RenderAPI/BsRenderAPI.h"
  5. #include "RenderAPI/BsRenderWindow.h"
  6. #include "RenderAPI/BsCommandBuffer.h"
  7. #include "RenderAPI/BsGpuProgram.h"
  8. #include "RenderAPI/BsGpuPipelineState.h"
  9. #include "RenderAPI/BsBlendState.h"
  10. #include "RenderAPI/BsDepthStencilState.h"
  11. #include "RenderAPI/BsGpuParamBlockBuffer.h"
  12. #include "RenderAPI/BsIndexBuffer.h"
  13. #include "RenderAPI/BsVertexDataDesc.h"
  14. #include "Mesh/BsMeshData.h"
  15. #include "Math/BsQuaternion.h"
  16. #include "Utility/BsTime.h"
  17. #include "Renderer/BsRendererUtility.h"
  18. #include "BsEngineConfig.h"
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20. // This example uses the low-level rendering API to render a textured cube mesh. This is opposed to using scene objects
  21. // and components, in which case objects are rendered automatically based on their transform and other properties.
  22. //
  23. // Using low-level rendering API gives you full control over rendering, similar to using Vulkan, DirectX or OpenGL APIs.
  24. //
  25. // In order to use the low-level rendering system we need to override the Application class so we get notified of updates
  26. // and start-up/shut-down events. This is normally not necessary for a high level scene object based model.
  27. //
  28. // The rendering is performed on the core (i.e. rendering) thread, as opposed to the main thread, where majority of
  29. // bsf's code executes.
  30. //
  31. // The example first sets up necessary resources, like GPU programs, pipeline state, vertex & index buffers. Then every
  32. // frame it binds the necessary rendering resources and executes the draw call.
  33. //
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. namespace bs
  36. {
  37. UINT32 windowResWidth = 1280;
  38. UINT32 windowResHeight = 720;
  39. // Declare the methods we'll use to do work on the core thread. Note the "ct" namespace, which we use because we render
  40. // on the core thread (ct = core thread). Every object usable on the core thread lives in this namespace.
  41. namespace ct
  42. {
  43. void setup(const SPtr<RenderWindow>& renderWindow);
  44. void render();
  45. void shutdown();
  46. }
  47. // Override the default Application so we can get notified when engine starts-up, shuts-down and when it executes
  48. // every frame
  49. class MyApplication : public Application
  50. {
  51. public:
  52. // Pass along the start-up structure to the parent, we don't need to handle it
  53. MyApplication(const START_UP_DESC& desc)
  54. :Application(desc)
  55. { }
  56. private:
  57. // Called when the engine is first started up
  58. void onStartUp() override
  59. {
  60. // Ensure all parent systems are initialized first
  61. Application::onStartUp();
  62. // Get the primary window that was created during start-up. This will be the final destination for all our
  63. // rendering.
  64. SPtr<RenderWindow> renderWindow = getPrimaryWindow();
  65. // Get the version of the render window usable on the core thread, and send it along to setup()
  66. SPtr<ct::RenderWindow> renderWindowCore = renderWindow->getCore();
  67. // Initialize all the resources we need for rendering. Since we do rendering on a separate thread (the "core
  68. // thread"), we don't call the method directly, but rather queue it for execution using the CoreThread class.
  69. gCoreThread().queueCommand(std::bind(&ct::setup, renderWindowCore));
  70. }
  71. // Called when the engine is about to be shut down
  72. void onShutDown() override
  73. {
  74. // Queue the method for execution on the core thread
  75. gCoreThread().queueCommand(&ct::shutdown);
  76. // Shut-down engine components
  77. Application::onShutDown();
  78. }
  79. // Called every frame, before any other engine system (optionally use postUpdate())
  80. void preUpdate() override
  81. {
  82. // Queue the method for execution on the core thread
  83. gCoreThread().queueCommand(&ct::render);
  84. // Call the default version of this method to handle normal functionality
  85. Application::preUpdate();
  86. }
  87. };
  88. }
  89. // Main entry point into the application
  90. #if BS_PLATFORM == BS_PLATFORM_WIN32
  91. #include <windows.h>
  92. int CALLBACK WinMain(
  93. _In_ HINSTANCE hInstance,
  94. _In_ HINSTANCE hPrevInstance,
  95. _In_ LPSTR lpCmdLine,
  96. _In_ int nCmdShow
  97. )
  98. #else
  99. int main()
  100. #endif
  101. {
  102. using namespace bs;
  103. // Define a video mode for the resolution of the primary rendering window.
  104. VideoMode videoMode(windowResWidth, windowResHeight);
  105. // Start-up the engine using our custom MyApplication class. This will also create the primary rendering window.
  106. // We provide the initial resolution of the window, its title and fullscreen state.
  107. Application::startUp<MyApplication>(videoMode, "bsf Example App", false);
  108. // Runs the main loop that does most of the work. This method will exit when user closes the main
  109. // window or exits in some other way.
  110. Application::instance().runMainLoop();
  111. // Clean up when done
  112. Application::shutDown();
  113. return 0;
  114. }
  115. namespace bs { namespace ct
  116. {
  117. // Declarations for some helper methods we'll use during setup
  118. void writeBoxVertices(const AABox& box, UINT8* positions, UINT8* uvs, UINT32 stride);
  119. void writeBoxIndices(UINT32* indices);
  120. const char* getVertexProgSource();
  121. const char* getFragmentProgSource();
  122. Matrix4 createWorldViewProjectionMatrix();
  123. // Fields where we'll store the resources required during calls to render(). These are initialized in setup()
  124. // and cleaned up in shutDown()
  125. SPtr<GraphicsPipelineState> gPipelineState;
  126. SPtr<Texture> gSurfaceTex;
  127. SPtr<SamplerState> gSurfaceSampler;
  128. SPtr<GpuParams> gGpuParams;
  129. SPtr<VertexDeclaration> gVertexDecl;
  130. SPtr<VertexBuffer> gVertexBuffer;
  131. SPtr<IndexBuffer> gIndexBuffer;
  132. SPtr<RenderTexture> gRenderTarget;
  133. SPtr<RenderWindow> gRenderWindow;
  134. bool gUseHLSL = true;
  135. bool gUseVKSL = false;
  136. const UINT32 NUM_VERTICES = 24;
  137. const UINT32 NUM_INDICES = 36;
  138. // Structure that will hold uniform block variables for the GPU programs
  139. struct UniformBlock
  140. {
  141. Matrix4 gMatWVP; // World view projection matrix
  142. Color gTint; // Tint to apply on top of the texture
  143. };
  144. // Initializes any resources required for rendering
  145. void setup(const SPtr<RenderWindow>& renderWindow)
  146. {
  147. // Determine which shading language to use (depending on the RenderAPI chosen during build)
  148. gUseHLSL = strcmp(BS_RENDER_API_MODULE, "bsfD3D11RenderAPI") == 0;
  149. gUseVKSL = strcmp(BS_RENDER_API_MODULE, "bsfVulkanRenderAPI") == 0;
  150. // This will be the primary output for our rendering (created by the main thread on start-up)
  151. gRenderWindow = renderWindow;
  152. // Create a vertex GPU program
  153. const char* vertProgSrc = getVertexProgSource();
  154. GPU_PROGRAM_DESC vertProgDesc;
  155. vertProgDesc.type = GPT_VERTEX_PROGRAM;
  156. vertProgDesc.entryPoint = "main";
  157. vertProgDesc.language = gUseHLSL ? "hlsl" : gUseVKSL ? "vksl" : "glsl4_1";
  158. vertProgDesc.source = vertProgSrc;
  159. SPtr<GpuProgram> vertProg = GpuProgram::create(vertProgDesc);
  160. // Create a fragment GPU program
  161. const char* fragProgSrc = getFragmentProgSource();
  162. GPU_PROGRAM_DESC fragProgDesc;
  163. fragProgDesc.type = GPT_FRAGMENT_PROGRAM;
  164. fragProgDesc.entryPoint = "main";
  165. fragProgDesc.language = gUseHLSL ? "hlsl" : gUseVKSL ? "vksl" : "glsl4_1";
  166. fragProgDesc.source = fragProgSrc;
  167. SPtr<GpuProgram> fragProg = GpuProgram::create(fragProgDesc);
  168. // Create a graphics pipeline state
  169. BLEND_STATE_DESC blendDesc;
  170. blendDesc.renderTargetDesc[0].blendEnable = true;
  171. blendDesc.renderTargetDesc[0].renderTargetWriteMask = 0b0111; // RGB, don't write to alpha
  172. blendDesc.renderTargetDesc[0].blendOp = BO_ADD;
  173. blendDesc.renderTargetDesc[0].srcBlend = BF_SOURCE_ALPHA;
  174. blendDesc.renderTargetDesc[0].dstBlend = BF_INV_SOURCE_ALPHA;
  175. DEPTH_STENCIL_STATE_DESC depthStencilDesc;
  176. depthStencilDesc.depthWriteEnable = false;
  177. depthStencilDesc.depthReadEnable = false;
  178. PIPELINE_STATE_DESC pipelineDesc;
  179. pipelineDesc.blendState = BlendState::create(blendDesc);
  180. pipelineDesc.depthStencilState = DepthStencilState::create(depthStencilDesc);
  181. pipelineDesc.vertexProgram = vertProg;
  182. pipelineDesc.fragmentProgram = fragProg;
  183. gPipelineState = GraphicsPipelineState::create(pipelineDesc);
  184. // Create an object containing GPU program parameters
  185. gGpuParams = GpuParams::create(gPipelineState);
  186. // Create a vertex declaration for shader inputs
  187. SPtr<VertexDataDesc> vertexDesc = VertexDataDesc::create();
  188. vertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  189. vertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD);
  190. gVertexDecl = VertexDeclaration::create(vertexDesc);
  191. // Create & fill the vertex buffer for a box mesh
  192. UINT32 vertexStride = vertexDesc->getVertexStride();
  193. VERTEX_BUFFER_DESC vbDesc;
  194. vbDesc.numVerts = NUM_VERTICES;
  195. vbDesc.vertexSize = vertexStride;
  196. gVertexBuffer = VertexBuffer::create(vbDesc);
  197. UINT8* vbData = (UINT8*)gVertexBuffer->lock(0, vertexStride * NUM_VERTICES, GBL_WRITE_ONLY_DISCARD);
  198. UINT8* positions = vbData + vertexDesc->getElementOffsetFromStream(VES_POSITION);
  199. UINT8* uvs = vbData + vertexDesc->getElementOffsetFromStream(VES_TEXCOORD);
  200. AABox box(Vector3::ONE * -10.0f, Vector3::ONE * 10.0f);
  201. writeBoxVertices(box, positions, uvs, vertexStride);
  202. gVertexBuffer->unlock();
  203. // Create & fill the index buffer for a box mesh
  204. INDEX_BUFFER_DESC ibDesc;
  205. ibDesc.numIndices = NUM_INDICES;
  206. ibDesc.indexType = IT_32BIT;
  207. gIndexBuffer = IndexBuffer::create(ibDesc);
  208. UINT32* ibData = (UINT32*)gIndexBuffer->lock(0, NUM_INDICES * sizeof(UINT32), GBL_WRITE_ONLY_DISCARD);
  209. writeBoxIndices(ibData);
  210. gIndexBuffer->unlock();
  211. // Create a simple 2x2 checkerboard texture to map to the object we're about to render
  212. SPtr<PixelData> pixelData = PixelData::create(2, 2, 1, PF_RGBA8);
  213. pixelData->setColorAt(Color::White, 0, 0);
  214. pixelData->setColorAt(Color::Black, 1, 0);
  215. pixelData->setColorAt(Color::White, 1, 1);
  216. pixelData->setColorAt(Color::Black, 0, 1);
  217. gSurfaceTex = Texture::create(pixelData);
  218. // Create a sampler state for the texture above
  219. SAMPLER_STATE_DESC samplerDesc;
  220. samplerDesc.minFilter = FO_POINT;
  221. samplerDesc.magFilter = FO_POINT;
  222. gSurfaceSampler = SamplerState::create(samplerDesc);
  223. // Create a color attachment texture for the render surface
  224. TEXTURE_DESC colorAttDesc;
  225. colorAttDesc.width = windowResWidth;
  226. colorAttDesc.height = windowResHeight;
  227. colorAttDesc.format = PF_RGBA8;
  228. colorAttDesc.usage = TU_RENDERTARGET;
  229. SPtr<Texture> colorAtt = Texture::create(colorAttDesc);
  230. // Create a depth attachment texture for the render surface
  231. TEXTURE_DESC depthAttDesc;
  232. depthAttDesc.width = windowResWidth;
  233. depthAttDesc.height = windowResHeight;
  234. depthAttDesc.format = PF_D32;
  235. depthAttDesc.usage = TU_DEPTHSTENCIL;
  236. SPtr<Texture> depthAtt = Texture::create(depthAttDesc);
  237. // Create the render surface
  238. RENDER_TEXTURE_DESC desc;
  239. desc.colorSurfaces[0].texture = colorAtt;
  240. desc.depthStencilSurface.texture = depthAtt;
  241. gRenderTarget = RenderTexture::create(desc);
  242. }
  243. // Render the box, called every frame
  244. void render()
  245. {
  246. // Fill out the uniform block variables
  247. UniformBlock uniformBlock;
  248. uniformBlock.gMatWVP = createWorldViewProjectionMatrix();
  249. uniformBlock.gTint = Color(1.0f, 1.0f, 1.0f, 0.5f);
  250. // Create a uniform block buffer for holding the uniform variables
  251. SPtr<GpuParamBlockBuffer> uniformBuffer = GpuParamBlockBuffer::create(sizeof(UniformBlock));
  252. uniformBuffer->write(0, &uniformBlock, sizeof(uniformBlock));
  253. // Assign the uniform buffer & texture
  254. gGpuParams->setParamBlockBuffer(GPT_FRAGMENT_PROGRAM, "Params", uniformBuffer);
  255. gGpuParams->setParamBlockBuffer(GPT_VERTEX_PROGRAM, "Params", uniformBuffer);
  256. gGpuParams->setTexture(GPT_FRAGMENT_PROGRAM, "gMainTexture", gSurfaceTex);
  257. // HLSL uses separate sampler states, so we need to use a different name for the sampler
  258. if(gUseHLSL)
  259. gGpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gMainTexSamp", gSurfaceSampler);
  260. else
  261. gGpuParams->setSamplerState(GPT_FRAGMENT_PROGRAM, "gMainTexture", gSurfaceSampler);
  262. // Create a command buffer
  263. SPtr<CommandBuffer> cmds = CommandBuffer::create(GQT_GRAPHICS);
  264. // Get the primary render API access point
  265. RenderAPI& rapi = RenderAPI::instance();
  266. // Bind render surface & clear it
  267. rapi.setRenderTarget(gRenderTarget, 0, RT_NONE, cmds);
  268. rapi.clearRenderTarget(FBT_COLOR | FBT_DEPTH, Color::Blue, 1, 0, 0xFF, cmds);
  269. // Bind the pipeline state
  270. rapi.setGraphicsPipeline(gPipelineState, cmds);
  271. // Set the vertex & index buffers, as well as vertex declaration and draw type
  272. rapi.setVertexBuffers(0, &gVertexBuffer, 1, cmds);
  273. rapi.setIndexBuffer(gIndexBuffer, cmds);
  274. rapi.setVertexDeclaration(gVertexDecl, cmds);
  275. rapi.setDrawOperation(DOT_TRIANGLE_LIST, cmds);
  276. // Bind the GPU program parameters (i.e. resource descriptors)
  277. rapi.setGpuParams(gGpuParams, cmds);
  278. // Draw
  279. rapi.drawIndexed(0, NUM_INDICES, 0, NUM_VERTICES, 1, cmds);
  280. // Submit the command buffer
  281. rapi.submitCommandBuffer(cmds);
  282. // Blit the image from the render texture, to the render window
  283. rapi.setRenderTarget(gRenderWindow);
  284. // Get the color attachment
  285. SPtr<Texture> colorTexture = gRenderTarget->getColorTexture(0);
  286. // Use the helper RendererUtility to draw a full-screen quad of the provided texture and output it to the currently
  287. // bound render target. Internally this uses the same calls we used above, just with a different pipeline and mesh.
  288. gRendererUtility().blit(colorTexture);
  289. // Present the rendered image to the user
  290. rapi.swapBuffers(gRenderWindow);
  291. }
  292. // Clean up any resources
  293. void shutdown()
  294. {
  295. gPipelineState = nullptr;
  296. gSurfaceTex = nullptr;
  297. gGpuParams = nullptr;
  298. gVertexDecl = nullptr;
  299. gVertexBuffer = nullptr;
  300. gIndexBuffer = nullptr;
  301. gRenderTarget = nullptr;
  302. gRenderWindow = nullptr;
  303. gSurfaceSampler = nullptr;
  304. }
  305. /////////////////////////////////////////////////////////////////////////////////////
  306. //////////////////////////////////HELPER METHODS/////////////////////////////////////
  307. /////////////////////////////////////////////////////////////////////////////////////
  308. void writeBoxVertices(const AABox& box, UINT8* positions, UINT8* uvs, UINT32 stride)
  309. {
  310. AABox::Corner vertOrder[] =
  311. {
  312. AABox::NEAR_LEFT_BOTTOM, AABox::NEAR_RIGHT_BOTTOM, AABox::NEAR_RIGHT_TOP, AABox::NEAR_LEFT_TOP,
  313. AABox::FAR_RIGHT_BOTTOM, AABox::FAR_LEFT_BOTTOM, AABox::FAR_LEFT_TOP, AABox::FAR_RIGHT_TOP,
  314. AABox::FAR_LEFT_BOTTOM, AABox::NEAR_LEFT_BOTTOM, AABox::NEAR_LEFT_TOP, AABox::FAR_LEFT_TOP,
  315. AABox::NEAR_RIGHT_BOTTOM, AABox::FAR_RIGHT_BOTTOM, AABox::FAR_RIGHT_TOP, AABox::NEAR_RIGHT_TOP,
  316. AABox::FAR_LEFT_TOP, AABox::NEAR_LEFT_TOP, AABox::NEAR_RIGHT_TOP, AABox::FAR_RIGHT_TOP,
  317. AABox::FAR_LEFT_BOTTOM, AABox::FAR_RIGHT_BOTTOM, AABox::NEAR_RIGHT_BOTTOM, AABox::NEAR_LEFT_BOTTOM
  318. };
  319. for (auto& entry : vertOrder)
  320. {
  321. Vector3 pos = box.getCorner(entry);
  322. memcpy(positions, &pos, sizeof(pos));
  323. positions += stride;
  324. }
  325. for (UINT32 i = 0; i < 6; i++)
  326. {
  327. Vector2 uv;
  328. uv = Vector2(0.0f, 1.0f);
  329. memcpy(uvs, &uv, sizeof(uv));
  330. uvs += stride;
  331. uv = Vector2(1.0f, 1.0f);
  332. memcpy(uvs, &uv, sizeof(uv));
  333. uvs += stride;
  334. uv = Vector2(1.0f, 0.0f);
  335. memcpy(uvs, &uv, sizeof(uv));
  336. uvs += stride;
  337. uv = Vector2(0.0f, 0.0f);
  338. memcpy(uvs, &uv, sizeof(uv));
  339. uvs += stride;
  340. }
  341. }
  342. void writeBoxIndices(UINT32* indices)
  343. {
  344. for (UINT32 face = 0; face < 6; face++)
  345. {
  346. UINT32 faceVertOffset = face * 4;
  347. indices[face * 6 + 0] = faceVertOffset + 2;
  348. indices[face * 6 + 1] = faceVertOffset + 1;
  349. indices[face * 6 + 2] = faceVertOffset + 0;
  350. indices[face * 6 + 3] = faceVertOffset + 0;
  351. indices[face * 6 + 4] = faceVertOffset + 3;
  352. indices[face * 6 + 5] = faceVertOffset + 2;
  353. }
  354. }
  355. const char* getVertexProgSource()
  356. {
  357. if(gUseHLSL)
  358. {
  359. static const char* src = R"(
  360. cbuffer Params
  361. {
  362. float4x4 gMatWVP;
  363. float4 gTint;
  364. }
  365. void main(
  366. in float3 inPos : POSITION,
  367. in float2 uv : TEXCOORD0,
  368. out float4 oPosition : SV_Position,
  369. out float2 oUv : TEXCOORD0)
  370. {
  371. oPosition = mul(gMatWVP, float4(inPos.xyz, 1));
  372. oUv = uv;
  373. }
  374. )";
  375. return src;
  376. }
  377. else if(gUseVKSL)
  378. {
  379. static const char* src = R"(
  380. layout (binding = 0, std140) uniform Params
  381. {
  382. mat4 gMatWVP;
  383. vec4 gTint;
  384. };
  385. layout (location = 0) in vec3 bs_position;
  386. layout (location = 1) in vec2 bs_texcoord0;
  387. layout (location = 0) out vec2 texcoord0;
  388. out gl_PerVertex
  389. {
  390. vec4 gl_Position;
  391. };
  392. void main()
  393. {
  394. gl_Position = gMatWVP * vec4(bs_position.xyz, 1);
  395. texcoord0 = bs_texcoord0;
  396. }
  397. )";
  398. return src;
  399. }
  400. else
  401. {
  402. static const char* src = R"(
  403. layout (std140) uniform Params
  404. {
  405. mat4 gMatWVP;
  406. vec4 gTint;
  407. };
  408. in vec3 bs_position;
  409. in vec2 bs_texcoord0;
  410. out vec2 texcoord0;
  411. out gl_PerVertex
  412. {
  413. vec4 gl_Position;
  414. };
  415. void main()
  416. {
  417. gl_Position = gMatWVP * vec4(bs_position.xyz, 1);
  418. texcoord0 = bs_texcoord0;
  419. }
  420. )";
  421. return src;
  422. }
  423. }
  424. const char* getFragmentProgSource()
  425. {
  426. if (gUseHLSL)
  427. {
  428. static const char* src = R"(
  429. cbuffer Params
  430. {
  431. float4x4 gMatWVP;
  432. float4 gTint;
  433. }
  434. SamplerState gMainTexSamp : register(s0);
  435. Texture2D gMainTexture : register(t0);
  436. float4 main(in float4 inPos : SV_Position, float2 uv : TEXCOORD0) : SV_Target
  437. {
  438. float4 color = gMainTexture.Sample(gMainTexSamp, uv);
  439. return color * gTint;
  440. }
  441. )";
  442. return src;
  443. }
  444. else if(gUseVKSL)
  445. {
  446. static const char* src = R"(
  447. layout (binding = 0, std140) uniform Params
  448. {
  449. mat4 gMatWVP;
  450. vec4 gTint;
  451. };
  452. layout (binding = 1) uniform sampler2D gMainTexture;
  453. layout (location = 0) in vec2 texcoord0;
  454. layout (location = 0) out vec4 fragColor;
  455. void main()
  456. {
  457. vec4 color = texture(gMainTexture, texcoord0.st);
  458. fragColor = color * gTint;
  459. }
  460. )";
  461. return src;
  462. }
  463. else
  464. {
  465. static const char* src = R"(
  466. layout (std140) uniform Params
  467. {
  468. mat4 gMatWVP;
  469. vec4 gTint;
  470. };
  471. uniform sampler2D gMainTexture;
  472. in vec2 texcoord0;
  473. out vec4 fragColor;
  474. void main()
  475. {
  476. vec4 color = texture(gMainTexture, texcoord0.st);
  477. fragColor = color * gTint;
  478. }
  479. )";
  480. return src;
  481. }
  482. }
  483. Matrix4 createWorldViewProjectionMatrix()
  484. {
  485. Matrix4 proj = Matrix4::projectionPerspective(Degree(75.0f), 16.0f / 9.0f, 0.05f, 1000.0f);
  486. bs::RenderAPI::convertProjectionMatrix(proj, proj);
  487. Vector3 cameraPos = Vector3(0.0f, -20.0f, 50.0f);
  488. Vector3 lookDir = -Vector3::normalize(cameraPos);
  489. Quaternion cameraRot(BsIdentity);
  490. cameraRot.lookRotation(lookDir);
  491. Matrix4 view = Matrix4::view(cameraPos, cameraRot);
  492. Quaternion rotation(Vector3::UNIT_Y, Degree(gTime().getTime() * 90.0f));
  493. Matrix4 world = Matrix4::TRS(Vector3::ZERO, rotation, Vector3::ONE);
  494. Matrix4 viewProj = proj * view * world;
  495. // GLSL uses column major matrices, so transpose
  496. if(!gUseHLSL)
  497. viewProj = viewProj.transpose();
  498. return viewProj;
  499. }
  500. }}