TDME2  1.9.200
BatchRendererTriangles.cpp
Go to the documentation of this file.
2 
3 #include <array>
4 #include <memory>
5 #include <string>
6 #include <vector>
7 
8 #include <tdme/tdme.h>
13 #include <tdme/engine/Engine.h>
14 #include <tdme/math/Vector2.h>
15 #include <tdme/math/Vector3.h>
18 
19 using std::array;
20 using std::string;
21 using std::to_string;
22 using std::unique_ptr;
23 using std::vector;
24 
35 
36 constexpr int32_t BatchRendererTriangles::VERTEX_COUNT;
37 
38 BatchRendererTriangles::BatchRendererTriangles(Renderer* renderer, int32_t id)
39 {
40  this->id = id;
41  this->renderer = renderer;
42  this->acquired = false;
43  this->vertices = 0;
44  this->fbVertices = (fbVerticesByteBuffer = unique_ptr<ByteBuffer>(ByteBuffer::allocate(VERTEX_COUNT * 3 * sizeof(float))))->asFloatBuffer();
45  this->fbNormals = (fbNormalsByteBuffer = unique_ptr<ByteBuffer>(ByteBuffer::allocate(VERTEX_COUNT * 3 * sizeof(float))))->asFloatBuffer();
46  this->fbTextureCoordinates = (fbTextureCoordinatesByteBuffer = unique_ptr<ByteBuffer>(ByteBuffer::allocate(VERTEX_COUNT * 2 * sizeof(float))))->asFloatBuffer();
47  this->fbModelMatrices = (fbModelMatricesByteBuffer = unique_ptr<ByteBuffer>(ByteBuffer::allocate(1 * 16 * sizeof(float))))->asFloatBuffer();
49  this->fbEffectColorMuls = (fbEffectColorMulsByteBuffer = unique_ptr<ByteBuffer>(ByteBuffer::allocate(1 * 4 * sizeof(float))))->asFloatBuffer();
50  this->fbEffectColorMuls.put(Color4(1.0f, 1.0f, 1.0f, 1.0f).getArray());
51  this->fbEffectColorAdds = (fbEffectColorAddsByteBuffer = unique_ptr<ByteBuffer>(ByteBuffer::allocate(1 * 4 * sizeof(float))))->asFloatBuffer();
52  this->fbEffectColorAdds.put(Color4(0.0f, 0.0f, 0.0f, 0.0f).getArray());
53 }
54 
56 {
57 }
58 
60 {
61  return acquired;
62 }
63 
65 {
66  if (acquired == true)
67  return false;
68 
69  acquired = true;
70  return true;
71 }
72 
74 {
75  acquired = false;
76 }
77 
79 {
80  // initialize if not yet done
81  if (vboIds == nullptr) {
82  auto created = false;
83  auto vboManaged = Engine::getInstance()->getVBOManager()->addVBO("tdme.batchvborenderertriangles." + to_string(id), 6, false, false, created);
84  vboIds = vboManaged->getVBOIds();
85  }
86 }
87 
89 {
90  // skip if no vertex data exists
91  if (fbVertices.getPosition() == 0 || fbNormals.getPosition() == 0 || fbTextureCoordinates.getPosition() == 0) return;
92  // use default context
93  auto contextIdx = renderer->CONTEXTINDEX_DEFAULT;
94  // determine triangles count
95  auto triangles = fbVertices.getPosition() / 3 /*vertices*/ / 3 /*vector components*/;
96  // upload vertices
97  renderer->uploadBufferObject(contextIdx, (*vboIds)[0], fbVertices.getPosition() * sizeof(float), &fbVertices);
98  // upload normals
99  renderer->uploadBufferObject(contextIdx, (*vboIds)[1], fbNormals.getPosition() * sizeof(float), &fbNormals);
100  // upload texture coordinates
102  // bind vertices
103  renderer->bindVerticesBufferObject(contextIdx, (*vboIds)[0]);
104  // bind normals
105  renderer->bindNormalsBufferObject(contextIdx, (*vboIds)[1]);
106  // bind texture coordinates
108  // handle instanced rendering
109  // TODO: check if to move somewhere else
110  if (renderer->isInstancedRenderingAvailable() == true) {
115  renderer->uploadBufferObject(contextIdx, (*vboIds)[3], fbModelMatrices.getPosition() * sizeof(float), &fbModelMatrices);
116  renderer->bindModelMatricesBufferObject(contextIdx, (*vboIds)[3]);
117  renderer->uploadBufferObject(contextIdx, (*vboIds)[4], fbEffectColorMuls.getPosition() * sizeof(float), &fbEffectColorMuls);
118  renderer->bindEffectColorMulsBufferObject(contextIdx, (*vboIds)[4], 1);
119  renderer->uploadBufferObject(contextIdx, (*vboIds)[5], fbEffectColorAdds.getPosition() * sizeof(float), &fbEffectColorAdds);
120  renderer->bindEffectColorAddsBufferObject(contextIdx, (*vboIds)[5], 1);
121 
122  // draw
123  renderer->drawInstancedTrianglesFromBufferObjects(contextIdx, triangles, 0, 1);
124  } else {
125  // draw
126  renderer->drawTrianglesFromBufferObjects(contextIdx, triangles, 0);
127  }
128 }
129 
131 {
132  if (vboIds != nullptr) {
133  Engine::getInstance()->getVBOManager()->removeVBO("tdme.batchvborenderertriangles." + to_string(id));
134  vboIds = nullptr;
135  }
136 }
137 
139 {
140  vertices = 0;
141  fbVertices.clear();
142  fbNormals.clear();
144 }
Color 4 definition class.
Definition: Color4.h:18
const array< float, 4 > & getArray() const
Definition: Color4.h:262
Engine main class.
Definition: Engine.h:131
static Engine * getInstance()
Returns engine instance.
Definition: Engine.h:612
static VBOManager * getVBOManager()
Definition: Engine.h:635
VBOManager_VBOManaged * addVBO(const string &vboId, int32_t ids, bool useGPUMemory, bool shared, bool &created)
Adds a VBO to manager or retrieve VBO if existing.
Definition: VBOManager.cpp:31
void removeVBO(const string &vboId)
Removes a VBO from manager.
Definition: VBOManager.cpp:73
virtual void drawInstancedTrianglesFromBufferObjects(int contextIdx, int32_t triangles, int32_t trianglesOffset, int32_t instances)=0
Draw instanced triangles from buffer objects.
array< float, 4 > & getEffectColorAdd(int contextIdx)
Get effect color add.
Definition: Renderer.h:1141
virtual void bindEffectColorMulsBufferObject(int contextIdx, int32_t bufferObjectId, int32_t divisor)=0
Bind effect color muls buffer object.
array< float, 4 > & getEffectColorMul(int contextIdx)
Get effect color mul.
Definition: Renderer.h:1131
virtual void bindModelMatricesBufferObject(int contextIdx, int32_t bufferObjectId)=0
Bind model matrices buffer object.
virtual bool isInstancedRenderingAvailable()=0
Checks if instanced rendering is available.
virtual void drawTrianglesFromBufferObjects(int contextIdx, int32_t triangles, int32_t trianglesOffset)=0
Draw triangles from buffer objects.
virtual void uploadBufferObject(int contextIdx, int32_t bufferObjectId, int32_t size, FloatBuffer *data)=0
Uploads buffer data to buffer object.
virtual void bindTextureCoordinatesBufferObject(int contextIdx, int32_t bufferObjectId)=0
Bind texture coordinates buffer object.
virtual void bindVerticesBufferObject(int contextIdx, int32_t bufferObjectId)=0
Bind vertices buffer object.
virtual void bindEffectColorAddsBufferObject(int contextIdx, int32_t bufferObjectId, int32_t divisor)=0
Bind effect color adds buffer object.
virtual void bindNormalsBufferObject(int contextIdx, int32_t bufferObjectId)=0
Bind normals buffer object.
Buffers used to transfer data between main memory to graphics board memory.
Definition: ObjectBuffer.h:24
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Matrix4x4 & identity()
Creates identity matrix.
Definition: Matrix4x4.h:158
const array< float, 16 > & getArray() const
Definition: Matrix4x4.h:611
Vector2 class representing vector2 mathematical structure and operations with x, y components.
Definition: Vector2.h:20
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Buffer * clear()
Clear.
Definition: Buffer.h:74
Byte buffer class.
Definition: ByteBuffer.h:27
Float buffer class.
Definition: FloatBuffer.h:18
FloatBuffer * put(float value)
Put a float value into float buffer.
Definition: FloatBuffer.h:67
virtual int64_t getPosition()
Definition: FloatBuffer.h:45