CSEngine
Loading...
Searching...
No Matches
MeshSurface.cpp
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"
7// #include <iostream>
8
9using namespace CSE;
10
11SURFACE_CONSTRUCTOR(MeshSurface) {}
12
13SURFACE_SUB_CONSTRUCTOR(MeshSurface, int sizeVert, float* vertices, float* normals), m_faceSize(0), m_vertexSize(0),
14 m_indexSize(-1) {
15 MakeVertices(sizeVert, vertices, normals, nullptr, nullptr, nullptr);
16}
17
18
19SURFACE_SUB_CONSTRUCTOR(MeshSurface, int sizeVert, float* vertices, float* normals, float* texCoords), m_faceSize(0),
20 m_vertexSize(0),
21 m_indexSize(-1) {
22 MakeVertices(sizeVert, vertices, normals, texCoords, nullptr, nullptr);
23}
24
25
26MeshSurface::~MeshSurface() = default;
27
28bool MeshSurface::MakeVertices(int sizeVert, float* vertices, float* normals, float* texCoords, float* weights,
29 short* jointIds) {
30 if (!m_Verts.empty()) return false;
31
32 struct Vertex {
33 vec3 Position;
34 vec3 Normal;
35 vec2 TexCoord;
36 vec3 Weight;
37 vec3 JointId;
38 };
39
40 if (jointIds != nullptr) m_meshId.m_hasJoint = true;
41 m_Verts.resize(sizeVert * 14);
42
43 auto vertex_tmp = reinterpret_cast<Vertex*>(&m_Verts[0]);
44
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)++;
49
50 const auto d = vec3::Distance(vec3::Zero, vertex_tmp->Position);
51 if (d > m_meshId.m_maxSize)
52 m_meshId.m_maxSize = d;
53
54 if (normals == nullptr) {
55 vertex_tmp->Normal.Set(0, 0, 0);
56 } else {
57 vertex_tmp->Normal.x = *(normals)++;
58 vertex_tmp->Normal.y = *(normals)++;
59 vertex_tmp->Normal.z = *(normals)++;
60 }
61
62 if (texCoords == nullptr) {
63 vertex_tmp->TexCoord.x = 0;
64 vertex_tmp->TexCoord.y = 0;
65 } else {
66 vertex_tmp->TexCoord.x = *(texCoords)++;
67 vertex_tmp->TexCoord.y = *(texCoords)++;
68 }
69
70 if (weights == nullptr) {
71 vertex_tmp->Weight.Set(0, 0, 0);
72 } else {
73 vertex_tmp->Weight.x = *(weights)++;
74 vertex_tmp->Weight.y = *(weights)++;
75 vertex_tmp->Weight.z = *(weights)++;
76 }
77
78 if (jointIds == nullptr) {
79 vertex_tmp->JointId.Set(0, 0, 0);
80 } else {
81 vertex_tmp->JointId.x = *(jointIds)++;
82 vertex_tmp->JointId.y = *(jointIds)++;
83 vertex_tmp->JointId.z = *(jointIds)++;
84 }
85 vertex_tmp++;
86 }
87 m_vertexSize = sizeVert;
88 return true;
89}
90
91
92bool MeshSurface::MakeIndices(int sizeIndic, int* indices) {
93 if (!m_Indics.empty()) return false;
94
95 m_Indics.resize(sizeIndic * 3);
96
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)++);
101 }
102
103 m_indexSize = sizeIndic;
104 return true;
105}
106
107int MeshSurface::GetVertexCount() const {
108 return m_vertexSize;
109}
110
111int MeshSurface::GetLineIndexCount() const {
112 return -1;
113}
114
115int MeshSurface::GetTriangleIndexCount() const {
116 return m_indexSize;
117}
118
119void MeshSurface::GenerateVertices(std::vector<float>& vertices, unsigned char flags) const {
120 vertices.resize(GetVertexCount() * 14); // xzy + xyz + st
121 vertices = m_Verts;
122
123}
124
125void MeshSurface::GenerateLineIndices(std::vector<unsigned short>& indices) const {
126 indices.resize(GetTriangleIndexCount() * 3); //xyz
127 indices = m_Indics;
128}
129
130void MeshSurface::GenerateTriangleIndices(std::vector<unsigned short>& indices) const {
131 indices.resize(GetTriangleIndexCount() * 3);
132 indices = m_Indics;
133}
134
135bool MeshSurface::HasJoint() const {
136 return m_meshId.m_hasJoint;
137}
138
139vec3 MeshSurface::GenerateTopTriangle(const vec3& v0, const vec3& v1, const vec3& v2) {
140 float height = v1.y - v0.y;
141 float width = 0.0f;
142 vec3 S;
143 vec3 E;
144 vec3 N;
145
146 for (int i = 0; i < (int) height; ++i) {
147 float kCoff = (float) i / height;
148
149 S = LerpFilter(v0, v1, kCoff);
150 E = LerpFilter(v0, v2, kCoff);
151 N = S.Cross(E).Normalized();
152 }
153 return N;
154}
155
156
157vec3 MeshSurface::GenerateBottomTriangle(const vec3& v0, const vec3& v1, const vec3& v2) {
158 float height = v2.y - v0.y;
159 float width = 0.0f;
160 vec3 S;
161 vec3 E;
162 vec3 N;
163
164 for (int i = 0; i < (int) height; ++i) {
165 float kCoff = (float) i / height;
166
167 S = LerpFilter(v0, v2, kCoff);
168 E = LerpFilter(v1, v2, kCoff);
169 N = S.Cross(E).Normalized();
170 }
171 return N;
172}
173
174
175vec3 MeshSurface::LerpFilter(const vec3& v0, const vec3& v1, float kCoff) {
176 vec3 v = v1 * kCoff + (v0 * (1.0f - kCoff));
177 return v;
178}
179
180
181void MeshSurface::Exterminate() {
182 const GLuint vertexBuffer = m_meshId.m_vertexBuffer;
183 const GLuint indexBuffer = m_meshId.m_indexBuffer;
184
185 glDeleteBuffers(1, &vertexBuffer);
186 glDeleteBuffers(1, &indexBuffer);
187}
188
189
190void MeshSurface::Destroy() {
191 CORE->GetCore(MemoryMgr)->ReleaseObject(this);
192}
193
194void MeshSurface::Init(const AssetMgr::AssetReference* asset) {
195 std::string parent_id = split(asset->id, '?')[0];
196 auto model = CORE->GetCore(ResMgr)->GetAssetReference(parent_id);
197 AssetMgr::TYPE type = model->type;
198
199 // 프리팹에 모든 정보가 있으므로 아예 프리팹 새로 생성
200 // 프리팹 객체에서만 모든걸 만들어야 로직이 꼬이지 않기 때문에 해당 예외처리는 허용되지 않음 (삭제 예정)
201// SPrefab* prefab = nullptr;
202//
203// switch (type) {
204// case AssetMgr::DAE:
205// prefab = DAELoader::GeneratePrefab(model->path.c_str(), nullptr, this, nullptr, nullptr);
206// break;
207// default:
208// break;
209// }
210}
211
212void MeshSurface::SetValue(std::string name_str, VariableBinder::Arguments value) {
213}
214
215std::string MeshSurface::PrintValue() const {
216 return {};
217}
void SetValue(std::string name_str, Arguments value) override