diff --git a/core/glbuffer.cpp b/core/glbuffer.cpp index d0f00f6..bdd75aa 100644 --- a/core/glbuffer.cpp +++ b/core/glbuffer.cpp @@ -1,36 +1,36 @@ /* - QGL Buffer - Ivan Pelipenko peri4ko@yandex.ru + QGL Buffer + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #define GL_GLEXT_PROTOTYPES -#include #include "glbuffer.h" +#include + Buffer::Buffer(GLenum target, GLenum _usage) { - target_ = target; - usage_ = _usage; - buffer_ = 0; + target_ = target; + usage_ = _usage; + buffer_ = 0; prev_size = 0; } -Buffer::~Buffer() { -} +Buffer::~Buffer() {} void Buffer::init(QOpenGLExtraFunctions * f) { @@ -49,13 +49,13 @@ void Buffer::destroy(QOpenGLExtraFunctions * f) { void Buffer::reinit() { - buffer_ = 0; + buffer_ = 0; prev_size = 0; } void Buffer::bind(QOpenGLExtraFunctions * f) { - //qDebug() << "bind" << target_ << buffer_; + // qDebug() << "bind" << target_ << buffer_; f->glBindBuffer(target_, buffer_); } @@ -78,10 +78,10 @@ void Buffer::unmap(QOpenGLExtraFunctions * f) { bool Buffer::resize(QOpenGLExtraFunctions * f, int new_size) { if (new_size <= 0) return false; - //qDebug() << "check resize buffer" << buffer_ << "bytes" << new_size << ", old =" << prev_size; + // qDebug() << "check resize buffer" << buffer_ << "bytes" << new_size << ", old =" << prev_size; if (new_size <= prev_size) return false; prev_size = new_size; - //qDebug() << "resize buffer " << buffer_ << target_ << "for" << new_size << "bytes"; + // qDebug() << "resize buffer " << buffer_ << target_ << "for" << new_size << "bytes"; f->glBufferData(target_, new_size, 0, usage_); return true; } @@ -89,6 +89,6 @@ bool Buffer::resize(QOpenGLExtraFunctions * f, int new_size) { void Buffer::load(QOpenGLExtraFunctions * f, const void * data, int size, int offset) { if (!data || size <= 0) return; - //qDebug() << "load buffer" << buffer_ << "bytes" << size; + // qDebug() << "load buffer" << buffer_ << "bytes" << size; f->glBufferSubData(target_, offset, size, data); } diff --git a/core/glbuffer.h b/core/glbuffer.h index 486403f..986ce43 100644 --- a/core/glbuffer.h +++ b/core/glbuffer.h @@ -1,19 +1,19 @@ /* - QGL Buffer - Ivan Pelipenko peri4ko@yandex.ru + QGL Buffer + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLBUFFER_H @@ -22,37 +22,36 @@ #include "gltypes.h" -class Buffer -{ +class Buffer { friend class ObjectBase; + public: Buffer(GLenum target, GLenum usage = GL_DYNAMIC_DRAW); ~Buffer(); - void init (QOpenGLExtraFunctions * f); - void destroy (QOpenGLExtraFunctions * f); + void init(QOpenGLExtraFunctions * f); + void destroy(QOpenGLExtraFunctions * f); void reinit(); - void bind (QOpenGLExtraFunctions * f); - void release (QOpenGLExtraFunctions * f); - void * map (QOpenGLExtraFunctions * f, GLbitfield mode, int size = -1); - void unmap (QOpenGLExtraFunctions * f); + void bind(QOpenGLExtraFunctions * f); + void release(QOpenGLExtraFunctions * f); + void * map(QOpenGLExtraFunctions * f, GLbitfield mode, int size = -1); + void unmap(QOpenGLExtraFunctions * f); // returns true if size changed - bool resize (QOpenGLExtraFunctions * f, int new_size); - void load (QOpenGLExtraFunctions * f, const void * data, int size, int offset = 0); + bool resize(QOpenGLExtraFunctions * f, int new_size); + void load(QOpenGLExtraFunctions * f, const void * data, int size, int offset = 0); - GLuint ID() const {return buffer_;} - GLenum usage() const {return usage_;} - GLenum target() const {return target_;} - void setTarget(GLenum t) {target_ = t;} - bool isInit() const {return buffer_ != 0;} + GLuint ID() const { return buffer_; } + GLenum usage() const { return usage_; } + GLenum target() const { return target_; } + void setTarget(GLenum t) { target_ = t; } + bool isInit() const { return buffer_ != 0; } private: GLenum target_, usage_; GLuint buffer_; int prev_size; - }; diff --git a/core/glcubemap.cpp b/core/glcubemap.cpp index 190fd94..c0c6eb4 100644 --- a/core/glcubemap.cpp +++ b/core/glcubemap.cpp @@ -1,23 +1,24 @@ /* - QGL CubeTexture - Ivan Pelipenko peri4ko@yandex.ru + QGL CubeTexture + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ -#include "gltypes.h" #include "glcubemap.h" + +#include "gltypes.h" #include "hdr_p.h" using namespace QGLEngineShaders; @@ -51,9 +52,9 @@ QVector loadFileHDR(const QString & path, QSize * size) { qDebug() << "[QGLEngine] File" << path << "has unknown size!"; return ret; } - sz.setWidth (sl[3].toInt()); + sz.setWidth(sl[3].toInt()); sz.setHeight(sl[1].toInt()); - //qDebug() << "found size" << sz; + // qDebug() << "found size" << sz; break; } } @@ -61,26 +62,23 @@ QVector loadFileHDR(const QString & path, QSize * size) { f.seek(ts.pos()); QDataStream ds(&f); int count = sz.width() * sz.height(); - QVector data(count*3); - if (!RGBE_ReadPixels_RLE(&ds, data.data(), sz.width(), sz.height())) - return ret; + QVector data(count * 3); + if (!RGBE_ReadPixels_RLE(&ds, data.data(), sz.width(), sz.height())) return ret; if (size) *size = sz; ret.resize(count); - //QColor col; - //QImage im(sz, QImage::Format_ARGB32); - //QRgb * imdata = (QRgb*)im.bits(); + // QColor col; + // QImage im(sz, QImage::Format_ARGB32); + // QRgb * imdata = (QRgb*)im.bits(); for (int i = 0; i < count; ++i) { - QVector3D p(pow(data[i*3 + 2], 1. / 2.2), - pow(data[i*3 + 1], 1. / 2.2), - pow(data[i*3 + 0], 1. / 2.2)); + QVector3D p(pow(data[i * 3 + 2], 1. / 2.2), pow(data[i * 3 + 1], 1. / 2.2), pow(data[i * 3 + 0], 1. / 2.2)); ret[i] = p; - //col = QColor::fromRgbF(piClamp(p[0], 0.f, 1.f), + // col = QColor::fromRgbF(piClamp(p[0], 0.f, 1.f), // piClamp(p[1], 0.f, 1.f), // piClamp(p[2], 0.f, 1.f)); - //imdata[i] = col.rgb(); + // imdata[i] = col.rgb(); } - //im.save("_hdr.png"); + // im.save("_hdr.png"); return ret; } @@ -90,72 +88,71 @@ QVector faceHDR(const QVector & data, QSize sz, QSize & fs QVector ret; if (data.isEmpty() || sz.isNull()) return ret; QRect fr; - int fw = sz.width () / 4; + int fw = sz.width() / 4; int fh = sz.height() / 3; - fsz = QSize(fw, fh); + fsz = QSize(fw, fh); ret.reserve(fw * fh); switch (face) { case GL_TEXTURE_CUBE_MAP_POSITIVE_X: fr.setRect(fw, fh, fw, fh); for (int x = fr.left(); x <= fr.right(); ++x) { for (int y = fr.top(); y <= fr.bottom(); ++y) { - ret << data[y*sz.width() + x]; + ret << data[y * sz.width() + x]; } } break; case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: - fr.setRect(fw*3, fh, fw, fh); + fr.setRect(fw * 3, fh, fw, fh); for (int x = fr.right(); x >= fr.left(); --x) { for (int y = fr.bottom(); y >= fr.top(); --y) { - ret << data[y*sz.width() + x]; + ret << data[y * sz.width() + x]; } } break; case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: - fr.setRect( 0, fh, fw, fh); + fr.setRect(0, fh, fw, fh); for (int y = fr.bottom(); y >= fr.top(); --y) { for (int x = fr.left(); x <= fr.right(); ++x) { - ret << data[y*sz.width() + x]; + ret << data[y * sz.width() + x]; } } break; case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: - fr.setRect(fw*2, fh, fw, fh); + fr.setRect(fw * 2, fh, fw, fh); for (int y = fr.top(); y <= fr.bottom(); ++y) { for (int x = fr.right(); x >= fr.left(); --x) { - ret << data[y*sz.width() + x]; + ret << data[y * sz.width() + x]; } } break; case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: - fr.setRect(fw, 0, fw, fh); + fr.setRect(fw, 0, fw, fh); for (int x = fr.left(); x <= fr.right(); ++x) { for (int y = fr.top(); y <= fr.bottom(); ++y) { - ret << data[y*sz.width() + x]; + ret << data[y * sz.width() + x]; } } break; case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: - fr.setRect(fw, fh*2, fw, fh); + fr.setRect(fw, fh * 2, fw, fh); for (int x = fr.left(); x <= fr.right(); ++x) { for (int y = fr.top(); y <= fr.bottom(); ++y) { - ret << data[y*sz.width() + x]; + ret << data[y * sz.width() + x]; } } break; default: break; } if (fr.isEmpty()) return ret; - //qDebug() << ret.size() << fr; + // qDebug() << ret.size() << fr; return ret; } - CubeTexture::CubeTexture(QOpenGLExtraFunctions * f_, int _size, const GLenum & _format): f(f_) { - size = _size; - format_ = _format; - id_ = 0; + size = _size; + format_ = _format; + id_ = 0; changed_ = false; } @@ -199,9 +196,17 @@ void CubeTexture::loadHDR(const QVector & data, QSize sz) { QVector fd; for (int i = 0; i < 6; ++i) { fd = faceHDR(data, sz, fsz, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i); - //qDebug() << "load cube" << fd[0]; - f->glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format_, fsz.width(), fsz.height(), 0, GL_RGB, GL_FLOAT, fd.isEmpty() ? 0 : fd.constData()); - //qDebug() << QString::number(GetLastError(), 16); + // qDebug() << "load cube" << fd[0]; + f->glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, + 0, + format_, + fsz.width(), + fsz.height(), + 0, + GL_RGB, + GL_FLOAT, + fd.isEmpty() ? 0 : fd.constData()); + // qDebug() << QString::number(GetLastError(), 16); } f->glGenerateMipmap(GL_TEXTURE_CUBE_MAP); } @@ -230,4 +235,3 @@ void CubeTexture::load() { } changed_ = false; } - diff --git a/core/glcubemap.h b/core/glcubemap.h index 2aebd2c..744a844 100644 --- a/core/glcubemap.h +++ b/core/glcubemap.h @@ -1,26 +1,26 @@ /* - QGL CubeTexture - Ivan Pelipenko peri4ko@yandex.ru + QGL CubeTexture + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLCUBEMAP_H #define GLCUBEMAP_H -#include "glshaders_types.h" #include "chunkstream.h" +#include "glshaders_types.h" QVector loadFileHDR(const QString & path, QSize * size = 0); @@ -31,17 +31,23 @@ public: void destroy(); void bind(int channel = 0); void release(); - void resize(int _size) {size = _size; changed_ = true;} + void resize(int _size) { + size = _size; + changed_ = true; + } void loadHDR(const QVector & data, QSize sz); void setFileHDR(const QString & path); - QString fileHDR() const {return hdr_path;} - GLenum format() const {return format_;} - void setFormat(GLenum f) {format_ = f; changed_ = true;} - GLuint id() const {return id_;} - bool isInit() const {return id_ != 0;} + QString fileHDR() const { return hdr_path; } + GLenum format() const { return format_; } + void setFormat(GLenum f) { + format_ = f; + changed_ = true; + } + GLuint id() const { return id_; } + bool isInit() const { return id_ != 0; } void load(); -private: +private: QOpenGLExtraFunctions * f; bool changed_; int size; diff --git a/core/glframebuffer.cpp b/core/glframebuffer.cpp index 41eccd3..6e2a267 100644 --- a/core/glframebuffer.cpp +++ b/core/glframebuffer.cpp @@ -1,51 +1,54 @@ /* - QGL Framebuffer - Ivan Pelipenko peri4ko@yandex.ru + QGL Framebuffer + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ -#include #include "glframebuffer.h" + +#include #include Framebuffer::Framebuffer(QOpenGLExtraFunctions * f_, int colorAttachments_, bool withDepth, GLenum colorFormat_, GLenum _target) -: f(f_), pbo(GL_PIXEL_PACK_BUFFER, GL_STREAM_DRAW) { + : f(f_) + , pbo(GL_PIXEL_PACK_BUFFER, GL_STREAM_DRAW) { is_depth = withDepth; - target_ = _target; + target_ = _target; color_formats.fill(colorFormat_, colorAttachments_); colors.fill(0, colorAttachments_); fbo = drbo = 0; - tex_d = 0; - wid = hei = 0; + tex_d = 0; + wid = hei = 0; pbo_queried = 0; - is_changed = false; + is_changed = false; } Framebuffer::Framebuffer(QOpenGLExtraFunctions * f_, QVector colors_, bool withDepth, GLenum _target) -: f(f_), pbo(GL_PIXEL_PACK_BUFFER, GL_STREAM_DRAW) { - is_depth = withDepth; - target_ = _target; + : f(f_) + , pbo(GL_PIXEL_PACK_BUFFER, GL_STREAM_DRAW) { + is_depth = withDepth; + target_ = _target; color_formats = colors_; colors.fill(0, colors_.size()); fbo = drbo = 0; - tex_d = 0; - wid = hei = 0; + tex_d = 0; + wid = hei = 0; pbo_queried = 0; - is_changed = false; + is_changed = false; } @@ -67,7 +70,7 @@ void Framebuffer::resize(int width, int height, bool force) { if (fbo > 0) deleteGLFramebuffer(fbo); f->glGenFramebuffers(1, &fbo); f->glBindFramebuffer(GL_FRAMEBUFFER, fbo); - //qDebug() << "resize" << f << wid << hei << fbo; + // qDebug() << "resize" << f << wid << hei << fbo; for (int i = 0; i < colors.size(); ++i) { deleteGLTexture(f, colors[i]); createGLTexture(f, colors[i], width, height, color_formats[i], target_); @@ -103,10 +106,10 @@ void Framebuffer::resize(int width, int height, bool force) { void Framebuffer::reinit() { pbo.reinit(); colors.fill(0); - fbo = drbo = 0; - tex_d = 0; + fbo = drbo = 0; + tex_d = 0; pbo_queried = 0; - is_changed = true; + is_changed = true; } @@ -152,8 +155,7 @@ uint Framebuffer::getPoint() const { uint ret = 0; pbo.bind(f); void * map = pbo.map(f, GL_MAP_READ_BIT, sizeof(uint)); - if (map) - memcpy(&ret, map, sizeof(uint)); + if (map) memcpy(&ret, map, sizeof(uint)); pbo.unmap(f); pbo.release(f); return ret; @@ -166,8 +168,7 @@ QVector Framebuffer::getPointsByte() const { ret.resize(pbo_queried); pbo.bind(f); void * map = pbo.map(f, GL_MAP_READ_BIT, pbo_queried * sizeof(uint)); - if (map) - memcpy(ret.data(), map, pbo_queried * sizeof(uint)); + if (map) memcpy(ret.data(), map, pbo_queried * sizeof(uint)); pbo.unmap(f); pbo.release(f); return ret; @@ -180,8 +181,7 @@ QVector Framebuffer::getPointsFloat() const { ret.resize(pbo_queried); pbo.bind(f); void * map = pbo.map(f, GL_MAP_READ_BIT, pbo_queried * sizeof(QVector4D)); - if (map) - memcpy(ret.data(), map, pbo_queried * sizeof(QVector4D)); + if (map) memcpy(ret.data(), map, pbo_queried * sizeof(QVector4D)); pbo.unmap(f); pbo.release(f); return ret; @@ -191,12 +191,11 @@ QVector Framebuffer::getPointsFloat() const { QImage Framebuffer::getImage() const { QImage ret; if (!pbo.isInit() || (pbo_queried == 0)) return ret; - ret = QImage(size(), QImage::Format_RGBA8888); + ret = QImage(size(), QImage::Format_RGBA8888); int bytes = width() * height() * 4; pbo.bind(f); void * map = pbo.map(f, GL_MAP_READ_BIT, bytes); - if (map) - memcpy(ret.bits(), map, bytes); + if (map) memcpy(ret.bits(), map, bytes); pbo.unmap(f); pbo.release(f); return ret; @@ -277,7 +276,7 @@ void Framebuffer::unsetWriteBuffers() { void Framebuffer::enablePixelBuffer() { pbo.init(f); pbo.bind(f); - pbo.resize(f, width()*height()*4*4); + pbo.resize(f, width() * height() * 4 * 4); pbo.release(f); } @@ -311,14 +310,12 @@ void Framebuffer::bindDepthTexture(int channel) { void Framebuffer::deleteGLRenderbuffer(GLuint & drbo) { - if (drbo != 0) - f->glDeleteRenderbuffers(1, &drbo); + if (drbo != 0) f->glDeleteRenderbuffers(1, &drbo); drbo = 0; } void Framebuffer::deleteGLFramebuffer(GLuint & fbo) { - if (fbo != 0) - f->glDeleteFramebuffers(1, &fbo); + if (fbo != 0) f->glDeleteFramebuffers(1, &fbo); fbo = 0; } diff --git a/core/glframebuffer.h b/core/glframebuffer.h index 138d4b2..242a73f 100644 --- a/core/glframebuffer.h +++ b/core/glframebuffer.h @@ -1,19 +1,19 @@ /* - QGL Framebuffer - Ivan Pelipenko peri4ko@yandex.ru + QGL Framebuffer + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLFRAMEBUFFER_H @@ -22,23 +22,27 @@ #include "glbuffer.h" -class Framebuffer -{ +class Framebuffer { friend class FramebufferMipmap; + public: - Framebuffer(QOpenGLExtraFunctions * f_, int colorAttachments = 1, bool withDepth = true, GLenum colorFormat = GL_RGBA8, GLenum _target = GL_TEXTURE_2D); + Framebuffer(QOpenGLExtraFunctions * f_, + int colorAttachments = 1, + bool withDepth = true, + GLenum colorFormat = GL_RGBA8, + GLenum _target = GL_TEXTURE_2D); Framebuffer(QOpenGLExtraFunctions * f_, QVector colors_, bool withDepth = true, GLenum _target = GL_TEXTURE_2D); virtual ~Framebuffer(); - GLuint id() const {return fbo;} - GLuint colorTexture(int index = 0) const {return colors[index];} - GLuint depthTexture() const {return tex_d;} - GLenum target() const {return target_;} - bool isInit() const {return fbo != 0;} - int width() const {return wid;} - int height() const {return hei;} - QSize size() const {return QSize(wid, hei);} - QRect rect() const {return QRect(0, 0, wid, hei);} + GLuint id() const { return fbo; } + GLuint colorTexture(int index = 0) const { return colors[index]; } + GLuint depthTexture() const { return tex_d; } + GLenum target() const { return target_; } + bool isInit() const { return fbo != 0; } + int width() const { return wid; } + int height() const { return hei; } + QSize size() const { return QSize(wid, hei); } + QRect rect() const { return QRect(0, 0, wid, hei); } QImage grab() const; QVector grabF(int index) const; void queryPoint(int index, QPoint p); @@ -48,23 +52,29 @@ public: QVector getPointsByte() const; QVector getPointsFloat() const; QImage getImage() const; - int queriedPoints() const {return pbo_queried;} - void blit(int index_from, GLuint fb_to, int index_to, QRect from, QRect to, GLbitfield mask = GL_COLOR_BUFFER_BIT, GLenum filter = GL_NEAREST) const; + int queriedPoints() const { return pbo_queried; } + void blit(int index_from, + GLuint fb_to, + int index_to, + QRect from, + QRect to, + GLbitfield mask = GL_COLOR_BUFFER_BIT, + GLenum filter = GL_NEAREST) const; void resize(int width, int height, bool force = false); void reinit(); void bind(); void release(); - void setReadBuffer(int index) {glReadBuffer(GL_COLOR_ATTACHMENT0 + index);} + void setReadBuffer(int index) { glReadBuffer(GL_COLOR_ATTACHMENT0 + index); } void setWriteBuffer(int index); void setWriteBuffers(const int * indeces, int count); - void setWriteBuffers(const QVector & indeces) {setWriteBuffers(indeces.constData(), indeces.size());} + void setWriteBuffers(const QVector & indeces) { setWriteBuffers(indeces.constData(), indeces.size()); } void setWriteBuffers(); void unsetWriteBuffers(); void enablePixelBuffer(); void setColorTextureFiltering(int index, GLenum filter); - void copyDepthFrom(GLuint tex) {;} + void copyDepthFrom(GLuint tex) { ; } void bindColorTexture(int index, int channel = 0); void bindColorTextures(); void bindDepthTexture(int channel); diff --git a/core/glframebuffer_mipmap.cpp b/core/glframebuffer_mipmap.cpp index 38c5ba2..cb7f291 100644 --- a/core/glframebuffer_mipmap.cpp +++ b/core/glframebuffer_mipmap.cpp @@ -1,23 +1,24 @@ /* - QGL FramebufferMipmap - Ivan Pelipenko peri4ko@yandex.ru + QGL FramebufferMipmap + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ -#include #include "glframebuffer_mipmap.h" + +#include #include @@ -28,9 +29,7 @@ FramebufferMipmap::FramebufferMipmap(const Framebuffer & fb, int index_from_, in } -FramebufferMipmap::~FramebufferMipmap() { - -} +FramebufferMipmap::~FramebufferMipmap() {} void FramebufferMipmap::resize() { diff --git a/core/glframebuffer_mipmap.h b/core/glframebuffer_mipmap.h index 34ceb00..29a5164 100644 --- a/core/glframebuffer_mipmap.h +++ b/core/glframebuffer_mipmap.h @@ -1,19 +1,19 @@ /* - QGL FramebufferMipmap - Ivan Pelipenko peri4ko@yandex.ru + QGL FramebufferMipmap + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLFRAMEBUFFER_MIPMAP_H @@ -22,20 +22,19 @@ #include "glframebuffer.h" -class FramebufferMipmap -{ +class FramebufferMipmap { public: FramebufferMipmap(const Framebuffer & fb, int index_from_, int levels = 2); virtual ~FramebufferMipmap(); - int levelsCount() const {return fbo.size();} - int lastLevel() const {return fbo.size() - 1;} - Framebuffer & plane(int level) {return *fbo[level];} - Framebuffer & lastPlane() {return *fbo[lastLevel()];} - int width (int level) const {return fbo[level]->wid;} - int height(int level) const {return fbo[level]->hei;} - QSize size(int level) const {return fbo[level]->size();} - QRect rect(int level) const {return fbo[level]->rect();} + int levelsCount() const { return fbo.size(); } + int lastLevel() const { return fbo.size() - 1; } + Framebuffer & plane(int level) { return *fbo[level]; } + Framebuffer & lastPlane() { return *fbo[lastLevel()]; } + int width(int level) const { return fbo[level]->wid; } + int height(int level) const { return fbo[level]->hei; } + QSize size(int level) const { return fbo[level]->size(); } + QRect rect(int level) const { return fbo[level]->rect(); } void resize(); void create(); @@ -44,8 +43,7 @@ public: private: int index_from; const Framebuffer & src_fb; - QVector fbo; - + QVector fbo; }; #endif // GLFRAMEBUFFER_MIPMAP_H diff --git a/core/glmaterial.cpp b/core/glmaterial.cpp index 874948a..b38aed2 100644 --- a/core/glmaterial.cpp +++ b/core/glmaterial.cpp @@ -1,48 +1,47 @@ /* - QGL Material - Ivan Pelipenko peri4ko@yandex.ru + QGL Material + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ -#include "gltypes.h" #include "gltexture_manager.h" +#include "gltypes.h" #include "qglview.h" using namespace QGLEngineShaders; Map::Map() { - bitmap_id = 0; + bitmap_id = 0; color_amount = 1.f; color_offset = 0.f; bitmap_scale = QPointF(1., 1.); - use_bitmap = false; - _changed = true; - _layer = 0; + use_bitmap = false; + _changed = true; + _layer = 0; } void Map::setBitmapPath(const QString & p) { bitmap_path = p; - _changed = true; + _changed = true; } void Map::load(TextureManager * tm) { - if (bitmap_id == 0) - bitmap_id = tm->loadTexture(bitmap_path, true, _type == mtNormal); + if (bitmap_id == 0) bitmap_id = tm->loadTexture(bitmap_path, true, _type == mtNormal); } @@ -60,21 +59,19 @@ void Map::copyToQGLMap(QGLMap & m) const { } - - Material::Material(const QString _name) { setTypes(); - name = _name; - color_diffuse = Qt::white; + name = _name; + color_diffuse = Qt::white; color_emission = Qt::black; - glass = false; + glass = false; transparency = reflectivity = 0.f; - map_roughness.color_amount = 0.75f; - map_metalness.color_amount = 0.25f; - iof = 1.f; - dispersion = 0.05f; - _changed = true; - _index = 0; + map_roughness.color_amount = 0.75f; + map_metalness.color_amount = 0.25f; + iof = 1.f; + dispersion = 0.05f; + _changed = true; + _index = 0; } @@ -89,63 +86,59 @@ bool Material::hasTransparency() const { bool Material::isMapsChanged() const { - return map_diffuse ._changed || - map_normal ._changed || - map_metalness._changed || - map_roughness._changed || - map_emission ._changed || - map_relief ._changed; + return map_diffuse._changed || map_normal._changed || map_metalness._changed || map_roughness._changed || map_emission._changed || + map_relief._changed; } bool Material::isMapChanged(int type) const { switch (type) { - case mtDiffuse : return map_diffuse ._changed; - case mtNormal : return map_normal ._changed; + case mtDiffuse: return map_diffuse._changed; + case mtNormal: return map_normal._changed; case mtMetalness: return map_metalness._changed; case mtRoughness: return map_roughness._changed; - case mtEmission : return map_emission ._changed; - case mtRelief : return map_relief ._changed; + case mtEmission: return map_emission._changed; + case mtRelief: return map_relief._changed; } return false; } void Material::load(TextureManager * tm) { - map_diffuse .load(tm); - map_normal .load(tm); + map_diffuse.load(tm); + map_normal.load(tm); map_metalness.load(tm); map_roughness.load(tm); - map_emission .load(tm); - map_relief .load(tm); + map_emission.load(tm); + map_relief.load(tm); } void Material::setMapsChanged() { - map_diffuse ._changed = true; - map_normal ._changed = true; + map_diffuse._changed = true; + map_normal._changed = true; map_metalness._changed = true; map_roughness._changed = true; - map_emission ._changed = true; - map_relief ._changed = true; + map_emission._changed = true; + map_relief._changed = true; } void Material::setTypes() { - map_diffuse ._type = mtDiffuse ; - map_normal ._type = mtNormal ; + map_diffuse._type = mtDiffuse; + map_normal._type = mtNormal; map_metalness._type = mtMetalness; map_roughness._type = mtRoughness; - map_emission ._type = mtEmission ; - map_relief ._type = mtRelief ; + map_emission._type = mtEmission; + map_relief._type = mtRelief; } void Material::detectMaps() { - map_diffuse .use_bitmap = !map_diffuse .bitmap_path.isEmpty(); - map_normal .use_bitmap = !map_normal .bitmap_path.isEmpty(); + map_diffuse.use_bitmap = !map_diffuse.bitmap_path.isEmpty(); + map_normal.use_bitmap = !map_normal.bitmap_path.isEmpty(); map_metalness.use_bitmap = !map_metalness.bitmap_path.isEmpty(); map_roughness.use_bitmap = !map_roughness.bitmap_path.isEmpty(); - map_emission .use_bitmap = !map_emission .bitmap_path.isEmpty(); - map_relief .use_bitmap = !map_relief .bitmap_path.isEmpty(); + map_emission.use_bitmap = !map_emission.bitmap_path.isEmpty(); + map_relief.use_bitmap = !map_relief.bitmap_path.isEmpty(); } diff --git a/core/glmaterial.h b/core/glmaterial.h index 4ed4148..c0ffa06 100644 --- a/core/glmaterial.h +++ b/core/glmaterial.h @@ -1,38 +1,38 @@ /* - QGL Material - Ivan Pelipenko peri4ko@yandex.ru + QGL Material + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLMATERIAL_H #define GLMATERIAL_H -#include "glshaders_types.h" #include "chunkstream.h" +#include "glshaders_types.h" class Map { public: Map(); void setBitmapPath(const QString & p); - void clearBitmap() {setBitmapPath(QString());} - bool hasBitmap() const {return !bitmap_path.isEmpty();} + void clearBitmap() { setBitmapPath(QString()); } + bool hasBitmap() const { return !bitmap_path.isEmpty(); } void load(TextureManager * tm); void copyToQGLMap(QGLEngineShaders::QGLMap & m) const; QString bitmap_path; - GLuint bitmap_id; + GLuint bitmap_id; QPointF bitmap_offset; QPointF bitmap_scale; float color_amount; @@ -62,51 +62,60 @@ public: float reflectivity; float iof; float dispersion; - Map map_diffuse ; - Map map_normal ; + Map map_diffuse; + Map map_normal; Map map_metalness; Map map_roughness; - Map map_emission ; - Map map_relief ; + Map map_emission; + Map map_relief; bool _changed; int _index; }; -inline QDataStream & operator <<(QDataStream & s, const Map & m) { +inline QDataStream & operator<<(QDataStream & s, const Map & m) { ChunkStream cs; - cs.add(1, m.bitmap_path).add(2, m.color_amount).add(3, m.color_offset).add(6, m.bitmap_scale) - .add(7, m.use_bitmap); - s << cs.data(); return s; + cs.add(1, m.bitmap_path).add(2, m.color_amount).add(3, m.color_offset).add(6, m.bitmap_scale).add(7, m.use_bitmap); + s << cs.data(); + return s; } -inline QDataStream & operator >>(QDataStream & s, Map & m) { +inline QDataStream & operator>>(QDataStream & s, Map & m) { ChunkStream cs(s); cs.readAll(); - cs.get(1, m.bitmap_path).get(2, m.color_amount).get(3, m.color_offset).get(6, m.bitmap_scale) - .get(7, m.use_bitmap); + cs.get(1, m.bitmap_path).get(2, m.color_amount).get(3, m.color_offset).get(6, m.bitmap_scale).get(7, m.use_bitmap); return s; } -inline QDataStream & operator <<(QDataStream & s, const Material * m) { +inline QDataStream & operator<<(QDataStream & s, const Material * m) { ChunkStream cs; - cs.add(1, m->name).add(2, m->color_diffuse).add(4, m->color_emission) - .add(5, m->transparency).add(6, m->reflectivity).add(7, m->glass).add(8, m->map_diffuse).add(9, m->map_normal) - .add(10, m->map_relief).add(11, m->map_metalness).add(12, m->map_roughness).add(13, m->map_emission); - s << (cs.data()); return s; + cs.add(1, m->name) + .add(2, m->color_diffuse) + .add(4, m->color_emission) + .add(5, m->transparency) + .add(6, m->reflectivity) + .add(7, m->glass) + .add(8, m->map_diffuse) + .add(9, m->map_normal) + .add(10, m->map_relief) + .add(11, m->map_metalness) + .add(12, m->map_roughness) + .add(13, m->map_emission); + s << (cs.data()); + return s; } -inline QDataStream & operator >>(QDataStream & s, Material *& m) { +inline QDataStream & operator>>(QDataStream & s, Material *& m) { m = new Material(); ChunkStream cs(s); while (!cs.atEnd()) { switch (cs.read()) { - case 1: cs.get(m->name); break; - case 2: cs.get(m->color_diffuse); break; - case 4: cs.get(m->color_emission); break; - case 5: cs.get(m->transparency); break; - case 6: cs.get(m->reflectivity); break; - case 7: cs.get(m->glass); break; - case 8: cs.get(m->map_diffuse); break; - case 9: cs.get(m->map_normal); break; + case 1: cs.get(m->name); break; + case 2: cs.get(m->color_diffuse); break; + case 4: cs.get(m->color_emission); break; + case 5: cs.get(m->transparency); break; + case 6: cs.get(m->reflectivity); break; + case 7: cs.get(m->glass); break; + case 8: cs.get(m->map_diffuse); break; + case 9: cs.get(m->map_normal); break; case 10: cs.get(m->map_relief); break; case 11: cs.get(m->map_metalness); break; case 12: cs.get(m->map_roughness); break; diff --git a/core/glmesh.cpp b/core/glmesh.cpp index 1180c4e..347d70c 100644 --- a/core/glmesh.cpp +++ b/core/glmesh.cpp @@ -1,56 +1,59 @@ /* - QGL Mesh - Ivan Pelipenko peri4ko@yandex.ru + QGL Mesh + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #define GL_GLEXT_PROTOTYPES -#include #include "glmesh.h" + #include "globject.h" + +#include #include using namespace QGLEngineShaders; -//static int _count = 0; +// static int _count = 0; -Mesh::Mesh(GLenum geom_type_): geom_type(geom_type_), - buffer_geom(GL_ARRAY_BUFFER, GL_STATIC_DRAW), - buffer_ind (GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW) { - hash_ = 0; +Mesh::Mesh(GLenum geom_type_) + : geom_type(geom_type_) + , buffer_geom(GL_ARRAY_BUFFER, GL_STATIC_DRAW) + , buffer_ind(GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW) { + hash_ = 0; changed = hash_changed = true; - //qDebug() << "Mesh, now" << ++_count; + // qDebug() << "Mesh, now" << ++_count; } Mesh::~Mesh() { - //qDebug() << "~Mesh, now" << --_count; + // qDebug() << "~Mesh, now" << --_count; } Mesh * Mesh::clone() { - Mesh * c = new Mesh(); - c->vertices_ = vertices_ ; - c->normals_ = normals_ ; - c->texcoords_ = texcoords_; - c->triangles_ = triangles_; - c->lines_ = lines_; - c->geom_type = geom_type; - c->hash_ = hash_; + Mesh * c = new Mesh(); + c->vertices_ = vertices_; + c->normals_ = normals_; + c->texcoords_ = texcoords_; + c->triangles_ = triangles_; + c->lines_ = lines_; + c->geom_type = geom_type; + c->hash_ = hash_; c->hash_changed = hash_changed; - //qDebug() << "clone VBO"; + // qDebug() << "clone VBO"; return c; } @@ -58,7 +61,7 @@ Mesh * Mesh::clone() { void Mesh::init(QOpenGLExtraFunctions * f) { if (!isInit()) { buffer_geom.init(f); - buffer_ind .init(f); + buffer_ind.init(f); changed = true; } } @@ -66,8 +69,8 @@ void Mesh::init(QOpenGLExtraFunctions * f) { void Mesh::reinit() { buffer_geom.reinit(); - buffer_ind .reinit(); - QMapIterator it(vao_map); + buffer_ind.reinit(); + QMapIterator it(vao_map); while (it.hasNext()) it.next().value()->reinit(); changed = true; @@ -76,9 +79,9 @@ void Mesh::reinit() { void Mesh::destroy(QOpenGLExtraFunctions * f) { buffer_geom.destroy(f); - buffer_ind .destroy(f); - QList vaol = vao_map.values(); - foreach (VertexObject* vao, vaol) + buffer_ind.destroy(f); + QList vaol = vao_map.values(); + foreach(VertexObject * vao, vaol) vao->destroy(f); qDeleteAll(vao_map); vao_map.clear(); @@ -88,12 +91,12 @@ void Mesh::destroy(QOpenGLExtraFunctions * f) { void Mesh::calculateNormals() { normals_.resize(vertices_.size()); QVector3D dv1, dv2, n; - foreach (const Vector3i & t, triangles_) { + foreach(const Vector3i & t, triangles_) { QVector3D & v0(vertices_[t.p0]); QVector3D & v1(vertices_[t.p1]); QVector3D & v2(vertices_[t.p2]); dv1 = v1 - v0, dv2 = v2 - v0; - n = QVector3D::crossProduct(dv1, dv2).normalized(); + n = QVector3D::crossProduct(dv1, dv2).normalized(); normals_[t.p0] = n; normals_[t.p1] = n; normals_[t.p2] = n; @@ -104,32 +107,32 @@ void Mesh::calculateNormals() { void Mesh::calculateTangents() { if (vertices_.isEmpty() || texcoords_.isEmpty()) return; if (texcoords_.size() != vertices_.size()) return; - tangents_ .resize(vertices_.size()); + tangents_.resize(vertices_.size()); bitangents_.resize(vertices_.size()); - //qDebug() << "calculateBinormals" << vcnt << tcnt << vertices_.size() << texcoords_.size() << "..."; + // qDebug() << "calculateBinormals" << vcnt << tcnt << vertices_.size() << texcoords_.size() << "..."; QVector3D dv1, dv2; QVector2D dt1, dt2; QVector3D tan, bitan; - foreach (const Vector3i & t, triangles_) { - QVector3D & v0(vertices_ [t.p0]); - QVector3D & v1(vertices_ [t.p1]); - QVector3D & v2(vertices_ [t.p2]); + foreach(const Vector3i & t, triangles_) { + QVector3D & v0(vertices_[t.p0]); + QVector3D & v1(vertices_[t.p1]); + QVector3D & v2(vertices_[t.p2]); QVector2D & t0(texcoords_[t.p0]); QVector2D & t1(texcoords_[t.p1]); QVector2D & t2(texcoords_[t.p2]); dv1 = v1 - v0, dv2 = v2 - v0; dt1 = t1 - t0, dt2 = t2 - t0; - tan = (dv1 * dt2.y() - dv2 * dt1.y()).normalized(); - bitan = (dv2 * dt1.x() - dv1 * dt2.x()).normalized(); - tangents_ [t.p0] = tan; - tangents_ [t.p1] = tan; - tangents_ [t.p2] = tan; + tan = (dv1 * dt2.y() - dv2 * dt1.y()).normalized(); + bitan = (dv2 * dt1.x() - dv1 * dt2.x()).normalized(); + tangents_[t.p0] = tan; + tangents_[t.p1] = tan; + tangents_[t.p2] = tan; bitangents_[t.p0] = bitan; bitangents_[t.p1] = bitan; bitangents_[t.p2] = bitan; - //qDebug() << " t" << t << vi << ti << dv1.toQVector3D() << "..."; + // qDebug() << " t" << t << vi << ti << dv1.toQVector3D() << "..."; } - //qDebug() << "calculateBinormals" << vcnt << tcnt << tangents_.size(); + // qDebug() << "calculateBinormals" << vcnt << tcnt << tangents_.size(); } @@ -145,8 +148,7 @@ VertexObject * Mesh::vaoByType(int type) { bool Mesh::rebuffer(QOpenGLExtraFunctions * f) { changed = false; if (vertices_.isEmpty()) return true; - if (normals_.isEmpty()) - calculateNormals(); + if (normals_.isEmpty()) calculateNormals(); calculateTangents(); vert_count = qMin(vertices_.size(), normals_.size()); vert_count = qMin(vert_count, tangents_.size()); @@ -155,11 +157,11 @@ bool Mesh::rebuffer(QOpenGLExtraFunctions * f) { data_.resize(vert_count); for (int i = 0; i < vert_count; ++i) { Vertex & v(data_[i]); - v.pos = vertices_ [i]; - v.normal = normals_ [i]; - v.tangent = tangents_ [i]; + v.pos = vertices_[i]; + v.normal = normals_[i]; + v.tangent = tangents_[i]; v.bitangent = bitangents_[i]; - v.tex = texcoords_ [i]; + v.tex = texcoords_[i]; } int gsize = data_.size() * sizeof(Vertex); int tsize = triangles_.size() * sizeof(Vector3i); @@ -186,7 +188,7 @@ void Mesh::draw(QOpenGLExtraFunctions * f, int count, int type) { if (isEmpty()) return; if (!isInit()) init(f); if (changed) rebuffer(f); - //qDebug() << "draw" << geom_type << vert_count << count; + // qDebug() << "draw" << geom_type << vert_count << count; VertexObject * vao = vaoByType(type); vao->bindBuffers(f, buffer_geom, buffer_ind); @@ -198,14 +200,14 @@ void Mesh::draw(QOpenGLExtraFunctions * f, int count, int type) { void Mesh::clear() { - vertices_ .clear(); - normals_ .clear(); - tangents_ .clear(); + vertices_.clear(); + normals_.clear(); + tangents_.clear(); bitangents_.clear(); - texcoords_ .clear(); - triangles_ .clear(); - lines_ .clear(); - data_ .clear(); + texcoords_.clear(); + triangles_.clear(); + lines_.clear(); + data_.clear(); changed = hash_changed = true; } @@ -231,23 +233,23 @@ void Mesh::loadSelections(QOpenGLExtraFunctions * f, const QVector & sels uint Mesh::hash() const { if (hash_changed) { hash_changed = false; - hash_ = qHashBits(vertices_ .constData(), vertices_ .size() * sizeof(QVector3D)); - hash_ ^= qHashBits(normals_ .constData(), normals_ .size() * sizeof(QVector3D)); + hash_ = qHashBits(vertices_.constData(), vertices_.size() * sizeof(QVector3D)); + hash_ ^= qHashBits(normals_.constData(), normals_.size() * sizeof(QVector3D)); hash_ ^= qHashBits(texcoords_.constData(), texcoords_.size() * sizeof(QVector2D)); - hash_ ^= qHashBits(triangles_.constData(), triangles_.size() * sizeof( Vector3i)); - hash_ ^= qHashBits(lines_ .constData(), lines_ .size() * sizeof( Vector2i)); + hash_ ^= qHashBits(triangles_.constData(), triangles_.size() * sizeof(Vector3i)); + hash_ ^= qHashBits(lines_.constData(), lines_.size() * sizeof(Vector2i)); } return hash_; } bool Mesh::isObjectsChanged(int type) const { - return (const_cast(this))->vaoByType(type)->isObjectsChanged(); + return (const_cast(this))->vaoByType(type)->isObjectsChanged(); } bool Mesh::isSelectionChanged(int type) const { - return (const_cast(this))->vaoByType(type)->isSelectionChanged(); + return (const_cast(this))->vaoByType(type)->isSelectionChanged(); } @@ -262,14 +264,14 @@ void Mesh::setSelectionChanged(int type, bool yes) { void Mesh::setAllObjectsChanged(bool yes) { - QMapIterator it(vao_map); + QMapIterator it(vao_map); while (it.hasNext()) it.next().value()->setObjectsChanged(yes); } void Mesh::setAllSelectionChanged(bool yes) { - QMapIterator it(vao_map); + QMapIterator it(vao_map); while (it.hasNext()) it.next().value()->setSelectionChanged(yes); } @@ -301,8 +303,7 @@ void Mesh::transformPoints(const QMatrix4x4 & mat) { int vcnt = vertices_.size(), ncnt = normals_.size(); for (int i = 0; i < vcnt; ++i) { vertices_[i] = (mat * QVector4D(vertices_[i], 1)).toVector3D(); - if (i < ncnt) - normals_[i] = (mat * QVector4D(normals_[i], 0)).toVector3D(); + if (i < ncnt) normals_[i] = (mat * QVector4D(normals_[i], 0)).toVector3D(); } changed = hash_changed = true; } @@ -326,8 +327,8 @@ void Mesh::append(const Mesh * m) { if (m->isEmpty()) return; if (normals_.isEmpty()) calculateNormals(); int vcnt = vertices_.size(); - vertices_ .append(m->vertices_ ); - normals_ .append(m->normals_ ); + vertices_.append(m->vertices_); + normals_.append(m->normals_); texcoords_.append(m->texcoords_); QVector tri = m->triangles_; for (int i = 0; i < tri.size(); ++i) @@ -378,7 +379,7 @@ bool Mesh::loadFromFile(const QString & filename) { Box3D Mesh::boundingBox() const { if (vertices_.isEmpty()) return Box3D(); int vcnt = vertices_.size(); - //qDebug() << "calculateBinormals" << vcnt << tcnt << vertices_.size() << texcoords_.size() << "..."; + // qDebug() << "calculateBinormals" << vcnt << tcnt << vertices_.size() << texcoords_.size() << "..."; GLfloat mix, miy, miz, max, may, maz; QVector3D v0(vertices_[0]); mix = max = v0.x(); @@ -394,9 +395,9 @@ Box3D Mesh::boundingBox() const { if (miz > v.z()) miz = v.z(); if (maz < v.z()) maz = v.z(); } - bound.x = mix; - bound.y = miy; - bound.z = miz; + bound.x = mix; + bound.y = miy; + bound.z = miz; bound.length = max - mix; bound.width = may - miy; bound.height = maz - miz; @@ -404,26 +405,26 @@ Box3D Mesh::boundingBox() const { } -QDataStream & operator <<(QDataStream & s, const Mesh * m) { +QDataStream & operator<<(QDataStream & s, const Mesh * m) { ChunkStream cs; - //qDebug() << "place VBO" << m.vertices_.size() << m.normals_.size() << m.texcoords_.size() << m.colors_.size() << "..."; - cs.add(1, m->vertices_).add(2, m->normals_).add(3, m->texcoords_) - .add(6, m->triangles_).add(7, m->lines_).add(10, int(m->geom_type)); - //qDebug() << "place VBO done" << cs.data().size() << "..."; - s << cs.data(); return s; + // qDebug() << "place VBO" << m.vertices_.size() << m.normals_.size() << m.texcoords_.size() << m.colors_.size() << "..."; + cs.add(1, m->vertices_).add(2, m->normals_).add(3, m->texcoords_).add(6, m->triangles_).add(7, m->lines_).add(10, int(m->geom_type)); + // qDebug() << "place VBO done" << cs.data().size() << "..."; + s << cs.data(); + return s; } -QDataStream & operator >>(QDataStream & s, Mesh *& m) { +QDataStream & operator>>(QDataStream & s, Mesh *& m) { m = new Mesh(); ChunkStream cs(s); while (!cs.atEnd()) { switch (cs.read()) { - case 1 : cs.get(m->vertices_ ); break; - case 2 : cs.get(m->normals_ ); break; - case 3 : cs.get(m->texcoords_); break; - case 6 : cs.get(m->triangles_); break; - case 7 : cs.get(m->lines_ ); break; + case 1: cs.get(m->vertices_); break; + case 2: cs.get(m->normals_); break; + case 3: cs.get(m->texcoords_); break; + case 6: cs.get(m->triangles_); break; + case 7: cs.get(m->lines_); break; case 10: m->geom_type = cs.getData(); break; } } diff --git a/core/glmesh.h b/core/glmesh.h index 5c384b2..dfe205c 100644 --- a/core/glmesh.h +++ b/core/glmesh.h @@ -1,74 +1,92 @@ /* - QGL Mesh - Ivan Pelipenko peri4ko@yandex.ru + QGL Mesh + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLMESH_H #define GLMESH_H -#include #include "glvertexobject.h" +#include + + +class Mesh { + friend QDataStream & operator<<(QDataStream & s, const Mesh * m); + friend QDataStream & operator>>(QDataStream & s, Mesh *& m); -class Mesh -{ - friend QDataStream & operator <<(QDataStream & s, const Mesh * m); - friend QDataStream & operator >>(QDataStream & s, Mesh *& m); public: Mesh(GLenum geom_type_ = GL_TRIANGLES); ~Mesh(); Mesh * clone(); - void init (QOpenGLExtraFunctions * f); - void destroy (QOpenGLExtraFunctions * f); + void init(QOpenGLExtraFunctions * f); + void destroy(QOpenGLExtraFunctions * f); bool rebuffer(QOpenGLExtraFunctions * f); - void draw (QOpenGLExtraFunctions * f, int count, int type = 0); + void draw(QOpenGLExtraFunctions * f, int count, int type = 0); void reinit(); void clear(); - void loadObject (QOpenGLExtraFunctions * f, const QGLEngineShaders::Object & object, int type = 0); - void loadObjects (QOpenGLExtraFunctions * f, const QVector & objects, int type = 0); + void loadObject(QOpenGLExtraFunctions * f, const QGLEngineShaders::Object & object, int type = 0); + void loadObjects(QOpenGLExtraFunctions * f, const QVector & objects, int type = 0); void loadSelections(QOpenGLExtraFunctions * f, const QVector & sels, int type = 0); - int verticesCount() const {return vertices_.size();} - int trianglesCount() const {return triangles_.size();} - int linesCount() const {return lines_.size();} - bool isInit() const {return buffer_geom.isInit();} - bool isEmpty() const {return vertices_.isEmpty();} + int verticesCount() const { return vertices_.size(); } + int trianglesCount() const { return triangles_.size(); } + int linesCount() const { return lines_.size(); } + bool isInit() const { return buffer_geom.isInit(); } + bool isEmpty() const { return vertices_.isEmpty(); } uint hash() const; - bool isObjectsChanged (int type = 0) const; - bool isSelectionChanged (int type = 0) const; - void setObjectsChanged (int type = 0, bool yes = true); + bool isObjectsChanged(int type = 0) const; + bool isSelectionChanged(int type = 0) const; + void setObjectsChanged(int type = 0, bool yes = true); void setSelectionChanged(int type = 0, bool yes = true); - void setAllObjectsChanged (bool yes = true); + void setAllObjectsChanged(bool yes = true); void setAllSelectionChanged(bool yes = true); - QVector & vertices () {changed = hash_changed = true; return vertices_;} - QVector & normals () {changed = hash_changed = true; return normals_;} - QVector & texcoords() {changed = hash_changed = true; return texcoords_;} - QVector< Vector3i> & indicesTriangles() {changed = hash_changed = true; return triangles_;} - QVector< Vector2i> & indicesLines () {changed = hash_changed = true; return lines_;} + QVector & vertices() { + changed = hash_changed = true; + return vertices_; + } + QVector & normals() { + changed = hash_changed = true; + return normals_; + } + QVector & texcoords() { + changed = hash_changed = true; + return texcoords_; + } + QVector & indicesTriangles() { + changed = hash_changed = true; + return triangles_; + } + QVector & indicesLines() { + changed = hash_changed = true; + return lines_; + } void translatePoints(const QVector3D & dp); - void translatePoints(const double & x, const double & y, const double & z) {translatePoints(QVector3D(x, y, z));} - void scalePoints (const QVector3D & dp); - void scalePoints (const double & s) {scalePoints(QVector3D(s, s, s));} - void rotatePoints (const double & angle, const QVector3D & a); - void rotatePoints (const double & angle, const double & x, const double & y, const double & z) {rotatePoints(angle, QVector3D(x, y, z));} + void translatePoints(const double & x, const double & y, const double & z) { translatePoints(QVector3D(x, y, z)); } + void scalePoints(const QVector3D & dp); + void scalePoints(const double & s) { scalePoints(QVector3D(s, s, s)); } + void rotatePoints(const double & angle, const QVector3D & a); + void rotatePoints(const double & angle, const double & x, const double & y, const double & z) { + rotatePoints(angle, QVector3D(x, y, z)); + } void transformPoints(const QMatrix4x4 & mat); void flipNormals(); void append(const Mesh * m); @@ -85,13 +103,13 @@ private: QVector vertices_, normals_, tangents_, bitangents_; QVector texcoords_; - QVector< Vector3i> triangles_; - QVector< Vector2i> lines_; + QVector triangles_; + QVector lines_; QVector data_; GLenum geom_type; Buffer buffer_geom, buffer_ind; - QMap vao_map; + QMap vao_map; mutable uint hash_; mutable bool hash_changed; int vert_count; @@ -99,7 +117,7 @@ private: }; -QDataStream & operator <<(QDataStream & s, const Mesh * m); -QDataStream & operator >>(QDataStream & s, Mesh *& m); +QDataStream & operator<<(QDataStream & s, const Mesh * m); +QDataStream & operator>>(QDataStream & s, Mesh *& m); #endif // GLMESH_H diff --git a/core/glprimitives.cpp b/core/glprimitives.cpp index 70e7307..6c7e302 100644 --- a/core/glprimitives.cpp +++ b/core/glprimitives.cpp @@ -1,34 +1,36 @@ /* - QGL Primitives - Ivan Pelipenko peri4ko@yandex.ru + QGL Primitives + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "glprimitives.h" + #include "glmesh.h" Mesh * Primitive::plane(float width, float length) { Mesh * ret = new Mesh(); - QVector & v(ret->vertices ()); - QVector & n(ret->normals ()); + QVector & v(ret->vertices()); + QVector & n(ret->normals()); QVector & t(ret->texcoords()); - QVector< Vector3i> & i(ret->indicesTriangles ()); + QVector & i(ret->indicesTriangles()); float hw = width / 2.f, hl = length / 2.f; - for (int j = 0; j < 4; ++j) n << QVector3D(0., 0., 1.); - t << QVector2D(0., 0.) << QVector2D(0., 1.) << QVector2D(1., 1.) << QVector2D(1., 0.); + for (int j = 0; j < 4; ++j) + n << QVector3D(0., 0., 1.); + t << QVector2D(0., 0.) << QVector2D(0., 1.) << QVector2D(1., 1.) << QVector2D(1., 0.); v << QVector3D(-hw, -hl, 0.) << QVector3D(-hw, hl, 0.) << QVector3D(hw, hl, 0.) << QVector3D(hw, -hl, 0.); i << Vector3i(0, 2, 1) << Vector3i(0, 3, 2); return ret; @@ -38,17 +40,18 @@ Mesh * Primitive::plane(float width, float length) { Mesh * Primitive::cube(float width, float length, float height) { Mesh * ret = new Mesh(); QVector3D scale(width, length, height); - QVector & v(ret->vertices ()); - QVector & n(ret->normals ()); + QVector & v(ret->vertices()); + QVector & n(ret->normals()); QVector & t(ret->texcoords()); - QVector< Vector3i> & i(ret->indicesTriangles ()); + QVector & i(ret->indicesTriangles()); float hs = 0.5f; - int si = 0; + int si = 0; QMatrix4x4 mat; si = v.size(); - for (int j = 0; j < 4; ++j) n << QVector3D(0., -1., 0.); - t << QVector2D(0., 0.) << QVector2D(1., 0.) << QVector2D(1., 1.) << QVector2D(0., 1.); + for (int j = 0; j < 4; ++j) + n << QVector3D(0., -1., 0.); + t << QVector2D(0., 0.) << QVector2D(1., 0.) << QVector2D(1., 1.) << QVector2D(0., 1.); v << QVector3D(-hs, -hs, -hs) << QVector3D(hs, -hs, -hs) << QVector3D(hs, -hs, hs) << QVector3D(-hs, -hs, hs); i << Vector3i(si + 0, si + 1, si + 2) << Vector3i(si + 0, si + 2, si + 3); @@ -60,21 +63,21 @@ Mesh * Primitive::cube(float width, float length, float height) { n << cn; v << mat.map(QVector4D(v[j])).toVector3D(); } - t << QVector2D(0., 0.) << QVector2D(1., 0.) << QVector2D(1., 1.) << QVector2D(0., 1.); + t << QVector2D(0., 0.) << QVector2D(1., 0.) << QVector2D(1., 1.) << QVector2D(0., 1.); i << Vector3i(si + 0, si + 1, si + 2) << Vector3i(si + 0, si + 2, si + 3); } mat.setToIdentity(); - mat.rotate(90., 1., 0.,0.); + mat.rotate(90., 1., 0., 0.); for (int r = 0; r < 2; ++r) { si = v.size(); - mat.rotate(180., 1., 0.,0.); + mat.rotate(180., 1., 0., 0.); QVector3D cn = mat.map(n[0]); for (int j = 0; j < 4; ++j) { n << cn; v << mat.map(QVector4D(v[j])).toVector3D(); } - t << QVector2D(0., 0.) << QVector2D(1., 0.) << QVector2D(1., 1.) << QVector2D(0., 1.); + t << QVector2D(0., 0.) << QVector2D(1., 0.) << QVector2D(1., 1.) << QVector2D(0., 1.); i << Vector3i(si + 0, si + 1, si + 2) << Vector3i(si + 0, si + 2, si + 3); } @@ -87,10 +90,10 @@ Mesh * Primitive::cube(float width, float length, float height) { Mesh * Primitive::ellipsoid(int segments_wl, int segments_h, float radius, float end_angle) { Mesh * ret = new Mesh(); - QVector & v(ret->vertices ()); - QVector & n(ret->normals ()); + QVector & v(ret->vertices()); + QVector & n(ret->normals()); QVector & t(ret->texcoords()); - QVector< Vector3i> & ind(ret->indicesTriangles()); + QVector & ind(ret->indicesTriangles()); int hseg = segments_h + 1, wlseg = segments_wl + 1; double crw, crl, a, ch, twl; double eang = deg2rad * end_angle; @@ -99,16 +102,16 @@ Mesh * Primitive::ellipsoid(int segments_wl, int segments_h, float radius, float for (int i = 0; i <= hseg; i++) { ch = -cos((double)i / hseg * M_PI); cp.setZ(ch * radius); - twl = sqrt(1. - ch * ch); - crw = twl * radius; - crl = twl * radius; + twl = sqrt(1. - ch * ch); + crw = twl * radius; + crl = twl * radius; int cvcnt = wlseg * 2; for (int j = 0; j < cvcnt; j++) { a = (double)j / (cvcnt - 1) * eang; cp.setX(crl * cos(a)); cp.setY(crw * sin(a)); v << cp; - t << QVector2D((double)j / (cvcnt - 1), ch/2.f + 0.5f); + t << QVector2D((double)j / (cvcnt - 1), ch / 2.f + 0.5f); n << cp.normalized(); int si = v.size() - 1; if (j > 0 && i > 0) { @@ -118,7 +121,7 @@ Mesh * Primitive::ellipsoid(int segments_wl, int segments_h, float radius, float } } if (end_angle < 360.) { - Mesh * cap = Primitive::disc(segments_h+1, radius, 180); + Mesh * cap = Primitive::disc(segments_h + 1, radius, 180); cap->rotatePoints(90, 0, 1, 0); cap->rotatePoints(-90, 0, 0, 1); ret->append(cap); @@ -133,10 +136,10 @@ Mesh * Primitive::ellipsoid(int segments_wl, int segments_h, float radius, float Mesh * Primitive::disc(int segments, float radius, float end_angle) { Mesh * ret = new Mesh(); - QVector & v(ret->vertices ()); - QVector & n(ret->normals ()); + QVector & v(ret->vertices()); + QVector & n(ret->normals()); QVector & t(ret->texcoords()); - QVector< Vector3i> & ind(ret->indicesTriangles()); + QVector & ind(ret->indicesTriangles()); segments = qMax(segments + 1, 4); QVector3D cp; @@ -173,10 +176,10 @@ QVector3D coneNormal(double r, double height, double ang) { Mesh * Primitive::cone(int segments, float radius, float height) { Mesh * ret = new Mesh(); - QVector & v(ret->vertices ()); - QVector & n(ret->normals ()); + QVector & v(ret->vertices()); + QVector & n(ret->normals()); QVector & t(ret->texcoords()); - QVector< Vector3i> & ind(ret->indicesTriangles()); + QVector & ind(ret->indicesTriangles()); int seg = qMax(segments + 1, 4); QVector3D cp; @@ -194,8 +197,7 @@ Mesh * Primitive::cone(int segments, float radius, float height) { t << QVector2D((double)i / (seg - 1), 0.f); n << coneNormal(radius, height, a); int si = v.size() - 1; - if (i > 0) - ind << Vector3i(si - 1, si - 2, si); + if (i > 0) ind << Vector3i(si - 1, si - 2, si); } Mesh * cap = Primitive::disc(segments, radius); @@ -209,10 +211,10 @@ Mesh * Primitive::cone(int segments, float radius, float height) { Mesh * Primitive::cylinder(int segments, float radius, float height, float end_angle) { Mesh * ret = new Mesh(); - QVector & v(ret->vertices ()); - QVector & n(ret->normals ()); + QVector & v(ret->vertices()); + QVector & n(ret->normals()); QVector & t(ret->texcoords()); - QVector< Vector3i> & ind(ret->indicesTriangles()); + QVector & ind(ret->indicesTriangles()); int seg = qMax(segments + 1, 4); QVector3D cp, norm; @@ -229,7 +231,8 @@ Mesh * Primitive::cylinder(int segments, float radius, float height, float end_a v << cp; t << QVector2D((double)i / (seg - 1), 0.f); t << QVector2D((double)i / (seg - 1), 1.f); - n << norm; n << norm; + n << norm; + n << norm; int si = v.size() - 1; if (i > 0) { ind << Vector3i(si - 2, si - 1, si); @@ -248,7 +251,7 @@ Mesh * Primitive::cylinder(int segments, float radius, float height, float end_a if (end_angle < 360.) { Mesh * cap = Primitive::plane(radius, height); cap->rotatePoints(90, 1, 0, 0); - cap->translatePoints(radius/2, 0, height/2); + cap->translatePoints(radius / 2, 0, height / 2); ret->append(cap); cap->flipNormals(); cap->rotatePoints(end_angle, 0, 0, 1); @@ -263,8 +266,8 @@ Mesh * Primitive::cylinder(int segments, float radius, float height, float end_a Mesh * Primitive::arrow(int segments, float thick, float angle) { double cone_r = 1.5 * thick; double cone_h = 2. * cone_r / tan(angle * deg2rad); - Mesh * ret = new Mesh(); - Mesh * m = Primitive::cylinder(segments, thick / 2., 1. - cone_h); + Mesh * ret = new Mesh(); + Mesh * m = Primitive::cylinder(segments, thick / 2., 1. - cone_h); ret->append(m); delete m; m = Primitive::cone(segments, cone_r, cone_h); @@ -277,10 +280,10 @@ Mesh * Primitive::arrow(int segments, float thick, float angle) { Mesh * Primitive::torus(int segments_main, int segments_second, float radius_main, float radius_second, float end_angle) { Mesh * ret = new Mesh(); - QVector & v(ret->vertices ()); - QVector & n(ret->normals ()); + QVector & v(ret->vertices()); + QVector & n(ret->normals()); QVector & t(ret->texcoords()); - QVector< Vector3i> & ind(ret->indicesTriangles()); + QVector & ind(ret->indicesTriangles()); QVector cv, cn; QVector ct; @@ -315,7 +318,7 @@ Mesh * Primitive::torus(int segments_main, int segments_second, float radius_mai pcnt = v.size(); } if (end_angle < 360.) { - Mesh * cap = Primitive::disc(segments_second-1, radius_second); + Mesh * cap = Primitive::disc(segments_second - 1, radius_second); cap->rotatePoints(90, 1, 0, 0); cap->translatePoints(radius_main, 0, 0); ret->append(cap); @@ -328,22 +331,20 @@ Mesh * Primitive::torus(int segments_main, int segments_second, float radius_mai } - - Mesh * Primitive::cubeFrame(float width, float length, float height) { Mesh * ret = new Mesh(GL_LINES); QVector3D scale(width, length, height); - QVector & v(ret->vertices ()); - QVector & n(ret->normals ()); + QVector & v(ret->vertices()); + QVector & n(ret->normals()); QVector & t(ret->texcoords()); - QVector< Vector2i> & i(ret->indicesLines()); + QVector & i(ret->indicesLines()); float hs = 0.5f; - v << QVector3D(-hs, -hs, -hs) << QVector3D(-hs, hs, -hs) << QVector3D( hs, hs, -hs) << QVector3D( hs, -hs, -hs); - v << QVector3D(-hs, -hs, hs) << QVector3D(-hs, hs, hs) << QVector3D( hs, hs, hs) << QVector3D( hs, -hs, hs); + v << QVector3D(-hs, -hs, -hs) << QVector3D(-hs, hs, -hs) << QVector3D(hs, hs, -hs) << QVector3D(hs, -hs, -hs); + v << QVector3D(-hs, -hs, hs) << QVector3D(-hs, hs, hs) << QVector3D(hs, hs, hs) << QVector3D(hs, -hs, hs); for (int j = 0; j < 8; ++j) { v[j] *= scale; t << QVector2D(0, 0); - n << QVector3D(0,0,1); + n << QVector3D(0, 0, 1); } for (int j = 0; j < 4; ++j) { i << Vector2i(j, (j + 1) % 4); @@ -356,10 +357,10 @@ Mesh * Primitive::cubeFrame(float width, float length, float height) { Mesh * Primitive::ellipsoidFrame(int segments_wl, int segments_h, float radius) { Mesh * ret = new Mesh(GL_LINES); - QVector & v(ret->vertices ()); - QVector & n(ret->normals ()); + QVector & v(ret->vertices()); + QVector & n(ret->normals()); QVector & t(ret->texcoords()); - QVector< Vector2i> & ind(ret->indicesLines()); + QVector & ind(ret->indicesLines()); int hseg = segments_h + 1, wlseg = segments_wl + 1; double crw, crl, a, ch, twl; @@ -367,16 +368,16 @@ Mesh * Primitive::ellipsoidFrame(int segments_wl, int segments_h, float radius) for (int i = 0; i <= hseg; i++) { ch = -cos((double)i / hseg * M_PI); cp.setZ(ch * radius); - twl = sqrt(1. - ch * ch); - crw = twl * radius; - crl = twl * radius; + twl = sqrt(1. - ch * ch); + crw = twl * radius; + crl = twl * radius; int cvcnt = wlseg * 2; for (int j = 0; j < cvcnt; j++) { a = (double)j / (cvcnt - 1) * M_2PI; cp.setX(crl * cos(a)); cp.setY(crw * sin(a)); v << cp; - t << QVector2D((double)j / (cvcnt - 1), ch/2.f + 0.5f); + t << QVector2D((double)j / (cvcnt - 1), ch / 2.f + 0.5f); n << cp.normalized(); int si = v.size() - 1; if (j > 0 && i > 0) { @@ -391,10 +392,10 @@ Mesh * Primitive::ellipsoidFrame(int segments_wl, int segments_h, float radius) Mesh * Primitive::coneFrame(int segments, float radius, float height) { Mesh * ret = new Mesh(GL_LINES); - QVector & v(ret->vertices ()); - QVector & n(ret->normals ()); + QVector & v(ret->vertices()); + QVector & n(ret->normals()); QVector & t(ret->texcoords()); - QVector< Vector2i> & ind(ret->indicesLines()); + QVector & ind(ret->indicesLines()); int seg = qMax(segments + 1, 4); QVector3D cp; @@ -424,13 +425,13 @@ Mesh * Primitive::coneFrame(int segments, float radius, float height) { Mesh * Primitive::lineFrame(QVector3D p0, QVector3D p1) { Mesh * ret = new Mesh(GL_LINES); - QVector & v(ret->vertices ()); - QVector & n(ret->normals ()); + QVector & v(ret->vertices()); + QVector & n(ret->normals()); QVector & t(ret->texcoords()); - QVector< Vector2i> & ind(ret->indicesLines()); + QVector & ind(ret->indicesLines()); v << p0 << p1; - n << QVector3D(0,0,1) << QVector3D(0,0,1); - t << QVector2D(0,0) << QVector2D(1,0); + n << QVector3D(0, 0, 1) << QVector3D(0, 0, 1); + t << QVector2D(0, 0) << QVector2D(1, 0); ind << Vector2i(0, 1); return ret; } diff --git a/core/glprimitives.h b/core/glprimitives.h index 78208fb..9e88f9d 100644 --- a/core/glprimitives.h +++ b/core/glprimitives.h @@ -1,19 +1,19 @@ /* - QGL Primitives - Ivan Pelipenko peri4ko@yandex.ru + QGL Primitives + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLPRIMITIVE_CUBE_H @@ -50,6 +50,6 @@ Mesh * ellipsoidFrame(int segments_wl, int segments_h, float radius = 1.); Mesh * coneFrame(int segments, float radius = 1., float height = 1.); -} +} // namespace Primitive #endif // GLPRIMITIVE_CUBE_H diff --git a/core/glshaders.cpp b/core/glshaders.cpp index b5a8bd2..3b414d1 100644 --- a/core/glshaders.cpp +++ b/core/glshaders.cpp @@ -1,35 +1,41 @@ /* - QGLEngineShaders - Ivan Pelipenko peri4ko@yandex.ru + QGLEngineShaders + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ +#include "glshaders.h" + +#include "glshaders_headers.h" #include "gltypes.h" #include "qglview.h" -#include "glshaders.h" -#include "glshaders_headers.h" using namespace QGLEngineShaders; -bool addShader(QOpenGLShaderProgram * prog, QOpenGLShader::ShaderType type, QString & content, const QString & file, bool add_qgl, const QString & defs) { +bool addShader(QOpenGLShaderProgram * prog, + QOpenGLShader::ShaderType type, + QString & content, + const QString & file, + bool add_qgl, + const QString & defs) { if (type == 0 || content.isEmpty()) { content.clear(); return true; } - //qDebug() << "[QGLEngine] Shader" << file << "found" << (QOpenGLShader::ShaderTypeBit)(int)type << "section ..."; + // qDebug() << "[QGLEngine] Shader" << file << "found" << (QOpenGLShader::ShaderTypeBit)(int)type << "section ..."; if (add_qgl) { switch (type) { case QOpenGLShader::Fragment: @@ -37,12 +43,8 @@ bool addShader(QOpenGLShaderProgram * prog, QOpenGLShader::ShaderType type, QStr content.prepend(qgl_uniform); content.prepend(qgl_structs); break; - case QOpenGLShader::Vertex : - content.prepend(qgl_vertex_head ); - break; - case QOpenGLShader::Geometry: - content.prepend(qgl_geometry_head); - break; + case QOpenGLShader::Vertex: content.prepend(qgl_vertex_head); break; + case QOpenGLShader::Geometry: content.prepend(qgl_geometry_head); break; } } content.prepend(defs); @@ -57,15 +59,14 @@ bool addShader(QOpenGLShaderProgram * prog, QOpenGLShader::ShaderType type, QStr QString prepareDefines(const QStringList & defines) { if (defines.isEmpty()) return QString(); QString ret; - foreach (QString s, defines) + foreach(QString s, defines) ret.append("#define " + s + "\n"); return ret; } bool QGLEngineShaders::loadShadersMulti(QOpenGLShaderProgram *& prog, const QString & file, bool add_qgl, const QStringList & defines) { - if (!prog) - prog = new QOpenGLShaderProgram(); + if (!prog) prog = new QOpenGLShaderProgram(); QFile f(file); if (!f.open(QIODevice::ReadOnly)) { qDebug() << "[QGLEngine] Shader" << file << "Error: can`t open file!"; @@ -76,7 +77,7 @@ bool QGLEngineShaders::loadShadersMulti(QOpenGLShaderProgram *& prog, const QStr QOpenGLShader::ShaderType type = QOpenGLShader::ShaderType(); while (!ts.atEnd()) { line = ts.readLine(); - pl = line.trimmed().remove(' ').remove('\t').mid(2).toLower(); + pl = line.trimmed().remove(' ').remove('\t').mid(2).toLower(); pl.chop(2); if (pl == "vertex" || pl == "vert") { if (!addShader(prog, type, cur_shader, file, add_qgl, defs)) return false; @@ -117,14 +118,13 @@ bool QGLEngineShaders::loadShadersMulti(QOpenGLShaderProgram *& prog, const QStr bool QGLEngineShaders::loadShaders(QOpenGLShaderProgram *& prog, const QStringList & files, bool add_qgl, const QStringList & defines) { - if (!prog) - prog = new QOpenGLShaderProgram(); + if (!prog) prog = new QOpenGLShaderProgram(); prog->removeAllShaders(); QString cur_shader, defs = prepareDefines(defines); - foreach (QString f, files) { + foreach(QString f, files) { QFileInfo fi(f); QOpenGLShader::ShaderType type = QOpenGLShader::ShaderType(); - if (fi.suffix().toLower() == "vert") type = QOpenGLShader::Vertex ; + if (fi.suffix().toLower() == "vert") type = QOpenGLShader::Vertex; if (fi.suffix().toLower() == "frag") type = QOpenGLShader::Fragment; if (fi.suffix().toLower() == "geom") type = QOpenGLShader::Geometry; if (type == 0) continue; diff --git a/core/glshaders.h b/core/glshaders.h index 57fc6a9..f513cd5 100644 --- a/core/glshaders.h +++ b/core/glshaders.h @@ -1,19 +1,19 @@ /* - QGLEngineShaders - Ivan Pelipenko peri4ko@yandex.ru + QGLEngineShaders + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLSHADERS_H @@ -26,6 +26,6 @@ namespace QGLEngineShaders { bool loadShadersMulti(QOpenGLShaderProgram *& prog, const QString & file, bool add_qgl = true, const QStringList & defines = QStringList()); bool loadShaders(QOpenGLShaderProgram *& prog, const QStringList & files, bool add_qgl = true, const QStringList & defines = QStringList()); -} +} // namespace QGLEngineShaders #endif // GLSHADERS_H diff --git a/core/glshaders_headers.h b/core/glshaders_headers.h index 5cd1f86..bcdada9 100644 --- a/core/glshaders_headers.h +++ b/core/glshaders_headers.h @@ -1,19 +1,19 @@ /* - QGLEngineShaders - Ivan Pelipenko peri4ko@yandex.ru + QGLEngineShaders + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLSHADERS_HEADERS_H @@ -21,32 +21,30 @@ namespace QGLEngineShaders { -const int max_materials = 128; -const int max_lights = 256 ; +const int max_materials = 128; +const int max_lights = 256; -const char qgl_common_head[] = - "#version 400 core\n" - ""; +const char qgl_common_head[] = "#version 400 core\n" + ""; -const char qgl_vertex_head[] = - "layout(location = 1 ) in vec3 qgl_Vertex ;\n" - "layout(location = 2 ) in vec3 qgl_Normal ;\n" - "layout(location = 3 ) in vec3 qgl_Tangent ;\n" - "layout(location = 4 ) in vec3 qgl_Bitangent ;\n" - "layout(location = 5 ) in vec2 qgl_Texture ;\n" - "layout(location = 6 ) in uint qgl_Material ;\n" - "layout(location = 7 ) in uint qgl_ObjectSelected;\n" - "layout(location = 8 ) in uint qgl_ObjectID ;\n" - "layout(location = 9 ) in vec4 qgl_ObjectColor ;\n" - "layout(location = 10) in mat4 qgl_ModelMatrix ;\n" - "out vec2 qgl_FragTexture;\n" - "flat out uint qgl_MaterialIndex;\n" - "uniform mat4 qgl_ViewMatrix;\n" - "uniform mat4 qgl_ViewProjMatrix;\n" - "mat3 qgl_getNormalMatrix() {return inverse(mat3(qgl_ViewMatrix * qgl_ModelMatrix));}\n" - "mat3 qgl_getTangentMatrix() {return mat3(qgl_ViewMatrix * qgl_ModelMatrix);}\n" - "vec4 qgl_ftransform() {return qgl_ViewProjMatrix * (qgl_ModelMatrix * vec4(qgl_Vertex, 1));}\n" - ""; +const char qgl_vertex_head[] = "layout(location = 1 ) in vec3 qgl_Vertex ;\n" + "layout(location = 2 ) in vec3 qgl_Normal ;\n" + "layout(location = 3 ) in vec3 qgl_Tangent ;\n" + "layout(location = 4 ) in vec3 qgl_Bitangent ;\n" + "layout(location = 5 ) in vec2 qgl_Texture ;\n" + "layout(location = 6 ) in uint qgl_Material ;\n" + "layout(location = 7 ) in uint qgl_ObjectSelected;\n" + "layout(location = 8 ) in uint qgl_ObjectID ;\n" + "layout(location = 9 ) in vec4 qgl_ObjectColor ;\n" + "layout(location = 10) in mat4 qgl_ModelMatrix ;\n" + "out vec2 qgl_FragTexture;\n" + "flat out uint qgl_MaterialIndex;\n" + "uniform mat4 qgl_ViewMatrix;\n" + "uniform mat4 qgl_ViewProjMatrix;\n" + "mat3 qgl_getNormalMatrix() {return inverse(mat3(qgl_ViewMatrix * qgl_ModelMatrix));}\n" + "mat3 qgl_getTangentMatrix() {return mat3(qgl_ViewMatrix * qgl_ModelMatrix);}\n" + "vec4 qgl_ftransform() {return qgl_ViewProjMatrix * (qgl_ModelMatrix * vec4(qgl_Vertex, 1));}\n" + ""; const char qgl_fragment_head[] = "in vec2 qgl_FragTexture;\n" @@ -63,59 +61,56 @@ const char qgl_fragment_head[] = "#define qgl_FragColor qgl_FragData[0]\n" ""; -const char qgl_geometry_head[] = - ""; +const char qgl_geometry_head[] = ""; -const char qgl_structs[] = - "#define QGL_MAPS_COUNT 6\n" - "#define QGL_MAP_DIFFUSE 0\n" - "#define QGL_MAP_NORMAL 1\n" - "#define QGL_MAP_METALNESS 2\n" - "#define QGL_MAP_ROUGHNESS 3\n" - "#define QGL_MAP_EMISSION 4\n" - "#define QGL_MAP_RELIEF 5\n" - "#define QGL_TEXTURE_ARRAY_EMPTY 0\n" - "#define QGL_TEXTURE_ARRAY_MAPS 1\n" - "struct QGLMap {\n" - " float offset;\n" - " float amount;\n" - " vec2 scale;\n" - " uint array_index;\n" - " uint map_index;\n" - "};\n" - "struct QGLMaterial {\n" - " vec4 color_diffuse;\n" - " vec4 color_emission;\n" - " float transparency;\n" - " float reflectivity;\n" - " float iof;\n" - " float dispersion;\n" - " QGLMap map[QGL_MAPS_COUNT];\n" - "};\n" - "struct QGLLightParameter {\n" - " vec4 color;\n" - " vec4 decay_intensity;\n" - " vec4 angles;\n" - "};\n" - "struct QGLLightPosition {\n" - " vec4 position;\n" - " vec4 direction;\n" - "};\n" - ""; +const char qgl_structs[] = "#define QGL_MAPS_COUNT 6\n" + "#define QGL_MAP_DIFFUSE 0\n" + "#define QGL_MAP_NORMAL 1\n" + "#define QGL_MAP_METALNESS 2\n" + "#define QGL_MAP_ROUGHNESS 3\n" + "#define QGL_MAP_EMISSION 4\n" + "#define QGL_MAP_RELIEF 5\n" + "#define QGL_TEXTURE_ARRAY_EMPTY 0\n" + "#define QGL_TEXTURE_ARRAY_MAPS 1\n" + "struct QGLMap {\n" + " float offset;\n" + " float amount;\n" + " vec2 scale;\n" + " uint array_index;\n" + " uint map_index;\n" + "};\n" + "struct QGLMaterial {\n" + " vec4 color_diffuse;\n" + " vec4 color_emission;\n" + " float transparency;\n" + " float reflectivity;\n" + " float iof;\n" + " float dispersion;\n" + " QGLMap map[QGL_MAPS_COUNT];\n" + "};\n" + "struct QGLLightParameter {\n" + " vec4 color;\n" + " vec4 decay_intensity;\n" + " vec4 angles;\n" + "};\n" + "struct QGLLightPosition {\n" + " vec4 position;\n" + " vec4 direction;\n" + "};\n" + ""; -const char qgl_uniform[] = - "layout (std140) uniform QGLMaterialData {\n" - " QGLMaterial qgl_material[128];\n" - "};\n" - "layout (std140) uniform QGLLightParameterData {\n" - " QGLLightParameter qgl_light_parameter[256];\n" - "};\n" - "layout (std140) uniform QGLLightPositionData {\n" - " QGLLightPosition qgl_light_position[256];\n" - "};\n" - "uniform sampler2DArray qgl_texture_array[2];\n" - ""; +const char qgl_uniform[] = "layout (std140) uniform QGLMaterialData {\n" + " QGLMaterial qgl_material[128];\n" + "};\n" + "layout (std140) uniform QGLLightParameterData {\n" + " QGLLightParameter qgl_light_parameter[256];\n" + "};\n" + "layout (std140) uniform QGLLightPositionData {\n" + " QGLLightPosition qgl_light_position[256];\n" + "};\n" + "uniform sampler2DArray qgl_texture_array[2];\n" + ""; -} +} // namespace QGLEngineShaders #endif // GLSHADERS_HEADERS_H diff --git a/core/glshaders_types.cpp b/core/glshaders_types.cpp index fe57b1b..1a3de8a 100644 --- a/core/glshaders_types.cpp +++ b/core/glshaders_types.cpp @@ -1,39 +1,39 @@ /* - QGLEngineShaders - Ivan Pelipenko peri4ko@yandex.ru + QGLEngineShaders + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "glshaders_types.h" QGLEngineShaders::QGLMap::QGLMap() { - offset = 0.; - amount = 1.; - scale = QVector2D(1., 1.); + offset = 0.; + amount = 1.; + scale = QVector2D(1., 1.); array_index = map_index = 0; } QGLEngineShaders::QGLMaterial::QGLMaterial() { - color_diffuse = QVector4D(1., 1., 1., 0.); - color_emission = QVector4D(0., 0., 0., 0.); - transparency = 0.; - reflectivity = 0.; - iof = 0.; - dispersion = 0.; + color_diffuse = QVector4D(1., 1., 1., 0.); + color_emission = QVector4D(0., 0., 0., 0.); + transparency = 0.; + reflectivity = 0.; + iof = 0.; + dispersion = 0.; map[mtNormal].map_index = emrBlue; map[mtRoughness].amount = 0.75; map[mtMetalness].amount = 0.25; @@ -41,57 +41,60 @@ QGLEngineShaders::QGLMaterial::QGLMaterial() { void QGLEngineShaders::prepareDrawGeom(QOpenGLExtraFunctions * f) { - //qDebug() << "prepareDrawGeom"; + // qDebug() << "prepareDrawGeom"; - f->glEnableVertexAttribArray(pos_loc ); - f->glEnableVertexAttribArray(normal_loc ); - f->glEnableVertexAttribArray(tangent_loc ); + f->glEnableVertexAttribArray(pos_loc); + f->glEnableVertexAttribArray(normal_loc); + f->glEnableVertexAttribArray(tangent_loc); f->glEnableVertexAttribArray(bitangent_loc); - f->glEnableVertexAttribArray(tex_loc ); + f->glEnableVertexAttribArray(tex_loc); int size = sizeof(Vertex); - f->glVertexAttribPointer(pos_loc , 3, GL_FLOAT, GL_FALSE, size, (const void *)pos_offset ); - f->glVertexAttribPointer(normal_loc , 3, GL_FLOAT, GL_FALSE, size, (const void *)normal_offset ); - f->glVertexAttribPointer(tangent_loc , 3, GL_FLOAT, GL_FALSE, size, (const void *)tangent_offset ); + f->glVertexAttribPointer(pos_loc, 3, GL_FLOAT, GL_FALSE, size, (const void *)pos_offset); + f->glVertexAttribPointer(normal_loc, 3, GL_FLOAT, GL_FALSE, size, (const void *)normal_offset); + f->glVertexAttribPointer(tangent_loc, 3, GL_FLOAT, GL_FALSE, size, (const void *)tangent_offset); f->glVertexAttribPointer(bitangent_loc, 3, GL_FLOAT, GL_FALSE, size, (const void *)bitangent_offset); - f->glVertexAttribPointer(tex_loc , 2, GL_FLOAT, GL_FALSE, size, (const void *)tex_offset ); + f->glVertexAttribPointer(tex_loc, 2, GL_FLOAT, GL_FALSE, size, (const void *)tex_offset); } void QGLEngineShaders::prepareDrawObj(QOpenGLExtraFunctions * f) { - //qDebug() << "prepareDrawObj"; + // qDebug() << "prepareDrawObj"; - f->glEnableVertexAttribArray(material_loc ); + f->glEnableVertexAttribArray(material_loc); f->glEnableVertexAttribArray(object_id_loc); - f->glEnableVertexAttribArray(color_loc ); + f->glEnableVertexAttribArray(color_loc); for (int i = 0; i < 4; ++i) { f->glEnableVertexAttribArray(modelmatrix_loc + i); } GLsizei size = sizeof(Object); - f->glVertexAttribIPointer(material_loc , 1, GL_UNSIGNED_INT , size, (const void *)material_offset ); - f->glVertexAttribIPointer(object_id_loc, 1, GL_UNSIGNED_INT , size, (const void *)object_id_offset); - f->glVertexAttribPointer (color_loc , 4, GL_FLOAT, GL_FALSE, size, (const void *)color_offset ); + f->glVertexAttribIPointer(material_loc, 1, GL_UNSIGNED_INT, size, (const void *)material_offset); + f->glVertexAttribIPointer(object_id_loc, 1, GL_UNSIGNED_INT, size, (const void *)object_id_offset); + f->glVertexAttribPointer(color_loc, 4, GL_FLOAT, GL_FALSE, size, (const void *)color_offset); for (int i = 0; i < 4; ++i) { - f->glVertexAttribPointer(modelmatrix_loc + i, 4, GL_FLOAT, GL_FALSE, size, (const void *)(modelmatrix_offset + sizeof(QVector4D)*i)); + f->glVertexAttribPointer(modelmatrix_loc + i, + 4, + GL_FLOAT, + GL_FALSE, + size, + (const void *)(modelmatrix_offset + sizeof(QVector4D) * i)); } - f->glVertexAttribDivisor(material_loc , 1); + f->glVertexAttribDivisor(material_loc, 1); f->glVertexAttribDivisor(object_id_loc, 1); - f->glVertexAttribDivisor(color_loc , 1); + f->glVertexAttribDivisor(color_loc, 1); for (int i = 0; i < 4; ++i) { f->glVertexAttribDivisor(modelmatrix_loc + i, 1); } - } void QGLEngineShaders::prepareDrawSel(QOpenGLExtraFunctions * f) { - //qDebug() << "prepareDrawObj"; + // qDebug() << "prepareDrawObj"; f->glEnableVertexAttribArray(is_selected_loc); GLsizei size = 1; f->glVertexAttribIPointer(is_selected_loc, 1, GL_UNSIGNED_BYTE, size, (const void *)is_selected_offset); f->glVertexAttribDivisor(is_selected_loc, 1); - } diff --git a/core/glshaders_types.h b/core/glshaders_types.h index fcdaa79..4bec346 100644 --- a/core/glshaders_types.h +++ b/core/glshaders_types.h @@ -1,19 +1,19 @@ /* - QGLEngineShaders - Ivan Pelipenko peri4ko@yandex.ru + QGLEngineShaders + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLSHADERS_TYPES_H @@ -27,32 +27,32 @@ namespace QGLEngineShaders { /// VBO // geometry -const GLsizei pos_offset = 0; -const GLsizei normal_offset = sizeof(QVector3D) + pos_offset ; -const GLsizei tangent_offset = sizeof(QVector3D) + normal_offset ; -const GLsizei bitangent_offset = sizeof(QVector3D) + tangent_offset ; -const GLsizei tex_offset = sizeof(QVector3D) + bitangent_offset; +const GLsizei pos_offset = 0; +const GLsizei normal_offset = sizeof(QVector3D) + pos_offset; +const GLsizei tangent_offset = sizeof(QVector3D) + normal_offset; +const GLsizei bitangent_offset = sizeof(QVector3D) + tangent_offset; +const GLsizei tex_offset = sizeof(QVector3D) + bitangent_offset; // object const GLsizei material_offset = 0; -const GLsizei object_id_offset = sizeof(GLuint ) + material_offset ; -const GLsizei color_offset = sizeof(GLuint ) + object_id_offset ; +const GLsizei object_id_offset = sizeof(GLuint) + material_offset; +const GLsizei color_offset = sizeof(GLuint) + object_id_offset; const GLsizei modelmatrix_offset = sizeof(QVector4D) + color_offset; const GLsizei is_selected_offset = 0; -const GLuint pos_loc = 1 ; // qgl_Vertex -const GLuint normal_loc = 2 ; // qgl_Normal -const GLuint tangent_loc = 3 ; // qgl_Tangent -const GLuint bitangent_loc = 4 ; // qgl_Bitangent -const GLuint tex_loc = 5 ; // qgl_Texture +const GLuint pos_loc = 1; // qgl_Vertex +const GLuint normal_loc = 2; // qgl_Normal +const GLuint tangent_loc = 3; // qgl_Tangent +const GLuint bitangent_loc = 4; // qgl_Bitangent +const GLuint tex_loc = 5; // qgl_Texture -const GLuint material_loc = 6 ; // qgl_Material -const GLuint object_id_loc = 8 ; // qgl_ObjectID -const GLuint color_loc = 9 ; // qgl_ObjectColor -const GLuint modelmatrix_loc = 10; // qgl_ModelViewProjectionMatrix +const GLuint material_loc = 6; // qgl_Material +const GLuint object_id_loc = 8; // qgl_ObjectID +const GLuint color_loc = 9; // qgl_ObjectColor +const GLuint modelmatrix_loc = 10; // qgl_ModelViewProjectionMatrix -const GLuint is_selected_loc = 7 ; // qgl_ObjectSelected +const GLuint is_selected_loc = 7; // qgl_ObjectSelected #pragma pack(push, 1) struct Vertex { @@ -65,13 +65,13 @@ struct Vertex { struct Object { Object() { material = object_id = 0; - color = QVector4D(1,1,1,1); + color = QVector4D(1, 1, 1, 1); QMatrix4x4().copyDataTo(modelmatrix); } - GLuint material; - GLuint object_id; + GLuint material; + GLuint object_id; QVector4D color; - GLfloat modelmatrix[16]; + GLfloat modelmatrix[16]; }; #pragma pack(pop) @@ -87,7 +87,7 @@ enum BindingPoints { enum MapType { mtDiffuse = 0, mtNormal = 1, - mtMetalness = 2, + mtMetalness = 2, mtRoughness = 3, mtEmission = 4, mtRelief = 5, @@ -124,7 +124,7 @@ struct QGLMaterial { struct QGLLightParameter { QVector4D color; QVector4D decay_intensity; // [^0, ^1, ^2, intensity] - QVector4D angles; // [start, cos(start), end, cos(end)] + QVector4D angles; // [start, cos(start), end, cos(end)] }; struct QGLLightPosition { QVector4D position; @@ -133,10 +133,10 @@ struct QGLLightPosition { #pragma pack(pop) void prepareDrawGeom(QOpenGLExtraFunctions * f); -void prepareDrawObj (QOpenGLExtraFunctions * f); -void prepareDrawSel (QOpenGLExtraFunctions * f); +void prepareDrawObj(QOpenGLExtraFunctions * f); +void prepareDrawSel(QOpenGLExtraFunctions * f); -} +} // namespace QGLEngineShaders #endif // GLSHADERS_TYPES_H diff --git a/core/gltexturearray.cpp b/core/gltexturearray.cpp index 0a6b064..eafdc9b 100644 --- a/core/gltexturearray.cpp +++ b/core/gltexturearray.cpp @@ -1,36 +1,36 @@ /* - QGL Texture2DArray - Ivan Pelipenko peri4ko@yandex.ru + QGL Texture2DArray + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #define GL_GLEXT_PROTOTYPES -#include #include "gltexturearray.h" +#include + Texture2DArray::Texture2DArray(bool filter) { - target_ = GL_TEXTURE_2D_ARRAY; - texture_ = 0; - layers_ = 0; + target_ = GL_TEXTURE_2D_ARRAY; + texture_ = 0; + layers_ = 0; filtering_ = filter; } -Texture2DArray::~Texture2DArray() { -} +Texture2DArray::~Texture2DArray() {} void Texture2DArray::init(QOpenGLExtraFunctions * f) { @@ -50,7 +50,7 @@ void Texture2DArray::destroy(QOpenGLExtraFunctions * f) { void Texture2DArray::reinit() { texture_ = 0; - layers_ = 0; + layers_ = 0; } @@ -68,7 +68,7 @@ void Texture2DArray::release(QOpenGLExtraFunctions * f) { bool Texture2DArray::resize(QOpenGLExtraFunctions * f, QSize new_size, int layers_count) { if (new_size.isNull() || layers_count <= 0) return false; if ((size_ == new_size) && (layers_ >= layers_count)) return false; - size_ = new_size; + size_ = new_size; layers_ = layers_count; f->glBindTexture(target_, texture_); f->glTexParameteri(target_, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -88,10 +88,9 @@ bool Texture2DArray::resize(QOpenGLExtraFunctions * f, QSize new_size, int layer void Texture2DArray::load(QOpenGLExtraFunctions * f, const QImage & image, int layer) { if (image.isNull() || size_.isNull() || layer < 0 || layer >= layers_) return; - QImage im = image.mirrored(false, true) - .scaled(size_, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) - .convertToFormat(QImage::Format_RGBA8888); - //qDebug() << "Texture2DArray::load image" << image.size() << "to layer" << layer; + QImage im = + image.mirrored(false, true).scaled(size_, Qt::IgnoreAspectRatio, Qt::SmoothTransformation).convertToFormat(QImage::Format_RGBA8888); + // qDebug() << "Texture2DArray::load image" << image.size() << "to layer" << layer; f->glTexSubImage3D(target_, 0, 0, 0, layer, size_.width(), size_.height(), 1, GL_RGBA, GL_UNSIGNED_BYTE, im.constBits()); } diff --git a/core/gltexturearray.h b/core/gltexturearray.h index 61ea7b5..7609f13 100644 --- a/core/gltexturearray.h +++ b/core/gltexturearray.h @@ -1,19 +1,19 @@ /* - QGL Texture2DArray - Ivan Pelipenko peri4ko@yandex.ru + QGL Texture2DArray + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLTEXTUREARRAY_H @@ -22,26 +22,25 @@ #include "gltypes.h" -class Texture2DArray -{ +class Texture2DArray { public: Texture2DArray(bool filter); ~Texture2DArray(); - void init (QOpenGLExtraFunctions * f); - void destroy (QOpenGLExtraFunctions * f); + void init(QOpenGLExtraFunctions * f); + void destroy(QOpenGLExtraFunctions * f); void reinit(); - void bind (QOpenGLExtraFunctions * f, int channel = 0); - void release (QOpenGLExtraFunctions * f); + void bind(QOpenGLExtraFunctions * f, int channel = 0); + void release(QOpenGLExtraFunctions * f); // returns true if size changed - bool resize (QOpenGLExtraFunctions * f, QSize new_size, int layers_count); - void load (QOpenGLExtraFunctions * f, const QImage & image, int layer); - void mipmaps (QOpenGLExtraFunctions * f); + bool resize(QOpenGLExtraFunctions * f, QSize new_size, int layers_count); + void load(QOpenGLExtraFunctions * f, const QImage & image, int layer); + void mipmaps(QOpenGLExtraFunctions * f); - GLuint ID() const {return texture_;} - bool isInit() const {return texture_ != 0;} + GLuint ID() const { return texture_; } + bool isInit() const { return texture_ != 0; } private: GLenum target_; diff --git a/core/gltransform.cpp b/core/gltransform.cpp index c72e60a..cf7c646 100644 --- a/core/gltransform.cpp +++ b/core/gltransform.cpp @@ -1,36 +1,50 @@ /* - QGL Transform - Ivan Pelipenko peri4ko@yandex.ru + QGL Transform + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "gltransform.h" + #include "gltypes.h" + #include -inline void composeQMatrix4x4(const QVector3D & position, const QVector3D & orientation, const QVector3D & scale, QMatrix4x4 &m) { +inline void composeQMatrix4x4(const QVector3D & position, const QVector3D & orientation, const QVector3D & scale, QMatrix4x4 & m) { const QMatrix3x3 rot3x3(Transform::toRotationMatrix(orientation)); // set up final matrix with scale, rotation and translation - m(0, 0) = scale.x() * rot3x3(0, 0); m(0, 1) = scale.y() * rot3x3(0, 1); m(0, 2) = scale.z() * rot3x3(0, 2); m(0, 3) = position.x(); - m(1, 0) = scale.x() * rot3x3(1, 0); m(1, 1) = scale.y() * rot3x3(1, 1); m(1, 2) = scale.z() * rot3x3(1, 2); m(1, 3) = position.y(); - m(2, 0) = scale.x() * rot3x3(2, 0); m(2, 1) = scale.y() * rot3x3(2, 1); m(2, 2) = scale.z() * rot3x3(2, 2); m(2, 3) = position.z(); + m(0, 0) = scale.x() * rot3x3(0, 0); + m(0, 1) = scale.y() * rot3x3(0, 1); + m(0, 2) = scale.z() * rot3x3(0, 2); + m(0, 3) = position.x(); + m(1, 0) = scale.x() * rot3x3(1, 0); + m(1, 1) = scale.y() * rot3x3(1, 1); + m(1, 2) = scale.z() * rot3x3(1, 2); + m(1, 3) = position.y(); + m(2, 0) = scale.x() * rot3x3(2, 0); + m(2, 1) = scale.y() * rot3x3(2, 1); + m(2, 2) = scale.z() * rot3x3(2, 2); + m(2, 3) = position.z(); // no projection term - m(3, 0) = 0.0f; m(3, 1) = 0.0f; m(3, 2) = 0.0f; m(3, 3) = 1.0f; + m(3, 0) = 0.0f; + m(3, 1) = 0.0f; + m(3, 2) = 0.0f; + m(3, 3) = 1.0f; } inline void decomposeQMatrix3x3(const QMatrix3x3 & m, QMatrix3x3 & Q, QVector3D & D, QVector3D & U) { @@ -62,24 +76,24 @@ inline void decomposeQMatrix3x3(const QMatrix3x3 & m, QMatrix3x3 & Q, QVector3D // build orthogonal matrix Q float invLen = 1.0f / std::sqrt(m(0, 0) * m(0, 0) + m(1, 0) * m(1, 0) + m(2, 0) * m(2, 0)); - Q(0, 0) = m(0, 0) * invLen; - Q(1, 0) = m(1, 0) * invLen; - Q(2, 0) = m(2, 0) * invLen; + Q(0, 0) = m(0, 0) * invLen; + Q(1, 0) = m(1, 0) * invLen; + Q(2, 0) = m(2, 0) * invLen; - float dot = Q(0, 0) * m(0, 1) + Q(1, 0) * m(1, 1) + Q(2, 0) * m(2, 1); - Q(0, 1) = m(0, 1) - dot * Q(0, 0); - Q(1, 1) = m(1, 1) - dot * Q(1, 0); - Q(2, 1) = m(2, 1) - dot * Q(2, 0); - invLen = 1.0f / std::sqrt(Q(0, 1) * Q(0, 1) + Q(1, 1) * Q(1, 1) + Q(2, 1) * Q(2, 1)); + float dot = Q(0, 0) * m(0, 1) + Q(1, 0) * m(1, 1) + Q(2, 0) * m(2, 1); + Q(0, 1) = m(0, 1) - dot * Q(0, 0); + Q(1, 1) = m(1, 1) - dot * Q(1, 0); + Q(2, 1) = m(2, 1) - dot * Q(2, 0); + invLen = 1.0f / std::sqrt(Q(0, 1) * Q(0, 1) + Q(1, 1) * Q(1, 1) + Q(2, 1) * Q(2, 1)); Q(0, 1) *= invLen; Q(1, 1) *= invLen; Q(2, 1) *= invLen; - dot = Q(0, 0) * m(0, 2) + Q(1, 0) * m(1, 2) + Q(2, 0) * m(2, 2); + dot = Q(0, 0) * m(0, 2) + Q(1, 0) * m(1, 2) + Q(2, 0) * m(2, 2); Q(0, 2) = m(0, 2) - dot * Q(0, 0); Q(1, 2) = m(1, 2) - dot * Q(1, 0); Q(2, 2) = m(2, 2) - dot * Q(2, 0); - dot = Q(0, 1) * m(0, 2) + Q(1, 1) * m(1, 2) + Q(2, 1) * m(2, 2); + dot = Q(0, 1) * m(0, 2) + Q(1, 1) * m(1, 2) + Q(2, 1) * m(2, 2); Q(0, 2) -= dot * Q(0, 1); Q(1, 2) -= dot * Q(1, 1); Q(2, 2) -= dot * Q(2, 1); @@ -89,11 +103,9 @@ inline void decomposeQMatrix3x3(const QMatrix3x3 & m, QMatrix3x3 & Q, QVector3D Q(2, 2) *= invLen; // guarantee that orthogonal matrix has determinant 1 (no reflections) - const float det = Q(0, 0) * Q(1, 1) * Q(2, 2) + Q(0, 1) * Q(1, 2) * Q(2, 0) + - Q(0, 2) * Q(1, 0) * Q(2, 1) - Q(0, 2) * Q(1, 1) * Q(2, 0) - - Q(0, 1) * Q(1, 0) * Q(2, 2) - Q(0, 0) * Q(1, 2) * Q(2, 1); - if (det < 0.0f) - Q *= -1.0f; + const float det = Q(0, 0) * Q(1, 1) * Q(2, 2) + Q(0, 1) * Q(1, 2) * Q(2, 0) + Q(0, 2) * Q(1, 0) * Q(2, 1) - + Q(0, 2) * Q(1, 1) * Q(2, 0) - Q(0, 1) * Q(1, 0) * Q(2, 2) - Q(0, 0) * Q(1, 2) * Q(2, 1); + if (det < 0.0f) Q *= -1.0f; // build "right" matrix R QMatrix3x3 R(Qt::Uninitialized); @@ -105,30 +117,26 @@ inline void decomposeQMatrix3x3(const QMatrix3x3 & m, QMatrix3x3 & Q, QVector3D R(2, 2) = Q(0, 2) * m(0, 2) + Q(1, 2) * m(1, 2) + Q(2, 2) * m(2, 2); // the scaling component - D[0] = R(0, 0); - D[1] = R(1, 1); - D[2] = R(2, 2); + D[0] = R(0, 0); + D[1] = R(1, 1); + D[2] = R(2, 2); // the shear component - U[0] = R(0, 1) / D[0]; - U[1] = R(0, 2) / D[0]; - U[2] = R(1, 2) / D[1]; + U[0] = R(0, 1) / D[0]; + U[1] = R(0, 2) / D[0]; + U[2] = R(1, 2) / D[1]; } -inline bool hasScale(const QMatrix4x4 &m) { +inline bool hasScale(const QMatrix4x4 & m) { // If the columns are orthonormal and form a right-handed system, then there is no scale float t(m.determinant()); - if (!qFuzzyIsNull(t - 1.0f)) - return true; + if (!qFuzzyIsNull(t - 1.0f)) return true; t = m(0, 0) * m(0, 0) + m(1, 0) * m(1, 0) + m(2, 0) * m(2, 0); - if (!qFuzzyIsNull(t - 1.0f)) - return true; + if (!qFuzzyIsNull(t - 1.0f)) return true; t = m(0, 1) * m(0, 1) + m(1, 1) * m(1, 1) + m(2, 1) * m(2, 1); - if (!qFuzzyIsNull(t - 1.0f)) - return true; + if (!qFuzzyIsNull(t - 1.0f)) return true; t = m(0, 2) * m(0, 2) + m(1, 2) * m(1, 2) + m(2, 2) * m(2, 2); - if (!qFuzzyIsNull(t - 1.0f)) - return true; + if (!qFuzzyIsNull(t - 1.0f)) return true; return false; } @@ -142,64 +150,57 @@ inline void decomposeQMatrix4x4(const QMatrix4x4 & m, QVector3D & position, QVec decomposeQMatrix3x3(m3x3, rot3x3, scale, position); } else { // we know there is no scaling part; no need for QDU decomposition - scale = QVector3D(1.0f, 1.0f, 1.0f); + scale = QVector3D(1.0f, 1.0f, 1.0f); rot3x3 = m3x3; } orientation = Transform::fromRotationMatrix(rot3x3); - position = QVector3D(m(0, 3), m(1, 3), m(2, 3)); + position = QVector3D(m(0, 3), m(1, 3), m(2, 3)); } +Transform::Transform(): m_scale(1.0f, 1.0f, 1.0f), m_translation(), m_eulerRotationAngles(), m_matrixDirty(false) {} -Transform::Transform(): m_scale(1.0f, 1.0f, 1.0f), - m_translation(), m_eulerRotationAngles(), m_matrixDirty(false) { -} - - -Transform & Transform::operator =(const Transform & t) { - m_scale = t.m_scale; - m_translation = t.m_translation; +Transform & Transform::operator=(const Transform & t) { + m_scale = t.m_scale; + m_translation = t.m_translation; m_eulerRotationAngles = t.m_eulerRotationAngles; - m_matrixDirty = true; + m_matrixDirty = true; return *this; } void Transform::setMatrix(const QMatrix4x4 & m) { if (m != matrix()) { - m_matrix = m; + m_matrix = m; m_matrixDirty = false; QVector3D s; QVector3D t; QVector3D r; decomposeQMatrix4x4(m, t, r, s); - m_scale = s; - m_translation = t; + m_scale = s; + m_translation = t; m_eulerRotationAngles = r; } } void Transform::setRotationX(float r) { - if (m_eulerRotationAngles.x() == r) - return; + if (m_eulerRotationAngles.x() == r) return; m_eulerRotationAngles.setX(r); m_matrixDirty = true; } void Transform::setRotationY(float r) { - if (m_eulerRotationAngles.y() == r) - return; + if (m_eulerRotationAngles.y() == r) return; m_eulerRotationAngles.setY(r); m_matrixDirty = true; } void Transform::setRotationZ(float r) { - if (m_eulerRotationAngles.z() == r) - return; + if (m_eulerRotationAngles.z() == r) return; m_eulerRotationAngles.setZ(r); m_matrixDirty = true; } @@ -230,7 +231,7 @@ QMatrix4x4 Transform::matrixRotateScale() const { QVector3D Transform::direction() const { - return matrixRotate().mapVector(QVector3D(0,0,-1)).normalized(); + return matrixRotate().mapVector(QVector3D(0, 0, -1)).normalized(); } @@ -238,7 +239,7 @@ void Transform::buildMatrix() const { if (m_matrixDirty) { composeQMatrix4x4(m_translation, m_eulerRotationAngles, m_scale, m_matrix); composeQMatrix4x4(QVector3D(), m_eulerRotationAngles, m_scale, m_matrixWT); - composeQMatrix4x4(QVector3D(), m_eulerRotationAngles, QVector3D(1,1,1), m_matrixR); + composeQMatrix4x4(QVector3D(), m_eulerRotationAngles, QVector3D(1, 1, 1), m_matrixR); composeQMatrix4x4(QVector3D(), QVector3D(), m_scale, m_matrixS); m_matrixDirty = false; } @@ -262,7 +263,7 @@ float Transform::rotationZ() const { void Transform::setScale(const QVector3D & s) { if (s != m_scale) { - m_scale = s; + m_scale = s; m_matrixDirty = true; } } @@ -305,7 +306,7 @@ float Transform::scale() const { void Transform::setRotation(const QVector3D & r) { if (r != m_eulerRotationAngles) { m_eulerRotationAngles = r; - m_matrixDirty = true; + m_matrixDirty = true; } } @@ -362,17 +363,19 @@ QQuaternion Transform::fromAxisAndAngle(float x, float y, float z, float angle) } -QQuaternion Transform::fromAxesAndAngles(const QVector3D & axis1, float angle1, - const QVector3D & axis2, float angle2) { +QQuaternion Transform::fromAxesAndAngles(const QVector3D & axis1, float angle1, const QVector3D & axis2, float angle2) { const QQuaternion q1 = QQuaternion::fromAxisAndAngle(axis1, angle1); const QQuaternion q2 = QQuaternion::fromAxisAndAngle(axis2, angle2); return q2 * q1; } -QQuaternion Transform::fromAxesAndAngles(const QVector3D & axis1, float angle1, - const QVector3D & axis2, float angle2, - const QVector3D & axis3, float angle3) { +QQuaternion Transform::fromAxesAndAngles(const QVector3D & axis1, + float angle1, + const QVector3D & axis2, + float angle2, + const QVector3D & axis3, + float angle3) { const QQuaternion q1 = QQuaternion::fromAxisAndAngle(axis1, angle1); const QQuaternion q2 = QQuaternion::fromAxisAndAngle(axis2, angle2); const QQuaternion q3 = QQuaternion::fromAxisAndAngle(axis3, angle3); @@ -398,16 +401,16 @@ QVector3D Transform::fromDirection(QVector3D d, float pitch) { QVector3D Transform::fromRotationMatrix(const QMatrix3x3 & m) { - float sy = sqrt(m(0,0) * m(0,0) + m(1,0) * m(1,0)); + float sy = sqrt(m(0, 0) * m(0, 0) + m(1, 0) * m(1, 0)); bool singular = sy < 1.E-6; // If float x, y, z; if (!singular) { - x = atan2( m(2,1), m(2,2)); - y = atan2(-m(2,0), sy); - z = atan2( m(1,0), m(0,0)); + x = atan2(m(2, 1), m(2, 2)); + y = atan2(-m(2, 0), sy); + z = atan2(m(1, 0), m(0, 0)); } else { - x = atan2(-m(1,2), m(1,1)); - y = atan2(-m(2,0), sy); + x = atan2(-m(1, 2), m(1, 1)); + y = atan2(-m(2, 0), sy); z = 0.; } return QVector3D(x, y, z) * rad2deg; @@ -419,7 +422,7 @@ QMatrix3x3 Transform::toRotationMatrix(const QVector3D & r) { if (r.z() != 0.f) m.rotate(r.z(), 0., 0., 1.); if (r.y() != 0.f) m.rotate(r.y(), 0., 1., 0.); if (r.x() != 0.f) m.rotate(r.x(), 1., 0., 0.); - return m.toGenericMatrix<3,3>(); + return m.toGenericMatrix<3, 3>(); } @@ -433,8 +436,20 @@ QMatrix4x4 Transform::rotateAround(const QVector3D & point, float angle, const Q QMatrix4x4 Transform::rotateFromAxes(const QVector3D & xAxis, const QVector3D & yAxis, const QVector3D & zAxis) { - return QMatrix4x4(xAxis.x(), yAxis.x(), zAxis.x(), 0.0f, - xAxis.y(), yAxis.y(), zAxis.y(), 0.0f, - xAxis.z(), yAxis.z(), zAxis.z(), 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f); + return QMatrix4x4(xAxis.x(), + yAxis.x(), + zAxis.x(), + 0.0f, + xAxis.y(), + yAxis.y(), + zAxis.y(), + 0.0f, + xAxis.z(), + yAxis.z(), + zAxis.z(), + 0.0f, + 0.0f, + 0.0f, + 0.0f, + 1.0f); } diff --git a/core/gltransform.h b/core/gltransform.h index 2eb1129..a6ab882 100644 --- a/core/gltransform.h +++ b/core/gltransform.h @@ -1,19 +1,19 @@ /* - QGL Transform - Ivan Pelipenko peri4ko@yandex.ru + QGL Transform + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLTRANSFORM_H @@ -26,10 +26,11 @@ class Transform { - friend QDataStream & operator >>(QDataStream & s, Transform & v); + friend QDataStream & operator>>(QDataStream & s, Transform & v); + public: Transform(); - Transform & operator =(const Transform & t); + Transform & operator=(const Transform & t); float scale() const; QVector3D scale3D() const; @@ -45,7 +46,7 @@ public: float rotationY() const; float rotationZ() const; - void setScale(float s) {setScale(QVector3D(s, s, s));} + void setScale(float s) { setScale(QVector3D(s, s, s)); } void setScale(const QVector3D & s); void setScaleX(float s); void setScaleY(float s); @@ -62,17 +63,15 @@ public: void setTranslationZ(float t); void setMatrix(const QMatrix4x4 & matrix); - void setDirty(bool yes = true) {m_matrixDirty = yes;} + void setDirty(bool yes = true) { m_matrixDirty = yes; } static QQuaternion fromAxisAndAngle(const QVector3D & axis, float angle); static QQuaternion fromAxisAndAngle(float x, float y, float z, float angle); - static QQuaternion fromAxesAndAngles(const QVector3D & axis1, float angle1, - const QVector3D & axis2, float angle2); - static QQuaternion fromAxesAndAngles(const QVector3D & axis1, float angle1, - const QVector3D & axis2, float angle2, - const QVector3D & axis3, float angle3); + static QQuaternion fromAxesAndAngles(const QVector3D & axis1, float angle1, const QVector3D & axis2, float angle2); + static QQuaternion + fromAxesAndAngles(const QVector3D & axis1, float angle1, const QVector3D & axis2, float angle2, const QVector3D & axis3, float angle3); static QQuaternion fromAxes(const QVector3D & xAxis, const QVector3D & yAxis, const QVector3D & zAxis); static QVector3D fromDirection(QVector3D d, float pitch = 0.f); @@ -91,14 +90,14 @@ protected: mutable QMatrix4x4 m_matrix, m_matrixWT, m_matrixR, m_matrixS; mutable bool m_matrixDirty; - }; -inline QDataStream & operator <<(QDataStream & s, const Transform & v) { - s << v.matrix(); return s; +inline QDataStream & operator<<(QDataStream & s, const Transform & v) { + s << v.matrix(); + return s; } -inline QDataStream & operator >>(QDataStream & s, Transform & v) { +inline QDataStream & operator>>(QDataStream & s, Transform & v) { QMatrix4x4 m; s >> m; v.setMatrix(m); diff --git a/core/gltypes.cpp b/core/gltypes.cpp index fe4134c..66e8ab7 100644 --- a/core/gltypes.cpp +++ b/core/gltypes.cpp @@ -1,24 +1,24 @@ /* - QGLView Types - Ivan Pelipenko peri4ko@yandex.ru + QGLView Types + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "glcamera.h" -#include "qglview.h" #include "gltexture_manager.h" +#include "qglview.h" #include @@ -37,15 +37,14 @@ QString readCharsUntilNull(QDataStream & s) { QString findFile(const QString & file, const QStringList & pathes) { QFileInfo fi(QString(file).replace("\\", "/")); - //qDebug() << "search" << file << "in" << pathes; + // qDebug() << "search" << file << "in" << pathes; if (fi.exists()) return fi.absoluteFilePath(); QString fn = fi.fileName(); if (fn.contains("/")) fn = fn.mid(fn.lastIndexOf("/")); - foreach (QString p, pathes) { + foreach(QString p, pathes) { QFileInfoList fil = QDir(p).entryInfoList(QStringList(fn), QDir::Files | QDir::NoDotAndDotDot); - //qDebug() << "findFile" << fn << "in" << p << "->" << fil.size(); - if (!fil.isEmpty()) - return fil[0].absoluteFilePath(); + // qDebug() << "findFile" << fn << "in" << p << "->" << fil.size(); + if (!fil.isEmpty()) return fil[0].absoluteFilePath(); } return QString(); } @@ -55,21 +54,27 @@ void glDrawQuad(QOpenGLShaderProgram * prog, QVector4D * corner_dirs, GLfloat x, glSetPolygonMode(GL_FILL); glDisable(GL_LIGHTING); glEnable(GL_TEXTURE_2D); - int loc = prog ? prog->attributeLocation("qgl_Color") : -1, - locv = prog ? prog->attributeLocation("qgl_Vertex") : -1, - loct = prog ? prog->attributeLocation("qgl_Texture") : -1, - locc = prog ? prog->attributeLocation("view_corner") : -1; + int loc = prog ? prog->attributeLocation("qgl_Color") : -1, locv = prog ? prog->attributeLocation("qgl_Vertex") : -1, + loct = prog ? prog->attributeLocation("qgl_Texture") : -1, locc = prog ? prog->attributeLocation("view_corner") : -1; QOpenGLFunctions * glFuncs = QOpenGLContext::currentContext()->functions(); if (prog) { - static const GLfloat cols [] = {1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f}; - static const GLfloat verts[] = {x, y, x+w, y, x, y+h, x+w, y+h}; - static const GLfloat texs [] = {0.f, 0.f, 1.f, 0.f, 0.f, 1.f, 1.f, 1.f}; - GLfloat vcs[] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; + static const GLfloat cols[] = {1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f}; + static const GLfloat verts[] = {x, y, x + w, y, x, y + h, x + w, y + h}; + static const GLfloat texs[] = {0.f, 0.f, 1.f, 0.f, 0.f, 1.f, 1.f, 1.f}; + GLfloat vcs[] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; if (corner_dirs) { - vcs[0] = corner_dirs[0].x(); vcs[1] = corner_dirs[0].y(); vcs[2] = corner_dirs[0].z(); - vcs[3] = corner_dirs[1].x(); vcs[4] = corner_dirs[1].y(); vcs[5] = corner_dirs[1].z(); - vcs[6] = corner_dirs[2].x(); vcs[7] = corner_dirs[2].y(); vcs[8] = corner_dirs[2].z(); - vcs[9] = corner_dirs[3].x(); vcs[10] = corner_dirs[3].y(); vcs[11] = corner_dirs[3].z(); + vcs[0] = corner_dirs[0].x(); + vcs[1] = corner_dirs[0].y(); + vcs[2] = corner_dirs[0].z(); + vcs[3] = corner_dirs[1].x(); + vcs[4] = corner_dirs[1].y(); + vcs[5] = corner_dirs[1].z(); + vcs[6] = corner_dirs[2].x(); + vcs[7] = corner_dirs[2].y(); + vcs[8] = corner_dirs[2].z(); + vcs[9] = corner_dirs[3].x(); + vcs[10] = corner_dirs[3].y(); + vcs[11] = corner_dirs[3].z(); } glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); @@ -92,10 +97,14 @@ void glDrawQuad(QOpenGLShaderProgram * prog, QVector4D * corner_dirs, GLfloat x, } else { glBegin(GL_TRIANGLE_STRIP); glColor4f(1.f, 1.f, 1.f, 1.f); - glTexCoord2f(0.f, 0.f); glVertex2f(x, y); - glTexCoord2f(1.f, 0.f); glVertex2f(x+w, y); - glTexCoord2f(0.f, 1.f); glVertex2f(x, y+h); - glTexCoord2f(1.f, 1.f); glVertex2f(x+w, y+h); + glTexCoord2f(0.f, 0.f); + glVertex2f(x, y); + glTexCoord2f(1.f, 0.f); + glVertex2f(x + w, y); + glTexCoord2f(0.f, 1.f); + glVertex2f(x, y + h); + glTexCoord2f(1.f, 1.f); + glVertex2f(x + w, y + h); glEnd(); } } @@ -140,15 +149,13 @@ void createGLTexture(QOpenGLExtraFunctions * f, GLuint & tex, int width, int hei f->glTexImage2D(target, 0, format, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, nullptr); else { int type = GL_UNSIGNED_BYTE; - int fmt = GL_RGBA; - if (format == GL_RGB32F || format == GL_RGB16F || format == GL_RGBA32F || format == GL_RGBA16F) - type = GL_FLOAT; - if (format == GL_RGB32F || format == GL_RGB16F || format == GL_RGB8 || format == GL_RGB) - fmt = GL_RGB; + int fmt = GL_RGBA; + if (format == GL_RGB32F || format == GL_RGB16F || format == GL_RGBA32F || format == GL_RGBA16F) type = GL_FLOAT; + if (format == GL_RGB32F || format == GL_RGB16F || format == GL_RGB8 || format == GL_RGB) fmt = GL_RGB; f->glTexImage2D(target, 0, format, width, height, 0, fmt, type, nullptr); - //qDebug() << "glTexImage2D" << width << height << QString::number(t, 16); + // qDebug() << "glTexImage2D" << width << height << QString::number(t, 16); } - //qDebug() << QString::number(glGetError(), 16); + // qDebug() << QString::number(glGetError(), 16); } @@ -174,7 +181,7 @@ void createGLTexture(QOpenGLExtraFunctions * f, GLuint & tex, const QImage & ima if (target == GL_TEXTURE_1D || target == GL_TEXTURE_2D || target == GL_TEXTURE_3D) { f->glGenerateMipmap(target); } - //qDebug() << target << format << tex << im.width() << im.height() << im.bits() << QString::number(glGetError(), 16); + // qDebug() << target << format << tex << im.width() << im.height() << im.bits() << QString::number(glGetError(), 16); } @@ -216,14 +223,15 @@ QImage rotateQImageRight(const QImage & im) { } - - QColor colorFromString(const QString & str) { QString s = str.trimmed(); - int i = s.indexOf("\t"); + int i = s.indexOf("\t"); float r, g, b; - r = s.left(i).toFloat(); s = s.right(s.length() - i - 1); i = s.indexOf("\t"); - g = s.left(i).toFloat(); s = s.right(s.length() - i - 1); + r = s.left(i).toFloat(); + s = s.right(s.length() - i - 1); + i = s.indexOf("\t"); + g = s.left(i).toFloat(); + s = s.right(s.length() - i - 1); b = s.toFloat(); return QColor(r * 255.f, g * 255.f, b * 255.f); } @@ -232,9 +240,12 @@ QColor colorFromString(const QString & str) { QVector3D orthToVector(const QVector3D & v, const float & scale) { if (v.isNull()) return QVector3D(); QVector3D rv, fn, sn; - if (v.x() != 0.f) rv.setZ(1.); - else if (v.y() != 0.f) rv.setX(1.); - else rv.setY(1.); + if (v.x() != 0.f) + rv.setZ(1.); + else if (v.y() != 0.f) + rv.setX(1.); + else + rv.setY(1.); fn = QVector3D::crossProduct(v, rv).normalized(); sn = QVector3D::crossProduct(v, fn).normalized(); return fn * urand(scale) + sn * urand(scale); @@ -268,18 +279,22 @@ void lengthenVector(QVector3D & v, const float & l) { Vector2i::Vector2i(const QString & str) { QString s = str.trimmed(); - int i = s.indexOf("\t"); - p0 = s.left(i).toInt(); s = s.right(s.length() - i - 1); - p1 = s.toInt(); + int i = s.indexOf("\t"); + p0 = s.left(i).toInt(); + s = s.right(s.length() - i - 1); + p1 = s.toInt(); } Vector3i::Vector3i(const QString & str) { QString s = str.trimmed(); - int i = s.indexOf("\t"); - p0 = s.left(i).toInt(); s = s.right(s.length() - i - 1); i = s.indexOf("\t"); - p1 = s.left(i).toInt(); s = s.right(s.length() - i - 1); - p2 = s.toInt(); + int i = s.indexOf("\t"); + p0 = s.left(i).toInt(); + s = s.right(s.length() - i - 1); + i = s.indexOf("\t"); + p1 = s.left(i).toInt(); + s = s.right(s.length() - i - 1); + p2 = s.toInt(); } @@ -305,8 +320,6 @@ void glClearFramebuffer(const QColor & color, bool depth) { } - - Box3D::Box3D(const QVector & points) { x = y = z = width = length = height = angle_z = angle_xy = angle_roll = 0.f; if (points.isEmpty()) return; @@ -315,13 +328,16 @@ Box3D::Box3D(const QVector & points) { 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()); + 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; + x = ix; + y = iy; + z = iz; length = ax - ix; width = ay - iy; height = az - iz; @@ -331,32 +347,42 @@ Box3D::Box3D(const QVector & points) { 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); + << 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) { +Box3D & Box3D::operator|=(const Box3D & o) { if (o.isEmpty()) return *this; - if (isEmpty()) *this = 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; + 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; } QVector3D vectorFromString(const QString & str) { - QTextStream s(const_cast(&str), QIODevice::ReadOnly); + QTextStream s(const_cast(&str), QIODevice::ReadOnly); QVector3D ret; float f(0.f); - s >> f; ret.setX(f); - s >> f; ret.setY(f); - s >> f; ret.setZ(f); + s >> f; + ret.setX(f); + s >> f; + ret.setY(f); + s >> f; + ret.setZ(f); return ret; } diff --git a/core/gltypes.h b/core/gltypes.h index 6ac9b00..51d83f7 100644 --- a/core/gltypes.h +++ b/core/gltypes.h @@ -1,19 +1,19 @@ /* - QGLView Types - Ivan Pelipenko peri4ko@yandex.ru + QGLView Types + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLTYPES_H @@ -30,9 +30,9 @@ #endif #ifndef WINDOWS # ifndef QNX -# ifndef MAC -# define LINUX -# endif +# ifndef MAC +# define LINUX +# endif # endif #endif #if __GNUC__ @@ -43,49 +43,50 @@ #include -//#ifndef WINDOWS -//# ifdef MAC -//# include -//# include -//# include -//# else -//# include -//# include -//# include -//# endif -//#endif +// #ifndef WINDOWS +// # ifdef MAC +// # include +// # include +// # include +// # else +// # include +// # include +// # include +// # endif +// #endif +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include -#include -#include -#include -#include -#include -#include -#include #include #include -#include -#include -#include -#include +#include +#include +#include #ifndef QNX -# include -# include +# include +# include #else -# include -# include +# include +# include #endif -#include -#include "qglengine_version.h" #include "qglengine_core_export.h" +#include "qglengine_version.h" + +#include -//#ifdef WINDOWS -//# define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF -//# define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE -//#endif +// #ifdef WINDOWS +// # define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF +// # define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE +// #endif #ifndef M_PI # define M_PI 3.14159265358979323846 @@ -116,53 +117,131 @@ const float deg2rad = atanf(1.f) / 45.f; const float rad2deg = 45.f / atanf(1.f); # ifdef WINDOWS -inline int random() {return rand();} +inline int random() { + return rand(); +} # endif #else -#define random randomi +# define random randomi #endif #ifdef CC_VC -inline float round(const float & v) {return floor(v + 0.5);} +inline float round(const float & v) { + return floor(v + 0.5); +} #endif -inline float randomu() {return float(random()) / RAND_MAX;} +inline float randomu() { + return float(random()) / RAND_MAX; +} -inline const QSizeF operator *(const QSizeF & f, const QSizeF & s) {return QSizeF(f.width() * s.width(), f.height() * s.height());} +inline const QSizeF operator*(const QSizeF & f, const QSizeF & s) { + return QSizeF(f.width() * s.width(), f.height() * s.height()); +} #ifndef PIP_VERSION -template inline void piSwap(T & f, T & s) {T t(f); f = s; s = t;} -template inline Type piMin(const Type & f, const Type & s) {return (f > s) ? s : f;} -template inline Type piMin(const Type & f, const Type & s, const Type & t) {return (f < s && f < t) ? f : ((s < t) ? s : t);} -template inline Type piMax(const Type & f, const Type & s) {return (f < s) ? s : f;} -template inline Type piMax(const Type & f, const Type & s, const Type & t) {return (f > s && f > t) ? f : ((s > t) ? s : t);} -template inline Type piClamp(const Type & v, const Type & min, const Type & max) {return (v > max ? max : (v < min ? min : v));} -inline ushort letobe_s(ushort v) {return (v << 8) | (v >> 8);} -inline uint letobe_i(const uint & v) {return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | ((v << 24) & 0xFF000000);} +template +inline void piSwap(T & f, T & s) { + T t(f); + f = s; + s = t; +} +template +inline Type piMin(const Type & f, const Type & s) { + return (f > s) ? s : f; +} +template +inline Type piMin(const Type & f, const Type & s, const Type & t) { + return (f < s && f < t) ? f : ((s < t) ? s : t); +} +template +inline Type piMax(const Type & f, const Type & s) { + return (f < s) ? s : f; +} +template +inline Type piMax(const Type & f, const Type & s, const Type & t) { + return (f > s && f > t) ? f : ((s > t) ? s : t); +} +template +inline Type piClamp(const Type & v, const Type & min, const Type & max) { + return (v > max ? max : (v < min ? min : v)); +} +inline ushort letobe_s(ushort v) { + return (v << 8) | (v >> 8); +} +inline uint letobe_i(const uint & v) { + return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | ((v << 24) & 0xFF000000); +} #endif // return [-1, 1] -inline float urand(const float & scale = 1.) {return ((float)rand() / RAND_MAX - .5f) * (scale + scale);} +inline float urand(const float & scale = 1.) { + return ((float)rand() / RAND_MAX - .5f) * (scale + scale); +} // return [0, 1] -inline float uprand(const float & scale = 1.) {return ((float)rand() / RAND_MAX) * scale;} +inline float uprand(const float & scale = 1.) { + return ((float)rand() / RAND_MAX) * scale; +} QString readCharsUntilNull(QDataStream & s); QString findFile(const QString & file, const QStringList & pathes); -inline QColor operator *(const QColor & c, float v) {return QColor(piClamp(c.red() * v, 0, 255), piClamp(c.green() * v, 0, 255), piClamp(c.blue() * v, 0, 255), piClamp(c.alpha() * v, 0, 255));} -inline QColor operator /(const QColor & c, float v) {return QColor(piClamp(c.red() / v, 0, 255), piClamp(c.green() / v, 0, 255), piClamp(c.blue() / v, 0, 255), piClamp(c.alpha() / v, 0, 255));} +inline QColor operator*(const QColor & c, float v) { + return QColor(piClamp(c.red() * v, 0, 255), + piClamp(c.green() * v, 0, 255), + piClamp(c.blue() * v, 0, 255), + piClamp(c.alpha() * v, 0, 255)); +} +inline QColor operator/(const QColor & c, float v) { + return QColor(piClamp(c.red() / v, 0, 255), + piClamp(c.green() / v, 0, 255), + piClamp(c.blue() / v, 0, 255), + piClamp(c.alpha() / v, 0, 255)); +} -inline void qglColor(const QColor & c) {glColor4f(c.redF(), c.greenF(), c.blueF(), c.alphaF());} -inline void glClearError() {int c = 100; while (glGetError() != GL_NO_ERROR && --c > 0) glGetError();} -inline void glSetCapEnabled(GLenum cap, bool on = true) {if (on) glEnable(cap); else glDisable(cap);} -inline void glSetPolygonMode(GLenum mode) {glPolygonMode(GL_FRONT_AND_BACK, mode);} -inline void deleteGLTexture(QOpenGLExtraFunctions * f, GLuint & tex) {if (tex != 0) f->glDeleteTextures(1, &tex); tex = 0;} +inline void qglColor(const QColor & c) { + glColor4f(c.redF(), c.greenF(), c.blueF(), c.alphaF()); +} +inline void glClearError() { + int c = 100; + while (glGetError() != GL_NO_ERROR && --c > 0) + glGetError(); +} +inline void glSetCapEnabled(GLenum cap, bool on = true) { + if (on) + glEnable(cap); + else + glDisable(cap); +} +inline void glSetPolygonMode(GLenum mode) { + glPolygonMode(GL_FRONT_AND_BACK, mode); +} +inline void deleteGLTexture(QOpenGLExtraFunctions * f, GLuint & tex) { + if (tex != 0) f->glDeleteTextures(1, &tex); + tex = 0; +} void glEnableDepth(); void glDisableDepth(); void glClearFramebuffer(const QColor & color = Qt::black, bool depth = true); -void glDrawQuad(QOpenGLShaderProgram * prog = nullptr, QVector4D * corner_dirs = nullptr, GLfloat x = -1.f, GLfloat y = -1.f, GLfloat w = 2.f, GLfloat h = 2.f); -void createGLTexture(QOpenGLExtraFunctions * f, GLuint & tex, int width, int height, const GLenum & format = GL_RGBA, const GLenum & target = GL_TEXTURE_2D); -void createGLTexture(QOpenGLExtraFunctions * f, GLuint & tex, const QImage & image, const GLenum & format = GL_RGBA, const GLenum & target = GL_TEXTURE_2D); +void glDrawQuad(QOpenGLShaderProgram * prog = nullptr, + QVector4D * corner_dirs = nullptr, + GLfloat x = -1.f, + GLfloat y = -1.f, + GLfloat w = 2.f, + GLfloat h = 2.f); +void createGLTexture(QOpenGLExtraFunctions * f, + GLuint & tex, + int width, + int height, + const GLenum & format = GL_RGBA, + const GLenum & target = GL_TEXTURE_2D); +void createGLTexture(QOpenGLExtraFunctions * f, + GLuint & tex, + const QImage & image, + const GLenum & format = GL_RGBA, + const GLenum & target = GL_TEXTURE_2D); QMatrix4x4 glMatrixPerspective(float angle, float aspect, float near_); QImage rotateQImageLeft(const QImage & im); QImage rotateQImageRight(const QImage & im); -inline QImage rotateQImage180(const QImage & im) {return im.mirrored(true, true);} +inline QImage rotateQImage180(const QImage & im) { + return im.mirrored(true, true); +} class QGLView; class MouseController; @@ -192,7 +271,7 @@ enum RenderPass { rpTransparent, }; -typedef QList ObjectBaseList; +typedef QList ObjectBaseList; struct Box3D { GLfloat x; @@ -204,101 +283,215 @@ struct Box3D { GLfloat angle_z; GLfloat angle_xy; 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() { 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-6f) && (qAbs(length) < 1E-6f) && (qAbs(height) < 1E-6f);} - 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.f + x, width / 2.f + y, height / 2.f + z);} - QVector3D angles() const {return QVector3D(angle_xy, angle_roll, angle_z);} + bool isEmpty() const { return (qAbs(width) < 1E-6f) && (qAbs(length) < 1E-6f) && (qAbs(height) < 1E-6f); } + 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.f + x, width / 2.f + y, height / 2.f + 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();} - 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); + 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(); + } + 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 {start (" << 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; +} #pragma pack(push, 1) struct Vector2i { - Vector2i(int p0_ = 0, int p1_ = 0) {p0 = p0_; p1 = p1_;} + Vector2i(int p0_ = 0, int p1_ = 0) { + p0 = p0_; + p1 = p1_; + } Vector2i(const QString & str); - Vector2i movedX(const int & o) {return Vector2i(p0 + o, p1);} - Vector2i movedY(const int & o) {return Vector2i(p0, p1 + o);} - Vector2i moved(const int & x, const int & y) {return Vector2i(p0 + x, p1 + y);} + Vector2i movedX(const int & o) { return Vector2i(p0 + o, p1); } + Vector2i movedY(const int & o) { return Vector2i(p0, p1 + o); } + Vector2i moved(const int & x, const int & y) { return Vector2i(p0 + x, p1 + y); } GLint p0; GLint p1; - bool operator ==(const Vector2i & o) const {return p0 == o.p0 && p1 == o.p1;} - bool operator !=(const Vector2i & o) const {return p0 != o.p0 || p1 != o.p1;} - void operator +=(int v) {p0 += v; p1 += v;} - QVector2D toQVector2D() const {return QVector2D(p0, p1);} + bool operator==(const Vector2i & o) const { return p0 == o.p0 && p1 == o.p1; } + bool operator!=(const Vector2i & o) const { return p0 != o.p0 || p1 != o.p1; } + void operator+=(int v) { + p0 += v; + p1 += v; + } + QVector2D toQVector2D() const { return QVector2D(p0, p1); } }; #pragma pack(pop) -inline Vector2i operator +(const Vector2i & f, const Vector2i & s) {return Vector2i(f.p0 + s.p0, f.p1 + s.p1);} -inline Vector2i operator -(const Vector2i & f, const Vector2i & s) {return Vector2i(f.p0 - s.p0, f.p1 - s.p1);} -inline Vector2i operator /(const Vector2i & f, const int & s) {return Vector2i(f.p0 / s, f.p1 / s);} -inline uint qHash(const Vector2i & v) {return v.p0 ^ ((v.p1 << 8) | (v.p1 >> 24));} -inline QDebug operator <<(QDebug d, const Vector2i & v) {d.nospace() << "{" << v.p0 << ", " << v.p1 << "}"; return d.space();} +inline Vector2i operator+(const Vector2i & f, const Vector2i & s) { + return Vector2i(f.p0 + s.p0, f.p1 + s.p1); +} +inline Vector2i operator-(const Vector2i & f, const Vector2i & s) { + return Vector2i(f.p0 - s.p0, f.p1 - s.p1); +} +inline Vector2i operator/(const Vector2i & f, const int & s) { + return Vector2i(f.p0 / s, f.p1 / s); +} +inline uint qHash(const Vector2i & v) { + return v.p0 ^ ((v.p1 << 8) | (v.p1 >> 24)); +} +inline QDebug operator<<(QDebug d, const Vector2i & v) { + d.nospace() << "{" << v.p0 << ", " << v.p1 << "}"; + return d.space(); +} -inline QDataStream & operator <<(QDataStream & s, const Vector2i & v) {s << v.p0 << v.p1; return s;} -inline QDataStream & operator >>(QDataStream & s, Vector2i & v) {s >> v.p0 >> v.p1; return s;} +inline QDataStream & operator<<(QDataStream & s, const Vector2i & v) { + s << v.p0 << v.p1; + return s; +} +inline QDataStream & operator>>(QDataStream & s, Vector2i & v) { + s >> v.p0 >> v.p1; + return s; +} #pragma pack(push, 1) struct Vector3i { - Vector3i(int p0_ = 0, int p1_ = 0, int p2_ = 0) {p0 = p0_; p1 = p1_; p2 = p2_;} + Vector3i(int p0_ = 0, int p1_ = 0, int p2_ = 0) { + p0 = p0_; + p1 = p1_; + p2 = p2_; + } Vector3i(const QString & str); - Vector3i movedX(const int & o) {return Vector3i(p0 + o, p1, p2);} - Vector3i movedY(const int & o) {return Vector3i(p0, p1 + o, p2);} - Vector3i movedZ(const int & o) {return Vector3i(p0, p1, p2 + o);} - Vector3i moved(const int & x, const int & y, const int & z) {return Vector3i(p0 + x, p1 + y, p2 + z);} + Vector3i movedX(const int & o) { return Vector3i(p0 + o, p1, p2); } + Vector3i movedY(const int & o) { return Vector3i(p0, p1 + o, p2); } + Vector3i movedZ(const int & o) { return Vector3i(p0, p1, p2 + o); } + Vector3i moved(const int & x, const int & y, const int & z) { return Vector3i(p0 + x, p1 + y, p2 + z); } GLint p0; GLint p1; GLint p2; - bool operator ==(const Vector3i & o) const {return p0 == o.p0 && p1 == o.p1 && p2 == o.p2;} - bool operator !=(const Vector3i & o) const {return p0 != o.p0 || p1 != o.p1 || p2 != o.p2;} - void operator +=(int v) {p0 += v; p1 += v; p2 += v;} - QVector3D toQVector3D() const {return QVector3D(p0, p1, p2);} + bool operator==(const Vector3i & o) const { return p0 == o.p0 && p1 == o.p1 && p2 == o.p2; } + bool operator!=(const Vector3i & o) const { return p0 != o.p0 || p1 != o.p1 || p2 != o.p2; } + void operator+=(int v) { + p0 += v; + p1 += v; + p2 += v; + } + QVector3D toQVector3D() const { return QVector3D(p0, p1, p2); } }; #pragma pack(pop) -inline Vector3i operator +(const Vector3i & f, const Vector3i & s) {return Vector3i(f.p0 + s.p0, f.p1 + s.p1, f.p2 + s.p2);} -inline Vector3i operator -(const Vector3i & f, const Vector3i & s) {return Vector3i(f.p0 - s.p0, f.p1 - s.p1, f.p2 - s.p2);} -inline Vector3i operator /(const Vector3i & f, const int & s) {return Vector3i(f.p0 / s, f.p1 / s, f.p2 / s);} -inline uint qHash(const Vector3i & v) {return v.p0 ^ ((v.p1 << 8) | (v.p1 >> 24)) ^ ((v.p2 << 16) | (v.p2 >> 16));} -inline QDebug operator <<(QDebug d, const Vector3i & v) {d.nospace() << "{" << v.p0 << ", " << v.p1 << ", " << v.p2 << "}"; return d.space();} +inline Vector3i operator+(const Vector3i & f, const Vector3i & s) { + return Vector3i(f.p0 + s.p0, f.p1 + s.p1, f.p2 + s.p2); +} +inline Vector3i operator-(const Vector3i & f, const Vector3i & s) { + return Vector3i(f.p0 - s.p0, f.p1 - s.p1, f.p2 - s.p2); +} +inline Vector3i operator/(const Vector3i & f, const int & s) { + return Vector3i(f.p0 / s, f.p1 / s, f.p2 / s); +} +inline uint qHash(const Vector3i & v) { + return v.p0 ^ ((v.p1 << 8) | (v.p1 >> 24)) ^ ((v.p2 << 16) | (v.p2 >> 16)); +} +inline QDebug operator<<(QDebug d, const Vector3i & v) { + d.nospace() << "{" << v.p0 << ", " << v.p1 << ", " << v.p2 << "}"; + return d.space(); +} -inline QDataStream & operator <<(QDataStream & s, const Vector3i & v) {s << v.p0 << v.p1 << v.p2; return s;} -inline QDataStream & operator >>(QDataStream & s, Vector3i & v) {s >> v.p0 >> v.p1 >> v.p2; return s;} +inline QDataStream & operator<<(QDataStream & s, const Vector3i & v) { + s << v.p0 << v.p1 << v.p2; + return s; +} +inline QDataStream & operator>>(QDataStream & s, Vector3i & v) { + s >> v.p0 >> v.p1 >> v.p2; + return s; +} QVector3D vectorFromString(const QString & str); QColor colorFromString(const QString & str); -inline QVector4D QColor2QVector(const QColor & c) {return QVector4D(c.redF(), c.greenF(), c.blueF(), c.alphaF());} +inline QVector4D QColor2QVector(const QColor & c) { + return QVector4D(c.redF(), c.greenF(), c.blueF(), c.alphaF()); +} inline float cosABV(const QVector3D & v0, const QVector3D & v1) { float l = v0.length() * v1.length(); if (l == 0.f) return 0.; return (QVector3D::dotProduct(v0, v1)) / l; } -inline void normalizeAngleRad(float & a) {while (a < 0.) a += M_2PI; while (a >= M_2PI) a -= M_2PI;} -inline void normalizeAngleDeg360(float & a) {while (a < 0.) a += 360. ; while (a >= 360. ) a -= 360. ;} -inline QVector3D projection(const QVector3D & v, const QVector3D & to) {return to.normalized() * v.length() * cosABV(v, to);} +inline void normalizeAngleRad(float & a) { + while (a < 0.) + a += M_2PI; + while (a >= M_2PI) + a -= M_2PI; +} +inline void normalizeAngleDeg360(float & a) { + while (a < 0.) + a += 360.; + while (a >= 360.) + a -= 360.; +} +inline QVector3D projection(const QVector3D & v, const QVector3D & to) { + return to.normalized() * v.length() * cosABV(v, to); +} QVector3D orthToVector(const QVector3D & v, const float & scale = 1.); QVector3D rotateVector(const QVector3D & v, const QVector3D & a); void setVectorLength(QVector3D & v, const float & l); void lengthenVector(QVector3D & v, const float & l); -inline float squareLength(const QVector3D & from, const QVector3D & to) {return (to.x() - from.x())*(to.x() - from.x()) + (to.y() - from.y())*(to.y() - from.y()) + (to.z() - from.z())*(to.z() - from.z());} -inline QVector3D directionFromAngles(const QVector3D & a) {return rotateVector(QVector3D(1., 0., 0.), a);} -inline float frac(const float & x, const float & b) {return x - int(x / b) * b;} +inline float squareLength(const QVector3D & from, const QVector3D & to) { + return (to.x() - from.x()) * (to.x() - from.x()) + (to.y() - from.y()) * (to.y() - from.y()) + + (to.z() - from.z()) * (to.z() - from.z()); +} +inline QVector3D directionFromAngles(const QVector3D & a) { + return rotateVector(QVector3D(1., 0., 0.), a); +} +inline float frac(const float & x, const float & b) { + return x - int(x / b) * b; +} #endif // GLTYPES_H diff --git a/core/glvertexobject.cpp b/core/glvertexobject.cpp index 913e06a..803ac73 100644 --- a/core/glvertexobject.cpp +++ b/core/glvertexobject.cpp @@ -1,39 +1,37 @@ /* - QGL VertexObject - Ivan Pelipenko peri4ko@yandex.ru + QGL VertexObject + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #define GL_GLEXT_PROTOTYPES -#include #include "glvertexobject.h" +#include + using namespace QGLEngineShaders; -VertexObject::VertexObject(): - buffer_obj (GL_ARRAY_BUFFER, GL_STREAM_DRAW), - buffer_sel (GL_ARRAY_BUFFER, GL_STREAM_DRAW) { - vao_ = 0; - buffers_binded = false; +VertexObject::VertexObject(): buffer_obj(GL_ARRAY_BUFFER, GL_STREAM_DRAW), buffer_sel(GL_ARRAY_BUFFER, GL_STREAM_DRAW) { + vao_ = 0; + buffers_binded = false; objects_changed = selected_changed = true; } -VertexObject::~VertexObject() { -} +VertexObject::~VertexObject() {} void VertexObject::init(QOpenGLExtraFunctions * f) { @@ -59,13 +57,13 @@ void VertexObject::reinit() { vao_ = 0; buffer_obj.reinit(); buffer_sel.reinit(); - buffers_binded = false; + buffers_binded = false; objects_changed = selected_changed = true; } void VertexObject::bind(QOpenGLExtraFunctions * f) { - //qDebug() << "bind" << target_ << buffer_; + // qDebug() << "bind" << target_ << buffer_; f->glBindVertexArray(vao_); } diff --git a/core/glvertexobject.h b/core/glvertexobject.h index 7296d28..9e88ab4 100644 --- a/core/glvertexobject.h +++ b/core/glvertexobject.h @@ -1,19 +1,19 @@ /* - QGL VertexObject - Ivan Pelipenko peri4ko@yandex.ru + QGL VertexObject + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLVERTEXOBJECT_H @@ -23,33 +23,32 @@ #include "glshaders_types.h" -class VertexObject -{ +class VertexObject { public: VertexObject(); ~VertexObject(); - void init (QOpenGLExtraFunctions * f); - void destroy (QOpenGLExtraFunctions * f); + void init(QOpenGLExtraFunctions * f); + void destroy(QOpenGLExtraFunctions * f); void reinit(); - void bind (QOpenGLExtraFunctions * f); - void release (QOpenGLExtraFunctions * f); + void bind(QOpenGLExtraFunctions * f); + void release(QOpenGLExtraFunctions * f); - void bindBuffers (QOpenGLExtraFunctions * f, Buffer & geom, Buffer & elem, bool force = false); - void loadObject (QOpenGLExtraFunctions * f, const QGLEngineShaders::Object & object); - void loadObjects (QOpenGLExtraFunctions * f, const QVector & objects); + void bindBuffers(QOpenGLExtraFunctions * f, Buffer & geom, Buffer & elem, bool force = false); + void loadObject(QOpenGLExtraFunctions * f, const QGLEngineShaders::Object & object); + void loadObjects(QOpenGLExtraFunctions * f, const QVector & objects); void loadSelections(QOpenGLExtraFunctions * f, const QVector & sels); void draw(QOpenGLExtraFunctions * f, GLenum geom_type, int vert_cout, int obj_count); - GLuint ID() const {return vao_;} - bool isInit() const {return vao_ != 0;} + GLuint ID() const { return vao_; } + bool isInit() const { return vao_ != 0; } - bool isObjectsChanged() const {return objects_changed;} - bool isSelectionChanged() const {return selected_changed;} - void setObjectsChanged(bool yes = true) {objects_changed = yes;} - void setSelectionChanged(bool yes = true) {selected_changed = yes;} + bool isObjectsChanged() const { return objects_changed; } + bool isSelectionChanged() const { return selected_changed; } + void setObjectsChanged(bool yes = true) { objects_changed = yes; } + void setSelectionChanged(bool yes = true) { selected_changed = yes; } private: void loadBuffer(QOpenGLExtraFunctions * f, Buffer & buf, const void * data, int size); diff --git a/core/hdr.cpp b/core/hdr.cpp index 7e4af1d..223c015 100644 --- a/core/hdr.cpp +++ b/core/hdr.cpp @@ -1,41 +1,41 @@ /* - QGL HDR - Ivan Pelipenko peri4ko@yandex.ru + QGL HDR + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "hdr_p.h" -#include + #include +#include -#define RGBE_DATA_RED 2 -#define RGBE_DATA_GREEN 1 -#define RGBE_DATA_BLUE 0 +#define RGBE_DATA_RED 2 +#define RGBE_DATA_GREEN 1 +#define RGBE_DATA_BLUE 0 /* number of floats per pixel */ -#define RGBE_DATA_SIZE 3 +#define RGBE_DATA_SIZE 3 -void rgbe2float(float *red, float *green, float *blue, uchar rgbe[4]) { +void rgbe2float(float * red, float * green, float * blue, uchar rgbe[4]) { float f; if (rgbe[3]) { - f = static_cast(ldexp(1.0,rgbe[3]-(int)(128+8))); - *red = rgbe[0] * f; + f = static_cast(ldexp(1.0, rgbe[3] - (int)(128 + 8))); + *red = rgbe[0] * f; *green = rgbe[1] * f; - *blue = rgbe[2] * f; - } - else + *blue = rgbe[2] * f; + } else *red = *green = *blue = 0.0; } @@ -43,10 +43,9 @@ void rgbe2float(float *red, float *green, float *blue, uchar rgbe[4]) { /* simple read routine. will not correctly handle run length encoding */ bool RGBE_ReadPixels(QDataStream * fp, float * data, int numpixels) { uchar rgbe[4]; - while(numpixels-- > 0) { - if (fp->readRawData((char*)rgbe, sizeof(rgbe)) < 1) - return false; - rgbe2float(&data[RGBE_DATA_RED], &data[RGBE_DATA_GREEN], &data[RGBE_DATA_BLUE],rgbe); + while (numpixels-- > 0) { + if (fp->readRawData((char *)rgbe, sizeof(rgbe)) < 1) return false; + rgbe2float(&data[RGBE_DATA_RED], &data[RGBE_DATA_GREEN], &data[RGBE_DATA_BLUE], rgbe); data += RGBE_DATA_SIZE; } return true; @@ -59,50 +58,48 @@ bool RGBE_ReadPixels_RLE(QDataStream * fp, float * data, int scanline_width, int uchar buf[2]; QByteArray scanline_buffer; - if ((scanline_width < 8)||(scanline_width > 0x7fff)) - /* run length encoding is not allowed so read flat*/ - return RGBE_ReadPixels(fp,data,scanline_width*num_scanlines); - scanline_buffer.resize(4*scanline_width); + if ((scanline_width < 8) || (scanline_width > 0x7fff)) /* run length encoding is not allowed so read flat*/ + return RGBE_ReadPixels(fp, data, scanline_width * num_scanlines); + scanline_buffer.resize(4 * scanline_width); /* read in each successive scanline */ - while(num_scanlines > 0) { - if (fp->readRawData((char*)rgbe,sizeof(rgbe)) < 1) { + while (num_scanlines > 0) { + if (fp->readRawData((char *)rgbe, sizeof(rgbe)) < 1) { return false; } - if ((rgbe[0] != 2)||(rgbe[1] != 2)||(rgbe[2] & 0x80)) { + if ((rgbe[0] != 2) || (rgbe[1] != 2) || (rgbe[2] & 0x80)) { /* this file is not run length encoded */ - rgbe2float(&data[RGBE_DATA_RED],&data[RGBE_DATA_GREEN],&data[RGBE_DATA_BLUE],rgbe); + rgbe2float(&data[RGBE_DATA_RED], &data[RGBE_DATA_GREEN], &data[RGBE_DATA_BLUE], rgbe); data += RGBE_DATA_SIZE; - return RGBE_ReadPixels(fp,data,scanline_width*num_scanlines-1); + return RGBE_ReadPixels(fp, data, scanline_width * num_scanlines - 1); } - if ((((int)rgbe[2])<<8 | rgbe[3]) != scanline_width) { + if ((((int)rgbe[2]) << 8 | rgbe[3]) != scanline_width) { return false; } - ptr = (uchar*)scanline_buffer.data(); + ptr = (uchar *)scanline_buffer.data(); /* read each of the four channels for the scanline into the buffer */ - for(i=0;i<4;i++) { - ptr_end = (uchar*)scanline_buffer.data() + ((i+1)*scanline_width); - while(ptr < ptr_end) { - if (fp->readRawData((char*)buf,sizeof(buf[0])*2) < 1) { + for (i = 0; i < 4; i++) { + ptr_end = (uchar *)scanline_buffer.data() + ((i + 1) * scanline_width); + while (ptr < ptr_end) { + if (fp->readRawData((char *)buf, sizeof(buf[0]) * 2) < 1) { return false; } if (buf[0] > 128) { - /* a run of the same value */ - count = buf[0]-128; - if ((count == 0)||(count > ptr_end - ptr)) { - return false; - } - while(count-- > 0) - *ptr++ = buf[1]; - } - else { + /* a run of the same value */ + count = buf[0] - 128; + if ((count == 0) || (count > ptr_end - ptr)) { + return false; + } + while (count-- > 0) + *ptr++ = buf[1]; + } else { /* a non-run */ count = buf[0]; - if ((count == 0)||(count > ptr_end - ptr)) { + if ((count == 0) || (count > ptr_end - ptr)) { return false; } *ptr++ = buf[1]; if (--count > 0) { - if (fp->readRawData((char*)ptr,sizeof(*ptr)*count) < 1) { + if (fp->readRawData((char *)ptr, sizeof(*ptr) * count) < 1) { return false; } ptr += count; @@ -111,13 +108,12 @@ bool RGBE_ReadPixels_RLE(QDataStream * fp, float * data, int scanline_width, int } } /* now convert data from buffer into floats */ - for(i=0;i. + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef HDR_P_H diff --git a/formats/loader_assimp.cpp b/formats/loader_assimp.cpp index 9b20ee2..458a872 100644 --- a/formats/loader_assimp.cpp +++ b/formats/loader_assimp.cpp @@ -1,41 +1,52 @@ /* - QGL Loader Assimp - Ivan Pelipenko peri4ko@yandex.ru + QGL Loader Assimp + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "loader_assimp.h" -#include "glscene.h" -#include "glmesh.h" + #include "glmaterial.h" +#include "glmesh.h" #include "globject.h" +#include "glscene.h" + #include +#include +#include #include #include -#include -#include -QString fromAiString (const aiString & s) {return QString::fromLocal8Bit(s.C_Str());} -QColor fromAiColor (const aiColor4D & c) {return QColor::fromRgbF(piClamp(c.r, 0.f, 1.f), piClamp(c.g, 0.f, 1.f), piClamp(c.b, 0.f, 1.f));} -QVector3D fromAiVector3D(const aiVector3D & v) {return QVector3D(v.x, v.y, v.z);} - Vector3i fromAiFace (const aiFace & v) {return Vector3i(v.mIndices[0], v.mIndices[1], v.mIndices[2]);} -QMatrix4x4 fromAiMatrix4D(const aiMatrix4x4 & v) {return QMatrix4x4(v.a1, v.a2, v.a3, v.a4, - v.b1, v.b2, v.b3, v.b4, - v.c1, v.c2, v.c3, v.c4, - v.d1, v.d2, v.d3, v.d4);} -bool isAiMeshTriangles(const aiMesh * m) {return (m->mPrimitiveTypes & aiPrimitiveType_TRIANGLE) == aiPrimitiveType_TRIANGLE;} +QString fromAiString(const aiString & s) { + return QString::fromLocal8Bit(s.C_Str()); +} +QColor fromAiColor(const aiColor4D & c) { + return QColor::fromRgbF(piClamp(c.r, 0.f, 1.f), piClamp(c.g, 0.f, 1.f), piClamp(c.b, 0.f, 1.f)); +} +QVector3D fromAiVector3D(const aiVector3D & v) { + return QVector3D(v.x, v.y, v.z); +} +Vector3i fromAiFace(const aiFace & v) { + return Vector3i(v.mIndices[0], v.mIndices[1], v.mIndices[2]); +} +QMatrix4x4 fromAiMatrix4D(const aiMatrix4x4 & v) { + return QMatrix4x4(v.a1, v.a2, v.a3, v.a4, v.b1, v.b2, v.b3, v.b4, v.c1, v.c2, v.c3, v.c4, v.d1, v.d2, v.d3, v.d4); +} +bool isAiMeshTriangles(const aiMesh * m) { + return (m->mPrimitiveTypes & aiPrimitiveType_TRIANGLE) == aiPrimitiveType_TRIANGLE; +} Mesh * assimpMesh(const aiMesh * m) { @@ -44,9 +55,11 @@ Mesh * assimpMesh(const aiMesh * m) { Mesh * ret = new Mesh(); int vcnt = m->mNumVertices, tcnt = m->mNumFaces; - QVector & v(ret->vertices()); v.resize(vcnt); - QVector & t(ret->texcoords()); t.resize(vcnt); - QVector< Vector3i> & ind(ret->indicesTriangles()); + QVector & v(ret->vertices()); + v.resize(vcnt); + QVector & t(ret->texcoords()); + t.resize(vcnt); + QVector & ind(ret->indicesTriangles()); for (int i = 0; i < vcnt; ++i) v[i] = fromAiVector3D(m->mVertices[i]); @@ -72,12 +85,12 @@ Mesh * assimpMesh(const aiMesh * m) { ind.resize(tcnt); int si = 0; for (int i = 0; i < tcnt; ++i) { - si = i+i+i; - ind[i] = Vector3i(si, si+1, si+2); + si = i + i + i; + ind[i] = Vector3i(si, si + 1, si + 2); } } - //qDebug() << "add mesh" << v.size() << ret->normals().size() << ret->texcoords().size() << ret->indices().size(); + // qDebug() << "add mesh" << v.size() << ret->normals().size() << ret->texcoords().size() << ret->indices().size(); return ret; } @@ -86,7 +99,7 @@ Mesh * assimpMesh(const aiMesh * m) { QColor aiMatColor(const aiMaterial * m, const char * key, uint s0, uint s1, const QColor & def = Qt::white) { aiColor4D col; aiReturn r = m->Get(key, s0, s1, col); - //qDebug() << key << r << col.r << col.g << col.b; + // qDebug() << key << r << col.r << col.g << col.b; if (r != aiReturn_SUCCESS) return def; return fromAiColor(col); } @@ -98,36 +111,36 @@ float aiMatFloat(const aiMaterial * m, const char * key, uint s0, uint s1, float } QString aiMatString(const aiMaterial * m, const char * key, uint s0, uint s1) { aiString p; - aiReturn r = const_cast(m)->Get(key, s0, s1, p); - //qDebug() << "GetTexture" << key << s0 << r << fromAiString(p); + aiReturn r = const_cast(m)->Get(key, s0, s1, p); + // qDebug() << "GetTexture" << key << s0 << r << fromAiString(p); if (r != aiReturn_SUCCESS) return QString(); return fromAiString(p); } Material * assimpMaterial(const aiMaterial * m) { if (!m) return 0; Material * ret = new Material(); - ///WARNING: no function GetName() in aiMaterial in stable release - //ret->name = fromAiString(const_cast(m)->GetName()); + /// WARNING: no function GetName() in aiMaterial in stable release + // ret->name = fromAiString(const_cast(m)->GetName()); aiString name; - const_cast(m)->Get(AI_MATKEY_NAME,name); - ret->name = fromAiString(name); - //qDebug() << "mat" << ret->name; - //for (int i = 0; i < m->mNumProperties; ++i) { + const_cast(m)->Get(AI_MATKEY_NAME, name); + ret->name = fromAiString(name); + // qDebug() << "mat" << ret->name; + // for (int i = 0; i < m->mNumProperties; ++i) { // qDebug()<< fromAiString(m->mProperties[i]->mKey);// << "=" << aiMatFloat(m, m->mProperties[i]->mKey.C_Str(), 0, 0); - //} + // } ret->color_diffuse = aiMatColor(m, AI_MATKEY_COLOR_DIFFUSE); ret->color_emission = aiMatColor(m, AI_MATKEY_COLOR_EMISSIVE); - float shine = aiMatFloat(m, AI_MATKEY_SHININESS, -1.f); + float shine = aiMatFloat(m, AI_MATKEY_SHININESS, -1.f); if (shine >= 0) { ret->map_roughness.color_amount = 0.8f - (shine / 100.f * 0.6f); - //qDebug() << "shine" << shine; + // qDebug() << "shine" << shine; } - ret->map_diffuse .bitmap_path = aiMatString(m, AI_MATKEY_TEXTURE_DIFFUSE(0)); - ret->map_normal .bitmap_path = aiMatString(m, AI_MATKEY_TEXTURE_NORMALS(0)); - //ret->map_metalness.bitmap_path = aiMatString(m, AI_MATKEY_TEXTURE_SPECULAR(0)); + ret->map_diffuse.bitmap_path = aiMatString(m, AI_MATKEY_TEXTURE_DIFFUSE(0)); + ret->map_normal.bitmap_path = aiMatString(m, AI_MATKEY_TEXTURE_NORMALS(0)); + // ret->map_metalness.bitmap_path = aiMatString(m, AI_MATKEY_TEXTURE_SPECULAR(0)); ret->map_roughness.bitmap_path = aiMatString(m, AI_MATKEY_TEXTURE_SHININESS(0)); - ret->map_emission .bitmap_path = aiMatString(m, AI_MATKEY_TEXTURE_EMISSIVE(0)); - ret->transparency = 1.f - aiMatFloat(m, AI_MATKEY_OPACITY, 1.f); + ret->map_emission.bitmap_path = aiMatString(m, AI_MATKEY_TEXTURE_EMISSIVE(0)); + ret->transparency = 1.f - aiMatFloat(m, AI_MATKEY_OPACITY, 1.f); ret->detectMaps(); ret->setTypes(); return ret; @@ -136,19 +149,17 @@ Material * assimpMaterial(const aiMaterial * m) { Light * assimpLight(const aiLight * l) { if (!l) return 0; - if (l->mType != aiLightSource_POINT && l->mType != aiLightSource_SPOT) - return 0; + if (l->mType != aiLightSource_POINT && l->mType != aiLightSource_SPOT) return 0; Light * ret = new Light(); ret->setName(fromAiString(l->mName)); ret->setPos(fromAiVector3D(l->mPosition)); ret->setDirection(fromAiVector3D(l->mDirection)); - ret->decay_const = l->mAttenuationConstant ; - ret->decay_linear = l->mAttenuationLinear ; + ret->decay_const = l->mAttenuationConstant; + ret->decay_linear = l->mAttenuationLinear; ret->decay_quadratic = l->mAttenuationQuadratic; - ret->angle_start = l->mAngleInnerCone * rad2deg; - ret->angle_end = l->mAngleOuterCone * rad2deg; - if (l->mType == aiLightSource_SPOT) - ret->light_type = Light::Cone; + ret->angle_start = l->mAngleInnerCone * rad2deg; + ret->angle_end = l->mAngleOuterCone * rad2deg; + if (l->mType == aiLightSource_SPOT) ret->light_type = Light::Cone; QVector3D col(l->mColorDiffuse.r, l->mColorDiffuse.g, l->mColorDiffuse.b); ret->intensity = col.length(); col /= ret->intensity; @@ -157,25 +168,28 @@ Light * assimpLight(const aiLight * l) { } -ObjectBaseList assimpObject(const aiNode * n, const QVector & meshes, aiMesh ** ai_meshes, - const QVector & materials, - const QMap & light_by_name, QVector & out_lights) { +ObjectBaseList assimpObject(const aiNode * n, + const QVector & meshes, + aiMesh ** ai_meshes, + const QVector & materials, + const QMap & light_by_name, + QVector & out_lights) { if (!n) return ObjectBaseList(); ObjectBaseList ret; ObjectBase * obj = 0; - QString name = fromAiString(n->mName); - Light * light = light_by_name.value(name, 0); + QString name = fromAiString(n->mName); + Light * light = light_by_name.value(name, 0); if (light) { obj = light->clone(); - out_lights << (Light*)obj; + out_lights << (Light *)obj; } else obj = new ObjectBase(); obj->setName(name); obj->setMatrix(fromAiMatrix4D(n->mTransformation)); ret << obj; - //qDebug() << "add object" << ret << ret->name(); + // qDebug() << "add object" << ret << ret->name(); if (!light) { - //qDebug() << name << "has" << n->mNumMeshes << "meshes"; + // qDebug() << name << "has" << n->mNumMeshes << "meshes"; for (uint i = 0; i < n->mNumMeshes; ++i) { int mi = n->mMeshes[i]; if (meshes[mi]) { @@ -185,17 +199,16 @@ ObjectBaseList assimpObject(const aiNode * n, const QVector & meshes, a } obj->setMesh(meshes[mi]); int mati = ai_meshes[mi]->mMaterialIndex; - if (mati >= 0 || mati < materials.size()) - obj->setMaterial(materials[mati]); - //ret->setMesh(meshes[mi]); - //qDebug() << "set mesh" << mi << ret->mesh(); - //break; + if (mati >= 0 || mati < materials.size()) obj->setMaterial(materials[mati]); + // ret->setMesh(meshes[mi]); + // qDebug() << "set mesh" << mi << ret->mesh(); + // break; } } } for (uint i = 0; i < n->mNumChildren; ++i) { ObjectBaseList cl = assimpObject(n->mChildren[i], meshes, ai_meshes, materials, light_by_name, out_lights); - foreach (ObjectBase * c, cl) + foreach(ObjectBase * c, cl) obj->addChild(c); } @@ -207,38 +220,36 @@ Scene * loadScene(const QString & filepath) { if (filepath.isEmpty()) return 0; qDebug() << "[Loader Assimp] Import" << filepath << "..."; Assimp::Importer importer; - const aiScene * ais = importer.ReadFile(filepath.toUtf8(), aiProcess_Triangulate | - aiProcess_SortByPType | - aiProcess_GenUVCoords | - aiProcess_TransformUVCoords); + const aiScene * ais = + importer.ReadFile(filepath.toUtf8(), + aiProcess_Triangulate | aiProcess_SortByPType | aiProcess_GenUVCoords | aiProcess_TransformUVCoords); if (!ais) { qDebug() << "[Loader Assimp] Error: \"" + QString(importer.GetErrorString()) + "\""; return 0; } qDebug() << "[Loader Assimp] Imported" << ais->mNumMeshes << "meshes"; - QVector meshes; + QVector meshes; for (uint i = 0; i < ais->mNumMeshes; ++i) meshes << assimpMesh(ais->mMeshes[i]); - QVector materials; + QVector materials; for (uint i = 0; i < ais->mNumMaterials; ++i) materials << assimpMaterial(ais->mMaterials[i]); - QVector lights; + QVector lights; for (uint i = 0; i < ais->mNumLights; ++i) lights << assimpLight(ais->mLights[i]); - QMap light_by_name; - foreach (Light * l, lights) - if (l) - light_by_name[l->name()] = l; + QMap light_by_name; + foreach(Light * l, lights) + if (l) light_by_name[l->name()] = l; - QVector out_lights; + QVector out_lights; ObjectBaseList rootl = assimpObject(ais->mRootNode, meshes, ais->mMeshes, materials, light_by_name, out_lights); if (rootl.isEmpty()) return 0; ObjectBase * root = rootl[0]; root->transferTransformToChildren(true); ObjectBaseList rcl = root->children(true); - foreach (ObjectBase * c, rcl) { - foreach (Light * l, out_lights) { + foreach(ObjectBase * c, rcl) { + foreach(Light * l, out_lights) { if (c->name() == (l->name() + ".Target")) { l->setDistance((l->worldPos() - c->worldPos()).length()); delete c; @@ -250,7 +261,7 @@ Scene * loadScene(const QString & filepath) { Scene * scene = new Scene(); scene->setName(root->name()); - foreach (ObjectBase * o, root->children()) + foreach(ObjectBase * o, root->children()) scene->addObject(o); lights.removeAll(nullptr); qDeleteAll(lights); diff --git a/formats/loader_assimp.h b/formats/loader_assimp.h index 348c32f..086d25b 100644 --- a/formats/loader_assimp.h +++ b/formats/loader_assimp.h @@ -1,19 +1,19 @@ /* - QGL Loader Assimp - Ivan Pelipenko peri4ko@yandex.ru + QGL Loader Assimp + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef LOADER_ASSIMP_H diff --git a/formats/loader_qgl.cpp b/formats/loader_qgl.cpp index 9e04190..5b4db1a 100644 --- a/formats/loader_qgl.cpp +++ b/formats/loader_qgl.cpp @@ -1,23 +1,25 @@ /* - QGL Loader QGL - Ivan Pelipenko peri4ko@yandex.ru + QGL Loader QGL + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "loader_qgl.h" + #include "glscene.h" + #include @@ -37,7 +39,7 @@ Scene * loadFromQGLFile(const QString & filepath) { return 0; } ushort version = 0x1; - f.peek((char*)&version, 2); + f.peek((char *)&version, 2); if (version != 1) { qDebug() << "[Loader QGL] Error: \"" + filepath + "\" unsupported version!"; return 0; @@ -45,7 +47,7 @@ Scene * loadFromQGLFile(const QString & filepath) { Scene * ret = 0; s.skipRawData(2); s >> ret; - //root->buildTransform(); + // root->buildTransform(); qDebug() << "[Loader QGL] Loaded" << ret->objectsCount(true) << "objects from" << filepath; return ret; } @@ -53,15 +55,14 @@ Scene * loadFromQGLFile(const QString & filepath) { bool saveToQGLFile(const QString & filepath, const Scene * scene) { QFile f(filepath); - if (!f.open(QIODevice::ReadWrite)) - return false; + if (!f.open(QIODevice::ReadWrite)) return false; f.resize(0); QDataStream s(&f); s.setVersion(QDataStream::Qt_5_0); - char sign[4] = {'Q', 'G', 'L', 'E'}; + char sign[4] = {'Q', 'G', 'L', 'E'}; ushort version = 0x1; s.writeRawData(sign, 4); - s.writeRawData((char*)&version, 2); + s.writeRawData((char *)&version, 2); s << scene; return true; } diff --git a/formats/loader_qgl.h b/formats/loader_qgl.h index 72aed12..42060f9 100644 --- a/formats/loader_qgl.h +++ b/formats/loader_qgl.h @@ -1,25 +1,26 @@ /* - QGL Loader QGL - Ivan Pelipenko peri4ko@yandex.ru + QGL Loader QGL + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef LOADER_QGL_H #define LOADER_QGL_H #include "gltypes.h" + #include Scene * loadFromQGLFile(const QString & filepath); diff --git a/glcamera.cpp b/glcamera.cpp index 374da44..348d275 100644 --- a/glcamera.cpp +++ b/glcamera.cpp @@ -1,19 +1,19 @@ /* - QGL Camera - Ivan Pelipenko peri4ko@yandex.ru + QGL Camera + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "gltypes.h" @@ -21,9 +21,9 @@ Camera::Camera() { - type_ = glCamera; - fov_ = 60.; - roll_ = 0.; + type_ = glCamera; + fov_ = 60.; + roll_ = 0.; depth_start = 0.1f; mirror_x = mirror_y = false; } @@ -37,11 +37,11 @@ QMatrix4x4 Camera::offsetMatrix() const { void Camera::assign(const Camera & c) { - trans = c.trans; - aim_dist = c.aim_dist; - fov_ = c.fov_; - mirror_x = c.mirror_x; - mirror_y = c.mirror_y; + trans = c.trans; + aim_dist = c.aim_dist; + fov_ = c.fov_; + mirror_x = c.mirror_x; + mirror_y = c.mirror_y; depth_start = c.depth_start; buildTransform(); } @@ -50,34 +50,34 @@ void Camera::assign(const Camera & c) { ObjectBase * Camera::clone(bool withChildren) { Camera * o = new Camera(*this); o->is_init = false; - o->name_ = name_; - o->scene_ = nullptr; + o->name_ = name_; + o->scene_ = nullptr; o->children_.clear(); if (withChildren) { for (int i = 0; i < children_.size(); ++i) o->addChild(children_[i]->clone(withChildren)); } - o->trans = trans; - o->aim_dist = aim_dist; - o->fov_ = fov_; - o->roll_ = roll_; - o->mirror_x = mirror_x; - o->mirror_y = mirror_y; + o->trans = trans; + o->aim_dist = aim_dist; + o->fov_ = fov_; + o->roll_ = roll_; + o->mirror_x = mirror_x; + o->mirror_y = mirror_y; o->depth_start = depth_start; - o->meta = meta; + o->meta = meta; return o; } QMatrix4x4 Camera::viewMatrix() const { QMatrix4x4 ret; - //qDebug() << pos() << aim(); + // qDebug() << pos() << aim(); ret.translate(0., 0., -distance()); ret.rotate(-roll_, 0., 0., 1.); ret *= trans.matrixRotateScale().inverted(); if (parent_) { QMatrix4x4 pmat = parent_->worldTransform(); - offset_ = pmat.column(3).toVector3D(); + offset_ = pmat.column(3).toVector3D(); pmat(0, 3) = pmat(1, 3) = pmat(2, 3) = 0.; pmat.translate(aim()); ret *= pmat.inverted(); diff --git a/glcamera.h b/glcamera.h index ef805a5..b54aa22 100644 --- a/glcamera.h +++ b/glcamera.h @@ -1,19 +1,19 @@ /* - QGL Camera - Ivan Pelipenko peri4ko@yandex.ru + QGL Camera + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLCAMERA_H @@ -22,29 +22,29 @@ #include "globject.h" -class Camera: public AimedObject -{ - friend QDataStream & operator <<(QDataStream & s, const ObjectBase * p); - friend QDataStream & operator >>(QDataStream & s, ObjectBase *& p); +class Camera: public AimedObject { + friend QDataStream & operator<<(QDataStream & s, const ObjectBase * p); + friend QDataStream & operator>>(QDataStream & s, ObjectBase *& p); + public: Camera(); - void setFOV(const float & f) {fov_ = f;} - void setAngles(const QVector3D & a) {setRotation(a);} - void setAngleZ(const float & a) {setRotationZ(a);} - void setAngleXY(const float & a) {setRotationX(a);} - void setAngleRoll(const float & a) {roll_ = a;} - void setDepthStart(const float & d) {depth_start = d;} - void setMirrorX(bool yes) {mirror_x = yes;} - void setMirrorY(bool yes) {mirror_y = yes;} + void setFOV(const float & f) { fov_ = f; } + void setAngles(const QVector3D & a) { setRotation(a); } + void setAngleZ(const float & a) { setRotationZ(a); } + void setAngleXY(const float & a) { setRotationX(a); } + void setAngleRoll(const float & a) { roll_ = a; } + void setDepthStart(const float & d) { depth_start = d; } + void setMirrorX(bool yes) { mirror_x = yes; } + void setMirrorY(bool yes) { mirror_y = yes; } - float FOV() const {return fov_;} - float angleZ() const {return rotationZ();} - float angleXY() const {return rotationX();} - float angleRoll() const {return roll_;} - float depthStart() const {return depth_start;} - bool isMirrorX() const {return mirror_x;} - bool isMirrorY() const {return mirror_y;} + float FOV() const { return fov_; } + float angleZ() const { return rotationZ(); } + float angleXY() const { return rotationX(); } + float angleRoll() const { return roll_; } + float depthStart() const { return depth_start; } + bool isMirrorX() const { return mirror_x; } + bool isMirrorY() const { return mirror_y; } void assign(const Camera & c); virtual ObjectBase * clone(bool withChildren = true); @@ -52,7 +52,7 @@ public: QMatrix4x4 projectionMatrix(double aspect) const; QMatrix4x4 offsetMatrix() const; - QMatrix4x4 fullViewMatrix() const {return viewMatrix() * offsetMatrix();} + QMatrix4x4 fullViewMatrix() const { return viewMatrix() * offsetMatrix(); } private: mutable QVector3D offset_; diff --git a/globject.cpp b/globject.cpp index bad61ee..48639f6 100644 --- a/globject.cpp +++ b/globject.cpp @@ -1,59 +1,61 @@ /* - QGL ObjectBase & Light - Ivan Pelipenko peri4ko@yandex.ru + QGL ObjectBase & Light + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "globject.h" + #include "glcamera.h" -#include "glscene.h" #include "glmesh.h" +#include "glscene.h" + #include -//static int _count = 0; +// static int _count = 0; ObjectBase::ObjectBase(Mesh * geom, Material * mat) { - type_ = glMesh; + type_ = glMesh; render_mode = View; - prev_pass = rpSolid; - parent_ = nullptr; - color_ = Qt::white; + prev_pass = rpSolid; + parent_ = nullptr; + color_ = Qt::white; is_root = is_init = selected_ = false; visible_ = accept_fog = accept_light = cast_shadow = rec_shadow = select_ = true; - line_width = -1.; - id_ = 0; - blend_src = GL_SRC_ALPHA; - blend_dest = GL_ONE_MINUS_SRC_ALPHA; - type_ = glMesh; + line_width = -1.; + id_ = 0; + blend_src = GL_SRC_ALPHA; + blend_dest = GL_ONE_MINUS_SRC_ALPHA; + type_ = glMesh; raw_matrix = selected_aim = false; mat_.setToIdentity(); - scene_ = nullptr; - mesh_ = geom; + scene_ = nullptr; + mesh_ = geom; material_ = mat; - //qDebug() << "ObjectBase, now" << ++_count; + // qDebug() << "ObjectBase, now" << ++_count; } ObjectBase::~ObjectBase() { - //qDebug() << "~ObjectBase, now" << --_count; + // qDebug() << "~ObjectBase, now" << --_count; if (parent_) parent_->children_.removeAll(this); if (scene_) { scene_->__objectDeleted(this); scene_->setTreeChanged(); scene_->setTreeStructChanged(); } - foreach (ObjectBase * c, children_) { + foreach(ObjectBase * c, children_) { c->parent_ = nullptr; delete c; } @@ -61,27 +63,27 @@ ObjectBase::~ObjectBase() { ObjectBase * ObjectBase::clone(bool withChildren) { - ObjectBase * o = new ObjectBase(); - o->prev_pass = prev_pass; - o->is_init = false; + ObjectBase * o = new ObjectBase(); + o->prev_pass = prev_pass; + o->is_init = false; o->accept_light = accept_light; - o->accept_fog = accept_fog; - o->visible_ = visible_; - o->color_ = color_; - o->type_ = type_; - o->raw_matrix = raw_matrix; - o->mat_ = mat_; - o->trans = trans; - o->itransform_ = itransform_; - o->bound = bound; - o->name_ = name_;// + "_copy"; - o->blend_src = blend_src; - o->blend_dest = blend_dest; - o->pos_h = pos_h; - o->material_ = material_; - o->mesh_ = mesh_; - o->meta = meta; - o->scene_ = nullptr; + o->accept_fog = accept_fog; + o->visible_ = visible_; + o->color_ = color_; + o->type_ = type_; + o->raw_matrix = raw_matrix; + o->mat_ = mat_; + o->trans = trans; + o->itransform_ = itransform_; + o->bound = bound; + o->name_ = name_; // + "_copy"; + o->blend_src = blend_src; + o->blend_dest = blend_dest; + o->pos_h = pos_h; + o->material_ = material_; + o->mesh_ = mesh_; + o->meta = meta; + o->scene_ = nullptr; if (withChildren) { for (int i = 0; i < children_.size(); ++i) o->addChild(children_[i]->clone(withChildren)); @@ -97,8 +99,8 @@ void ObjectBase::destroy() { void ObjectBase::init() { calculateBoundingBox(); - //material_.reflection.create(); - //qDebug() << "init" << vbo.buffer_; + // material_.reflection.create(); + // qDebug() << "init" << vbo.buffer_; is_init = true; } @@ -106,33 +108,31 @@ void ObjectBase::init() { RenderPass ObjectBase::pass() const { RenderPass ret = rpSolid; if (material_) - if (material_->hasTransparency()) - ret = rpTransparent; + if (material_->hasTransparency()) ret = rpTransparent; return ret; } void ObjectBase::setScene(Scene * v) { scene_ = v; - foreach (ObjectBase * c, children_) + foreach(ObjectBase * c, children_) c->setScene(v); } void ObjectBase::addChild(ObjectBase * o) { if (o == this) return; - if (o->parent_) - o->parent_->children_.removeAll(o); + if (o->parent_) o->parent_->children_.removeAll(o); children_ << o; o->parent_ = this; o->setScene(scene_); o->buildTransform(); /*if (scene_) { - ObjectBaseList cl = o->children(true); - cl << o; - //foreach (ObjectBase * i, cl) { - // emit view_->objectAdded(i); - //} + ObjectBaseList cl = o->children(true); + cl << o; + //foreach (ObjectBase * i, cl) { + // emit view_->objectAdded(i); + //} }*/ setSceneTreeChanged(); } @@ -156,8 +156,8 @@ void ObjectBase::removeChild(int index) { void ObjectBase::clearChildren(bool deleteAll) { - foreach (ObjectBase * i, children_) { - i->scene_ = nullptr; + foreach(ObjectBase * i, children_) { + i->scene_ = nullptr; i->parent_ = nullptr; i->clearChildren(deleteAll); if (deleteAll) { @@ -178,7 +178,7 @@ ObjectBase * ObjectBase::child(int index) { ObjectBase * ObjectBase::child(const QString & name) { - foreach (ObjectBase * i, children_) + foreach(ObjectBase * i, children_) if (i->name_ == name) return i; return nullptr; } @@ -191,7 +191,7 @@ const ObjectBase * ObjectBase::child(int index) const { const ObjectBase * ObjectBase::child(const QString & name) const { - foreach (ObjectBase * i, children_) + foreach(ObjectBase * i, children_) if (i->name_ == name) return i; return nullptr; } @@ -226,9 +226,9 @@ void ObjectBase::setVisible(bool v) { void ObjectBase::rotateZ(GLfloat a) { raw_matrix = false; trans.setRotationZ(trans.rotationZ() + a); - //angles_.setZ(angles_.z() + a); - //while (angles_.z() < -360.f) angles_.setZ(angles_.z() + 360.f); - //while (angles_.z() > 360.f) angles_.setZ(angles_.z() - 360.f); + // angles_.setZ(angles_.z() + a); + // while (angles_.z() < -360.f) angles_.setZ(angles_.z() + 360.f); + // while (angles_.z() > 360.f) angles_.setZ(angles_.z() - 360.f); buildTransform(); } @@ -236,9 +236,9 @@ void ObjectBase::rotateZ(GLfloat a) { void ObjectBase::setRotationZ(GLfloat a) { raw_matrix = false; trans.setRotationZ(a); - //angles_.setZ(a); - //while (angles_.z() < -360.f) angles_.setZ(angles_.z() + 360.f); - //while (angles_.z() > 360.f) angles_.setZ(angles_.z() - 360.f); + // angles_.setZ(a); + // while (angles_.z() < -360.f) angles_.setZ(angles_.z() + 360.f); + // while (angles_.z() > 360.f) angles_.setZ(angles_.z() - 360.f); buildTransform(); } @@ -250,7 +250,7 @@ void ObjectBase::setTransform(const Transform & t) { void ObjectBase::addChildren(ObjectBaseList & list, ObjectBase * where) { - foreach (ObjectBase * i, where->children_) { + foreach(ObjectBase * i, where->children_) { list << i; addChildren(list, i); } @@ -260,13 +260,13 @@ void ObjectBase::addChildren(ObjectBaseList & list, ObjectBase * where) { void ObjectBase::calculateBoundingBox() { bound = Box3D(); if (mesh_) { - bound = mesh_->boundingBox(); + bound = mesh_->boundingBox(); QVector c = bound.corners(), tc; - foreach (QVector3D p, c) + foreach(QVector3D p, c) tc << (itransform_ * QVector4D(p, 1)).toVector3D(); bound = Box3D(tc); } - foreach (ObjectBase * i, children_) { + foreach(ObjectBase * i, children_) { i->calculateBoundingBox(); bound |= i->boundingBox(); } @@ -300,10 +300,10 @@ void ObjectBase::removeProperty(const QString & pn) { void ObjectBase::setMatrix(const QMatrix4x4 & t) { - //raw_matrix = true; - //mat_ = t; - //pos_ = mat_.column(3).toVector3D(); - //mat_.setColumn(3, QVector4D(0., 0., 0., 1.)); + // raw_matrix = true; + // mat_ = t; + // pos_ = mat_.column(3).toVector3D(); + // mat_.setColumn(3, QVector4D(0., 0., 0., 1.)); raw_matrix = false; trans.setMatrix(t); buildTransform(); @@ -324,10 +324,12 @@ QVector3D ObjectBase::inParentSpace(const QVector3D & v) const { void ObjectBase::transferTransformToChildren(bool only_scale) { QMatrix4x4 m = trans.matrix(); if (only_scale) m = trans.matrixScale(); - foreach (ObjectBase * i, children_) + foreach(ObjectBase * i, children_) i->trans.setMatrix(m * i->trans.matrix()); - if (only_scale) resetScale(); - else setMatrix(QMatrix4x4()); + if (only_scale) + resetScale(); + else + setMatrix(QMatrix4x4()); } @@ -356,11 +358,9 @@ bool ObjectBase::isSelected(bool check_parents) const { void ObjectBase::setSelected(bool yes) { - //qDebug() << "select" << name() << view_; - if (select_) - selected_ = yes; - if (!selected_) - selected_aim = false; + // qDebug() << "select" << name() << view_; + if (select_) selected_ = yes; + if (!selected_) selected_aim = false; } @@ -377,7 +377,8 @@ ObjectBase * ObjectBase::selectedParent() const { void ObjectBase::setMaterial(Material * m, bool with_children) { material_ = m; if (with_children) - foreach (ObjectBase * i, children_) i->setMaterial(m, true); + foreach(ObjectBase * i, children_) + i->setMaterial(m, true); setObjectsChanged(); if (scene_) scene_->mat_changed = scene_->tree_changed = true; } @@ -386,14 +387,14 @@ void ObjectBase::setMaterial(Material * m, bool with_children) { void ObjectBase::setColor(QColor c, bool with_children) { color_ = c; if (with_children) - foreach (ObjectBase * i, children_) i->setColor(c, true); + foreach(ObjectBase * i, children_) + i->setColor(c, true); setObjectsChanged(); } void ObjectBase::setMesh(Mesh * v) { - if (scene_) - v = scene_->attachMesh(v); + if (scene_) v = scene_->attachMesh(v); mesh_ = v; setSceneTreeChanged(); setObjectsChanged(); @@ -404,16 +405,15 @@ void ObjectBase::buildTransform(bool force) { if (force) trans.setDirty(); itransform_.setToIdentity(); ObjectBase * p = parent_; - if (p) - itransform_ = p->itransform_; - //if (raw_matrix) { + if (p) itransform_ = p->itransform_; + // if (raw_matrix) { // itransform_.translate(pos_); // itransform_ *= mat_; // //qDebug() << "raw_matrix" << itransform_; - //} else - localTransform(itransform_); - //qDebug() << name_ << itransform_; - foreach (ObjectBase * i, children_) + // } else + localTransform(itransform_); + // qDebug() << name_ << itransform_; + foreach(ObjectBase * i, children_) i->buildTransform(force); setObjectsChanged(); } @@ -421,7 +421,8 @@ void ObjectBase::buildTransform(bool force) { void ObjectBase::initInternal() { init(); - foreach (ObjectBase * i, children_) i->initInternal(); + foreach(ObjectBase * i, children_) + i->initInternal(); } @@ -442,10 +443,10 @@ void ObjectBase::setSceneTreeChanged() { void ObjectBase::setObjectsChanged() { int p = pass(); if (mesh_) { - mesh_->setObjectsChanged (p, true); + mesh_->setObjectsChanged(p, true); mesh_->setSelectionChanged(p, true); if (prev_pass != p) { - mesh_->setObjectsChanged (prev_pass, true); + mesh_->setObjectsChanged(prev_pass, true); mesh_->setSelectionChanged(prev_pass, true); } } @@ -455,22 +456,20 @@ void ObjectBase::setObjectsChanged() { QMatrix4x4 ObjectBase::worldMatrix(QMatrix4x4 parent) const { QMatrix4x4 mat; - //mat.translate(pos_); - //if (raw_matrix) { + // mat.translate(pos_); + // if (raw_matrix) { // mat *= mat_; - //} else { + // } else { // if (angles_.z() != 0.f) mat.rotate(angles_.z(), 0., 0., 1.); // if (angles_.y() != 0.f) mat.rotate(angles_.y(), 0., 1., 0.); // if (angles_.x() != 0.f) mat.rotate(angles_.x(), 1., 0., 0.); // mat.scale(scale_); - //} + // } mat = trans.matrix(); return parent * mat; } - - AimedObject::AimedObject() { aim_dist = 1.; } @@ -487,8 +486,8 @@ void AimedObject::setAim(const QVector3D & p) { trans.setRotation(Transform::fromDirection(dir, trans.rotationY())); aim_dist = dir.length(); buildTransform(); - //if (!p.isNull()) - //qDebug() << "setAim" << p << aim() << worldAim(); + // if (!p.isNull()) + // qDebug() << "setAim" << p << aim() << worldAim(); } @@ -498,8 +497,8 @@ QVector3D AimedObject::direction() const { void AimedObject::setDirection(const QVector3D & d) { - //double len = qMax(aim_.length(), 0.001f); - //aim_ = d.normalized() * len; + // double len = qMax(aim_.length(), 0.001f); + // aim_ = d.normalized() * len; buildTransform(); } @@ -521,7 +520,7 @@ void AimedObject::flyFarer(double s) { void AimedObject::flyToDistance(double d) { move(direction() * (aim_dist - d)); aim_dist = d; - //qDebug() << d << (aim() - pos()).length() << aim(); + // qDebug() << d << (aim() - pos()).length() << aim(); } @@ -560,17 +559,13 @@ void AimedObject::orbitXY(const float & a) { } -void AimedObject::transformChanged() { - -} - - +void AimedObject::transformChanged() {} Light::Light(): AimedObject(), shadow_map(0, true, GL_R16F) { - type_ = glLight; - light_type = Omni; - intensity = 1.; + type_ = glLight; + light_type = Omni; + intensity = 1.; angle_start = angle_end = 180.; decay_linear = decay_quadratic = decay_start = 0.; decay_const = decay_end = 1.; @@ -579,10 +574,10 @@ Light::Light(): AimedObject(), shadow_map(0, true, GL_R16F) { Light::Light(const QVector3D & p, const QColor & c, float i): AimedObject(), shadow_map(0, true, GL_R16F) { - type_ = glLight; - light_type = Omni; - intensity = i; - color_ = c; + type_ = glLight; + light_type = Omni; + intensity = i; + color_ = c; angle_start = angle_end = 180.; decay_linear = decay_quadratic = decay_start = 0.; decay_const = decay_end = 1.; @@ -592,27 +587,27 @@ Light::Light(const QVector3D & p, const QColor & c, float i): AimedObject(), sha ObjectBase * Light::clone(bool withChildren) { - Light * o = new Light(*this); - //GLObjectBase::clone(withChildren); + Light * o = new Light(*this); + // GLObjectBase::clone(withChildren); o->is_init = false; - o->name_ = name_;// + "_copy"; - o->scene_ = nullptr; + o->name_ = name_; // + "_copy"; + o->scene_ = nullptr; o->children_.clear(); if (withChildren) { for (int i = 0; i < children_.size(); ++i) o->addChild(children_[i]->clone(withChildren)); } - o->color_ = color_; - o->light_type = light_type; - o->trans = trans; - o->aim_dist = aim_dist; - o->angle_start = angle_start; - o->angle_end = angle_end; - o->intensity = intensity; - o->decay_const = decay_const; - o->decay_linear = decay_linear; + o->color_ = color_; + o->light_type = light_type; + o->trans = trans; + o->aim_dist = aim_dist; + o->angle_start = angle_start; + o->angle_end = angle_end; + o->intensity = intensity; + o->decay_const = decay_const; + o->decay_linear = decay_linear; o->decay_quadratic = decay_quadratic; - o->meta = meta; + o->meta = meta; return o; } @@ -622,89 +617,180 @@ void Light::apply() { } -QDataStream & operator <<(QDataStream & s, const ObjectBase * p) { +QDataStream & operator<<(QDataStream & s, const ObjectBase * p) { ChunkStream cs; - //qDebug() << "place" << p->name() << "..."; - cs.add(1, int(p->type_)).add(2, p->accept_light).add(3, p->accept_fog).add(4, p->visible_) - .add(5, p->cast_shadow).add(6, p->rec_shadow).add(7, p->raw_matrix).add(8, p->line_width) - .add(9, int(p->render_mode)).add(14, p->mat_).add(16, p->children_.size()) - .add(17, p->name_).add(18, p->meta).add(19, p->color_).add(20, p->trans); - //qDebug() << "place self done"; + // qDebug() << "place" << p->name() << "..."; + cs.add(1, int(p->type_)) + .add(2, p->accept_light) + .add(3, p->accept_fog) + .add(4, p->visible_) + .add(5, p->cast_shadow) + .add(6, p->rec_shadow) + .add(7, p->raw_matrix) + .add(8, p->line_width) + .add(9, int(p->render_mode)) + .add(14, p->mat_) + .add(16, p->children_.size()) + .add(17, p->name_) + .add(18, p->meta) + .add(19, p->color_) + .add(20, p->trans); + // qDebug() << "place self done"; if (p->type_ == ObjectBase::glLight) { - //qDebug() << "place light ..."; - const Light * l = (const Light*)p; - cs.add(101, l->angle_start).add(102, l->angle_end).add(103, l->intensity) - .add(104, l->decay_const).add(105, l->decay_linear).add(106, l->decay_quadratic) - .add(107, l->decay_start).add(108, l->decay_end).add(109, int(l->light_type)) - .add(111, l->distance()); + // qDebug() << "place light ..."; + const Light * l = (const Light *)p; + cs.add(101, l->angle_start) + .add(102, l->angle_end) + .add(103, l->intensity) + .add(104, l->decay_const) + .add(105, l->decay_linear) + .add(106, l->decay_quadratic) + .add(107, l->decay_start) + .add(108, l->decay_end) + .add(109, int(l->light_type)) + .add(111, l->distance()); } if (p->type_ == ObjectBase::glCamera) { - //qDebug() << "place camera ..."; - const Camera * c = (const Camera*)p; - cs.add(200, c->aim()).add(201, c->fov_).add(202, c->depth_start) - .add(206, c->mirror_x).add(207, c->mirror_y).add(208, c->distance()) - .add(209, c->roll_); + // qDebug() << "place camera ..."; + const Camera * c = (const Camera *)p; + cs.add(200, c->aim()) + .add(201, c->fov_) + .add(202, c->depth_start) + .add(206, c->mirror_x) + .add(207, c->mirror_y) + .add(208, c->distance()) + .add(209, c->roll_); } - //qDebug() << "place" << p->name() << cs.data().size() << s.device()->size(); + // qDebug() << "place" << p->name() << cs.data().size() << s.device()->size(); s << cs.data(); - foreach (const ObjectBase * c, p->children_) + foreach(const ObjectBase * c, p->children_) s << c; return s; } -QDataStream & operator >>(QDataStream & s, ObjectBase *& p) { +QDataStream & operator>>(QDataStream & s, ObjectBase *& p) { ChunkStream cs(s); - p = nullptr; - int ccnt = 0; - Light * l = nullptr; + p = nullptr; + int ccnt = 0; + Light * l = nullptr; Camera * c = nullptr; - //qDebug() << "read obj ..."; + // qDebug() << "read obj ..."; while (!cs.atEnd()) { switch (cs.read()) { case 1: { ObjectBase::Type type = (ObjectBase::Type)cs.getData(); switch (type) { case ObjectBase::glMesh: p = new ObjectBase(); break; - case ObjectBase::glLight: p = new Light(); l = (Light*)p; break; - case ObjectBase::glCamera: p = new Camera(); c = (Camera*)p; break; - default : break; + case ObjectBase::glLight: + p = new Light(); + l = (Light *)p; + break; + case ObjectBase::glCamera: + p = new Camera(); + c = (Camera *)p; + break; + default: break; } if (p) p->type_ = type; - } break; - case 2: if (p) p->accept_light = cs.getData(); break; - case 3: if (p) p->accept_fog = cs.getData(); break; - case 4: if (p) p->visible_ = cs.getData(); break; - case 5: if (p) p->cast_shadow = cs.getData(); break; - case 6: if (p) p->rec_shadow = cs.getData(); break; - case 7: if (p) p->raw_matrix = cs.getData(); break; - case 8: if (p) p->line_width = cs.getData(); break; - case 9: if (p) p->render_mode = (ObjectBase::RenderMode)cs.getData(); break; - case 14: if (p) p->mat_ = cs.getData(); break; - case 16: if (p) ccnt = cs.getData(); break; - case 17: if (p) p->name_ = cs.getData(); break; - case 18: if (p) p->meta = cs.getData(); break; - case 19: if (p) p->color_ = cs.getData(); break; - case 20: if (p) p->trans = cs.getData(); break; - case 100: if (l) l->setAim(cs.getData()); break; - case 101: if (l) l->angle_start = cs.getData(); break; - case 102: if (l) l->angle_end = cs.getData(); break; - case 103: if (l) l->intensity = cs.getData(); break; - case 104: if (l) l->decay_const = cs.getData(); break; - case 105: if (l) l->decay_linear = cs.getData(); break; - case 106: if (l) l->decay_quadratic = cs.getData(); break; - case 107: if (l) l->decay_start = cs.getData(); break; - case 108: if (l) l->decay_end = cs.getData(); break; - case 109: if (l) l->light_type = (Light::Type)cs.getData(); break; - case 111: if (l) l->setDistance(cs.getData()); break; - case 200: if (c) c->setAim(cs.getData()); break; - case 201: if (c) c->setFOV(cs.getData()); break; - case 202: if (c) c->setDepthStart(cs.getData()); break; - case 206: if (c) c->mirror_x = cs.getData(); break; - case 207: if (c) c->mirror_y = cs.getData(); break; - case 208: if (c) c->setDistance(cs.getData()); break; - case 209: if (c) c->roll_ = cs.getData(); break; + } break; + case 2: + if (p) p->accept_light = cs.getData(); + break; + case 3: + if (p) p->accept_fog = cs.getData(); + break; + case 4: + if (p) p->visible_ = cs.getData(); + break; + case 5: + if (p) p->cast_shadow = cs.getData(); + break; + case 6: + if (p) p->rec_shadow = cs.getData(); + break; + case 7: + if (p) p->raw_matrix = cs.getData(); + break; + case 8: + if (p) p->line_width = cs.getData(); + break; + case 9: + if (p) p->render_mode = (ObjectBase::RenderMode)cs.getData(); + break; + case 14: + if (p) p->mat_ = cs.getData(); + break; + case 16: + if (p) ccnt = cs.getData(); + break; + case 17: + if (p) p->name_ = cs.getData(); + break; + case 18: + if (p) p->meta = cs.getData(); + break; + case 19: + if (p) p->color_ = cs.getData(); + break; + case 20: + if (p) p->trans = cs.getData(); + break; + case 100: + if (l) l->setAim(cs.getData()); + break; + case 101: + if (l) l->angle_start = cs.getData(); + break; + case 102: + if (l) l->angle_end = cs.getData(); + break; + case 103: + if (l) l->intensity = cs.getData(); + break; + case 104: + if (l) l->decay_const = cs.getData(); + break; + case 105: + if (l) l->decay_linear = cs.getData(); + break; + case 106: + if (l) l->decay_quadratic = cs.getData(); + break; + case 107: + if (l) l->decay_start = cs.getData(); + break; + case 108: + if (l) l->decay_end = cs.getData(); + break; + case 109: + if (l) l->light_type = (Light::Type)cs.getData(); + break; + case 111: + if (l) l->setDistance(cs.getData()); + break; + case 200: + if (c) c->setAim(cs.getData()); + break; + case 201: + if (c) c->setFOV(cs.getData()); + break; + case 202: + if (c) c->setDepthStart(cs.getData()); + break; + case 206: + if (c) c->mirror_x = cs.getData(); + break; + case 207: + if (c) c->mirror_y = cs.getData(); + break; + case 208: + if (c) c->setDistance(cs.getData()); + break; + case 209: + if (c) c->roll_ = cs.getData(); + break; } } - //qDebug() << p->name() << ccnt; + // qDebug() << p->name() << ccnt; for (int i = 0; i < ccnt; ++i) { ObjectBase * c = nullptr; s >> c; diff --git a/globject.h b/globject.h index a1fe541..23c42ad 100644 --- a/globject.h +++ b/globject.h @@ -1,19 +1,19 @@ /* - QGL ObjectBase & Light - Ivan Pelipenko peri4ko@yandex.ru + QGL ObjectBase & Light + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLOBJECT_H @@ -21,20 +21,30 @@ #include "glframebuffer.h" #include "glmaterial.h" -#include "gltypes.h" #include "gltransform.h" +#include "gltypes.h" -class ObjectBase -{ +class ObjectBase { friend class Scene; friend class RendererSelection; - friend QDataStream & operator <<(QDataStream & s, const ObjectBase * p); - friend QDataStream & operator >>(QDataStream & s, ObjectBase *& p); - friend QDataStream & operator >>(QDataStream & s, Scene *& p); + friend QDataStream & operator<<(QDataStream & s, const ObjectBase * p); + friend QDataStream & operator>>(QDataStream & s, ObjectBase *& p); + friend QDataStream & operator>>(QDataStream & s, Scene *& p); + public: - enum Type {glMesh, glLight, glCamera, glParticlesSystem}; - enum RenderMode {View = 0, Point = GL_POINT, Line = GL_LINE, Fill = GL_FILL}; + enum Type { + glMesh, + glLight, + glCamera, + glParticlesSystem + }; + enum RenderMode { + View = 0, + Point = GL_POINT, + Line = GL_LINE, + Fill = GL_FILL + }; explicit ObjectBase(Mesh * geom = 0, Material * mat = 0); virtual ~ObjectBase(); @@ -42,32 +52,32 @@ public: virtual ObjectBase * clone(bool withChildren = true); void destroy(); - QString name() const {return name_;} - void setName(const QString & name) {name_ = name;} + QString name() const { return name_; } + void setName(const QString & name) { name_ = name; } virtual void init(); virtual void update() {} - bool isInit() const {return is_init;} - Type type() const {return type_;} + bool isInit() const { return is_init; } + Type type() const { return type_; } RenderPass pass() const; - uint id() const {return id_;} + uint id() const { return id_; } - RenderMode renderMode() const {return render_mode;} - void setRenderMode(RenderMode mode) {render_mode = mode;} + RenderMode renderMode() const { return render_mode; } + void setRenderMode(RenderMode mode) { render_mode = mode; } - float lineWidth() const {return line_width;} - void setLineWidth(const float & width) {line_width = width;} + float lineWidth() const { return line_width; } + void setLineWidth(const float & width) { line_width = width; } - ObjectBase * parent() {return parent_;} - void setParent(ObjectBase * o) {parent_ = o;} - bool hasParent() const {return parent_ != nullptr;} - bool hasChildren() const {return !children_.isEmpty();} + ObjectBase * parent() { return parent_; } + void setParent(ObjectBase * o) { parent_ = o; } + bool hasParent() const { return parent_ != nullptr; } + bool hasChildren() const { return !children_.isEmpty(); } void setScene(Scene * v); void addChild(ObjectBase * o); void removeChild(ObjectBase * o); void removeChild(int index); void clearChildren(bool deleteAll = false); - int childCount() const {return children_.size();} + int childCount() const { return children_.size(); } ObjectBase * child(int index); ObjectBase * child(const QString & name); const ObjectBase * child(int index) const; @@ -75,109 +85,224 @@ public: ObjectBaseList children(bool all_ = false); bool isVisible(bool check_parents = false) const; - bool isHidden(bool check_parents = false) const {return !isVisible(check_parents);} + bool isHidden(bool check_parents = false) const { return !isVisible(check_parents); } void setVisible(bool v); - void setHidden(bool v) {setVisible(!v);} - void show() {setVisible(true);} - void hide() {setVisible(false);} + void setHidden(bool v) { setVisible(!v); } + void show() { setVisible(true); } + void hide() { setVisible(false); } - bool isReceiveShadows() const {return rec_shadow;} - bool isCastShadows() const {return cast_shadow;} - void setReceiveShadows(bool on) {rec_shadow = on;} - void setCastShadows(bool on) {cast_shadow = on;} + bool isReceiveShadows() const { return rec_shadow; } + bool isCastShadows() const { return cast_shadow; } + void setReceiveShadows(bool on) { rec_shadow = on; } + void setCastShadows(bool on) { cast_shadow = on; } - void move(const QVector3D & dv) {trans.setTranslation(pos() + dv); buildTransform();} - void moveTo(const QVector3D & dv) {trans.setTranslation(dv); buildTransform();} - void move(GLfloat dx, GLfloat dy, GLfloat dz = 0.) {move(QVector3D(dx, dy, dz)); buildTransform();} - void moveTo(GLfloat dx, GLfloat dy, GLfloat dz = 0.) {moveTo(QVector3D(dx, dy, dz)); buildTransform();} - void moveX(GLfloat o) {trans.setTranslationX(posX() + o); buildTransform();} - void moveY(GLfloat o) {trans.setTranslationY(posY() + o); buildTransform();} - void moveZ(GLfloat o) {trans.setTranslationZ(posZ() + o); buildTransform();} - void setPosX(GLfloat o) {trans.setTranslationX(o); buildTransform();} - void setPosY(GLfloat o) {trans.setTranslationY(o); buildTransform();} - void setPosZ(GLfloat o) {trans.setTranslationZ(o); buildTransform();} - void setPos(GLfloat x, GLfloat y, GLfloat z) {trans.setTranslation(QVector3D(x, y, z)); buildTransform();} - void setPos(const QVector3D & p) {trans.setTranslation(p); buildTransform();} - void resetPos() {trans.setTranslation(QVector3D()); buildTransform();} + void move(const QVector3D & dv) { + trans.setTranslation(pos() + dv); + buildTransform(); + } + void moveTo(const QVector3D & dv) { + trans.setTranslation(dv); + buildTransform(); + } + void move(GLfloat dx, GLfloat dy, GLfloat dz = 0.) { + move(QVector3D(dx, dy, dz)); + buildTransform(); + } + void moveTo(GLfloat dx, GLfloat dy, GLfloat dz = 0.) { + moveTo(QVector3D(dx, dy, dz)); + buildTransform(); + } + void moveX(GLfloat o) { + trans.setTranslationX(posX() + o); + buildTransform(); + } + void moveY(GLfloat o) { + trans.setTranslationY(posY() + o); + buildTransform(); + } + void moveZ(GLfloat o) { + trans.setTranslationZ(posZ() + o); + buildTransform(); + } + void setPosX(GLfloat o) { + trans.setTranslationX(o); + buildTransform(); + } + void setPosY(GLfloat o) { + trans.setTranslationY(o); + buildTransform(); + } + void setPosZ(GLfloat o) { + trans.setTranslationZ(o); + buildTransform(); + } + void setPos(GLfloat x, GLfloat y, GLfloat z) { + trans.setTranslation(QVector3D(x, y, z)); + buildTransform(); + } + void setPos(const QVector3D & p) { + trans.setTranslation(p); + buildTransform(); + } + void resetPos() { + trans.setTranslation(QVector3D()); + buildTransform(); + } - QVector3D pos() const {return trans.translation();} - float posX() const {return trans.translation().x();} - float posY() const {return trans.translation().y();} - float posZ() const {return trans.translation().z();} - QVector3D worldPos() const {return (itransform_ * QVector4D(0, 0, 0, 1.)).toVector3D();} - QMatrix4x4 worldTransform() const {return itransform_;} + QVector3D pos() const { return trans.translation(); } + float posX() const { return trans.translation().x(); } + float posY() const { return trans.translation().y(); } + float posZ() const { return trans.translation().z(); } + QVector3D worldPos() const { return (itransform_ * QVector4D(0, 0, 0, 1.)).toVector3D(); } + QMatrix4x4 worldTransform() const { return itransform_; } - QVector3D rotation() const {return trans.rotation();} - float rotationX() const {return rotation().x();} - float rotationY() const {return rotation().y();} - float rotationZ() const {return rotation().z();} - void rotateX(GLfloat a) {raw_matrix = false; trans.setRotationX(trans.rotationX() + a); buildTransform();} - void rotateY(GLfloat a) {raw_matrix = false; trans.setRotationY(trans.rotationY() + a); buildTransform();} + QVector3D rotation() const { return trans.rotation(); } + float rotationX() const { return rotation().x(); } + float rotationY() const { return rotation().y(); } + float rotationZ() const { return rotation().z(); } + void rotateX(GLfloat a) { + raw_matrix = false; + trans.setRotationX(trans.rotationX() + a); + buildTransform(); + } + void rotateY(GLfloat a) { + raw_matrix = false; + trans.setRotationY(trans.rotationY() + a); + buildTransform(); + } void rotateZ(GLfloat a); - void setRotationX(GLfloat a) {raw_matrix = false; trans.setRotationX(a); buildTransform();} - void setRotationY(GLfloat a) {raw_matrix = false; trans.setRotationY(a); buildTransform();} + void setRotationX(GLfloat a) { + raw_matrix = false; + trans.setRotationX(a); + buildTransform(); + } + void setRotationY(GLfloat a) { + raw_matrix = false; + trans.setRotationY(a); + buildTransform(); + } void setRotationZ(GLfloat a); - void setRotation(const QVector3D & a) {raw_matrix = false; trans.setRotation(a); buildTransform();} - void resetRotation() {raw_matrix = false; trans.setRotation(QVector3D()); buildTransform();} + void setRotation(const QVector3D & a) { + raw_matrix = false; + trans.setRotation(a); + buildTransform(); + } + void resetRotation() { + raw_matrix = false; + trans.setRotation(QVector3D()); + buildTransform(); + } - QVector3D scale() {return trans.scale3D();} - float scaleX() {return trans.scale3D().x();} - float scaleY() {return trans.scale3D().y();} - float scaleZ() {return trans.scale3D().z();} - void scale(const QVector3D & sv) {raw_matrix = false; trans.setScale(trans.scale3D() * sv); buildTransform();} - void scale(GLfloat sx, GLfloat sy, GLfloat sz) {raw_matrix = false; scale(QVector3D(sx, sy, sz)); buildTransform();} - void scale(GLfloat sx, GLfloat sy) {raw_matrix = false; scale(QVector3D(sx, sy, sy)); buildTransform();} - void scale(GLfloat sx) {raw_matrix = false; scale(QVector3D(sx, sx, sx)); buildTransform();} - void scaleX(GLfloat a) {raw_matrix = false; trans.setScaleX(trans.scale3D().x() + a); buildTransform();} - void scaleY(GLfloat a) {raw_matrix = false; trans.setScaleY(trans.scale3D().y() + a); buildTransform();} - void scaleZ(GLfloat a) {raw_matrix = false; trans.setScaleZ(trans.scale3D().z() + a); buildTransform();} - void setScale(const QVector3D & a) {raw_matrix = false; trans.setScale(a); buildTransform();} - void setScale(GLfloat a) {raw_matrix = false; trans.setScale(a); buildTransform();} - void setScaleX(GLfloat a) {raw_matrix = false; trans.setScaleX(a); buildTransform();} - void setScaleY(GLfloat a) {raw_matrix = false; trans.setScaleY(a); buildTransform();} - void setScaleZ(GLfloat a) {raw_matrix = false; trans.setScaleZ(a); buildTransform();} - void resetScale() {raw_matrix = false; trans.setScale(1.f); buildTransform();} - - Transform transform() {return trans;} + QVector3D scale() { return trans.scale3D(); } + float scaleX() { return trans.scale3D().x(); } + float scaleY() { return trans.scale3D().y(); } + float scaleZ() { return trans.scale3D().z(); } + void scale(const QVector3D & sv) { + raw_matrix = false; + trans.setScale(trans.scale3D() * sv); + buildTransform(); + } + void scale(GLfloat sx, GLfloat sy, GLfloat sz) { + raw_matrix = false; + scale(QVector3D(sx, sy, sz)); + buildTransform(); + } + void scale(GLfloat sx, GLfloat sy) { + raw_matrix = false; + scale(QVector3D(sx, sy, sy)); + buildTransform(); + } + void scale(GLfloat sx) { + raw_matrix = false; + scale(QVector3D(sx, sx, sx)); + buildTransform(); + } + void scaleX(GLfloat a) { + raw_matrix = false; + trans.setScaleX(trans.scale3D().x() + a); + buildTransform(); + } + void scaleY(GLfloat a) { + raw_matrix = false; + trans.setScaleY(trans.scale3D().y() + a); + buildTransform(); + } + void scaleZ(GLfloat a) { + raw_matrix = false; + trans.setScaleZ(trans.scale3D().z() + a); + buildTransform(); + } + void setScale(const QVector3D & a) { + raw_matrix = false; + trans.setScale(a); + buildTransform(); + } + void setScale(GLfloat a) { + raw_matrix = false; + trans.setScale(a); + buildTransform(); + } + void setScaleX(GLfloat a) { + raw_matrix = false; + trans.setScaleX(a); + buildTransform(); + } + void setScaleY(GLfloat a) { + raw_matrix = false; + trans.setScaleY(a); + buildTransform(); + } + void setScaleZ(GLfloat a) { + raw_matrix = false; + trans.setScaleZ(a); + buildTransform(); + } + void resetScale() { + raw_matrix = false; + trans.setScale(1.f); + buildTransform(); + } + + Transform transform() { return trans; } void setTransform(const Transform & t); void setMatrix(const QMatrix4x4 & t); QMatrix4x4 matrix() const; - bool isRawMatrix() {return raw_matrix;} + bool isRawMatrix() { return raw_matrix; } QVector3D inParentSpace(const QVector3D & v) const; void transferTransformToChildren(bool only_scale = false); void cleanTree(); - bool isAcceptLight() const {return accept_light;} - void setAcceptLight(bool yes) {accept_light = yes;} + bool isAcceptLight() const { return accept_light; } + void setAcceptLight(bool yes) { accept_light = yes; } - bool isAcceptFog() const {return accept_fog;} - void setAcceptFog(bool yes) {accept_fog = yes;} + bool isAcceptFog() const { return accept_fog; } + void setAcceptFog(bool yes) { accept_fog = yes; } bool isSelected(bool check_parents = false) const; void setSelected(bool yes); - void select() {setSelected(true);} - void deselect() {setSelected(false);} + void select() { setSelected(true); } + void deselect() { setSelected(false); } ObjectBase * selectedParent() const; - void setAimSelected(bool yes) {selected_aim = yes;} - bool isAimSelected() const {return selected_aim;} + void setAimSelected(bool yes) { selected_aim = yes; } + bool isAimSelected() const { return selected_aim; } - bool isSelectable() const {return select_;} - void setSelectable(bool yes) {select_ = yes;} + bool isSelectable() const { return select_; } + void setSelectable(bool yes) { select_ = yes; } - GLenum srcAlpha() const {return blend_src;} - GLenum destAlpha() const {return blend_dest;} - void setSrcAlpha(GLenum mode) {blend_src = mode;} - void setDestAlpha(GLenum mode) {blend_dest = mode;} + GLenum srcAlpha() const { return blend_src; } + GLenum destAlpha() const { return blend_dest; } + void setSrcAlpha(GLenum mode) { blend_src = mode; } + void setDestAlpha(GLenum mode) { blend_dest = mode; } void setMaterial(Material * m, bool with_children = false); - Material * material() {return material_;} + Material * material() { return material_; } void setColor(QColor c, bool with_children = false); - QColor color() {return color_;} + QColor color() { return color_; } - const Box3D & boundingBox() const {return bound;} + const Box3D & boundingBox() const { return bound; } void setMesh(Mesh * v); - Mesh * mesh() {return mesh_;} + Mesh * mesh() { return mesh_; } void calculateBoundingBox(); void updateTransform(); @@ -220,7 +345,9 @@ protected: QVariantMap meta; }; -inline bool operator <(const ObjectBase & f, const ObjectBase & s) {return f.pos_h.z() < s.pos_h.z();} +inline bool operator<(const ObjectBase & f, const ObjectBase & s) { + return f.pos_h.z() < s.pos_h.z(); +} class AimedObject: public ObjectBase { @@ -228,31 +355,32 @@ class AimedObject: public ObjectBase { friend class GLRendererBase; friend class Light; friend class Camera; + public: AimedObject(); ~AimedObject() {} - QVector3D aim() const {return pos() + (direction() * aim_dist);} + QVector3D aim() const { return pos() + (direction() * aim_dist); } QVector3D worldAim() const; void setAim(const QVector3D & p); QVector3D direction() const; - QVector3D worldDirection() const {return (itransform_ * QVector4D(QVector3D(0,0,-1), 0.)).toVector3D().normalized();} + QVector3D worldDirection() const { return (itransform_ * QVector4D(QVector3D(0, 0, -1), 0.)).toVector3D().normalized(); } void setDirection(const QVector3D & d); - void setDirection(double x, double y, double z) {setDirection(QVector3D(x, y, z));} + void setDirection(double x, double y, double z) { setDirection(QVector3D(x, y, z)); } - double distance() const {return aim_dist;} - void setDistance(double d) {aim_dist = d;} + double distance() const { return aim_dist; } + void setDistance(double d) { aim_dist = d; } void flyCloser(double s); void flyFarer(double s); void flyToDistance(double d); void moveForward(const float & x, bool withZ = true); - void moveBackward(const float & x, bool withZ = true) {moveForward(-x, withZ);} + void moveBackward(const float & x, bool withZ = true) { moveForward(-x, withZ); } void moveLeft(const float & x, bool withZ = true); - void moveRight(const float & x, bool withZ = true) {moveLeft(-x, withZ);} + void moveRight(const float & x, bool withZ = true) { moveLeft(-x, withZ); } void moveUp(const float & x, bool onlyZ = false); - void moveDown(const float & x, bool onlyZ = false) {moveUp(-x, onlyZ);} + void moveDown(const float & x, bool onlyZ = false) { moveUp(-x, onlyZ); } - void rotateRoll(const float & a) {rotateY(a);} + void rotateRoll(const float & a) { rotateY(a); } void orbitZ(const float & a); void orbitXY(const float & a); @@ -265,13 +393,21 @@ protected: class Light: public AimedObject { friend class QGLView; friend class RendererBase; + public: - enum Type {Omni, Cone, Directional}; + enum Type { + Omni, + Cone, + Directional + }; Light(); Light(const QVector3D & p, const QColor & c = Qt::white, float i = 1.); virtual ObjectBase * clone(bool withChildren = true); - virtual void init() {shadow_map.resize(512, 512); is_init = true;} + virtual void init() { + shadow_map.resize(512, 512); + is_init = true; + } void apply(); float angle_start; @@ -287,17 +423,31 @@ public: QMatrix4x4 shadow_matrix; }; -template -inline T globject_cast(ObjectBase * object) {return reinterpret_cast(object);} +template +inline T globject_cast(ObjectBase * object) { + return reinterpret_cast(object); +} -template -inline T globject_cast(const ObjectBase * object) {return reinterpret_cast(object);} +template +inline T globject_cast(const ObjectBase * object) { + return reinterpret_cast(object); +} -QDataStream & operator <<(QDataStream & s, const ObjectBase * p); -QDataStream & operator >>(QDataStream & s, ObjectBase *& p); +QDataStream & operator<<(QDataStream & s, const ObjectBase * p); +QDataStream & operator>>(QDataStream & s, ObjectBase *& p); -inline ObjectBaseList lights2objectList(const QList & v) {ObjectBaseList ret; foreach (Light*i, v) ret << (ObjectBase*)i; return ret;} -inline ObjectBaseList cameras2objectList(const QList & v) {ObjectBaseList ret; foreach (Camera*i, v) ret << (ObjectBase*)i; return ret;} +inline ObjectBaseList lights2objectList(const QList & v) { + ObjectBaseList ret; + foreach(Light * i, v) + ret << (ObjectBase *)i; + return ret; +} +inline ObjectBaseList cameras2objectList(const QList & v) { + ObjectBaseList ret; + foreach(Camera * i, v) + ret << (ObjectBase *)i; + return ret; +} #endif // GLOBJECT_H diff --git a/glscene.cpp b/glscene.cpp index 3d95e64..8d94939 100644 --- a/glscene.cpp +++ b/glscene.cpp @@ -1,25 +1,27 @@ /* - QGL Scene - Ivan Pelipenko peri4ko@yandex.ru + QGL Scene + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "glscene.h" + #include "glcamera.h" #include "glmesh.h" #include "qglview.h" + #include @@ -27,9 +29,9 @@ Scene::Scene() { root_ = new ObjectBase(); root_->setScene(this); tree_changed = mat_changed = lights_changed = true; - destroying = false; + destroying = false; need_reload_materials = tree_struct_changed = true; - sel_mode_ = smSingleSelection; + sel_mode_ = smSingleSelection; } @@ -41,9 +43,9 @@ Scene::~Scene() { Scene * Scene::clone() { - Scene * ret = new Scene(); + Scene * ret = new Scene(); ObjectBase * o = root_->clone(); - foreach (ObjectBase * co, o->children()) + foreach(ObjectBase * co, o->children()) ret->addObject(co); o->clearChildren(); delete o; @@ -54,7 +56,7 @@ Scene * Scene::clone() { void Scene::addObject(ObjectBase * o) { ObjectBaseList aol = o->children(true); attachObject(o); - foreach (ObjectBase * c, aol) + foreach(ObjectBase * c, aol) attachObject(c); root_->addChild(o); tree_changed = tree_struct_changed = true; @@ -63,13 +65,13 @@ void Scene::addObject(ObjectBase * o) { void Scene::addScene(const Scene * s) { if (!s) return; - //qDebug() << "addScene clone ..."; + // qDebug() << "addScene clone ..."; ObjectBase * o = s->root_->clone(); o->setName(s->name()); - //qDebug() << "addScene clone ok" << o << o->children(true).size(); + // qDebug() << "addScene clone ok" << o << o->children(true).size(); addObject(o); makeMaterialsUniqueNames(); - //qDebug() << "addScene add ok" << o; + // qDebug() << "addScene add ok" << o; } @@ -77,16 +79,16 @@ void Scene::assignFrom(const Scene * s) { clear(); if (!s) return; setName(s->name()); - foreach (Material * m, s->materials) { + foreach(Material * m, s->materials) { Material * nm = new Material(); - *nm = *m; - nm->_changed = true; + *nm = *m; + nm->_changed = true; nm->setMapsChanged(); materials << nm; } for (int i = 0; i < s->root_->childCount(); ++i) { addObject(s->root_->child(i)->clone()); - //qDebug() << i << o->child(i)->pos(); + // qDebug() << i << o->child(i)->pos(); } tree_changed = mat_changed = lights_changed = need_reload_materials = tree_struct_changed = true; } @@ -113,14 +115,14 @@ void Scene::reinitAll() { i->reinit(); setTreeChanged(); mat_changed = lights_changed = true; - need_reload_materials = true; - tree_struct_changed = true; + need_reload_materials = true; + tree_struct_changed = true; } void Scene::objectsCountInternal(int * cnt, ObjectBase * where) { ++(*cnt); - foreach (ObjectBase * i, where->children()) + foreach(ObjectBase * i, where->children()) objectsCountInternal(cnt, i); } int Scene::objectsCount(bool all) { @@ -133,7 +135,7 @@ int Scene::objectsCount(bool all) { void Scene::removeObjectInternal(ObjectBase * o, ObjectBase * where) { if (destroying) return; - foreach (ObjectBase * i, where->children()) { + foreach(ObjectBase * i, where->children()) { if (o == i) { where->removeChild(i); setObjectMeshChanged(i); @@ -145,30 +147,29 @@ void Scene::removeObjectInternal(ObjectBase * o, ObjectBase * where) { void Scene::emitSelectionChanged() { selected_top.clear(); - foreach (ObjectBase * o, selected_) { + foreach(ObjectBase * o, selected_) { ObjectBase * po = o->selectedParent(); if (!po) po = o; - if (!selected_top.contains(po)) - selected_top << po; + if (!selected_top.contains(po)) selected_top << po; } - foreach (Mesh * m, geometries) + foreach(Mesh * m, geometries) m->setAllSelectionChanged(true); selectionChanged(); } QString Scene::uniqueName(QString n, const QSet & names) { - if (!names.contains(n)) - return n; + if (!names.contains(n)) return n; QString num; while (!n.isEmpty()) { if (n.right(1)[0].isDigit()) { num.push_front(n.right(1)); n.chop(1); - } else break; + } else + break; } if (!n.endsWith('_')) n += '_'; - int in = num.toInt() + 1; + int in = num.toInt() + 1; QString nn = n + QString::number(in).rightJustified(3, '0'); while (names.contains(nn)) nn = n + QString::number(++in).rightJustified(3, '0'); @@ -176,7 +177,6 @@ QString Scene::uniqueName(QString n, const QSet & names) { } - void Scene::removeObject(ObjectBase * o, bool inChildren) { if (destroying) return; o->setScene(nullptr); @@ -205,11 +205,13 @@ void Scene::clearObjects(bool deleteAll) { void Scene::selectObject(ObjectBase * o, bool add_to_selection) { - //qDebug() << "selectObject" << o << add_to_selection; + // qDebug() << "selectObject" << o << add_to_selection; if (!add_to_selection || (sel_mode_ == smSingleSelection)) clearSelection(); if (o) { - if (!add_to_selection) o->setSelected(true); - else o->setSelected(!o->isSelected()); + if (!add_to_selection) + o->setSelected(true); + else + o->setSelected(!o->isSelected()); gatherSelection(); } emitSelectionChanged(); @@ -218,7 +220,7 @@ void Scene::selectObject(ObjectBase * o, bool add_to_selection) { void Scene::selectObjects(ObjectBaseList ol, bool add_to_selection) { if (!add_to_selection || (sel_mode_ == smSingleSelection)) clearSelection(); - foreach (ObjectBase * o, ol) { + foreach(ObjectBase * o, ol) { if (!o) continue; o->setSelected(true); } @@ -229,14 +231,12 @@ void Scene::selectObjects(ObjectBaseList ol, bool add_to_selection) { void Scene::selectObjectsByMesh() { ObjectBaseList csl = selected_; - QSet sml; - foreach (ObjectBase * o, csl) - if (o->mesh()) - sml << o->mesh(); + QSet sml; + foreach(ObjectBase * o, csl) + if (o->mesh()) sml << o->mesh(); ObjectBaseList ol = root_->children(true); - foreach (ObjectBase * o, ol) { - if (sml.contains(o->mesh())) - o->setSelected(true); + foreach(ObjectBase * o, ol) { + if (sml.contains(o->mesh())) o->setSelected(true); } gatherSelection(); emitSelectionChanged(); @@ -245,14 +245,12 @@ void Scene::selectObjectsByMesh() { void Scene::selectObjectsByMaterial() { ObjectBaseList csl = selected_; - QSet sml; - foreach (ObjectBase * o, csl) - if (o->material()) - sml << o->material(); + QSet sml; + foreach(ObjectBase * o, csl) + if (o->material()) sml << o->material(); ObjectBaseList ol = root_->children(true); - foreach (ObjectBase * o, ol) { - if (sml.contains(o->material())) - o->setSelected(true); + foreach(ObjectBase * o, ol) { + if (sml.contains(o->material())) o->setSelected(true); } gatherSelection(); emitSelectionChanged(); @@ -263,7 +261,7 @@ void Scene::clearSelection() { selected_.clear(); selected_top.clear(); ObjectBaseList ol = root_->children(true); - foreach (ObjectBase * o, ol) { + foreach(ObjectBase * o, ol) { o->setSelected(false); o->setAimSelected(false); } @@ -282,13 +280,13 @@ ObjectBase * Scene::selectedObject() const { } -void gatherMeshes(ObjectBase * o, QSet & ums) { +void gatherMeshes(ObjectBase * o, QSet & ums) { if (o->mesh()) ums << o->mesh(); for (int i = 0; i < o->childCount(); ++i) gatherMeshes(o->child(i), ums); } void Scene::cleanUnused() { - QSet ums; + QSet ums; gatherMeshes(root_, ums); for (int i = 0; i < geometries.size(); ++i) { if (ums.contains(geometries[i])) continue; @@ -316,9 +314,8 @@ Material * Scene::newMaterial(const QString & name) { void Scene::removeMaterial(Material * m) { if (!m || !materials.contains(m)) return; ObjectBaseList ol = root_->children(true); - foreach (ObjectBase * o, ol) - if (o->material_ == m) - o->setMaterial(0); + foreach(ObjectBase * o, ol) + if (o->material_ == m) o->setMaterial(0); materials.removeAll(m); changed_materials.removeAll(m); mat_changed = true; @@ -327,7 +324,7 @@ void Scene::removeMaterial(Material * m) { void Scene::makeMaterialsUniqueNames() { QSet names; - foreach (Material * m, materials) { + foreach(Material * m, materials) { if (m->name.isEmpty()) m->name = "default_000"; m->name = uniqueName(m->name, names); names << m->name; @@ -355,9 +352,8 @@ void Scene::dump() { void Scene::gatherSelection() { selected_.clear(); ObjectBaseList ol = root_->children(true); - foreach (ObjectBase * o, ol) - if (o->isSelected()) - selected_ << o; + foreach(ObjectBase * o, ol) + if (o->isSelected()) selected_ << o; } @@ -369,9 +365,9 @@ void Scene::attachObject(ObjectBase * o) { setObjectMeshChanged(o); } if (o->material()) { // search suitable material in this scene - uint ohash = o->material()->hash(); + uint ohash = o->material()->hash(); bool need_new = true; - foreach (Material * m, materials) { + foreach(Material * m, materials) { if (m == o->material()) { // already exists by ptr need_new = false; break; @@ -384,7 +380,7 @@ void Scene::attachObject(ObjectBase * o) { } if (need_new) { // need to clone material and add to scene Material * nmat = new Material(); - *nmat = *(o->material()); + *nmat = *(o->material()); nmat->setMapsChanged(); o->setMaterial(nmat); materials << nmat; @@ -397,7 +393,7 @@ void Scene::attachObject(ObjectBase * o) { Mesh * Scene::attachMesh(Mesh * mesh) { if (!mesh) return 0; uint mhash = mesh->hash(); - foreach (Mesh * m, geometries) { + foreach(Mesh * m, geometries) { if (m == mesh) { // already exists by ptr return m; } @@ -415,7 +411,7 @@ Mesh * Scene::attachMesh(Mesh * mesh) { void Scene::setTreeChanged() { if (destroying) return; tree_changed = true; - foreach (Mesh * m, geometries) { + foreach(Mesh * m, geometries) { m->setAllObjectsChanged(true); m->setAllSelectionChanged(true); } @@ -434,22 +430,20 @@ void Scene::setObjectMeshChanged(ObjectBase * o) { void Scene::prepareTree(ObjectBase * o) { - foreach (ObjectBase * co, o->children_) { + foreach(ObjectBase * co, o->children_) { if (co->isHidden()) continue; switch (co->type_) { case ObjectBase::glLight: { - Light * l = globject_cast(co); + Light * l = globject_cast(co); lights_used[l->light_type] << l; - } break; + } break; case ObjectBase::glMesh: if (co->mesh()) { geometries_used[co->pass()][co->mesh()] << co; co->setObjectsChanged(); } break; - case ObjectBase::glCamera: - cameras_used << globject_cast(co); - break; + case ObjectBase::glCamera: cameras_used << globject_cast(co); break; default: break; } prepareTree(co); @@ -459,7 +453,7 @@ void Scene::prepareTree(ObjectBase * o) { bool Scene::prepare() { changed_materials.clear(); - foreach (Material * m, materials) { + foreach(Material * m, materials) { if (m->_changed) { need_reload_materials = tree_changed = true; changed_materials << m; @@ -470,7 +464,7 @@ bool Scene::prepare() { if (!tree_changed && !mat_changed) return false; aol = root_->children(true); if (tree_changed) { - geometries_used[rpSolid ].clear(); + geometries_used[rpSolid].clear(); geometries_used[rpTransparent].clear(); lights_used.clear(); cameras_used.clear(); @@ -480,7 +474,7 @@ bool Scene::prepare() { cleanUnused(); QMetaObject::invokeMethod(this, "treeChanged", Qt::QueuedConnection); } - tree_changed = false; + tree_changed = false; lights_changed = true; } mat_changed = false; @@ -489,36 +483,35 @@ bool Scene::prepare() { void Scene::destroy(QOpenGLExtraFunctions * f) { - foreach (Mesh * g, geometries) + foreach(Mesh * g, geometries) g->destroy(f); } void Scene::destroyUnused(QOpenGLExtraFunctions * f) { - foreach (Mesh * i, td_geometries) + foreach(Mesh * i, td_geometries) i->destroy(f); qDeleteAll(td_geometries); td_geometries.clear(); } -QDataStream & operator <<(QDataStream & s, const Scene * p) { +QDataStream & operator<<(QDataStream & s, const Scene * p) { ChunkStream cs; - //qDebug() << "place" << p->name() << "..."; + // qDebug() << "place" << p->name() << "..."; QVector geom_ind, mat_ind; ObjectBaseList cl = p->root_->children(true); geom_ind.reserve(cl.size()); mat_ind.reserve(cl.size()); - foreach (ObjectBase * c, cl) { + foreach(ObjectBase * c, cl) { geom_ind << p->geometries.indexOf(c->mesh()); mat_ind << p->materials.indexOf(c->material()); } - cs.add(1, p->name_).add(10, p->geometries).add(11, p->materials) - .add(20, p->root_).add(21, geom_ind).add(22, mat_ind); + cs.add(1, p->name_).add(10, p->geometries).add(11, p->materials).add(20, p->root_).add(21, geom_ind).add(22, mat_ind); s << qCompress(cs.data()); return s; } -QDataStream & operator >>(QDataStream & s, Scene *& p) { +QDataStream & operator>>(QDataStream & s, Scene *& p) { p = new Scene(); QByteArray ba; s >> ba; @@ -527,21 +520,24 @@ QDataStream & operator >>(QDataStream & s, Scene *& p) { QVector geom_ind, mat_ind; while (!cs.atEnd()) { switch (cs.read()) { - case 1 : cs.get(p->name_); break; + case 1: cs.get(p->name_); break; case 10: cs.get(p->geometries); break; case 11: cs.get(p->materials); break; - case 20: cs.get(p->root_); p->root_->setScene(p); break; + case 20: + cs.get(p->root_); + p->root_->setScene(p); + break; case 21: cs.get(geom_ind); break; case 22: cs.get(mat_ind); break; } } p->makeMaterialsUniqueNames(); ObjectBaseList cl = p->root_->children(true); - int cnt = qMin(qMin(cl.size(), geom_ind.size()), mat_ind.size()); + int cnt = qMin(qMin(cl.size(), geom_ind.size()), mat_ind.size()); for (int i = 0; i < cnt; ++i) { ObjectBase * c(cl[i]); - if (geom_ind[i] >= 0) c->mesh_ = p->geometries[geom_ind[i]]; - if (mat_ind [i] >= 0) c->material_ = p->materials [mat_ind [i]]; + if (geom_ind[i] >= 0) c->mesh_ = p->geometries[geom_ind[i]]; + if (mat_ind[i] >= 0) c->material_ = p->materials[mat_ind[i]]; } return s; } diff --git a/glscene.h b/glscene.h index 62bc619..839c480 100644 --- a/glscene.h +++ b/glscene.h @@ -1,19 +1,19 @@ /* - QGL Scene - Ivan Pelipenko peri4ko@yandex.ru + QGL Scene + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLSCENE_H @@ -31,8 +31,9 @@ class Scene: public QObject { friend class RendererSelection; friend class ObjectBase; friend class Light; - friend QDataStream & operator <<(QDataStream & s, const Scene * p); - friend QDataStream & operator >>(QDataStream & s, Scene *& p); + friend QDataStream & operator<<(QDataStream & s, const Scene * p); + friend QDataStream & operator>>(QDataStream & s, Scene *& p); + public: explicit Scene(); virtual ~Scene(); @@ -45,8 +46,8 @@ public: Q_ENUMS(SelectionMode) - QString name() const {return name_;} - void setName(const QString & name) {name_ = name;} + QString name() const { return name_; } + void setName(const QString & name) { name_ = name; } bool prepare(); @@ -64,13 +65,13 @@ public: int objectsCount(bool all = false); ObjectBaseList objects(bool all = false); - ObjectBase * rootObject() {return root_;} + ObjectBase * rootObject() { return root_; } void removeObject(ObjectBase * o, bool inChildren = true); void removeObject(ObjectBase & o, bool inChildren = true); void clearObjects(bool deleteAll = false); - SelectionMode selectionMode() const {return sel_mode_;} - void setSelectionMode(SelectionMode mode) {sel_mode_ = mode;} + SelectionMode selectionMode() const { return sel_mode_; } + void setSelectionMode(SelectionMode mode) { sel_mode_ = mode; } void selectObject(ObjectBase * o, bool add_to_selection = false); void selectObjects(ObjectBaseList ol, bool add_to_selection = false); void selectObjectsByMesh(); @@ -82,7 +83,7 @@ public: const Box3D & boundingBox() const; - QVector getMaterials() const {return materials;} + QVector getMaterials() const { return materials; } Material * newMaterial(const QString & name = QString()); void removeMaterial(Material * m); void makeMaterialsUniqueNames(); @@ -105,8 +106,8 @@ protected: Mesh * attachMesh(Mesh * mesh); void setTreeChanged(); void setTreeStructChanged(); - void setMaterialsChanged() {mat_changed = true;} - void setLightsChanged() {lights_changed = tree_changed = true;} + void setMaterialsChanged() { mat_changed = true; } + void setLightsChanged() { lights_changed = tree_changed = true; } void setObjectMeshChanged(ObjectBase * o); @@ -116,13 +117,13 @@ protected: bool need_reload_materials, tree_struct_changed; QVector mat_map_changed; - QVector geometries, td_geometries; - QVector materials; + QVector geometries, td_geometries; + QVector materials; - QMap> geometries_used; - QMap> lights_used; - QList cameras_used; - QVector changed_materials; + QMap> geometries_used; + QMap> lights_used; + QList cameras_used; + QVector changed_materials; SelectionMode sel_mode_; ObjectBaseList selected_, selected_top; @@ -137,7 +138,7 @@ signals: }; -QDataStream & operator <<(QDataStream & s, const Scene * p); -QDataStream & operator >>(QDataStream & s, Scene *& p); +QDataStream & operator<<(QDataStream & s, const Scene * p); +QDataStream & operator>>(QDataStream & s, Scene *& p); #endif // GLSCENE_H diff --git a/gltexture_manager.cpp b/gltexture_manager.cpp index 474578a..781ca3f 100644 --- a/gltexture_manager.cpp +++ b/gltexture_manager.cpp @@ -1,29 +1,30 @@ /* - QGL TextureManager - Ivan Pelipenko peri4ko@yandex.ru + QGL TextureManager + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "gltexture_manager.h" + #include "gltypes.h" QStringList TextureManager::search_pathes("."); QVector3D colorVector(QRgb c) { - return QVector3D(((uchar*)(&c))[0] / 255.f, ((uchar*)(&c))[1] / 255.f, ((uchar*)(&c))[2] / 255.f); + return QVector3D(((uchar *)(&c))[0] / 255.f, ((uchar *)(&c))[1] / 255.f, ((uchar *)(&c))[2] / 255.f); } @@ -37,12 +38,12 @@ GLuint TextureManager::loadTexture(const QString & path, bool ownership, bool bu if (p.isEmpty()) return 0; int tid = textureID(p, bump); if (tid > 0) { - //qDebug() << "[TextureManager] Found" << path << "as" << tid; + // qDebug() << "[TextureManager] Found" << path << "as" << tid; return tid; } QImage image(p); if (bump) convertToNormal(image); - //qDebug() << p << image.width() << image.height() << image.format() << bump; + // qDebug() << p << image.width() << image.height() << image.format() << bump; GLuint tid_ = tid; createGLTexture(f, tid_, image); tid = tid_; @@ -53,7 +54,7 @@ GLuint TextureManager::loadTexture(const QString & path, bool ownership, bool bu qDebug() << "[TextureManager] Loaded" << p << "as" << tid; if (ownership) { tex_ids[bump ? 1 : 0].insert(p, tid); - tex_im [bump ? 1 : 0].insert(p, image); + tex_im[bump ? 1 : 0].insert(p, image); } return tid; } @@ -69,7 +70,7 @@ GLuint TextureManager::loadTexture(const QImage & im, bool ownership, bool bump) qDebug() << "[TextureManager] Can`t load image"; return tid; } - //qDebug() << "[TextureManager] Loaded image as" << tid; + // qDebug() << "[TextureManager] Loaded image as" << tid; return tid; } @@ -109,15 +110,18 @@ void TextureManager::reloadTexture(GLuint tid, const QImage & im) { void TextureManager::convertToNormal(QImage & im) { if (im.isNull()) return; - QImage sim = im.convertToFormat(QImage::Format_ARGB32); - float sum[3] = {0., 0., 0.}; - llong a = 0; + QImage sim = im.convertToFormat(QImage::Format_ARGB32); + float sum[3] = {0., 0., 0.}; + llong a = 0; const uchar * sd = sim.constBits(); for (int i = 0; i < sim.height(); i++) { for (int j = 0; j < sim.width(); j++) { - sum[2] += sd[a] / 255.f - 0.5f; ++a; - sum[1] += sd[a] / 255.f - 0.5f; ++a; - sum[0] += sd[a] / 255.f - 0.5f; ++a; + sum[2] += sd[a] / 255.f - 0.5f; + ++a; + sum[1] += sd[a] / 255.f - 0.5f; + ++a; + sum[0] += sd[a] / 255.f - 0.5f; + ++a; ++a; } } @@ -125,13 +129,13 @@ void TextureManager::convertToNormal(QImage & im) { sum[0] /= wh; sum[1] /= wh; sum[2] /= wh; - //qDebug() << sum[0] << sum[1] << sum[2]; + // qDebug() << sum[0] << sum[1] << sum[2]; if ((qAbs(sum[0]) <= 0.05f) && (qAbs(sum[1]) <= 0.05f) && (sum[2] >= 0.25f)) /// already normal return; - //qDebug() << "convert to normal"; + // qDebug() << "convert to normal"; QImage dim = QImage(sim.width(), sim.height(), QImage::Format_ARGB32); int tx, ty, w = sim.width(), h = sim.height(); - a = 0; + a = 0; uchar * dd = dim.bits(); for (int i = 0; i < sim.height(); i++) { for (int j = 0; j < sim.width(); j++) { @@ -145,27 +149,31 @@ void TextureManager::convertToNormal(QImage & im) { p[2] = colorVector(sim.pixel(tx, i)); res.setY(piClamp(0.5f + (p[0].length() - p[1].length()) / 2.f, 0.f, 1.f)); res.setX(piClamp(0.5f - (p[0].length() - p[2].length()) / 2.f, 0.f, 1.f)); - tx = (j + 1) % w; - ty = (i + 1) % h; + tx = (j + 1) % w; + ty = (i + 1) % h; p[1] = colorVector(sim.pixel(j, ty)); p[2] = colorVector(sim.pixel(tx, i)); res.setY(piClamp(0.5f + (p[0].length() - p[1].length()) / 2.f, 0.f, 1.f)); res.setX(piClamp(0.5f - (p[0].length() - p[2].length()) / 2.f, 0.f, 1.f)); res.setZ(1.f); - dd[a] = res.z() * 255; ++a; - dd[a] = res.y() * 255; ++a; - dd[a] = res.x() * 255; ++a; - dd[a] = 255; ++a; + dd[a] = res.z() * 255; + ++a; + dd[a] = res.y() * 255; + ++a; + dd[a] = res.x() * 255; + ++a; + dd[a] = 255; + ++a; } } im = dim; - //im.save("_normal.png"); + // im.save("_normal.png"); } bool TextureManager::loadTextures() { QFileInfoList fil; - foreach (const QString & i, tex_pathes) + foreach(const QString & i, tex_pathes) loadTexture(i, true); tex_pathes.clear(); return true; @@ -178,7 +186,7 @@ void TextureManager::deleteTextures() { qDebug() << "[TextureManager] Delete" << texs.size() << "textures"; if (!texs.isEmpty()) f->glDeleteTextures(texs.size(), &texs[0]); tex_ids[i].clear(); - tex_im [i].clear(); + tex_im[i].clear(); } } @@ -189,7 +197,7 @@ void TextureManager::deleteTexture(const QString & name) { GLuint id = tex_ids[i][name]; f->glDeleteTextures(1, &id); tex_ids[i].remove(name); - tex_im [i].remove(name); + tex_im[i].remove(name); } } } diff --git a/gltexture_manager.h b/gltexture_manager.h index aebe8b3..f77c1e4 100644 --- a/gltexture_manager.h +++ b/gltexture_manager.h @@ -1,29 +1,29 @@ /* - QGL TextureManager - Ivan Pelipenko peri4ko@yandex.ru + QGL TextureManager + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLTEXTUREMANAGER_H #define GLTEXTUREMANAGER_H -#include #include -#include #include #include +#include +#include class TextureManager { @@ -31,18 +31,20 @@ public: TextureManager(QOpenGLExtraFunctions * f_): f(f_) {} virtual ~TextureManager() {} - static void addSearchPath(const QString & path) {if (!search_pathes.contains(path)) search_pathes << path;} - static void clearSearchPathes() {search_pathes.clear();} - static QStringList searchPathes() {return search_pathes;} + static void addSearchPath(const QString & path) { + if (!search_pathes.contains(path)) search_pathes << path; + } + static void clearSearchPathes() { search_pathes.clear(); } + static QStringList searchPathes() { return search_pathes; } static QString findFile(const QString & path); GLuint loadTexture(const QString & path, bool ownership = true, bool bump = false); GLuint loadTexture(const QImage & image, bool ownership = true, bool bump = false); QImage loadTextureImage(const QString & path, bool bump = false); void reloadTexture(GLuint tid, const QString & path); void reloadTexture(GLuint tid, const QImage & image); - int textureID (const QString & path, bool bump = false) {return tex_ids[bump ? 1 : 0][path];} - QImage textureImage(const QString & path, bool bump = false) {return tex_im [bump ? 1 : 0][path];} - void addTexture(const QString & path) {tex_pathes << path;} + int textureID(const QString & path, bool bump = false) { return tex_ids[bump ? 1 : 0][path]; } + QImage textureImage(const QString & path, bool bump = false) { return tex_im[bump ? 1 : 0][path]; } + void addTexture(const QString & path) { tex_pathes << path; } bool loadTextures(); void deleteTextures(); void deleteTexture(const QString & name); @@ -54,7 +56,7 @@ protected: static QStringList search_pathes; QMap tex_ids[2]; - QMap tex_im [2]; + QMap tex_im[2]; QStringList tex_pathes; QOpenGLExtraFunctions * f; }; diff --git a/glwidget.cpp b/glwidget.cpp index 8dbab0e..e18bcc0 100644 --- a/glwidget.cpp +++ b/glwidget.cpp @@ -1,31 +1,33 @@ /* - QGL GLWidget - Ivan Pelipenko peri4ko@yandex.ru + QGL GLWidget + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "glwidget.h" + #include "qglview.h" + #include -GLWidget::GLWidget(QWidget *parent) : QWidget(parent) { - view_ = new QGLView(); - //view_->setFlags(windowFlags() | Qt::FramelessWindowHint); +GLWidget::GLWidget(QWidget * parent): QWidget(parent) { + view_ = new QGLView(); + // view_->setFlags(windowFlags() | Qt::FramelessWindowHint); container = QWidget::createWindowContainer(view_, this); - lay = new QVBoxLayout(this); + lay = new QVBoxLayout(this); lay->addWidget(container); lay->setContentsMargins(0, 0, 0, 0); lay->setSpacing(0); @@ -70,8 +72,7 @@ bool GLWidget::isMouseSelectionEnabled() const { } -bool GLWidget::isCameraOrbit() const -{ +bool GLWidget::isCameraOrbit() const { return view_->isCameraOrbit(); } @@ -121,7 +122,7 @@ QByteArray GLWidget::saveCamera() { } -void GLWidget::restoreCamera(const QByteArray &ba) { +void GLWidget::restoreCamera(const QByteArray & ba) { view_->restoreCamera(ba); } diff --git a/glwidget.h b/glwidget.h index e65c090..81faae6 100644 --- a/glwidget.h +++ b/glwidget.h @@ -1,19 +1,19 @@ /* - QGL GLWidget - Ivan Pelipenko peri4ko@yandex.ru + QGL GLWidget + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef GLWIDGET_H @@ -26,25 +26,25 @@ class QGLView; class ObjectBase; class Scene; -class GLWidget : public QWidget -{ +class GLWidget: public QWidget { Q_OBJECT - Q_PROPERTY (qreal lineWidth READ lineWidth WRITE setLineWidth) - Q_PROPERTY (qreal FOV READ FOV WRITE setFOV) - Q_PROPERTY (qreal depthStart READ depthStart WRITE setDepthStart) - Q_PROPERTY (bool grabMouse READ isGrabMouseEnabled WRITE setGrabMouseEnabled) - Q_PROPERTY (bool mouseRotate READ isMouseRotateEnabled WRITE setMouseRotateEnabled) - Q_PROPERTY (bool mouseSelection READ isMouseSelectionEnabled WRITE setMouseSelectionEnabled) - Q_PROPERTY (bool cameraOrbit READ isCameraOrbit WRITE setCameraOrbit) - Q_PROPERTY (bool hoverHalo READ isHoverHaloEnabled WRITE setHoverHaloEnabled) - Q_PROPERTY (QColor hoverHaloColor READ hoverHaloColor WRITE setHoverHaloColor) - Q_PROPERTY (qreal hoverHaloFillAlpha READ hoverHaloFillAlpha WRITE setHoverHaloFillAlpha) - Q_PROPERTY (bool selectionHalo READ isSelectionHaloEnabled WRITE setSelectionHaloEnabled) - Q_PROPERTY (QColor selectionHaloColor READ selectionHaloColor WRITE setSelectionHaloColor) - Q_PROPERTY (qreal selectionHaloFillAlpha READ selectionHaloFillAlpha WRITE setSelectionHaloFillAlpha) + Q_PROPERTY(qreal lineWidth READ lineWidth WRITE setLineWidth) + Q_PROPERTY(qreal FOV READ FOV WRITE setFOV) + Q_PROPERTY(qreal depthStart READ depthStart WRITE setDepthStart) + Q_PROPERTY(bool grabMouse READ isGrabMouseEnabled WRITE setGrabMouseEnabled) + Q_PROPERTY(bool mouseRotate READ isMouseRotateEnabled WRITE setMouseRotateEnabled) + Q_PROPERTY(bool mouseSelection READ isMouseSelectionEnabled WRITE setMouseSelectionEnabled) + Q_PROPERTY(bool cameraOrbit READ isCameraOrbit WRITE setCameraOrbit) + Q_PROPERTY(bool hoverHalo READ isHoverHaloEnabled WRITE setHoverHaloEnabled) + Q_PROPERTY(QColor hoverHaloColor READ hoverHaloColor WRITE setHoverHaloColor) + Q_PROPERTY(qreal hoverHaloFillAlpha READ hoverHaloFillAlpha WRITE setHoverHaloFillAlpha) + Q_PROPERTY(bool selectionHalo READ isSelectionHaloEnabled WRITE setSelectionHaloEnabled) + Q_PROPERTY(QColor selectionHaloColor READ selectionHaloColor WRITE setSelectionHaloColor) + Q_PROPERTY(qreal selectionHaloFillAlpha READ selectionHaloFillAlpha WRITE setSelectionHaloFillAlpha) + public: - explicit GLWidget(QWidget *parent = nullptr); - QGLView * view() {return view_;} + explicit GLWidget(QWidget * parent = nullptr); + QGLView * view() { return view_; } QColor backColor() const; qreal lineWidth() const; diff --git a/mouse_controller.cpp b/mouse_controller.cpp index d5ef0aa..f747928 100644 --- a/mouse_controller.cpp +++ b/mouse_controller.cpp @@ -1,49 +1,50 @@ /* - QGL MouseController - Ivan Pelipenko peri4ko@yandex.ru + QGL MouseController + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "mouse_controller.h" + #include "glmesh.h" #include "qglview.h" -#include + #include #include +#include using namespace QGLEngineShaders; MouseController::MouseController(QGLView * view_): view(view_) { - app_scale = 1; - lastPos = QPoint(-1, -1); - cur_action = RendererService::haNoAction; - sel_button = Qt::LeftButton; - sel_mod = Qt::ControlModifier; + app_scale = 1; + lastPos = QPoint(-1, -1); + cur_action = RendererService::haNoAction; + sel_button = Qt::LeftButton; + sel_mod = Qt::ControlModifier; mouse_first = mouseSelect_ = mouseRotate_ = cameraOrbit_ = canSelect_ = true; grabMouse_ = mouse_sec = selecting_ = customMouseMove_ = false; } -MouseController::~MouseController() { -} +MouseController::~MouseController() {} void MouseController::resize() { mouse_first = true; - app_scale = appScale(); + app_scale = appScale(); } @@ -54,8 +55,8 @@ void MouseController::mouseReleaseEvent(QMouseEvent * e) { } bool add_ts = e->modifiers().testFlag(sel_mod); if (selecting_) { - selecting_ = false; - canSelect_ = true; + selecting_ = false; + canSelect_ = true; view->renderer_.mouse_rect = QRect(); view->scene()->selectObjects(hov_objects.toList(), add_ts); return; @@ -63,12 +64,11 @@ void MouseController::mouseReleaseEvent(QMouseEvent * e) { if (canSelect_ && mouseSelect_) { if ((lastPos - downPos).manhattanLength() < QApplication::startDragDistance()) { if (e->button() == Qt::LeftButton) { - //qDebug() << hov_objects << hov_aims; + // qDebug() << hov_objects << hov_aims; if (hov_objects.isEmpty() && hov_aims.isEmpty()) { view->scene()->clearSelection(); } else { - if (!hov_objects.isEmpty()) - view->scene()->selectObject(hov_objects[0], add_ts); + if (!hov_objects.isEmpty()) view->scene()->selectObject(hov_objects[0], add_ts); if (!hov_aims.isEmpty()) { view->scene()->selectObject(hov_aims[0], add_ts); hov_aims[0]->setAimSelected(true); @@ -79,11 +79,11 @@ void MouseController::mouseReleaseEvent(QMouseEvent * e) { if (view->renderer_.edit_mode && !view->scene()->selectedObjects().isEmpty()) view->popupMenu( #if QT_VERSION_MAJOR <= 5 - ((QMouseEvent*)e)->globalPos() + ((QMouseEvent *)e)->globalPos() #else - ((QMouseEvent*)e)->globalPosition().toPoint() + ((QMouseEvent *)e)->globalPosition().toPoint() #endif - ); + ); } } } @@ -98,7 +98,7 @@ void MouseController::mousePressEvent(QMouseEvent * e) { return; } if (selecting_) { - selecting_ = false; + selecting_ = false; view->renderer_.mouse_rect = QRect(); return; } @@ -134,11 +134,12 @@ void MouseController::mouseMoveEvent(QMouseEvent * e) { default: break; } QVector scales; - foreach (int a, axis) { - QVector3D axe_vector; axe_vector[a] = 1.; - QMatrix4x4 axis_mat = view->camera()->fullViewMatrix() * rs.axis_mat; + foreach(int a, axis) { + QVector3D axe_vector; + axe_vector[a] = 1.; + QMatrix4x4 axis_mat = view->camera()->fullViewMatrix() * rs.axis_mat; QVector3D center_screen = axis_mat.map(rs.selection_center); - QVector3D axe_screen = (axis_mat.map(rs.selection_center + axe_vector) - center_screen).normalized(); + QVector3D axe_screen = (axis_mat.map(rs.selection_center + axe_vector) - center_screen).normalized(); QVector3D mouse_vector(cpos - lastPos); mouse_vector[1] *= -1.; if (cur_action == RendererService::haMove) { @@ -147,13 +148,12 @@ void MouseController::mouseMoveEvent(QMouseEvent * e) { mouse_vector *= -center_screen.z() * len_scl; axe_vector *= QVector3D::dotProduct(axe_screen, mouse_vector); QMatrix4x4 pmat; - foreach (ObjectBase * o, objects) { + foreach(ObjectBase * o, objects) { pmat.setToIdentity(); - if (o->parent()) - pmat = o->parent()->worldTransform().inverted(); + if (o->parent()) pmat = o->parent()->worldTransform().inverted(); QVector3D dv = pmat.mapVector(axe_vector); if (o->isAimSelected()) { - AimedObject * ao = (AimedObject*)o; + AimedObject * ao = (AimedObject *)o; ao->setAim(ao->aim() + dv); } else o->move(dv); @@ -164,7 +164,7 @@ void MouseController::mouseMoveEvent(QMouseEvent * e) { axe_screen.normalize(); QVector3D norm = QVector3D(axe_screen.y(), -axe_screen.x(), 0.); axe_vector *= QVector3D::dotProduct(mouse_vector, norm) / 2. / app_scale; - foreach (ObjectBase * o, objects) + foreach(ObjectBase * o, objects) o->setRotation(o->rotation() + axe_vector); } if (cur_action == RendererService::haScale) { @@ -176,17 +176,17 @@ void MouseController::mouseMoveEvent(QMouseEvent * e) { } if (cur_action == RendererService::haScale) { double sc = 0., max = 0.; - foreach (const QVector3D & s, scales) { - double v = QVector3D::dotProduct(s, QVector3D(1,1,1)); + foreach(const QVector3D & s, scales) { + double v = QVector3D::dotProduct(s, QVector3D(1, 1, 1)); sc += v; max = qMax(max, qAbs(v)); } sc = max * (sc > 0. ? 1. : -1); QVector3D axe_vector; - foreach (int a, axis) + foreach(int a, axis) axe_vector[a] = 1.; - foreach (ObjectBase * o, objects) - o->scale(QVector3D(1,1,1) + (axe_vector * sc)); + foreach(ObjectBase * o, objects) + o->scale(QVector3D(1, 1, 1) + (axe_vector * sc)); QCursor::setPos(view->mapToGlobal(downPos)); } else lastPos = e->pos(); @@ -215,7 +215,7 @@ void MouseController::mouseMoveEvent(QMouseEvent * e) { #endif if (e->buttons().testFlag(QT_MID_BUTTON)) { if (cameraOrbit_) { - view->camera()->orbitZ (dx / 4.f); + view->camera()->orbitZ(dx / 4.f); view->camera()->orbitXY(dy / 4.f); } else { view->camera()->rotateZ(-dx / 4.f); @@ -225,34 +225,33 @@ void MouseController::mouseMoveEvent(QMouseEvent * e) { } else if (e->buttons().testFlag(Qt::RightButton)) { float ad = view->camera()->distance(); view->camera()->moveLeft(dx / 1000.f * ad); - view->camera()->moveUp (dy / 1000.f * ad); + view->camera()->moveUp(dy / 1000.f * ad); emit view->cameraPosChanged(view->camera()->pos()); } } if (customMouseMove_) emit view->customMouseMoveEvent(e->pos(), lastPos, e->buttons()); lastPos = e->pos(); if (e->buttons() == 0) { - cur_handle = QFlags(); - cur_action = RendererService::haNoAction; + cur_handle = QFlags(); + cur_action = RendererService::haNoAction; Qt::CursorShape cs = Qt::CrossCursor; if (view->renderer_.edit_mode) { - uint hid = view->renderer_.rend_selection.id_hover; + uint hid = view->renderer_.rend_selection.id_hover; cur_handle = (RendererService::HandleMesh)hid; - if (hid >= RendererService::hmMoveX && hid <= RendererService::hmMaxMove ) { + if (hid >= RendererService::hmMoveX && hid <= RendererService::hmMaxMove) { cur_action = RendererService::haMove; - cs = Qt::SizeAllCursor; + cs = Qt::SizeAllCursor; } if (hid >= RendererService::hmRotateX && hid <= RendererService::hmMaxRotate) { cur_action = RendererService::haRotate; - cs = Qt::PointingHandCursor; + cs = Qt::PointingHandCursor; } - if (hid >= RendererService::hmScaleX && hid <= RendererService::hmMaxScale ) { + if (hid >= RendererService::hmScaleX && hid <= RendererService::hmMaxScale) { cur_action = RendererService::haScale; - cs = Qt::SplitHCursor; + cs = Qt::SplitHCursor; } } - if (cur_action == RendererService::haNoAction) - cur_handle = QFlags(); + if (cur_action == RendererService::haNoAction) cur_handle = QFlags(); view->setCursor(cs); view->renderer_.rend_service.current_handle = cur_handle; } @@ -264,7 +263,7 @@ void MouseController::mouseMoveEvent(QMouseEvent * e) { } if (mouse_first) { mouse_first = false; - mouse_sec = true; + mouse_sec = true; return; } lastPos = g_rect.center(); @@ -272,8 +271,8 @@ void MouseController::mouseMoveEvent(QMouseEvent * e) { int dx = e->x() - lastPos.x(); int dy = e->y() - lastPos.y(); #else - int dx = e->position().toPoint().x() - lastPos.x(); - int dy = e->position().toPoint().y() - lastPos.y(); + int dx = e->position().toPoint().x() - lastPos.x(); + int dy = e->position().toPoint().y() - lastPos.y(); #endif emit view->glMouseMoveEvent(new QMouseEvent(QEvent::MouseMove, QPoint(dx, dy), e->button(), e->buttons(), e->modifiers())); return; @@ -286,9 +285,9 @@ void MouseController::wheelEvent(QWheelEvent * e) { if (mouseRotate_) { double ang = #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) - e->delta(); + e->delta(); #else - e->angleDelta().y(); + e->angleDelta().y(); #endif if (ang > 0) view->camera()->flyCloser(0.1f); if (ang < 0) view->camera()->flyFarer(0.1f); @@ -298,13 +297,12 @@ void MouseController::wheelEvent(QWheelEvent * e) { } -void MouseController::leaveEvent(QEvent * ) { +void MouseController::leaveEvent(QEvent *) { lastPos = QPoint(-1, -1); - //qDebug() << lastPos; + // qDebug() << lastPos; } void MouseController::mouseDoubleClickEvent(QMouseEvent * e) { - if (e->buttons().testFlag(QT_MID_BUTTON)) - emit view->doubleClick(); + if (e->buttons().testFlag(QT_MID_BUTTON)) emit view->doubleClick(); } diff --git a/mouse_controller.h b/mouse_controller.h index 310fe45..8e9fd82 100644 --- a/mouse_controller.h +++ b/mouse_controller.h @@ -1,50 +1,51 @@ /* - QGL MouseController - Ivan Pelipenko peri4ko@yandex.ru + QGL MouseController + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef MOUSE_CONTROLLER_H #define MOUSE_CONTROLLER_H -#include "glprimitives.h" #include "glcamera.h" +#include "glprimitives.h" #include "renderer_service.h" + #include #include -class MouseController: public QObject -{ +class MouseController: public QObject { friend class QGLView; friend class RendererSelection; Q_OBJECT + public: MouseController(QGLView * view_); virtual ~MouseController(); - bool isGrabMouseEnabled() const {return grabMouse_;} - bool isMouseRotateEnabled() const {return mouseRotate_;} - bool isMouseSelectionEnabled() const {return mouseSelect_;} - bool isCameraOrbit() const {return cameraOrbit_;} + bool isGrabMouseEnabled() const { return grabMouse_; } + bool isMouseRotateEnabled() const { return mouseRotate_; } + bool isMouseSelectionEnabled() const { return mouseSelect_; } + bool isCameraOrbit() const { return cameraOrbit_; } - Qt::MouseButton selectionButton() const {return sel_button;} - Qt::KeyboardModifier selectionModifier() const {return sel_mod;} + Qt::MouseButton selectionButton() const { return sel_button; } + Qt::KeyboardModifier selectionModifier() const { return sel_mod; } - void setSelectionButton(Qt::MouseButton v) {sel_button = v;} - void setSelectionModifier(Qt::KeyboardModifier v) {sel_mod = v;} + void setSelectionButton(Qt::MouseButton v) { sel_button = v; } + void setSelectionModifier(Qt::KeyboardModifier v) { sel_mod = v; } protected: void resize(); @@ -52,14 +53,14 @@ protected: void mouseMoveEvent(QMouseEvent * e); void mouseReleaseEvent(QMouseEvent * e); void wheelEvent(QWheelEvent * e); - void leaveEvent(QEvent * ); + void leaveEvent(QEvent *); void mouseDoubleClickEvent(QMouseEvent * e); private: QGLView * view; QPoint lastPos, downPos; QSet keys_; - QVector hov_objects, hov_aims; + QVector hov_objects, hov_aims; Qt::MouseButton sel_button; Qt::KeyboardModifier sel_mod; RendererService::HandleAction cur_action; @@ -71,14 +72,16 @@ private: private slots: public slots: - void setGrabMouseEnabled(const bool & arg) {grabMouse_ = arg; mouse_first = true;} - void setMouseRotateEnabled(const bool & arg) {mouseRotate_ = arg;} - void setMouseSelectionEnabled(const bool & arg) {mouseSelect_ = arg;} - void setCustomMouseMove(const bool & arg) {customMouseMove_ = arg;} - void setCameraOrbit(const bool & arg) {cameraOrbit_ = arg;} + void setGrabMouseEnabled(const bool & arg) { + grabMouse_ = arg; + mouse_first = true; + } + void setMouseRotateEnabled(const bool & arg) { mouseRotate_ = arg; } + void setMouseSelectionEnabled(const bool & arg) { mouseSelect_ = arg; } + void setCustomMouseMove(const bool & arg) { customMouseMove_ = arg; } + void setCameraOrbit(const bool & arg) { cameraOrbit_ = arg; } signals: - }; #endif // QGLVIEW_H diff --git a/openglwindow.cpp b/openglwindow.cpp index 48c401c..f14f804 100644 --- a/openglwindow.cpp +++ b/openglwindow.cpp @@ -1,4 +1,5 @@ #include "openglwindow.h" + #include #include #include @@ -6,7 +7,7 @@ #include -OpenGLWindow::OpenGLWindow(QWindow *parent) : QWindow(parent) { +OpenGLWindow::OpenGLWindow(QWindow * parent): QWindow(parent) { setFlags(flags() | Qt::FramelessWindowHint); setSurfaceType(QWindow::OpenGLSurface); QSurfaceFormat format = QSurfaceFormat::defaultFormat(); @@ -29,18 +30,15 @@ void OpenGLWindow::renderLater() { } -bool OpenGLWindow::event(QEvent *event) { +bool OpenGLWindow::event(QEvent * event) { switch (event->type()) { - case QEvent::UpdateRequest: - renderNow(); - return true; - default: - return QWindow::event(event); + case QEvent::UpdateRequest: renderNow(); return true; + default: return QWindow::event(event); } } -void OpenGLWindow::exposeEvent(QExposeEvent *event) { +void OpenGLWindow::exposeEvent(QExposeEvent * event) { if (isExposed()) renderNow(); } @@ -68,19 +66,19 @@ bool OpenGLWindow::getVSync() const { } -//void OpenGLWindow::setSamples(int samples) { +// void OpenGLWindow::setSamples(int samples) { // QSurfaceFormat f = requestedFormat(); // if (f.samples() != samples) { // f.setSamples(samples); // setFormat(f); // format_change = true; // } -//} +// } -//int OpenGLWindow::getSamples() const { +// int OpenGLWindow::getSamples() const { // return requestedFormat().samples(); -//} +// } void OpenGLWindow::renderNow() { @@ -92,7 +90,7 @@ void OpenGLWindow::renderNow() { m_context->setFormat(requestedFormat()); m_context->create(); needsInitialize = true; - format_change = false; + format_change = false; } m_context->makeCurrent(this); if (needsInitialize) { @@ -103,4 +101,3 @@ void OpenGLWindow::renderNow() { m_context->swapBuffers(this); frame_cnt++; } - diff --git a/openglwindow.h b/openglwindow.h index 36f4e45..7a124f9 100644 --- a/openglwindow.h +++ b/openglwindow.h @@ -1,38 +1,40 @@ -#include #include +#include class QPainter; class QOpenGLContext; class QOpenGLPaintDevice; -class OpenGLWindow : public QWindow, public QOpenGLExtraFunctions { +class OpenGLWindow + : public QWindow + , public QOpenGLExtraFunctions { Q_OBJECT + public: - explicit OpenGLWindow(QWindow *parent = nullptr); + explicit OpenGLWindow(QWindow * parent = nullptr); virtual ~OpenGLWindow() {} virtual void render() {} virtual void initialize() {} - QOpenGLContext * context() {return m_context;} + QOpenGLContext * context() { return m_context; } void setVSync(bool on); bool getVSync() const; -// void setSamples(int samples); -// int getSamples() const; - int getFrameCounter() const {return frame_cnt;} + // void setSamples(int samples); + // int getSamples() const; + int getFrameCounter() const { return frame_cnt; } public slots: void renderLater(); void renderNow(); protected: - bool event(QEvent *event) override; - void exposeEvent(QExposeEvent *event) override; + bool event(QEvent * event) override; + void exposeEvent(QExposeEvent * event) override; private: QOpenGLContext * m_context = nullptr; - bool format_change = false; - int frame_cnt = 0; + bool format_change = false; + int frame_cnt = 0; }; - diff --git a/plugin/qglview_designerplugin.cpp b/plugin/qglview_designerplugin.cpp index 708776b..04c4ed2 100644 --- a/plugin/qglview_designerplugin.cpp +++ b/plugin/qglview_designerplugin.cpp @@ -1,14 +1,13 @@ #include "qglview_designerplugin.h" + #include "qglviewplugin.h" -QGLViewDesignerPlugin::QGLViewDesignerPlugin(QObject * parent): QObject(parent) -{ +QGLViewDesignerPlugin::QGLViewDesignerPlugin(QObject * parent): QObject(parent) { m_widgets.append(new QGLViewPlugin(this)); } -QList QGLViewDesignerPlugin::customWidgets() const { +QList QGLViewDesignerPlugin::customWidgets() const { return m_widgets; } - diff --git a/plugin/qglview_designerplugin.h b/plugin/qglview_designerplugin.h index db6fc67..ba7610c 100644 --- a/plugin/qglview_designerplugin.h +++ b/plugin/qglview_designerplugin.h @@ -1,22 +1,23 @@ #ifndef QGLVIEW_DESIGNERPLUGIN_H #define QGLVIEW_DESIGNERPLUGIN_H -#include #include +#include -class QGLViewDesignerPlugin: public QObject, public QDesignerCustomWidgetCollectionInterface -{ +class QGLViewDesignerPlugin + : public QObject + , public QDesignerCustomWidgetCollectionInterface { Q_OBJECT Q_PLUGIN_METADATA(IID "qad.qglview") Q_INTERFACES(QDesignerCustomWidgetCollectionInterface) + public: QGLViewDesignerPlugin(QObject * parent = 0); - virtual QList customWidgets() const; + virtual QList customWidgets() const; private: - QList m_widgets; - + QList m_widgets; }; #endif // QGLVIEW_DESIGNERPLUGIN_H diff --git a/plugin/qglviewplugin.cpp b/plugin/qglviewplugin.cpp index 97db1e4..92cfa98 100644 --- a/plugin/qglviewplugin.cpp +++ b/plugin/qglviewplugin.cpp @@ -1,9 +1,11 @@ -#include "../glwidget.h" #include "qglviewplugin.h" -#include + +#include "../glwidget.h" #include "glprimitives.h" #include "qglview.h" +#include + QGLViewPlugin::QGLViewPlugin(QObject * parent): QObject(parent) { m_initialized = false; @@ -11,8 +13,7 @@ QGLViewPlugin::QGLViewPlugin(QObject * parent): QObject(parent) { void QGLViewPlugin::initialize(QDesignerFormEditorInterface * /* core */) { - if (m_initialized) - return; + if (m_initialized) return; // Add extension registrations, etc. here @@ -30,21 +31,24 @@ QWidget * QGLViewPlugin::createWidget(QWidget * parent) { if (m_initialized) { auto axis = new GLObjectBase(); GLObjectBase * obj; - float al = 1.; - obj = new GLPrimitiveLine(QVector3D(0, 0, -al), QVector3D(0, 0, al)); - obj->material().color_diffuse = Qt::darkBlue; obj->setAcceptLight(false); + float al = 1.; + obj = new GLPrimitiveLine(QVector3D(0, 0, -al), QVector3D(0, 0, al)); + obj->material().color_diffuse = Qt::darkBlue; + obj->setAcceptLight(false); obj->setSelectable(false); axis->addChild(obj); - obj = new GLPrimitiveLine(QVector3D(-al, 0, 0), QVector3D(al, 0, 0)); - obj->material().color_diffuse = Qt::darkRed; obj->setAcceptLight(false); + obj = new GLPrimitiveLine(QVector3D(-al, 0, 0), QVector3D(al, 0, 0)); + obj->material().color_diffuse = Qt::darkRed; + obj->setAcceptLight(false); obj->setSelectable(false); axis->addChild(obj); - obj = new GLPrimitiveLine(QVector3D(0, -al, 0), QVector3D(0, al, 0)); - obj->material().color_diffuse = Qt::darkGreen; obj->setAcceptLight(false); + obj = new GLPrimitiveLine(QVector3D(0, -al, 0), QVector3D(0, al, 0)); + obj->material().color_diffuse = Qt::darkGreen; + obj->setAcceptLight(false); obj->setSelectable(false); axis->addChild(obj); w->view()->addObject(axis); - auto cam_light = new Light(); + auto cam_light = new Light(); cam_light->intensity = 0.5; cam_light->setName("Camera_Light"); w->view()->camera()->addChild(cam_light); @@ -92,4 +96,3 @@ QString QGLViewPlugin::domXml() const { QString QGLViewPlugin::includeFile() const { return QLatin1String("glwidget.h"); } - diff --git a/plugin/qglviewplugin.h b/plugin/qglviewplugin.h index 6516afb..444a03a 100644 --- a/plugin/qglviewplugin.h +++ b/plugin/qglviewplugin.h @@ -5,8 +5,9 @@ #include -class QGLViewPlugin: public QObject, public QDesignerCustomWidgetInterface -{ +class QGLViewPlugin + : public QObject + , public QDesignerCustomWidgetInterface { Q_OBJECT Q_INTERFACES(QDesignerCustomWidgetInterface) @@ -27,7 +28,6 @@ public: private: bool m_initialized; - }; -#endif //QGLVIEWPLUGIN_H +#endif // QGLVIEWPLUGIN_H diff --git a/qglview.cpp b/qglview.cpp index 75a4d81..a04380f 100644 --- a/qglview.cpp +++ b/qglview.cpp @@ -1,51 +1,53 @@ /* - QGLView - Ivan Pelipenko peri4ko@yandex.ru + QGLView + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "qglview.h" + #include "glmesh.h" #include "gltexture_manager.h" + +#include +#include +#include #include #include -#include -#include -#include using namespace QGLEngineShaders; QGLView::QGLView(): OpenGLWindow(), renderer_(this), mouse(this) { setIcon(QIcon(":/icons/qglview.png")); - is_init = false; - timer = 0; - hoverHaloColor_ = QColor(195, 140, 255); + is_init = false; + timer = 0; + hoverHaloColor_ = QColor(195, 140, 255); selectionHaloColor_ = QColor(175, 255, 140); - lineWidth_ = 1.; - max_anisotropic = 1; + lineWidth_ = 1.; + max_anisotropic = 1; max_texture_chanels = 8; - lightEnabled_ = true; - shaders_supported = false; - fps_cnt = 0; - fps_tm = fps_ = 0.; - fogColor_ = Qt::darkGray; - fogDensity_ = 0.; - fogDecay_ = 10.; + lightEnabled_ = true; + shaders_supported = false; + fps_cnt = 0; + fps_tm = fps_ = 0.; + fogColor_ = Qt::darkGray; + fogDensity_ = 0.; + fogDecay_ = 10.; hoverHaloFill_ = selectionHaloFill_ = 0.15f; - //lmode = Simple; + // lmode = Simple; setFeature(qglFXAA, false); setFeature(qglAnisotropicLevel, 8); setFeature(qglEyeAccomodationEnabled, false); @@ -72,16 +74,16 @@ QGLView::QGLView(): OpenGLWindow(), renderer_(this), mouse(this) { setFeature(qglDepthOfFieldDiaphragm, 8.); hoverHalo_ = selectionHalo_ = true; fogEnabled_ = shaders_bind = false; - rmode = ObjectBase::Fill; - scene_ = new Scene(); + rmode = ObjectBase::Fill; + scene_ = new Scene(); connect(scene_, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged())); connect(scene_, SIGNAL(__destroyed()), this, SLOT(__destroyed())); - connect(scene_, SIGNAL(__objectDeleted(ObjectBase*)), this, SLOT(__objectDeleted(ObjectBase*))); + connect(scene_, SIGNAL(__objectDeleted(ObjectBase *)), this, SLOT(__objectDeleted(ObjectBase *))); default_camera = new Camera(); default_camera->setPos(QVector3D(2, 2, 2)); default_camera->setAim(QVector3D()); camera_ = default_camera; -// qDebug() << camera_->aim(); + // qDebug() << camera_->aim(); default_camera->setName("Camera"); emit cameraPosChanged(default_camera->pos()); @@ -112,27 +114,25 @@ void QGLView::start(float freq) { } -QList QGLView::selectedLights() const { - QList ret; +QList QGLView::selectedLights() const { + QList ret; ObjectBaseList sol = scene_->selectedObjects(); - foreach (ObjectBase * o, sol) - if (o->type() == ObjectBase::glLight) - ret << (Light*)o; + foreach(ObjectBase * o, sol) + if (o->type() == ObjectBase::glLight) ret << (Light *)o; return ret; } -QList QGLView::selectedCameras() const { - QList ret; +QList QGLView::selectedCameras() const { + QList ret; ObjectBaseList sol = scene_->selectedObjects(); - for (ObjectBase * o : sol) { - if (o->type() == ObjectBase::glCamera) ret << (Camera*)o; + for (ObjectBase * o: sol) { + if (o->type() == ObjectBase::glCamera) ret << (Camera *)o; } return ret; } - void QGLView::resizeEvent(QResizeEvent * e) { renderLater(); mouse.resize(); @@ -142,7 +142,7 @@ void QGLView::resizeEvent(QResizeEvent * e) { void QGLView::timerEvent(QTimerEvent *) { renderNow(); Qt::KeyboardModifiers km = QApplication::keyboardModifiers(); - foreach (int i, keys_) + foreach(int i, keys_) emit keyEvent((Qt::Key)i, km); } @@ -157,8 +157,8 @@ void QGLView::render() { fps_cnt++; if (fps_tm < 1000.) return; time.restart(); - fps_ = fps_cnt / fps_tm * 1000.; - fps_tm = 0.; + fps_ = fps_cnt / fps_tm * 1000.; + fps_tm = 0.; fps_cnt = 0; } @@ -179,7 +179,7 @@ void QGLView::initialize() { renderer_.reloadShaders(); renderer_.init(width(), height()); scene_->reinitAll(); - is_init = true; + is_init = true; prev_size = QSize(); emit glInitializeDone(); } @@ -207,9 +207,9 @@ void QGLView::resizeGL(int width, int height) { if (width <= 0 || height <= 0) return; if (prev_size == QSize(width, height)) return; prev_size = QSize(width, height); - aspect = float(width) / float(height); + aspect = float(width) / float(height); renderer_.resize(width, height); - //qDebug() << "resize" << width << height; + // qDebug() << "resize" << width << height; iaspect = (aspect == 0.f) ? 0. : 1 / aspect; glViewport(0, 0, width, height); emit glResize(width, height); diff --git a/qglview.h b/qglview.h index d4d6753..eba1263 100644 --- a/qglview.h +++ b/qglview.h @@ -1,62 +1,62 @@ /* - QGLView - Ivan Pelipenko peri4ko@yandex.ru + QGLView + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef QGLVIEW_H #define QGLVIEW_H -#include "openglwindow.h" +#include "glcamera.h" #include "glframebuffer.h" #include "glprimitives.h" -#include "glcamera.h" #include "glscene.h" -#include "renderer.h" #include "mouse_controller.h" +#include "openglwindow.h" +#include "renderer.h" + #include #include -class QGLView: public OpenGLWindow -{ +class QGLView: public OpenGLWindow { friend class RendererSelection; Q_OBJECT - Q_PROPERTY (float lineWidth READ lineWidth WRITE setLineWidth) - Q_PROPERTY (float FOV READ FOV WRITE setFOV) - Q_PROPERTY (float depthStart READ depthStart WRITE setDepthStart) - Q_PROPERTY (float gamma READ gamma WRITE setGamma) - Q_PROPERTY (bool autoExposure READ autoExposure WRITE setAutoExposure) - Q_PROPERTY (QColor fogColor READ fogColor WRITE setFogColor) - Q_PROPERTY (bool fogEnabled READ isFogEnabled WRITE setFogEnabled) - Q_PROPERTY (float fogDensity READ fogDensity WRITE setFogDensity) - Q_PROPERTY (float fogDecay READ fogDecay WRITE setFogDecay) - Q_PROPERTY (int renderMode READ renderMode WRITE setRenderMode) - Q_PROPERTY (bool grabMouse READ isGrabMouseEnabled WRITE setGrabMouseEnabled) - Q_PROPERTY (bool mouseRotate READ isMouseRotateEnabled WRITE setMouseRotateEnabled) - Q_PROPERTY (bool mouseSelection READ isMouseSelectionEnabled WRITE setMouseSelectionEnabled) - Q_PROPERTY (bool cameraOrbit READ isCameraOrbit WRITE setCameraOrbit) - Q_PROPERTY (bool hoverHalo READ isHoverHaloEnabled WRITE setHoverHaloEnabled) - Q_PROPERTY (QColor hoverHaloColor READ hoverHaloColor WRITE setHoverHaloColor) - Q_PROPERTY (float hoverHaloFillAlpha READ hoverHaloFillAlpha WRITE setHoverHaloFillAlpha) - Q_PROPERTY (bool selectionHalo READ isSelectionHaloEnabled WRITE setSelectionHaloEnabled) - Q_PROPERTY (QColor selectionHaloColor READ selectionHaloColor WRITE setSelectionHaloColor) - Q_PROPERTY (float selectionHaloFillAlpha READ selectionHaloFillAlpha WRITE setSelectionHaloFillAlpha) - Q_PROPERTY (Qt::MouseButton selectionButton READ selectionButton WRITE setSelectionButton) - Q_PROPERTY (Qt::KeyboardModifier selectionModifier READ selectionModifier WRITE setSelectionModifier) - Q_PROPERTY (Scene::SelectionMode selectionMode READ selectionMode WRITE setSelectionMode) + Q_PROPERTY(float lineWidth READ lineWidth WRITE setLineWidth) + Q_PROPERTY(float FOV READ FOV WRITE setFOV) + Q_PROPERTY(float depthStart READ depthStart WRITE setDepthStart) + Q_PROPERTY(float gamma READ gamma WRITE setGamma) + Q_PROPERTY(bool autoExposure READ autoExposure WRITE setAutoExposure) + Q_PROPERTY(QColor fogColor READ fogColor WRITE setFogColor) + Q_PROPERTY(bool fogEnabled READ isFogEnabled WRITE setFogEnabled) + Q_PROPERTY(float fogDensity READ fogDensity WRITE setFogDensity) + Q_PROPERTY(float fogDecay READ fogDecay WRITE setFogDecay) + Q_PROPERTY(int renderMode READ renderMode WRITE setRenderMode) + Q_PROPERTY(bool grabMouse READ isGrabMouseEnabled WRITE setGrabMouseEnabled) + Q_PROPERTY(bool mouseRotate READ isMouseRotateEnabled WRITE setMouseRotateEnabled) + Q_PROPERTY(bool mouseSelection READ isMouseSelectionEnabled WRITE setMouseSelectionEnabled) + Q_PROPERTY(bool cameraOrbit READ isCameraOrbit WRITE setCameraOrbit) + Q_PROPERTY(bool hoverHalo READ isHoverHaloEnabled WRITE setHoverHaloEnabled) + Q_PROPERTY(QColor hoverHaloColor READ hoverHaloColor WRITE setHoverHaloColor) + Q_PROPERTY(float hoverHaloFillAlpha READ hoverHaloFillAlpha WRITE setHoverHaloFillAlpha) + Q_PROPERTY(bool selectionHalo READ isSelectionHaloEnabled WRITE setSelectionHaloEnabled) + Q_PROPERTY(QColor selectionHaloColor READ selectionHaloColor WRITE setSelectionHaloColor) + Q_PROPERTY(float selectionHaloFillAlpha READ selectionHaloFillAlpha WRITE setSelectionHaloFillAlpha) + Q_PROPERTY(Qt::MouseButton selectionButton READ selectionButton WRITE setSelectionButton) + Q_PROPERTY(Qt::KeyboardModifier selectionModifier READ selectionModifier WRITE setSelectionModifier) + Q_PROPERTY(Scene::SelectionMode selectionMode READ selectionMode WRITE setSelectionMode) public: QGLView(); @@ -100,82 +100,89 @@ public: void stop(); void start(float freq = 0.); - float lineWidth() const {return lineWidth_;} - float FOV() const {return camera()->FOV();} - float depthStart() const {return camera()->depthStart();} - float currentFPS() const {return fps_;} - float gamma() const {return renderer_.gamma_;} - bool autoExposure() const {return renderer_.tone_proc.enabled;} - int maxAnisotropicLevel() const {return max_anisotropic;} - QString environmentMapFile() const {return renderer_.tex_env.fileHDR();} + float lineWidth() const { return lineWidth_; } + float FOV() const { return camera()->FOV(); } + float depthStart() const { return camera()->depthStart(); } + float currentFPS() const { return fps_; } + float gamma() const { return renderer_.gamma_; } + bool autoExposure() const { return renderer_.tone_proc.enabled; } + int maxAnisotropicLevel() const { return max_anisotropic; } + QString environmentMapFile() const { return renderer_.tex_env.fileHDR(); } - QColor fogColor() const {return fogColor_;} - float fogDensity() const {return fogDensity_;} - float fogDecay() const {return fogDecay_;} - bool isFogEnabled() const {return fogEnabled_;} - bool isLightEnabled() const {return lightEnabled_;} - bool isGrabMouseEnabled() const {return mouse.isGrabMouseEnabled();} - bool isMouseRotateEnabled() const {return mouse.isMouseRotateEnabled();} - bool isMouseSelectionEnabled() const {return mouse.isMouseSelectionEnabled();} - bool isCameraOrbit() const {return mouse.isCameraOrbit();} - bool isHoverHaloEnabled() const {return hoverHalo_;} - QColor hoverHaloColor() const {return hoverHaloColor_;} - float hoverHaloFillAlpha() const {return hoverHaloFill_;} - bool isSelectionHaloEnabled() const {return selectionHalo_;} - QColor selectionHaloColor() const {return selectionHaloColor_;} - float selectionHaloFillAlpha() const {return selectionHaloFill_;} + QColor fogColor() const { return fogColor_; } + float fogDensity() const { return fogDensity_; } + float fogDecay() const { return fogDecay_; } + bool isFogEnabled() const { return fogEnabled_; } + bool isLightEnabled() const { return lightEnabled_; } + bool isGrabMouseEnabled() const { return mouse.isGrabMouseEnabled(); } + bool isMouseRotateEnabled() const { return mouse.isMouseRotateEnabled(); } + bool isMouseSelectionEnabled() const { return mouse.isMouseSelectionEnabled(); } + bool isCameraOrbit() const { return mouse.isCameraOrbit(); } + bool isHoverHaloEnabled() const { return hoverHalo_; } + QColor hoverHaloColor() const { return hoverHaloColor_; } + float hoverHaloFillAlpha() const { return hoverHaloFill_; } + bool isSelectionHaloEnabled() const { return selectionHalo_; } + QColor selectionHaloColor() const { return selectionHaloColor_; } + float selectionHaloFillAlpha() const { return selectionHaloFill_; } - QVariant feature(Feature f) const {return features_.value(int(f));} - QVariant setFeature(Feature f, const QVariant & value) {QVariant ret = features_.value(int(f)); features_[int(f)] = value; return ret;} - bool isFeatureEnabled(Feature f) const {return features_[int(f)].toBool();} + QVariant feature(Feature f) const { return features_.value(int(f)); } + QVariant setFeature(Feature f, const QVariant & value) { + QVariant ret = features_.value(int(f)); + features_[int(f)] = value; + return ret; + } + bool isFeatureEnabled(Feature f) const { return features_[int(f)].toBool(); } - int renderMode() const {return (int)rmode;} - void setRenderMode(int mode) {rmode = (ObjectBase::RenderMode)mode;} + int renderMode() const { return (int)rmode; } + void setRenderMode(int mode) { rmode = (ObjectBase::RenderMode)mode; } - bool isServiceMode() const {return renderer_.edit_mode;} - void setServiceMode(bool yes) {renderer_.edit_mode = yes;} + bool isServiceMode() const { return renderer_.edit_mode; } + void setServiceMode(bool yes) { renderer_.edit_mode = yes; } - Scene::SelectionMode selectionMode() const {return scene_->selectionMode();} - Qt::MouseButton selectionButton() const {return mouse.selectionButton();} - Qt::KeyboardModifier selectionModifier() const {return mouse.selectionModifier();} + Scene::SelectionMode selectionMode() const { return scene_->selectionMode(); } + Qt::MouseButton selectionButton() const { return mouse.selectionButton(); } + Qt::KeyboardModifier selectionModifier() const { return mouse.selectionModifier(); } - void setSelectionMode(Scene::SelectionMode m) {scene_->setSelectionMode(m);} - void setSelectionButton(Qt::MouseButton v) {mouse.setSelectionButton(v);} - void setSelectionModifier(Qt::KeyboardModifier v) {mouse.setSelectionModifier(v);} + void setSelectionMode(Scene::SelectionMode m) { scene_->setSelectionMode(m); } + void setSelectionButton(Qt::MouseButton v) { mouse.setSelectionButton(v); } + void setSelectionModifier(Qt::KeyboardModifier v) { mouse.setSelectionModifier(v); } - void selectObject(ObjectBase * o, bool add_to_selection = false) {scene_->selectObject(o, add_to_selection);} - void clearSelection() {scene_->clearSelection();} - ObjectBaseList selectedObjects(bool top_only = false) const {return scene_->selectedObjects(top_only);} - QList selectedLights() const; - QList selectedCameras() const; - ObjectBase * selectedObject() const {return scene_->selectedObject();} + void selectObject(ObjectBase * o, bool add_to_selection = false) { scene_->selectObject(o, add_to_selection); } + void clearSelection() { scene_->clearSelection(); } + ObjectBaseList selectedObjects(bool top_only = false) const { return scene_->selectedObjects(top_only); } + QList selectedLights() const; + QList selectedCameras() const; + ObjectBase * selectedObject() const { return scene_->selectedObject(); } - TextureManager * textureManager() {return renderer_.textures_manager;} - void reloadTextures() {renderer_.markReloadTextures();} + TextureManager * textureManager() { return renderer_.textures_manager; } + void reloadTextures() { renderer_.markReloadTextures(); } - Scene * scene() {return scene_;} + Scene * scene() { return scene_; } void focusOn(const Box3D & bb); - void setCameraLightMode(CameraLightMode m) {renderer_.setCameraLightMode(m);} - CameraLightMode cameraLightMode() const {return (CameraLightMode)renderer_.cameraLightMode();} + void setCameraLightMode(CameraLightMode m) { renderer_.setCameraLightMode(m); } + CameraLightMode cameraLightMode() const { return (CameraLightMode)renderer_.cameraLightMode(); } - Camera * camera() {return camera_;} - const Camera * camera() const {return camera_;} - void setCamera(Camera * camera) {camera_ = camera;} - void setDefaultCamera() {camera_ = default_camera;} - bool isDefaultCamera() const {return camera_ == default_camera;} + Camera * camera() { return camera_; } + const Camera * camera() const { return camera_; } + void setCamera(Camera * camera) { camera_ = camera; } + void setDefaultCamera() { camera_ = default_camera; } + bool isDefaultCamera() const { return camera_ == default_camera; } QByteArray saveCamera(); void restoreCamera(const QByteArray & ba); QByteArray saveFeatures(); void restoreFeatures(const QByteArray & ba); - QImage materialThumbnail(Material * m) {return renderer_.materialThumbnail(m);} - void setCurrentAction(RendererService::HandleAction ha) {renderer_.rend_service.setCurrentAction(ha);} - void setContextActions(QList al) {context_menu.clear(); context_menu.addActions(al);} - void popupMenu(const QPoint &pos, QAction *at = nullptr) {context_menu.popup(pos, at);} - void setGrabImage(bool on) {renderer_.setGrabImage(on);} - bool isGrabImage() const {return renderer_.isGrabImage();} - QImage getImage() const {return renderer_.getImage();} + QImage materialThumbnail(Material * m) { return renderer_.materialThumbnail(m); } + void setCurrentAction(RendererService::HandleAction ha) { renderer_.rend_service.setCurrentAction(ha); } + void setContextActions(QList al) { + context_menu.clear(); + context_menu.addActions(al); + } + void popupMenu(const QPoint & pos, QAction * at = nullptr) { context_menu.popup(pos, at); } + void setGrabImage(bool on) { renderer_.setGrabImage(on); } + bool isGrabImage() const { return renderer_.isGrabImage(); } + QImage getImage() const { return renderer_.getImage(); } GLfloat aspect, iaspect; Renderer renderer_; @@ -184,15 +191,15 @@ protected: void render(); void resizeEvent(QResizeEvent * e); - void timerEvent(QTimerEvent * ); + void timerEvent(QTimerEvent *); void initialize(); void resizeGL(int width, int height); - void mousePressEvent(QMouseEvent * e) {mouse.mousePressEvent(e);} - void mouseMoveEvent(QMouseEvent * e) {mouse.mouseMoveEvent(e);} - void mouseReleaseEvent(QMouseEvent * e) {mouse.mouseReleaseEvent(e);} - void wheelEvent(QWheelEvent * e) {mouse.wheelEvent(e);} - void mouseDoubleClickEvent(QMouseEvent * e) {mouse.mouseDoubleClickEvent(e);} - void leaveEvent(QEvent * ); + void mousePressEvent(QMouseEvent * e) { mouse.mousePressEvent(e); } + void mouseMoveEvent(QMouseEvent * e) { mouse.mouseMoveEvent(e); } + void mouseReleaseEvent(QMouseEvent * e) { mouse.mouseReleaseEvent(e); } + void wheelEvent(QWheelEvent * e) { mouse.wheelEvent(e); } + void mouseDoubleClickEvent(QMouseEvent * e) { mouse.mouseDoubleClickEvent(e); } + void leaveEvent(QEvent *); void keyPressEvent(QKeyEvent * e); void keyReleaseEvent(QKeyEvent * e); @@ -205,7 +212,7 @@ private: bool setupViewport(); Scene * scene_; - Camera * camera_, * default_camera; + Camera *camera_, *default_camera; MouseController mouse; QMenu context_menu; QSet keys_; @@ -229,30 +236,33 @@ private slots: void __objectDeleted(ObjectBase * o); public slots: - void setLineWidth(const float & arg) {lineWidth_ = arg;} - void setFOV(const float & arg) {camera()->setFOV(arg);} - void setDepthStart(const float & arg) {camera()->setDepthStart(arg);} - void setGamma(const float & arg) {renderer_.gamma_ = arg;} - void setAutoExposure(bool arg) {renderer_.tone_proc.enabled = arg;} - void setEnvironmentMapFile(QString file) {renderer_.tex_env.setFileHDR(file); renderer_.recreateMaterialThumbnails(true);} - void setFogColor(const QColor & arg) {fogColor_ = arg;} - void setFogDensity(const float & arg) {fogDensity_ = arg;} - void setFogDecay(const float & arg) {fogDecay_ = arg;} - void setFogEnabled(const bool & arg) {fogEnabled_ = arg;} - void setLightEnabled(const bool & arg) {lightEnabled_ = arg;} - void setGrabMouseEnabled(const bool & arg) {mouse.setGrabMouseEnabled(arg);} - void setMouseRotateEnabled(const bool & arg) {mouse.setMouseRotateEnabled(arg);} - void setMouseSelectionEnabled(const bool & arg) {mouse.setMouseSelectionEnabled(arg);} - void setCustomMouseMove(const bool & arg) {mouse.setCustomMouseMove(arg);} - void setCameraOrbit(const bool & arg) {mouse.setCameraOrbit(arg);} - void setHoverHaloEnabled(const bool & arg) {hoverHalo_ = arg;} - void setHoverHaloColor(const QColor & arg) {hoverHaloColor_ = arg;} - void setHoverHaloFillAlpha(const float & arg) {hoverHaloFill_ = arg;} - void setSelectionHaloEnabled(const bool & arg) {selectionHalo_ = arg;} - void setSelectionHaloColor(const QColor & arg) {selectionHaloColor_ = arg;} - void setSelectionHaloFillAlpha(const float & arg) {selectionHaloFill_ = arg;} + void setLineWidth(const float & arg) { lineWidth_ = arg; } + void setFOV(const float & arg) { camera()->setFOV(arg); } + void setDepthStart(const float & arg) { camera()->setDepthStart(arg); } + void setGamma(const float & arg) { renderer_.gamma_ = arg; } + void setAutoExposure(bool arg) { renderer_.tone_proc.enabled = arg; } + void setEnvironmentMapFile(QString file) { + renderer_.tex_env.setFileHDR(file); + renderer_.recreateMaterialThumbnails(true); + } + void setFogColor(const QColor & arg) { fogColor_ = arg; } + void setFogDensity(const float & arg) { fogDensity_ = arg; } + void setFogDecay(const float & arg) { fogDecay_ = arg; } + void setFogEnabled(const bool & arg) { fogEnabled_ = arg; } + void setLightEnabled(const bool & arg) { lightEnabled_ = arg; } + void setGrabMouseEnabled(const bool & arg) { mouse.setGrabMouseEnabled(arg); } + void setMouseRotateEnabled(const bool & arg) { mouse.setMouseRotateEnabled(arg); } + void setMouseSelectionEnabled(const bool & arg) { mouse.setMouseSelectionEnabled(arg); } + void setCustomMouseMove(const bool & arg) { mouse.setCustomMouseMove(arg); } + void setCameraOrbit(const bool & arg) { mouse.setCameraOrbit(arg); } + void setHoverHaloEnabled(const bool & arg) { hoverHalo_ = arg; } + void setHoverHaloColor(const QColor & arg) { hoverHaloColor_ = arg; } + void setHoverHaloFillAlpha(const float & arg) { hoverHaloFill_ = arg; } + void setSelectionHaloEnabled(const bool & arg) { selectionHalo_ = arg; } + void setSelectionHaloColor(const QColor & arg) { selectionHaloColor_ = arg; } + void setSelectionHaloFillAlpha(const float & arg) { selectionHaloFill_ = arg; } - void reloadShaders() {renderer_.reloadShaders();} + void reloadShaders() { renderer_.reloadShaders(); } signals: void glBeginPaint(); @@ -273,7 +283,7 @@ signals: void selectionChanged(); void objectsPositionChanged(); void materialsChanged(); - void materialThumbnailCreated(Material*); + void materialThumbnailCreated(Material *); void doubleClick(); }; diff --git a/qglview_test/main.cpp b/qglview_test/main.cpp index eb43d61..51d162e 100644 --- a/qglview_test/main.cpp +++ b/qglview_test/main.cpp @@ -1,25 +1,26 @@ /* - QGLViewWindow - Ivan Pelipenko peri4ko@yandex.ru + QGLViewWindow + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ +#include "qglview_window.h" + #include #include #include -#include "qglview_window.h" int main(int argc, char ** argv) { QApplication a(argc, argv); @@ -28,7 +29,7 @@ int main(int argc, char ** argv) { w.show(); QStringList al(a.arguments()); al.pop_front(); - foreach (QString s, al) + foreach(QString s, al) w.loadFile(s); return a.exec(); } diff --git a/qglview_test/qglview_window.cpp b/qglview_test/qglview_window.cpp index 217ac09..804af9f 100644 --- a/qglview_test/qglview_window.cpp +++ b/qglview_test/qglview_window.cpp @@ -1,29 +1,31 @@ /* - QGLViewWindow - Ivan Pelipenko peri4ko@yandex.ru + QGLViewWindow + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "qglview_window.h" -#include "renderer.h" -#include "glwidget.h" + #include "gltexture_manager.h" +#include "glwidget.h" +#include "qad_types.h" +#include "renderer.h" + #include #include #include -#include "qad_types.h" QGLViewWindow::QGLViewWindow(QWidget * parent): QMainWindow(parent), Ui::QGLViewWindow() { @@ -33,11 +35,11 @@ QGLViewWindow::QGLViewWindow(QWidget * parent): QMainWindow(parent), Ui::QGLView extensions = "All(" + supportedFormats().join(" ") + " *.qgl)"; extensions += ";;QGLEngine(*.qgl)"; - - //view->view()->camera()->setPos(QVector3D(2, 2, 2)); - //view->view()->camera()->setAim(QVector3D()); - //view->view()->camera()->flyToDistance(2.); -// view->setFrameShape(QFrame::NoFrame); + + // view->view()->camera()->setPos(QVector3D(2, 2, 2)); + // view->view()->camera()->setAim(QVector3D()); + // view->view()->camera()->flyToDistance(2.); + // view->setFrameShape(QFrame::NoFrame); view->view()->setMouseRotateEnabled(true); view->view()->setMouseSelectionEnabled(true); view->view()->setSelectionHaloEnabled(true); @@ -70,10 +72,10 @@ QGLViewWindow::QGLViewWindow(QWidget * parent): QMainWindow(parent), Ui::QGLView spinDOFSpeed->setValue(view->view()->feature(QGLView::qglDepthOfFieldAutoFocusSpeed).toDouble()); view->view()->start(-1); - startTimer(1000/60); + startTimer(1000 / 60); connect(view->view(), SIGNAL(keyEvent(Qt::Key, Qt::KeyboardModifiers)), this, SLOT(view_keyEvent(Qt::Key, Qt::KeyboardModifiers))); - //connect(matEditor, SIGNAL(changed()), this, SLOT(materialChanged())); + // connect(matEditor, SIGNAL(changed()), this, SLOT(materialChanged())); sceneTree->assignQGLView(view->view()); matEditor->assignQGLView(view->view()); objectEditor->assignQGLView(view->view()); @@ -82,14 +84,14 @@ QGLViewWindow::QGLViewWindow(QWidget * parent): QMainWindow(parent), Ui::QGLView session.load(); - //loadFile("axis.DAE.qgl"); - //matEditor->setMaterial(const_cast(view->view()->scene()->rootObject()->child(0))->material()); + // loadFile("axis.DAE.qgl"); + // matEditor->setMaterial(const_cast(view->view()->scene()->rootObject()->child(0))->material()); /*Scene * sc = loadScene("truck.obj"); //view->view()->scene()->addScene(sc); sc->rootObject()->moveY(-8); for (int i = 0; i < 7; ++i) { - sc->rootObject()->moveY(2); - view->view()->scene()->addScene(sc); + sc->rootObject()->moveY(2); + view->view()->scene()->addScene(sc); } //view->view()->scene()->dump(); delete sc;*/ @@ -98,7 +100,7 @@ QGLViewWindow::QGLViewWindow(QWidget * parent): QMainWindow(parent), Ui::QGLView QGLViewWindow::~QGLViewWindow() { session.save(); - //delete ps; + // delete ps; } @@ -111,8 +113,8 @@ void QGLViewWindow::changeEvent(QEvent * e) { } -void QGLViewWindow::timerEvent(QTimerEvent * ) { - //static double t = 0.; +void QGLViewWindow::timerEvent(QTimerEvent *) { + // static double t = 0.; //((RendererSimple*)(view->view()->renderer()))->mpos = view->view()->mapFromGlobal(QCursor::pos()); statusBar()->showMessage(QString("FPS: %1").arg(QString::number(view->view()->currentFPS(), 'f', 2))); } @@ -123,15 +125,18 @@ void QGLViewWindow::loadFile(const QString & path, bool import) { QApplication::setOverrideCursor(Qt::WaitCursor); QFileInfo fi(path); Scene * s = nullptr; - if (fi.suffix().toLower() == "qgl") s = loadFromQGLFile(path); - else s = loadScene(path); + if (fi.suffix().toLower() == "qgl") + s = loadFromQGLFile(path); + else + s = loadScene(path); QApplication::restoreOverrideCursor(); if (!s) { QMessageBox::critical(this, "Import", "Can`t load " + path + "!"); return; } s->setName(fi.baseName()); - if (import) view->scene()->addScene(s); + if (import) + view->scene()->addScene(s); else { TextureManager::clearSearchPathes(); view->scene()->assignFrom(s); @@ -143,9 +148,9 @@ void QGLViewWindow::loadFile(const QString & path, bool import) { void QGLViewWindow::on_actionReset_triggered() { - ///view->view()->removeObject(axis, false); + /// view->view()->removeObject(axis, false); view->view()->scene()->clear(); - ///view->view()->addObject(axis); + /// view->view()->addObject(axis); } @@ -153,7 +158,7 @@ void QGLViewWindow::on_actionImport_triggered() { QStringList fl = QFileDialog::getOpenFileNames(this, "Select files", prev_path, extensions); if (fl.isEmpty()) return; prev_path = fl.back(); - foreach (QString f, fl) + foreach(QString f, fl) loadFile(f, true); } @@ -161,8 +166,7 @@ void QGLViewWindow::on_actionImport_triggered() { void QGLViewWindow::on_actionSave_triggered() { QString f = QFileDialog::getSaveFileName(this, "Select file", prev_path, "QGLView(*.qgl)"); if (f.isEmpty()) return; - if (f.right(4).toLower() != ".qgl") - f += ".qgl"; + if (f.right(4).toLower() != ".qgl") f += ".qgl"; prev_path = f; QApplication::setOverrideCursor(Qt::WaitCursor); saveToQGLFile(f, view->scene()); @@ -175,11 +179,10 @@ void QGLViewWindow::on_actionSaveSelected_triggered() { if (!sel_obj) return; QString f = QFileDialog::getSaveFileName(this, "Select file", prev_path, "QGLView(*.qgl)"); if (f.isEmpty()) return; - if (f.right(4).toLower() != ".qgl") - f += ".qgl"; + if (f.right(4).toLower() != ".qgl") f += ".qgl"; prev_path = f; QApplication::setOverrideCursor(Qt::WaitCursor); - ///saveToQGLFile(f, sel_obj); + /// saveToQGLFile(f, sel_obj); QApplication::restoreOverrideCursor(); } @@ -193,10 +196,9 @@ void QGLViewWindow::on_actionOpen_triggered() { void QGLViewWindow::view_keyEvent(Qt::Key k, Qt::KeyboardModifiers m) { - //qDebug() << k; + // qDebug() << k; double spd = 0.2; - if (m.testFlag(Qt::ShiftModifier)) - spd = 0.5; + if (m.testFlag(Qt::ShiftModifier)) spd = 0.5; switch (k) { case Qt::Key_W: view->view()->camera()->moveForward(spd); break; case Qt::Key_S: view->view()->camera()->moveBackward(spd); break; @@ -208,37 +210,37 @@ void QGLViewWindow::view_keyEvent(Qt::Key k, Qt::KeyboardModifiers m) { void QGLViewWindow::on_actionArrow_triggered(bool on) { - actionArrow ->setChecked(true); - actionMove ->setChecked(false); + actionArrow->setChecked(true); + actionMove->setChecked(false); actionRotate->setChecked(false); - actionScale ->setChecked(false); + actionScale->setChecked(false); view->view()->setCurrentAction(RendererService::haNoAction); } void QGLViewWindow::on_actionMove_triggered(bool on) { - actionArrow ->setChecked(false); - actionMove ->setChecked(true); + actionArrow->setChecked(false); + actionMove->setChecked(true); actionRotate->setChecked(false); - actionScale ->setChecked(false); + actionScale->setChecked(false); view->view()->setCurrentAction(RendererService::haMove); } void QGLViewWindow::on_actionRotate_triggered(bool on) { - actionArrow ->setChecked(false); - actionMove ->setChecked(false); + actionArrow->setChecked(false); + actionMove->setChecked(false); actionRotate->setChecked(true); - actionScale ->setChecked(false); + actionScale->setChecked(false); view->view()->setCurrentAction(RendererService::haRotate); } void QGLViewWindow::on_actionScale_triggered(bool on) { - actionArrow ->setChecked(false); - actionMove ->setChecked(false); + actionArrow->setChecked(false); + actionMove->setChecked(false); actionRotate->setChecked(false); - actionScale ->setChecked(true); + actionScale->setChecked(true); view->view()->setCurrentAction(RendererService::haScale); } diff --git a/qglview_test/qglview_window.h b/qglview_test/qglview_window.h index 304353f..dc31c97 100644 --- a/qglview_test/qglview_window.h +++ b/qglview_test/qglview_window.h @@ -1,47 +1,50 @@ /* - QGLViewWindow - Ivan Pelipenko peri4ko@yandex.ru + QGLViewWindow + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef QGLVIEWWINDOW_H #define QGLVIEWWINDOW_H -#include -#include -#include -#include -#include +#include "formats/loader_assimp.h" +#include "formats/loader_qgl.h" +#include "session_manager.h" +#include "ui_qglview_window.h" + #include +#include +#include +#include +#include #include -#include +#include #include #include -#include -#include "ui_qglview_window.h" -#include "formats/loader_qgl.h" -#include "formats/loader_assimp.h" -#include "session_manager.h" -//#include "renderer_rt.h" +#include +#include +// #include "renderer_rt.h" #include "qglview.h" #include "ui_qglview_window.h" -class QGLViewWindow: public QMainWindow, public Ui::QGLViewWindow -{ +class QGLViewWindow + : public QMainWindow + , public Ui::QGLViewWindow { Q_OBJECT + public: QGLViewWindow(QWidget * parent = 0); ~QGLViewWindow(); @@ -51,41 +54,41 @@ public: private: // Qt`s overloaded void changeEvent(QEvent * e); - void timerEvent(QTimerEvent * ); + void timerEvent(QTimerEvent *); QString extensions; QTranslator translator; QString prev_path; - //GLPrimitiveCube * box; + // GLPrimitiveCube * box; Material m; SessionManager session; bool isChanged; private slots: - void on_groupShadows_clicked(bool val) {view->view()->setFeature(QGLView::qglShadowsEnabled, val);} - void on_groupEyeAccomodation_clicked(bool val) {view->view()->setFeature(QGLView::qglEyeAccomodationEnabled, val);} - void on_groupBloom_clicked(bool val) {view->view()->setFeature(QGLView::qglBloomEnabled, val);} - void on_groupMotionBlur_clicked(bool val) {view->view()->setFeature(QGLView::qglMotionBlurEnabled, val);} - void on_groupReflections_clicked(bool val) {view->view()->setFeature(QGLView::qglReflectionsEnabled, val);} - void on_checkSoftShadows_clicked(bool val) {view->view()->setFeature(QGLView::qglShadowsSoftEnabled, val);} - void on_groupSSAO_clicked(bool val) {view->view()->setFeature(QGLView::qglSSAOEnabled, val);} - void on_groupDOF_clicked(bool val) {view->view()->setFeature(QGLView::qglDepthOfFieldEnabled, val);} - void on_checkDOFAutoFocus_clicked(bool val) {view->view()->setFeature(QGLView::qglDepthOfFieldAutoFocusEnabled, val);} - void on_spinDOFFocus_valueChanged(double val) {view->view()->setFeature(QGLView::qglDepthOfFieldFocus, val);} - void on_spinDOFDiaphragm_valueChanged(double val) {view->view()->setFeature(QGLView::qglDepthOfFieldDiaphragm, val);} - void on_spinDOFSpeed_valueChanged(double val) {view->view()->setFeature(QGLView::qglDepthOfFieldAutoFocusSpeed, val);} - void on_spinAccom_valueChanged(double val) {view->view()->setFeature(QGLView::qglEyeAccomodationTime, val);} - void on_spinAccomMS_valueChanged(double val) {view->view()->setFeature(QGLView::qglEyeAccomodationMaxSpeed, val);} - void on_checkReflectionsBlur_clicked(bool val) {view->view()->setFeature(QGLView::qglReflectionsBlur, val);} - void on_spinShadowmapSize_valueChanged(double val) {view->view()->setFeature(QGLView::qglShadowsMapSize, val);} - void on_spinMotionBlurFactor_valueChanged(double val) {view->view()->setFeature(QGLView::qglMotionBlurFactor, val);} - void on_spinMotionBlurSteps_valueChanged(int val) {view->view()->setFeature(QGLView::qglMotionBlurSteps, val);} - void on_spinBloomFactor_valueChanged(double val) {view->view()->setFeature(QGLView::qglBloomFactor, val);} - void on_spinBloomRadius_valueChanged(int val) {view->view()->setFeature(QGLView::qglBloomRadius, val);} - void on_spinBloomThreshold_valueChanged(double val) {view->view()->setFeature(QGLView::qglBloomThreshold, val);} - void on_spinSSAORadius_valueChanged(int val) {view->view()->setFeature(QGLView::qglSSAORadius, val);} + void on_groupShadows_clicked(bool val) { view->view()->setFeature(QGLView::qglShadowsEnabled, val); } + void on_groupEyeAccomodation_clicked(bool val) { view->view()->setFeature(QGLView::qglEyeAccomodationEnabled, val); } + void on_groupBloom_clicked(bool val) { view->view()->setFeature(QGLView::qglBloomEnabled, val); } + void on_groupMotionBlur_clicked(bool val) { view->view()->setFeature(QGLView::qglMotionBlurEnabled, val); } + void on_groupReflections_clicked(bool val) { view->view()->setFeature(QGLView::qglReflectionsEnabled, val); } + void on_checkSoftShadows_clicked(bool val) { view->view()->setFeature(QGLView::qglShadowsSoftEnabled, val); } + void on_groupSSAO_clicked(bool val) { view->view()->setFeature(QGLView::qglSSAOEnabled, val); } + void on_groupDOF_clicked(bool val) { view->view()->setFeature(QGLView::qglDepthOfFieldEnabled, val); } + void on_checkDOFAutoFocus_clicked(bool val) { view->view()->setFeature(QGLView::qglDepthOfFieldAutoFocusEnabled, val); } + void on_spinDOFFocus_valueChanged(double val) { view->view()->setFeature(QGLView::qglDepthOfFieldFocus, val); } + void on_spinDOFDiaphragm_valueChanged(double val) { view->view()->setFeature(QGLView::qglDepthOfFieldDiaphragm, val); } + void on_spinDOFSpeed_valueChanged(double val) { view->view()->setFeature(QGLView::qglDepthOfFieldAutoFocusSpeed, val); } + void on_spinAccom_valueChanged(double val) { view->view()->setFeature(QGLView::qglEyeAccomodationTime, val); } + void on_spinAccomMS_valueChanged(double val) { view->view()->setFeature(QGLView::qglEyeAccomodationMaxSpeed, val); } + void on_checkReflectionsBlur_clicked(bool val) { view->view()->setFeature(QGLView::qglReflectionsBlur, val); } + void on_spinShadowmapSize_valueChanged(double val) { view->view()->setFeature(QGLView::qglShadowsMapSize, val); } + void on_spinMotionBlurFactor_valueChanged(double val) { view->view()->setFeature(QGLView::qglMotionBlurFactor, val); } + void on_spinMotionBlurSteps_valueChanged(int val) { view->view()->setFeature(QGLView::qglMotionBlurSteps, val); } + void on_spinBloomFactor_valueChanged(double val) { view->view()->setFeature(QGLView::qglBloomFactor, val); } + void on_spinBloomRadius_valueChanged(int val) { view->view()->setFeature(QGLView::qglBloomRadius, val); } + void on_spinBloomThreshold_valueChanged(double val) { view->view()->setFeature(QGLView::qglBloomThreshold, val); } + void on_spinSSAORadius_valueChanged(int val) { view->view()->setFeature(QGLView::qglSSAORadius, val); } - void on_actionExit_triggered() {close();} + void on_actionExit_triggered() { close(); } void on_actionReset_triggered(); void on_actionImport_triggered(); void on_actionSave_triggered(); @@ -94,16 +97,16 @@ private slots: void view_keyEvent(Qt::Key k, Qt::KeyboardModifiers m); - void on_pushButton_2_clicked() {view->view()->reloadShaders();} + void on_pushButton_2_clicked() { view->view()->reloadShaders(); } void on_actionArrow_triggered(bool on); void on_actionMove_triggered(bool on); void on_actionRotate_triggered(bool on); void on_actionScale_triggered(bool on); - void on_colorFogBack_colorChanged(const QColor & v) {view->view()->setFogColor(v);} - void on_spinFogDensity_valueChanged(double v) {view->view()->setFogDensity(v);} - void on_spinFogDecay_valueChanged(double v) {view->view()->setFogDecay(v);} + void on_colorFogBack_colorChanged(const QColor & v) { view->view()->setFogColor(v); } + void on_spinFogDensity_valueChanged(double v) { view->view()->setFogDensity(v); } + void on_spinFogDecay_valueChanged(double v) { view->view()->setFogDecay(v); } void on_pushButton_3_clicked(); @@ -113,7 +116,6 @@ signals: private: QMatrix4x4 cam_mat; - }; #endif // QGLVIEWWINDOW_H diff --git a/renderer.cpp b/renderer.cpp index 8b375e6..6c4bda1 100644 --- a/renderer.cpp +++ b/renderer.cpp @@ -1,60 +1,68 @@ /* - QGL Renderer - Ivan Pelipenko peri4ko@yandex.ru + QGL Renderer + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #define GL_GLEXT_PROTOTYPES +#include "renderer.h" + +#include "glmesh.h" +#include "glshaders.h" +#include "gltexture_manager.h" +#include "qglview.h" + #include #include -#include "renderer.h" -#include "qglview.h" -#include "glmesh.h" -#include "gltexture_manager.h" -#include "glshaders.h" using namespace QGLEngineShaders; -Renderer::Renderer(QGLView * view_): RendererBase(view_), - fbo_ds (view_, QVector() << GL_RGBA16F << GL_RGBA32F << GL_RGBA16F << GL_RGBA16F << GL_RGBA16F), - fbo_out (view_, obrBuffersCount, false, GL_RGBA16F), - rend_mat(this), rend_service(this), rend_selection(this), tone_proc(this), tex_env(view_, 512) { - quad = Primitive::plane(2., 2.); - cam_light = new Light(); +Renderer::Renderer(QGLView * view_) + : RendererBase(view_) + , fbo_ds(view_, QVector() << GL_RGBA16F << GL_RGBA32F << GL_RGBA16F << GL_RGBA16F << GL_RGBA16F) + , fbo_out(view_, obrBuffersCount, false, GL_RGBA16F) + , rend_mat(this) + , rend_service(this) + , rend_selection(this) + , tone_proc(this) + , tex_env(view_, 512) { + quad = Primitive::plane(2., 2.); + cam_light = new Light(); cam_light->intensity = 0.75; cam_light->setName("Camera_Light"); - shader_files[srSelectionFill ] = "selection.glsl"; - shader_files[srSelectionHalo ] = "selection_halo.glsl"; + shader_files[srSelectionFill] = "selection.glsl"; + shader_files[srSelectionHalo] = "selection_halo.glsl"; shader_files[srSelectionApply] = "selection_apply.glsl"; shader_files[srSelectionFrame] = "selection_frame.glsl"; - shader_files[srServiceFill ] = "service_fill.glsl"; - shader_files[srServiceFrame] = "service_frame.glsl"; - shader_files[srServiceLine ] = "service_line.glsl"; + shader_files[srServiceFill] = "service_fill.glsl"; + shader_files[srServiceFrame] = "service_frame.glsl"; + shader_files[srServiceLine] = "service_line.glsl"; - shader_files[srGeometryPass ] = "ds_geom.glsl"; - shader_files[srLightOmniPass] = "ds_light.glsl"; - shader_files[srLightSpotPass] = "ds_light.glsl"; shader_defines[srLightSpotPass] << "SPOT"; - shader_files[srFinalPass ] = "ds_final.glsl"; - shader_files[srTonemapPass ] = "ds_tonemap.glsl"; + shader_files[srGeometryPass] = "ds_geom.glsl"; + shader_files[srLightOmniPass] = "ds_light.glsl"; + shader_files[srLightSpotPass] = "ds_light.glsl"; + shader_defines[srLightSpotPass] << "SPOT"; + shader_files[srFinalPass] = "ds_final.glsl"; + shader_files[srTonemapPass] = "ds_tonemap.glsl"; edit_mode = need_init_shaders = true; - camera_light_mode = QGLView::clmAuto; + camera_light_mode = QGLView::clmAuto; } @@ -94,8 +102,8 @@ void Renderer::resize(int width, int height) { rend_mat.resize(width, height); rend_service.resize(width, height); rend_selection.resize(width, height); - fbo_ds .resize(width, height); - fbo_out .resize(width, height); + fbo_ds.resize(width, height); + fbo_out.resize(width, height); tone_proc.resize(); } @@ -108,14 +116,16 @@ void Renderer::reloadShaders() { shader_fxaa = nullptr; if (tone_proc.shader_sum) delete tone_proc.shader_sum; tone_proc.shader_sum = nullptr; - QString dir = ":shaders/"; + QString dir = ":shaders/"; while (it.hasNext()) { it.next(); loadShadersMulti(shaders[it.key()], dir + it.value(), true, shader_defines.value(it.key())); } loadShadersMulti(tone_proc.shader_sum, dir + "sum.glsl", false); QStringList fxaa_defs; - fxaa_defs << "FXAA_PC 1" << "FXAA_GLSL_130 1" << "FXAA_QUALITY__PRESET 15"; + fxaa_defs << "FXAA_PC 1" + << "FXAA_GLSL_130 1" + << "FXAA_QUALITY__PRESET 15"; loadShaders(shader_fxaa, QStringList() << (dir + "fxaa.vert") << (dir + "fxaa.frag"), true, fxaa_defs); need_init_shaders = true; view->scene()->setLightsChanged(); @@ -145,24 +155,24 @@ bool Renderer::bindShader(QOpenGLShaderProgram * sp) { void Renderer::initShaders() { if (!need_init_shaders) return; need_init_shaders = false; - initUniformBuffer(shaders.value(srGeometryPass ), &buffer_materials , bpMaterials , "QGLMaterialData" ); - initUniformBuffer(shaders.value(srLightOmniPass), &buffer_materials , bpMaterials , "QGLMaterialData" ); - initUniformBuffer(shaders.value(srLightOmniPass), &buffer_lights , bpLightParameters, "QGLLightParameterData"); - initUniformBuffer(shaders.value(srLightOmniPass), &buffer_lights_pos, bpLightPositions , "QGLLightPositionData" ); - initUniformBuffer(shaders.value(srLightSpotPass), &buffer_materials , bpMaterials , "QGLMaterialData" ); - initUniformBuffer(shaders.value(srLightSpotPass), &buffer_lights , bpLightParameters, "QGLLightParameterData"); - initUniformBuffer(shaders.value(srLightSpotPass), &buffer_lights_pos, bpLightPositions , "QGLLightPositionData" ); - ShaderRole roles[] = {srLightOmniPass, srLightSpotPass}; + initUniformBuffer(shaders.value(srGeometryPass), &buffer_materials, bpMaterials, "QGLMaterialData"); + initUniformBuffer(shaders.value(srLightOmniPass), &buffer_materials, bpMaterials, "QGLMaterialData"); + initUniformBuffer(shaders.value(srLightOmniPass), &buffer_lights, bpLightParameters, "QGLLightParameterData"); + initUniformBuffer(shaders.value(srLightOmniPass), &buffer_lights_pos, bpLightPositions, "QGLLightPositionData"); + initUniformBuffer(shaders.value(srLightSpotPass), &buffer_materials, bpMaterials, "QGLMaterialData"); + initUniformBuffer(shaders.value(srLightSpotPass), &buffer_lights, bpLightParameters, "QGLLightParameterData"); + initUniformBuffer(shaders.value(srLightSpotPass), &buffer_lights_pos, bpLightPositions, "QGLLightPositionData"); + ShaderRole roles[] = {srLightOmniPass, srLightSpotPass}; QOpenGLShaderProgram * prog = 0; for (int p = 0; p < 2; ++p) { if (!bindShader(roles[p], &prog)) continue; for (int i = 0; i < 5; ++i) prog->setUniformValue(QString("tex_%1").arg(i).toLatin1().constData(), i); prog->setUniformValue("tex_coeffs[0]", (int)Renderer::dbrBuffersCount); - prog->setUniformValue("tex_env", (int)Renderer::dbrBuffersCount+1); + prog->setUniformValue("tex_env", (int)Renderer::dbrBuffersCount + 1); } if (bindShader(srFinalPass, &prog)) { - prog->setUniformValue("tex_g1" , 0); + prog->setUniformValue("tex_g1", 0); prog->setUniformValue("tex_s_0", 1); prog->setUniformValue("tex_s_1", 2); prog->setUniformValue("tex_t_0", 3); @@ -190,23 +200,22 @@ void Renderer::fillObjectsBuffer(const ObjectBaseList & ol, RenderPass pass) { ObjectBase * o = ol[i]; if (o->material()) { so.material = o->material()->_index; - so.color = QVector4D(1,1,1,1); + so.color = QVector4D(1, 1, 1, 1); } else { so.material = 0; - so.color = QColor2QVector(o->color()); + so.color = QColor2QVector(o->color()); } so.object_id = o->id(); o->worldTransform().transposed().copyDataTo(so.modelmatrix); - //qDebug() << "load obj" << o->name() << o->worldTransform(); + // qDebug() << "load obj" << o->name() << o->worldTransform(); } - //qDebug() << "fillObjectsBuffer" << ol.size(); - + // qDebug() << "fillObjectsBuffer" << ol.size(); } void Renderer::renderObjects(Scene & scene, RenderPass pass) { QOpenGLExtraFunctions * f = view; - QMapIterator it(scene.geometries_used[pass]); + QMapIterator it(scene.geometries_used[pass]); bool emit_pos_change = false; while (it.hasNext()) { it.next(); @@ -215,16 +224,16 @@ void Renderer::renderObjects(Scene & scene, RenderPass pass) { mesh->setObjectsChanged(pass, false); emit_pos_change = true; fillObjectsBuffer(it.value(), pass); - //qDebug() << "loadObjects" << pass << cur_objects_.size(); + // qDebug() << "loadObjects" << pass << cur_objects_.size(); mesh->loadObjects(f, cur_objects_, pass); } if (mesh->isSelectionChanged(pass) && edit_mode) { mesh->setSelectionChanged(pass, false); fillSelectionsBuffer(rend_selection.cur_selections_, it.value()); - //qDebug() << "fillSelectionsBuffer" << pass << rend_selection.cur_selections_.size(); + // qDebug() << "fillSelectionsBuffer" << pass << rend_selection.cur_selections_.size(); mesh->loadSelections(f, rend_selection.cur_selections_, pass); } - //qDebug() << "draw" << pass << it.value().size(); + // qDebug() << "draw" << pass << it.value().size(); mesh->draw(f, it.value().size(), pass); } if (emit_pos_change) emit view->objectsPositionChanged(); @@ -233,20 +242,20 @@ void Renderer::renderObjects(Scene & scene, RenderPass pass) { void Renderer::renderLight(int first_wr_buff, bool clear_only) { QOpenGLShaderProgram * prog = 0; - Camera * cam = view->camera(); + Camera * cam = view->camera(); for (int i = 0; i < 2; ++i) { view->glActiveTexture(GL_TEXTURE0 + Renderer::dbrBuffersCount + i); view->glBindTexture(GL_TEXTURE_2D, tex_coeff[i]); } fbo_ds.bindColorTextures(); fbo_out.bind(); - tex_env.bind((int)Renderer::dbrBuffersCount+1); + tex_env.bind((int)Renderer::dbrBuffersCount + 1); typedef QPair PassPair; QVector passes; passes << PassPair(srLightOmniPass, Light::Omni) << PassPair(srLightSpotPass, Light::Cone); QColor back = view->fogColor(); back.setAlpha(0); - foreach (PassPair pass, passes) { + foreach(PassPair pass, passes) { if (bindShader(pass.first, &prog)) { fbo_out.setWriteBuffer(first_wr_buff + pass.second); glClearFramebuffer(back, false); @@ -258,7 +267,7 @@ void Renderer::renderLight(int first_wr_buff, bool clear_only) { prog->setUniformValue("fog_color", view->fogColor()); prog->setUniformValue("fog_decay", qMax(view->fogDecay(), 0.001f)); prog->setUniformValue("fog_density", view->fogDensity()); - prog->setUniformValue("view_mat", cam->viewMatrix().inverted().toGenericMatrix<3,3>()); + prog->setUniformValue("view_mat", cam->viewMatrix().inverted().toGenericMatrix<3, 3>()); renderQuad(prog, quad, cam); } } @@ -270,17 +279,16 @@ void Renderer::renderScene() { tex_env.load(); QOpenGLExtraFunctions * f = view; Scene & scene(*(view->scene())); - Camera * cam = view->camera(); + Camera * cam = view->camera(); QOpenGLShaderProgram * prog = 0; - bool scene_changed = scene.prepare(); + bool scene_changed = scene.prepare(); scene.destroyUnused(f); /// reload materials on change if (scene_changed || scene.need_reload_materials) { rend_selection.generateObjectsID(scene); reloadMaterials(scene); - if (edit_mode) - recreateMaterialThumbnails(); + if (edit_mode) recreateMaterialThumbnails(); emit view->materialsChanged(); } @@ -290,7 +298,7 @@ void Renderer::renderScene() { } /// lights - cur_lights = scene.lights_used; + cur_lights = scene.lights_used; bool use_camlight = (camera_light_mode == QGLView::clmOn); if ((camera_light_mode == QGLView::clmAuto) && cur_lights.isEmpty()) { use_camlight = true; @@ -317,7 +325,7 @@ void Renderer::renderScene() { if (bindShader(srGeometryPass, &prog)) { setUniformCamera(prog, cam); textures_empty.bind(f, tarEmpty); - textures_maps .bind(f, tarMaps ); + textures_maps.bind(f, tarMaps); renderObjects(scene, rpSolid); } fbo_ds.release(); @@ -339,8 +347,8 @@ void Renderer::renderScene() { /// blending layers if (bindShader(srFinalPass, &prog)) { - fbo_out.bindColorTexture(obrSolidOmni , 1); - fbo_out.bindColorTexture(obrSolidSpot , 2); + fbo_out.bindColorTexture(obrSolidOmni, 1); + fbo_out.bindColorTexture(obrSolidSpot, 2); fbo_out.bindColorTexture(obrTransparentOmni, 3); fbo_out.bindColorTexture(obrTransparentSpot, 4); fbo_out.setWriteBuffer(obrSum); @@ -371,7 +379,7 @@ void Renderer::renderScene() { if (is_grabbing) { fbo_out.queryImage(0); last_img = fbo_out.getImage().mirrored(); - //qDebug() << last_img.size(); + // qDebug() << last_img.size(); } } @@ -383,5 +391,5 @@ void Renderer::setCameraLightMode(int m) { void Renderer::setGrabImage(bool on) { is_grabbing = on; - //fbo_out.enablePixelBuffer(); + // fbo_out.enablePixelBuffer(); } diff --git a/renderer.h b/renderer.h index fc2773e..604e1af 100644 --- a/renderer.h +++ b/renderer.h @@ -1,30 +1,31 @@ /* - QGL Renderer - Ivan Pelipenko peri4ko@yandex.ru + QGL Renderer + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef RENDERER_H #define RENDERER_H +#include "glcubemap.h" #include "renderer_base.h" #include "renderer_material.h" -#include "renderer_service.h" #include "renderer_selection.h" +#include "renderer_service.h" #include "tonemapping_proc.h" -#include "glcubemap.h" + #include @@ -83,13 +84,13 @@ public: void reloadShaders(); void renderScene(); void setCameraLightMode(int m); - int cameraLightMode() const {return camera_light_mode;} + int cameraLightMode() const { return camera_light_mode; } void setGrabImage(bool on); - bool isGrabImage() const {return is_grabbing;} - QImage getImage() const {return last_img;} + bool isGrabImage() const { return is_grabbing; } + QImage getImage() const { return last_img; } - QImage materialThumbnail(Material * m) {return rend_mat.materialThumbnail(m);} - void recreateMaterialThumbnails(bool force_all = false) {rend_mat.recreateMaterialThumbnails(force_all);} + QImage materialThumbnail(Material * m) { return rend_mat.materialThumbnail(m); } + void recreateMaterialThumbnails(bool force_all = false) { rend_mat.recreateMaterialThumbnails(force_all); } protected: void fillObjectsBuffer(const ObjectBaseList & ol, RenderPass pass); @@ -109,7 +110,7 @@ private: Framebuffer fbo_ds, fbo_out; QMap shader_files; QMap shader_defines; - QMap shaders; + QMap shaders; QOpenGLShaderProgram * shader_fxaa = nullptr; RendererMaterial rend_mat; @@ -127,7 +128,7 @@ private: QMatrix3x3 nm; QVector4D corner_dirs[4]; QVector hcontent; - QMap> cur_lights; + QMap> cur_lights; QImage last_img; bool is_grabbing = false; }; diff --git a/renderer_base.cpp b/renderer_base.cpp index 54626c8..3e34869 100644 --- a/renderer_base.cpp +++ b/renderer_base.cpp @@ -1,46 +1,46 @@ /* - QGL RendererBase - Ivan Pelipenko peri4ko@yandex.ru + QGL RendererBase + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #define GL_GLEXT_PROTOTYPES -#include #include "renderer_base.h" -#include "renderer.h" -#include "qglview.h" + #include "glmesh.h" -#include "gltexture_manager.h" #include "glshaders_headers.h" +#include "gltexture_manager.h" +#include "qglview.h" +#include "renderer.h" + +#include using namespace QGLEngineShaders; -RendererBase::RendererBase(QGLView * view_): - view(view_), - buffer_materials (GL_UNIFORM_BUFFER, GL_STREAM_DRAW), - buffer_lights (GL_UNIFORM_BUFFER, GL_STREAM_DRAW), - buffer_lights_pos(GL_UNIFORM_BUFFER, GL_STREAM_DRAW), - textures_empty(false), - textures_maps(true) -{ +RendererBase::RendererBase(QGLView * view_) + : view(view_) + , buffer_materials(GL_UNIFORM_BUFFER, GL_STREAM_DRAW) + , buffer_lights(GL_UNIFORM_BUFFER, GL_STREAM_DRAW) + , buffer_lights_pos(GL_UNIFORM_BUFFER, GL_STREAM_DRAW) + , textures_empty(false) + , textures_maps(true) { textures_manager = new TextureManager(view); - maps_size = QSize(512, 512); - maps_hash = 0; + maps_size = QSize(512, 512); + maps_hash = 0; tex_coeff[0] = tex_coeff[1] = 0; - } @@ -68,23 +68,23 @@ void RendererBase::initUniformBuffer(QOpenGLShaderProgram * prog, Buffer * buffe if (!prog->isLinked()) return; QOpenGLExtraFunctions * f = view; buffer->init(f); - //glClearError(); + // glClearError(); GLint ubo_ind = f->glGetUniformBlockIndex(prog->programId(), blockName); f->glUniformBlockBinding(prog->programId(), ubo_ind, bind_point); f->glBindBufferBase(GL_UNIFORM_BUFFER, bind_point, buffer->ID()); - //qDebug() << "initUBO" << QString::number(f->glGetError(), 16); + // qDebug() << "initUBO" << QString::number(f->glGetError(), 16); } void RendererBase::setUniformHalo(QOpenGLShaderProgram * prog, const char * type, QColor color, float fill) { prog->setUniformValue((QString(type) + "_color").toLatin1().constData(), color); - prog->setUniformValue((QString(type) + "_fill" ).toLatin1().constData(), fill); + prog->setUniformValue((QString(type) + "_fill").toLatin1().constData(), fill); } void RendererBase::setUniformMaps(QOpenGLShaderProgram * prog) { prog->setUniformValue("qgl_texture_array[0]", (int)tarEmpty); - prog->setUniformValue("qgl_texture_array[1]", (int)tarMaps ); + prog->setUniformValue("qgl_texture_array[1]", (int)tarMaps); } @@ -103,7 +103,7 @@ void RendererBase::setUniformCamera(QOpenGLShaderProgram * prog, Camera * cam, b prog->setUniformValue("z_near", cam->depthStart()); } prog->setUniformValue("dt", QVector2D(1. / w, 1. / h)); - prog->setUniformValue("qgl_ViewMatrix" , mat_view); + prog->setUniformValue("qgl_ViewMatrix", mat_view); prog->setUniformValue("qgl_ViewProjMatrix", mat_proj * mat_view); } @@ -118,9 +118,9 @@ void RendererBase::setUniformViewCorners(QOpenGLShaderProgram * prog, Camera * c QMatrix4x4 mviewi = cam->viewMatrix().inverted(); QVector4D corner_dirs[4], world_dirs[4]; corner_dirs[0] = (mproji * QVector4D(-1, -1, 0, 1)); - corner_dirs[1] = (mproji * QVector4D(-1, 1, 0, 1)); - corner_dirs[2] = (mproji * QVector4D( 1, 1, 0, 1)); - corner_dirs[3] = (mproji * QVector4D( 1, -1, 0, 1)); + corner_dirs[1] = (mproji * QVector4D(-1, 1, 0, 1)); + corner_dirs[2] = (mproji * QVector4D(1, 1, 0, 1)); + corner_dirs[3] = (mproji * QVector4D(1, -1, 0, 1)); for (int i = 0; i < 4; ++i) { world_dirs[i] = QVector4D(mviewi.mapVector(corner_dirs[i].toVector3D())); prog->setUniformValue(QString("view_corners[%1]").arg(i).toLatin1().constData(), corner_dirs[i]); @@ -145,19 +145,19 @@ void RendererBase::fillSelectionsBuffer(QVector & buffer, bool yes, int s void RendererBase::reloadMaterials(Scene & scene) { - //qDebug() << "reloadMaterias"; - QList maps[2]; + // qDebug() << "reloadMaterias"; + QList maps[2]; QMap tex_layers[2]; - foreach (Material * m, scene.materials) { - if (m->map_diffuse .hasBitmap()) maps[0] << &(m->map_diffuse ); - if (m->map_normal .hasBitmap()) maps[1] << &(m->map_normal ); + foreach(Material * m, scene.materials) { + if (m->map_diffuse.hasBitmap()) maps[0] << &(m->map_diffuse); + if (m->map_normal.hasBitmap()) maps[1] << &(m->map_normal); if (m->map_metalness.hasBitmap()) maps[0] << &(m->map_metalness); if (m->map_roughness.hasBitmap()) maps[0] << &(m->map_roughness); - if (m->map_emission .hasBitmap()) maps[0] << &(m->map_emission ); - if (m->map_relief .hasBitmap()) maps[0] << &(m->map_relief ); + if (m->map_emission.hasBitmap()) maps[0] << &(m->map_emission); + if (m->map_relief.hasBitmap()) maps[0] << &(m->map_relief); } for (int i = 0; i < 2; ++i) { - foreach (Map * m, maps[i]) + foreach(Map * m, maps[i]) tex_layers[i][m->bitmap_path] = 0; } int layers_count = tex_layers[0].size() + tex_layers[1].size(), cl = -1; @@ -174,52 +174,51 @@ void RendererBase::reloadMaterials(Scene & scene) { textures_maps.load(view, im, ++cl); it.value() = cl; } - foreach (Map * m, maps[i]) { + foreach(Map * m, maps[i]) { m->_layer = tex_layers[i].value(m->bitmap_path); - //qDebug() << "assign" << m->bitmap_path << "layer" << m->_layer; + // qDebug() << "assign" << m->bitmap_path << "layer" << m->_layer; } } textures_maps.mipmaps(view); -// qDebug() << "load" << (cl+1) << "bitmaps"; + // qDebug() << "load" << (cl+1) << "bitmaps"; } QGLMaterial glm; cur_materials_.clear(); cur_materials_ << glm; - foreach (Material * m, scene.materials) { + foreach(Material * m, scene.materials) { if (cur_materials_.size() >= max_materials) { qDebug() << "[QGLEngine] Warning: Too many materials! Maximum" << max_materials; break; } - m->_index = cur_materials_.size(); - m->_changed = false; - glm.color_diffuse = QColor2QVector(m->color_diffuse ); + m->_index = cur_materials_.size(); + m->_changed = false; + glm.color_diffuse = QColor2QVector(m->color_diffuse); glm.color_emission = QColor2QVector(m->color_emission); - glm.transparency = m->transparency; - glm.reflectivity = m->reflectivity; - glm.iof = m->iof ; - glm.dispersion = m->dispersion ; - m->map_diffuse .copyToQGLMap(glm.map[mtDiffuse ]); - m->map_normal .copyToQGLMap(glm.map[mtNormal ]); - m->map_metalness.copyToQGLMap(glm.map[mtMetalness ]); + glm.transparency = m->transparency; + glm.reflectivity = m->reflectivity; + glm.iof = m->iof; + glm.dispersion = m->dispersion; + m->map_diffuse.copyToQGLMap(glm.map[mtDiffuse]); + m->map_normal.copyToQGLMap(glm.map[mtNormal]); + m->map_metalness.copyToQGLMap(glm.map[mtMetalness]); m->map_roughness.copyToQGLMap(glm.map[mtRoughness]); - m->map_emission .copyToQGLMap(glm.map[mtEmission ]); - m->map_relief .copyToQGLMap(glm.map[mtRelief ]); + m->map_emission.copyToQGLMap(glm.map[mtEmission]); + m->map_relief.copyToQGLMap(glm.map[mtRelief]); cur_materials_ << glm; } - //qDebug() << "load" << cur_materials_.size() << "materials"; + // qDebug() << "load" << cur_materials_.size() << "materials"; buffer_materials.bind(view); buffer_materials.resize(view, cur_materials_.size() * sizeof(QGLMaterial)); buffer_materials.load(view, cur_materials_.constData(), cur_materials_.size() * sizeof(QGLMaterial)); scene.need_reload_materials = false; - } -void RendererBase::reloadLightsParameters(const QMap> & lights) { +void RendererBase::reloadLightsParameters(const QMap> & lights) { lights_start.clear(); lights_start[Light::Omni] = 0; - QMapIterator> it(lights); + QMapIterator> it(lights); current_lights.clear(); while (it.hasNext()) { it.next(); @@ -227,19 +226,18 @@ void RendererBase::reloadLightsParameters(const QMap> & light current_lights.append(it.value()); } cur_lights_params_.resize(qMin(current_lights.size(), max_lights)); - //qDebug() << "reloadLightsParameters" << cur_lights_params_.size(); + // qDebug() << "reloadLightsParameters" << cur_lights_params_.size(); for (int i = 0; i < cur_lights_params_.size(); ++i) { QGLLightParameter & so(cur_lights_params_[i]); - Light * l = current_lights[i]; + Light * l = current_lights[i]; double ang_start = l->angle_start / 2.f, ang_end = l->angle_end / 2.f; - if (l->light_type == Light::Omni) - ang_start = ang_end = 180.; - //qDebug() << "light" << light->name() << ulightn << pos; - so.color = QColor2QVector(l->color_); - so.angles[0] = ang_start; - so.angles[1] = cos(ang_start * deg2rad); - so.angles[2] = ang_end; - so.angles[3] = cos(ang_end * deg2rad); + if (l->light_type == Light::Omni) ang_start = ang_end = 180.; + // qDebug() << "light" << light->name() << ulightn << pos; + so.color = QColor2QVector(l->color_); + so.angles[0] = ang_start; + so.angles[1] = cos(ang_start * deg2rad); + so.angles[2] = ang_end; + so.angles[3] = cos(ang_end * deg2rad); so.decay_intensity[0] = l->decay_const; so.decay_intensity[1] = l->decay_linear; so.decay_intensity[2] = l->decay_quadratic; @@ -256,12 +254,12 @@ void RendererBase::reloadLightsPositions(Camera * cam) { QMatrix4x4 mat = cam->viewMatrix() * cam->offsetMatrix(); for (int i = 0; i < cur_lights_pos_.size(); ++i) { QGLLightPosition & so(cur_lights_pos_[i]); - Light * l = current_lights[i]; + Light * l = current_lights[i]; QMatrix4x4 m = mat * l->worldTransform(); - QVector4D pos(0, 0, 0, 1.), dir(QVector3D(0,0,-1), 1); - pos = m * pos; - dir = (m * QVector4D(QVector3D(0,0,-1),0)).normalized(); - so.position = pos; + QVector4D pos(0, 0, 0, 1.), dir(QVector3D(0, 0, -1), 1); + pos = m * pos; + dir = (m * QVector4D(QVector3D(0, 0, -1), 0)).normalized(); + so.position = pos; so.direction = dir; } buffer_lights_pos.bind(view); @@ -293,8 +291,7 @@ void RendererBase::initQuad(Mesh * mesh, QMatrix4x4 mat) { void RendererBase::renderQuad(QOpenGLShaderProgram * prog, Mesh * mesh, Camera * cam, bool uniforms) { glDisableDepth(); - if (uniforms) - setUniformCamera(prog, cam, false); + if (uniforms) setUniformCamera(prog, cam, false); mesh->draw(view, 1); } @@ -312,20 +309,20 @@ float RadicalInverse_VdC(uint bits) { QVector2D Hammersley(uint i, uint N) { - return QVector2D(float(i)/float(N), RadicalInverse_VdC(i)); + return QVector2D(float(i) / float(N), RadicalInverse_VdC(i)); } QVector3D ImportanceSampleGGX(QVector2D Xi, QVector3D N, float roughness) { - float a = roughness*roughness; - float phi = 2.0 * M_PI * Xi[0]; - float cosTheta = sqrt((1.0 - Xi[1]) / (1.0 + (a*a - 1.0) * Xi[1])); - float sinTheta = sqrt(1.0 - cosTheta*cosTheta); + float a = roughness * roughness; + float phi = 2.0 * M_PI * Xi[0]; + float cosTheta = sqrt((1.0 - Xi[1]) / (1.0 + (a * a - 1.0) * Xi[1])); + float sinTheta = sqrt(1.0 - cosTheta * cosTheta); // преобразование из сферических в декартовы координаты QVector3D H; - H[0] = cos(phi) * sinTheta; - H[1] = sin(phi) * sinTheta; - H[2] = cosTheta; + H[0] = cos(phi) * sinTheta; + H[1] = sin(phi) * sinTheta; + H[2] = cosTheta; // преобразование из касательного пространства в мировые координаты QVector3D up = qAbs(N[2]) < 0.999 ? QVector3D(0.0, 0.0, 1.0) : QVector3D(1.0, 0.0, 0.0); QVector3D tangent = QVector3D::crossProduct(up, N).normalized(); @@ -336,7 +333,7 @@ QVector3D ImportanceSampleGGX(QVector2D Xi, QVector3D N, float roughness) { float GeometrySchlickGGX(float NdotV, float roughness) { - float k = (roughness * roughness) / 2.0; + float k = (roughness * roughness) / 2.0; float nom = NdotV; float denom = NdotV * (1.0 - k) + k; return nom / denom; @@ -346,32 +343,32 @@ float GeometrySchlickGGX(float NdotV, float roughness) { float GeometrySmith(QVector3D N, QVector3D V, QVector3D L, float roughness) { float NdotV = piMax(QVector3D::dotProduct(N, V), 0.f); float NdotL = piMax(QVector3D::dotProduct(N, L), 0.f); - float ggx2 = GeometrySchlickGGX(NdotV, roughness); - float ggx1 = GeometrySchlickGGX(NdotL, roughness); + float ggx2 = GeometrySchlickGGX(NdotV, roughness); + float ggx1 = GeometrySchlickGGX(NdotL, roughness); return ggx1 * ggx2; } QVector2D IntegrateBRDF(float NdotV, float roughness) { QVector3D V; - V[0] = sqrt(1.f - NdotV*NdotV); - V[1] = 0.f; - V[2] = NdotV; - float A = 0.f; - float B = 0.f; - QVector3D N = QVector3D(0.f, 0.f, 1.f); + V[0] = sqrt(1.f - NdotV * NdotV); + V[1] = 0.f; + V[2] = NdotV; + float A = 0.f; + float B = 0.f; + QVector3D N = QVector3D(0.f, 0.f, 1.f); const uint SAMPLE_COUNT = 256u; - for(uint i = 0u; i < SAMPLE_COUNT; ++i) { + for (uint i = 0u; i < SAMPLE_COUNT; ++i) { QVector2D Xi = Hammersley(i, SAMPLE_COUNT); QVector3D H = ImportanceSampleGGX(Xi, N, roughness); QVector3D L = (2.f * QVector3D::dotProduct(V, H) * H - V).normalized(); - float NdotL = piMax(L[2], 0.f); - float NdotH = piMax(H[2], 0.f); - float VdotH = piMax(QVector3D::dotProduct(V, H), 0.f); - if(NdotL > 0.f) { - float G = GeometrySmith(N, V, L, roughness); + float NdotL = piMax(L[2], 0.f); + float NdotH = piMax(H[2], 0.f); + float VdotH = piMax(QVector3D::dotProduct(V, H), 0.f); + if (NdotL > 0.f) { + float G = GeometrySmith(N, V, L, roughness); float G_Vis = (G * VdotH) / (NdotH * NdotV); - float Fc = pow(1.f - VdotH, 5.f); + float Fc = pow(1.f - VdotH, 5.f); A += (1.f - Fc) * G_Vis; B += Fc * G_Vis; } @@ -384,12 +381,12 @@ QVector2D IntegrateBRDF(float NdotV, float roughness) { void RendererBase::initCoeffTextures() { QImage im = QImage(":/coeffs_brdf.png").mirrored(); - int size = im.width(); - QVector data(size*size); + int size = im.width(); + QVector data(size * size); int ind = -1; for (int x = 0; x < size; ++x) { for (int y = 0; y < size; ++y) { - QColor p = im.pixelColor(x, y); + QColor p = im.pixelColor(x, y); data[++ind] = QVector2D(p.redF(), p.greenF()); } } @@ -408,8 +405,17 @@ void RendererBase::createCoeffTexture(GLuint & id, const void * data, int size, f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); GLenum iformat = GL_R16F, format = GL_RED; - if (channels == 2) {iformat = GL_RG16F; format = GL_RG;} - if (channels == 3) {iformat = GL_RGB16F; format = GL_RGB;} - if (channels == 4) {iformat = GL_RGBA16F; format = GL_RGBA;} + if (channels == 2) { + iformat = GL_RG16F; + format = GL_RG; + } + if (channels == 3) { + iformat = GL_RGB16F; + format = GL_RGB; + } + if (channels == 4) { + iformat = GL_RGBA16F; + format = GL_RGBA; + } f->glTexImage2D(GL_TEXTURE_2D, 0, iformat, size, size, 0, format, GL_FLOAT, data); } diff --git a/renderer_base.h b/renderer_base.h index a7a5133..75d4acb 100644 --- a/renderer_base.h +++ b/renderer_base.h @@ -1,27 +1,27 @@ /* - QGL RendererBase - Ivan Pelipenko peri4ko@yandex.ru + QGL RendererBase + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef RENDERER_BASE_H #define RENDERER_BASE_H +#include "glbuffer.h" #include "glshaders_types.h" #include "gltexturearray.h" -#include "glbuffer.h" class RendererBase { @@ -31,16 +31,16 @@ public: protected: void initTextureArrays(); - void initUniformBuffer (QOpenGLShaderProgram * prog, Buffer * buffer, int bind_point, const char * blockName); - void setUniformHalo (QOpenGLShaderProgram * prog, const char * type, QColor color, float fill); - void setUniformMaps (QOpenGLShaderProgram * prog); - void setUniformCamera (QOpenGLShaderProgram * prog, Camera * cam, bool matrices = true, QSize viewport = QSize()); + void initUniformBuffer(QOpenGLShaderProgram * prog, Buffer * buffer, int bind_point, const char * blockName); + void setUniformHalo(QOpenGLShaderProgram * prog, const char * type, QColor color, float fill); + void setUniformMaps(QOpenGLShaderProgram * prog); + void setUniformCamera(QOpenGLShaderProgram * prog, Camera * cam, bool matrices = true, QSize viewport = QSize()); void setUniformViewCorners(QOpenGLShaderProgram * prog, Camera * cam, QSize viewport = QSize()); void fillSelectionsBuffer(QVector & buffer, const ObjectBaseList & ol); void fillSelectionsBuffer(QVector & buffer, bool yes, int size); void reloadMaterials(Scene & scene); - void reloadLightsParameters(const QMap> & lights); - void reloadLightsPositions (Camera * cam); + void reloadLightsParameters(const QMap> & lights); + void reloadLightsPositions(Camera * cam); void markReloadTextures(); void setMapsSize(QSize sz); void initQuad(Mesh * mesh, QMatrix4x4 mat = QMatrix4x4()); @@ -61,7 +61,7 @@ protected: uint maps_hash; GLuint tex_coeff[2]; QMap lights_start; - QList current_lights; + QList current_lights; }; #endif // RENDERER_BASE_H diff --git a/renderer_material.cpp b/renderer_material.cpp index d1e70a9..56be0c9 100644 --- a/renderer_material.cpp +++ b/renderer_material.cpp @@ -1,35 +1,36 @@ /* - QGL RendererMaterial - Ivan Pelipenko peri4ko@yandex.ru + QGL RendererMaterial + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #define GL_GLEXT_PROTOTYPES -#include #include "renderer_material.h" -#include "renderer.h" -#include "qglview.h" + #include "glmesh.h" #include "gltexture_manager.h" +#include "qglview.h" +#include "renderer.h" + +#include #include using namespace QGLEngineShaders; -RendererMaterial::RendererMaterial(Renderer * r_) -: r(r_), fbo_mat_thumb(r->view, 6, true , GL_RGBA16F) { +RendererMaterial::RendererMaterial(Renderer * r_): r(r_), fbo_mat_thumb(r->view, 6, true, GL_RGBA16F) { mat_sphere = Primitive::ellipsoid(16, 16); mat_camera = new Camera(); mat_camera->setPos(QVector3D(2, 2, 2)); @@ -62,19 +63,19 @@ void RendererMaterial::resize(int width, int height) { void RendererMaterial::renderMaterial(Material * m) { - //qDebug() << "renderMaterial" << m; - last_thumb_material = m; + // qDebug() << "renderMaterial" << m; + last_thumb_material = m; QOpenGLShaderProgram * prog = 0; - QOpenGLExtraFunctions * f = r->view; + QOpenGLExtraFunctions * f = r->view; fbo_mat_thumb.bind(); glEnableDepth(); - glClearFramebuffer(QColor(0,0,0,0)); + glClearFramebuffer(QColor(0, 0, 0, 0)); if (r->bindShader(Renderer::srGeometryPass, &prog)) { r->setUniformMaps(prog); r->setUniformCamera(prog, mat_camera, true, fbo_mat_thumb.size()); - //qDebug() << mat_camera->viewMatrix(); + // qDebug() << mat_camera->viewMatrix(); r->textures_empty.bind(f, tarEmpty); - r->textures_maps .bind(f, tarMaps ); + r->textures_maps.bind(f, tarMaps); Object o; o.material = m->_index; mat_sphere->loadObject(f, o); @@ -91,7 +92,7 @@ void RendererMaterial::renderMaterial(Material * m) { prog->setUniformValue("tex_d", 5); prog->setUniformValue("lights_start", 0); prog->setUniformValue("lights_count", 1); - QMap> mat_l; + QMap> mat_l; mat_l[Light::Omni] << mat_light; r->reloadLightsParameters(mat_l); r->reloadLightsPositions(mat_camera); @@ -110,8 +111,7 @@ void RendererMaterial::procQueue() { emit r->view->materialThumbnailCreated(last_thumb_material); last_thumb_material = 0; } - if (!mat_thumb_queue.isEmpty()) - renderMaterial(mat_thumb_queue.dequeue()); + if (!mat_thumb_queue.isEmpty()) renderMaterial(mat_thumb_queue.dequeue()); } @@ -123,12 +123,11 @@ QImage RendererMaterial::materialThumbnail(Material * m) { void RendererMaterial::recreateMaterialThumbnails(bool force_all) { if (force_all) { mat_thumb_queue.clear(); - //qDebug() << "recreateMaterialThumbnails" << view->scene_->materials; - foreach (Material * m, r->view->scene()->materials) + // qDebug() << "recreateMaterialThumbnails" << view->scene_->materials; + foreach(Material * m, r->view->scene()->materials) mat_thumb_queue.enqueue(m); } else { - foreach (Material * m, r->view->scene()->changed_materials) - if (!mat_thumb_queue.contains(m)) - mat_thumb_queue.enqueue(m); + foreach(Material * m, r->view->scene()->changed_materials) + if (!mat_thumb_queue.contains(m)) mat_thumb_queue.enqueue(m); } } diff --git a/renderer_material.h b/renderer_material.h index 31f8255..d93e586 100644 --- a/renderer_material.h +++ b/renderer_material.h @@ -1,30 +1,32 @@ /* - QGL RendererMaterial - Ivan Pelipenko peri4ko@yandex.ru + QGL RendererMaterial + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef RENDERER_MATERIAL_H #define RENDERER_MATERIAL_H #include "glframebuffer.h" + #include class RendererMaterial { friend class QGLView; + public: RendererMaterial(Renderer * r_); virtual ~RendererMaterial(); @@ -43,9 +45,9 @@ private: Mesh * mat_sphere; Camera * mat_camera; Light * mat_light; - QMap mat_thumbnails; + QMap mat_thumbnails; Material * last_thumb_material; - QQueue mat_thumb_queue; + QQueue mat_thumb_queue; }; #endif // RENDERER_MATERIAL_H diff --git a/renderer_selection.cpp b/renderer_selection.cpp index 8a3bf77..95bb8b2 100644 --- a/renderer_selection.cpp +++ b/renderer_selection.cpp @@ -1,37 +1,38 @@ /* - QGL RendererSelection - Ivan Pelipenko peri4ko@yandex.ru + QGL RendererSelection + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #define GL_GLEXT_PROTOTYPES -#include #include "renderer_selection.h" -#include "qglview.h" + #include "glmesh.h" +#include "qglview.h" + +#include #include using namespace QGLEngineShaders; -RendererSelection::RendererSelection(Renderer * r_) -: r(r_), fbo_selection(r->view, 6) { - sel_frame = Primitive::plane(2., 2.); - id_hover = 0; +RendererSelection::RendererSelection(Renderer * r_): r(r_), fbo_selection(r->view, 6) { + sel_frame = Primitive::plane(2., 2.); + id_hover = 0; line_thick_ = 2.; - scale_ = 0.5; + scale_ = 0.5; } @@ -49,7 +50,7 @@ void RendererSelection::init(int width, int height) { void RendererSelection::resize(int width, int height) { line_thick_ = lineThickness() + 1.; - scale_ = 0.5 / appScale(); + scale_ = 0.5 / appScale(); fbo_selection.enablePixelBuffer(); fbo_selection.resize(width * scale_, height * scale_); } @@ -59,31 +60,31 @@ void RendererSelection::generateObjectsID(Scene & scene) { ids.clear(); aim_ids.clear(); QList passes = scene.geometries_used.keys(); - foreach (int p, passes) { - QMapIterator it(scene.geometries_used[p]); + foreach(int p, passes) { + QMapIterator it(scene.geometries_used[p]); while (it.hasNext()) { it.next(); - foreach (ObjectBase * o, it.value()) { + foreach(ObjectBase * o, it.value()) { uint id = qHash(o); ids[id] = o; - o->id_ = id; + o->id_ = id; } } - QMapIterator> lit(scene.lights_used); + QMapIterator> lit(scene.lights_used); while (lit.hasNext()) { lit.next(); - foreach (ObjectBase * o, lit.value()) { - uint id = qHash(o); - ids[id] = o; + foreach(ObjectBase * o, lit.value()) { + uint id = qHash(o); + ids[id] = o; aim_ids[id + 1] = o; - o->id_ = id; + o->id_ = id; } } - foreach (Camera * o, scene.cameras_used) { - uint id = qHash(o); - ids[id] = o; + foreach(Camera * o, scene.cameras_used) { + uint id = qHash(o); + ids[id] = o; aim_ids[id + 1] = o; - o->id_ = id; + o->id_ = id; } } } @@ -91,7 +92,7 @@ void RendererSelection::generateObjectsID(Scene & scene) { void RendererSelection::renderSelection(Scene & scene) { QOpenGLShaderProgram * prog = 0; - QGLView * view = r->view; + QGLView * view = r->view; MouseController & mc(view->mouse); if (r->bindShader(Renderer::srSelectionFill, &prog)) { mc.hov_objects.clear(); @@ -99,28 +100,27 @@ void RendererSelection::renderSelection(Scene & scene) { id_hover = 0; if (fbo_selection.queriedPoints() > 0) { if (fbo_selection.queriedPoints() == 1) { - id_hover = fbo_selection.getPoint(); + id_hover = fbo_selection.getPoint(); ObjectBase * o = ids.value(id_hover); if (o) mc.hov_objects << o; else { o = aim_ids.value(id_hover); - if (o) - mc.hov_aims << o; + if (o) mc.hov_aims << o; } - //qDebug() << id_hover; + // qDebug() << id_hover; } else { QVector points = fbo_selection.getPointsByte(); QSet ids_hover; - foreach (uint i, points) + foreach(uint i, points) ids_hover << i; - foreach (uint i, ids_hover) { + foreach(uint i, ids_hover) { ObjectBase * o = ids.value(i); if (o) mc.hov_objects << o; o = aim_ids.value(i); if (o) mc.hov_aims << o; } - //qDebug() << ids_hover; + // qDebug() << ids_hover; } } @@ -128,7 +128,7 @@ void RendererSelection::renderSelection(Scene & scene) { fbo_selection.setWriteBuffers(); glEnableDepth(); - glClearFramebuffer(QColor(0,0,0,0)); + glClearFramebuffer(QColor(0, 0, 0, 0)); r->setUniformCamera(prog, view->camera()); r->renderObjects(scene, rpSolid); r->renderObjects(scene, rpTransparent); @@ -141,27 +141,27 @@ void RendererSelection::renderSelection(Scene & scene) { if (r->mouse_rect.isNull()) fbo_selection.queryPoint(0, r->mouse_pos * scale_); else - fbo_selection.queryPoints(0, QRect(r->mouse_rect.topLeft() * scale_, - r->mouse_rect.size() * scale_)); + fbo_selection.queryPoints(0, QRect(r->mouse_rect.topLeft() * scale_, r->mouse_rect.size() * scale_)); - //qDebug() << id_hover; - fbo_selection.bindColorTexture(sbrSrcHover , sbrSrcHover ); + // qDebug() << id_hover; + fbo_selection.bindColorTexture(sbrSrcHover, sbrSrcHover); fbo_selection.bindColorTexture(sbrSrcSelect, sbrSrcSelect); fbo_selection.setWriteBuffers(QVector() << sbrHovered << sbrSelected); if (!view->isHoverHaloEnabled() && !view->isSelectionHaloEnabled()) - glClearFramebuffer(QColor(0,0,0,0), false); + glClearFramebuffer(QColor(0, 0, 0, 0), false); else { r->bindShader(Renderer::srSelectionHalo, &prog); - r->setUniformHalo(prog, "hover" , view->hoverHaloColor() , view->hoverHaloFillAlpha()); + r->setUniformHalo(prog, "hover", view->hoverHaloColor(), view->hoverHaloFillAlpha()); r->setUniformHalo(prog, "selection", view->selectionHaloColor(), view->selectionHaloFillAlpha()); - prog->setUniformValue("has_hover" , view->isHoverHaloEnabled() && (id_hover > 0) ? 1.f : 0.f); + prog->setUniformValue("has_hover", view->isHoverHaloEnabled() && (id_hover > 0) ? 1.f : 0.f); prog->setUniformValue("has_selection", view->isSelectionHaloEnabled() ? 1.f : 0.f); - prog->setUniformValue("fb_hover" , (int)sbrSrcHover); + prog->setUniformValue("fb_hover", (int)sbrSrcHover); prog->setUniformValue("fb_selection", (int)sbrSrcSelect); - prog->setUniformValue("hover_id", QVector4D(float( id_hover & 0xFF) / 255.f, - float((id_hover >> 8 ) & 0xFF) / 255.f, - float((id_hover >> 16) & 0xFF) / 255.f, - float((id_hover >> 24) & 0xFF) / 255.f)); + prog->setUniformValue("hover_id", + QVector4D(float(id_hover & 0xFF) / 255.f, + float((id_hover >> 8) & 0xFF) / 255.f, + float((id_hover >> 16) & 0xFF) / 255.f, + float((id_hover >> 24) & 0xFF) / 255.f)); r->renderQuad(prog, r->quad, view->camera()); } @@ -184,9 +184,9 @@ void RendererSelection::renderSelectionFrame() { QOpenGLShaderProgram * prog = 0; if (r->bindShader(Renderer::srSelectionFrame, &prog)) { QMatrix4x4 mat; - double mrx = r->mouse_rect.x(), mrw = r->mouse_rect.width() , vw = r->view->width(); + double mrx = r->mouse_rect.x(), mrw = r->mouse_rect.width(), vw = r->view->width(); double mry = r->mouse_rect.y(), mrh = r->mouse_rect.height(), vh = r->view->height(); - mat.translate(-1. + (mrw + mrx*2) / vw, 1. - (mrh + mry*2) / vh, 0.); + mat.translate(-1. + (mrw + mrx * 2) / vw, 1. - (mrh + mry * 2) / vh, 0.); mat.scale(mrw / vw, mrh / vh, 0.); r->initQuad(sel_frame, mat); prog->setUniformValue("size", QVector2D(mrw / vw, mrh / vh)); @@ -204,8 +204,8 @@ void RendererSelection::drawSelection(Framebuffer & fbo_out, int index_out) { if (r->bindShader(Renderer::srSelectionApply, &prog)) { fbo_selection.bindColorTextures(); fbo_out.bindColorTexture(index_out); - prog->setUniformValue("fb_out" , 0); - prog->setUniformValue("fb_hover" , (int)sbrHoveredFXAA ); + prog->setUniformValue("fb_out", 0); + prog->setUniformValue("fb_hover", (int)sbrHoveredFXAA); prog->setUniformValue("fb_select", (int)sbrSelectedFXAA); r->renderQuad(prog, r->quad, r->view->camera()); if (!r->mouse_rect.isNull()) { diff --git a/renderer_selection.h b/renderer_selection.h index 712c786..1967bdf 100644 --- a/renderer_selection.h +++ b/renderer_selection.h @@ -1,25 +1,26 @@ /* - QGL RendererSelection - Ivan Pelipenko peri4ko@yandex.ru + QGL RendererSelection + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef RENDERER_SELECTION_H #define RENDERER_SELECTION_H #include "renderer_service.h" + #include @@ -57,8 +58,8 @@ private: Mesh * sel_frame; float line_thick_, scale_; QVector cur_selections_; - QHash ids; - QHash aim_ids; + QHash ids; + QHash aim_ids; uint id_hover; }; diff --git a/renderer_service.cpp b/renderer_service.cpp index 30ef287..ee0915f 100644 --- a/renderer_service.cpp +++ b/renderer_service.cpp @@ -1,83 +1,86 @@ /* - QGL RendererService - Ivan Pelipenko peri4ko@yandex.ru + QGL RendererService + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #define GL_GLEXT_PROTOTYPES -#include #include "renderer_service.h" -#include "renderer.h" -#include "qglview.h" + #include "glmesh.h" +#include "qglview.h" +#include "renderer.h" + +#include #include using namespace QGLEngineShaders; RendererService::RendererService(Renderer * r_): r(r_) { - line_width = 1; + line_width = 1; current_action = haNoAction; current_handle = QFlags(); - mat_xyz.resize(3); mat_ms2.resize(3); - color_xyz.resize(3); color_ms2.resize(3); - const QVector3D _rot [3] = {QVector3D(0,1,0), QVector3D(-1,0,0), QVector3D(0, 0,1)}; - const QVector3D _rot2[3] = {QVector3D(0,0,0), QVector3D( 1,0,0), QVector3D(0,-1,0)}; + mat_xyz.resize(3); + mat_ms2.resize(3); + color_xyz.resize(3); + color_ms2.resize(3); + const QVector3D _rot[3] = {QVector3D(0, 1, 0), QVector3D(-1, 0, 0), QVector3D(0, 0, 1)}; + const QVector3D _rot2[3] = {QVector3D(0, 0, 0), QVector3D(1, 0, 0), QVector3D(0, -1, 0)}; for (int i = 0; i < 3; ++i) { QMatrix4x4 m; m.rotate(90., _rot[i]); mat_xyz[i] = m; m.setToIdentity(); - if (!_rot2[i].isNull()) - m.rotate(90., _rot2[i]); - mat_ms2[i] = m; - color_xyz[i] = color_ms2[i] = QVector4D(0,0,0,0.8); - color_xyz[i][i] = 1.; + if (!_rot2[i].isNull()) m.rotate(90., _rot2[i]); + mat_ms2[i] = m; + color_xyz[i] = color_ms2[i] = QVector4D(0, 0, 0, 0.8); + color_xyz[i][i] = 1.; } color_ms2[0] = (color_xyz[0] + color_xyz[1]) / 2.; color_ms2[1] = (color_xyz[0] + color_xyz[2]) / 2.; color_ms2[2] = (color_xyz[1] + color_xyz[2]) / 2.; - axis_camera = new Camera(); + axis_camera = new Camera(); axis_camera->setAim(QVector3D()); axis_camera->setFOV(45.); - axis_mesh = Primitive::arrow(12); + axis_mesh = Primitive::arrow(12); size_vp_scale = size_full_scale = 1.; - box_mesh = Primitive::cube(0.8, 0.8, 0.8); - box_mesh_f = Primitive::cubeFrame(0.8, 0.8, 0.8); - omni_mesh = Primitive::ellipsoid(2, 1, 0.5); - omni_mesh_f = Primitive::ellipsoidFrame(2, 1, 0.5); - omni_mesh ->scalePoints(1.5); - omni_mesh_f ->scalePoints(1.5); + box_mesh = Primitive::cube(0.8, 0.8, 0.8); + box_mesh_f = Primitive::cubeFrame(0.8, 0.8, 0.8); + omni_mesh = Primitive::ellipsoid(2, 1, 0.5); + omni_mesh_f = Primitive::ellipsoidFrame(2, 1, 0.5); + omni_mesh->scalePoints(1.5); + omni_mesh_f->scalePoints(1.5); cone_mesh = Primitive::cone(8, 0.5, 1.); cone_mesh_f = Primitive::coneFrame(8, 0.5, 1.); QMatrix4x4 mat; - mat.translate(0,0,-1); - cone_mesh ->transformPoints(mat); + mat.translate(0, 0, -1); + cone_mesh->transformPoints(mat); cone_mesh_f->transformPoints(mat); cone_mesh_f->scalePoints(1.5); - box_mesh ->scalePoints(1.3); - omni_mesh ->scalePoints(1.3); - cone_mesh ->translatePoints(0,0,0.5); - cone_mesh ->scalePoints(1.4); - cone_mesh ->translatePoints(0,0,-0.5); - cone_mesh ->scalePoints(1.5); + box_mesh->scalePoints(1.3); + omni_mesh->scalePoints(1.3); + cone_mesh->translatePoints(0, 0, 0.5); + cone_mesh->scalePoints(1.4); + cone_mesh->translatePoints(0, 0, -0.5); + cone_mesh->scalePoints(1.5); camera_mesh = Primitive::cube(1.2, 1.2, 1.2); - mat.translate(0,0,-0.5); + mat.translate(0, 0, -0.5); Mesh * m = Primitive::cone(6, 0.6, 1.); m->scalePoints(1.4); m->transformPoints(mat); @@ -88,34 +91,34 @@ RendererService::RendererService(Renderer * r_): r(r_) { m->transformPoints(mat); camera_mesh_f->append(m); - line_spot_f = Primitive::lineFrame(QVector3D(), QVector3D(0, 0, -1)); - line_camera_f = Primitive::lineFrame(QVector3D(), QVector3D(0, 0, -1)); + line_spot_f = Primitive::lineFrame(QVector3D(), QVector3D(0, 0, -1)); + line_camera_f = Primitive::lineFrame(QVector3D(), QVector3D(0, 0, -1)); handle_move_mesh = Primitive::arrow(12, 0.06); handle_ms_2_mesh = Primitive::torus(8, 12, 0.5, 0.02, 90); - m = Primitive::disc(8, 0.5, 90); + m = Primitive::disc(8, 0.5, 90); handle_ms_2_mesh->append(m); m->flipNormals(); handle_ms_2_mesh->append(m); delete m; handle_rotate_mesh = Primitive::arrow(12, 0.03); - m = Primitive::torus(30, 12, 0.5, 0.06); + m = Primitive::torus(30, 12, 0.5, 0.06); m->translatePoints(QVector3D(0., 0., 0.75)); handle_rotate_mesh->append(m); delete m; handle_scale_mesh = Primitive::cylinder(12, 0.03, 0.85); - m = Primitive::ellipsoid(12, 12, 0.15); + m = Primitive::ellipsoid(12, 12, 0.15); m->translatePoints(QVector3D(0., 0., 0.85)); handle_scale_mesh->append(m); delete m; handle_scale_3_mesh = Primitive::ellipsoid(12, 12, 0.2); - handle_move_mesh ->scalePoints(7.5); - handle_ms_2_mesh ->scalePoints(7.5); - handle_rotate_mesh ->scalePoints(7.5); - handle_scale_mesh ->scalePoints(7.5); + handle_move_mesh->scalePoints(7.5); + handle_ms_2_mesh->scalePoints(7.5); + handle_rotate_mesh->scalePoints(7.5); + handle_scale_mesh->scalePoints(7.5); handle_scale_3_mesh->scalePoints(7.5); } @@ -142,23 +145,23 @@ RendererService::~RendererService() { void RendererService::init(int width, int height) { - box_mesh ->reinit(); - box_mesh_f ->reinit(); - omni_mesh ->reinit(); - omni_mesh_f ->reinit(); - cone_mesh ->reinit(); - cone_mesh_f ->reinit(); - camera_mesh ->reinit(); - camera_mesh_f ->reinit(); - line_spot_f ->reinit(); - line_camera_f ->reinit(); - axis_mesh ->reinit(); - handle_move_mesh ->reinit(); - handle_ms_2_mesh ->reinit(); - handle_rotate_mesh ->reinit(); - handle_scale_mesh ->reinit(); + box_mesh->reinit(); + box_mesh_f->reinit(); + omni_mesh->reinit(); + omni_mesh_f->reinit(); + cone_mesh->reinit(); + cone_mesh_f->reinit(); + camera_mesh->reinit(); + camera_mesh_f->reinit(); + line_spot_f->reinit(); + line_camera_f->reinit(); + axis_mesh->reinit(); + handle_move_mesh->reinit(); + handle_ms_2_mesh->reinit(); + handle_rotate_mesh->reinit(); + handle_scale_mesh->reinit(); handle_scale_3_mesh->reinit(); - axis_mesh ->reinit(); + axis_mesh->reinit(); fillXYZObjects(); axis_mesh->loadObjects(r->view, cur_objects); resize(width, height); @@ -167,15 +170,15 @@ void RendererService::init(int width, int height) { void RendererService::resize(int width, int height) { axis_viewport = preferredIconSize(10.); - line_width = lineThickness(); + line_width = lineThickness(); size_vp_scale = 25. * appScale() / qMax(qMin(width, height), 1); - //qDebug() << axis_viewport; + // qDebug() << axis_viewport; } QMatrix4x4 RendererService::invariantSizeMatrix(QVector3D p, double * ret_scale) { QVector4D pos = QVector4D(p, 1.); - double dist = -(v_mat * pos).z(); + double dist = -(v_mat * pos).z(); QMatrix4x4 m; m.translate(pos.toVector3D()); m.scale(dist * size_full_scale); @@ -209,10 +212,10 @@ void RendererService::fillXYZObjects() { void RendererService::fillOmniObjects() { - QList ll = r->view->scene()->lights_used.value(Light::Omni); + QList ll = r->view->scene()->lights_used.value(Light::Omni); Object o; cur_objects.clear(); - foreach (Light * l, ll) { + foreach(Light * l, ll) { QMatrix4x4 m = invariantSizeMatrix(l->worldPos()); m.transposed().copyDataTo(o.modelmatrix); o.object_id = l->id(); @@ -225,12 +228,12 @@ void RendererService::fillAimedObjects(const ObjectBaseList & objects, Mesh * li Object o; cur_objects.clear(); cur_aims.clear(); - QVector & lv (line_mesh->vertices ()); - QVector & ln (line_mesh->normals ()); - QVector & lt (line_mesh->texcoords ()); - QVector< Vector2i> & lind(line_mesh->indicesLines()); + QVector & lv(line_mesh->vertices()); + QVector & ln(line_mesh->normals()); + QVector & lt(line_mesh->texcoords()); + QVector & lind(line_mesh->indicesLines()); lv.clear(); - foreach (ObjectBase * go, objects) { + foreach(ObjectBase * go, objects) { AimedObject * ao = (AimedObject *)go; QMatrix4x4 m; m = invariantSizeMatrix(ao->worldPos()) * parentRotationMatrix(ao); @@ -249,20 +252,25 @@ void RendererService::fillAimedObjects(const ObjectBaseList & objects, Mesh * li lt.resize(lv.size()); lind.resize(lv.size() / 2); for (int i = 0; i < lind.size(); ++i) { - lind[i] = Vector2i(i*2, i*2 + 1); + lind[i] = Vector2i(i * 2, i * 2 + 1); } } -void RendererService::fillHandleObjects(QVector3D center, HandleMesh ids[], const QVector & mats, const QVector & colors, QMatrix4x4 add_mat, int count) { +void RendererService::fillHandleObjects(QVector3D center, + HandleMesh ids[], + const QVector & mats, + const QVector & colors, + QMatrix4x4 add_mat, + int count) { QMatrix4x4 m = invariantSizeMatrix(center) * add_mat; cur_objects.resize(count); for (int i = 0; i < count; ++i) { - cur_objects[i].color = colors[i]; - QMatrix4x4 omat = m * mats[i]; + cur_objects[i].color = colors[i]; + QMatrix4x4 omat = m * mats[i]; cur_objects[i].object_id = ids[i]; if (current_handle.testFlag(ids[i])) { - cur_objects[i].color = QVector4D(0,1,1,1); + cur_objects[i].color = QVector4D(0, 1, 1, 1); } omat.transposed().copyDataTo(cur_objects[i].modelmatrix); } @@ -275,7 +283,7 @@ bool RendererService::calculateCenter() { selection_center = sol[0]->worldPos(); if (sol.size() > 1) { Box3D bb; - foreach (ObjectBase * o, sol) { + foreach(ObjectBase * o, sol) { o->calculateBoundingBox(); bb |= o->boundingBox(); } @@ -286,8 +294,7 @@ bool RendererService::calculateCenter() { axis_mat = QMatrix4x4(); if ((sol.size() == 1)) { if (current_action == haMove) { - if (sol[0]->isAimSelected()) - selection_center = ((AimedObject*)sol[0])->worldAim(); + if (sol[0]->isAimSelected()) selection_center = ((AimedObject *)sol[0])->worldAim(); } else { axis_mat = parentRotationMatrix(sol[0]); } @@ -301,9 +308,21 @@ void RendererService::drawCurrentHandleObjects() { if (calculateCenter()) { HandleMesh ids[3]; switch (current_action) { - case haMove : ids[0] = hmMoveX ; ids[1] = hmMoveY ; ids[2] = hmMoveZ ; break; - case haRotate: ids[0] = hmRotateX; ids[1] = hmRotateY; ids[2] = hmRotateZ; break; - case haScale : ids[0] = hmScaleX ; ids[1] = hmScaleY ; ids[2] = hmScaleZ ; break; + case haMove: + ids[0] = hmMoveX; + ids[1] = hmMoveY; + ids[2] = hmMoveZ; + break; + case haRotate: + ids[0] = hmRotateX; + ids[1] = hmRotateY; + ids[2] = hmRotateZ; + break; + case haScale: + ids[0] = hmScaleX; + ids[1] = hmScaleY; + ids[2] = hmScaleZ; + break; default: break; } fillHandleObjects(selection_center, ids, mat_xyz, color_xyz, axis_mat); @@ -317,8 +336,16 @@ void RendererService::drawCurrentHandleObjects() { } if (current_action == haMove || current_action == haScale) { switch (current_action) { - case haMove : ids[0] = hmMoveXY ; ids[1] = hmMoveXZ ; ids[2] = hmMoveYZ ; break; - case haScale : ids[0] = hmScaleXY ; ids[1] = hmScaleXZ ; ids[2] = hmScaleYZ ; break; + case haMove: + ids[0] = hmMoveXY; + ids[1] = hmMoveXZ; + ids[2] = hmMoveYZ; + break; + case haScale: + ids[0] = hmScaleXY; + ids[1] = hmScaleXZ; + ids[2] = hmScaleYZ; + break; default: break; } hm = handle_ms_2_mesh; @@ -328,8 +355,10 @@ void RendererService::drawCurrentHandleObjects() { hm->draw(r->view, 3); if (current_action == haScale) { hm = handle_scale_3_mesh; - QVector mv; mv.resize(1); - QVector cv; cv.fill(QVector4D(1, 1, 0.5, 1), 1); + QVector mv; + mv.resize(1); + QVector cv; + cv.fill(QVector4D(1, 1, 0.5, 1), 1); ids[0] = hmMaxScale; fillHandleObjects(selection_center, ids, mv, cv, axis_mat, 1); hm->loadObjects(r->view, cur_objects); @@ -360,7 +389,6 @@ void RendererService::drawLights() { box_mesh->loadObjects(v, cur_aims); box_mesh->loadSelections(v, rs.cur_selections_); box_mesh->draw(v, cur_aims.size()); - } @@ -387,7 +415,6 @@ void RendererService::drawLightsFrame(QColor color) { box_mesh_f->loadObjects(v, cur_aims); box_mesh_f->loadSelections(v, rs.cur_selections_); box_mesh_f->draw(v, cur_aims.size()); - } @@ -405,7 +432,6 @@ void RendererService::drawCameras() { box_mesh->loadObjects(v, cur_aims); box_mesh->loadSelections(v, rs.cur_selections_); box_mesh->draw(v, cur_aims.size()); - } @@ -426,7 +452,6 @@ void RendererService::drawCamerasFrame(QColor color) { box_mesh_f->loadObjects(v, cur_aims); box_mesh_f->loadSelections(v, rs.cur_selections_); box_mesh_f->draw(v, cur_aims.size()); - } @@ -439,9 +464,9 @@ void RendererService::setObjectsColor(QVector & ol, QColor col) { void RendererService::renderService() { QOpenGLShaderProgram * prog = 0; - QOpenGLExtraFunctions * f = r->view; - size_full_scale = tan(r->view->camera()->FOV() / 2. * deg2rad) * size_vp_scale; - v_mat = r->view->camera()->fullViewMatrix(); + QOpenGLExtraFunctions * f = r->view; + size_full_scale = tan(r->view->camera()->FOV() / 2. * deg2rad) * size_vp_scale; + v_mat = r->view->camera()->fullViewMatrix(); f->glEnable(GL_MULTISAMPLE); glEnableDepth(); f->glClear(GL_DEPTH_BUFFER_BIT); @@ -460,14 +485,13 @@ void RendererService::renderService() { prog->setUniformValue("z_offset", -1.E-2f); drawLightsFrame(Qt::black); drawCamerasFrame(Qt::black); - } if (r->bindShader(Renderer::srServiceLine, &prog)) { r->setUniformCamera(prog, r->view->camera()); line_object.color = QColor2QVector(Qt::white); - line_spot_f ->loadObject(f, line_object); + line_spot_f->loadObject(f, line_object); line_camera_f->loadObject(f, line_object); - line_spot_f ->draw(f, 1); + line_spot_f->draw(f, 1); line_camera_f->draw(f, 1); } glEnable(GL_CULL_FACE); @@ -488,7 +512,6 @@ void RendererService::renderService() { r->setUniformCamera(prog, axis_camera, true, axis_viewport); axis_mesh->draw(f, 3); f->glViewport(0, 0, r->view->width(), r->view->height()); - } f->glDisable(GL_MULTISAMPLE); } @@ -496,9 +519,9 @@ void RendererService::renderService() { Mesh * RendererService::currentHandleMesh() { switch (current_action) { - case haMove : return handle_move_mesh; + case haMove: return handle_move_mesh; case haRotate: return handle_rotate_mesh; - case haScale : return handle_scale_mesh; + case haScale: return handle_scale_mesh; default: break; } return 0; diff --git a/renderer_service.h b/renderer_service.h index 9829e9f..bef6b06 100644 --- a/renderer_service.h +++ b/renderer_service.h @@ -1,19 +1,19 @@ /* - QGL RendererService - Ivan Pelipenko peri4ko@yandex.ru + QGL RendererService + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef RENDERER_SERVICE_H @@ -21,6 +21,7 @@ #include "glframebuffer.h" #include "glshaders_types.h" + #include @@ -70,7 +71,12 @@ public: void fillXYZObjects(); void fillOmniObjects(); void fillAimedObjects(const ObjectBaseList & objects, Mesh * line_mesh); - void fillHandleObjects(QVector3D center, HandleMesh ids[], const QVector & mats, const QVector & colors, QMatrix4x4 add_mat, int count = 3); + void fillHandleObjects(QVector3D center, + HandleMesh ids[], + const QVector & mats, + const QVector & colors, + QMatrix4x4 add_mat, + int count = 3); bool calculateCenter(); void drawCurrentHandleObjects(); void drawLights(); @@ -79,16 +85,16 @@ public: void drawCamerasFrame(QColor color); void setObjectsColor(QVector & ol, QColor col); void renderService(); - void setCurrentAction(HandleAction ha) {current_action = ha;} + void setCurrentAction(HandleAction ha) { current_action = ha; } Mesh * currentHandleMesh(); private: Renderer * r; - Mesh * axis_mesh, * handle_move_mesh, * handle_rotate_mesh, * handle_scale_mesh; - Mesh * handle_ms_2_mesh, * handle_scale_3_mesh; - Mesh * box_mesh_f, * omni_mesh_f, * cone_mesh_f, * camera_mesh_f; - Mesh * box_mesh, * omni_mesh, * cone_mesh, * camera_mesh; - Mesh * line_spot_f, * line_camera_f; + Mesh *axis_mesh, *handle_move_mesh, *handle_rotate_mesh, *handle_scale_mesh; + Mesh *handle_ms_2_mesh, *handle_scale_3_mesh; + Mesh *box_mesh_f, *omni_mesh_f, *cone_mesh_f, *camera_mesh_f; + Mesh *box_mesh, *omni_mesh, *cone_mesh, *camera_mesh; + Mesh *line_spot_f, *line_camera_f; QMatrix4x4 v_mat, axis_mat; QVector3D selection_center; QVector mat_xyz, mat_ms2; diff --git a/tonemapping_proc.cpp b/tonemapping_proc.cpp index c3de4c3..d72960e 100644 --- a/tonemapping_proc.cpp +++ b/tonemapping_proc.cpp @@ -1,47 +1,50 @@ /* - QGL TonemappingProc - Ivan Pelipenko peri4ko@yandex.ru + QGL TonemappingProc + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #define GL_GLEXT_PROTOTYPES -#include #include "tonemapping_proc.h" + #include "qglview.h" + +#include #include using namespace QGLEngineShaders; -TonemappingProc::TonemappingProc(Renderer * rend): QThread(), r(rend), - fbo_1x1(r->view, 1, false, GL_RGB32F), - fbomm(r->fbo_out, Renderer::obrSum, 3), - buffer_vbo(GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW) { - shader_sum = 0; +TonemappingProc::TonemappingProc(Renderer * rend) + : QThread() + , r(rend) + , fbo_1x1(r->view, 1, false, GL_RGB32F) + , fbomm(r->fbo_out, Renderer::obrSum, 3) + , buffer_vbo(GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW) { + shader_sum = 0; timer_delim = 0; frame_max = cur_max = 1.; need_render_sum = exit_ = false; - enabled = false; - timer_tone = startTimer(10); + enabled = false; + timer_tone = startTimer(10); } TonemappingProc::~TonemappingProc() { exit_ = true; - if (!wait(1000)) - terminate(); + if (!wait(1000)) terminate(); killTimer(timer_tone); if (shader_sum) delete shader_sum; } @@ -70,13 +73,13 @@ void TonemappingProc::init() { void TonemappingProc::resize() { QOpenGLExtraFunctions * f = r->view; fbomm.resize(); - int pcnt = fbomm.width(fbomm.lastLevel()) * fbomm.height(fbomm.lastLevel()); + int pcnt = fbomm.width(fbomm.lastLevel()) * fbomm.height(fbomm.lastLevel()); QVector _data; _data.resize(pcnt); pcnt = -1; for (int x = 0; x < fbomm.width(fbomm.lastLevel()); ++x) for (int y = 0; y < fbomm.height(fbomm.lastLevel()); ++y) - _data[++pcnt] = Vector2i(x,y); + _data[++pcnt] = Vector2i(x, y); buffer_vbo.bind(f); buffer_vbo.resize(f, _data.size() * sizeof(Vector2i)); buffer_vbo.load(f, _data.constData(), _data.size() * sizeof(Vector2i)); @@ -85,8 +88,7 @@ void TonemappingProc::resize() { void TonemappingProc::timerEvent(QTimerEvent * e) { if (!fbo_1x1.isInit() || !enabled) return; - if (timer_delim == 0) - need_render_sum = true; + if (timer_delim == 0) need_render_sum = true; timer_delim = (timer_delim + 1) % 10; mutex.lock(); float fmax = frame_max; @@ -98,7 +100,7 @@ void TonemappingProc::timerEvent(QTimerEvent * e) { void TonemappingProc::renderSum(Framebuffer & fbo_src, int index) { QOpenGLExtraFunctions * f = r->view; - int pcnt = fbo_src.width() * fbo_src.height(); + int pcnt = fbo_src.width() * fbo_src.height(); fbo_src.bindColorTexture(index); fbo_1x1.bind(); glClearFramebuffer(); @@ -139,8 +141,7 @@ void TonemappingProc::run() { mutex.unlock(); float max = calcHistogram(data); last_max << max; - if (last_max.size() > 5) - last_max.removeAt(0); + if (last_max.size() > 5) last_max.removeAt(0); float cm = last_max[0]; for (int i = 1; i < last_max.size(); ++i) cm += last_max[i]; @@ -156,9 +157,9 @@ float TonemappingProc::calcHistogram(const QVector & data) { if (data.isEmpty()) return 1.f; float max = 0.; QVector3D luma(0.299, 0.587, 0.114); - foreach (const QVector4D & p, data) { + foreach(const QVector4D & p, data) { float l = QVector3D::dotProduct(p.toVector3D(), luma); - max = qMax(max, l); + max = qMax(max, l); } return max; } diff --git a/tonemapping_proc.h b/tonemapping_proc.h index e972883..46caf30 100644 --- a/tonemapping_proc.h +++ b/tonemapping_proc.h @@ -1,32 +1,33 @@ /* - QGL TonemappingProc - Ivan Pelipenko peri4ko@yandex.ru + QGL TonemappingProc + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef TONEMAPPING_PROC_H #define TONEMAPPING_PROC_H #include "glframebuffer_mipmap.h" + #include -class TonemappingProc: public QThread -{ +class TonemappingProc: public QThread { friend class Renderer; friend class QGLView; + public: TonemappingProc(Renderer * rend); virtual ~TonemappingProc(); diff --git a/widgets/material_editor.cpp b/widgets/material_editor.cpp index e6636fe..06967ff 100644 --- a/widgets/material_editor.cpp +++ b/widgets/material_editor.cpp @@ -1,40 +1,41 @@ /* - QGL MaterialEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL MaterialEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "material_editor.h" -#include "ui_material_editor.h" + #include "glmaterial.h" +#include "ui_material_editor.h" MaterialEditor::MaterialEditor(QWidget * parent): QWidget(parent) { ui = new Ui::MaterialEditor(); ui->setupUi(this); - ui->mapDiffuse ->configure(tr("Diffuse"), true); - ui->mapNormal ->configure(tr("Normal")); + ui->mapDiffuse->configure(tr("Diffuse"), true); + ui->mapNormal->configure(tr("Normal")); ui->mapMetalness->configure(tr("Metalness")); ui->mapRoughness->configure(tr("Roughness")); - ui->mapEmission ->configure(tr("Emission"), true); - ui->mapRelief ->configure(tr("Relief")); + ui->mapEmission->configure(tr("Emission"), true); + ui->mapRelief->configure(tr("Relief")); ui->checkGlass->hide(); ui->frameReflection->hide(); ui->label_13->hide(); - mat = 0; - active = true; + mat = 0; + active = true; ignore_next = 0; } @@ -42,90 +43,94 @@ MaterialEditor::MaterialEditor(QWidget * parent): QWidget(parent) { void MaterialEditor::changeEvent(QEvent * e) { QWidget::changeEvent(e); switch (e->type()) { - case QEvent::LanguageChange: - ui->retranslateUi(this); - break; - default: - break; + case QEvent::LanguageChange: ui->retranslateUi(this); break; + default: break; } } void MaterialEditor::materialChanged() { if (!active || !mat) return; - ignore_next = 2; - mat->_changed = true; - mat->color_diffuse = ui->mapDiffuse ->color(); + ignore_next = 2; + mat->_changed = true; + mat->color_diffuse = ui->mapDiffuse->color(); mat->color_emission = ui->mapEmission->color(); - mat->transparency = ui->spinTransparent->value(); - mat->reflectivity = ui->spinReflect->value(); - mat->iof = ui->spinIOF->value(); - mat->dispersion = ui->spinDispersion->value(); - mat->glass = ui->checkGlass->isChecked(); - //emit changed(); + mat->transparency = ui->spinTransparent->value(); + mat->reflectivity = ui->spinReflect->value(); + mat->iof = ui->spinIOF->value(); + mat->dispersion = ui->spinDispersion->value(); + mat->glass = ui->checkGlass->isChecked(); + // emit changed(); } void MaterialEditor::setMaterial(Material * m) { if (ignore_next > 0) { - //ignore_next = false; + // ignore_next = false; return; } active = false; - mat = m; + mat = m; setEnabled(m); if (!mat) return; - ui->mapDiffuse ->setColor(mat->color_diffuse ); + ui->mapDiffuse->setColor(mat->color_diffuse); ui->mapEmission->setColor(mat->color_emission); ui->spinTransparent->setValue(mat->transparency); ui->spinReflect->setValue(mat->reflectivity); ui->spinIOF->setValue(mat->iof); ui->spinDispersion->setValue(mat->dispersion); ui->checkGlass->setChecked(mat->glass); - ui->mapDiffuse ->setMap(&(mat->map_diffuse )); - ui->mapNormal ->setMap(&(mat->map_normal )); + ui->mapDiffuse->setMap(&(mat->map_diffuse)); + ui->mapNormal->setMap(&(mat->map_normal)); ui->mapMetalness->setMap(&(mat->map_metalness)); ui->mapRoughness->setMap(&(mat->map_roughness)); - ui->mapEmission ->setMap(&(mat->map_emission )); - ui->mapRelief ->setMap(&(mat->map_relief )); - /*ui->lineReflFront->setProperty("GLpath", mat->map_reflection.path(0)); ui->lineReflFront->setText(QFileInfo(mat->map_reflection.path(0)).fileName()); - ui->lineReflBack->setProperty("GLpath", mat->map_reflection.path(1)); ui->lineReflBack->setText(QFileInfo(mat->map_reflection.path(1)).fileName()); - ui->lineReflLeft->setProperty("GLpath", mat->map_reflection.path(2)); ui->lineReflLeft->setText(QFileInfo(mat->map_reflection.path(2)).fileName()); - ui->lineReflRight->setProperty("GLpath", mat->map_reflection.path(3)); ui->lineReflRight->setText(QFileInfo(mat->map_reflection.path(3)).fileName()); - ui->lineReflTop->setProperty("GLpath", mat->map_reflection.path(4)); ui->lineReflTop->setText(QFileInfo(mat->map_reflection.path(4)).fileName()); - ui->lineReflBottom->setProperty("GLpath", mat->map_reflection.path(5)); ui->lineReflBottom->setText(QFileInfo(mat->map_reflection.path(5)).fileName()); - */active = true; + ui->mapEmission->setMap(&(mat->map_emission)); + ui->mapRelief->setMap(&(mat->map_relief)); + /*ui->lineReflFront->setProperty("GLpath", mat->map_reflection.path(0)); + ui->lineReflFront->setText(QFileInfo(mat->map_reflection.path(0)).fileName()); ui->lineReflBack->setProperty("GLpath", + mat->map_reflection.path(1)); ui->lineReflBack->setText(QFileInfo(mat->map_reflection.path(1)).fileName()); + ui->lineReflLeft->setProperty("GLpath", mat->map_reflection.path(2)); + ui->lineReflLeft->setText(QFileInfo(mat->map_reflection.path(2)).fileName()); ui->lineReflRight->setProperty("GLpath", + mat->map_reflection.path(3)); ui->lineReflRight->setText(QFileInfo(mat->map_reflection.path(3)).fileName()); + ui->lineReflTop->setProperty("GLpath", mat->map_reflection.path(4)); + ui->lineReflTop->setText(QFileInfo(mat->map_reflection.path(4)).fileName()); ui->lineReflBottom->setProperty("GLpath", + mat->map_reflection.path(5)); ui->lineReflBottom->setText(QFileInfo(mat->map_reflection.path(5)).fileName()); + */ + active = true; } /* Material MaterialEditor::material() { - Material m; - mat->color_diffuse = ui->colorDiffuse ->color(); - mat->color_specular = ui->colorSpecular->color(); - mat->color_emission = ui->colorEmission->color(); - mat->transparency = ui->spinTransparent->value(); - mat->reflectivity = ui->spinReflect->value(); - mat->iof = ui->spinIOF->value(); - mat->dispersion = ui->spinDispersion->value(); - mat->glass = ui->checkGlass->isChecked(); - mat->map_diffuse = ui->mapDiffuse->map(); - mat->map_normal = ui->mapNormal->map(); - mat->map_specular = ui->mapSpecular->map(); - mat->map_roughness = ui->mapRoughness->map(); - mat->map_emission = ui->mapEmission->map(); - mat->map_relief = ui->mapRelief->map(); - mat->map_reflection.setPath(0, ui->lineReflFront->property("GLpath").toString()); - mat->map_reflection.setPath(1, ui->lineReflBack->property("GLpath").toString()); - mat->map_reflection.setPath(2, ui->lineReflLeft->property("GLpath").toString()); - mat->map_reflection.setPath(3, ui->lineReflRight->property("GLpath").toString()); - mat->map_reflection.setPath(4, ui->lineReflTop->property("GLpath").toString()); - mat->map_reflection.setPath(5, ui->lineReflBottom->property("GLpath").toString()); - return m; + Material m; + mat->color_diffuse = ui->colorDiffuse ->color(); + mat->color_specular = ui->colorSpecular->color(); + mat->color_emission = ui->colorEmission->color(); + mat->transparency = ui->spinTransparent->value(); + mat->reflectivity = ui->spinReflect->value(); + mat->iof = ui->spinIOF->value(); + mat->dispersion = ui->spinDispersion->value(); + mat->glass = ui->checkGlass->isChecked(); + mat->map_diffuse = ui->mapDiffuse->map(); + mat->map_normal = ui->mapNormal->map(); + mat->map_specular = ui->mapSpecular->map(); + mat->map_roughness = ui->mapRoughness->map(); + mat->map_emission = ui->mapEmission->map(); + mat->map_relief = ui->mapRelief->map(); + mat->map_reflection.setPath(0, ui->lineReflFront->property("GLpath").toString()); + mat->map_reflection.setPath(1, ui->lineReflBack->property("GLpath").toString()); + mat->map_reflection.setPath(2, ui->lineReflLeft->property("GLpath").toString()); + mat->map_reflection.setPath(3, ui->lineReflRight->property("GLpath").toString()); + mat->map_reflection.setPath(4, ui->lineReflTop->property("GLpath").toString()); + mat->map_reflection.setPath(5, ui->lineReflBottom->property("GLpath").toString()); + return m; } */ void MaterialEditor::on_buttonReflFrontSelect_clicked() { - QString str = QFileDialog::getOpenFileName(this, "Select image", ui->lineReflFront->property("GLpath").toString(), "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); + QString str = QFileDialog::getOpenFileName(this, + "Select image", + ui->lineReflFront->property("GLpath").toString(), + "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); if (str.isEmpty()) return; ui->lineReflFront->setProperty("GLpath", str); ui->lineReflFront->setText(QFileInfo(str).fileName()); @@ -134,7 +139,10 @@ void MaterialEditor::on_buttonReflFrontSelect_clicked() { void MaterialEditor::on_buttonReflBackSelect_clicked() { - QString str = QFileDialog::getOpenFileName(this, "Select image", ui->lineReflBack->property("GLpath").toString(), "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); + QString str = QFileDialog::getOpenFileName(this, + "Select image", + ui->lineReflBack->property("GLpath").toString(), + "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); if (str.isEmpty()) return; ui->lineReflBack->setProperty("GLpath", str); ui->lineReflBack->setText(QFileInfo(str).fileName()); @@ -143,7 +151,10 @@ void MaterialEditor::on_buttonReflBackSelect_clicked() { void MaterialEditor::on_buttonReflLeftSelect_clicked() { - QString str = QFileDialog::getOpenFileName(this, "Select image", ui->lineReflLeft->property("GLpath").toString(), "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); + QString str = QFileDialog::getOpenFileName(this, + "Select image", + ui->lineReflLeft->property("GLpath").toString(), + "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); if (str.isEmpty()) return; ui->lineReflLeft->setProperty("GLpath", str); ui->lineReflLeft->setText(QFileInfo(str).fileName()); @@ -152,7 +163,10 @@ void MaterialEditor::on_buttonReflLeftSelect_clicked() { void MaterialEditor::on_buttonReflRightSelect_clicked() { - QString str = QFileDialog::getOpenFileName(this, "Select image", ui->lineReflRight->property("GLpath").toString(), "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); + QString str = QFileDialog::getOpenFileName(this, + "Select image", + ui->lineReflRight->property("GLpath").toString(), + "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); if (str.isEmpty()) return; ui->lineReflRight->setProperty("GLpath", str); ui->lineReflRight->setText(QFileInfo(str).fileName()); @@ -161,7 +175,10 @@ void MaterialEditor::on_buttonReflRightSelect_clicked() { void MaterialEditor::on_buttonReflTopSelect_clicked() { - QString str = QFileDialog::getOpenFileName(this, "Select image", ui->lineReflTop->property("GLpath").toString(), "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); + QString str = QFileDialog::getOpenFileName(this, + "Select image", + ui->lineReflTop->property("GLpath").toString(), + "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); if (str.isEmpty()) return; ui->lineReflTop->setProperty("GLpath", str); ui->lineReflTop->setText(QFileInfo(str).fileName()); @@ -170,7 +187,10 @@ void MaterialEditor::on_buttonReflTopSelect_clicked() { void MaterialEditor::on_buttonReflBottomSelect_clicked() { - QString str = QFileDialog::getOpenFileName(this, "Select image", ui->lineReflBottom->property("GLpath").toString(), "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); + QString str = QFileDialog::getOpenFileName(this, + "Select image", + ui->lineReflBottom->property("GLpath").toString(), + "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); if (str.isEmpty()) return; ui->lineReflBottom->setProperty("GLpath", str); ui->lineReflBottom->setText(QFileInfo(str).fileName()); diff --git a/widgets/material_editor.h b/widgets/material_editor.h index 16bc29d..2210d58 100644 --- a/widgets/material_editor.h +++ b/widgets/material_editor.h @@ -1,35 +1,36 @@ /* - QGL MaterialEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL MaterialEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef MATERIAL_EDITOR_H #define MATERIAL_EDITOR_H -#include #include "gltypes.h" +#include + namespace Ui { - class MaterialEditor; +class MaterialEditor; } -class MaterialEditor: public QWidget -{ +class MaterialEditor: public QWidget { friend class MaterialsEditor; Q_OBJECT + public: explicit MaterialEditor(QWidget * parent = 0); @@ -61,7 +62,6 @@ private slots: signals: void changed(); - }; #endif // MATERIAL_EDITOR_H diff --git a/widgets/material_map_editor.cpp b/widgets/material_map_editor.cpp index 283aa49..5526b4b 100644 --- a/widgets/material_map_editor.cpp +++ b/widgets/material_map_editor.cpp @@ -1,25 +1,27 @@ /* - QGL MaterialMapEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL MaterialMapEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "material_map_editor.h" -#include "ui_material_map_editor.h" -#include "gltexture_manager.h" + #include "glmaterial.h" +#include "gltexture_manager.h" +#include "ui_material_map_editor.h" + #include @@ -34,11 +36,8 @@ MaterialMapEditor::MaterialMapEditor(QWidget * parent): QWidget(parent) { void MaterialMapEditor::changeEvent(QEvent * e) { QWidget::changeEvent(e); switch (e->type()) { - case QEvent::LanguageChange: - ui->retranslateUi(this); - break; - default: - break; + case QEvent::LanguageChange: ui->retranslateUi(this); break; + default: break; } } @@ -57,7 +56,7 @@ void MaterialMapEditor::updateIcon() { void MaterialMapEditor::mapChanged() { if (!active || !map) return; - active = false; + active = false; map->use_bitmap = ui->checkMap->isChecked(); if (ui->checkMap->isChecked()) { map->color_amount = ui->sliderAmount->value(); @@ -75,7 +74,7 @@ void MaterialMapEditor::mapChanged() { void MaterialMapEditor::setMap(Map * m) { active = false; - map = m; + map = m; setEnabled(m); if (!map) return; ui->stackedWidget->setCurrentIndex(map->use_bitmap ? 1 : 0); @@ -85,7 +84,8 @@ void MaterialMapEditor::setMap(Map * m) { ui->sliderOffset->setValue(map->color_offset); ui->spinScaleX->setValue(map->bitmap_scale.x()); ui->spinScaleY->setValue(map->bitmap_scale.y()); - ui->linePath->setProperty("GLpath", map->bitmap_path); ui->linePath->setText(QFileInfo(map->bitmap_path).fileName()); + ui->linePath->setProperty("GLpath", map->bitmap_path); + ui->linePath->setText(QFileInfo(map->bitmap_path).fileName()); updateIcon(); active = true; } @@ -110,18 +110,21 @@ QColor MaterialMapEditor::color() const { /* Map MaterialMapEditor::map() { - Map m; - map->color_amount = ui->sliderAmount->value(); - map->color_offset = ui->sliderOffset->value(); - map->bitmap_scale.setX(ui->spinScaleX->value()); - map->bitmap_scale.setY(ui->spinScaleY->value()); - map->bitmap_path = ui->linePath->property("GLpath").toString(); - return m; + Map m; + map->color_amount = ui->sliderAmount->value(); + map->color_offset = ui->sliderOffset->value(); + map->bitmap_scale.setX(ui->spinScaleX->value()); + map->bitmap_scale.setY(ui->spinScaleY->value()); + map->bitmap_path = ui->linePath->property("GLpath").toString(); + return m; } */ void MaterialMapEditor::on_buttonSelect_clicked() { - QString str = QFileDialog::getOpenFileName(this, "Select image", ui->linePath->property("GLpath").toString(), "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); + QString str = QFileDialog::getOpenFileName(this, + "Select image", + ui->linePath->property("GLpath").toString(), + "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)"); if (str.isEmpty()) return; ui->linePath->setProperty("GLpath", str); ui->linePath->setText(QFileInfo(str).fileName()); diff --git a/widgets/material_map_editor.h b/widgets/material_map_editor.h index 8175247..a06f130 100644 --- a/widgets/material_map_editor.h +++ b/widgets/material_map_editor.h @@ -1,34 +1,35 @@ /* - QGL MaterialMapEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL MaterialMapEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef MATERIAL_MAP_EDITOR_H #define MATERIAL_MAP_EDITOR_H -#include #include "gltypes.h" +#include + namespace Ui { - class MaterialMapEditor; +class MaterialMapEditor; } -class MaterialMapEditor: public QWidget -{ +class MaterialMapEditor: public QWidget { Q_OBJECT + public: explicit MaterialMapEditor(QWidget * parent = 0); @@ -54,7 +55,6 @@ private slots: signals: void changed(); - }; #endif // MATERIAL_MAP_EDITOR_H diff --git a/widgets/materials_editor.cpp b/widgets/materials_editor.cpp index 467e613..7833423 100644 --- a/widgets/materials_editor.cpp +++ b/widgets/materials_editor.cpp @@ -1,29 +1,31 @@ /* - QGL MaterialsEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL MaterialsEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "materials_editor.h" -#include "ui_materials_editor.h" + +#include "glmaterial.h" #include "material_editor.h" #include "qglview.h" -#include "glmaterial.h" -#include -#include +#include "ui_materials_editor.h" + #include +#include +#include MaterialsEditor::MaterialsEditor(QWidget * parent): QWidget(parent) { @@ -40,7 +42,7 @@ void MaterialsEditor::assignQGLView(QGLView * v) { view = v; connect(view, SIGNAL(selectionChanged()), this, SLOT(selectionChanged())); connect(view, SIGNAL(materialsChanged()), this, SLOT(materialsChanged())); - connect(view, SIGNAL(materialThumbnailCreated(Material*)), this, SLOT(materialThumbnailCreated(Material*))); + connect(view, SIGNAL(materialThumbnailCreated(Material *)), this, SLOT(materialThumbnailCreated(Material *))); materialsChanged(); } @@ -50,11 +52,11 @@ bool MaterialsEditor::event(QEvent * e) { QSize sz = preferredIconSize(1.5, this); ui->comboMaterial->setIconSize(sz * 3); ui->buttonRename->setIconSize(sz); - ui->buttonAdd ->setIconSize(sz); - ui->buttonClone ->setIconSize(sz); + ui->buttonAdd->setIconSize(sz); + ui->buttonClone->setIconSize(sz); ui->buttonDelete->setIconSize(sz); ui->buttonAssign->setIconSize(sz); - ui->buttonUnset ->setIconSize(sz); + ui->buttonUnset->setIconSize(sz); } return QWidget::event(e); } @@ -63,19 +65,16 @@ bool MaterialsEditor::event(QEvent * e) { void MaterialsEditor::changeEvent(QEvent * e) { QWidget::changeEvent(e); switch (e->type()) { - case QEvent::LanguageChange: - ui->retranslateUi(this); - break; - default: - break; + case QEvent::LanguageChange: ui->retranslateUi(this); break; + default: break; } } Material * MaterialsEditor::currentMaterial() { if (!view) return 0; - int ind = ui->comboMaterial->currentIndex(); - QVector mats = view->scene()->getMaterials(); + int ind = ui->comboMaterial->currentIndex(); + QVector mats = view->scene()->getMaterials(); if (ind < 0 || ind >= mats.size()) return 0; return mats[ind]; } @@ -87,7 +86,7 @@ void MaterialsEditor::selectMaterial(Material * m) { return; } for (int i = 0; i < ui->comboMaterial->count(); ++i) { - if ((Material*)(ui->comboMaterial->itemData(i, Qt::UserRole + 1).value()) == m) { + if ((Material *)(ui->comboMaterial->itemData(i, Qt::UserRole + 1).value()) == m) { ui->comboMaterial->setCurrentIndex(i); break; } @@ -98,8 +97,7 @@ void MaterialsEditor::selectMaterial(Material * m) { int MaterialsEditor::indexByMaterial(Material * m) { if (!m) return -1; for (int i = 0; i < ui->comboMaterial->count(); ++i) { - if ((Material*)(ui->comboMaterial->itemData(i, Qt::UserRole + 1).value()) == m) - return i; + if ((Material *)(ui->comboMaterial->itemData(i, Qt::UserRole + 1).value()) == m) return i; } return -1; } @@ -107,7 +105,7 @@ int MaterialsEditor::indexByMaterial(Material * m) { void MaterialsEditor::selectionChanged() { if (!view) return; - //qDebug() << "selectionChanged"; + // qDebug() << "selectionChanged"; ObjectBase * o = view->selectedObject(); if (o) selectMaterial(o->material()); } @@ -121,7 +119,7 @@ void MaterialsEditor::materialsChanged() { Material * cm = currentMaterial(); ui->comboMaterial->clear(); if (!view) return; - QVector mats = view->scene()->getMaterials(); + QVector mats = view->scene()->getMaterials(); for (int i = 0; i < mats.size(); ++i) { Material * m = mats[i]; ui->comboMaterial->addItem(QString("[%1] " + m->name).arg(i + 1), QVariant(m->name)); @@ -129,15 +127,14 @@ void MaterialsEditor::materialsChanged() { ui->comboMaterial->setItemIcon(i, QIcon(QPixmap::fromImage(view->materialThumbnail(m)))); if (cm == m) ui->comboMaterial->setCurrentIndex(i); } - } void MaterialsEditor::materialThumbnailCreated(Material * m) { - //qDebug() << "materialThumbnailCreated" << m; + // qDebug() << "materialThumbnailCreated" << m; int i = indexByMaterial(m); if (i < 0 || !view) return; - //qDebug() << "materialThumbnailCreated set to" << i; + // qDebug() << "materialThumbnailCreated set to" << i; ui->comboMaterial->setItemIcon(i, QIcon(QPixmap::fromImage(view->materialThumbnail(m)))); } @@ -172,10 +169,10 @@ void MaterialsEditor::on_buttonClone_clicked() { Material * curm = currentMaterial(); if (!curm) return; Material * m = view->scene()->newMaterial(curm->name); - QString nm = m->name; - *m = *curm; - m->name = nm; - m->_changed = true; + QString nm = m->name; + *m = *curm; + m->name = nm; + m->_changed = true; materialsChanged(); selectMaterial(m); } @@ -192,9 +189,9 @@ void MaterialsEditor::on_buttonDelete_clicked() { void MaterialsEditor::on_buttonAssign_clicked() { if (!view) return; - Material * m = currentMaterial(); + Material * m = currentMaterial(); ObjectBaseList ol = view->selectedObjects(); - foreach (ObjectBase * o, ol) + foreach(ObjectBase * o, ol) o->setMaterial(m, true); } @@ -202,6 +199,6 @@ void MaterialsEditor::on_buttonAssign_clicked() { void MaterialsEditor::on_buttonUnset_clicked() { if (!view) return; ObjectBaseList ol = view->selectedObjects(); - foreach (ObjectBase * o, ol) + foreach(ObjectBase * o, ol) o->setMaterial(0, true); } diff --git a/widgets/materials_editor.h b/widgets/materials_editor.h index 5cd704d..3d4438d 100644 --- a/widgets/materials_editor.h +++ b/widgets/materials_editor.h @@ -1,34 +1,35 @@ /* - QGL MaterialsEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL MaterialsEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef MATERIALS_EDITOR_H #define MATERIALS_EDITOR_H -#include #include "gltypes.h" +#include + namespace Ui { - class MaterialsEditor; +class MaterialsEditor; } -class MaterialsEditor: public QWidget -{ +class MaterialsEditor: public QWidget { Q_OBJECT + public: explicit MaterialsEditor(QWidget * parent = 0); @@ -60,7 +61,6 @@ private slots: signals: void changed(); - }; #endif // MATERIALS_EDITOR_H diff --git a/widgets/object_editor.cpp b/widgets/object_editor.cpp index 6507ead..3bc4b23 100644 --- a/widgets/object_editor.cpp +++ b/widgets/object_editor.cpp @@ -1,33 +1,35 @@ /* - QGL ObjectEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL ObjectEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "object_editor.h" -#include "ui_object_editor.h" + #include "qglview.h" -#include +#include "ui_object_editor.h" + #include +#include ObjectEditor::ObjectEditor(QWidget * parent): QWidget(parent) { ui = new Ui::ObjectEditor(); ui->setupUi(this); - view = 0; - active = true; + view = 0; + active = true; ignore_next = false; on_comboLightType_currentIndexChanged(0); ui->widgetMain->setEnabled(false); @@ -35,31 +37,27 @@ ObjectEditor::ObjectEditor(QWidget * parent): QWidget(parent) { ui->spinAimDist->hide(); QObjectList ol; - ol << ui->spinPosX << ui->spinPosY << ui->spinPosZ << - ui->spinRotationX << ui->spinRotationY << ui->spinRotationZ << - ui->spinScaleX << ui->spinScaleY << ui->spinScaleZ; - foreach (QObject * o, ol) + ol << ui->spinPosX << ui->spinPosY << ui->spinPosZ << ui->spinRotationX << ui->spinRotationY << ui->spinRotationZ << ui->spinScaleX + << ui->spinScaleY << ui->spinScaleZ; + foreach(QObject * o, ol) connect(o, SIGNAL(valueChanged(double)), this, SLOT(spinChanged(double))); ol.clear(); - ol << ui->spinLightIntensity << ui->spinLightDecayConst << ui->spinLightDecayLinear << - ui->spinLightDecayQuadratic << ui->spinLightAngleStart << ui->spinLightAngleEnd << - ui->spinAimDist; - foreach (QObject * o, ol) + ol << ui->spinLightIntensity << ui->spinLightDecayConst << ui->spinLightDecayLinear << ui->spinLightDecayQuadratic + << ui->spinLightAngleStart << ui->spinLightAngleEnd << ui->spinAimDist; + foreach(QObject * o, ol) connect(o, SIGNAL(valueChanged(double)), this, SLOT(spinLightChanged(double))); ol.clear(); ol << ui->spinCameraFOV << ui->spinCameraDepthStart << ui->spinCameraRoll << ui->spinAimDist; - foreach (QObject * o, ol) + foreach(QObject * o, ol) connect(o, SIGNAL(valueChanged(double)), this, SLOT(spinCameraChanged(double))); ol.clear(); - ol << ui->checkVisible << ui->checkCastShadows << ui->checkReceiveShadows << - ui->checkAcceptLight << ui->checkAcceptFog << - ui->checkCameraMirrorX << ui->checkCameraMirrorY; - foreach (QObject * o, ol) + ol << ui->checkVisible << ui->checkCastShadows << ui->checkReceiveShadows << ui->checkAcceptLight << ui->checkAcceptFog + << ui->checkCameraMirrorX << ui->checkCameraMirrorY; + foreach(QObject * o, ol) connect(o, SIGNAL(toggled(bool)), this, SLOT(checkChanged(bool))); - } @@ -75,11 +73,8 @@ void ObjectEditor::assignQGLView(QGLView * v) { void ObjectEditor::changeEvent(QEvent * e) { QWidget::changeEvent(e); switch (e->type()) { - case QEvent::LanguageChange: - ui->retranslateUi(this); - break; - default: - break; + case QEvent::LanguageChange: ui->retranslateUi(this); break; + default: break; } } @@ -102,10 +97,10 @@ void ObjectEditor::selectionChanged() { return; } bool hl = !view->selectedLights().isEmpty(), hc = !view->selectedCameras().isEmpty(); - ui->groupLight ->setVisible(hl); - ui->groupCamera ->setVisible(hc); + ui->groupLight->setVisible(hl); + ui->groupCamera->setVisible(hc); ui->labelAimDist->setVisible(hl || hc); - ui->spinAimDist ->setVisible(hl || hc); + ui->spinAimDist->setVisible(hl || hc); ui->labelTitle->setText(tr("[%1 objects]").arg(sol.size())); } @@ -131,12 +126,12 @@ void ObjectEditor::setObject(ObjectBase * o) { bool is_l = o->type() == ObjectBase::glLight; bool is_c = o->type() == ObjectBase::glCamera; ui->labelAimDist->setVisible(is_l || is_c); - ui->spinAimDist ->setVisible(is_l || is_c); - ui->groupLight ->setVisible(is_l); - ui->groupCamera ->setVisible(is_c); + ui->spinAimDist->setVisible(is_l || is_c); + ui->groupLight->setVisible(is_l); + ui->groupCamera->setVisible(is_c); if (is_l) { - Light * l = globject_cast(o); - //bool is_dir = l->light_type == Light::Directional, is_cone = l->light_type == Light::Cone; + Light * l = globject_cast(o); + // bool is_dir = l->light_type == Light::Directional, is_cone = l->light_type == Light::Cone; ui->comboLightType->setCurrentIndex(l->light_type); ui->spinLightIntensity->setValue(l->intensity); ui->spinLightDecayConst->setValue(l->decay_const); @@ -148,7 +143,7 @@ void ObjectEditor::setObject(ObjectBase * o) { on_comboLightType_currentIndexChanged(ui->comboLightType->currentIndex()); } if (is_c) { - Camera * c = globject_cast(o); + Camera * c = globject_cast(o); ui->checkCameraMirrorX->setChecked(c->isMirrorX()); ui->checkCameraMirrorY->setChecked(c->isMirrorY()); ui->spinCameraFOV->setValue(c->FOV()); @@ -161,26 +156,24 @@ void ObjectEditor::setObject(ObjectBase * o) { void ObjectEditor::on_spinLightAngleStart_valueChanged(double v) { - if (ui->spinLightAngleEnd->value() < v) - ui->spinLightAngleEnd->setValue(v); + if (ui->spinLightAngleEnd->value() < v) ui->spinLightAngleEnd->setValue(v); } void ObjectEditor::on_spinLightAngleEnd_valueChanged(double v) { - if (ui->spinLightAngleStart->value() > v) - ui->spinLightAngleStart->setValue(v); + if (ui->spinLightAngleStart->value() > v) ui->spinLightAngleStart->setValue(v); } void ObjectEditor::on_comboLightType_currentIndexChanged(int index) { bool ang = (index == Light::Cone); - ui->labelLightAngle ->setVisible(ang); + ui->labelLightAngle->setVisible(ang); ui->labelLightAngleDash->setVisible(ang); ui->spinLightAngleStart->setVisible(ang); - ui->spinLightAngleEnd ->setVisible(ang); + ui->spinLightAngleEnd->setVisible(ang); if (!view || !active) return; - QList sll = view->selectedLights(); - foreach (Light * o, sll) { + QList sll = view->selectedLights(); + foreach(Light * o, sll) { o->light_type = (Light::Type)index; o->apply(); } @@ -190,10 +183,10 @@ void ObjectEditor::on_comboLightType_currentIndexChanged(int index) { void ObjectEditor::on_buttonColor_colorChanged(const QColor & v) { if (!view || !active) return; ObjectBaseList sol = view->selectedObjects(); - foreach (ObjectBase * o, sol) + foreach(ObjectBase * o, sol) o->setColor(v); - QList sll = view->selectedLights(); - foreach (Light * o, sll) + QList sll = view->selectedLights(); + foreach(Light * o, sll) o->apply(); } @@ -201,17 +194,17 @@ void ObjectEditor::on_buttonColor_colorChanged(const QColor & v) { void ObjectEditor::spinChanged(double v) { if (!view || !active) return; ObjectBaseList sol = view->selectedObjects(true); - QObject * s = sender(); - foreach (ObjectBase * o, sol) { - if (s == ui->spinPosX ) o->setPosX (v); - if (s == ui->spinPosY ) o->setPosY (v); - if (s == ui->spinPosZ ) o->setPosZ (v); + QObject * s = sender(); + foreach(ObjectBase * o, sol) { + if (s == ui->spinPosX) o->setPosX(v); + if (s == ui->spinPosY) o->setPosY(v); + if (s == ui->spinPosZ) o->setPosZ(v); if (s == ui->spinRotationX) o->setRotationX(v); if (s == ui->spinRotationY) o->setRotationY(v); if (s == ui->spinRotationZ) o->setRotationZ(v); - if (s == ui->spinScaleX ) o->setScaleX (v); - if (s == ui->spinScaleY ) o->setScaleY (v); - if (s == ui->spinScaleZ ) o->setScaleZ (v); + if (s == ui->spinScaleX) o->setScaleX(v); + if (s == ui->spinScaleY) o->setScaleY(v); + if (s == ui->spinScaleZ) o->setScaleZ(v); } ignore_next = true; } @@ -219,16 +212,16 @@ void ObjectEditor::spinChanged(double v) { void ObjectEditor::spinLightChanged(double v) { if (!view || !active) return; - QList sll = view->selectedLights(); - QObject * s = sender(); - foreach (Light * o, sll) { - if (s == ui->spinLightIntensity ) o->intensity = v; - if (s == ui->spinLightDecayConst ) o->decay_const = v; - if (s == ui->spinLightDecayLinear ) o->decay_linear = v; + QList sll = view->selectedLights(); + QObject * s = sender(); + foreach(Light * o, sll) { + if (s == ui->spinLightIntensity) o->intensity = v; + if (s == ui->spinLightDecayConst) o->decay_const = v; + if (s == ui->spinLightDecayLinear) o->decay_linear = v; if (s == ui->spinLightDecayQuadratic) o->decay_quadratic = v; - if (s == ui->spinLightAngleStart ) o->angle_start = v; - if (s == ui->spinLightAngleEnd ) o->angle_end = v; - if (s == ui->spinAimDist ) o->setDistance(v); + if (s == ui->spinLightAngleStart) o->angle_start = v; + if (s == ui->spinLightAngleEnd) o->angle_end = v; + if (s == ui->spinAimDist) o->setDistance(v); o->apply(); } ignore_next = true; @@ -237,13 +230,13 @@ void ObjectEditor::spinLightChanged(double v) { void ObjectEditor::spinCameraChanged(double v) { if (!view || !active) return; - QList scl = view->selectedCameras(); - QObject * s = sender(); - foreach (Camera * o, scl) { - if (s == ui->spinCameraFOV ) o->setFOV (v); - if (s == ui->spinCameraRoll ) o->setAngleRoll (v); + QList scl = view->selectedCameras(); + QObject * s = sender(); + foreach(Camera * o, scl) { + if (s == ui->spinCameraFOV) o->setFOV(v); + if (s == ui->spinCameraRoll) o->setAngleRoll(v); if (s == ui->spinCameraDepthStart) o->setDepthStart(v); - if (s == ui->spinAimDist ) o->setDistance (v); + if (s == ui->spinAimDist) o->setDistance(v); } ignore_next = true; } @@ -251,18 +244,18 @@ void ObjectEditor::spinCameraChanged(double v) { void ObjectEditor::checkChanged(bool v) { if (!view || !active) return; - ObjectBaseList sol = view->selectedObjects(); - QList scl = view->selectedCameras(); - QObject * s = sender(); - foreach (ObjectBase * o, sol) { - if (s == ui->checkVisible ) o->setVisible (v); - if (s == ui->checkCastShadows ) o->setCastShadows (v); + ObjectBaseList sol = view->selectedObjects(); + QList scl = view->selectedCameras(); + QObject * s = sender(); + foreach(ObjectBase * o, sol) { + if (s == ui->checkVisible) o->setVisible(v); + if (s == ui->checkCastShadows) o->setCastShadows(v); if (s == ui->checkReceiveShadows) o->setReceiveShadows(v); - if (s == ui->checkAcceptLight ) o->setAcceptLight (v); - if (s == ui->checkAcceptFog ) o->setAcceptFog (v); + if (s == ui->checkAcceptLight) o->setAcceptLight(v); + if (s == ui->checkAcceptFog) o->setAcceptFog(v); } - foreach (Camera * o, scl) { - if (s == ui->checkCameraMirrorX ) o->setMirrorX(v); - if (s == ui->checkCameraMirrorY ) o->setMirrorY(v); + foreach(Camera * o, scl) { + if (s == ui->checkCameraMirrorX) o->setMirrorX(v); + if (s == ui->checkCameraMirrorY) o->setMirrorY(v); } } diff --git a/widgets/object_editor.h b/widgets/object_editor.h index 89311f1..47b9ef7 100644 --- a/widgets/object_editor.h +++ b/widgets/object_editor.h @@ -1,34 +1,35 @@ /* - QGL ObjectEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL ObjectEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef OBJECT_EDITOR_H #define OBJECT_EDITOR_H -#include #include "globject.h" +#include + namespace Ui { - class ObjectEditor; +class ObjectEditor; } -class ObjectEditor: public QWidget -{ +class ObjectEditor: public QWidget { Q_OBJECT + public: explicit ObjectEditor(QWidget * parent = 0); @@ -48,7 +49,7 @@ private slots: void on_spinLightAngleEnd_valueChanged(double v); void on_comboLightType_currentIndexChanged(int index); - void on_buttonColor_colorChanged(const QColor &v); + void on_buttonColor_colorChanged(const QColor & v); void spinChanged(double v); void spinLightChanged(double v); void spinCameraChanged(double v); @@ -56,7 +57,6 @@ private slots: signals: void changed(); - }; #endif // OBJECT_EDITOR_H diff --git a/widgets/primitiveeditor.cpp b/widgets/primitiveeditor.cpp index a10de68..a6bc926 100644 --- a/widgets/primitiveeditor.cpp +++ b/widgets/primitiveeditor.cpp @@ -1,34 +1,36 @@ /* - QGL PrimitiveEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL PrimitiveEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "primitiveeditor.h" + +#include "glmesh.h" +#include "glprimitives.h" #include "ui_primitiveeditor.h" + #include #include -#include "glprimitives.h" -#include "glmesh.h" -PrimitiveEditor::PrimitiveEditor(QWidget *parent) : QWidget(parent), ui(new Ui::PrimitiveEditor) { - view = 0; +PrimitiveEditor::PrimitiveEditor(QWidget * parent): QWidget(parent), ui(new Ui::PrimitiveEditor) { + view = 0; can_replace = false; ui->setupUi(this); -#if QT_VERSION >= QT_VERSION_CHECK(5,12,0) +#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) ui->spinSegments->setStepType(QAbstractSpinBox::AdaptiveDecimalStepType); ui->spinSegments2->setStepType(QAbstractSpinBox::AdaptiveDecimalStepType); #endif @@ -58,12 +60,12 @@ PrimitiveEditor::PrimitiveEditor(QWidget *parent) : QWidget(parent), ui(new Ui:: editors[Torus] << ui->widgetAngle; QSet all; QMetaEnum me = metaObject()->enumerator(0); - for (int i=0; icomboPrimitives->addItem(me.key(i)); all.unite(QList2QSet(editors[(PrimitiveType)me.value(i)])); } all_editors = all.values(); - foreach (QWidget * w, all_editors) { + foreach(QWidget * w, all_editors) { if (w->layout()) w->layout()->setContentsMargins(0, layout()->spacing(), 0, 0); } @@ -85,93 +87,65 @@ void PrimitiveEditor::assignQGLView(QGLView * v) { Mesh * PrimitiveEditor::createMesh(QVariantList & params) { - Mesh * m = 0; + Mesh * m = 0; PrimitiveType pt = (PrimitiveType)ui->comboPrimitives->currentIndex(); params << pt; switch (pt) { - case Plane: - m = Primitive::plane(ui->spinWidth->value(), - ui->spinLength->value()); - params << ui->spinWidth->value() - << ui->spinLength->value(); - break; - case Cube: - m = Primitive::cube(ui->spinWidth->value(), - ui->spinLength->value(), - ui->spinHeight->value()); - params << ui->spinWidth->value() - << ui->spinLength->value() - << ui->spinHeight->value(); - break; - case Ellipsoid: - m = Primitive::ellipsoid(ui->spinSegments->value(), - ui->spinSegments2->value(), - ui->spinRadius->value(), - ui->spinAngle->value()); - params << ui->spinSegments->value() - << ui->spinSegments2->value() - << ui->spinRadius->value() - << ui->spinAngle->value(); - break; - case Disc: - m = Primitive::disc(ui->spinSegments->value(), - ui->spinRadius->value(), - ui->spinAngle->value()); - params << ui->spinSegments->value() - << ui->spinRadius->value() - << ui->spinAngle->value(); - break; - case Cone: - m = Primitive::cone(ui->spinSegments->value(), - ui->spinRadius->value(), - ui->spinHeight->value()); - params << ui->spinSegments->value() - << ui->spinRadius->value() - << ui->spinHeight->value(); - break; - case Cylinder: - m = Primitive::cylinder(ui->spinSegments->value(), - ui->spinRadius->value(), - ui->spinHeight->value(), - ui->spinAngle->value()); - params << ui->spinSegments->value() - << ui->spinRadius->value() - << ui->spinHeight->value() - << ui->spinAngle->value(); - break; - case Torus: - m = Primitive::torus(ui->spinSegments->value(), - ui->spinSegments2->value(), - ui->spinRadius->value(), - ui->spinRadius2->value(), - ui->spinAngle->value()); - params << ui->spinSegments->value() - << ui->spinSegments2->value() - << ui->spinRadius->value() - << ui->spinRadius2->value() - << ui->spinAngle->value(); - break; - default: return 0; + case Plane: + m = Primitive::plane(ui->spinWidth->value(), ui->spinLength->value()); + params << ui->spinWidth->value() << ui->spinLength->value(); + break; + case Cube: + m = Primitive::cube(ui->spinWidth->value(), ui->spinLength->value(), ui->spinHeight->value()); + params << ui->spinWidth->value() << ui->spinLength->value() << ui->spinHeight->value(); + break; + case Ellipsoid: + m = Primitive::ellipsoid(ui->spinSegments->value(), ui->spinSegments2->value(), ui->spinRadius->value(), ui->spinAngle->value()); + params << ui->spinSegments->value() << ui->spinSegments2->value() << ui->spinRadius->value() << ui->spinAngle->value(); + break; + case Disc: + m = Primitive::disc(ui->spinSegments->value(), ui->spinRadius->value(), ui->spinAngle->value()); + params << ui->spinSegments->value() << ui->spinRadius->value() << ui->spinAngle->value(); + break; + case Cone: + m = Primitive::cone(ui->spinSegments->value(), ui->spinRadius->value(), ui->spinHeight->value()); + params << ui->spinSegments->value() << ui->spinRadius->value() << ui->spinHeight->value(); + break; + case Cylinder: + m = Primitive::cylinder(ui->spinSegments->value(), ui->spinRadius->value(), ui->spinHeight->value(), ui->spinAngle->value()); + params << ui->spinSegments->value() << ui->spinRadius->value() << ui->spinHeight->value() << ui->spinAngle->value(); + break; + case Torus: + m = Primitive::torus(ui->spinSegments->value(), + ui->spinSegments2->value(), + ui->spinRadius->value(), + ui->spinRadius2->value(), + ui->spinAngle->value()); + params << ui->spinSegments->value() << ui->spinSegments2->value() << ui->spinRadius->value() << ui->spinRadius2->value() + << ui->spinAngle->value(); + break; + default: return 0; } params << ui->flipNormals->isChecked(); params << ui->colorButton->color(); - if (ui->flipNormals->isChecked()) - m->flipNormals(); + if (ui->flipNormals->isChecked()) m->flipNormals(); return m; } void PrimitiveEditor::showEditors() { - foreach (QWidget * w, all_editors) w->hide(); - PrimitiveType pt = (PrimitiveType)ui->comboPrimitives->currentIndex(); + foreach(QWidget * w, all_editors) + w->hide(); + PrimitiveType pt = (PrimitiveType)ui->comboPrimitives->currentIndex(); QList wds = editors[pt]; - foreach (QWidget * w, wds) w->show(); + foreach(QWidget * w, wds) + w->show(); } void PrimitiveEditor::selectionChanged() { - ObjectBase * so = view->selectedObject();\ - can_replace = false; + ObjectBase * so = view->selectedObject(); + can_replace = false; if (so) { QVariantList vl = so->property("primitive", &can_replace).toList(); if (can_replace && !vl.isEmpty()) { @@ -181,41 +155,41 @@ void PrimitiveEditor::selectionChanged() { case Plane: ui->spinWidth->setValue(vl.takeFirst().toDouble()); ui->spinLength->setValue(vl.takeFirst().toDouble()); - break; + break; case Cube: ui->spinWidth->setValue(vl.takeFirst().toDouble()); ui->spinLength->setValue(vl.takeFirst().toDouble()); ui->spinHeight->setValue(vl.takeFirst().toDouble()); - break; + break; case Ellipsoid: ui->spinSegments->setValue(vl.takeFirst().toDouble()); ui->spinSegments2->setValue(vl.takeFirst().toDouble()); ui->spinRadius->setValue(vl.takeFirst().toDouble()); ui->spinAngle->setValue(vl.takeFirst().toDouble()); - break; + break; case Disc: ui->spinSegments->setValue(vl.takeFirst().toDouble()); ui->spinRadius->setValue(vl.takeFirst().toDouble()); ui->spinAngle->setValue(vl.takeFirst().toDouble()); - break; + break; case Cone: ui->spinSegments->setValue(vl.takeFirst().toDouble()); ui->spinRadius->setValue(vl.takeFirst().toDouble()); ui->spinHeight->setValue(vl.takeFirst().toDouble()); - break; + break; case Cylinder: ui->spinSegments->setValue(vl.takeFirst().toDouble()); ui->spinRadius->setValue(vl.takeFirst().toDouble()); ui->spinHeight->setValue(vl.takeFirst().toDouble()); ui->spinAngle->setValue(vl.takeFirst().toDouble()); - break; + break; case Torus: ui->spinSegments->setValue(vl.takeFirst().toDouble()); ui->spinSegments2->setValue(vl.takeFirst().toDouble()); ui->spinRadius->setValue(vl.takeFirst().toDouble()); ui->spinRadius2->setValue(vl.takeFirst().toDouble()); ui->spinAngle->setValue(vl.takeFirst().toDouble()); - break; + break; } ui->flipNormals->setChecked(vl.takeFirst().toBool()); ui->colorButton->setColor(vl.takeFirst().value()); diff --git a/widgets/primitiveeditor.h b/widgets/primitiveeditor.h index 7d1b5a4..3b8b7f9 100644 --- a/widgets/primitiveeditor.h +++ b/widgets/primitiveeditor.h @@ -1,35 +1,37 @@ /* - QGL PrimitiveEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL PrimitiveEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef PRIMITIVEEDITOR_H #define PRIMITIVEEDITOR_H -#include #include "qglview.h" +#include + namespace Ui { class PrimitiveEditor; } -class PrimitiveEditor : public QWidget { +class PrimitiveEditor: public QWidget { Q_OBJECT + public: enum PrimitiveType { Plane, @@ -42,7 +44,7 @@ public: }; Q_ENUMS(PrimitiveType) - explicit PrimitiveEditor(QWidget *parent = nullptr); + explicit PrimitiveEditor(QWidget * parent = nullptr); ~PrimitiveEditor(); void assignQGLView(QGLView * v); @@ -51,9 +53,9 @@ protected: Mesh * createMesh(QVariantList & params); void showEditors(); - Ui::PrimitiveEditor *ui; + Ui::PrimitiveEditor * ui; QGLView * view; - QMap > editors; + QMap> editors; QList all_editors; bool can_replace; diff --git a/widgets/propertyeditor.cpp b/widgets/propertyeditor.cpp index bdacdee..82e54ea 100644 --- a/widgets/propertyeditor.cpp +++ b/widgets/propertyeditor.cpp @@ -1,57 +1,63 @@ /* - QGL PropertyEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL PropertyEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "propertyeditor.h" + +#include "clineedit.h" +#include "colorbutton.h" #include "qpointedit.h" #include "qrectedit.h" -#include "colorbutton.h" -#include "clineedit.h" -#include -#include + #include +#include +#include QWidget * Delegate::widgetForProperty(QWidget * parent, const QModelIndex & index) const { - QWidget * w = nullptr; - int type = 0; + QWidget * w = nullptr; + int type = 0; QVariant value = index.data(Qt::UserRole); if (index.data(Qt::UserRole + 2).toString() == "__flags") return nullptr; if (index.data(Qt::UserRole + 1).toString() == "__flag") { qulonglong key = index.data(Qt::UserRole).toULongLong(); - value = index.parent().data(Qt::UserRole); - //QMetaProperty prop = index.parent().data(Qt::UserRole + 1).value(); - w = new QCheckBox(parent); type = 14; ((QCheckBox*)w)->setChecked(((value.toULongLong() & key) == key && key != 0) || (value.toULongLong() == 0 && key == 0)); - ((QCheckBox*)w)->setText("0x" + QString::number(key, 16).toUpper()); - connect((QCheckBox*)w, SIGNAL(clicked(bool)), this, SLOT(changedFlag())); - //qDebug() << prop.enumerator().name(); + value = index.parent().data(Qt::UserRole); + // QMetaProperty prop = index.parent().data(Qt::UserRole + 1).value(); + w = new QCheckBox(parent); + type = 14; + ((QCheckBox *)w)->setChecked(((value.toULongLong() & key) == key && key != 0) || (value.toULongLong() == 0 && key == 0)); + ((QCheckBox *)w)->setText("0x" + QString::number(key, 16).toUpper()); + connect((QCheckBox *)w, SIGNAL(clicked(bool)), this, SLOT(changedFlag())); + // qDebug() << prop.enumerator().name(); } else { if (value.canConvert()) { PropertyValuePair prop = value.value(); if (prop.first.isEnumType()) { - w = new QComboBox(parent); type = 13; ((QComboBox*)w)->setCurrentIndex(value.toInt()); + w = new QComboBox(parent); + type = 13; + ((QComboBox *)w)->setCurrentIndex(value.toInt()); w->setProperty("__prop", QVariant::fromValue(prop.first)); QMetaEnum menum = prop.first.enumerator(); for (int i = 0; i < menum.keyCount(); ++i) { - ((QComboBox*)w)->addItem(QString(menum.key(i)) + " (0x" + QString::number(menum.value(i), 16).toUpper() + ")", menum.value(i)); - if (menum.value(i) == prop.second.toInt()) - ((QComboBox*)w)->setCurrentIndex(i); + ((QComboBox *)w) + ->addItem(QString(menum.key(i)) + " (0x" + QString::number(menum.value(i), 16).toUpper() + ")", menum.value(i)); + if (menum.value(i) == prop.second.toInt()) ((QComboBox *)w)->setCurrentIndex(i); } - connect((QComboBox*)w, SIGNAL(currentIndexChanged(int)), this, SLOT(changed())); + connect((QComboBox *)w, SIGNAL(currentIndexChanged(int)), this, SLOT(changed())); } } else { #if QT_VERSION_MAJOR <= 5 @@ -64,73 +70,129 @@ QWidget * Delegate::widgetForProperty(QWidget * parent, const QModelIndex & inde #else case QMetaType::Int: #endif - w = new QSpinBox(parent); type = 2; ((QSpinBox*)w)->setRange(-0x7FFFFFFF, 0x7FFFFFFF); connect((QSpinBox*)w, SIGNAL(valueChanged(int)), this, SLOT(changed())); break; + w = new QSpinBox(parent); + type = 2; + ((QSpinBox *)w)->setRange(-0x7FFFFFFF, 0x7FFFFFFF); + connect((QSpinBox *)w, SIGNAL(valueChanged(int)), this, SLOT(changed())); + break; #if QT_VERSION_MAJOR <= 5 case QVariant::UInt: #else case QMetaType::UInt: #endif - w = new QSpinBox(parent); type = 3; ((QSpinBox*)w)->setRange(0, 0xFFFFFFFF); connect((QSpinBox*)w, SIGNAL(valueChanged(int)), this, SLOT(changed())); break; + w = new QSpinBox(parent); + type = 3; + ((QSpinBox *)w)->setRange(0, 0xFFFFFFFF); + connect((QSpinBox *)w, SIGNAL(valueChanged(int)), this, SLOT(changed())); + break; #if QT_VERSION_MAJOR <= 5 case QVariant::LongLong: #else case QMetaType::LongLong: #endif - w = new QSpinBox(parent); type = 4; ((QSpinBox*)w)->setRange(-0x7FFFFFFF, 0x7FFFFFFF); connect((QSpinBox*)w, SIGNAL(valueChanged(int)), this, SLOT(changed())); break; + w = new QSpinBox(parent); + type = 4; + ((QSpinBox *)w)->setRange(-0x7FFFFFFF, 0x7FFFFFFF); + connect((QSpinBox *)w, SIGNAL(valueChanged(int)), this, SLOT(changed())); + break; #if QT_VERSION_MAJOR <= 5 case QVariant::ULongLong: #else case QMetaType::ULongLong: #endif - w = new QSpinBox(parent); type = 5; ((QSpinBox*)w)->setRange(0, 0xFFFFFFFF); connect((QSpinBox*)w, SIGNAL(valueChanged(int)), this, SLOT(changed())); break; + w = new QSpinBox(parent); + type = 5; + ((QSpinBox *)w)->setRange(0, 0xFFFFFFFF); + connect((QSpinBox *)w, SIGNAL(valueChanged(int)), this, SLOT(changed())); + break; #if QT_VERSION_MAJOR <= 5 case QVariant::Double: #else case QMetaType::Double: #endif - w = new QDoubleSpinBox(parent); type = 6; ((QDoubleSpinBox*)w)->setRange(-999999999, 999999999); ((QDoubleSpinBox*)w)->setDecimals(3); connect((QDoubleSpinBox*)w, SIGNAL(valueChanged(double)), this, SLOT(changed())); break; + w = new QDoubleSpinBox(parent); + type = 6; + ((QDoubleSpinBox *)w)->setRange(-999999999, 999999999); + ((QDoubleSpinBox *)w)->setDecimals(3); + connect((QDoubleSpinBox *)w, SIGNAL(valueChanged(double)), this, SLOT(changed())); + break; #if QT_VERSION_MAJOR <= 5 case QVariant::Bool: #else case QMetaType::Bool: #endif - w = new QCheckBox(parent); type = 7; ((QCheckBox*)w)->setChecked(value.toBool()); connect((QCheckBox*)w, SIGNAL(toggled(bool)), this, SLOT(changed())); break; + w = new QCheckBox(parent); + type = 7; + ((QCheckBox *)w)->setChecked(value.toBool()); + connect((QCheckBox *)w, SIGNAL(toggled(bool)), this, SLOT(changed())); + break; #if QT_VERSION_MAJOR <= 5 case QVariant::Color: #else case QMetaType::QColor: #endif - w = new ColorButton(parent); type = 8; ((ColorButton*)w)->setUseAlphaChannel(true); ((ColorButton*)w)->setColor(value.value()); connect((ColorButton*)w, SIGNAL(colorChanged(QColor)), this, SLOT(changed())); break; + w = new ColorButton(parent); + type = 8; + ((ColorButton *)w)->setUseAlphaChannel(true); + ((ColorButton *)w)->setColor(value.value()); + connect((ColorButton *)w, SIGNAL(colorChanged(QColor)), this, SLOT(changed())); + break; #if QT_VERSION_MAJOR <= 5 case QVariant::Point: #else case QMetaType::QPoint: #endif - w = new QPointEdit(parent); type = 9; ((QPointEdit*)w)->setDecimals(0); ((QPointEdit*)w)->setValue(QPointF(value.toPoint())); connect((QPointEdit*)w, SIGNAL(valueChanged(QPointF)), this, SLOT(changed())); break; + w = new QPointEdit(parent); + type = 9; + ((QPointEdit *)w)->setDecimals(0); + ((QPointEdit *)w)->setValue(QPointF(value.toPoint())); + connect((QPointEdit *)w, SIGNAL(valueChanged(QPointF)), this, SLOT(changed())); + break; #if QT_VERSION_MAJOR <= 5 case QVariant::PointF: #else case QMetaType::QPointF: #endif - w = new QPointEdit(parent); type = 10; ((QPointEdit*)w)->setDecimals(3); ((QPointEdit*)w)->setValue(value.toPointF()); connect((QPointEdit*)w, SIGNAL(valueChanged(QPointF)), this, SLOT(changed())); break; + w = new QPointEdit(parent); + type = 10; + ((QPointEdit *)w)->setDecimals(3); + ((QPointEdit *)w)->setValue(value.toPointF()); + connect((QPointEdit *)w, SIGNAL(valueChanged(QPointF)), this, SLOT(changed())); + break; #if QT_VERSION_MAJOR <= 5 case QVariant::Rect: #else case QMetaType::QRect: #endif - w = new QRectEdit(parent); type = 11; ((QRectEdit*)w)->setDecimals(0); ((QRectEdit*)w)->setValue(QRectF(value.toRect())); connect((QRectEdit*)w, SIGNAL(valueChanged(QRectF)), this, SLOT(changed())); break; + w = new QRectEdit(parent); + type = 11; + ((QRectEdit *)w)->setDecimals(0); + ((QRectEdit *)w)->setValue(QRectF(value.toRect())); + connect((QRectEdit *)w, SIGNAL(valueChanged(QRectF)), this, SLOT(changed())); + break; #if QT_VERSION_MAJOR <= 5 case QVariant::RectF: #else case QMetaType::QRectF: #endif - w = new QRectEdit(parent); type = 12; ((QRectEdit*)w)->setDecimals(3); ((QRectEdit*)w)->setValue(value.toRectF()); connect((QRectEdit*)w, SIGNAL(valueChanged(QRectF)), this, SLOT(changed())); break; + w = new QRectEdit(parent); + type = 12; + ((QRectEdit *)w)->setDecimals(3); + ((QRectEdit *)w)->setValue(value.toRectF()); + connect((QRectEdit *)w, SIGNAL(valueChanged(QRectF)), this, SLOT(changed())); + break; #if QT_VERSION_MAJOR <= 5 - case QVariant::String: default: + case QVariant::String: + default: #else - case QMetaType::QString: default: + case QMetaType::QString: + default: #endif - w = new CLineEdit(parent); type = 1; ((CLineEdit*)w)->setDefaultText(value.toString()); connect((CLineEdit*)w, SIGNAL(textChanged(QString)), this, SLOT(changed())); break; + w = new CLineEdit(parent); + type = 1; + ((CLineEdit *)w)->setDefaultText(value.toString()); + connect((CLineEdit *)w, SIGNAL(textChanged(QString)), this, SLOT(changed())); + break; } } } @@ -147,15 +209,18 @@ QWidget * Delegate::widgetForProperty(QWidget * parent, const QModelIndex & inde void Delegate::setWidgetProperty(QWidget * w, const QVariant & value) const { if (w == 0) return; switch (w->property("__type").toInt()) { - case 1: ((CLineEdit*)w)->setText(value.toString()); break; - case 2: case 3: case 4: case 5: ((QSpinBox*)w)->setValue(value.toInt()); break; - case 6: ((QDoubleSpinBox*)w)->setValue(value.toDouble()); break; - case 7: ((QCheckBox*)w)->setChecked(value.toBool()); break; - case 8: ((ColorButton*)w)->setColor(value.value()); break; - case 9: ((QPointEdit*)w)->setValue(value.value()); break; - case 10: ((QPointEdit*)w)->setValue(value.value()); break; - case 11: ((QRectEdit*)w)->setValue(value.value()); break; - case 12: ((QRectEdit*)w)->setValue(value.value()); break; + case 1: ((CLineEdit *)w)->setText(value.toString()); break; + case 2: + case 3: + case 4: + case 5: ((QSpinBox *)w)->setValue(value.toInt()); break; + case 6: ((QDoubleSpinBox *)w)->setValue(value.toDouble()); break; + case 7: ((QCheckBox *)w)->setChecked(value.toBool()); break; + case 8: ((ColorButton *)w)->setColor(value.value()); break; + case 9: ((QPointEdit *)w)->setValue(value.value()); break; + case 10: ((QPointEdit *)w)->setValue(value.value()); break; + case 11: ((QRectEdit *)w)->setValue(value.value()); break; + case 12: ((QRectEdit *)w)->setValue(value.value()); break; } } @@ -163,19 +228,22 @@ void Delegate::setWidgetProperty(QWidget * w, const QVariant & value) const { const QVariant Delegate::widgetProperty(QWidget * w) const { if (w == 0) return QVariant(); switch (w->property("__type").toInt()) { - case 1: return QVariant::fromValue(((CLineEdit*)w)->text()); break; - case 2: return QVariant::fromValue(((QSpinBox*)w)->value()); break; - case 3: return QVariant::fromValue(((QSpinBox*)w)->value()); break; - case 4: return QVariant::fromValue(((QSpinBox*)w)->value()); break; - case 5: return QVariant::fromValue(((QSpinBox*)w)->value()); break; - case 6: return QVariant::fromValue(((QDoubleSpinBox*)w)->value()); break; - case 7: return QVariant::fromValue(((QCheckBox*)w)->isChecked()); break; - case 8: return QVariant::fromValue(((ColorButton*)w)->color()); break; - case 9: return QVariant::fromValue(((QPointEdit*)w)->value().toPoint()); break; - case 10: return QVariant::fromValue(((QPointEdit*)w)->value()); break; - case 11: return QVariant::fromValue(((QRectEdit*)w)->value().toRect()); break; - case 12: return QVariant::fromValue(((QRectEdit*)w)->value()); break; - case 13: return QVariant::fromValue(PropertyValuePair(w->property("__prop").value(), ((QComboBox*)w)->itemData(((QComboBox*)w)->currentIndex()))); break; + case 1: return QVariant::fromValue(((CLineEdit *)w)->text()); break; + case 2: return QVariant::fromValue(((QSpinBox *)w)->value()); break; + case 3: return QVariant::fromValue(((QSpinBox *)w)->value()); break; + case 4: return QVariant::fromValue(((QSpinBox *)w)->value()); break; + case 5: return QVariant::fromValue(((QSpinBox *)w)->value()); break; + case 6: return QVariant::fromValue(((QDoubleSpinBox *)w)->value()); break; + case 7: return QVariant::fromValue(((QCheckBox *)w)->isChecked()); break; + case 8: return QVariant::fromValue(((ColorButton *)w)->color()); break; + case 9: return QVariant::fromValue(((QPointEdit *)w)->value().toPoint()); break; + case 10: return QVariant::fromValue(((QPointEdit *)w)->value()); break; + case 11: return QVariant::fromValue(((QRectEdit *)w)->value().toRect()); break; + case 12: return QVariant::fromValue(((QRectEdit *)w)->value()); break; + case 13: + return QVariant::fromValue( + PropertyValuePair(w->property("__prop").value(), ((QComboBox *)w)->itemData(((QComboBox *)w)->currentIndex()))); + break; default: return QVariant(); break; } return QVariant(); @@ -183,35 +251,44 @@ const QVariant Delegate::widgetProperty(QWidget * w) const { void Delegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const { - if (index.data(Qt::UserRole + 1).toString() != "__flag") - model->setData(index, widgetProperty(editor), Qt::UserRole); + if (index.data(Qt::UserRole + 1).toString() != "__flag") model->setData(index, widgetProperty(editor), Qt::UserRole); } void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const { QStyledItemDelegate::paint(painter, option, index); - QVariant value = index.data(Qt::UserRole); - QStyle * style = QApplication::style(); - QStyleOption * so = 0; + QVariant value = index.data(Qt::UserRole); + QStyle * style = QApplication::style(); + QStyleOption * so = 0; QStyleOptionComplex * soc = 0; QString text; QRect rect; - QPalette::ColorRole role = (option.state.testFlag(QStyle::State_Selected) && option.state.testFlag(QStyle::State_Active) ? QPalette::HighlightedText : QPalette::WindowText); + QPalette::ColorRole role = + (option.state.testFlag(QStyle::State_Selected) && option.state.testFlag(QStyle::State_Active) ? QPalette::HighlightedText + : QPalette::WindowText); if (index.data(Qt::UserRole + 2).toString() == "__flags") { text = "0x" + QString::number(value.toInt(), 16).toUpper(); - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, text, role); + style->drawItemText(painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + text, + role); return; } if (index.data(Qt::UserRole + 1) == "__flag") { - qulonglong key = index.data(Qt::UserRole).toULongLong(); - value = index.parent().data(Qt::UserRole); - so = new QStyleOptionButton(); - so->rect = option.rect; - so->palette = option.palette; + qulonglong key = index.data(Qt::UserRole).toULongLong(); + value = index.parent().data(Qt::UserRole); + so = new QStyleOptionButton(); + so->rect = option.rect; + so->palette = option.palette; so->fontMetrics = option.fontMetrics; - ((QStyleOptionButton*)so)->state = (((value.toULongLong() & key) == key && key != 0) || (value.toULongLong() == 0 && key == 0) ? QStyle::State_On : QStyle::State_Off) | option.state; - ((QStyleOptionButton*)so)->text = "0x" + QString::number(key, 16).toUpper(); + ((QStyleOptionButton *)so)->state = + (((value.toULongLong() & key) == key && key != 0) || (value.toULongLong() == 0 && key == 0) ? QStyle::State_On + : QStyle::State_Off) | + option.state; + ((QStyleOptionButton *)so)->text = "0x" + QString::number(key, 16).toUpper(); if (option.state.testFlag(QStyle::State_Selected)) so->palette.setColor(QPalette::WindowText, so->palette.color(QPalette::HighlightedText)); style->drawControl(QStyle::CE_CheckBox, so, painter); @@ -226,8 +303,13 @@ void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, co break; } } - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, text, role); + style->drawItemText(painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + text, + role); } } else { #if QT_VERSION_MAJOR <= 5 @@ -241,8 +323,13 @@ void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, co case QMetaType::Int: #endif text.setNum(value.toInt()); - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, text, role); + style->drawItemText(painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + text, + role); break; #if QT_VERSION_MAJOR <= 5 case QVariant::UInt: @@ -250,8 +337,13 @@ void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, co case QMetaType::UInt: #endif text.setNum(value.toUInt()); - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, text, role); + style->drawItemText(painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + text, + role); break; #if QT_VERSION_MAJOR <= 5 case QVariant::LongLong: @@ -259,8 +351,13 @@ void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, co case QMetaType::LongLong: #endif text.setNum(value.toLongLong()); - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, text, role); + style->drawItemText(painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + text, + role); break; #if QT_VERSION_MAJOR <= 5 case QVariant::ULongLong: @@ -268,8 +365,13 @@ void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, co case QMetaType::ULongLong: #endif text.setNum(value.toULongLong()); - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, text, role); + style->drawItemText(painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + text, + role); break; #if QT_VERSION_MAJOR <= 5 case QVariant::Double: @@ -277,20 +379,25 @@ void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, co case QMetaType::Double: #endif text.setNum(value.toDouble(), 'f', 3); - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, text, role); + style->drawItemText(painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + text, + role); break; #if QT_VERSION_MAJOR <= 5 case QVariant::Bool: #else case QMetaType::Bool: #endif - so = new QStyleOptionButton(); - so->rect = option.rect; - so->state = option.state; - so->palette = option.palette; - so->fontMetrics = option.fontMetrics; - ((QStyleOptionButton*)so)->state = (value.toBool() ? QStyle::State_On : QStyle::State_Off) | option.state; + so = new QStyleOptionButton(); + so->rect = option.rect; + so->state = option.state; + so->palette = option.palette; + so->fontMetrics = option.fontMetrics; + ((QStyleOptionButton *)so)->state = (value.toBool() ? QStyle::State_On : QStyle::State_Off) | option.state; style->drawControl(QStyle::CE_CheckBox, so, painter); break; #if QT_VERSION_MAJOR <= 5 @@ -298,7 +405,7 @@ void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, co #else case QMetaType::QColor: #endif - rect = option.rect;//style->subElementRect(QStyle::QStyle::SE_FrameContents, so); + rect = option.rect; // style->subElementRect(QStyle::QStyle::SE_FrameContents, so); rect.setRect(rect.x() + 3, rect.y() + 3, rect.width() - 6, rect.height() - 6); painter->fillRect(rect, ab); painter->fillRect(rect, value.value()); @@ -309,8 +416,13 @@ void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, co case QMetaType::QPoint: #endif text = pointString(value.toPoint()); - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, text, role); + style->drawItemText(painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + text, + role); break; #if QT_VERSION_MAJOR <= 5 case QVariant::PointF: @@ -318,8 +430,13 @@ void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, co case QMetaType::QPointF: #endif text = pointString(value.toPointF()); - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, text, role); + style->drawItemText(painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + text, + role); break; #if QT_VERSION_MAJOR <= 5 case QVariant::Rect: @@ -327,8 +444,13 @@ void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, co case QMetaType::QRect: #endif text = rectString(value.toRect()); - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, text, role); + style->drawItemText(painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + text, + role); break; #if QT_VERSION_MAJOR <= 5 case QVariant::RectF: @@ -336,40 +458,52 @@ void Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, co case QMetaType::QRectF: #endif text = rectString(value.toRectF()); - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, text, role); + style->drawItemText(painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, text), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + text, + role); break; #if QT_VERSION_MAJOR <= 5 - case QVariant::String: default: + case QVariant::String: + default: #else - case QMetaType::QString: default: + case QMetaType::QString: + default: #endif - style->drawItemText(painter, style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, value.toString()), - Qt::AlignLeft | Qt::AlignVCenter, option.palette, true, value.toString(), role); + style->drawItemText( + painter, + style->itemTextRect(option.fontMetrics, option.rect, Qt::AlignLeft | Qt::AlignVCenter, true, value.toString()), + Qt::AlignLeft | Qt::AlignVCenter, + option.palette, + true, + value.toString(), + role); break; } } } - /*so = new QStyleOptionFrame(); - so->rect = option.rect; - so->state = option.state; - so->palette = option.palette; - so->fontMetrics = option.fontMetrics; - ((QStyleOptionFrame*)so)->state = (value.toBool() ? QStyle::State_On : QStyle::State_Off); - style->drawPrimitive(QStyle::PE_PanelLineEdit, so, painter); - style->drawPrimitive(QStyle::PE_FrameLineEdit, so, painter); - break;*/ + /*so = new QStyleOptionFrame(); + so->rect = option.rect; + so->state = option.state; + so->palette = option.palette; + so->fontMetrics = option.fontMetrics; + ((QStyleOptionFrame*)so)->state = (value.toBool() ? QStyle::State_On : QStyle::State_Off); + style->drawPrimitive(QStyle::PE_PanelLineEdit, so, painter); + style->drawPrimitive(QStyle::PE_FrameLineEdit, so, painter); + break;*/ if (so != 0) delete so; if (soc != 0) delete soc; - } void Delegate::changedFlag() { - QAbstractItemModel * model = const_cast(cmi.model()); - model->setData(cmi, qobject_cast(sender())->isChecked(), Qt::UserRole + 3); - QModelIndex p = cmi.parent(), mi; - int row = 0; + QAbstractItemModel * model = const_cast(cmi.model()); + model->setData(cmi, qobject_cast(sender())->isChecked(), Qt::UserRole + 3); + QModelIndex p = cmi.parent(), mi; + int row = 0; qulonglong val = 0; QList chldr; mi = model->index(row, 1, p); @@ -378,26 +512,24 @@ void Delegate::changedFlag() { model->setData(mi, !mi.data(Qt::UserRole + 4).toBool(), Qt::UserRole + 4); mi = model->index(++row, 1, p); } - bool cc = cmi.data(Qt::UserRole + 3).toBool(); + bool cc = cmi.data(Qt::UserRole + 3).toBool(); qulonglong cv = cmi.data(Qt::UserRole).toULongLong(); - //qDebug() << "*****"; + // qDebug() << "*****"; if (cc && cv == 0) { val = 0; - //qDebug() << "null" << cv; + // qDebug() << "null" << cv; } else { if (!cc && cv != 0) { - //qDebug() << "uncheck" << cv; + // qDebug() << "uncheck" << cv; for (int i = 0; i < chldr.size(); ++i) { if (chldr[i] == cmi) continue; - //qDebug() << (chldr[i].data(Qt::UserRole).toULongLong() & cv); - if (chldr[i].data(Qt::UserRole).toULongLong() & cv) - model->setData(chldr[i], false, Qt::UserRole + 3); + // qDebug() << (chldr[i].data(Qt::UserRole).toULongLong() & cv); + if (chldr[i].data(Qt::UserRole).toULongLong() & cv) model->setData(chldr[i], false, Qt::UserRole + 3); } } for (int i = 0; i < chldr.size(); ++i) { - //qDebug() << chldr[i].data(Qt::UserRole + 3).toBool(); - if (chldr[i].data(Qt::UserRole + 3).toBool()) - val |= chldr[i].data(Qt::UserRole).toULongLong(); + // qDebug() << chldr[i].data(Qt::UserRole + 3).toBool(); + if (chldr[i].data(Qt::UserRole + 3).toBool()) val |= chldr[i].data(Qt::UserRole).toULongLong(); } } for (int i = 0; i < chldr.size(); ++i) { @@ -405,25 +537,22 @@ void Delegate::changedFlag() { cv = chldr[i].data(Qt::UserRole).toULongLong(); model->setData(chldr[i], ((val & cv) == cv && cv != 0) || (val == 0 && cv == 0), Qt::UserRole + 3); } - //qDebug() << val; + // qDebug() << val; model->setData(p, val, Qt::UserRole); model->setData(p.sibling(p.row(), 1), val, Qt::UserRole); } - PropertyEditor::PropertyEditor(QWidget * parent): QTreeWidget(parent) { - object = 0; + object = 0; active_ = false; configTree(); - connect(this, SIGNAL(itemClicked(QTreeWidgetItem * , int)), this, SLOT(itemClicked(QTreeWidgetItem * , int))); - connect(this, SIGNAL(itemChanged(QTreeWidgetItem * , int)), this, SLOT(itemChanged(QTreeWidgetItem * , int))); + connect(this, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(itemClicked(QTreeWidgetItem *, int))); + connect(this, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(itemChanged(QTreeWidgetItem *, int))); } -PropertyEditor::~PropertyEditor() { - -} +PropertyEditor::~PropertyEditor() {} void PropertyEditor::changeEvent(QEvent * e) { @@ -446,7 +575,6 @@ void PropertyEditor::configTree() { setHeaderLabels(lbls); setAlternatingRowColors(true); setItemDelegateForColumn(1, new Delegate()); - } @@ -475,25 +603,25 @@ void PropertyEditor::rebuild() { clear(); configTree(); if (object == 0) return; - active_ = false; + active_ = false; const QMetaObject * mo = object->metaObject(); - QList mol; + QList mol; while (mo != 0) { mol.push_front(mo); mo = mo->superClass(); } int ps, pe; - QTreeWidgetItem * ti, * tli, * tfi; + QTreeWidgetItem *ti, *tli, *tfi; QVariant value; -// QWidget * pw = 0; + // QWidget * pw = 0; int chue = 0; QColor bc; font_b = font(); font_b.setBold(true); - foreach (const QMetaObject * o, mol) { - ps = o->propertyOffset(); - pe = o->propertyCount();// - ps; - //qDebug() << i->className() << ps << pe; + foreach(const QMetaObject * o, mol) { + ps = o->propertyOffset(); + pe = o->propertyCount(); // - ps; + // qDebug() << i->className() << ps << pe; tli = new QTreeWidgetItem(); tli->setText(0, o->className()); tli->setFont(0, font_b); @@ -505,7 +633,7 @@ void PropertyEditor::rebuild() { for (int i = ps; i < pe; ++i) { props << o->property(i); value = o->property(i).read(object); - ti = new QTreeWidgetItem(); + ti = new QTreeWidgetItem(); ti->setSizeHint(1, QSize(20, 20)); bc.setHsv(chue, 60, 245 + (i % 2) * 20 - 10); setItemBackColor(ti, bc); @@ -528,14 +656,13 @@ void PropertyEditor::rebuild() { ti->setData(1, Qt::UserRole, value); ti->setData(1, Qt::UserRole + 2, "__flags"); ti->setData(0, Qt::UserRole + 1, QVariant::fromValue(props.back())); - } - else if (props.back().isEnumType()) + } else if (props.back().isEnumType()) value.setValue(PropertyValuePair(props.back(), value)); - //ti->setText(1, value.toString()); + // ti->setText(1, value.toString()); ti->setData(1, Qt::UserRole, value); tli->addChild(ti); - //const_cast(indexFromItem(ti, 1)).; - //if (pw != 0) setItemWidget(ti, 1, pw); + // const_cast(indexFromItem(ti, 1)).; + // if (pw != 0) setItemWidget(ti, 1, pw); } chue += 60; chue %= 360; @@ -544,6 +671,4 @@ void PropertyEditor::rebuild() { } -void PropertyEditor::refresh() { - -} +void PropertyEditor::refresh() {} diff --git a/widgets/propertyeditor.h b/widgets/propertyeditor.h index 6b1f245..226c733 100644 --- a/widgets/propertyeditor.h +++ b/widgets/propertyeditor.h @@ -1,85 +1,104 @@ /* - QGL PropertyEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL PropertyEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef PROPERTYEDITOR_H #define PROPERTYEDITOR_H -#include +#include +#include #include #include -#include -#include #include +#include #include class Delegate: public QStyledItemDelegate { Q_OBJECT + public: - Delegate(QObject * parent = 0): QStyledItemDelegate() {ab = QBrush(QImage(":/icons/alpha.png"));} - - QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const {cmi = const_cast(index); return widgetForProperty(parent, index);} - void setEditorData(QWidget * editor, const QModelIndex & index) const {setWidgetProperty(editor, index.data(Qt::UserRole));} + Delegate(QObject * parent = 0): QStyledItemDelegate() { ab = QBrush(QImage(":/icons/alpha.png")); } + + QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const { + cmi = const_cast(index); + return widgetForProperty(parent, index); + } + void setEditorData(QWidget * editor, const QModelIndex & index) const { setWidgetProperty(editor, index.data(Qt::UserRole)); } void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const; - void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const {editor->setGeometry(option.rect);} + void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const { + editor->setGeometry(option.rect); + } void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const; - + private: QWidget * widgetForProperty(QWidget * parent, const QModelIndex & index) const; void setWidgetProperty(QWidget * w, const QVariant & value) const; const QVariant widgetProperty(QWidget * w) const; - QString pointString(const QPoint & p) const {return QString::number(p.x()) + " x " + QString::number(p.y());} - QString pointString(const QPointF & p) const {return QString::number(p.x()) + " x " + QString::number(p.y());} - QString rectString(const QRect & r) const {return QString::number(r.x()) + " x " + QString::number(r.y()) + " : " + - QString::number(r.width()) + " x " + QString::number(r.height());} - QString rectString(const QRectF & r) const {return QString::number(r.x()) + " x " + QString::number(r.y()) + " : " + - QString::number(r.width()) + " x " + QString::number(r.height());} - + QString pointString(const QPoint & p) const { return QString::number(p.x()) + " x " + QString::number(p.y()); } + QString pointString(const QPointF & p) const { return QString::number(p.x()) + " x " + QString::number(p.y()); } + QString rectString(const QRect & r) const { + return QString::number(r.x()) + " x " + QString::number(r.y()) + " : " + QString::number(r.width()) + " x " + + QString::number(r.height()); + } + QString rectString(const QRectF & r) const { + return QString::number(r.x()) + " x " + QString::number(r.y()) + " : " + QString::number(r.width()) + " x " + + QString::number(r.height()); + } + QBrush ab; mutable QModelIndex cmi; - + private slots: - void changed() {setModelData((QWidget * )sender(), const_cast(cmi.model()), cmi);} + void changed() { setModelData((QWidget *)sender(), const_cast(cmi.model()), cmi); } void changedFlag(); - }; typedef QPair PropertyValuePair; -Q_DECLARE_METATYPE (PropertyValuePair) +Q_DECLARE_METATYPE(PropertyValuePair) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - Q_DECLARE_METATYPE (QMetaProperty) +Q_DECLARE_METATYPE(QMetaProperty) #endif class PropertyEditor: public QTreeWidget { Q_OBJECT + public: explicit PropertyEditor(QWidget * parent = 0); virtual ~PropertyEditor(); - void assignObject(QObject * o) {object = o; rebuild();} + void assignObject(QObject * o) { + object = o; + rebuild(); + } protected: void changeEvent(QEvent * e); private: void configTree(); - void setItemBackColor(QTreeWidgetItem * i, const QColor & c) {i->setBackground(0, c); i->setBackground(1, c);} - void setItemForeColor(QTreeWidgetItem * i, const QColor & c) {i->setForeground(0, c); i->setForeground(1, c);} + void setItemBackColor(QTreeWidgetItem * i, const QColor & c) { + i->setBackground(0, c); + i->setBackground(1, c); + } + void setItemForeColor(QTreeWidgetItem * i, const QColor & c) { + i->setForeground(0, c); + i->setForeground(1, c); + } void rebuild(); void refresh(); @@ -91,7 +110,6 @@ private: private slots: void itemClicked(QTreeWidgetItem * item, int column); void itemChanged(QTreeWidgetItem * item, int column); - }; #endif // PROPERTYEDITOR_H diff --git a/widgets/scene_tree.cpp b/widgets/scene_tree.cpp index 6c319ca..99f59f0 100644 --- a/widgets/scene_tree.cpp +++ b/widgets/scene_tree.cpp @@ -1,29 +1,31 @@ /* - QGL SceneTree - Ivan Pelipenko peri4ko@yandex.ru + QGL SceneTree + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "scene_tree.h" -#include "ui_scene_tree.h" + #include "glcamera.h" #include "qglview.h" -#include -#include +#include "ui_scene_tree.h" + #include #include +#include +#include enum Column { cName, @@ -65,13 +67,17 @@ SceneTree::SceneTree(QWidget * parent): QWidget(parent) { icon_vis[0] = QIcon(":/icons/layer-visible-off.png"); icon_vis[1] = QIcon(":/icons/layer-visible-on.png"); ui->treeObjects->addActions(actionsSelection()); - ui->treeObjects->addAction (newSeparator()); + ui->treeObjects->addAction(newSeparator()); ui->treeObjects->addActions(actionsAdd()); - ui->buttonFilter->addActions(QList() << ui->actionFilter_node << ui->actionFilter_mesh << ui->actionFilter_light << ui->actionFilter_camera); - view = 0; + ui->buttonFilter->addActions(QList() + << ui->actionFilter_node << ui->actionFilter_mesh << ui->actionFilter_light << ui->actionFilter_camera); + view = 0; hidden_by_filter = obj_count = 0; - block_tree = false; - connect(ui->treeObjects->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(treeObjects_selectionCnahged())); + block_tree = false; + connect(ui->treeObjects->selectionModel(), + SIGNAL(selectionChanged(QItemSelection, QItemSelection)), + this, + SLOT(treeObjects_selectionCnahged())); } @@ -87,7 +93,7 @@ void SceneTree::assignQGLView(QGLView * v) { connect(view, SIGNAL(selectionChanged()), this, SLOT(selectionChanged())); connect(view, SIGNAL(materialsChanged()), this, SLOT(materialsChanged())); connect(view->scene(), SIGNAL(treeChanged()), this, SLOT(objectsTreeChanged())); - connect(view->scene(), SIGNAL(__objectDeleted(ObjectBase*)), this, SLOT(__objectDeleted(ObjectBase*))); + connect(view->scene(), SIGNAL(__objectDeleted(ObjectBase *)), this, SLOT(__objectDeleted(ObjectBase *))); view->setContextActions(actionsSelection()); checkActions(); } @@ -102,12 +108,9 @@ QList SceneTree::actionsAdd() { QList SceneTree::actionsSelection() { QList ret; - ret << ui->actionFocus << newSeparator() - << ui->actionGroup << ui->actionClone << newSeparator() - << ui->actionSelect_parent << ui->actionSelect_by_mesh << ui->actionSelect_by_material << newSeparator() - << ui->actionTransfer_transform_to_children << newSeparator() - << ui->actionActive_camera << ui->actionDefault_camera << newSeparator() - << ui->actionRemove; + ret << ui->actionFocus << newSeparator() << ui->actionGroup << ui->actionClone << newSeparator() << ui->actionSelect_parent + << ui->actionSelect_by_mesh << ui->actionSelect_by_material << newSeparator() << ui->actionTransfer_transform_to_children + << newSeparator() << ui->actionActive_camera << ui->actionDefault_camera << newSeparator() << ui->actionRemove; return ret; } @@ -129,8 +132,7 @@ void SceneTree::changeEvent(QEvent * e) { void SceneTree::rememberExpanded(QTreeWidgetItem * ti) { for (int i = 0; i < ti->childCount(); ++i) { QTreeWidgetItem * ci = ti->child(i); - if (ci->isExpanded()) - expanded_ << itemObject(ci); + if (ci->isExpanded()) expanded_ << itemObject(ci); rememberExpanded(ci); } } @@ -148,13 +150,12 @@ void SceneTree::restoreExpanded(QTreeWidgetItem * ti) { void SceneTree::makeObjetTree(ObjectBase * o, QTreeWidgetItem * ti) { ++obj_count; for (int i = 0; i < o->childCount(); ++i) { - ObjectBase * co = o->child(i); + ObjectBase * co = o->child(i); QTreeWidgetItem * ci = new QTreeWidgetItem(ti); ci->setText(cName, co->name()); ci->setCheckState(cVis, co->isVisible() ? Qt::Checked : Qt::Unchecked); ci->setIcon(cVis, icon_vis[co->isVisible(true)]); - if (co->material()) - ci->setText(cMaterial, co->material()->name); + if (co->material()) ci->setText(cMaterial, co->material()->name); ci->setFlags(ci->flags() | Qt::ItemIsEditable); ObjectType t = otNode; switch (co->type()) { @@ -189,7 +190,7 @@ void SceneTree::makeObjetTree(ObjectBase * o, QTreeWidgetItem * ti) { ObjectBase * SceneTree::itemObject(QTreeWidgetItem * item) const { if (!item) return 0; - return (ObjectBase*)(item->data(cName, irObject).toULongLong()); + return (ObjectBase *)(item->data(cName, irObject).toULongLong()); } @@ -201,12 +202,11 @@ int SceneTree::itemType(QTreeWidgetItem * item) const { void SceneTree::selectionChanged() { if (block_tree) return; - block_tree = true; - QList il = ui->treeObjects->findItems("", Qt::MatchContains | Qt::MatchRecursive); - const ObjectBase * fo = 0; - if (view->selectedObjects().size() == 1) - fo = view->selectedObject(); - foreach (QTreeWidgetItem * i, il) { + block_tree = true; + QList il = ui->treeObjects->findItems("", Qt::MatchContains | Qt::MatchRecursive); + const ObjectBase * fo = 0; + if (view->selectedObjects().size() == 1) fo = view->selectedObject(); + foreach(QTreeWidgetItem * i, il) { ObjectBase * o = itemObject(i); i->setSelected(o->isSelected()); if (fo && (fo == o)) { @@ -219,18 +219,17 @@ void SceneTree::selectionChanged() { void SceneTree::materialsChanged() { - foreach (QTreeWidgetItem * i, geo_items) { + foreach(QTreeWidgetItem * i, geo_items) { ObjectBase * o = itemObject(i); if (!o) continue; - if (o->material()) - i->setText(cMaterial, o->material()->name); + if (o->material()) i->setText(cMaterial, o->material()->name); } } void SceneTree::cameraChanged() { if (!view) return; - foreach (QTreeWidgetItem * i, cam_items) { + foreach(QTreeWidgetItem * i, cam_items) { ObjectBase * o = itemObject(i); if (!o) continue; i->setText(cVis, (o == view->camera()) ? "*" : ""); @@ -242,8 +241,8 @@ bool SceneTree::filterTree(QTreeWidgetItem * ti, const QString & filter, int typ bool ret = false; for (int i = 0; i < ti->childCount(); ++i) { QTreeWidgetItem * ci = ti->child(i); - QString cit = ci->text(cName); - int t = itemType(ci); + QString cit = ci->text(cName); + int t = itemType(ci); if (ci->childCount() > 0) { if (!filterTree(ci, filter, types)) { ci->setHidden(true); @@ -259,11 +258,12 @@ bool SceneTree::filterTree(QTreeWidgetItem * ti, const QString & filter, int typ } else { f = f || cit.contains(filter, Qt::CaseInsensitive); } - if ((types & t) != t) - f = false; + if ((types & t) != t) f = false; ci->setHidden(!f); - if (f) ret = true; - else ++hidden_by_filter; + if (f) + ret = true; + else + ++hidden_by_filter; } } return ret; @@ -272,23 +272,23 @@ bool SceneTree::filterTree(QTreeWidgetItem * ti, const QString & filter, int typ void SceneTree::checkActions() { bool has_1 = false, has_m = false; - bool has_cam = false; - bool has_mesh = false; + bool has_cam = false; + bool has_mesh = false; bool is_def_cam = false; if (view) { - is_def_cam = view->isDefaultCamera(); + is_def_cam = view->isDefaultCamera(); ObjectBaseList slo = view->selectedObjects(); - has_1 = !slo.isEmpty(); - has_m = slo.size() > 1; - for (ObjectBase * o : slo) { + has_1 = !slo.isEmpty(); + has_m = slo.size() > 1; + for (ObjectBase * o: slo) { if (o->type() == ObjectBase::glCamera) has_cam = (slo.size() == 1); if (o->type() == ObjectBase::glMesh) has_mesh = true; } } - ui->actionFocus ->setEnabled(has_mesh); + ui->actionFocus->setEnabled(has_mesh); ui->actionRemove->setEnabled(has_1); - ui->actionClone ->setEnabled(has_1); + ui->actionClone->setEnabled(has_1); ui->actionGroup->setEnabled(has_m); ui->actionTransfer_transform_to_children->setEnabled(has_1); ui->actionSelect_parent->setEnabled(has_1); @@ -304,8 +304,8 @@ void SceneTree::treeObjects_selectionCnahged() { block_tree = true; view->scene()->clearSelection(); ObjectBaseList sol; - QList til = ui->treeObjects->selectedItems(); - foreach (QTreeWidgetItem * i, til) + QList til = ui->treeObjects->selectedItems(); + foreach(QTreeWidgetItem * i, til) sol << itemObject(i); view->scene()->selectObjects(sol); block_tree = false; @@ -315,9 +315,9 @@ void SceneTree::treeObjects_selectionCnahged() { void SceneTree::filter() { int types = 0; - if (ui->actionFilter_node ->isChecked()) types |= otNode ; - if (ui->actionFilter_mesh ->isChecked()) types |= otMesh ; - if (ui->actionFilter_light ->isChecked()) types |= otLight ; + if (ui->actionFilter_node->isChecked()) types |= otNode; + if (ui->actionFilter_mesh->isChecked()) types |= otMesh; + if (ui->actionFilter_light->isChecked()) types |= otLight; if (ui->actionFilter_camera->isChecked()) types |= otCamera; if (types == 0) types = 0xFF; hidden_by_filter = 0; @@ -348,14 +348,14 @@ void SceneTree::on_treeObjects_itemChanged(QTreeWidgetItem * item, int column) { if (o) o->setName(item->text(cName)); } if (column == cVis) { - bool vis = item->checkState(cVis) == Qt::Checked; - QList til = ui->treeObjects->selectedItems(); + bool vis = item->checkState(cVis) == Qt::Checked; + QList til = ui->treeObjects->selectedItems(); if (!til.contains(item)) { til.clear(); til << item; } - foreach (QTreeWidgetItem * ti, til) { - //itemObject(ti)->setVisible(vis); + foreach(QTreeWidgetItem * ti, til) { + // itemObject(ti)->setVisible(vis); itemObject(ti)->setVisible(vis); } } @@ -365,13 +365,12 @@ void SceneTree::on_treeObjects_itemChanged(QTreeWidgetItem * item, int column) { void SceneTree::on_treeObjects_itemMoved(QTreeWidgetItem * item, QTreeWidgetItem * new_parent) { ObjectBase * co = itemObject(item); if (!co->hasParent()) return; - //co->parent()->removeChild(co); + // co->parent()->removeChild(co); if (new_parent == ui->treeObjects->invisibleRootItem()) { view->scene()->rootObject()->addChild(co); } else { ObjectBase * po = itemObject(new_parent); - if (po) - po->addChild(co); + if (po) po->addChild(co); } } @@ -402,9 +401,9 @@ void SceneTree::on_actionAdd_camera_triggered() { void SceneTree::on_actionClone_triggered() { if (!view) return; - QList sil = ui->treeObjects->selectedItems(); + QList sil = ui->treeObjects->selectedItems(); ObjectBaseList col; - foreach (QTreeWidgetItem * i, sil) { + foreach(QTreeWidgetItem * i, sil) { ObjectBase * o = itemObject(i); if (!o) continue; ObjectBase * no = o->clone(); @@ -418,10 +417,10 @@ void SceneTree::on_actionClone_triggered() { void SceneTree::on_actionGroup_triggered() { if (!view) return; ObjectBaseList sol = view->scene()->selectedObjects(true); - ObjectBase * cp = sol[0]->parent(); - ObjectBase * nr = new ObjectBase(); + ObjectBase * cp = sol[0]->parent(); + ObjectBase * nr = new ObjectBase(); cp->addChild(nr); - foreach (ObjectBase * o, sol) + foreach(ObjectBase * o, sol) nr->addChild(o); view->scene()->selectObject(nr); } @@ -430,7 +429,7 @@ void SceneTree::on_actionGroup_triggered() { void SceneTree::on_actionTransfer_transform_to_children_triggered() { if (!view) return; ObjectBaseList sol = view->scene()->selectedObjects(true); - foreach (ObjectBase * o, sol) + foreach(ObjectBase * o, sol) o->transferTransformToChildren(); } @@ -455,11 +454,10 @@ void SceneTree::on_actionDefault_camera_triggered() { void SceneTree::on_actionSelect_parent_triggered() { if (!view) return; ObjectBaseList sol = view->scene()->selectedObjects(true); - QSet nsl; - foreach (ObjectBase * o, sol) { + QSet nsl; + foreach(ObjectBase * o, sol) { ObjectBase * po = o->parent(); - if (po != view->scene()->rootObject()) - o = po; + if (po != view->scene()->rootObject()) o = po; nsl << o; } view->scene()->selectObjects(nsl.values()); @@ -478,8 +476,8 @@ void SceneTree::on_actionSelect_by_material_triggered() { void SceneTree::removeObjects() { if (!view) return; - QList sil = ui->treeObjects->selectedItems(); - foreach (QTreeWidgetItem * i, sil) { + QList sil = ui->treeObjects->selectedItems(); + foreach(QTreeWidgetItem * i, sil) { ObjectBase * o = itemObject(i); if (o->isSelected(true)) view->scene()->clearSelection(); if (o) delete o; @@ -493,7 +491,7 @@ void SceneTree::focusObjects() { if (!view->camera()) return; Box3D bb; ObjectBaseList ol = view->selectedObjects(); - foreach (ObjectBase * o, ol) { + foreach(ObjectBase * o, ol) { o->calculateBoundingBox(); bb |= o->boundingBox(); } @@ -512,7 +510,7 @@ void SceneTree::objectsTreeChanged() { block_tree = false; if (!view) return; block_tree = true; - obj_count = 0; + obj_count = 0; makeObjetTree(view->scene()->rootObject(), ui->treeObjects->invisibleRootItem()); --obj_count; restoreExpanded(ui->treeObjects->invisibleRootItem()); diff --git a/widgets/scene_tree.h b/widgets/scene_tree.h index 8be300c..42f1d93 100644 --- a/widgets/scene_tree.h +++ b/widgets/scene_tree.h @@ -1,44 +1,45 @@ /* - QGL SceneTree - Ivan Pelipenko peri4ko@yandex.ru + QGL SceneTree + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef SCENE_TREE_H #define SCENE_TREE_H -#include -#include #include "glscene.h" +#include +#include + class QTreeWidgetItem; namespace Ui { - class SceneTree; +class SceneTree; } -class SceneTree: public QWidget -{ +class SceneTree: public QWidget { Q_OBJECT + public: SceneTree(QWidget * parent = 0); ~SceneTree(); void assignQGLView(QGLView * v); - QList actionsAdd(); - QList actionsSelection(); + QList actionsAdd(); + QList actionsSelection(); void expandItems(); private: @@ -55,21 +56,21 @@ private: bool block_tree; int hidden_by_filter, obj_count; QIcon icon_empty, icon_geo, icon_camera, icon_light, icon_vis[2]; - QSet expanded_; - QList geo_items, cam_items; + QSet expanded_; + QList geo_items, cam_items; QGLView * view; private slots: void treeObjects_selectionCnahged(); void on_treeObjects_itemChanged(QTreeWidgetItem * item, int column); - void on_treeObjects_itemMoved (QTreeWidgetItem * item, QTreeWidgetItem * new_parent); + void on_treeObjects_itemMoved(QTreeWidgetItem * item, QTreeWidgetItem * new_parent); void on_actionAdd_node_triggered(); void on_actionAdd_light_triggered(); void on_actionAdd_camera_triggered(); - void on_actionFocus_triggered() {focusObjects();} - void on_actionRemove_triggered() {removeObjects();} + void on_actionFocus_triggered() { focusObjects(); } + void on_actionRemove_triggered() { removeObjects(); } void on_actionClone_triggered(); void on_actionGroup_triggered(); void on_actionTransfer_transform_to_children_triggered(); @@ -95,7 +96,6 @@ public slots: signals: private: - }; #endif // SCENE_TREE_H diff --git a/widgets/treewidget_p.h b/widgets/treewidget_p.h index 9e0f0eb..ded96b6 100644 --- a/widgets/treewidget_p.h +++ b/widgets/treewidget_p.h @@ -1,59 +1,58 @@ /* - QGL SceneTree - Ivan Pelipenko peri4ko@yandex.ru + QGL SceneTree + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef TREEWIDGET_H #define TREEWIDGET_H -#include -#include #include -#include +#include #include +#include +#include -class InternalMoveTreeWidget: public QTreeWidget -{ +class InternalMoveTreeWidget: public QTreeWidget { Q_OBJECT + public: InternalMoveTreeWidget(QWidget * parent = 0): QTreeWidget(parent) {} - + protected: virtual void dropEvent(QDropEvent * e) { - QList sil = selectedItems(); + QList sil = selectedItems(); if (sil.isEmpty()) return; QTreeWidget::dropEvent(e); - foreach (QTreeWidgetItem * ti, sil) { + foreach(QTreeWidgetItem * ti, sil) { QTreeWidgetItem * ti_p = ti->parent(); if (!ti_p) ti_p = invisibleRootItem(); int ti_ppos = ti_p->indexOfChild(ti); emit itemMoved(ti, ti_p, ti_ppos); } } - + signals: void itemMoved(QTreeWidgetItem * item, QTreeWidgetItem * new_parent, int new_index); - }; class NoEditDelegate: public QStyledItemDelegate { public: - NoEditDelegate(QObject* parent=0): QStyledItemDelegate(parent) {} - virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { + NoEditDelegate(QObject * parent = 0): QStyledItemDelegate(parent) {} + virtual QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const { return nullptr; } }; diff --git a/widgets/view_editor.cpp b/widgets/view_editor.cpp index 9928072..714196c 100644 --- a/widgets/view_editor.cpp +++ b/widgets/view_editor.cpp @@ -1,23 +1,25 @@ /* - QGL ViewEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL ViewEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #include "view_editor.h" + #include "ui_view_editor.h" + #include #include #include @@ -26,10 +28,10 @@ ViewEditor::ViewEditor(QWidget * parent): QWidget(parent) { ui = new Ui::ViewEditor(); ui->setupUi(this); - view = nullptr; + view = nullptr; active = true; ui->checkCameraLight->setCheckState(Qt::PartiallyChecked); -#if QT_VERSION >= QT_VERSION_CHECK(5,12,0) +#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) ui->spinDepthStart->setStepType(QAbstractSpinBox::AdaptiveDecimalStepType); #endif } @@ -64,11 +66,8 @@ void ViewEditor::assignQGLView(QGLView * v) { void ViewEditor::changeEvent(QEvent * e) { QWidget::changeEvent(e); switch (e->type()) { - case QEvent::LanguageChange: - ui->retranslateUi(this); - break; - default: - break; + case QEvent::LanguageChange: ui->retranslateUi(this); break; + default: break; } } @@ -161,7 +160,6 @@ void ViewEditor::on_checkService_clicked(bool val) { void ViewEditor::on_checkCameraLight_stateChanged(int s) { if (!view || !active) return; view->setCameraLightMode((QGLView::CameraLightMode)s); - } @@ -205,4 +203,3 @@ void ViewEditor::on_checkVSync_clicked(bool val) { if (!view || !active) return; view->setVSync(val); } - diff --git a/widgets/view_editor.h b/widgets/view_editor.h index 03cc744..6d8c1f9 100644 --- a/widgets/view_editor.h +++ b/widgets/view_editor.h @@ -1,34 +1,35 @@ /* - QGL ViewEditor - Ivan Pelipenko peri4ko@yandex.ru + QGL ViewEditor + Ivan Pelipenko peri4ko@yandex.ru - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ #ifndef VIEW_EDITOR_H #define VIEW_EDITOR_H -#include #include "qglview.h" +#include + namespace Ui { - class ViewEditor; +class ViewEditor; } -class ViewEditor: public QWidget -{ +class ViewEditor: public QWidget { Q_OBJECT + public: explicit ViewEditor(QWidget * parent = 0); @@ -60,7 +61,7 @@ private slots: void on_checkVSync_clicked(bool val); void on_buttonHDRClear_clicked(); void on_buttonHDRSelect_clicked(); - void on_colorFogBack_colorChanged(const QColor &color); + void on_colorFogBack_colorChanged(const QColor & color); void on_spinFogDensity_valueChanged(double arg1); void on_spinFogDecay_valueChanged(double arg1); };