diff --git a/Apps/Tuto/stage_shader.cpp b/Apps/Tuto/stage_shader.cpp index 2308d08b36abddae05397226201aabc2d021250f..f700af9b81d881908a36313eaea7fc33d3053f74 100644 --- a/Apps/Tuto/stage_shader.cpp +++ b/Apps/Tuto/stage_shader.cpp @@ -197,6 +197,96 @@ void StageShader::slot_doubleSpinBox_GridColor(double c) updateGL(); } +void StageShader::slot_pushButton_addSphere() +{ + m_shader->setClipSpheresCount(dock.comboBox_SphereIndex->count() + 1); + + m_shader->setClipSphereParamsCenter(m_bb.center(), dock.comboBox_SphereIndex->count() + 1 - 1); + m_shader->setClipSphereParamsRadius((m_bb.maxSize())*1.0, dock.comboBox_SphereIndex->count() + 1 - 1); + + std::string indexStr; + std::stringstream ss; + ss << (dock.comboBox_PlaneIndex->count() + 1); + indexStr = ss.str(); + + std::string str = "Sphere " + indexStr; + + dock.comboBox_SphereIndex->addItem(QString(str.c_str())); + + dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc())); + dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc())); + + updateGLMatrices(); +} + +void StageShader::slot_pushButton_deleteSphere() +{ + m_shader->setClipSpheresCount(dock.comboBox_SphereIndex->count() - 1); + + dock.comboBox_SphereIndex->removeItem(dock.comboBox_SphereIndex->count() - 1); + + dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc())); + dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc())); + + updateGLMatrices(); +} + +void StageShader::slot_comboBox_SphereIndexChanged(int newIndex) +{ + if (newIndex >= 0) + { + Geom::Vec3f currSphereCenter = m_shader->getClipSphereParamsCenter(newIndex); + dock.doubleSpinBox_SphereCenterx->setValue(currSphereCenter[0]); + dock.doubleSpinBox_SphereCentery->setValue(currSphereCenter[1]); + dock.doubleSpinBox_SphereCenterz->setValue(currSphereCenter[2]); + + dock.doubleSpinBox_SphereRadius->setValue(m_shader->getClipSphereParamsRadius(newIndex)); + } +} + +void StageShader::slot_doubleSpinBox_SphereCenter(double c) +{ + if (dock.comboBox_SphereIndex->currentIndex() >= 0) + { + float x = dynamic_cast(dockWidget())->doubleSpinBox_SphereCenterx->value(); + float y = dynamic_cast(dockWidget())->doubleSpinBox_SphereCentery->value(); + float z = dynamic_cast(dockWidget())->doubleSpinBox_SphereCenterz->value(); + m_shader->setClipSphereParamsCenter(Geom::Vec3f(x, y, z), dock.comboBox_SphereIndex->currentIndex()); + updateGL(); + } +} + +void StageShader::slot_doubleSpinBox_SphereRadius(double c) +{ + if (dock.comboBox_SphereIndex->currentIndex() >= 0) + { + m_shader->setClipSphereParamsRadius((float)c, dock.comboBox_SphereIndex->currentIndex()); + updateGL(); + } +} + +void StageShader::slot_spinBox_SphereGridResolutionX(int i) +{ + m_shader->setClipSpheresDisplayXRes((size_t)i); + updateGL(); +} + +void StageShader::slot_spinBox_SphereGridResolutionY(int i) +{ + m_shader->setClipSpheresDisplayYRes((size_t)i); + updateGL(); +} + +void StageShader::slot_doubleSpinBox_SphereGridColor(double c) +{ + float r = dynamic_cast(dockWidget())->doubleSpinBox_SphereGridColorR->value(); + float g = dynamic_cast(dockWidget())->doubleSpinBox_SphereGridColorG->value(); + float b = dynamic_cast(dockWidget())->doubleSpinBox_SphereGridColorB->value(); + + m_shader->setClipSpheresDisplayColor(Geom::Vec3f(r, g, b)); + updateGL(); +} + void StageShader::button_compile() { QString st1 = dynamic_cast(dockWidget())->vertexEdit->toPlainText(); @@ -278,10 +368,37 @@ void StageShader::initGUI() dock.doubleSpinBox_GridDisplaySize->setValue(m_shader->getClipPlanesDisplaySize()); dock.spinBox_GridResolutionX->setValue(m_shader->getClipPlanesDisplayXRes()); dock.spinBox_GridResolutionY->setValue(m_shader->getClipPlanesDisplayYRes()); - Geom::Vec3f col = m_shader->getClipPlanesDisplayColor(); - dock.doubleSpinBox_GridColorR->setValue(col[0]); - dock.doubleSpinBox_GridColorG->setValue(col[1]); - dock.doubleSpinBox_GridColorB->setValue(col[2]); + Geom::Vec3f planesCol = m_shader->getClipPlanesDisplayColor(); + dock.doubleSpinBox_GridColorR->setValue(planesCol[0]); + dock.doubleSpinBox_GridColorG->setValue(planesCol[1]); + dock.doubleSpinBox_GridColorB->setValue(planesCol[2]); + + + + setCallBack(dock.pushButton_addSphere, SIGNAL(clicked()), SLOT(slot_pushButton_addSphere())); + setCallBack(dock.pushButton_deleteSphere, SIGNAL(clicked()), SLOT(slot_pushButton_deleteSphere())); + + setCallBack(dock.comboBox_SphereIndex, SIGNAL(currentIndexChanged(int)), SLOT(slot_comboBox_SphereIndexChanged(int))); + + setCallBack(dock.doubleSpinBox_SphereCenterx, SIGNAL(valueChanged(double)), SLOT(slot_doubleSpinBox_SphereCenter(double))); + setCallBack(dock.doubleSpinBox_SphereCentery, SIGNAL(valueChanged(double)), SLOT(slot_doubleSpinBox_SphereCenter(double))); + setCallBack(dock.doubleSpinBox_SphereCenterz, SIGNAL(valueChanged(double)), SLOT(slot_doubleSpinBox_SphereCenter(double))); + + setCallBack(dock.doubleSpinBox_SphereRadius, SIGNAL(valueChanged(double)), SLOT(slot_doubleSpinBox_SphereRadius(double))); + + setCallBack(dock.spinBox_SphereGridResolutionX, SIGNAL(valueChanged(int)), SLOT(slot_spinBox_SphereGridResolutionX(int))); + setCallBack(dock.spinBox_SphereGridResolutionY, SIGNAL(valueChanged(int)), SLOT(slot_spinBox_SphereGridResolutionY(int))); + setCallBack(dock.doubleSpinBox_SphereGridColorR, SIGNAL(valueChanged(double)), SLOT(slot_doubleSpinBox_SphereGridColor(double))); + setCallBack(dock.doubleSpinBox_SphereGridColorG, SIGNAL(valueChanged(double)), SLOT(slot_doubleSpinBox_SphereGridColor(double))); + setCallBack(dock.doubleSpinBox_SphereGridColorB, SIGNAL(valueChanged(double)), SLOT(slot_doubleSpinBox_SphereGridColor(double))); + + dock.spinBox_SphereGridResolutionX->setValue(m_shader->getClipSpheresDisplayXRes()); + dock.spinBox_SphereGridResolutionY->setValue(m_shader->getClipSpheresDisplayYRes()); + Geom::Vec3f spheresCol = m_shader->getClipSpheresDisplayColor(); + dock.doubleSpinBox_SphereGridColorR->setValue(spheresCol[0]); + dock.doubleSpinBox_SphereGridColorG->setValue(spheresCol[1]); + dock.doubleSpinBox_SphereGridColorB->setValue(spheresCol[2]); + } void StageShader::cb_Open() @@ -367,11 +484,15 @@ void StageShader::cb_initGL() registerShader(m_shader); + m_shader->insertClippingCode(); + m_shader->setClipPlanesDisplayColor(Geom::Vec3f (1.0, 0.0, 0.0)); m_shader->setClipPlanesDisplayXRes(10); m_shader->setClipPlanesDisplayYRes(5); - m_shader->insertClippingCode(); + m_shader->setClipSpheresDisplayColor(Geom::Vec3f(0.0, 0.4, 1.0)); + m_shader->setClipSpheresDisplayXRes(20); + m_shader->setClipSpheresDisplayYRes(15); } void StageShader::updateVBOprimitives(int upType) @@ -422,6 +543,7 @@ void StageShader::cb_redraw() m_render_topo->drawTopo(); m_shader->displayClipPlanes(); + m_shader->displayClipSpheres(); } diff --git a/Apps/Tuto/stage_shader.h b/Apps/Tuto/stage_shader.h index 1b4c1edfff62762926a5f609738af216a7e2ac19..a01e9f5c39deee6669771f962fce6c79e4fcc11d 100644 --- a/Apps/Tuto/stage_shader.h +++ b/Apps/Tuto/stage_shader.h @@ -139,6 +139,18 @@ public slots: void slot_spinBox_GridResolutionY(int i); void slot_doubleSpinBox_GridColor(double c); + void slot_pushButton_addSphere(); + void slot_pushButton_deleteSphere(); + + void slot_comboBox_SphereIndexChanged(int newIndex); + + void slot_doubleSpinBox_SphereCenter(double c); + void slot_doubleSpinBox_SphereRadius(double c); + + void slot_spinBox_SphereGridResolutionX(int i); + void slot_spinBox_SphereGridResolutionY(int i); + void slot_doubleSpinBox_SphereGridColor(double c); + void button_compile(); }; diff --git a/Apps/Tuto/stage_shader.ui b/Apps/Tuto/stage_shader.ui index 4d98b137310fca8f4134ab939a66d0daa874e132..813aa15d845ced50fc68206d9725f013a392bdb0 100644 --- a/Apps/Tuto/stage_shader.ui +++ b/Apps/Tuto/stage_shader.ui @@ -6,7 +6,7 @@ 0 0 - 417 + 424 534 @@ -34,7 +34,7 @@ - 1 + 2 @@ -203,13 +203,13 @@ - Clipping + Plane Clipping - + true @@ -523,6 +523,225 @@ + + + Sphere Clipping + + + + + + + + true + + + Display Parameters + + + + + + + + 1.000000000000000 + + + 0.100000000000000 + + + + + + + Res. + + + + + + + 200 + + + + + + + Color + + + + + + + 1.000000000000000 + + + 0.100000000000000 + + + + + + + 1.000000000000000 + + + 0.100000000000000 + + + + + + + 200 + + + + + + + + + + + + Spheres Selection + + + + + 170 + 30 + 31 + 28 + + + + - + + + + + + 140 + 30 + 31 + 28 + + + + + + + + + + true + + + + 10 + 30 + 99 + 28 + + + + Qt::NoFocus + + + false + + + -1 + + + + + + + + Sphere Params + + + + + + + + -99.989999999999995 + + + 0.100000000000000 + + + 0.000000000000000 + + + + + + + -99.989999999999995 + + + 0.100000000000000 + + + 0.000000000000000 + + + + + + + -99.989999999999995 + + + 0.100000000000000 + + + 0.000000000000000 + + + + + + + -99.989999999999995 + + + 0.100000000000000 + + + 0.000000000000000 + + + + + + + Center + + + + + + + Radius + + + + + + + + + + + + diff --git a/src/Utils/clippingShader.cpp b/src/Utils/clippingShader.cpp index 98055e578fb61c5d404972b6724c01a74d03cb17..8c256b1d82986d01e35db9a1b2026d91a9a68ad9 100644 --- a/src/Utils/clippingShader.cpp +++ b/src/Utils/clippingShader.cpp @@ -713,6 +713,7 @@ void ClippingShader::updateClipSphereVBO(int sphereIndex) m_clipSpheresDrawers[sphereIndex]->begin(GL_LINES); + // Compute angle displacement steps float dTheta = 0.0; if (m_clipSpheresDisplayXRes != 0) dTheta = 2 * M_PI / (float)m_clipSpheresDisplayXRes; @@ -720,28 +721,38 @@ void ClippingShader::updateClipSphereVBO(int sphereIndex) if (m_clipSpheresDisplayYRes != 0) dPhi = M_PI / (float)m_clipSpheresDisplayYRes; + // Draw the sphere in wireframe for (size_t i = 0; i < m_clipSpheresDisplayXRes; i++) { for (size_t j = 0; j < m_clipSpheresDisplayYRes; j++) { + // Compute 3 points according to the parametric equation of the sphere Geom::Vec3f p1 ( - m_clipSpheres[sphereIndex].radius * cos(i*dTheta) * sin(j*dPhi), - m_clipSpheres[sphereIndex].radius * sin(i*dTheta) * sin(j*dPhi), - m_clipSpheres[sphereIndex].radius * cos(j*dPhi)); - + cos(i*dTheta) * sin(j*dPhi), + sin(i*dTheta) * sin(j*dPhi), + cos(j*dPhi)); Geom::Vec3f p2 ( - m_clipSpheres[sphereIndex].radius * cos((i+1)*dTheta) * sin(j*dPhi), - m_clipSpheres[sphereIndex].radius * sin((i+1)*dTheta) * sin(j*dPhi), - m_clipSpheres[sphereIndex].radius * cos(j*dPhi)); - + cos((i+1)*dTheta) * sin(j*dPhi), + sin((i+1)*dTheta) * sin(j*dPhi), + cos(j*dPhi)); Geom::Vec3f p3 ( - m_clipSpheres[sphereIndex].radius * cos(i*dTheta) * sin((j+1)*dPhi), - m_clipSpheres[sphereIndex].radius * sin(i*dTheta) * sin((j+1)*dPhi), - m_clipSpheres[sphereIndex].radius * cos((j+1)*dPhi)); + cos(i*dTheta) * sin((j+1)*dPhi), + sin(i*dTheta) * sin((j+1)*dPhi), + cos((j+1)*dPhi)); + + // Scale with the radius + p1 *= m_clipSpheres[sphereIndex].radius; + p2 *= m_clipSpheres[sphereIndex].radius; + p3 *= m_clipSpheres[sphereIndex].radius; + // Translate to the center + p1 += m_clipSpheres[sphereIndex].center; + p2 += m_clipSpheres[sphereIndex].center; + p3 += m_clipSpheres[sphereIndex].center; + + // Draw two lines with the 3 points m_clipSpheresDrawers[sphereIndex]->vertex(p1); m_clipSpheresDrawers[sphereIndex]->vertex(p2); - m_clipSpheresDrawers[sphereIndex]->vertex(p1); m_clipSpheresDrawers[sphereIndex]->vertex(p3); } @@ -900,9 +911,9 @@ bool ClippingShader::insertClippingCode() " {\n" " // Keep the distance to the nearest sphere\n" " if (minDistanceToClipping < 0.0)\n" - " minDistanceToClipping = distanceToSphere;\n" + " minDistanceToClipping = -distanceToSphere;\n" " else\n" - " minDistanceToClipping = min(minDistanceToClipping, distanceToSphere);\n" + " minDistanceToClipping = min(minDistanceToClipping, -distanceToSphere);\n" " }\n" " }\n" "\n"