Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
MikktspaceWrap.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_MIKKWRAP
19#define INCLUDED_MIKKWRAP
20
22#include "third_party/mikktspace/mikktspace.h"
23
24#include <vector>
25
26struct SModelVertex;
27
29{
30
31public:
32
33 MikkTSpace(const CModelDefPtr& m, std::vector<float>& v, bool gpuSkinning);
34
35 void Generate();
36
37private:
38
39 SMikkTSpaceInterface m_Interface;
40 SMikkTSpaceContext m_Context;
41
43
44 std::vector<float>& m_NewVertices;
45
47
48 /**
49 * @param[in] pContext - Pointer to the MikkTSpace context.
50 * @returns - the number of faces (triangles/quads) on the mesh to be processed.
51 */
52 static int GetNumFaces(const SMikkTSpaceContext* pContext);
53
54 /**
55 * @param[in] pContext - Pointer to the MikkTSpace context.
56 * @param[in] iFace - Number in the range { 0, 1, ..., getNumFaces() - 1 }.
57 * @returns - the number of faces (triangles/quads) on the mesh to be processed.
58 */
59 static int GetNumVerticesOfFace(const SMikkTSpaceContext* pContext, const int iFace);
60
61 /**
62 * @param[in] pContext - Pointer to the MikkTSpace context.
63 * @returns - The MikkTSpace.
64 */
65 static MikkTSpace* GetUserDataFromContext(const SMikkTSpaceContext* pContext);
66
67 /**
68 * @param[in] pContext - Pointer to the MikkTSpace context.
69 * @param[in] iFace - Number in the range { 0, 1, ..., getNumFaces() - 1 }.
70 * @param[in] iVert - Number in the range { 0, 1, 2 } for triangles and { 0, 1, 2, 3 } for quads.
71 * @returns - The MikkTSpace.
72 */
73 static SModelVertex GetVertex(const SMikkTSpaceContext* pContext, const int iFace, const int iVert);
74
75 /**
76 * @param[in] pContext - Pointer to the MikkTSpace context.
77 * @param[out] fvPosOut - The array containing the face.
78 * @param[in] iFace - Number in the range { 0, 1, ..., getNumFaces() - 1 }.
79 * @param[in] iVert - Number in the range { 0, 1, 2 } for triangles and { 0, 1, 2, 3 } for quads.
80 */
81 static void GetPosition(const SMikkTSpaceContext* pContext,
82 float* fvPosOut, const int iFace, const int iVert);
83
84 /**
85 * @param[in] pContext - Pointer to the MikkTSpace context.
86 * @param[out] fvNormOut iVert - The array containing the normal.
87 * @param[in] iFace - Number in the range { 0, 1, ..., getNumFaces() - 1 }.
88 * @param[in] iVert - Number in the range { 0, 1, 2 } for triangles and { 0, 1, 2, 3 } for quads.
89 */
90 static void GetNormal(const SMikkTSpaceContext* pContext,
91 float* fvNormOut, const int iFace, const int iVert);
92
93 /**
94 * @param[in] pContext - Pointer to the MikkTSpace context.
95 * @param[out] fvTexcOut iVert - Array containing the UV.
96 * @param[in] iFace - Number in the range { 0, 1, ..., getNumFaces() - 1 }.
97 * @param[in] iVert - Number in the range { 0, 1, 2 } for triangles and { 0, 1, 2, 3 } for quads.
98 */
99 static void GetTexCoord(const SMikkTSpaceContext* pContext,
100 float* fvTexcOut, const int iFace, const int iVert);
101
102 /**
103 * @brief This function is used to return tangent space results to the application.
104 * For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
105 * fSign = bIsOrientationPreserving ? 1.0f : (-1.0f);
106 * bitangent = fSign * cross(vN, tangent);
107 * @param[in] pContext - Pointer to the MikkTSpace context.
108 * @param[in] fvTangent - fvTangent - The tangent vector.
109 * @param[in] fvBiTangent - The "real" bitangent vector. Not be perpendicular to fvTangent. However, both are perpendicular to the vertex normal.
110 * @param[in] fMagS - magniture of the fvTangent vector.
111 * @param[in] fMagT - magniture of the fvBiTangent vector.
112 * @param[in] bIsOrientationPreserving - Whether the orientation should be preserved.
113 * @param[in] iFace - Number in the range {0,1,2} for triangles and {0,1,2,3} for quads.
114 * @param[in] iVert - Array containing the position vector of the face.
115 */
116 static void SetTSpace(const SMikkTSpaceContext* pContext, const float* fvTangent,
117 const float* /*fvBiTangent*/, const float /*fMagS*/, const float /*fMagT*/,
118 const tbool bIsOrientationPreserving, const int iFace, const int iVert);
119
120};
121
122
123#endif // INCLUDED_MIKKWRAP
std::shared_ptr< CModelDef > CModelDefPtr
Definition: MeshManager.h:29
Definition: MikktspaceWrap.h:29
MikkTSpace(const CModelDefPtr &m, std::vector< float > &v, bool gpuSkinning)
Definition: MikktspaceWrap.cpp:28
static SModelVertex GetVertex(const SMikkTSpaceContext *pContext, const int iFace, const int iVert)
Definition: MikktspaceWrap.cpp:145
std::vector< float > & m_NewVertices
Definition: MikktspaceWrap.h:44
static MikkTSpace * GetUserDataFromContext(const SMikkTSpaceContext *pContext)
Definition: MikktspaceWrap.cpp:140
const CModelDefPtr & m_Model
Definition: MikktspaceWrap.h:42
static void GetNormal(const SMikkTSpaceContext *pContext, float *fvNormOut, const int iFace, const int iVert)
Definition: MikktspaceWrap.cpp:74
SMikkTSpaceInterface m_Interface
Definition: MikktspaceWrap.h:39
static int GetNumFaces(const SMikkTSpaceContext *pContext)
Definition: MikktspaceWrap.cpp:53
static void GetTexCoord(const SMikkTSpaceContext *pContext, float *fvTexcOut, const int iFace, const int iVert)
Definition: MikktspaceWrap.cpp:85
static int GetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace)
Definition: MikktspaceWrap.cpp:58
static void GetPosition(const SMikkTSpaceContext *pContext, float *fvPosOut, const int iFace, const int iVert)
Definition: MikktspaceWrap.cpp:63
SMikkTSpaceContext m_Context
Definition: MikktspaceWrap.h:40
bool m_GpuSkinning
Definition: MikktspaceWrap.h:46
void Generate()
Definition: MikktspaceWrap.cpp:48
static void SetTSpace(const SMikkTSpaceContext *pContext, const float *fvTangent, const float *, const float, const float, const tbool bIsOrientationPreserving, const int iFace, const int iVert)
This function is used to return tangent space results to the application.
Definition: MikktspaceWrap.cpp:99
Definition: ModelDef.h:109