44 #include "TypeLight.hh" 46 #define DEF0 "Default Light 0.lgt" 47 #define DEF1 "Default Light 1.lgt" 48 #define DEF2 "Default Light 2.lgt" 50 TypeLightPlugin::TypeLightPlugin() :
59 viewerToolbarAction_(0),
69 hitLightTrackball_(false),
74 light_matrix_.identity();
77 bool TypeLightPlugin::registerType() {
90 void TypeLightPlugin::removeDefaultLights() {
97 emit deleteObject(light0);
99 emit deleteObject(light1);
101 emit deleteObject(light2);
103 defaultLights_ =
false;
106 void TypeLightPlugin::addDefaultLights() {
115 light0 = addDefaultLight(DEF0);
129 lightSrc0->brightness(1.0);
141 light1 = addDefaultLight(DEF1);
154 lightSrc1->brightness(1.0);
165 light2 = addDefaultLight(DEF2);
178 lightSrc2->brightness(1.0);
187 defaultLights_ =
true;
190 void TypeLightPlugin::showReducedUi(
bool reduced) {
191 viewerToolbarAction_->setVisible(!reduced);
196 if ( OpenFlipper::Options::gui() ){
198 emit addPickMode(
"Separator");
199 emit addHiddenPickMode(
"MoveLights");
200 emit setPickModeMouseTracking (
"MoveLights",
true);
201 emit addHiddenPickMode(
"TranslateLights");
202 emit setPickModeMouseTracking (
"TranslateLights",
true);
203 emit addHiddenPickMode(
"RotateLights");
204 emit setPickModeMouseTracking (
"RotateLights",
true);
207 toolbar_ =
new QToolBar(tr(
"Transform light sources"));
209 lightAction_ =
new QAction(tr(
"<B>Light mode</B><br>Transform light sources in 3D."), toolbar_);
210 lightAction_->setStatusTip(tr(
"Transform light sources in 3D."));
211 lightAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"light-mode.png") );
212 lightAction_->setCheckable(
true);
213 toolbar_->addAction(lightAction_);
215 connect(lightAction_, SIGNAL(triggered(
bool)),
this, SLOT(slotLightModeRequest(
bool)) );
218 QToolBar* viewerToolbar = 0;
220 emit getToolBar(
"Viewer Toolbar", viewerToolbar );
222 if ( viewerToolbar == 0 ) {
223 emit log(
LOGERR,
"Unable to get Viewer Toolbar!");
227 QList< QAction *> toolbarActions = viewerToolbar->actions();
228 QAction* pickAction = 0;
229 for (
int i = 0 ; i < toolbarActions.size() ; ++i ) {
230 if ( toolbarActions[i]->text() ==
"Pick" )
231 pickAction = toolbarActions[i];
236 viewerToolbarAction_ = viewerToolbar->insertWidget( pickAction, toolbar_ );
238 viewerToolbarAction_ = viewerToolbar->addWidget( toolbar_ );
239 viewerToolbarAction_->setText(tr(
"Light"));
243 lightOptions_ =
new QToolBar(tr(
"Transform light sources"));
244 lightOptions_->setAttribute(Qt::WA_AlwaysShowToolTips,
true);
246 QActionGroup* pickGroup =
new QActionGroup(lightOptions_);
247 pickGroup->setExclusive (
false);
249 moveMode_ =
new QAction(tr(
"Move light source"), pickGroup);
250 moveMode_->setStatusTip(tr(
"Move light source on virtual trackball around trackball center."));
251 moveMode_->setToolTip(tr(
"Move light source on virtual trackball around trackball center."));
252 moveMode_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"light-move.png") );
253 moveMode_->setCheckable(
true);
254 lightOptions_->addAction(moveMode_);
256 translateMode_ =
new QAction(tr(
"Translate light source"), pickGroup);
257 translateMode_->setStatusTip(tr(
"Translate light source on plane parallely to viewing plane."));
258 translateMode_->setToolTip(tr(
"Translate light source on plane parallely to viewing plane."));
259 translateMode_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"light-translate.png") );
260 translateMode_->setCheckable(
true);
261 lightOptions_->addAction(translateMode_);
263 rotateMode_ =
new QAction(tr(
"Rotate spotlight direction"), pickGroup);
264 rotateMode_->setStatusTip(tr(
"Rotate the spotlight direction of a light source."));
265 rotateMode_->setToolTip(tr(
"Rotate the spotlight direction of a light source."));
266 rotateMode_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"light-rotate.png") );
267 rotateMode_->setCheckable(
true);
268 lightOptions_->addAction(rotateMode_);
270 lightOptions_->addSeparator ();
272 allLightsMode_ =
new QAction(tr(
"All lights"), lightOptions_);
273 allLightsMode_->setStatusTip(tr(
"Transform all light sources if checked. If not, only apply to target light sources."));
274 allLightsMode_->setToolTip(tr(
"Transform all light sources if checked. If not, only apply to target light sources."));
275 allLightsMode_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"light-alllights.png") );
276 allLightsMode_->setCheckable(
true);
277 lightOptions_->addAction(allLightsMode_);
279 connect(pickGroup, SIGNAL(triggered(QAction*)),
this, SLOT(slotPickModeRequest(QAction*)) );
280 connect(allLightsMode_, SIGNAL(triggered(
bool)),
this, SLOT(slotSetAllOrTarget(
bool)) );
282 emit setPickModeToolbar (
"MoveLights", lightOptions_);
283 emit setPickModeToolbar (
"TranslateLights", lightOptions_);
284 emit setPickModeToolbar (
"RotateLights", lightOptions_);
297 lightAction_->setChecked(
false);
300 void TypeLightPlugin::slotLightModeRequest(
bool ) {
305 lightAction_->setChecked(
true);
308 moveMode_->setChecked(
true);
309 translateMode_->setChecked(
false);
310 rotateMode_->setChecked(
false);
313 void TypeLightPlugin::slotPickModeRequest(QAction* _action) {
317 if (_action == moveMode_){
320 lightAction_->setChecked(
true);
321 moveMode_->setChecked(
true);
322 translateMode_->setChecked(
false);
323 rotateMode_->setChecked(
false);
325 }
else if (_action == translateMode_){
328 lightAction_->setChecked(
true);
329 moveMode_->setChecked(
false);
330 translateMode_->setChecked(
true);
331 rotateMode_->setChecked(
false);
333 }
else if (_action == rotateMode_){
336 lightAction_->setChecked(
true);
337 moveMode_->setChecked(
false);
338 translateMode_->setChecked(
false);
339 rotateMode_->setChecked(
true);
343 void TypeLightPlugin::slotSetAllOrTarget(
bool _checked) {
345 allLightsMode_->setChecked(_checked);
346 onlyTargets_ = !_checked;
349 int TypeLightPlugin::addDefaultLight(QString _name) {
355 object->setName( _name );
357 object->defaultLight(
true);
363 emit emptyObjectAdded (object->
id() );
373 QString name = QString(tr(
"New Light %1.lgt").arg( object->
id() ));
377 object->setName( f.fileName() );
379 object->defaultLight(
false);
383 object->enablePicking(
true);
387 emit emptyObjectAdded (object->
id() );
407 removeDefaultLights();
408 lightSources_.push_back(_id);
413 for(uint i = 0; i < lightSources_.size(); ++i) {
414 if(lightSources_[i] == _id) lightSources_.erase(lightSources_.begin() + i);
417 if(lightSources_.empty() && !defaultLights_) {
433 if(lightObject != 0) {
449 if(d == FLT_MAX)
return 0.0f;
454 double TypeLightPlugin::getFarthestRadius() {
464 if(lightObject != 0) {
471 double tmp_d = fabs((source->
position() - c).norm());
472 if(tmp_d > d) d = tmp_d;
492 switch (_event->type())
494 case QEvent::MouseButtonPress:
497 light_matrix_.identity();
501 radius_ = getFarthestRadius();
503 depth_ = findDepth();
506 hitTrackball_ = computeClickOnTrackball(pos, lastPoint3D_, state);
518 QPoint p(_event->x(), _event->y());
540 lightRadius_ = (bbMin - bbMax).length()/4;
549 if(lightId_ == -1)
break;
551 hitLightTrackball_ = computeClickOnLightTrackball(pos, lastPoint3D_, state);
557 case QEvent::MouseButtonRelease:
560 hitLightTrackball_ =
false;
561 hitTrackball_ =
false;
564 case QEvent::MouseMove:
576 if(hitTrackball_ ==
false) {
578 hitTrackball_ = computeClickOnTrackball(pos, lastPoint3D_, state);
585 hitTrackball_ = computeClickOnTrackball(pos, lastPoint3D_, state);
596 double angle = acos(v1 | v2) * 180/M_PI;
598 rotateLights(axis, angle);
604 planeDepth_ = findDepth();
609 ACG::Vec3d p1(lastPoint2D_.x(), lastPoint2D_.y(), planeDepth_);
624 (lightId_ == -1))
break;
628 if(hitLightTrackball_ ==
false) {
630 hitLightTrackball_ = computeClickOnLightTrackball(pos, lastPoint3D_, state);
637 hitLightTrackball_ = computeClickOnLightTrackball(pos, lastPoint3D_, state);
648 double angle = acos(v1 | v2) * 180/M_PI;
650 rotateLightDirection(axis, angle);
681 double x = clickRelToC[0];
682 double y = clickRelToC[1];
686 double sq = radius_*radius_ - x*x - y*y;
688 double z = sq > 0.0 ? sqrt(sq) : 0.0;
690 if(sq <= 0.0) hit =
false;
712 double x = clickRelToC[0];
713 double y = clickRelToC[1];
717 double sq = lightRadius_*lightRadius_ - x*x - y*y;
719 double z = sq > 0.0 ? sqrt(sq) : 0.0;
721 if(sq <= 0.0) hit =
false;
732 light_matrix_.rotate(_angle, _axis[0], _axis[1], _axis[2]);
743 m.
rotate(_angle, _axis[0], _axis[1], _axis[2]);
774 if(lightObject != 0) {
781 std::cerr <<
"Not implemented yet! rotate directional light sources" << std::endl;
795 r = light_matrix_.transform_vector(r);
818 light_matrix_.identity();
const ACG::Vec3d trackBallCenter(int _viewer)
Get the trackball Center.
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
bool defaultLight() const
Is light default light source?
const GLMatrixd & inverse_modelview() const
get inverse modelview matrix
void slotAllCleared()
Called if the whole scene is cleared.
void addedEmptyObject(int _id)
An empty object has been added.
void rotateLights(ACG::Vec3d &_axis, double _angle)
Rotate light sources.
bool scenegraphPick(ACG::SceneGraph::PickTarget _pickTarget, const QPoint &_mousePos, size_t &_nodeIdx, size_t &_targetIdx, ACG::Vec3d *_hitPointPtr=0)
Execute picking operation on scenegraph.
int getObjectId(const QString &_name)
pick any of the prior targets (should be implemented for all nodes)
LightNode * lightNodeVis()
Get the scenegraph Node.
bool directional() const
Check if the light source is a directional light source.
void spotDirection(Vec3d _pos)
Set spot direction.
LightSource * lightSource()
void specularColor(Vec4f _color)
set Specular color for LightSource
unsigned int activeExaminer()
Get the id of the examiner which got the last mouse events.
bool getPickedObject(const size_t _node_idx, BaseObjectData *&_object)
Get the picked mesh.
bool computeClickOnTrackball(const QPoint &_v2D, ACG::Vec3d &_clickOnSphere, ACG::GLState &_state)
Compute click on trackball containing the light source.
void enable()
enable LightSource
int viewport_width() const
get viewport width
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
void updateLights()
Update light sources' positions.
const QStringList TARGET_OBJECTS("target")
Iterable object range.
VectorT< T, 3 > transform_point(const VectorT< T, 3 > &_v) const
transform point (x',y',z',1) = M * (x,y,z,1)
void slotPickModeChanged(const std::string &_pickMode)
The pickingMode has changed.
const QStringList ALL_OBJECTS
Iterable object range.
void rotateLightDirection(ACG::Vec3d &_axis, double _angle)
Rotate spot light direction.
LightObject * lightObject(BaseObjectData *_object)
Cast an BaseObject to a LightObject if possible.
void fixedPosition(bool _state)
make LightSource fixed or moveable with ModelViewMatrix
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
DLLEXPORT void setTypeIcon(DataType _id, QString _icon)
Set an Icon for a given DataType.
Viewer::ViewerProperties & viewerProperties(int _id)
Get the viewer properties Use this functions to get basic viewer properties such as backgroundcolor o...
Vec3d unproject(const Vec3d &_winPoint) const
unproject point in window coordinates _winPoint to world coordinates
Vec3d project(const Vec3d &_point) const
project point in world coordinates to window coordinates
void disableExaminerLightHandling()
Disable the core light handling.
VectorT< double, 3 > Vec3d
int viewport_height() const
get viewport height
Structure to hold options for one LightSource.
DLLEXPORT DataType addDataType(QString _name, QString _readableName)
Adds a datatype and returns the id for the new type.
void diffuseColor(Vec4f _color)
set Diffuse color for LightSource
Vec3d direction() const
Get direction of the light source.
const GLMatrixd & modelview() const
get modelview matrix
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.
void position(Vec3d _pos)
Set position for LightSource.
void identity()
setup an identity matrix
ACG::GLState & glState()
Get the glState of the Viewer.
Viewer::ActionMode actionMode()
Get the current Action mode.
void objectDeleted(int _id)
An object was deleted.
bool computeClickOnLightTrackball(const QPoint &_v2D, ACG::Vec3d &_clickOnSphere, ACG::GLState &_state)
Compute click on trackball of light source.
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
LightSource * lightSource(BaseObjectData *_object)
Get the lightSource in this Object.
void setName(QString _name)
Set the name of the Object.
VectorT< T, 3 > transform_vector(const VectorT< T, 3 > &_v) const
transform vector (x',y',z',0) = A * (x,y,z,0)
void boundingBox(ACG::Vec3d &, ACG::Vec3d &)
Get bounding box (for visualization purposes)
void pluginsInitialized()
Initialize Plugin step 2.
void traverse(ACG::SceneGraph::MouseEventAction &_action)
void slotMouseEventLight(QMouseEvent *_event)
Mouse Event from main application ( Light Mode )
const std::string pickMode()
Get the current Picking mode.
int addEmpty()
Create an empty object.
float findDepth()
Find depth of nearest light source.