1#include "DrawableStaticMeshComponent.h"
12DrawableStaticMeshComponent::~DrawableStaticMeshComponent() =
default;
15void DrawableStaticMeshComponent::Init() {
19void DrawableStaticMeshComponent::Tick(
float elapsedTime) {
23void DrawableStaticMeshComponent::Exterminate() {
33bool DrawableStaticMeshComponent::SetMesh(
const SISurface& meshSurface) {
35 if (m_meshId.m_vertexSize != -1 || m_meshId.m_vertexBuffer != -1)
return false;
37 if (meshSurface.m_meshId.m_vertexSize != -1
38 || meshSurface.m_meshId.m_vertexBuffer != -1) {
39 m_meshId = meshSurface.m_meshId;
44 CreateMeshBuffers(meshSurface);
50void DrawableStaticMeshComponent::CreateMeshBuffers(
const SISurface& surface) {
53 glGenVertexArrays(1, &vertexArray);
54 glBindVertexArray(vertexArray);
57 std::vector<float> vertices;
58 surface.GenerateVertices(vertices);
60 glGenBuffers(1, &vertexBuffer);
61 glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
62 glBufferData(GL_ARRAY_BUFFER,
63 vertices.size() *
sizeof(vertices[0]),
69 int vertexCount = surface.GetVertexCount();
70 int indexCount = surface.GetTriangleIndexCount();
80 std::vector<GLushort> indices(indexCount);
81 surface.GenerateTriangleIndices(indices);
82 glGenBuffers(1, &indexBuffer);
83 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
84 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
85 indexCount * 3 *
sizeof(GLushort),
92 surface.m_meshId.m_vertexBuffer = vertexBuffer;
93 surface.m_meshId.m_vertexSize = vertexCount;
94 surface.m_meshId.m_indexBuffer = indexBuffer;
95 surface.m_meshId.m_indexSize = indexCount;
96 surface.m_meshId.m_vertexArray = vertexArray;
98 m_meshId = surface.m_meshId;
101 glBindBuffer(GL_ARRAY_BUFFER, 0);
102 glBindVertexArray(0);
108 comp->m_meshId = m_meshId;