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
11MeshSurface::MeshSurface() = default;
12
13MeshSurface::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
19MeshSurface::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 if (normals == nullptr) {
51 vertex_tmp->Normal.Set(0, 0, 0);
52 } else {
53 vertex_tmp->Normal.x = *(normals)++;
54 vertex_tmp->Normal.y = *(normals)++;
55 vertex_tmp->Normal.z = *(normals)++;
56 }
57
58 if (texCoords == nullptr) {
59 vertex_tmp->TexCoord.x = 0;
60 vertex_tmp->TexCoord.y = 0;
61 } else {
62 vertex_tmp->TexCoord.x = *(texCoords)++;
63 vertex_tmp->TexCoord.y = *(texCoords)++;
64 }
65
66 if (weights == nullptr) {
67 vertex_tmp->Weight.Set(0, 0, 0);
68 } else {
69 vertex_tmp->Weight.x = *(weights)++;
70 vertex_tmp->Weight.y = *(weights)++;
71 vertex_tmp->Weight.z = *(weights)++;
72 }
73
74 if (jointIds == nullptr) {
75 vertex_tmp->JointId.Set(0, 0, 0);
76 } else {
77 vertex_tmp->JointId.x = *(jointIds)++;
78 vertex_tmp->JointId.y = *(jointIds)++;
79 vertex_tmp->JointId.z = *(jointIds)++;
80 }
81 vertex_tmp++;
82 }
83 m_vertexSize = sizeVert;
84 return true;
85}
86
87
88bool MeshSurface::MakeIndices(int sizeIndic, int* indices) {
89 if (!m_Indics.empty()) return false;
90
91 m_Indics.resize(sizeIndic * 3);
92
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)++);
97 }
98
99 m_indexSize = sizeIndic;
100 return true;
101}
102
103int MeshSurface::GetVertexCount() const {
104 return m_vertexSize;
105}
106
107int MeshSurface::GetLineIndexCount() const {
108 return -1;
109}
110
111int MeshSurface::GetTriangleIndexCount() const {
112 return m_indexSize;
113}
114
115void MeshSurface::GenerateVertices(std::vector<float>& vertices, unsigned char flags) const {
116 vertices.resize(GetVertexCount() * 14); // xzy + xyz + st
117 vertices = m_Verts;
118
119}
120
121void MeshSurface::GenerateLineIndices(std::vector<unsigned short>& indices) const {
122 indices.resize(GetTriangleIndexCount() * 3); //xyz
123 indices = m_Indics;
124}
125
126void MeshSurface::GenerateTriangleIndices(std::vector<unsigned short>& indices) const {
127 indices.resize(GetTriangleIndexCount() * 3);
128 indices = m_Indics;
129}
130
131bool MeshSurface::HasJoint() const {
132 return m_meshId.m_hasJoint;
133}
134
135vec3 MeshSurface::GenerateTopTriangle(const vec3& v0, const vec3& v1, const vec3& v2) {
136 float height = v1.y - v0.y;
137 float width = 0.0f;
138 vec3 S;
139 vec3 E;
140 vec3 N;
141
142 for (int i = 0; i < (int)height; ++i) {
143 float kCoff = (float)i / height;
144
145 S = LerpFilter(v0, v1, kCoff);
146 E = LerpFilter(v0, v2, kCoff);
147 N = S.Cross(E).Normalized();
148 }
149 return N;
150}
151
152
153vec3 MeshSurface::GenerateBottomTriangle(const vec3& v0, const vec3& v1, const vec3& v2) {
154 float height = v2.y - v0.y;
155 float width = 0.0f;
156 vec3 S;
157 vec3 E;
158 vec3 N;
159
160 for (int i = 0; i < (int)height; ++i) {
161 float kCoff = (float)i / height;
162
163 S = LerpFilter(v0, v2, kCoff);
164 E = LerpFilter(v1, v2, kCoff);
165 N = S.Cross(E).Normalized();
166 }
167 return N;
168}
169
170
171vec3 MeshSurface::LerpFilter(const vec3& v0, const vec3& v1, float kCoff) {
172 vec3 v = v1 * kCoff + (v0 * (1.0f - kCoff));
173 return v;
174}
175
176
177void MeshSurface::Exterminate() {
178 const GLuint vertexBuffer = m_meshId.m_vertexBuffer;
179 const GLuint indexBuffer = m_meshId.m_indexBuffer;
180
181 glDeleteBuffers(1, &vertexBuffer);
182 glDeleteBuffers(1, &indexBuffer);
183}
184
185
186void MeshSurface::Destroy() {
187 CORE->GetCore(MemoryMgr)->ReleaseObject(this);
188}
189
190void MeshSurface::Init(const AssetMgr::AssetReference* asset) {
191 std::string parent_id = split(asset->id, '?')[0];
192 auto model = CORE->GetCore(ResMgr)->GetAssetReference(parent_id);
193 AssetMgr::TYPE type = model->type;
194
195 // 프리팹에 모든 정보가 있으므로 아예 프리팹 새로 생성
196 // 프리팹 객체에서만 모든걸 만들어야 로직이 꼬이지 않기 때문에 해당 예외처리는 허용되지 않음 (삭제 예정)
197// SPrefab* prefab = nullptr;
198//
199// switch (type) {
200// case AssetMgr::DAE:
201// prefab = DAELoader::GeneratePrefab(model->path.c_str(), nullptr, this, nullptr, nullptr);
202// break;
203// default:
204// break;
205// }
206}