-
Notifications
You must be signed in to change notification settings - Fork 3
/
mat.h
148 lines (120 loc) · 3.71 KB
/
mat.h
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#pragma once
#include "mgs/common/util.h"
#include "mgs/texture/tri.h"
#include "noesis/plugin/pluginshare.h"
inline
int findMaterialIdx(char* matName, CArrayList<noesisMaterial_t*>& matList)
{
for (int i = 0; i < matList.Num(); i++)
{
if (!strcmp(matList[i]->name, matName)) return i;
}
return -1;
}
inline
int findTextureIdx(char* texName, CArrayList<noesisTex_t*>& texList) {
for (int i = 0; i < texList.Num(); i++) {
if (!strcmp(texList[i]->filename, texName))
return i;
}
return -1;
}
inline
std::string findTri(noeRAPI_t* rapi, uint32_t& strcode)
{
std::filesystem::path p{ rapi->Noesis_GetInputName() };
p = p.parent_path();
p = p.parent_path();
p = p.parent_path();
std::filesystem::path tri{ p.string() + "\\tri"};
if (!std::filesystem::exists(tri)) return "";
for (const std::filesystem::directory_entry& file : std::filesystem::recursive_directory_iterator(tri))
{
if (file.path().extension() == ".tri")
{
Tri tri = Tri(file.path().u8string());
if (tri.containsTexture(strcode))
return file.path().u8string();
}
}
return "";
}
inline
noesisTex_t* loadTexture(noeRAPI_t* rapi, uint32_t& strcode)
{
std::string triFile = findTri(rapi, strcode);
if (triFile == "") return NULL;
int size, bpp;
Tri tri = Tri(triFile);
uint8_t* texData = tri.getTexture(strcode, size, bpp);
noesisTex_t* noeTexture = rapi->Noesis_LoadTexByHandler(texData, size, ".tga");
delete[] texData;
return noeTexture;
}
inline
void bindMat(uint32_t strcode[3], BYTE* fileBuffer, noeRAPI_t* rapi, CArrayList<noesisMaterial_t*>& matList, CArrayList<noesisTex_t*>& texList)
{
std::string str[3];
str[0] = intToHexString(strcode[0]);
str[1] = intToHexString(strcode[1]);
str[2] = intToHexString(strcode[2]);
std::string matStr = str[0] + str[1] + str[2];
//set mat name
char matName[19];
strcpy_s(matName, matStr.c_str());
//check if material already exists
int x = findMaterialIdx(matName, matList);
//use existing mat if it does
if (x > -1)
{
rapi->rpgSetMaterial(matName);
return;
}
//create material
noesisMaterial_t* noeMat = rapi->Noesis_GetMaterialList(1, false);
noeMat->name = rapi->Noesis_PooledString(matName);
//set tex name
for (int i = 0; i < 3; i++)
{
if (!strcode[i]) continue;
std::string fullName = str[i] + ".tga";
char texName[11];
strcpy_s(texName, fullName.c_str());
//check if texture already exists
int y = findTextureIdx(texName, texList);
//set tex to mat if it does
if (y > -1)
{
switch (i) {
case 0:
noeMat->texIdx = y; break;
case 1:
noeMat->specularTexIdx = y; break;
case 2:
noeMat->envTexIdx = y; break;
}
matList.Append(noeMat);
rapi->rpgSetMaterial(matName);
return;
}
//load texture
noesisTex_t* noeTexture = loadTexture(rapi, strcode[i]);
if (!noeTexture) return;
noeTexture->filename = rapi->Noesis_PooledString(texName);
//set tex to mat
switch (i)
{
//also depends on vertdef flag(& 0x200); but I'll do that another day
case 0:
noeMat->texIdx = texList.Num(); break;
case 1:
noeMat->specularTexIdx = texList.Num(); break;
case 2:
noeMat->envTexIdx = texList.Num(); break;
}
matList.Append(noeMat);
texList.Append(noeTexture);
}
//set material
rapi->rpgSetMaterial(noeMat->name);
}