-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmodel.cpp
262 lines (245 loc) · 7.86 KB
/
gmodel.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "gmodel.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include "glog.h"
#include "gutils.h"
using namespace GMath;
using namespace std;
GOBJModel::GOBJModel(GModelType modelType, const std::string filename) : modelType(modelType), verts_(), uv_(), norms_(), facet_vrt_(), facet_tex_(), facet_nrm_(), diffusemap_(), normalmap_(), specularmap_()
{
Setup(modelType, filename);
}
void GOBJModel::Setup(GModelType modelType, const std::string filename)
{
if(this->modelType==GModelType::kMTInvalid)
{
GLog::LogError("model is setuped! originType = ", this->modelType, " newSetupType = ", modelType);
return;
}
modelFilePath = filename;
this->modelType = modelType;
switch (modelType)
{
case GModelType::kMTInvalid:
GLog::LogError("modelType is kInvalid");
break;
case GModelType::kMTTriange:
verts_.assign({vec3(-1,-1,0),vec3(1,-1,0),vec3(0,1,0)});
uv_.assign({vec2(0,0),vec2(1,0),vec2(1,1)});
norms_.assign({vec3(0,0,1)});
facet_vrt_.assign({3,2,1});
facet_tex_.assign({3,2,1});
facet_nrm_.assign({1,1,1});
break;
case GModelType::kMTCube:
verts_.assign({
// front
vec3(-0.5,-0.5,-0.5),vec3(0.5,-0.5,-0.5),vec3(0.5,0.5,-0.5),vec3(-0.5,0.5,-0.5),
// back
vec3(-0.5,-0.5,0.5),vec3(0.5,-0.5,0.5),vec3(0.5,0.5,0.5),vec3(-0.5,0.5,0.5),
});
uv_.assign({
vec2(0,0),vec2(1,0),vec2(1,1),vec2(0,1),
});
norms_.assign({
// front
vec3(0,0,-1),
// right
vec3(1,0,0),
// back
vec3(0,0,1),
// left
vec3(-1,0,0),
// top
vec3(0,1,0),
// bottom
vec3(0,-1,0),
});
// index start from 1
facet_vrt_.assign({
// front
3,2,1, 4,3,1,
// right
7,6,2, 3,7,2,
// back
5,6,7, 5,7,8,
// left
4,1,5, 4,5,8,
// top
7,3,4, 8,7,4,
// bottom
1,2,6, 1,6,5
});
facet_tex_.assign({
// front
3,2,1, 4,3,1,
// right
3,2,1, 4,3,1,
// back
3,2,1, 4,3,1,
// left
3,2,1, 4,3,1,
// top
3,2,1, 4,3,1,
// bottom
3,2,1, 4,3,1,
});
facet_nrm_.assign({
// front
1,1,1,1,1,1,
// right
2,2,2,2,2,2,
// back
3,3,3,3,3,3,
// left
4,4,4,4,4,4,
// top
5,5,5,5,5,5,
// bottom
6,6,6,6,6,6,
});
break;
case GModelType::kMTObj:
std::ifstream in;
in.open (filename, std::ifstream::in);
if (in.fail()) return;
std::string line;
while (!in.eof()) {
std::getline(in, line);
std::istringstream iss(line.c_str());
char trash;
if (!line.compare(0, 2, "v ")) {
iss >> trash;
vec3 v;
for (int i=0;i<3;i++) iss >> v[i];
verts_.push_back(v);
} else if (!line.compare(0, 3, "vn ")) {
iss >> trash >> trash;
vec3 n;
for (int i=0;i<3;i++) iss >> n[i];
norms_.push_back(n.normalize());
} else if (!line.compare(0, 3, "vt ")) {
iss >> trash >> trash;
vec2 uv;
for (int i=0;i<2;i++) iss >> uv[i];
uv_.push_back(uv);
} else if (!line.compare(0, 2, "f ")) {
int f,t,n;
iss >> trash;
int cnt = 0;
while (iss >> f >> trash >> t >> trash >> n) {
facet_vrt_.push_back(--f);
facet_tex_.push_back(--t);
facet_nrm_.push_back(--n);
cnt++;
}
if (3!=cnt) {
std::cerr << "Error: the obj file is supposed to be triangulated" << std::endl;
in.close();
return;
}
}
}
in.close();
std::cerr << "# v# " << verts_.size() << " f# " << nfaces() << " vt# " << uv_.size() << " vn# " << norms_.size() << std::endl;
break;
}
//load_texture(filename, "_diffuse.tga", diffusemap_);
//load_texture(filename, "_nm_tangent.tga", normalmap_);
//load_texture(filename, "_spec.tga", specularmap_);
}
int GOBJModel::nfaces() const {
return facet_vrt_.size()/3;
}
vec3 GOBJModel::vert(const int i) const {
return verts_[i];
}
vec3 GOBJModel::vert(const int iface, const int nthvert) const {
return verts_[facet_vrt_[iface*3+nthvert]];
}
TGAColor GOBJModel::diffuse(const vec2 &uvf) const {
return diffusemap_.get(uvf[0]*diffusemap_.get_width(), uvf[1]*diffusemap_.get_height());
}
vec3 GOBJModel::normal(const vec2 &uvf) const {
TGAColor c = normalmap_.get(uvf[0]*normalmap_.get_width(), uvf[1]*normalmap_.get_height());
vec3 res;
for (int i=0; i<3; i++)
res[2-i] = c[i]/255.*2 - 1;
return res;
}
double GOBJModel::specular(const vec2 &uvf) const {
return specularmap_.get(uvf[0]*specularmap_.get_width(), uvf[1]*specularmap_.get_height())[0];
}
vec2 GOBJModel::uv(const int iface, const int nthvert) const {
return uv_[facet_tex_[iface*3+nthvert]];
}
vec3 GOBJModel::normal(const int iface, const int nthvert) const {
return norms_[facet_nrm_[iface*3+nthvert]];
}
GGLModel GGLModel::CreateWithObjModel(GOBJModel *objModel, bool init_texture)
{
GGLModel glModel;
if(objModel==nullptr)
{
GLog::LogError("objModel==nullptr");
return glModel;
}
for(int vertIdx=0; vertIdx<objModel->nfaces()*3; vertIdx++)
{
int posIdx = objModel->facet_vrt_[vertIdx];
vec3 pos = objModel->verts_[posIdx];
glModel.verts_.push_back(pos);
int uvIdx = objModel->facet_tex_[vertIdx];
vec2 uv = objModel->uv_[uvIdx];
glModel.uv_.push_back(uv);
int normIdx = objModel->facet_nrm_[vertIdx];
vec3 norm = objModel->norms_[normIdx];
glModel.norms_.push_back(norm);
glModel.index_.push_back(vertIdx);
}
glModel.modelFilePath = objModel->modelFilePath;
if(init_texture)
{
glModel.init_texture();
}
return glModel;
}
void GGLModel::init_texture(GMipmapType diff, GMipmapType norm, GMipmapType spec)
{
diff_mipmaptype = diff;
norm_mipmaptype = norm;
spec_mipmaptype = spec;
load_mipmap(modelFilePath, "_diffuse", diffusemap_mipmaps_, diff!=GMipmapType::kMipmapOff?20:1);
load_mipmap(modelFilePath, "_nm_tangent", normalmap_mipmaps_, norm!=GMipmapType::kMipmapOff?20:1);
load_mipmap(modelFilePath, "_spec", specularmap_mipmaps_, spec!=GMipmapType::kMipmapOff?20:1);
}
int GGLModel::nverts() const
{
return verts_.size();
}
void GGLModel::load_texture(std::string texfile, TGAImage &img)
{
std::cerr << "texture file " << texfile << " loading " << (img.read_tga_file(texfile.c_str()) ? "ok" : "failed") << std::endl;
img.flip_vertically();
}
void GGLModel::load_mipmap(const std::string& modelFilePath, const std::string suffix, std::vector<TGAImage> &mipmaps, int mipmapMaxLevel)
{
size_t dot = modelFilePath.find_last_of(".");
if (dot==std::string::npos) return;
std::string texfilePath = modelFilePath.substr(0,dot) + suffix + "/";
for(int i=0; i<mipmapMaxLevel; i++)
{
string texfile = texfilePath + to_string(i) + ".tga";
if(GUtils::IsFileExist(texfile))
{
TGAImage mipmap;
load_texture(texfile, mipmap);
mipmaps.push_back(move(mipmap));
}
else
{
break;
}
}
}