diff --git a/Apps/Examples/clipping.cpp b/Apps/Examples/clipping.cpp index 9bcbc173f4747e827b1708301b66a8cd8e4f4f4f..c96925b7a312b75b97f26d59ddf9dc6059d61832 100644 --- a/Apps/Examples/clipping.cpp +++ b/Apps/Examples/clipping.cpp @@ -78,12 +78,31 @@ void Clipping::updatePickables() transfoMat[2][j] = vec1[j]; transfoMat[3][j] = pos[j]; } + transfoMat[0][3] = 0.0; + transfoMat[1][3] = 0.0; + transfoMat[2][3] = 0.0; + transfoMat[3][3] = 1.0; } for (size_t i = 0; i < m_pickableSpheres.size(); i++) { unsigned int id = m_pickableSpheres[i]->id(); - m_pickableSpheres[i]->translate(m_shader->getClipSphereParamsCenter(id)); - m_pickableSpheres[i]->scale(m_shader->getClipSphereParamsRadius(id)); + Geom::Vec3f pos = m_shader->getClipSphereParamsCenter(id); + float radius = m_shader->getClipSphereParamsRadius(id); + Geom::Vec3f vec1 (radius, 0.0, 0.0); + Geom::Vec3f vec2 (0.0, radius, 0.0); + Geom::Vec3f vec3 (0.0, 0.0, radius); + glm::mat4& transfoMat = m_pickableSpheres[i]->transfo(); + for (int j = 0; j < 3; j++) + { + transfoMat[0][j] = vec2[j]; + transfoMat[1][j] = vec3[j]; + transfoMat[2][j] = vec1[j]; + transfoMat[3][j] = pos[j]; + } + transfoMat[0][3] = 0.0; + transfoMat[1][3] = 0.0; + transfoMat[2][3] = 0.0; + transfoMat[3][3] = 1.0; } } @@ -317,20 +336,20 @@ void Clipping::slot_pushButton_applyStaticClippingPreset() { using namespace CGoGN::Utils::QT; - double centerX = (double)m_bb.center()[0]; - double centerY = (double)m_bb.center()[1]; - double centerZ = (double)m_bb.center()[2]; - double size = (double)m_bb.maxSize()*0.75; + float centerX = m_bb.center()[0]; + float centerY = m_bb.center()[1]; + float centerZ = m_bb.center()[2]; + float size = m_bb.maxSize()*0.75f; int axis = 0; bool facing = false; - if (inputValues(VarDbl(centerX - 100.0, centerX + 100.0, centerX, "Center X", - VarDbl(centerY - 100.0, centerY + 100.0, centerY, "Center Y", - VarDbl(centerZ - 100.0, centerZ + 100.0, centerZ, "Center Z", - VarDbl(size - 100.0, size + 100.0, size, "Size", + if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X", + VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y", + VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z", + VarFloat(size - 100.0f, size + 100.0f, size, "Size", VarSlider(0, 2, axis, "Axis", VarBool(facing, "Facing" )))))), "Preset Setup")) - preset = Utils::ClippingPreset::CreateDualPlanesPreset(Geom::Vec3f((float)centerX, (float)centerY, (float)centerZ), (float)size, axis, facing); + preset = Utils::ClippingPreset::CreateDualPlanesPreset(Geom::Vec3f(centerX, centerY, centerZ), size, axis, facing); } break; @@ -338,18 +357,18 @@ void Clipping::slot_pushButton_applyStaticClippingPreset() { using namespace CGoGN::Utils::QT; - double centerX = (double)m_bb.center()[0]; - double centerY = (double)m_bb.center()[1]; - double centerZ = (double)m_bb.center()[2]; - double size = (double)m_bb.maxSize()*0.75; + float centerX = m_bb.center()[0]; + float centerY = m_bb.center()[1]; + float centerZ = m_bb.center()[2]; + float size = m_bb.maxSize()*0.75f; bool facing = false; - if (inputValues(VarDbl(centerX - 100.0, centerX + 100.0, centerX, "Center X", - VarDbl(centerY - 100.0, centerY + 100.0, centerY, "Center Y", - VarDbl(centerZ - 100.0, centerZ + 100.0, centerZ, "Center Z", - VarDbl(size - 100.0, size + 100.0, size, "Size", + if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X", + VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y", + VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z", + VarFloat(size - 100.0f, size + 100.0f, size, "Size", VarBool(facing, "Facing" ))))), "Preset Setup")) - preset = Utils::ClippingPreset::CreateCubePreset(Geom::Vec3f((float)centerX, (float)centerY, (float)centerZ), (float)size, facing); + preset = Utils::ClippingPreset::CreateCubePreset(Geom::Vec3f(centerX, centerY, centerZ), size, facing); } break; @@ -357,22 +376,22 @@ void Clipping::slot_pushButton_applyStaticClippingPreset() { using namespace CGoGN::Utils::QT; - double centerX = (double)m_bb.center()[0]; - double centerY = (double)m_bb.center()[1]; - double centerZ = (double)m_bb.center()[2]; - double size = (double)m_bb.maxSize()*0.75; + float centerX = m_bb.center()[0]; + float centerY = m_bb.center()[1]; + float centerZ = m_bb.center()[2]; + float size = m_bb.maxSize()*0.75f; int axis = 0; int precision = 1; bool facing = false; - if (inputValues(VarDbl(centerX - 100.0, centerX + 100.0, centerX, "Center X", - VarDbl(centerY - 100.0, centerY + 100.0, centerY, "Center Y", - VarDbl(centerZ - 100.0, centerZ + 100.0, centerZ, "Center Z", - VarDbl(size - 100.0, size + 100.0, size, "Size", + if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X", + VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y", + VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z", + VarFloat(size - 100.0f, size + 100.0f, size, "Size", VarSlider(0, 2, axis, "Axis", VarSlider(1, 30, precision, "Precision", VarBool(facing, "Facing" ))))))), "Preset Setup")) - preset = Utils::ClippingPreset::CreateTubePreset(Geom::Vec3f((float)centerX, (float)centerY, (float)centerZ), (float)size, axis, precision, facing); + preset = Utils::ClippingPreset::CreateTubePreset(Geom::Vec3f(centerX, centerY, centerZ), size, axis, precision, facing); } break; @@ -380,20 +399,20 @@ void Clipping::slot_pushButton_applyStaticClippingPreset() { using namespace CGoGN::Utils::QT; - double centerX = (double)m_bb.center()[0]; - double centerY = (double)m_bb.center()[1]; - double centerZ = (double)m_bb.center()[2]; - double size = (double)m_bb.maxSize()*0.75; - double atomsRadiuses = (double)m_bb.maxSize()*0.25; + float centerX = m_bb.center()[0]; + float centerY = m_bb.center()[1]; + float centerZ = m_bb.center()[2]; + float size = m_bb.maxSize()*0.75f; + float atomsRadiuses = m_bb.maxSize()*0.25f; bool orClipping = true; - if (inputValues(VarDbl(centerX - 100.0, centerX + 100.0, centerX, "Center X", - VarDbl(centerY - 100.0, centerY + 100.0, centerY, "Center Y", - VarDbl(centerZ - 100.0, centerZ + 100.0, centerZ, "Center Z", - VarDbl(size - 100.0, size + 100.0, size, "Size", - VarDbl(atomsRadiuses - 100.0, atomsRadiuses + 100.0, atomsRadiuses, "Atoms radiuses", + if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X", + VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y", + VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z", + VarFloat(size - 100.0f, size + 100.0f, size, "Size", + VarFloat(atomsRadiuses - 100.0f, atomsRadiuses + 100.0f, atomsRadiuses, "Atoms radiuses", VarBool(orClipping, "OR clipping mode" )))))), "Preset Setup")) - preset = Utils::ClippingPreset::CreateMoleculePreset(Geom::Vec3f((float)centerX, (float)centerY, (float)centerZ), (float)size, atomsRadiuses, orClipping); + preset = Utils::ClippingPreset::CreateMoleculePreset(Geom::Vec3f(centerX, centerY, centerZ), size, atomsRadiuses, orClipping); } break; } @@ -458,30 +477,30 @@ void Clipping::slot_pushButton_applyAnimatedClippingPreset() Utils::ClippingPresetAnimatedDualPlanes *preset = NULL; using namespace CGoGN::Utils::QT; - double centerStartX = (double)m_bb.center()[0] - (double)m_bb.size(0)*0.25; - double centerStartY = (double)m_bb.center()[1]; - double centerStartZ = (double)m_bb.center()[2]; - double centerEndX = (double)m_bb.center()[0] + (double)m_bb.size(0)*0.25; - double centerEndY = (double)m_bb.center()[1]; - double centerEndZ = (double)m_bb.center()[2]; - double size = (double)m_bb.maxSize()*0.1; + float centerStartX = m_bb.center()[0] - m_bb.size(0)*0.25f; + float centerStartY = m_bb.center()[1]; + float centerStartZ = m_bb.center()[2]; + float centerEndX = m_bb.center()[0] + m_bb.size(0)*0.25f; + float centerEndY = m_bb.center()[1]; + float centerEndZ = m_bb.center()[2]; + float size = m_bb.maxSize()*0.1f; int axis = 0; bool facing = false; bool zigzag = false; - if (inputValues(VarDbl(centerStartX - 100.0, centerStartX + 100.0, centerStartX, "Center Start X", - VarDbl(centerStartY - 100.0, centerStartY + 100.0, centerStartY, "Center Start Y", - VarDbl(centerStartZ - 100.0, centerStartZ + 100.0, centerStartZ, "Center Start Z", - VarDbl(centerEndX - 100.0, centerEndX + 100.0, centerEndX, "Center End X", - VarDbl(centerEndY - 100.0, centerEndY + 100.0, centerEndY, "Center End Y", - VarDbl(centerEndZ - 100.0, centerEndZ + 100.0, centerEndZ, "Center End Z", - VarDbl(size - 100.0, size + 100.0, size, "Size", + if (inputValues(VarFloat(centerStartX - 100.0f, centerStartX + 100.0f, centerStartX, "Center Start X", + VarFloat(centerStartY - 100.0f, centerStartY + 100.0f, centerStartY, "Center Start Y", + VarFloat(centerStartZ - 100.0f, centerStartZ + 100.0f, centerStartZ, "Center Start Z", + VarFloat(centerEndX - 100.0f, centerEndX + 100.0f, centerEndX, "Center End X", + VarFloat(centerEndY - 100.0f, centerEndY + 100.0f, centerEndY, "Center End Y", + VarFloat(centerEndZ - 100.0f, centerEndZ + 100.0f, centerEndZ, "Center End Z", + VarFloat(size - 100.0f, size + 100.0f, size, "Size", VarSlider(0, 2, axis, "Axis", VarBool(facing, "Facing", VarBool(zigzag, "Zigzag" )))))))))), "Preset Setup")) preset = new Utils::ClippingPresetAnimatedDualPlanes( - Geom::Vec3f((float)centerStartX, (float)centerStartY, (float)centerStartZ), Geom::Vec3f((float)centerEndX, (float)centerEndY, (float)centerEndZ), - (float)size, axis, facing, zigzag); + Geom::Vec3f(centerStartX, centerStartY, centerStartZ), Geom::Vec3f(centerEndX, centerEndY, centerEndZ), + size, axis, facing, zigzag); animatedPreset = preset; } @@ -492,22 +511,73 @@ void Clipping::slot_pushButton_applyAnimatedClippingPreset() Utils::ClippingPresetAnimatedRotatingPlane *preset = NULL; using namespace CGoGN::Utils::QT; - double centerX = (double)m_bb.center()[0]; - double centerY = (double)m_bb.center()[1]; - double centerZ = (double)m_bb.center()[2]; + float centerX = m_bb.center()[0]; + float centerY = m_bb.center()[1]; + float centerZ = m_bb.center()[2]; int axis = 0; - if (inputValues(VarDbl(centerX - 100.0, centerX + 100.0, centerX, "Center X", - VarDbl(centerY - 100.0, centerY + 100.0, centerY, "Center Y", - VarDbl(centerZ - 100.0, centerZ + 100.0, centerZ, "Center Z", + if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X", + VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y", + VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z", VarSlider(0, 2, axis, "Axis" )))), "Preset Setup")) preset = new Utils::ClippingPresetAnimatedRotatingPlane( - Geom::Vec3f((float)centerX, (float)centerY, (float)centerZ), axis); + Geom::Vec3f(centerX, centerY, centerZ), axis); animatedPreset = preset; } break; + case 2 : // Scaled Sphere + { + Utils::ClippingPresetAnimatedScaledSphere *preset = NULL; + using namespace CGoGN::Utils::QT; + + float centerX = m_bb.center()[0]; + float centerY = m_bb.center()[1]; + float centerZ = m_bb.center()[2]; + float radiusStart = m_bb.maxSize()*0.1f; + float radiusEnd = m_bb.maxSize()*1.0f; + bool zigzag = false; + if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X", + VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y", + VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z", + VarFloat(-radiusEnd*4.0f, radiusEnd*4.0f, radiusStart, "Radius Start", + VarFloat(-radiusEnd*4.0f, radiusEnd*4.0f, radiusEnd, "Radius End", + VarBool(zigzag, "Zigzag" + )))))), "Preset Setup")) + preset = new Utils::ClippingPresetAnimatedScaledSphere( + Geom::Vec3f(centerX, centerY, centerZ), radiusStart, radiusEnd, zigzag); + + animatedPreset = preset; + } + break; + + case 3 : // Spheres Cube Collision + { + Utils::ClippingPresetAnimatedSpheresCubeCollision *preset = NULL; + using namespace CGoGN::Utils::QT; + + float centerX = m_bb.center()[0]; + float centerY = m_bb.center()[1]; + float centerZ = m_bb.center()[2]; + float size = m_bb.maxSize()*1.0f; + int spheresCount = 5; + float radius = m_bb.maxSize()*0.2f; + if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X", + VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y", + VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z", + VarFloat(-size*4.0f, size*4.0f, size, "Cube Size", + VarSlider(1, 20, spheresCount, "Spheres Count", + VarFloat(-radius*4.0f, radius*4.0f, radius, "Spheres Radius" + )))))), "Preset Setup")) + { + preset = new Utils::ClippingPresetAnimatedSpheresCubeCollision( + Geom::Vec3f(centerX, centerY, centerZ), size, spheresCount, radius); + } + + animatedPreset = preset; + } + break; } if (animatedPreset == NULL) @@ -553,6 +623,8 @@ void Clipping::slot_pushButton_applyAnimatedClippingPreset() // Set on animated mode m_lastAnimatedClippingPreset = animatedPreset; m_timer->start(1000.0f/60.0f); + m_lastAnimatedClippingPreset->setAnimationSpeedFactor((float)dock.doubleSpinBox_AnimatedClippingPresetSpeed->value()); + slot_setAnimationState(true); updateGLMatrices(); @@ -565,6 +637,7 @@ void Clipping::slot_pushButton_StopAnimation() delete m_lastAnimatedClippingPreset; m_lastAnimatedClippingPreset = NULL; } + slot_setAnimationState(false); } void Clipping::slot_doubleSpinBox_AnimatedClippingPresetSpeed(double c) @@ -573,6 +646,14 @@ void Clipping::slot_doubleSpinBox_AnimatedClippingPresetSpeed(double c) m_lastAnimatedClippingPreset->setAnimationSpeedFactor((float)c); } +void Clipping::slot_setAnimationState(bool state) +{ + dock.pushButton_addPlane->setEnabled(!state); + dock.pushButton_addSphere->setEnabled(!state); + dock.PushButton_ApplyStaticClippingPreset->setEnabled(!state); + dock.PushButton_ApplyAnimatedClippingPreset->setEnabled(!state); +} + void Clipping::slot_animationTimer() { if (m_lastAnimatedClippingPreset == NULL) @@ -692,6 +773,8 @@ void Clipping::initGUI() dock.comboBox_AnimatedClippingPresets->addItem("Moving Dual Planes"); dock.comboBox_AnimatedClippingPresets->addItem("Rotating Plane"); + dock.comboBox_AnimatedClippingPresets->addItem("Scaled Sphere"); + dock.comboBox_AnimatedClippingPresets->addItem("Spheres Cube Collision"); setCallBack(dock.pushButton_StopAnimation, SIGNAL(clicked()), SLOT(slot_pushButton_StopAnimation())); setCallBack(dock.doubleSpinBox_AnimatedClippingPresetSpeed, SIGNAL(valueChanged(double)), SLOT(slot_doubleSpinBox_AnimatedClippingPresetSpeed(double))); diff --git a/Apps/Examples/clipping.h b/Apps/Examples/clipping.h index 00b63c0d9a3f296d017c8df0bae0160644cf7c65..a6f850f7cc5a17cdbc83241177cd178cec90c81b 100644 --- a/Apps/Examples/clipping.h +++ b/Apps/Examples/clipping.h @@ -168,6 +168,8 @@ public slots: void slot_pushButton_StopAnimation(); void slot_doubleSpinBox_AnimatedClippingPresetSpeed(double c); + void slot_setAnimationState(bool state); + void slot_animationTimer(); void button_compile(); diff --git a/include/Utils/clippingPresetsAnimated.h b/include/Utils/clippingPresetsAnimated.h index 0a6e7fafe34d8e4f2821cabd8eed4816da3e88dc..ddadc6233bf992ac6a0c6f0e78639b98bc1a66e3 100644 --- a/include/Utils/clippingPresetsAnimated.h +++ b/include/Utils/clippingPresetsAnimated.h @@ -29,6 +29,9 @@ #include "Utils/clippingShader.h" #include "Geometry/vector_gen.h" #include +#include +#include +#include namespace CGoGN { @@ -180,6 +183,73 @@ private : }; +class ClippingPresetAnimatedScaledSphere : public ClippingPresetAnimated +{ + +public : + + /** + * constructor + * @param center center of sphere + * @param radiusStart radius of the sphere at the beginning of the animation + * @param radiusEnd radius of the sphere at the end of the animation + * @param zigzag true to enable zigzag mode + */ + ClippingPresetAnimatedScaledSphere(Geom::Vec3f center, float radiusStart, float radiusEnd, bool zigzag); + + /** + * runs the animation some steps further + * @param amount amount of steps the animation should do + */ + void step(unsigned int amount); + +private : + + /// start radius of the animation + float m_startRadius; + + /// end radius of the animation + float m_endRadius; + + /// zigzag mode ? + bool m_zigzag; + +}; + + +class ClippingPresetAnimatedSpheresCubeCollision : public ClippingPresetAnimated +{ + +public : + + /** + * constructor + * @param center center of cube + * @param size size of cube + * @param spheresCount spheres count + * @param radius radius of spheres + */ + ClippingPresetAnimatedSpheresCubeCollision(Geom::Vec3f center, float size, int spheresCount, float radius); + + /** + * runs the animation some steps further + * @param amount amount of steps the animation should do + */ + void step(unsigned int amount); + +private : + + /// center of cube + Geom::Vec3f m_cubeCenter; + + /// size of cube + float m_cubeSize; + + /// direction vector of spheres + std::vector m_spheresDirections; + +}; + } // namespace Utils diff --git a/src/Utils/clippingPresets.cpp b/src/Utils/clippingPresets.cpp index 177c825af0502a44908425569841719c4041d7d2..3f11eb7a35125d4e3ea92eb2a3b444a9736da12a 100644 --- a/src/Utils/clippingPresets.cpp +++ b/src/Utils/clippingPresets.cpp @@ -50,14 +50,15 @@ ClippingPreset* ClippingPreset::CreateDualPlanesPreset(Geom::Vec3f center, float ClippingPreset *preset = new ClippingPreset; // Correct axis if necessary - if ((axis < 0) || (axis > 2)) - axis = 0; + int usedAxis = axis; + if ((usedAxis < 0) || (usedAxis > 2)) + usedAxis = 0; // Axis on which planes will be aligned Geom::Vec3f positDir (0.0f, 0.0f, 0.0f); - positDir[axis] = 1.0f; + positDir[usedAxis] = 1.0f; Geom::Vec3f negDir (0.0f, 0.0f, 0.0f); - negDir[axis] = -1.0f; + negDir[usedAxis] = -1.0f; // Facing of planes float side = 1.0f; @@ -116,8 +117,9 @@ ClippingPreset* ClippingPreset::CreateTubePreset(Geom::Vec3f center, float size, ClippingPreset *preset = new ClippingPreset; // Correct axis if necessary - if ((axis < 0) || (axis > 2)) - axis = 0; + int usedAxis = axis; + if ((usedAxis < 0) || (usedAxis > 2)) + usedAxis = 0; // Correct precision if necessary if (precision < 1) @@ -133,8 +135,8 @@ ClippingPreset* ClippingPreset::CreateTubePreset(Geom::Vec3f center, float size, float dAngle = 2.0f * M_PI / precision; for (int i = 0; i < precision; i++) { - direction[(axis + 1) % 3] = cos(i*dAngle); - direction[(axis + 2) % 3] = sin(i*dAngle); + direction[(usedAxis + 1) % 3] = cos(i*dAngle); + direction[(usedAxis + 2) % 3] = sin(i*dAngle); preset->addClipPlane(direction, center + direction*(size / 2.0f)*(side)); } diff --git a/src/Utils/clippingPresetsAnimated.cpp b/src/Utils/clippingPresetsAnimated.cpp index 589252bb04f421ee34f54673bd93371262246e6c..eb0044b9b46433139eee679e6a4a758ce6c66f89 100644 --- a/src/Utils/clippingPresetsAnimated.cpp +++ b/src/Utils/clippingPresetsAnimated.cpp @@ -67,22 +67,21 @@ void ClippingPresetAnimated::apply(ClippingShader* clipShader, std::vector 2)) - axis = 0; + int usedAxis = axis; + if ((usedAxis < 0) || (usedAxis > 2)) + usedAxis = 0; // Axis on which planes will be aligned Geom::Vec3f positDir (0.0f, 0.0f, 0.0f); - positDir[axis] = 1.0f; + positDir[usedAxis] = 1.0f; Geom::Vec3f negDir (0.0f, 0.0f, 0.0f); - negDir[axis] = -1.0f; + negDir[usedAxis] = -1.0f; // Facing of planes float side = 1.0f; @@ -113,7 +112,7 @@ void ClippingPresetAnimatedDualPlanes::step(unsigned int amount) CGoGNerr << "ERROR -" << "ClippingPresetAnimatedDualPlanes::step" - << " - Some planes or spheres ids are not valid anymore - Animation stopped" + << " - Some planes or spheres ids are not valid anymore - Animation paused" << CGoGNendl; m_animationSpeedFactor = 0.0f; return; @@ -161,14 +160,14 @@ void ClippingPresetAnimatedDualPlanes::step(unsigned int amount) ClippingPresetAnimatedRotatingPlane::ClippingPresetAnimatedRotatingPlane(Geom::Vec3f center, int axis) { - // Correct axis if necessary and store it - if ((axis < 0) || (axis > 2)) - axis = 0; + // Store animation settings m_axis = axis; + if ((m_axis < 0) || (m_axis)) + m_axis = 0; // Axis on which planes will be aligned Geom::Vec3f normal (0.0f, 0.0f, 0.0f); - normal[(axis + 1) % 3] = 1.0f; + normal[(m_axis + 1) % 3] = 1.0f; // Add plane to preset addClipPlane(normal, center); @@ -188,8 +187,8 @@ void ClippingPresetAnimatedRotatingPlane::step(unsigned int amount) { CGoGNerr << "ERROR -" - << "ClippingPresetAnimatedDualPlanes::step" - << " - Some planes or spheres ids are not valid anymore - Animation stopped" + << "ClippingPresetAnimatedRotatingPlane::step" + << " - Some planes or spheres ids are not valid anymore - Animation paused" << CGoGNendl; m_animationSpeedFactor = 0.0f; return; @@ -208,8 +207,153 @@ void ClippingPresetAnimatedRotatingPlane::step(unsigned int amount) planeNormal[(m_axis + 1) % 3] = cos(angle); planeNormal[(m_axis + 2) % 3] = sin(angle); m_attachedClippingShader->setClipPlaneParamsNormal(m_planesIds[0], planeNormal); +} + +ClippingPresetAnimatedScaledSphere::ClippingPresetAnimatedScaledSphere(Geom::Vec3f center, float radiusStart, float radiusEnd, bool zigzag) +{ + // Store animation settings + m_startRadius = radiusStart; + m_endRadius = radiusEnd; + std::cout << "Given Start Radius : " << radiusStart << std::endl; + std::cout << "Actual Start Radius : " << m_startRadius << std::endl; + std::cout << "Given End Radius : " << radiusEnd << std::endl; + std::cout << "Actual End Radius : " << m_endRadius << std::endl; + m_zigzag = zigzag; + + // Add sphere to preset + addClipSphere(center, m_startRadius); + + // Set clipping mode + setClippingMode(ClippingShader::CLIPPING_MODE_AND); +} +void ClippingPresetAnimatedScaledSphere::step(unsigned int amount) +{ + // Check if the animation has been stopped + if (m_animationSpeedFactor == 0.0f) + return; + + // Check the validity of planes or spheres ids + if (!m_attachedClippingShader->isClipSphereIdValid(m_spheresIds[0])) + { + CGoGNerr + << "ERROR -" + << "ClippingPresetAnimatedScaledSphere::step" + << " - Some planes or spheres ids are not valid anymore - Animation paused" + << CGoGNendl; + m_animationSpeedFactor = 0.0f; + return; + } + // Update animation parameter value + m_animParam += (float)amount * m_animationOneStepIncrement * m_animationSpeedFactor; + if (!m_zigzag) + { + while (m_animParam < 0.0f) + m_animParam += 1.0f; + while (m_animParam > 1.0f) + m_animParam -= 1.0f; + } + else + { + while ( (m_animParam < 0.0f) || (m_animParam > 1.0f) ) + { + if (m_animParam < 0.0f) + { + m_animParam = -m_animParam; + m_animationOneStepIncrement = -m_animationOneStepIncrement; + } + else if (m_animParam > 1.0f) + { + m_animParam = 1.0f - (m_animParam - 1.0f); + m_animationOneStepIncrement = -m_animationOneStepIncrement; + } + } + } + + // Calculate new radius + float radius = (1.0f - m_animParam)*m_startRadius + m_animParam*m_endRadius; + m_attachedClippingShader->setClipSphereParamsRadius(m_spheresIds[0], radius); +} + +ClippingPresetAnimatedSpheresCubeCollision::ClippingPresetAnimatedSpheresCubeCollision(Geom::Vec3f center, float size, int spheresCount, float radius) +{ + // Store animation settings + m_cubeCenter = center; + m_cubeSize = size; + int usedSpheresCount = spheresCount; + if (usedSpheresCount < 1) + usedSpheresCount = 1; + + // Add spheres to preset + for (int i = 0; i < usedSpheresCount; i++) + addClipSphere(m_cubeCenter, radius); + + // Store spheres random directions + m_spheresDirections.resize(usedSpheresCount); + srand(time(NULL)); + for (size_t i = 0; i < m_spheresDirections.size(); i++) + { + Geom::Vec3f dir ((rand() % 1000) - 500, (rand() % 1000) - 500, (rand() % 1000) - 500); + dir.normalize(); + m_spheresDirections[i] = dir; + } + + // Set clipping mode + setClippingMode(ClippingShader::CLIPPING_MODE_AND); +} + +void ClippingPresetAnimatedSpheresCubeCollision::step(unsigned int amount) +{ + // Check if the animation has been stopped + if (m_animationSpeedFactor == 0.0f) + return; + + // Check the validity of planes or spheres ids + for (size_t i = 0; i < m_spheresIds.size(); i++) + { + if (!m_attachedClippingShader->isClipSphereIdValid(m_spheresIds[i])) + { + CGoGNerr + << "ERROR -" + << "ClippingPresetAnimatedSpheresCubeCollision::step" + << " - Some planes or spheres ids are not valid anymore - Animation paused" + << CGoGNendl; + m_animationSpeedFactor = 0.0f; + return; + } + } + + // Update animation parameter value + float dParam = (float)amount * m_animationOneStepIncrement * m_animationSpeedFactor; + m_animParam += dParam; + while (m_animParam < 0.0f) + m_animParam += 1.0f; + while (m_animParam > 1.0f) + m_animParam -= 1.0f; + + // Calculate new center and detect collisions with cube faces + for (size_t i = 0; i < m_spheresIds.size(); i++) + { + Geom::Vec3f oldCenter = m_attachedClippingShader->getClipSphereParamsCenter(m_spheresIds[i]); + Geom::Vec3f newCenter = oldCenter + dParam*m_spheresDirections[i]; + m_attachedClippingShader->setClipSphereParamsCenter(m_spheresIds[i], newCenter); + Geom::Vec3f posToCube = newCenter - m_cubeCenter; + for (int j = 0; j < 3; j++) + { + if ( (posToCube[j] < -m_cubeSize*0.5f) || (posToCube[j] > m_cubeSize*0.5f) ) + { + Geom::Vec3f cubeNormal (0.0f, 0.0f, 0.0f); + if (posToCube[j] < 0.0f) + cubeNormal[j] = 1.0f; + else + cubeNormal[j] = -1.0f; + // Reflect + if ( (m_spheresDirections[i] * cubeNormal) < 0.0f) + m_spheresDirections[i] = (2.0f * ((cubeNormal*m_spheresDirections[i])*cubeNormal) - m_spheresDirections[i])*-1.0f; + } + } + } } } // namespace Utils diff --git a/src/Utils/clippingShader.cpp b/src/Utils/clippingShader.cpp index d52d8d1c768e4f2983ee2e1d5eb3ef666b40d9c8..f8e3f91147ef133b311f820088085034a4b9ad02 100644 --- a/src/Utils/clippingShader.cpp +++ b/src/Utils/clippingShader.cpp @@ -198,7 +198,7 @@ int ClippingShader::getClipPlanesCount() bool ClippingShader::isClipPlaneIdValid(unsigned int id) { - if (id > m_clipPlanesIds.size()) + if (id >= m_clipPlanesIds.size()) return false; if (!m_clipPlanesIds[id].used) return false; @@ -496,7 +496,7 @@ int ClippingShader::getClipSpheresCount() bool ClippingShader::isClipSphereIdValid(unsigned int id) { - if (id > m_clipSpheresIds.size()) + if (id >= m_clipSpheresIds.size()) return false; if (!m_clipSpheresIds[id].used) return false;