diff options
author | Samuel Rødal <sroedal@trolltech.com> | 2008-06-25 16:35:27 +0200 |
---|---|---|
committer | Samuel Rødal <sroedal@trolltech.com> | 2008-06-25 17:34:38 +0200 |
commit | 299754c3aae4bc3327485c2ac6a4b663bbba84f6 (patch) | |
tree | e8add0120d933a15fbb94c9675866cbc9ebb24e3 | |
parent | 8c9b92b51c8962e3992ad91580798c02f7c65761 (diff) |
Optimize Edge import and rendering a bit.
-rw-r--r-- | model.cpp | 41 | ||||
-rw-r--r-- | model.h | 2 |
2 files changed, 11 insertions, 32 deletions
@@ -1,32 +1,11 @@ #include "model.h" #include <QFile> -#include <QSet> #include <QTextStream> #include <QVarLengthArray> #include <QtOpenGL> -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() { } @@ -37,9 +16,6 @@ Model::Model(const QString &filename) if (!file.open(QIODevice::ReadOnly)) return; - // use a set to avoid duplicate edges - QSet<Edge> edges; - Point3d boundsMin( 1e9, 1e9, 1e9); Point3d boundsMax(-1e9,-1e9,-1e9); @@ -74,8 +50,14 @@ Model::Model(const QString &filename) p.append(vertexIndex - 1); } - for (int i = 0; i < p.size(); ++i) - edges << Edge(p[i], p[(i + 1) % p.size()]); + for (int i = 0; i < p.size(); ++i) { + const int edgeA = p[i]; + const int edgeB = p[(i + 1) % p.size()]; + + // avoid duplicate edges + if (edgeA < edgeB) + m_edgeIndices << edgeA << edgeB; + } for (int i = 0; i < 3; ++i) m_pointIndices << p[i]; @@ -105,9 +87,6 @@ Model::Model(const QString &filename) 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 << m_points.at(edge.pointA) << m_points.at(edge.pointB); } void Model::render(bool wireframe, bool normals) const @@ -115,8 +94,8 @@ 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()); + glVertexPointer(3, GL_FLOAT, 0, (float *)m_points.data()); + glDrawElements(GL_LINES, m_edgeIndices.size(), GL_UNSIGNED_INT, m_edgeIndices.data()); } else { glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); @@ -18,7 +18,7 @@ public: private: QVector<Point3d> m_points; QVector<Point3d> m_normals; - QVector<Point3d> m_edges; + QVector<int> m_edgeIndices; QVector<int> m_pointIndices; }; |