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"
13SURFACE_SUB_CONSTRUCTOR(
MeshSurface,
int sizeVert,
float* vertices,
float* normals), m_faceSize(0), m_vertexSize(0),
15 MakeVertices(sizeVert, vertices, normals,
nullptr,
nullptr,
nullptr);
19SURFACE_SUB_CONSTRUCTOR(
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 const auto d = vec3::Distance(vec3::Zero, vertex_tmp->Position);
51 if (d > m_meshId.m_maxSize)
52 m_meshId.m_maxSize = d;
54 if (normals ==
nullptr) {
55 vertex_tmp->Normal.Set(0, 0, 0);
57 vertex_tmp->Normal.x = *(normals)++;
58 vertex_tmp->Normal.y = *(normals)++;
59 vertex_tmp->Normal.z = *(normals)++;
62 if (texCoords ==
nullptr) {
63 vertex_tmp->TexCoord.x = 0;
64 vertex_tmp->TexCoord.y = 0;
66 vertex_tmp->TexCoord.x = *(texCoords)++;
67 vertex_tmp->TexCoord.y = *(texCoords)++;
70 if (weights ==
nullptr) {
71 vertex_tmp->Weight.Set(0, 0, 0);
73 vertex_tmp->Weight.x = *(weights)++;
74 vertex_tmp->Weight.y = *(weights)++;
75 vertex_tmp->Weight.z = *(weights)++;
78 if (jointIds ==
nullptr) {
79 vertex_tmp->JointId.Set(0, 0, 0);
81 vertex_tmp->JointId.x = *(jointIds)++;
82 vertex_tmp->JointId.y = *(jointIds)++;
83 vertex_tmp->JointId.z = *(jointIds)++;
87 m_vertexSize = sizeVert;
92bool MeshSurface::MakeIndices(
int sizeIndic,
int* indices) {
93 if (!m_Indics.empty())
return false;
95 m_Indics.resize(sizeIndic * 3);
97 for (
int i = 0; i < sizeIndic; ++i) {
98 m_Indics[i * 3] =
static_cast<unsigned short>(*(indices)++);
99 m_Indics[i * 3 + 1] =
static_cast<unsigned short>(*(indices)++);
100 m_Indics[i * 3 + 2] =
static_cast<unsigned short>(*(indices)++);
103 m_indexSize = sizeIndic;
107int MeshSurface::GetVertexCount()
const {
111int MeshSurface::GetLineIndexCount()
const {
115int MeshSurface::GetTriangleIndexCount()
const {
119void MeshSurface::GenerateVertices(std::vector<float>& vertices,
unsigned char flags)
const {
120 vertices.resize(GetVertexCount() * 14);
125void MeshSurface::GenerateLineIndices(std::vector<unsigned short>& indices)
const {
126 indices.resize(GetTriangleIndexCount() * 3);
130void MeshSurface::GenerateTriangleIndices(std::vector<unsigned short>& indices)
const {
131 indices.resize(GetTriangleIndexCount() * 3);
135bool MeshSurface::HasJoint()
const {
136 return m_meshId.m_hasJoint;
139vec3 MeshSurface::GenerateTopTriangle(
const vec3& v0,
const vec3& v1,
const vec3& v2) {
140 float height = v1.y - v0.y;
146 for (
int i = 0; i < (int) height; ++i) {
147 float kCoff = (float) i / height;
149 S = LerpFilter(v0, v1, kCoff);
150 E = LerpFilter(v0, v2, kCoff);
151 N = S.Cross(E).Normalized();
157vec3 MeshSurface::GenerateBottomTriangle(
const vec3& v0,
const vec3& v1,
const vec3& v2) {
158 float height = v2.y - v0.y;
164 for (
int i = 0; i < (int) height; ++i) {
165 float kCoff = (float) i / height;
167 S = LerpFilter(v0, v2, kCoff);
168 E = LerpFilter(v1, v2, kCoff);
169 N = S.Cross(E).Normalized();
175vec3 MeshSurface::LerpFilter(
const vec3& v0,
const vec3& v1,
float kCoff) {
176 vec3 v = v1 * kCoff + (v0 * (1.0f - kCoff));
181void MeshSurface::Exterminate() {
182 const GLuint vertexBuffer = m_meshId.m_vertexBuffer;
183 const GLuint indexBuffer = m_meshId.m_indexBuffer;
185 glDeleteBuffers(1, &vertexBuffer);
186 glDeleteBuffers(1, &indexBuffer);
190void MeshSurface::Destroy() {
191 CORE->GetCore(
MemoryMgr)->ReleaseObject(
this);
195 std::string parent_id = split(asset->id,
'?')[0];
196 auto model = CORE->GetCore(
ResMgr)->GetAssetReference(parent_id);
197 AssetMgr::TYPE type = model->type;
215std::string MeshSurface::PrintValue()
const {
void SetValue(std::string name_str, Arguments value) override