-
Notifications
You must be signed in to change notification settings - Fork 0
/
computeMikkTSpace.cpp
125 lines (89 loc) · 4.18 KB
/
computeMikkTSpace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "computeMikkTSpace.h"
#include <spdlog/spdlog.h>
#include "vertex_config.h"
#include <glm/gtx/string_cast.hpp>
#define CALC_TANGENTS_DEBUG 0
CalcTangents::CalcTangents() {
iface.m_getNumFaces = get_num_faces;
iface.m_getNumVerticesOfFace = get_num_vertices_of_face;
iface.m_getNormal = get_normal;
iface.m_getPosition = get_position;
iface.m_getTexCoord = get_tex_coords;
iface.m_setTSpaceBasic = set_tspace_basic;
context.m_pInterface = &iface;
}
void CalcTangents::calc(primMeshGLTF *mesh) {
context.m_pUserData = mesh;
//if (CALC_TANGENTS_DEBUG) {
// spdlog::debug("[CalcTangents] with Mesh: {}", mesh->name);
//}
genTangSpaceDefault(&this->context);
}
int CalcTangents::get_num_faces(const SMikkTSpaceContext *context) {
primMeshGLTF *working_primMeshGLTF = static_cast<primMeshGLTF*>(context->m_pUserData);
float f_size = (float)working_primMeshGLTF->indices.size() / 3.f;
int i_size = (int)working_primMeshGLTF->indices.size() / 3;
assert((f_size - (float)i_size) == 0.f);
//if (CALC_TANGENTS_DEBUG) {
// spdlog::debug("[CalcTangents] get_num_faces: {}", i_size);
//}
return i_size;
}
int CalcTangents::get_num_vertices_of_face(const SMikkTSpaceContext *context, const int iFace) {
primMeshGLTF *working_primMeshGLTF = static_cast<primMeshGLTF*>(context->m_pUserData);
// if (working_primMeshGLTF->draw_mode == GL_TRIANGLES) {
return 3;
// }
// throw std::logic_error("no vertices with less than 3 and more than 3 supported");
}
void CalcTangents::get_position(const SMikkTSpaceContext *context, float *outpos, const int iFace, const int iVert) {
primMeshGLTF *working_primMeshGLTF = static_cast<primMeshGLTF*>(context->m_pUserData);
const auto index = get_vertex_index(context, iFace, iVert);
const auto &vertex = working_primMeshGLTF->vertices[index];
//if (CALC_TANGENTS_DEBUG) {
// spdlog::debug("[CalcTangents] get_position({}): {}", index, glm::to_string(vertex.pos));
//}
outpos[0] = vertex.pos.x;
outpos[1] = vertex.pos.y;
outpos[2] = vertex.pos.z;
}
void CalcTangents::get_normal(const SMikkTSpaceContext *context, float *outnormal, const int iFace, const int iVert) {
primMeshGLTF *working_primMeshGLTF = static_cast<primMeshGLTF*>(context->m_pUserData);
const auto index = get_vertex_index(context, iFace, iVert);
const auto vertex = working_primMeshGLTF->vertices[index];
//if (CALC_TANGENTS_DEBUG) {
// spdlog::debug("[CalcTangents] get_normal({}): {}", index, glm::to_string(vertex.norm));
//}
outnormal[0] = vertex.norm.x;
outnormal[1] = vertex.norm.y;
outnormal[2] = vertex.norm.z;
}
void CalcTangents::get_tex_coords(const SMikkTSpaceContext *context, float *outuv, const int iFace, const int iVert) {
primMeshGLTF *working_primMeshGLTF = static_cast<primMeshGLTF*>(context->m_pUserData);
const auto index = get_vertex_index(context, iFace, iVert);
const auto vertex = working_primMeshGLTF->vertices[index];
//if (CALC_TANGENTS_DEBUG) {
// spdlog::debug("[CalcTangents] get_tex_coords({}): {}", index, glm::to_string(vertex.texCoord0));
//}
outuv[0] = vertex.texCoord0.x;
outuv[1] = vertex.texCoord0.y;
}
void CalcTangents::set_tspace_basic(const SMikkTSpaceContext *context, const float *tangentu, const float fSign, const int iFace, const int iVert) {
primMeshGLTF *working_primMeshGLTF = static_cast<primMeshGLTF*>(context->m_pUserData);
const auto index = get_vertex_index(context, iFace, iVert);
auto *vertex = &working_primMeshGLTF->vertices[index];
vertex->tangent.x = tangentu[0];
vertex->tangent.y = tangentu[1];
vertex->tangent.z = tangentu[2];
vertex->tangent.w = fSign;
//if (CALC_TANGENTS_DEBUG) {
// spdlog::debug("[CalcTangents] set_tspace_basic({}) fSign:{} {}", index, fSign, glm::to_string(vertex->tangent));
//}
}
const int CalcTangents::get_vertex_index(const SMikkTSpaceContext *context, int iFace, int iVert) {
primMeshGLTF *working_primMeshGLTF = static_cast<primMeshGLTF*>(context->m_pUserData);
const auto face_size = get_num_vertices_of_face(context, iFace);
const auto &indices_index = (iFace * face_size) + iVert;
const auto index = working_primMeshGLTF->indices[indices_index];
return index;
}