TDME2 1.9.121
Object3DBase.cpp
Go to the documentation of this file.
2
3#include <map>
4#include <string>
5#include <vector>
6
7#include <tdme/tdme.h>
25#include <tdme/engine/Engine.h>
26#include <tdme/engine/Timing.h>
27#include <tdme/math/Math.h>
28#include <tdme/math/Matrix4x4.h>
29#include <tdme/math/Vector3.h>
31
32using std::map;
33using std::string;
34using std::to_string;
35using std::vector;
36
61
62Object3DBase::Object3DBase(Model* model, bool useManagers, Engine::AnimationProcessingTarget animationProcessingTarget, int instances)
63{
64 this->model = model;
65 this->animationProcessingTarget = animationProcessingTarget;
66 this->usesManagers = useManagers;
67 this->instances = instances;
69 this->currentInstance = 0;
70 instanceAnimations.resize(instances);
73 for (auto i = 0; i < instances; i++) {
74 instanceEnabled[i] = true;
76 }
78 // object 3d nodes
80 // do initial transformations if doing CPU no rendering for deriving bounding boxes and such
81 if (animationProcessingTarget == Engine::AnimationProcessingTarget::CPU_NORENDERING) Object3DNode::computeTransformations(0, object3dNodes);
82}
83
85 for (auto i = 0; i < instances; i++) {
86 delete instanceAnimations[i];
87 }
88 for (auto i = 0; i < object3dNodes.size(); i++) {
89 delete object3dNodes[i];
90 }
92}
93
95 return object3dNodes.size();
96}
97
98void Object3DBase::getTriangles(vector<Triangle>& triangles, int nodeIdx)
99{
100 if (nodeIdx == -1) {
101 for (auto object3DNode : object3dNodes) {
102 auto nodeVerticesTransformed = &object3DNode->mesh->transformedVertices;
103 for (auto& facesEntity : object3DNode->node->getFacesEntities())
104 for (auto& face : facesEntity.getFaces()) {
105 auto faceVertexIndices = face.getVertexIndices();
106 triangles.push_back(
107 Triangle(
108 (*nodeVerticesTransformed)[faceVertexIndices[0]],
109 (*nodeVerticesTransformed)[faceVertexIndices[1]],
110 (*nodeVerticesTransformed)[faceVertexIndices[2]]
111 )
112 );
113 }
114 }
115 } else {
116 auto object3DNode = object3dNodes[nodeIdx];
117 auto nodeVerticesTransformed = &object3DNode->mesh->transformedVertices;
118 for (auto& facesEntity : object3DNode->node->getFacesEntities())
119 for (auto& face : facesEntity.getFaces()) {
120 auto faceVertexIndices = face.getVertexIndices();
121 triangles.push_back(
122 Triangle(
123 (*nodeVerticesTransformed)[faceVertexIndices[0]],
124 (*nodeVerticesTransformed)[faceVertexIndices[1]],
125 (*nodeVerticesTransformed)[faceVertexIndices[2]]
126 )
127 );
128 }
129 }
130}
131
133{
134 if (transformedFacesIterator == nullptr) {
136 }
138}
139
141{
142 // TODO: maybe rather use a hash map than an array to have a faster access
143 for (auto object3DNode : object3dNodes) {
144 if (object3DNode->node->getId() == nodeId) {
145 return object3DNode->mesh;
146 }
147 }
148 return nullptr;
149}
150
152{
153 auto meshManager = Engine::getInstance()->getMeshManager();
154 //
155 // init mesh
156 for (auto i = 0; i < object3dNodes.size(); i++) {
157 auto object3DNode = object3dNodes[i];
158 // initiate mesh if not yet done, happens usually after disposing from engine and readding to engine
159 if (object3DNode->mesh == nullptr) {
160 vector<map<string, Matrix4x4*>*> instancesTransformationsMatrices;
161 vector<map<string, Matrix4x4*>*> instancesSkinningNodesMatrices;
162 for (auto animation: object3DNode->object->instanceAnimations) {
163 instancesTransformationsMatrices.push_back(&animation->transformationsMatrices[0]);
164 instancesSkinningNodesMatrices.push_back(animation->getSkinningNodesMatrices(object3DNode->node));
165 }
166 if (usesManagers == true) {
167 object3DNode->mesh = meshManager->getMesh(object3DNode->id);
168 if (object3DNode->mesh == nullptr) {
169 object3DNode->mesh = new Object3DNodeMesh(
170 object3DNode->renderer,
172 object3DNode->node,
173 instancesTransformationsMatrices,
174 instancesSkinningNodesMatrices,
176 );
177 }
178 } else {
179 object3DNode->mesh = new Object3DNodeMesh(
180 object3DNode->renderer,
182 object3DNode->node,
183 instancesTransformationsMatrices,
184 instancesSkinningNodesMatrices,
186 );
187 }
188 }
189 }
190}
191
193{
194 auto meshManager = Engine::getInstance()->getMeshManager();
195 // dispose mesh
196 for (auto i = 0; i < object3dNodes.size(); i++) {
197 auto object3DNode = object3dNodes[i];
198 // dispose renderer
199 object3DNode->renderer->dispose();
200 // dispose object3d node
201 object3DNode->dispose();
202 // dispose mesh
203 if (usesManagers == true) {
204 meshManager->removeMesh(object3DNode->id);
205 } else {
206 delete object3DNode->mesh;
207 }
208 object3DNode->mesh = nullptr;
209 }
210 //
211}
Engine main class.
Definition: Engine.h:122
static MeshManager * getMeshManager()
Definition: Engine.h:386
static Engine * getInstance()
Returns engine instance.
Definition: Engine.h:554
Timing class.
Definition: Timing.h:17
Represents a model face, consisting of vertex, normal, tangent and bitangent vectors,...
Definition: Face.h:19
Node faces entity A node can have multiple entities containing faces and a applied material.
Definition: FacesEntity.h:28
Joint / Bone.
Definition: Joint.h:19
Representation of a 3d model.
Definition: Model.h:32
Model node.
Definition: Node.h:31
Skinning definition for nodes.
Definition: Skinning.h:27
Triangle entity, this is not directly connectable with physics engine.
Definition: Triangle.h:19
void getTriangles(vector< Triangle > &triangles, int nodeIdx=-1)
Retrieves list of triangles of all or given nodes.
virtual void initialize()
Initiates this object3d.
Engine::AnimationProcessingTarget animationProcessingTarget
Definition: Object3DBase.h:58
vector< Transformations > instanceTransformations
Definition: Object3DBase.h:56
vector< Object3DAnimation * > instanceAnimations
Definition: Object3DBase.h:54
Object3DBase_TransformedFacesIterator * transformedFacesIterator
Definition: Object3DBase.h:46
Object3DNodeMesh * getMesh(const string &nodeId)
Returns object3d node mesh object.
Object3DBase_TransformedFacesIterator * getTransformedFacesIterator()
virtual void dispose()
Disposes this object3d.
Object 3D node mesh specifically for rendering.
Object 3d node specifically for rendering.
Definition: Object3DNode.h:39
static void createNodes(Object3DBase *object, bool useManagers, Engine::AnimationProcessingTarget animationProcessingTarget, vector< Object3DNode * > &object3DNodes)
Creates object 3d nodes from given object3d base object.
static void computeTransformations(int contextIdx, vector< Object3DNode * > &object3DNodes)
Applies transformations to meshes for given object 3d nodes.
Standard math functions.
Definition: Math.h:21
4x4 3D Matrix class
Definition: Matrix4x4.h:24
3D vector 3 class
Definition: Vector3.h:22
Console class.
Definition: Console.h:26