1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #include "BsRendererRenderable.h" |
4 | #include "Renderer/BsRendererUtility.h" |
5 | #include "Mesh/BsMesh.h" |
6 | #include "Utility/BsBitwise.h" |
7 | |
8 | namespace bs { namespace ct |
9 | { |
10 | PerObjectParamDef gPerObjectParamDef; |
11 | PerCallParamDef gPerCallParamDef; |
12 | |
13 | void PerObjectBuffer::update(SPtr<GpuParamBlockBuffer>& buffer, const Matrix4& tfrm, const Matrix4& tfrmNoScale, |
14 | UINT32 layer) |
15 | { |
16 | gPerObjectParamDef.gMatWorld.set(buffer, tfrm); |
17 | gPerObjectParamDef.gMatInvWorld.set(buffer, tfrm.inverseAffine()); |
18 | gPerObjectParamDef.gMatWorldNoScale.set(buffer, tfrmNoScale); |
19 | gPerObjectParamDef.gMatInvWorldNoScale.set(buffer, tfrmNoScale.inverseAffine()); |
20 | gPerObjectParamDef.gWorldDeterminantSign.set(buffer, tfrm.determinant3x3() >= 0.0f ? 1.0f : -1.0f); |
21 | gPerObjectParamDef.gLayer.set(buffer, (INT32)layer); |
22 | } |
23 | |
24 | void RenderableElement::draw() const |
25 | { |
26 | if (morphVertexDeclaration == nullptr) |
27 | gRendererUtility().draw(mesh, subMesh); |
28 | else |
29 | gRendererUtility().drawMorph(mesh, subMesh, morphShapeBuffer, morphVertexDeclaration); |
30 | } |
31 | |
32 | RendererRenderable::RendererRenderable() |
33 | { |
34 | perObjectParamBuffer = gPerObjectParamDef.createBuffer(); |
35 | perCallParamBuffer = gPerCallParamDef.createBuffer(); |
36 | } |
37 | |
38 | void RendererRenderable::updatePerObjectBuffer() |
39 | { |
40 | const Matrix4 worldTransform = renderable->getMatrix(); |
41 | const Matrix4 worldNoScaleTransform = renderable->getMatrixNoScale(); |
42 | const UINT32 layer = Bitwise::mostSignificantBit(renderable->getLayer()); |
43 | |
44 | PerObjectBuffer::update(perObjectParamBuffer, worldTransform, worldNoScaleTransform, layer); |
45 | } |
46 | |
47 | void RendererRenderable::updatePerCallBuffer(const Matrix4& viewProj, bool flush) |
48 | { |
49 | const Matrix4 worldViewProjMatrix = viewProj * renderable->getMatrix(); |
50 | |
51 | gPerCallParamDef.gMatWorldViewProj.set(perCallParamBuffer, worldViewProjMatrix); |
52 | |
53 | if(flush) |
54 | perCallParamBuffer->flushToGPU(); |
55 | } |
56 | }} |
57 | |