diff --git a/qglview/globject.cpp b/qglview/globject.cpp index 7935b65..6fadef3 100644 --- a/qglview/globject.cpp +++ b/qglview/globject.cpp @@ -137,25 +137,7 @@ void GLObjectBase::addChildren(QList & list, GLObjectBase * whe void GLObjectBase::calculateBoundingBox() { - if (points.isEmpty()) return; - GLfloat mix, miy, miz, max, may, maz; - mix = max = points[0].x; - miy = may = points[0].y; - miz = maz = points[0].z; - foreach (const Vector3d & i, points) { - if (mix > i.x) mix = i.x; - if (max < i.x) max = i.x; - if (miy > i.y) miy = i.y; - if (may < i.y) may = i.y; - if (miz > i.z) miz = i.z; - if (maz < i.z) maz = i.z; - } - bound.x = (mix + max) / 2.; - bound.y = (miy + may) / 2.; - bound.z = (miz + maz) / 2.; - bound.length = max - mix; - bound.width = may - miy; - bound.height = maz - miz; + bound = vbo.boundingBox(); } diff --git a/qglview/glvbo.cpp b/qglview/glvbo.cpp index 87edfeb..fa9917c 100644 --- a/qglview/glvbo.cpp +++ b/qglview/glvbo.cpp @@ -281,3 +281,33 @@ bool GLVBO::loadFromFile(const QString & filename) { } return false; } + + +Box3D GLVBO::boundingBox() const { + if (vertices_.size() < 3) return Box3D(); + int vcnt = vertices_.size() / 3; + //qDebug() << "calculateBinormals" << vcnt << tcnt << vertices_.size() << texcoords_.size() << "..."; + GLfloat mix, miy, miz, max, may, maz; + Vector3d v0(vertices_[0], vertices_[1], vertices_[2]); + mix = max = v0.x; + miy = may = v0.y; + miz = maz = v0.z; + Box3D bound; + for (int t = 1; t < vcnt; ++t) { + int vi = t*3; + Vector3d v(vertices_[vi + 0], vertices_[vi + 1], vertices_[vi + 2]); + if (mix > v.x) mix = v.x; + if (max < v.x) max = v.x; + if (miy > v.y) miy = v.y; + if (may < v.y) may = v.y; + if (miz > v.z) miz = v.z; + if (maz < v.z) maz = v.z; + } + bound.x = (mix + max) / 2.; + bound.y = (miy + may) / 2.; + bound.z = (miz + maz) / 2.; + bound.length = max - mix; + bound.width = may - miy; + bound.height = maz - miz; + return bound; +} diff --git a/qglview/glvbo.h b/qglview/glvbo.h index d6347e0..9e27727 100644 --- a/qglview/glvbo.h +++ b/qglview/glvbo.h @@ -55,6 +55,8 @@ public: bool saveToFile(const QString & filename); bool loadFromFile(const QString & filename); + Box3D boundingBox() const; + private: void calculateBinormals(); diff --git a/qglview/mainwindow.cpp b/qglview/mainwindow.cpp index d6f76c9..9b57166 100644 --- a/qglview/mainwindow.cpp +++ b/qglview/mainwindow.cpp @@ -201,6 +201,7 @@ void MainWindow::selectionChanged(GLObjectBase * cur, GLObjectBase * prev) { objectEditor->setObject(sel_obj); if (sel_obj == 0) return; matEditor->setMaterial(sel_obj->material()); + qDebug() << sel_obj->boundingBox(); }