Chira Engine
A customizable MIT-licensed game engine.
MeshData.cpp
1 #include "MeshData.h"
2 
3 #include <algorithm>
4 #include <string>
5 #include <config/ConEntry.h>
6 #include <math/Matrix.h>
7 
8 using namespace chira;
9 
11  this->handle = Renderer::createMesh(this->vertices, this->indices, MeshDrawMode::STATIC);
12  this->initialized = true;
13 }
14 
16  if (!this->initialized)
17  return;
18  Renderer::updateMesh(&this->handle, this->vertices, this->indices, this->drawMode);
19 }
20 
21 void MeshData::render(glm::mat4 model, MeshCullType cullType /*= MeshCullType::BACK*/) {
22  if (!this->initialized)
23  this->setupForRendering();
24  if (this->material) {
25  this->material->use();
26  if (this->material->getShader()->usesModelMatrix())
27  this->material->getShader()->setUniform("m", model);
28  }
29  Renderer::drawMesh(this->handle, this->depthFunction, cullType);
30 }
31 
32 MeshData::~MeshData() {
33  if (this->initialized) {
34  Renderer::destroyMesh(this->handle);
35  }
36 }
37 
38 SharedPointer<IMaterial> MeshData::getMaterial() const {
39  return this->material;
40 }
41 
42 void MeshData::setMaterial(SharedPointer<IMaterial> newMaterial) {
43  this->material = std::move(newMaterial);
44 }
45 
46 MeshDepthFunction MeshData::getDepthFunction() const {
47  return this->depthFunction;
48 }
49 
50 void MeshData::setDepthFunction(MeshDepthFunction function) {
51  this->depthFunction = function;
52 }
53 
54 std::vector<byte> MeshData::getMeshData(const std::string& meshLoader) const {
55  return IMeshLoader::getMeshLoader(meshLoader)->createMesh(this->vertices, this->indices);
56 }
57 
58 void MeshData::appendMeshData(const std::string& loader, const std::string& identifier) {
59  IMeshLoader::getMeshLoader(loader)->loadMesh(identifier, this->vertices, this->indices);
60 }
61 
63  this->vertices.clear();
64  this->indices.clear();
65 }
void updateMeshData()
Updates the vertex buffers with the current mesh data.
Definition: MeshData.cpp:15
void setupForRendering()
Establishes the vertex buffers and copies the current mesh data into them.
Definition: MeshData.cpp:10
void clearMeshData()
Does not call updateMeshData().
Definition: MeshData.cpp:62