Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move light position #1070

Merged
merged 10 commits into from
Nov 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
- Minor improvements of default settings in Viewer3D. (David
Coeurjolly, [#1066](https:/DGtal-team/DGtal/pull/1066))

- New possibility to move the light source direction using the mouse move
in Viewer3D (with the key SHIFT+CTRL (SHIFT+CMD on mac)). The light source
direction is now defined according the main coordinate system (no more from
the camera center).
(Bertrand Kerautret [#1070](https:/DGtal-team/DGtal/pull/1070))


## Bug Fixes

- *Configuration/General*
Expand Down
4 changes: 4 additions & 0 deletions src/DGtal/io/doc/moduleDisplay3D.dox
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ You should obtain the following visualization:

@note Note that you can display the camera settings in the console
(key C) which can be used in Board2Dto3D described in the following.
@note The light source direction is defined according the main coordinate
system (not from the camera center). It can be moved by a
mouse move in the Viewer3D (with the key SHIFT+CTRL (SHIFT+CMD on
mac)).



Expand Down
40 changes: 36 additions & 4 deletions src/DGtal/io/viewers/Viewer3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,28 @@ namespace DGtal
* @param e the QKeyEvent
**/
virtual void keyPressEvent ( QKeyEvent *e );



/**
* Used to manage a mouse move event (to handle light move).
*
* @param e the QMouseEvent
**/
virtual void mouseMoveEvent ( QMouseEvent *e );

/**
* Used to manage a mouse press event (to handle light move).
*
* @param e the QMouseEvent
**/
virtual void mousePressEvent ( QMouseEvent *e );

/**
* Used to manage a mouse release event (to handle light move).
*
* @param e the QMouseEvent
**/
virtual void mouseReleaseEvent ( QMouseEvent *e );

/**
* Used to sort pixel from camera
**/
Expand Down Expand Up @@ -1194,6 +1214,7 @@ namespace DGtal

/**
* Rotate a vertex from a given angle, a center point and a rotation direction.
* @tparam TValues the type of coordinate to be rotated.
* @param x the x coordinate of the point to rotated (return).
* @param y the y coordinate of the point to rotated (return).
* @param z the z coordinate of the point to rotated (return).
Expand All @@ -1203,9 +1224,10 @@ namespace DGtal
* @param rotationAngle the angle of the rotation.
* @param rotationDir the rotation is applied around this axis direction.
**/


template <typename TValues>
static
void rotatePoint(double &x, double &y, double &z,
void rotatePoint(TValues &x, TValues &y, TValues &z,
double cx, double cy, double cz,
double rotationAngle, ImageDirection rotationDir);

Expand Down Expand Up @@ -1257,6 +1279,10 @@ namespace DGtal
double camera_direction[3]; ///< camera direction
double camera_upVector[3]; ///< camera up-vector

float myLightTheta; /// the light position (inclination)
float myLightPhi; /// the light position (azimuth)
float myLightR; /// the light position (distance)
GLfloat myLightPosition [4]; // the light position in cartesian coordinates
double ZNear; ///< znear distance
double ZFar; ///< zfar distance

Expand All @@ -1268,6 +1294,12 @@ namespace DGtal
float myGLScaleFactorY;
float myGLScaleFactorZ;

// Used to apply interactive light rotation
float myLigthRotationStep; /// the angle rotation increment used for interactive light move
int myRefMouseXPos; /// the reference mouse x-position used to determince the light position change (azimuth)
int myRefMouseYPos; /// the reference mouse y-position used to determince the light position change (inclination)
bool myIsMovingLight; /// flag to display the ligth source when it is moved by the user

/// Used to store all displayed images
std::vector<TextureImage> myGSImageList;
/// Used to store all the domains
Expand Down
85 changes: 79 additions & 6 deletions src/DGtal/io/viewers/Viewer3D.ih
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ DGtal::Viewer3D<Space, KSpace>::rotateDomain(Image2DDomainD3D &anDom, double ang


template < typename Space ,typename KSpace >
template <typename TValues>
inline
void
DGtal::Viewer3D<Space, KSpace>::rotatePoint(double &x, double &y, double &z, double cx, double cy, double cz,
DGtal::Viewer3D<Space, KSpace>::rotatePoint(TValues &x, TValues &y, TValues &z, double cx, double cy, double cz,
double angleRotation, ImageDirection rotationDir){
double dx = x-cx; double dy = y-cy; double dz = z-cz;
if(rotationDir == zDirection){
Expand Down Expand Up @@ -663,13 +664,16 @@ inline
void
DGtal::Viewer3D<Space, KSpace>::draw()
{


glPushMatrix();
glMultMatrixd ( manipulatedFrame()->matrix() );
glPushMatrix();
glScalef(myGLScaleFactorX, myGLScaleFactorY, myGLScaleFactorZ);
glLightfv(GL_LIGHT0, GL_POSITION, myLightPosition);
unsigned int i = 0;
typename vector< typename Viewer3D<Space, KSpace>::ClippingPlaneD3D >::const_iterator it = Viewer3D<Space, KSpace>::myClippingPlaneList.begin();

// OpenGL can't draw more than GL_MAX_CLIP_PLANES clipping plane
while ( i < GL_MAX_CLIP_PLANES && it !=Viewer3D<Space, KSpace>::myClippingPlaneList.end() )
{
Expand Down Expand Up @@ -774,7 +778,10 @@ DGtal::Viewer3D<Space, KSpace>::draw()
glCallList(myPolygonSetListWiredId);
}


if(myIsMovingLight){
drawLight(GL_LIGHT0);
}

glPopMatrix();
glPopMatrix();
}
Expand All @@ -801,13 +808,25 @@ DGtal::Viewer3D<Space, KSpace>::init()
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

myIsMovingLight=false;
myLigthRotationStep = 0.01;
myRefMouseXPos=0;
myRefMouseYPos=0;
myLightTheta = M_PI/8.0;
myLightPhi = M_PI/4.0;
myLightR = 1.0;
myLightPosition[0] = myLightR*cos(myLightTheta)*cos(myLightPhi);
myLightPosition[1] = myLightR*cos(myLightTheta)*sin(myLightPhi);
myLightPosition[2] = myLightR*sin(myLightTheta);
myLightPosition[3] = 0.0;

glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_POSITION, myLightPosition);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
Expand Down Expand Up @@ -855,6 +874,12 @@ DGtal::Viewer3D<Space, KSpace>::init()
setKeyDescription ( Qt::Key_R, "Reset default scale for 3 axes to 1.0f." );
setKeyDescription ( Qt::Key_D, "Enable/Disable the two side face rendering." );
setKeyDescription ( Qt::Key_R, "Reset default scale for 3 axes to 1.0f." );
#if !defined (QGLVIEWER_VERSION) || QGLVIEWER_VERSION < 0x020500
setMouseBindingDescription((Qt::ControlModifier|Qt::ShiftModifier) + Qt::LeftButton, "move light source position defined in the main coordinate system (an x-axis (resp. y-axis) mouse move changes the azimuth (resp. inclination) angle of the light source). Note that light source is always looking at the center point (0,0,0).");
#else
setMouseBindingDescription(Qt::ControlModifier|Qt::ShiftModifier, Qt::LeftButton, "move light source position defined in the main coordinate system (an x-axis (resp. y-axis) mouse move changes the azimuth (resp. inclination) angle of the light source). Note that light source is always looking at the center point (0,0,0).");
#endif


glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

Expand Down Expand Up @@ -892,7 +917,6 @@ DGtal::Viewer3D<Space, KSpace>::sortTriangleFromCamera()
comp.posCam= camera()->position();
for (typename std::vector<std::vector< typename Viewer3D<Space, KSpace>::TriangleD3D> >::iterator it = Viewer3D<Space, KSpace>::myTriangleSetList.begin(); it != Viewer3D<Space, KSpace>::myTriangleSetList.end(); it++)
{
//DGtal::trace.info() << "sort triangle size" << it->size() << std::endl;
sort ( it->begin(), it->end(), comp );
}

Expand Down Expand Up @@ -1115,6 +1139,55 @@ DGtal::Viewer3D<Space, KSpace>::glDrawGLBall ( typename Viewer3D<Space, KSpace>:
}
}

template< typename Space, typename KSpace>
void
DGtal::Viewer3D<Space, KSpace>::mousePressEvent ( QMouseEvent *e )
{
if(e->modifiers() == (Qt::ControlModifier|Qt::ShiftModifier)){
myIsMovingLight=true;
myRefMouseXPos = e->x();
myRefMouseYPos = e->y();
}else{
QGLViewer::mousePressEvent(e);
}
}
template< typename Space, typename KSpace>
void
DGtal::Viewer3D<Space, KSpace>::mouseReleaseEvent ( QMouseEvent *e )
{
if(e->modifiers() == (Qt::ControlModifier|Qt::ShiftModifier) || myIsMovingLight){
myIsMovingLight=false;
updateGL();
}else{
QGLViewer::mouseReleaseEvent(e);
}
}

template< typename Space, typename KSpace>
void
DGtal::Viewer3D<Space, KSpace>::mouseMoveEvent ( QMouseEvent *e )
{
if(e->modifiers() == (Qt::ControlModifier|Qt::ShiftModifier)){
int varX = e->x() - myRefMouseXPos;
int varY = e->y() - myRefMouseYPos;
myLightPhi += varX*myLigthRotationStep;
myLightTheta += varY*myLigthRotationStep/2.0;
myLightPosition[0] = myLightR*cos(myLightTheta)*cos(myLightPhi);
myLightPosition[1] = myLightR*cos(myLightTheta)*sin(myLightPhi);
myLightPosition[2] = myLightR*sin(myLightTheta);
myLightPosition[3] = 0.0;

glLightfv(GL_LIGHT0, GL_POSITION, myLightPosition);
myRefMouseXPos = e->x();
myRefMouseYPos = e->y();
updateGL();
}else{
QGLViewer::mouseMoveEvent(e);
}
}




template< typename Space, typename KSpace>
void
Expand Down