diff options
Diffstat (limited to 'model.cpp')
-rw-r--r-- | model.cpp | 77 |
1 files changed, 25 insertions, 52 deletions
@@ -7,13 +7,6 @@ #include "GL/gl.h" -static Point3d readPoint(QTextStream &ts) -{ - Point3d p; - ts >> p.x >> p.y >> p.z; - return p; -} - struct Edge { Edge() : pointA(0), pointB(0) {} @@ -46,9 +39,6 @@ Model::Model(const QString &filename) Point3d min( 1e9, 1e9, 1e9); Point3d max(-1e9,-1e9,-1e9); - QVector<Point3d> pointData; - QVector<Point3d> normalData; - QTextStream in(&file); while (!in.atEnd()) { QString input = in.readLine(); @@ -62,18 +52,14 @@ Model::Model(const QString &filename) ts >> id; if (id == "v") { - Point3d p = readPoint(ts); - p.y = -p.y; - pointData << p; - normalData << Point3d(); - - min.x = qMin(min.x, p.x); - min.y = qMin(min.y, p.y); - min.z = qMin(min.z, p.z); - - max.x = qMax(max.x, p.x); - max.y = qMax(max.y, p.y); - max.z = qMax(max.z, p.z); + 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<int, 4> p; @@ -100,56 +86,43 @@ Model::Model(const QString &filename) } } - Point3d bounds = max - min; - qreal scale = 1 / qMax(bounds.x, qMax(bounds.y, bounds.z)); - - for (int i = 0; i < pointData.size(); ++i) { - Point3d &p = pointData[i]; - - p.x -= min.x + bounds.x * 0.5; - p.y -= min.y + bounds.y * 0.5; - p.z -= min.z + bounds.z * 0.5; - - p.x *= scale; - p.y *= scale; - p.z *= scale; - } + 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 = pointData.at(m_pointIndices.at(i)); - const Point3d b = pointData.at(m_pointIndices.at(i+1)); - const Point3d c = pointData.at(m_pointIndices.at(i+2)); + 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)); - Point3d normal = cross(c - a, b - a).normalize(); + const Point3d normal = cross(b - a, c - a).normalize(); for (int j = 0; j < 3; ++j) { - Point3d old = normalData.at(m_pointIndices.at(i + j)); - normalData[m_pointIndices.at(i + j)] = old + normal; + Point3d &n = m_normals[m_pointIndices.at(i + j)]; + n = n + normal; } } - for (int i = 0; i < normalData.size(); ++i) - normalData[i] = normalData.at(i).normalize(); - - m_points = pointData; - m_normals = normalData; + for (int i = 0; i < m_normals.size(); ++i) + m_normals[i] = m_normals[i].normalize(); - foreach(const Edge &edge, QVector<Edge>::fromList(edges.toList())) - m_edges << pointData.at(edge.pointA) << pointData.at(edge.pointB); + foreach (const Edge &edge, QVector<Edge>::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) { - glColor3f(0, 0, 0); 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); - glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); @@ -160,7 +133,6 @@ void Model::render(bool wireframe, bool normals) const glDrawElements(GL_TRIANGLES, m_pointIndices.size(), GL_UNSIGNED_INT, m_pointIndices.data()); glDisableClientState(GL_NORMAL_ARRAY); - glDisable(GL_DEPTH_TEST); glDisable(GL_COLOR_MATERIAL); glDisable(GL_LIGHT0); glDisable(GL_LIGHTING); @@ -178,5 +150,6 @@ void Model::render(bool wireframe, bool normals) const } glEnd(); } + glDisable(GL_DEPTH_TEST); } |