code format
This commit is contained in:
@@ -19,51 +19,78 @@
|
||||
#include "loader_obj.h"
|
||||
|
||||
|
||||
void LoaderOBJ::initOBJMesh(GLObjectBase * o, const QVector<Vector3d> & src_vertices, const QVector<Vector3d> & src_normals, const QVector<Vector3d> & src_texcoords) {
|
||||
QVector<GLfloat> & vertices(o->VBO().vertices()), & normals(o->VBO().normals()), & uvs(o->VBO().texcoords());
|
||||
QVector<Vector3i> & faces(o->faces), & uvws(o->uvws), & norms(o->norms);
|
||||
//Vector3d pos = Vector3d(o->pos());
|
||||
void LoaderOBJ::initOBJMesh(GLObjectBase * o,
|
||||
const QVector<Vector3d> & src_vertices,
|
||||
const QVector<Vector3d> & src_normals,
|
||||
const QVector<Vector3d> & src_texcoords) {
|
||||
QVector<GLfloat> &vertices(o->VBO().vertices()), &normals(o->VBO().normals()), &uvs(o->VBO().texcoords());
|
||||
QVector<Vector3i> &faces(o->faces), &uvws(o->uvws), &norms(o->norms);
|
||||
// Vector3d pos = Vector3d(o->pos());
|
||||
bool has_uv = !uvws.isEmpty();
|
||||
Vector3i cf, ct, cn;
|
||||
Vector3d v[3], t[3], n[3];
|
||||
//for (int i = 0; i < points.size(); ++i)
|
||||
// for (int i = 0; i < points.size(); ++i)
|
||||
// points[i] -= pos;
|
||||
int fcnt = faces.size() * 3;
|
||||
vertices.resize(fcnt * 3);
|
||||
normals.resize(vertices.size());
|
||||
if (has_uv) uvs.resize(fcnt * 2);
|
||||
int ind = 0, induv = 0;
|
||||
//qDebug() << "initOBJMesh" << faces.size();
|
||||
// qDebug() << "initOBJMesh" << faces.size();
|
||||
for (int i = 0; i < faces.size(); ++i) {
|
||||
cf = faces[i];
|
||||
ct = uvws[i];
|
||||
cn = norms[i];
|
||||
v[0] = src_vertices[cf.p0];
|
||||
v[1] = src_vertices[cf.p1];
|
||||
v[2] = src_vertices[cf.p2];
|
||||
n[0] = src_normals[cn.p0];
|
||||
n[1] = src_normals[cn.p1];
|
||||
n[2] = src_normals[cn.p2];
|
||||
vertices[ind] = v[0].x; normals[ind] = n[0].x; ++ind;
|
||||
vertices[ind] = v[0].y; normals[ind] = n[0].y; ++ind;
|
||||
vertices[ind] = v[0].z; normals[ind] = n[0].z; ++ind;
|
||||
vertices[ind] = v[1].x; normals[ind] = n[1].x; ++ind;
|
||||
vertices[ind] = v[1].y; normals[ind] = n[1].y; ++ind;
|
||||
vertices[ind] = v[1].z; normals[ind] = n[1].z; ++ind;
|
||||
vertices[ind] = v[2].x; normals[ind] = n[2].x; ++ind;
|
||||
vertices[ind] = v[2].y; normals[ind] = n[2].y; ++ind;
|
||||
vertices[ind] = v[2].z; normals[ind] = n[2].z; ++ind;
|
||||
cf = faces[i];
|
||||
ct = uvws[i];
|
||||
cn = norms[i];
|
||||
v[0] = src_vertices[cf.p0];
|
||||
v[1] = src_vertices[cf.p1];
|
||||
v[2] = src_vertices[cf.p2];
|
||||
n[0] = src_normals[cn.p0];
|
||||
n[1] = src_normals[cn.p1];
|
||||
n[2] = src_normals[cn.p2];
|
||||
vertices[ind] = v[0].x;
|
||||
normals[ind] = n[0].x;
|
||||
++ind;
|
||||
vertices[ind] = v[0].y;
|
||||
normals[ind] = n[0].y;
|
||||
++ind;
|
||||
vertices[ind] = v[0].z;
|
||||
normals[ind] = n[0].z;
|
||||
++ind;
|
||||
vertices[ind] = v[1].x;
|
||||
normals[ind] = n[1].x;
|
||||
++ind;
|
||||
vertices[ind] = v[1].y;
|
||||
normals[ind] = n[1].y;
|
||||
++ind;
|
||||
vertices[ind] = v[1].z;
|
||||
normals[ind] = n[1].z;
|
||||
++ind;
|
||||
vertices[ind] = v[2].x;
|
||||
normals[ind] = n[2].x;
|
||||
++ind;
|
||||
vertices[ind] = v[2].y;
|
||||
normals[ind] = n[2].y;
|
||||
++ind;
|
||||
vertices[ind] = v[2].z;
|
||||
normals[ind] = n[2].z;
|
||||
++ind;
|
||||
if (has_uv) {
|
||||
if ((ct.p0 >= 0) && (ct.p1 >= 0) && (ct.p2 >= 0)) {
|
||||
t[0] = src_texcoords[ct.p0];
|
||||
t[1] = src_texcoords[ct.p1];
|
||||
t[2] = src_texcoords[ct.p2];
|
||||
uvs[induv] = t[0].x; ++induv;
|
||||
uvs[induv] = t[0].y; ++induv;
|
||||
uvs[induv] = t[1].x; ++induv;
|
||||
uvs[induv] = t[1].y; ++induv;
|
||||
uvs[induv] = t[2].x; ++induv;
|
||||
uvs[induv] = t[2].y; ++induv;
|
||||
t[0] = src_texcoords[ct.p0];
|
||||
t[1] = src_texcoords[ct.p1];
|
||||
t[2] = src_texcoords[ct.p2];
|
||||
uvs[induv] = t[0].x;
|
||||
++induv;
|
||||
uvs[induv] = t[0].y;
|
||||
++induv;
|
||||
uvs[induv] = t[1].x;
|
||||
++induv;
|
||||
uvs[induv] = t[1].y;
|
||||
++induv;
|
||||
uvs[induv] = t[2].x;
|
||||
++induv;
|
||||
uvs[induv] = t[2].y;
|
||||
++induv;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,7 +100,8 @@ void LoaderOBJ::initOBJMesh(GLObjectBase * o, const QVector<Vector3d> & src_vert
|
||||
Vector3d readVector3d(QString s) {
|
||||
Vector3d ret;
|
||||
QStringList sl(s.trimmed().split(" "));
|
||||
sl.removeAll(""); sl.removeAll(" ");
|
||||
sl.removeAll("");
|
||||
sl.removeAll(" ");
|
||||
if (sl.size() > 0) ret.x = sl[0].toFloat();
|
||||
if (sl.size() > 1) ret.y = sl[1].toFloat();
|
||||
if (sl.size() > 2) ret.z = sl[2].toFloat();
|
||||
@@ -84,7 +112,8 @@ Vector3d readVector3d(QString s) {
|
||||
Vector2d readVector2d(QString s) {
|
||||
Vector2d ret;
|
||||
QStringList sl(s.trimmed().split(" "));
|
||||
sl.removeAll(""); sl.removeAll(" ");
|
||||
sl.removeAll("");
|
||||
sl.removeAll(" ");
|
||||
if (sl.size() > 0) ret.x = sl[0].toFloat();
|
||||
if (sl.size() > 1) ret.y = sl[1].toFloat();
|
||||
return ret;
|
||||
@@ -99,7 +128,8 @@ QColor readColor(QString s) {
|
||||
|
||||
void readFaces(QString s, GLObjectBase * co) {
|
||||
QStringList sl(s.trimmed().split(" "));
|
||||
sl.removeAll(""); sl.removeAll(" ");
|
||||
sl.removeAll("");
|
||||
sl.removeAll(" ");
|
||||
static Vector3i inds[4];
|
||||
for (int i = 0; i < sl.size(); ++i) {
|
||||
inds[i].p0 = inds[i].p1 = inds[i].p2 = 0;
|
||||
@@ -111,15 +141,15 @@ void readFaces(QString s, GLObjectBase * co) {
|
||||
}
|
||||
if (sl.size() == 3) {
|
||||
co->faces << Vector3i(inds[0].p0 - 1, inds[1].p0 - 1, inds[2].p0 - 1);
|
||||
co->uvws << Vector3i(inds[0].p1 - 1, inds[1].p1 - 1, inds[2].p1 - 1);
|
||||
co->uvws << Vector3i(inds[0].p1 - 1, inds[1].p1 - 1, inds[2].p1 - 1);
|
||||
co->norms << Vector3i(inds[0].p2 - 1, inds[1].p2 - 1, inds[2].p2 - 1);
|
||||
}
|
||||
if (sl.size() == 4) {
|
||||
co->faces << Vector3i(inds[0].p0 - 1, inds[1].p0 - 1, inds[2].p0 - 1);
|
||||
co->uvws << Vector3i(inds[0].p1 - 1, inds[1].p1 - 1, inds[2].p1 - 1);
|
||||
co->uvws << Vector3i(inds[0].p1 - 1, inds[1].p1 - 1, inds[2].p1 - 1);
|
||||
co->norms << Vector3i(inds[0].p2 - 1, inds[1].p2 - 1, inds[2].p2 - 1);
|
||||
co->faces << Vector3i(inds[0].p0 - 1, inds[2].p0 - 1, inds[3].p0 - 1);
|
||||
co->uvws << Vector3i(inds[0].p1 - 1, inds[2].p1 - 1, inds[3].p1 - 1);
|
||||
co->uvws << Vector3i(inds[0].p1 - 1, inds[2].p1 - 1, inds[3].p1 - 1);
|
||||
co->norms << Vector3i(inds[0].p2 - 1, inds[2].p2 - 1, inds[3].p2 - 1);
|
||||
}
|
||||
}
|
||||
@@ -140,9 +170,8 @@ QVector<Material> readMTL(QString obj_path, QString path) {
|
||||
while (!stream.atEnd()) {
|
||||
QString line = stream.readLine().trimmed();
|
||||
if (line.startsWith("newmtl")) {
|
||||
if (!mat.name.isEmpty())
|
||||
ret << mat;
|
||||
mat = Material();
|
||||
if (!mat.name.isEmpty()) ret << mat;
|
||||
mat = Material();
|
||||
mat.name = line.mid(6).trimmed();
|
||||
continue;
|
||||
}
|
||||
@@ -155,12 +184,12 @@ QVector<Material> readMTL(QString obj_path, QString path) {
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("Ks")) {
|
||||
Vector3d v = readVector3d(line.mid(2).trimmed());
|
||||
Vector3d v = readVector3d(line.mid(2).trimmed());
|
||||
mat.map_specular.color_amount = v.length();
|
||||
float mc = qMax(v.x, qMax(v.y, v.z));
|
||||
float mc = qMax(v.x, qMax(v.y, v.z));
|
||||
if (mc > 0.f) v /= mc;
|
||||
mat.color_specular = QColor::fromRgbF(v.x, v.y, v.z);
|
||||
//qDebug() << mat.shine_strength << mat.color_specular;
|
||||
// qDebug() << mat.shine_strength << mat.color_specular;
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("Ns")) {
|
||||
@@ -178,27 +207,25 @@ QVector<Material> readMTL(QString obj_path, QString path) {
|
||||
if (line.startsWith("map_bump")) {
|
||||
line = line.mid(8).trimmed();
|
||||
if (line.startsWith("-bm")) {
|
||||
line = line.mid(3).trimmed();
|
||||
QString sv = line.left(line.indexOf(" "));
|
||||
line = line.mid(sv.size()).trimmed();
|
||||
line = line.mid(3).trimmed();
|
||||
QString sv = line.left(line.indexOf(" "));
|
||||
line = line.mid(sv.size()).trimmed();
|
||||
mat.map_normal.color_amount = sv.toFloat();
|
||||
}
|
||||
mat.map_normal.bitmap_path = findFile(line, sp);
|
||||
//qDebug() << "BUMP" << mat.name << mat.bump_scale << mat.bump.bitmap_path;
|
||||
// qDebug() << "BUMP" << mat.name << mat.bump_scale << mat.bump.bitmap_path;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (!mat.name.isEmpty())
|
||||
ret << mat;
|
||||
if (!mat.name.isEmpty()) ret << mat;
|
||||
qDebug() << "load from MTL" << f.fileName() << ret.size() << "materials";
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Material LoaderOBJ::materialByName(const QVector<Material> & materials, const QString & name) {
|
||||
foreach (const Material & m, materials)
|
||||
if (m.name == name)
|
||||
return m;
|
||||
foreach(const Material & m, materials)
|
||||
if (m.name == name) return m;
|
||||
return Material();
|
||||
}
|
||||
|
||||
@@ -213,13 +240,13 @@ GLObjectBase * loadFromOBJFile(const QString & filepath, float scale) {
|
||||
QTextStream stream(&f);
|
||||
QVector<Vector3d> vertices, normals, texcoords;
|
||||
QVector<Material> materials;
|
||||
GLObjectBase * root = new GLObjectBase(), * co = nullptr;
|
||||
GLObjectBase *root = new GLObjectBase(), *co = nullptr;
|
||||
QString name, line, pline;
|
||||
root->setName(QFileInfo(f).baseName());
|
||||
int cnt = 0;
|
||||
while (!stream.atEnd()) {
|
||||
pline = line;
|
||||
line = stream.readLine().trimmed();
|
||||
line = stream.readLine().trimmed();
|
||||
if (line.startsWith("mtllib")) {
|
||||
materials = readMTL(filepath, line.mid(6).trimmed());
|
||||
continue;
|
||||
@@ -245,7 +272,7 @@ GLObjectBase * loadFromOBJFile(const QString & filepath, float scale) {
|
||||
}
|
||||
co = new GLObjectBase();
|
||||
co->setName(name);
|
||||
//qDebug() << "new object" << co->name();
|
||||
// qDebug() << "new object" << co->name();
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("f ")) {
|
||||
|
||||
Reference in New Issue
Block a user