TDME2 1.9.121
Object3DNodeRenderer.cpp
Go to the documentation of this file.
2
3#include <tdme/tdme.h>
12#include <tdme/engine/Engine.h>
13#include <tdme/math/Vector3.h>
16
30
31Object3DNodeRenderer::Object3DNodeRenderer(Object3DNode* object3DNode)
32{
33 this->object3DNode = object3DNode;
34 this->vboBaseIds = nullptr;
35 this->vboNormalMappingIds = nullptr;
36}
37
39{
40 // TODO: maybe store these in Object3DNodeRenderer too
41 auto meshUploaded = true;
42 auto created = false;
43
44 // initialize if not yet done
45 if (vboBaseIds == nullptr) {
48 3 + (object3DNode->mesh->node->getTextureCoordinates().empty() == false?1:0),
49 true,
50 true,
51 created
52 );
53 if (created == false) while (vboManagedBase->isUploaded() == false);
55 meshUploaded = created == false;
56
57 // initialize tangents, bitangents
58 if (Engine::renderer->isNormalMappingAvailable() &&
59 object3DNode->mesh->node->getTangents().empty() == false &&
60 object3DNode->mesh->node->getBitangents().empty() == false &&
61 vboNormalMappingIds == nullptr) {
62 vboManagedNormalMapping = Engine::getInstance()->getVBOManager()->addVBO(object3DNode->id + ".normalmapping", 2, true, true, created);
63 if (created == false) while (vboManagedNormalMapping->isUploaded() == false);
65 }
66
67 // initialize origins
68 if (object3DNode->mesh->node->getOrigins().empty() == false &&
69 vboOrigins == nullptr) {
70 vboManagedOrigins = Engine::getInstance()->getVBOManager()->addVBO(object3DNode->id + ".origins", 1, true, true, created);
71 if (created == false) while (vboManagedOrigins->isUploaded() == false);
73 }
74
75 // initialize tangents, bitangents
76 auto lodLevels = 0;
77 // TODO: we only support faces entities 0 lod indices for terrain now
78 if (object3DNode->mesh->node->getFacesEntities()[0].getLOD1Indices().empty() == false) lodLevels++;
79 if (object3DNode->mesh->node->getFacesEntities()[0].getLOD2Indices().empty() == false) lodLevels++;
80 if (object3DNode->mesh->node->getFacesEntities()[0].getLOD3Indices().empty() == false) lodLevels++;
81 if (lodLevels > 0 &&
82 vboLods == nullptr) {
83 vboManagedLods = Engine::getInstance()->getVBOManager()->addVBO(object3DNode->id + ".lodindices", lodLevels, true, true, created);
84 if (created == false) while (vboManagedLods->isUploaded() == false);
86 }
87 }
88
89 //
90 haveVBOs = true;
91
92 // model node updates
93 auto verticesUpdate = object3DNode->node->hasVerticesUpdate();
94 auto normalsUpdate = object3DNode->node->hasNormalsUpdate();
95
96 // check if to upload new mesh
97 if (object3DNode->mesh->getRecreatedBuffers() == true || meshUploaded == false) {
98 if (meshUploaded == false) {
99 // upload indices
101 // upload texture coordinates
102 if (object3DNode->mesh->node->getTextureCoordinates().empty() == false) {
104 }
105 // upload render node object origins
106 if (object3DNode->mesh->node->getOrigins().empty() == false) {
109 }
110 // TODO: we only support faces entities 0 lod indices for terrain now
111 auto lodLevel = 0;
112 if (object3DNode->mesh->node->getFacesEntities()[0].getLOD1Indices().empty() == false) {
113 object3DNode->mesh->setupLodBuffer(Engine::renderer, contextIdx, (*vboLods)[lodLevel], 1);
114 lodLevel++;
115 }
116 if (object3DNode->mesh->node->getFacesEntities()[0].getLOD2Indices().empty() == false) {
117 object3DNode->mesh->setupLodBuffer(Engine::renderer, contextIdx, (*vboLods)[lodLevel], 2);
118 lodLevel++;
119 }
120 if (object3DNode->mesh->node->getFacesEntities()[0].getLOD3Indices().empty() == false) {
121 object3DNode->mesh->setupLodBuffer(Engine::renderer, contextIdx, (*vboLods)[lodLevel], 3);
122 lodLevel++;
123 }
124 if (vboManagedLods != nullptr) vboManagedLods->setUploaded(true);
125 }
126 // upload vertices
128 // upload normals
130 // tangents, bitangents
131 if (vboNormalMappingIds != nullptr) {
135 }
137 } else
138 if (verticesUpdate == true || normalsUpdate == true) {
139 // upload vertices
140 if (verticesUpdate == true) object3DNode->mesh->setupVerticesBuffer(Engine::renderer, contextIdx, (*vboBaseIds)[1]);
141 // upload normals
142 if (normalsUpdate == true) object3DNode->mesh->setupNormalsBuffer(Engine::renderer, contextIdx, (*vboBaseIds)[2]);
143 }
144}
145
147{
148 if (vboBaseIds != nullptr) {
150 vboBaseIds = nullptr;
151 }
152 if (vboNormalMappingIds != nullptr) {
153 Engine::getInstance()->getVBOManager()->removeVBO(object3DNode->id + ".normalmapping");
154 vboNormalMappingIds = nullptr;
155 }
156 if (vboOrigins != nullptr) {
158 vboOrigins = nullptr;
159 }
160 if (vboLods != nullptr) {
162 vboLods = nullptr;
163 }
164}
Engine main class.
Definition: Engine.h:122
static STATIC_DLL_IMPEXT Renderer * renderer
Definition: Engine.h:184
static Engine * getInstance()
Returns engine instance.
Definition: Engine.h:554
static VBOManager * getVBOManager()
Definition: Engine.h:572
Model node.
Definition: Node.h:31
bool hasVerticesUpdate()
Definition: Node.h:142
const vector< Vector3 > & getTangents() const
Definition: Node.h:199
const vector< Vector3 > & getOrigins() const
Definition: Node.h:276
const vector< Vector3 > & getBitangents() const
Definition: Node.h:212
const vector< TextureCoordinate > & getTextureCoordinates() const
Definition: Node.h:186
const vector< FacesEntity > & getFacesEntities() const
Definition: Node.h:256
Class representing texture UV coordinates data.
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
Object 3D node mesh specifically for rendering.
void setupOriginsBuffer(Renderer *renderer, int contextIdx, int32_t vboId)
Set up render node object origins data buffer.
void setupNormalsBuffer(Renderer *renderer, int contextIdx, int32_t vboId)
Set up normals buffer.
void setupVertexIndicesBuffer(Renderer *renderer, int contextIdx, int32_t vboId)
Set up vertex indices buffer.
void setupLodBuffer(Renderer *renderer, int contextIdx, int32_t vboId, int lodLevel)
Set up render node object lod data buffer.
void setupTextureCoordinatesBuffer(Renderer *renderer, int contextIdx, int32_t vboId)
Set up texture coordinates buffer.
void setupTangentsBuffer(Renderer *renderer, int contextIdx, int32_t vboId)
Set up tangents buffer.
void setupVerticesBuffer(Renderer *renderer, int contextIdx, int32_t vboId)
Set up vertices buffer.
void setupBitangentsBuffer(Renderer *renderer, int contextIdx, int32_t vboId)
Set up bitangents buffer.
void preRender(int contextIdx)
Pre render step like uploading VBOs and such.
Object 3d node specifically for rendering.
Definition: Object3DNode.h:39
3D vector 3 class
Definition: Vector3.h:22
Float buffer class.
Definition: FloatBuffer.h:18
Short buffer class.
Definition: ShortBuffer.h:14