TDME2  1.9.200
LinesInternal.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 #include <vector>
5 
6 #include <tdme/tdme.h>
7 #include <tdme/engine/Texture.h>
9 #include <tdme/engine/Color4.h>
14 #include <tdme/engine/Engine.h>
15 #include <tdme/engine/Transform.h>
16 #include <tdme/math/Math.h>
17 #include <tdme/math/Matrix4x4.h>
18 #include <tdme/math/Vector3.h>
21 
22 using std::string;
23 using std::vector;
24 
35 using tdme::math::Math;
40 
41 LinesInternal::LinesInternal(const string& id, float lineWidth, const vector<Vector3>& points, const Color4& color, const vector<Color4>& colors, Texture* texture)
42 {
43  this->id = id;
44  this->enabled = true;
45  this->effectColorMul.set(1.0f, 1.0f, 1.0f, 1.0f);
46  this->effectColorAdd.set(0.0f, 0.0f, 0.0f, 0.0f);
47  this->pickable = false;
48  this->contributesShadows = false;
49  this->receivesShadows = false;
50  this->lineWidth = lineWidth;
51  this->points = points;
52  this->color = color;
53  this->colors = colors;
54  this->texture = texture;
55  this->textureId = 0;
57  if (points.size() > 1) {
60  for (const auto& point: points) {
61  boundingBox.extend(point);
62  }
65  }
66  vboIds = nullptr;
67 }
68 
70  if (this->texture != nullptr) texture->releaseReference();
71 }
72 
74 {
76  //
77  auto entityTransform = parentTransform * (*this);
78  entityTransformMatrix = entityTransform.getTransformMatrix();
79  //
81 }
82 
83 void LinesInternal::setTransform(const Transform& transform)
84 {
85  Transform::setTransform(transform);
86  //
87  auto entityTransform = parentTransform * (*this);
88  entityTransformMatrix = entityTransform.getTransformMatrix();
89  //
91 }
92 
94  // texture
95  this->textureId =
96  this->texture == nullptr?
97  engine->getTextureManager()->addTexture(this->texture = TextureReader::read("resources/engine/textures", "point.png"), renderer->CONTEXTINDEX_DEFAULT):
99 
100  // initialize if not yet done
101  auto created = false;
102  auto vboManaged = Engine::getInstance()->getVBOManager()->addVBO(id + ".vbos", 2, true, false, created);
103  vboIds = vboManaged->getVBOIds();
104 
105  //
106  auto contextIdx = renderer->CONTEXTINDEX_DEFAULT;
107 
108  {
109  // upload points
110  auto fbPoints = ObjectBuffer::getByteBuffer(contextIdx, points.size() * 3 * sizeof(float))->asFloatBuffer();
111  for (const auto& point: points) fbPoints.put(point.getArray());
112  renderer->uploadBufferObject(contextIdx, (*vboIds)[0], fbPoints.getPosition() * sizeof(float), &fbPoints);
113  }
114 
115  {
116  // upload colors
117  auto fbColors = ObjectBuffer::getByteBuffer(contextIdx, points.size() * 4 * sizeof(float))->asFloatBuffer();
118  if (colors.size() == points.size()) {
119  for (const auto& color: colors) fbColors.put(color.getArray());
120  } else {
121  for (const auto& point: points) fbColors.put(color.getArray());
122  }
123  renderer->uploadBufferObject(contextIdx, (*vboIds)[1], fbColors.getPosition() * sizeof(float), &fbColors);
124  }
125 }
126 
128 {
130  Engine::getInstance()->getVBOManager()->removeVBO(id + ".vbos");
131 }
Color 4 definition class.
Definition: Color4.h:18
const array< float, 4 > & getArray() const
Definition: Color4.h:262
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
static Engine * getInstance()
Returns engine instance.
Definition: Engine.h:612
static TextureManager * getTextureManager()
Definition: Engine.h:627
static VBOManager * getVBOManager()
Definition: Engine.h:635
Texture entity.
Definition: Texture.h:24
const string & getId() const
Definition: Texture.h:172
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
virtual void setTransform(const Transform &transform)
Set transform.
Definition: Transform.h:177
virtual void update()
Computes transform matrix.
Definition: Transform.cpp:33
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
void extend(BoundingBox *boundingBox)
Extend bounding box with given bounding box.
Definition: BoundingBox.h:158
void update()
Updates this bounding box.
void update() override
Update transform.
void setTransform(const Transform &transform) override
From transform.
void updateBoundingBox()
Update bounding volume.
Definition: LinesInternal.h:83
void removeTexture(Texture *texture)
Removes a texture from manager.
TextureManager_TextureManaged * addTexture(const string &id, bool &created)
Adds a texture to manager.
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 uploadBufferObject(int contextIdx, int32_t bufferObjectId, int32_t size, FloatBuffer *data)=0
Uploads buffer data to buffer object.
Buffers used to transfer data between main memory to graphics board memory.
Definition: ObjectBuffer.h:24
Standard math functions.
Definition: Math.h:19
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Matrix4x4 & identity()
Creates identity matrix.
Definition: Matrix4x4.h:158
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
Byte buffer class.
Definition: ByteBuffer.h:27
Float buffer class.
Definition: FloatBuffer.h:18
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
Definition: Reference.h:38