SIGN IN SIGN UP
ssloy / tinyrenderer UNCLAIMED

A brief computer graphics / rendering course

0 0 0 C++
2025-02-23 19:38:30 +01:00
#include <fstream>
#include <sstream>
#include "model.h"
Model::Model(const std::string filename) {
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;
2025-08-14 16:25:41 +02:00
vec4 v = {0,0,0,1};
2025-02-23 19:38:30 +01:00
for (int i : {0,1,2}) iss >> v[i];
verts.push_back(v);
2025-08-14 10:00:25 +02:00
} else if (!line.compare(0, 3, "vn ")) {
iss >> trash >> trash;
2025-08-14 16:25:41 +02:00
vec4 n;
2025-08-14 10:00:25 +02:00
for (int i : {0,1,2}) iss >> n[i];
norms.push_back(normalized(n));
2025-08-14 16:25:41 +02:00
} else if (!line.compare(0, 3, "vt ")) {
iss >> trash >> trash;
vec2 uv;
for (int i : {0,1}) iss >> uv[i];
tex.push_back({uv.x, 1-uv.y});
2025-02-23 19:38:30 +01:00
} else if (!line.compare(0, 2, "f ")) {
int f,t,n, cnt = 0;
iss >> trash;
while (iss >> f >> trash >> t >> trash >> n) {
facet_vrt.push_back(--f);
2025-08-14 16:25:41 +02:00
facet_tex.push_back(--t);
2025-08-14 10:00:25 +02:00
facet_nrm.push_back(--n);
2025-02-23 19:38:30 +01:00
cnt++;
}
if (3!=cnt) {
std::cerr << "Error: the obj file is supposed to be triangulated" << std::endl;
return;
}
}
}
std::cerr << "# v# " << nverts() << " f# " << nfaces() << std::endl;
2025-08-14 16:25:41 +02:00
auto load_texture = [&filename](const std::string suffix, TGAImage &img) {
size_t dot = filename.find_last_of(".");
if (dot==std::string::npos) return;
std::string texfile = filename.substr(0,dot) + suffix;
std::cerr << "texture file " << texfile << " loading " << (img.read_tga_file(texfile.c_str()) ? "ok" : "failed") << std::endl;
};
2025-08-27 09:38:58 +02:00
load_texture("_diffuse.tga", diffusemap );
2025-08-27 14:30:42 +02:00
load_texture("_nm_tangent.tga", normalmap);
2025-08-27 09:38:58 +02:00
load_texture("_spec.tga", specularmap);
2025-02-23 19:38:30 +01:00
}
int Model::nverts() const { return verts.size(); }
int Model::nfaces() const { return facet_vrt.size()/3; }
2025-08-14 16:25:41 +02:00
vec4 Model::vert(const int i) const {
2025-02-23 19:38:30 +01:00
return verts[i];
}
2025-08-14 16:25:41 +02:00
vec4 Model::vert(const int iface, const int nthvert) const {
2025-02-23 19:38:30 +01:00
return verts[facet_vrt[iface*3+nthvert]];
}
2025-08-14 16:25:41 +02:00
vec4 Model::normal(const int iface, const int nthvert) const {
2025-08-14 10:00:25 +02:00
return norms[facet_nrm[iface*3+nthvert]];
}
2025-08-14 16:25:41 +02:00
vec4 Model::normal(const vec2 &uv) const {
TGAColor c = normalmap.get(uv[0]*normalmap.width(), uv[1]*normalmap.height());
2025-08-27 14:30:42 +02:00
return normalized(vec4{(double)c[2],(double)c[1],(double)c[0],0}*2./255. - vec4{1,1,1,0});
2025-08-14 16:25:41 +02:00
}
vec2 Model::uv(const int iface, const int nthvert) const {
return tex[facet_tex[iface*3+nthvert]];
}
2025-08-27 09:38:58 +02:00
const TGAImage& Model::diffuse() const { return diffusemap; }
const TGAImage& Model::specular() const { return specularmap; }