summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Rødal <sroedal@trolltech.com>2008-06-23 12:21:49 +0200
committerSamuel Rødal <sroedal@trolltech.com>2008-06-23 12:21:49 +0200
commit883d04cc23cc7e6e4ec4cf1febc6dcd02cd90041 (patch)
treec337eaea5650689ec72933316d63383333d2d6c9
parentf940e3207433b19f06c52646a5c29bed46c11d4d (diff)
Some more simplifications.
-rw-r--r--graphicsscene.cpp12
-rw-r--r--main.cpp5
-rw-r--r--model.cpp77
-rw-r--r--point3d.h12
4 files changed, 42 insertions, 64 deletions
diff --git a/graphicsscene.cpp b/graphicsscene.cpp
index e26684a..01d1d4e 100644
--- a/graphicsscene.cpp
+++ b/graphicsscene.cpp
@@ -6,7 +6,7 @@
#include "GL/glu.h"
#include <QtGui>
-#include <QGLWidget>
+#include <QGLContext>
class Controls : public QGroupBox
{
@@ -165,8 +165,6 @@ void GraphicsScene::drawBackground(QPainter *painter, const QRectF &)
glClearColor(qRed(m_backgroundColor)/255.0f, qGreen(m_backgroundColor)/255.0f, qBlue(m_backgroundColor)/255.0f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- bool useMultisample = static_cast<QGLWidget *>(painter->device())->format().sampleBuffers();
-
if (m_model) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
@@ -187,13 +185,9 @@ void GraphicsScene::drawBackground(QPainter *painter, const QRectF &)
glTranslatef(0, 0, -m_distance);
glMultMatrixf(&m_matrix[0][0]);
- if (useMultisample)
- glEnable(GL_MULTISAMPLE);
-
+ glEnable(GL_MULTISAMPLE);
m_model->render(m_wireframeEnabled, m_normalsEnabled);
-
- if (useMultisample)
- glDisable(GL_MULTISAMPLE);
+ glDisable(GL_MULTISAMPLE);
glPopMatrix();
diff --git a/main.cpp b/main.cpp
index e71e334..9ac5558 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,8 +1,7 @@
-#include <QtGui>
-#include <QtOpenGL>
-
#include "graphicsscene.h"
+#include <QtOpenGL>
+
int main(int argc, char **argv)
{
QApplication app(argc, argv);
diff --git a/model.cpp b/model.cpp
index 50ed801..1af3c5a 100644
--- a/model.cpp
+++ b/model.cpp
@@ -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);
}
diff --git a/point3d.h b/point3d.h
index 2e37e42..5bf4212 100644
--- a/point3d.h
+++ b/point3d.h
@@ -3,6 +3,8 @@
#include "math.h"
+#include <qglobal.h>
+
struct Point3d
{
float x, y, z;
@@ -41,6 +43,16 @@ struct Point3d
{
return Point3d(x * f, y * f, z * f);
}
+
+ float &operator[](unsigned int index) {
+ Q_ASSERT(index < 3);
+ return (&x)[index];
+ }
+
+ const float &operator[](unsigned int index) const {
+ Q_ASSERT(index < 3);
+ return (&x)[index];
+ }
};
inline float dot(const Point3d &a, const Point3d &b)