TDME2  1.9.200
ObjectInternal.cpp
Go to the documentation of this file.
2 
3 #include <memory>
4 #include <string>
5 #include <unordered_map>
6 
7 #include <tdme/tdme.h>
8 #include <tdme/engine/Color4.h>
12 #include <tdme/engine/model/Node.h>
18 #include <tdme/engine/Engine.h>
19 #include <tdme/math/Vector3.h>
20 
21 using std::string;
22 using std::unique_ptr;
23 using std::unordered_map;
24 
38 
39 ObjectInternal::ObjectInternal(const string& id, Model* model, int instances) :
40  ObjectBase(model, true, Engine::animationProcessingTarget, instances)
41 {
42  this->id = id;
43  enabled = true;
44  pickable = false;
45  contributesShadows = false;
46  receivesShadows = false;
47  effectColorMul.set(1.0f, 1.0f, 1.0f, 1.0f);
48  effectColorAdd.set(0.0f, 0.0f, 0.0f, 0.0f);
51 }
52 
54 }
55 
56 void ObjectInternal::bindDiffuseTexture(ColorTexture* texture, const string& nodeId, const string& facesEntityId)
57 {
58  bindDiffuseTexture(texture != nullptr?texture->getColorTextureId():ObjectNode::TEXTUREID_NONE, nodeId, facesEntityId);
59 }
60 
61 void ObjectInternal::unbindDiffuseTexture(const string& nodeId, const string& facesEntityId)
62 {
63  bindDiffuseTexture(ObjectNode::TEXTUREID_NONE, nodeId, facesEntityId);
64 }
65 
66 void ObjectInternal::bindDiffuseTexture(int32_t textureId, const string& nodeId, const string& facesEntityId)
67 {
68  for (auto i = 0; i < objectNodes.size(); i++) {
69  auto objectNode = objectNodes[i];
70  // skip if a node is desired but not matching
71  if (nodeId != "" && nodeId != objectNode->node->getId())
72  continue;
73 
74  const auto& facesEntities = objectNode->node->getFacesEntities();
75  for (auto facesEntityIdx = 0; facesEntityIdx < facesEntities.size(); facesEntityIdx++) {
76  const auto& facesEntity = facesEntities[facesEntityIdx];
77  // skip if a faces entity is desired but not matching
78  if (facesEntityId != "" && facesEntityId != facesEntity.getId())
79  continue;
80  // set dynamic texture id
81  objectNode->specularMaterialDynamicDiffuseTextureIdsByEntities[facesEntityIdx] = textureId;
82  }
83  }
84 }
85 
86 void ObjectInternal::setTextureMatrix(const Matrix3x3& textureMatrix, const string& nodeId, const string& facesEntityId) {
87  for (auto i = 0; i < objectNodes.size(); i++) {
88  auto objectNode = objectNodes[i];
89  // skip if a node is desired but not matching
90  if (nodeId != "" && nodeId != objectNode->node->getId())
91  continue;
92 
93  const auto& facesEntities = objectNode->node->getFacesEntities();
94  for (auto facesEntityIdx = 0; facesEntityIdx < facesEntities.size(); facesEntityIdx++) {
95  const auto& facesEntity = facesEntities[facesEntityIdx];
96  // skip if a faces entity is desired but not matching
97  if (facesEntityId != "" && facesEntityId != facesEntity.getId())
98  continue;
99  // set dynamic texture id
100  objectNode->textureMatricesByEntities[facesEntityIdx].set(textureMatrix);
101  }
102  }
103 }
104 
105 void ObjectInternal::setNodeTransformMatrix(const string& id, const Matrix4x4& matrix) {
108  unordered_map<string, Matrix4x4*> _overriddenTransformMatrices;
109  for (const auto& [overriddenTransformMatrixId, overriddenTransformMatrix]: instanceAnimations[currentInstance]->overriddenTransformMatrices) {
110  _overriddenTransformMatrices[overriddenTransformMatrixId] = new Matrix4x4(*overriddenTransformMatrix);
111  }
112  auto newBoundingBox = unique_ptr<BoundingBox>(ModelUtilitiesInternal::createBoundingBox(this->getModel(), _overriddenTransformMatrices));
113  boundingBox.fromBoundingVolume(newBoundingBox.get());
114 }
115 
119  unordered_map<string, Matrix4x4*> _overriddenTransformMatrices;
120  for (const auto& [overriddenTransformMatrixId, overriddenTransformMatrix]: instanceAnimations[currentInstance]->overriddenTransformMatrices) {
121  _overriddenTransformMatrices[overriddenTransformMatrixId] = new Matrix4x4(*overriddenTransformMatrix);
122  }
123  auto newBoundingBox = unique_ptr<BoundingBox>(ModelUtilitiesInternal::createBoundingBox(this->getModel(), _overriddenTransformMatrices));
124  boundingBox.fromBoundingVolume(newBoundingBox.get());
125 }
126 
128 {
129  instanceTransform[currentInstance].setTransform(transform);
131 }
132 
134 {
136  if (nodeTransformMatrixUpdate == true) {
137  for (auto i = 0; i < objectNodes.size(); i++) {
138  objectNodes[i]->updateNodeTransformationsMatrix();
139  }
141  }
143 }
144 
146  BoundingBox instanceBoundingBox;
149  for (auto i = 1; i < instances; i++) {
151  worldBoundingBox.extend(&instanceBoundingBox);
152  }
153 }
Color 4 definition class.
Definition: Color4.h:18
void set(float r, float g, float b, float a)
Sets this color by its components.
Definition: Color4.h:66
Engine main class.
Definition: Engine.h:131
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
Represents a model face, consisting of vertex, normal, tangent and bitangent vectors,...
Definition: Face.h:18
Node faces entity A node can have multiple entities containing faces and a applied material.
Definition: FacesEntity.h:23
Representation of a 3D model.
Definition: Model.h:35
BoundingBox * getBoundingBox()
Definition: Model.cpp:150
Model node.
Definition: Node.h:32
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
void fromBoundingVolume(BoundingBox *original)
Set up this bounding volume from given bounding volume.
Definition: BoundingBox.cpp:70
void fromBoundingVolumeWithTransform(BoundingBox *original, const Transform &transform)
Create bounding volume from given original(of same type) with applied transform.
Definition: BoundingBox.h:150
void extend(BoundingBox *boundingBox)
Extend bounding box with given bounding box.
Definition: BoundingBox.h:158
static BoundingBox * createBoundingBox(Model *model, const unordered_map< string, Matrix4x4 * > overriddenNodeTransformMatrices=unordered_map< string, Matrix4x4 * >())
Creates a bounding box from given model.
void setNodeTransformMatrix(const string &id, const Matrix4x4 &matrix)
Set transform matrix for given node.
Definition: ObjectBase.h:278
void unsetNodeTransformMatrix(const string &id)
Unset transform matrix for given node.
Definition: ObjectBase.h:286
vector< ObjectAnimation * > instanceAnimations
Definition: ObjectBase.h:55
void setTextureMatrix(const Matrix3x3 &textureMatrix, const string &nodeId=string(), const string &facesEntityId=string())
Set texture matrix.
void setNodeTransformMatrix(const string &id, const Matrix4x4 &matrix)
Set node transform matrix.
void unbindDiffuseTexture(const string &nodeId=string(), const string &facesEntityId=string())
Unbind dynamic texture to a node and faces entity of this object.
void bindDiffuseTexture(int32_t textureId, const string &nodeId=string(), const string &facesEntityId=string())
Bind a texture to a node and faces entity.
void unsetNodeTransformMatrix(const string &id)
Unset node transform matrix.
Object node specifically for rendering.
Definition: ObjectNode.h:41
Matrix3x3 class representing matrix3x3 mathematical structure and operations for 2d space.
Definition: Matrix3x3.h:20
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Color texture interface.
Definition: ColorTexture.h:13
virtual int32_t getColorTextureId()=0