From cdba136c667d3875002bdc2cad97c40a8773ea7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D0=BB=D0=B8=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= Date: Fri, 17 Nov 2017 11:54:18 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/libs@319 a8b55f48-bf90-11e4-a774-851b48703e85 --- qglview/globject.cpp | 10 +++++++ qglview/globject.h | 3 +- qglview/gltypes.cpp | 43 +++++++++++++++++++++++++++ qglview/gltypes.h | 8 +++-- qglview/glvbo.cpp | 11 +++---- qglview/glvbo.h | 8 ++--- qglview/qglview.cpp | 5 ++-- qglview/qglview.h | 1 + qglview/renderer_deferred_shading.cpp | 2 +- 9 files changed, 76 insertions(+), 15 deletions(-) diff --git a/qglview/globject.cpp b/qglview/globject.cpp index 6e9a97c..f5dbff4 100644 --- a/qglview/globject.cpp +++ b/qglview/globject.cpp @@ -138,6 +138,16 @@ void GLObjectBase::addChildren(QList & list, GLObjectBase * whe void GLObjectBase::calculateBoundingBox() { bound = vbo.boundingBox(); + QVector c = bound.corners(), tc; + //QMatrix4x4 mat = itransform_.inverted(); + //qDebug() << itransform_ << mat_ << mat; + foreach (QVector3D p, c) + tc << (itransform_ * QVector4D(p, 1)).toVector3D(); + bound = Box3D(tc); + foreach (GLObjectBase * i, children_) { + i->calculateBoundingBox(); + bound |= i->boundingBox(); + } } diff --git a/qglview/globject.h b/qglview/globject.h index 0600321..5439c54 100644 --- a/qglview/globject.h +++ b/qglview/globject.h @@ -175,6 +175,8 @@ public: const Box3D & boundingBox(bool withChildren = true) const {return bound;} GLVBO & VBO() {return vbo;} + void calculateBoundingBox(); + QVector3D pos_h; QVector points, puvws; QVector faces, uvws, norms; @@ -186,7 +188,6 @@ protected: void addChildren(QList & list, GLObjectBase * where); void loadTextures(bool with_children = false) {material_.loadTextures((GLTextureManagerBase * )currentGLTextureManager); if (with_children) foreach (GLObjectBase * i, children_) i->loadTextures(); is_tex_loaded = true; checkPass();} //void deleteTextures() {foreach (GLuint i, textures) currentQGLView->deleteTexture(i); textures.clear();} - void calculateBoundingBox(); void buildTransform(); void initInternal() {init(); loadTextures(); foreach (GLObjectBase * i, children_) i->initInternal();} void render(int * id = 0, QMap * ids = 0, int sh_id_loc = 0); diff --git a/qglview/gltypes.cpp b/qglview/gltypes.cpp index 825316c..d0b75b4 100644 --- a/qglview/gltypes.cpp +++ b/qglview/gltypes.cpp @@ -305,3 +305,46 @@ const Camera & QGLViewBase::camera() const { void QGLViewBase::setCamera(const Camera & camera) { *camera_ = camera; } + + +Box3D::Box3D(const QVector & points) { + x = y = z = width = length = height = angle_z = angle_xy = angle_roll = 0.f; + if (points.isEmpty()) return; + float ix, iy, iz, ax, ay, az; + ix = ax = points[0].x(); + iy = ay = points[0].y(); + iz = az = points[0].z(); + for (int i = 1; i < points.size(); ++i) { + ix = qMin(ix, points[i].x()); ax = qMax(ax, points[i].x()); + iy = qMin(iy, points[i].y()); ay = qMax(ay, points[i].y()); + iz = qMin(iz, points[i].z()); az = qMax(az, points[i].z()); + } + x = ix; + y = iy; + z = iz; + length = ax - ix; + width = ay - iy; + height = az - iz; +} + + +QVector Box3D::corners() const { + QVector ret; + ret << QVector3D(x, y, z) << QVector3D(x, y + width, z) << QVector3D(x, y, z + height) << QVector3D(x, y + width, z + height) + << QVector3D(x + length, y, z) << QVector3D(x + length, y + width, z) + << QVector3D(x + length, y, z + height) << QVector3D(x + length, y + width, z + height); + return ret; +} + + +Box3D & Box3D::operator |=(const Box3D & o) { + if (isEmpty()) *this = o; + else { + GLfloat mx = x + length, my = y + width, mz = z + height; + GLfloat omx = o.x + o.length, omy = o.y + o.width, omz = o.z + o.height; + x = qMin(x, o.x); y = qMin(y, o.y); z = qMin(z, o.z); + mx = qMax(mx, omx); my = qMax(my, omy); mz = qMax(mz, omz); + length = mx - x; width = my - y; height = mz - z; + } + return *this; +} diff --git a/qglview/gltypes.h b/qglview/gltypes.h index e090da0..b41f924 100644 --- a/qglview/gltypes.h +++ b/qglview/gltypes.h @@ -236,21 +236,25 @@ struct Box3D { GLfloat angle_roll; Box3D() {x = y = z = width = length = height = angle_z = angle_xy = angle_roll = 0.f;} Box3D(const QVector3D & center, GLfloat hwid, GLfloat hlen, GLfloat hhei) {x = center.x() - hwid; y = center.y() - hlen; z = center.z() - hhei; width = 2 * hwid; length = 2 * hlen; height = 2 * hhei; angle_z = angle_xy = angle_roll = 0.f;} + Box3D(const QVector & points); + bool isEmpty() const {return (qAbs(width) < 1E-6) || (qAbs(length) < 1E-6) || (qAbs(height) < 1E-6);} QVector3D randomPoint() const {return QVector3D(uprand(length) + x, uprand(width) + y, uprand(height) + z);} QVector3D pos() const {return QVector3D(x, y, z);} QVector3D size() const {return QVector3D(length, width, height);} QVector3D center() const {return QVector3D(length / 2. + x, width / 2. + y, height / 2. + z);} QVector3D angles() const {return QVector3D(angle_xy, angle_roll, angle_z);} + QVector corners() const; void setPos(const QVector3D & p) {x = p.x(); y = p.y(); z = p.z();} void setAngles(const QVector3D & a) {angle_xy = a.x(); angle_roll = a.y(); angle_z = a.z();} - void setSize(const QVector3D & s) {length = s.x(); width = s.y(); height = s.z();} + void setSize(const QVector3D & s) {length = s.x(); width = s.y(); height = s.z();} Box3D & moveTo(const QVector3D & v) {x = v.x(); y = v.y(); z = v.z(); return *this;} Box3D & move(const QVector3D & v) {x += v.x(); y += v.y(); z += v.z(); return *this;} Box3D movedTo(const QVector3D & v) const {Box3D t(*this); t.x = v.x(); t.y = v.y(); t.z = v.z(); return t;} Box3D moved(const QVector3D & v) const {Box3D t(*this); t.x += v.x(); t.y += v.y(); t.z += v.z(); return t;} + Box3D & operator |=(const Box3D & o); }; -inline QDebug operator <<(QDebug d, const Box3D & v) {d << "Box3D {center (" << v.x << "," << v.y << "," << v.z << "), size (" << v.length << "," << v.width << "," << v.height << ")}"; return d;} +inline QDebug operator <<(QDebug d, const Box3D & v) {d << "Box3D {start (" << v.x << "," << v.y << "," << v.z << "), size (" << v.length << "," << v.width << "," << v.height << ")}"; return d;} struct Vector3d; diff --git a/qglview/glvbo.cpp b/qglview/glvbo.cpp index 00557fc..a765d67 100644 --- a/qglview/glvbo.cpp +++ b/qglview/glvbo.cpp @@ -36,7 +36,7 @@ void GLVBO::init() { #if QT_VERSION >= 0x050600 initializeOpenGLFunctions(); #endif - if (!isIinit()) { + if (!isInit()) { //glGenVertexArrays(1, &va_); glGenBuffers(1, &buffer_); } @@ -289,8 +289,9 @@ bool GLVBO::loadFromFile(const QString & filename) { } -Box3D GLVBO::boundingBox() const { +Box3D GLVBO::boundingBox(const QMatrix4x4 & mat) const { if (vertices_.size() < 3) return Box3D(); + bool mi = mat.isIdentity(); int vcnt = vertices_.size() / 3; //qDebug() << "calculateBinormals" << vcnt << tcnt << vertices_.size() << texcoords_.size() << "..."; GLfloat mix, miy, miz, max, may, maz; @@ -309,9 +310,9 @@ Box3D GLVBO::boundingBox() const { 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.x = mix; + bound.y = miy; + bound.z = miz; bound.length = max - mix; bound.width = may - miy; bound.height = maz - miz; diff --git a/qglview/glvbo.h b/qglview/glvbo.h index 3a97e90..a912020 100644 --- a/qglview/glvbo.h +++ b/qglview/glvbo.h @@ -43,9 +43,9 @@ public: void clear(); GLuint buffer() const {return buffer_;} - int verticesCount() const {return vert_count;} - bool isIinit() const {return buffer_ != 0;} - bool isEmpty() const {return vert_count == 0;} + int verticesCount() const {return vertices_.size() / 3;} + bool isInit() const {return buffer_ != 0;} + bool isEmpty() const {return vertices_.size() < 3;} QVector & vertices() {changed = true; return vertices_;} QVector & normals() {changed = true; return normals_;} @@ -58,7 +58,7 @@ public: bool saveToFile(const QString & filename); bool loadFromFile(const QString & filename); - Box3D boundingBox() const; + Box3D boundingBox(const QMatrix4x4 & mat = QMatrix4x4()) const; private: void calculateBinormals(); diff --git a/qglview/qglview.cpp b/qglview/qglview.cpp index 5c5fdbc..a1474cb 100644 --- a/qglview/qglview.cpp +++ b/qglview/qglview.cpp @@ -267,7 +267,7 @@ void QGLView::paintGL() { renderSelection(); if (shaders_supported) shader_select->release(); uchar cgid[4] = {0, 0, 0, 0}; - uint iid; + uint iid = 0; GLObjectBase * so = 0; if (!rect().contains(lastPos)) { if (hov_obj != 0) { @@ -302,6 +302,7 @@ void QGLView::paintGL() { camera().apply(aspect); start_rp.cam_offset_matrix = camera().offsetMatrix(); + cur_mvpm = start_rp.proj_matrix * start_rp.view_matrix * start_rp.cam_offset_matrix; //objects_.preparePos(camera()); static GLRendererBase * prev_rend = 0; @@ -640,7 +641,7 @@ void QGLView::mouseMoveEvent(QMouseEvent * e) { emit glMouseMoveEvent(new QMouseEvent(QEvent::MouseMove, QPoint(dx, dy), e->button(), e->buttons(), e->modifiers())); return; } - //emit glMouseMoveEvent(e); + emit glMouseMoveEvent(e); } diff --git a/qglview/qglview.h b/qglview/qglview.h index f383da9..5972139 100644 --- a/qglview/qglview.h +++ b/qglview/qglview.h @@ -202,6 +202,7 @@ public: void selectObject(GLObjectBase * o); GLdouble aspect, iaspect; + QMatrix4x4 cur_mvpm; //__GLShaderProgram__ * shader_rope; protected: diff --git a/qglview/renderer_deferred_shading.cpp b/qglview/renderer_deferred_shading.cpp index 1df6c89..a0b3f89 100644 --- a/qglview/renderer_deferred_shading.cpp +++ b/qglview/renderer_deferred_shading.cpp @@ -53,7 +53,7 @@ fbo_g(5, true, GL_RGBA16F), fbo_out(3, false, GL_RGBA16F), fbo_hsmall(1, false, pal.setBrush(QPalette::Window, QColor(255, 255, 255, 192)); df->setPalette(pal); if (view_) - view_->addObject(df); + ;//view_->addObject(df); }