1#include "MeshSurface.h"
2#include "../../Manager/MemoryMgr.h"
3#include "../../OGLDef.h"
4#include "../MoreString.h"
5#include "../../Object/SPrefab.h"
6#include "../../Manager/EngineCore.h"
11MeshSurface::MeshSurface() =
default;
13MeshSurface::MeshSurface(
int sizeVert,
float* vertices,
float* normals) : m_faceSize(0), m_vertexSize(0),
15 MakeVertices(sizeVert, vertices, normals,
nullptr,
nullptr,
nullptr);
19MeshSurface::MeshSurface(
int sizeVert,
float* vertices,
float* normals,
float* texCoords) : m_faceSize(0),
22 MakeVertices(sizeVert, vertices, normals, texCoords,
nullptr,
nullptr);
26MeshSurface::~MeshSurface() =
default;
28bool MeshSurface::MakeVertices(
int sizeVert,
float* vertices,
float* normals,
float* texCoords,
float* weights,
30 if (!m_Verts.empty())
return false;
40 if(jointIds !=
nullptr) m_meshId.m_hasJoint =
true;
41 m_Verts.resize(sizeVert * 14);
43 auto vertex_tmp =
reinterpret_cast<Vertex*
>(&m_Verts[0]);
45 for (
int i = 0; i < sizeVert; ++i) {
46 vertex_tmp->Position.x = *(vertices)++;
47 vertex_tmp->Position.y = *(vertices)++;
48 vertex_tmp->Position.z = *(vertices)++;
50 if (normals ==
nullptr) {
51 vertex_tmp->Normal.Set(0, 0, 0);
53 vertex_tmp->Normal.x = *(normals)++;
54 vertex_tmp->Normal.y = *(normals)++;
55 vertex_tmp->Normal.z = *(normals)++;
58 if (texCoords ==
nullptr) {
59 vertex_tmp->TexCoord.x = 0;
60 vertex_tmp->TexCoord.y = 0;
62 vertex_tmp->TexCoord.x = *(texCoords)++;
63 vertex_tmp->TexCoord.y = *(texCoords)++;
66 if (weights ==
nullptr) {
67 vertex_tmp->Weight.Set(0, 0, 0);
69 vertex_tmp->Weight.x = *(weights)++;
70 vertex_tmp->Weight.y = *(weights)++;
71 vertex_tmp->Weight.z = *(weights)++;
74 if (jointIds ==
nullptr) {
75 vertex_tmp->JointId.Set(0, 0, 0);
77 vertex_tmp->JointId.x = *(jointIds)++;
78 vertex_tmp->JointId.y = *(jointIds)++;
79 vertex_tmp->JointId.z = *(jointIds)++;
83 m_vertexSize = sizeVert;
88bool MeshSurface::MakeIndices(
int sizeIndic,
int* indices) {
89 if (!m_Indics.empty())
return false;
91 m_Indics.resize(sizeIndic * 3);
93 for (
int i = 0; i < sizeIndic; ++i) {
94 m_Indics[i * 3] =
static_cast<unsigned short>(*(indices)++);
95 m_Indics[i * 3 + 1] =
static_cast<unsigned short>(*(indices)++);
96 m_Indics[i * 3 + 2] =
static_cast<unsigned short>(*(indices)++);
99 m_indexSize = sizeIndic;
103int MeshSurface::GetVertexCount()
const {
107int MeshSurface::GetLineIndexCount()
const {
111int MeshSurface::GetTriangleIndexCount()
const {
115void MeshSurface::GenerateVertices(std::vector<float>& vertices,
unsigned char flags)
const {
116 vertices.resize(GetVertexCount() * 14);
121void MeshSurface::GenerateLineIndices(std::vector<unsigned short>& indices)
const {
122 indices.resize(GetTriangleIndexCount() * 3);
126void MeshSurface::GenerateTriangleIndices(std::vector<unsigned short>& indices)
const {
127 indices.resize(GetTriangleIndexCount() * 3);
131bool MeshSurface::HasJoint()
const {
132 return m_meshId.m_hasJoint;
135vec3 MeshSurface::GenerateTopTriangle(
const vec3& v0,
const vec3& v1,
const vec3& v2) {
136 float height = v1.y - v0.y;
142 for (
int i = 0; i < (int)height; ++i) {
143 float kCoff = (float)i / height;
145 S = LerpFilter(v0, v1, kCoff);
146 E = LerpFilter(v0, v2, kCoff);
147 N = S.Cross(E).Normalized();
153vec3 MeshSurface::GenerateBottomTriangle(
const vec3& v0,
const vec3& v1,
const vec3& v2) {
154 float height = v2.y - v0.y;
160 for (
int i = 0; i < (int)height; ++i) {
161 float kCoff = (float)i / height;
163 S = LerpFilter(v0, v2, kCoff);
164 E = LerpFilter(v1, v2, kCoff);
165 N = S.Cross(E).Normalized();
171vec3 MeshSurface::LerpFilter(
const vec3& v0,
const vec3& v1,
float kCoff) {
172 vec3 v = v1 * kCoff + (v0 * (1.0f - kCoff));
177void MeshSurface::Exterminate() {
178 const GLuint vertexBuffer = m_meshId.m_vertexBuffer;
179 const GLuint indexBuffer = m_meshId.m_indexBuffer;
181 glDeleteBuffers(1, &vertexBuffer);
182 glDeleteBuffers(1, &indexBuffer);
186void MeshSurface::Destroy() {
187 CORE->GetCore(
MemoryMgr)->ReleaseObject(
this);
191 std::string parent_id = split(asset->id,
'?')[0];
192 auto model = CORE->GetCore(
ResMgr)->GetAssetReference(parent_id);
193 AssetMgr::TYPE type = model->type;