#include "model.h" #include #include #include #include #include "GL/gl.h" struct Edge { Edge() : pointA(0), pointB(0) {} Edge(int pa, int pb) : pointA(pa), pointB(pb) {} int pointA; int pointB; }; uint qHash(const Edge &edge) { return qHash(edge.pointA) ^ qHash(edge.pointB); } bool operator==(const Edge &a, const Edge &b) { return (a.pointA == b.pointA && a.pointB == b.pointB) || (a.pointA == b.pointB && a.pointB == b.pointA); } Model::Model(const QString &filename) { QFile file(filename); if (!file.open(QIODevice::ReadOnly)) return; // use a set to avoid duplicate edges QSet edges; Point3d min( 1e9, 1e9, 1e9); Point3d max(-1e9,-1e9,-1e9); QTextStream in(&file); while (!in.atEnd()) { QString input = in.readLine(); // empty or comment? if (input.isEmpty() || input[0] == '#') continue; QTextStream ts(&input); QString id; ts >> id; if (id == "v") { Point3d p; ts >> p.x >> p.y >> p.z; m_points << p; for (int i = 0; i < 3; ++i) { min[i] = qMin(min[i], p[i]); max[i] = qMax(max[i], p[i]); } } else if (id == "f" || id == "fo") { QVarLengthArray p; while (!ts.atEnd()) { QString vertex; ts >> vertex; int index; QTextStream vertexStream(&vertex); vertexStream >> index; if (vertexStream.status() == QTextStream::Ok) p.append(index - 1); } for (int i = 0; i < p.size(); ++i) edges << Edge(p[i], p[(i + 1) % p.size()]); for (int i = 0; i < 3; ++i) m_pointIndices << p[i]; if (p.size() == 4) for (int i = 0; i < 3; ++i) m_pointIndices << p[(i + 2) % 4]; } } const Point3d bounds = max - min; const qreal scale = 1 / qMax(bounds.x, qMax(bounds.y, bounds.z)); for (int i = 0; i < m_points.size(); ++i) m_points[i] = (m_points[i] - (min + bounds * 0.5)) * scale; m_normals.resize(m_points.size()); for (int i = 0; i < m_pointIndices.size(); i += 3) { const Point3d a = m_points.at(m_pointIndices.at(i)); const Point3d b = m_points.at(m_pointIndices.at(i+1)); const Point3d c = m_points.at(m_pointIndices.at(i+2)); const Point3d normal = cross(b - a, c - a).normalize(); for (int j = 0; j < 3; ++j) { Point3d &n = m_normals[m_pointIndices.at(i + j)]; n = n + normal; } } for (int i = 0; i < m_normals.size(); ++i) m_normals[i] = m_normals[i].normalize(); foreach (const Edge &edge, QVector::fromList(edges.toList())) m_edges << m_points.at(edge.pointA) << m_points.at(edge.pointB); } void Model::render(bool wireframe, bool normals) const { glEnable(GL_DEPTH_TEST); glEnableClientState(GL_VERTEX_ARRAY); if (wireframe) { glVertexPointer(3, GL_FLOAT, 0, m_edges.data()); glDrawArrays(GL_LINES, 0, m_edges.size()); } else { glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); glShadeModel(GL_SMOOTH); glEnableClientState(GL_NORMAL_ARRAY); glVertexPointer(3, GL_FLOAT, 0, (float *)m_points.data()); glNormalPointer(GL_FLOAT, 0, (float *)m_normals.data()); glDrawElements(GL_TRIANGLES, m_pointIndices.size(), GL_UNSIGNED_INT, m_pointIndices.data()); glDisableClientState(GL_NORMAL_ARRAY); glDisable(GL_COLOR_MATERIAL); glDisable(GL_LIGHT0); glDisable(GL_LIGHTING); } glDisableClientState(GL_VERTEX_ARRAY); if (normals) { glColor3f(1, 0, 0); glBegin(GL_LINES); for (int i = 0; i < m_normals.size(); ++i) { Point3d a = m_points.at(i); Point3d b = m_points.at(i) + m_normals.at(i) * 0.02; glVertex3f(a.x, a.y, a.z); glVertex3f(b.x, b.y, b.z); } glEnd(); } glDisable(GL_DEPTH_TEST); }