git-svn-id: svn://db.shs.com.ru/libs@540 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -53,11 +53,10 @@ QGLView::QGLView(): OpenGLWindow(), fbo_selection(3) {
|
||||
fogDensity_ = fogEnd_ = 1.;
|
||||
fogStart_ = 0.;
|
||||
fogMode_ = Exp;
|
||||
hoverHaloFill_ = 0.333;
|
||||
selectionHaloFill_ = 0.5;
|
||||
hoverHaloFill_ = 0.333f;
|
||||
selectionHaloFill_ = 0.5f;
|
||||
//lmode = Simple;
|
||||
shader_select = shader_halo = nullptr;
|
||||
m_texture_manager = new GLTextureManager();
|
||||
setFeature(qglMSAA, false);
|
||||
setFeature(qglFXAA, false);
|
||||
setFeature(qglLinearFiltering, true);
|
||||
@@ -91,11 +90,11 @@ QGLView::QGLView(): OpenGLWindow(), fbo_selection(3) {
|
||||
sel_mode = QGLView::SingleSelection;
|
||||
// sel_pen = QPen(Qt::black, 1, Qt::DashLine);
|
||||
// sel_brush = QBrush(QColor(170, 100, 255, 120));
|
||||
camera().setAim(QVector3D(0,0,5.5));
|
||||
camera().setPos(QVector3D(10, 5, 5.5));
|
||||
camera().setName("Camera");
|
||||
camera()->setAim(QVector3D(0,0,5.5));
|
||||
camera()->setPos(QVector3D(10, 5, 5.5));
|
||||
camera()->setName("Camera");
|
||||
addObject(camera());
|
||||
emit cameraPosChanged(camera().pos());
|
||||
emit cameraPosChanged(camera()->pos());
|
||||
//camera().aim_ = camera().pos_;
|
||||
ktm_.restart();
|
||||
}
|
||||
@@ -105,7 +104,6 @@ QGLView::~QGLView() {
|
||||
stop();
|
||||
if (shader_select) delete shader_select;
|
||||
if (shader_halo) delete shader_halo;
|
||||
delete m_texture_manager;
|
||||
}
|
||||
|
||||
|
||||
@@ -114,8 +112,8 @@ void QGLView::stop() {
|
||||
}
|
||||
|
||||
|
||||
void QGLView::start(double freq) {
|
||||
timer = startTimer(freq <= 0. ? 0 : 1000. / freq);
|
||||
void QGLView::start(float freq) {
|
||||
timer = startTimer(freq <= 0.f ? 0 : int(1000.f / freq));
|
||||
}
|
||||
|
||||
|
||||
@@ -144,6 +142,14 @@ void QGLView::addObject(GLObjectBase * o) {
|
||||
}
|
||||
|
||||
|
||||
int QGLView::objectsCount(bool all) {
|
||||
if (!all) return objects_.childCount();
|
||||
int cnt = 0;
|
||||
objectsCountInternal(&cnt, &objects_);
|
||||
return cnt;
|
||||
}
|
||||
|
||||
|
||||
void QGLView::removeObject(GLObjectBase * o, bool inChildren) {
|
||||
o->setView(nullptr);
|
||||
if (inChildren)
|
||||
@@ -154,6 +160,11 @@ void QGLView::removeObject(GLObjectBase * o, bool inChildren) {
|
||||
}
|
||||
|
||||
|
||||
void QGLView::removeObject(GLObjectBase & o, bool inChildren) {
|
||||
removeObject(&o, inChildren);
|
||||
}
|
||||
|
||||
|
||||
void QGLView::clearObjects(bool deleteAll) {
|
||||
removeObject(camera_);
|
||||
objects_.clearChildren(deleteAll);
|
||||
@@ -163,6 +174,58 @@ void QGLView::clearObjects(bool deleteAll) {
|
||||
}
|
||||
|
||||
|
||||
QList<GLObjectBase *> QGLView::objects(bool all) {
|
||||
return objects_.children(all);
|
||||
}
|
||||
|
||||
|
||||
int QGLView::lightsCount() const {
|
||||
return lights_.size();
|
||||
}
|
||||
|
||||
|
||||
void QGLView::removeLight(int index) {
|
||||
removeObject(lights_.at(index));
|
||||
lights_.removeAt(index);
|
||||
}
|
||||
|
||||
|
||||
void QGLView::removeLight(Light * l) {
|
||||
foreach (Light * i, lights_)
|
||||
if (i == l) removeObject(i);
|
||||
lights_.removeAll(l);
|
||||
}
|
||||
|
||||
|
||||
void QGLView::clearLights(bool deleteAll) {
|
||||
if (deleteAll)
|
||||
foreach (Light * i, lights_) delete i;
|
||||
lights_.clear();
|
||||
}
|
||||
|
||||
|
||||
void QGLView::addTexture(const QString & path) {
|
||||
textures_manager->addTexture(path);
|
||||
}
|
||||
|
||||
|
||||
void QGLView::addAnimation(const QString & dir, const QString & name) {
|
||||
textures_manager->addAnimation(dir, name);
|
||||
}
|
||||
|
||||
|
||||
Light * QGLView::light(int index) {
|
||||
return lights_[index];
|
||||
}
|
||||
|
||||
|
||||
Light * QGLView::light(const QString & name) {
|
||||
foreach (Light * i, lights_)
|
||||
if (i->name_ == name) return i;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
void QGLView::selectObject(GLObjectBase * o) {
|
||||
if (o == sel_obj) return;
|
||||
GLObjectBase * pso = sel_obj;
|
||||
@@ -172,7 +235,8 @@ void QGLView::selectObject(GLObjectBase * o) {
|
||||
|
||||
|
||||
void QGLView::resizeEvent(QResizeEvent * e) {
|
||||
resizeGL(width(), height());
|
||||
// if (isExposed())
|
||||
// resizeGL(width(), height());
|
||||
/* QWindow::resizeEvent(e);*/
|
||||
}
|
||||
|
||||
@@ -186,15 +250,16 @@ void QGLView::timerEvent(QTimerEvent *) {
|
||||
|
||||
|
||||
void QGLView::render() {
|
||||
resizeGL(width(), height());
|
||||
QRect g_rect(QPoint(), size());
|
||||
emit glBeforePaint();
|
||||
//qDebug() << "paintGL";
|
||||
//QMutexLocker ml_v(&v_mutex);
|
||||
glEnable(GL_CULL_FACE);
|
||||
//glDisable(GL_CULL_FACE);
|
||||
camera().apply(aspect);
|
||||
camera()->apply(aspect);
|
||||
//objects_.preparePos(camera());
|
||||
start_rp.cam_offset_matrix = camera().offsetMatrix();
|
||||
start_rp.cam_offset_matrix = camera()->offsetMatrix();
|
||||
start_rp.proj_matrix = getGLMatrix(GL_PROJECTION_MATRIX);
|
||||
start_rp.view_matrix = getGLMatrix(GL_MODELVIEW_MATRIX);
|
||||
//objects_.buildTransform();
|
||||
@@ -232,7 +297,7 @@ void QGLView::render() {
|
||||
}
|
||||
} else {
|
||||
glReadPixels(lastPos.x(), height() - lastPos.y(), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, cgid);
|
||||
iid = (cgid[0] << 24) | (cgid[1] << 16) | (cgid[2] << 8) | cgid[3];
|
||||
iid = uint(cgid[0] << 24) | uint(cgid[1] << 16) | uint(cgid[2] << 8) | cgid[3];
|
||||
so = ids.value(iid, nullptr);
|
||||
//qDebug() <<cgid[0]<<cgid[1]<<cgid[2]<<cgid[3]<< iid;
|
||||
if (so != hov_obj) {
|
||||
@@ -256,8 +321,8 @@ void QGLView::render() {
|
||||
glEnableClientState(GL_COLOR_ARRAY);*/
|
||||
}
|
||||
|
||||
camera().apply(aspect);
|
||||
start_rp.cam_offset_matrix = camera().offsetMatrix();
|
||||
camera()->apply(aspect);
|
||||
start_rp.cam_offset_matrix = camera()->offsetMatrix();
|
||||
cur_mvpm = start_rp.proj_matrix * start_rp.view_matrix * start_rp.cam_offset_matrix;
|
||||
//objects_.preparePos(camera());
|
||||
|
||||
@@ -280,9 +345,9 @@ void QGLView::render() {
|
||||
emit glPainting();
|
||||
if (selectionHalo_ || hoverHalo_) {
|
||||
glReleaseTextures();
|
||||
glReleaseShaders();
|
||||
glReleaseFramebuffer();
|
||||
glActiveTextureChannel(0);
|
||||
glUseProgram(0);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnable(GL_BLEND);
|
||||
glDisable(GL_TEXTURE_CUBE_MAP);
|
||||
@@ -301,9 +366,9 @@ void QGLView::render() {
|
||||
}
|
||||
}
|
||||
|
||||
glReleaseShaders();
|
||||
glUseProgram(0);
|
||||
glResetAllTransforms();
|
||||
glReleaseFramebuffer();
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
|
||||
emit glEndPaint();
|
||||
|
||||
@@ -350,13 +415,13 @@ void QGLView::render() {
|
||||
|
||||
|
||||
void QGLView::initialize() {
|
||||
initializeOpenGLFunctions();
|
||||
//initializeOpenGLFunctions();
|
||||
glEnable(GL_TEXTURE_MAX_ANISOTROPY_EXT);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glEnableDepth();
|
||||
glEnable(GL_CULL_FACE);
|
||||
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
|
||||
glActiveTextureChannel(3);
|
||||
glActiveTexture(GL_TEXTURE0 + 3);
|
||||
glEnable(GL_TEXTURE_GEN_S);
|
||||
glEnable(GL_TEXTURE_GEN_T);
|
||||
glEnable(GL_TEXTURE_GEN_R);
|
||||
@@ -366,13 +431,13 @@ void QGLView::initialize() {
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
glActiveTextureChannel(0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glShadeModel(GL_SMOOTH);
|
||||
glCullFace(GL_BACK);
|
||||
glEnable(GL_COLOR_MATERIAL);
|
||||
glColorMaterial(GL_FRONT, GL_DIFFUSE);
|
||||
|
||||
textures_manager.loadTextures();
|
||||
textures_manager->loadTextures();
|
||||
objects_.initInternal();
|
||||
checkCaps();
|
||||
|
||||
@@ -380,23 +445,23 @@ void QGLView::initialize() {
|
||||
shader_halo = new QOpenGLShaderProgram(context());
|
||||
reloadThisShaders();
|
||||
is_init = true;
|
||||
resizeGL(width(), height());
|
||||
//resizeGL(width(), height());
|
||||
need_init_ = false;
|
||||
emit glInitializeDone();
|
||||
}
|
||||
|
||||
|
||||
void QGLView::renderHalo(const GLObjectBase * obj, const uint iid, const QColor & color, const double & fill) {
|
||||
void QGLView::renderHalo(const GLObjectBase * obj, const uint iid, const QColor & color, const float & fill) {
|
||||
if (!shaders_supported) return;
|
||||
if (!shader_halo) return;
|
||||
if (!shader_halo->isLinked()) return;
|
||||
if (obj) {
|
||||
shader_halo->bind();
|
||||
shader_halo->setUniformValue("qgl_ModelViewProjectionMatrix", QMatrix4x4());
|
||||
glActiveTextureChannel(0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, fbo_selection.colorTexture());
|
||||
shader_halo->setUniformValue("t0", 0);
|
||||
shader_halo->setUniformValue("dt", QVector2D(1. / width(), 1. / height()));
|
||||
shader_halo->setUniformValue("dt", QVector2D(1.f / width(), 1.f / height()));
|
||||
shader_halo->setUniformValue("selected", QVector4D(float((iid >> 24) & 0xFF) / 255.f,
|
||||
float((iid >> 16) & 0xFF) / 255.f,
|
||||
float((iid >> 8) & 0xFF) / 255.f,
|
||||
@@ -493,6 +558,13 @@ void QGLView::collectObjectLights(GLObjectBase * o) {
|
||||
}
|
||||
|
||||
|
||||
void QGLView::objectsCountInternal(int * cnt, GLObjectBase * where) {
|
||||
++(*cnt);
|
||||
foreach (GLObjectBase * i, where->children_)
|
||||
objectsCountInternal(cnt, i);
|
||||
}
|
||||
|
||||
|
||||
void QGLView::removeObjectInternal(GLObjectBase * o, GLObjectBase * where) {
|
||||
foreach (GLObjectBase * i, where->children_) {
|
||||
if (o == i)
|
||||
@@ -520,6 +592,14 @@ void QGLView::reloadThisShaders() {
|
||||
//loadShaders(shader_rope, "rope", "shaders");
|
||||
}
|
||||
|
||||
void QGLView::glReleaseTextures(int channels) {
|
||||
for (int i = channels - 1; i >= 0; --i) {
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void QGLView::applyFog() {
|
||||
GLfloat fog_col[4] = {float(fogColor_.redF()), float(fogColor_.greenF()), float(fogColor_.blueF()), .0f};
|
||||
@@ -540,12 +620,12 @@ void QGLView::applyFog() {
|
||||
void QGLView::resizeGL(int width, int height) {
|
||||
if (!is_init) return;
|
||||
if (width <= 0 || height <= 0) return;
|
||||
aspect = double(width) / double(height);
|
||||
aspect = float(width) / float(height);
|
||||
if (renderer_) renderer_->resize(width, height);
|
||||
//qDebug() << "resize" << width << height;
|
||||
fbo_selection.resize(width, height);
|
||||
mouse_first = true;
|
||||
iaspect = (aspect == 0.) ? 0. : 1 / aspect;
|
||||
iaspect = (aspect == 0.f) ? 0. : 1 / aspect;
|
||||
glViewport(0, 0, width, height);
|
||||
emit glResize(width, height);
|
||||
}
|
||||
@@ -592,26 +672,26 @@ void QGLView::mouseMoveEvent(QMouseEvent * e) {
|
||||
///qDebug() << e->x() << e->y();
|
||||
QRect g_rect(QPoint(), size());
|
||||
if (mouseRotate_) {
|
||||
double dx = e->x() - lastPos.x();
|
||||
double dy = e->y() - lastPos.y();
|
||||
float dx = e->x() - lastPos.x();
|
||||
float dy = e->y() - lastPos.y();
|
||||
if (e->buttons() & Qt::LeftButton) {
|
||||
//camera().angle_z += dx / 4.;
|
||||
//camera().angle_xy += dy / 4.;
|
||||
if (cameraOrbit_) {
|
||||
camera().orbitZ(dx / 4.);
|
||||
camera().orbitXY(dy / 4.);
|
||||
camera()->orbitZ(dx / 4.f);
|
||||
camera()->orbitXY(dy / 4.f);
|
||||
} else {
|
||||
camera().rotateZ(dx / 4.);
|
||||
camera().rotateXY(dy / 4.);
|
||||
camera()->rotateZ(dx / 4.f);
|
||||
camera()->rotateXY(dy / 4.f);
|
||||
}
|
||||
emit cameraPosChanged(camera().pos());
|
||||
emit cameraPosChanged(camera()->pos());
|
||||
} else if (e->buttons() & Qt::RightButton) {
|
||||
double ad = camera().distance();
|
||||
camera().moveLeft(dx / 1000. * ad);
|
||||
camera().moveUp(dy / 1000. * ad);
|
||||
float ad = camera()->distance();
|
||||
camera()->moveLeft(dx / 1000.f * ad);
|
||||
camera()->moveUp(dy / 1000.f * ad);
|
||||
//camera().pos.setX(camera().pos.x() + camera().pos.z() * dx / 500.);
|
||||
//camera().pos.setY(camera().pos.y() - camera().pos.z() * dy / 500.);
|
||||
emit cameraPosChanged(camera().pos());
|
||||
emit cameraPosChanged(camera()->pos());
|
||||
}
|
||||
//lights[0]->pos_ = camera().pos();
|
||||
}
|
||||
@@ -643,9 +723,9 @@ void QGLView::mouseMoveEvent(QMouseEvent * e) {
|
||||
|
||||
void QGLView::wheelEvent(QWheelEvent * e) {
|
||||
if (mouseRotate_) {
|
||||
if (e->delta() > 0) camera().flyCloser(0.1); //camera().pos.setZ(camera().pos.z() - 0.1 * camera().pos.z());
|
||||
if (e->delta() < 0) camera().flyFarer(0.1); //camera().pos.setZ(camera().pos.z() + 0.1 * camera().pos.z());
|
||||
emit cameraPosChanged(camera().pos());
|
||||
if (e->delta() > 0) camera()->flyCloser(0.1f); //camera().pos.setZ(camera().pos.z() - 0.1 * camera().pos.z());
|
||||
if (e->delta() < 0) camera()->flyFarer(0.1f); //camera().pos.setZ(camera().pos.z() + 0.1 * camera().pos.z());
|
||||
emit cameraPosChanged(camera()->pos());
|
||||
}
|
||||
emit glWheelEvent(e);
|
||||
}
|
||||
@@ -660,6 +740,9 @@ void QGLView::leaveEvent(QEvent * ) {
|
||||
void QGLView::keyPressEvent(QKeyEvent * e) {
|
||||
emit glKeyPressEvent(e);
|
||||
if (e->key() > 0) keys_.insert(e->key());
|
||||
if (e->key() == Qt::Key_F11) {
|
||||
emit doubleClick();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -669,7 +752,12 @@ void QGLView::keyReleaseEvent(QKeyEvent * e) {
|
||||
}
|
||||
|
||||
|
||||
void QGLView::focusOutEvent(QFocusEvent *)
|
||||
{keys_.clear();
|
||||
void QGLView::focusOutEvent(QFocusEvent *) {
|
||||
keys_.clear();
|
||||
}
|
||||
|
||||
|
||||
void QGLView::mouseDoubleClickEvent(QMouseEvent * e) {
|
||||
if (e->buttons().testFlag(Qt::MidButton))
|
||||
emit doubleClick();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user