2004-03-25 15:55:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
2016-06-13 07:29:25 +02:00
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
2004-03-25 15:55:25 +01:00
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
2013-03-19 17:59:45 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
2004-03-25 15:55:25 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
|
|
|
* for more details. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
|
|
History
|
|
|
|
|
|
|
|
$Log: not supported by cvs2svn $
|
2007-02-22 10:17:29 +01:00
|
|
|
Revision 1.15 2007/01/18 01:24:32 cignoni
|
|
|
|
Added cast for mac compiling
|
|
|
|
|
2007-01-18 02:26:23 +01:00
|
|
|
Revision 1.14 2005/10/15 15:23:32 ponchio
|
|
|
|
Fixed viewport<->window coordinate change for the z. (z = 2*z -1 now)
|
|
|
|
Accordingly to gluUnproject documentation.
|
|
|
|
|
2005-10-15 17:23:32 +02:00
|
|
|
Revision 1.13 2005/02/11 11:53:18 tommyfranken
|
|
|
|
changed pointf to point<t> in ViewLineFromWindow
|
|
|
|
|
2005-02-11 12:53:18 +01:00
|
|
|
Revision 1.12 2005/02/10 20:09:11 tarini
|
|
|
|
dispelled the mega-evil of GL_TRANSPOSE_*_MATRIX_ARB
|
|
|
|
|
2005-02-10 21:09:11 +01:00
|
|
|
Revision 1.11 2005/01/19 10:29:45 cignoni
|
|
|
|
Removed the inclusion of a glext.h
|
|
|
|
|
2005-01-19 11:29:45 +01:00
|
|
|
Revision 1.10 2005/01/18 16:47:42 ricciodimare
|
|
|
|
*** empty log message ***
|
|
|
|
|
2005-01-18 17:47:42 +01:00
|
|
|
Revision 1.9 2004/09/28 10:22:00 ponchio
|
|
|
|
Added #include <GL/gl.h>
|
|
|
|
|
2004-09-28 12:22:00 +02:00
|
|
|
Revision 1.8 2004/05/26 15:15:46 cignoni
|
|
|
|
Removed inclusion of gl extension stuff
|
|
|
|
|
2004-05-26 17:15:46 +02:00
|
|
|
Revision 1.7 2004/05/14 03:15:40 ponchio
|
|
|
|
Added ViewLineFromModel
|
|
|
|
|
2004-05-14 05:15:40 +02:00
|
|
|
Revision 1.6 2004/05/12 20:55:18 ponchio
|
|
|
|
*** empty log message ***
|
|
|
|
|
2004-05-12 22:55:18 +02:00
|
|
|
Revision 1.5 2004/05/07 12:46:08 cignoni
|
|
|
|
Restructured and adapted in a better way to opengl
|
|
|
|
|
2004-05-07 14:46:08 +02:00
|
|
|
Revision 1.4 2004/04/07 10:54:11 cignoni
|
|
|
|
Commented out unused parameter names and other minor warning related issues
|
|
|
|
|
2004-04-07 12:54:11 +02:00
|
|
|
Revision 1.3 2004/03/31 15:07:37 ponchio
|
|
|
|
CAMERA_H -> VCG_CAMERA_H
|
|
|
|
|
2004-03-31 17:07:37 +02:00
|
|
|
Revision 1.2 2004/03/25 14:55:25 ponchio
|
|
|
|
Adding copyright.
|
|
|
|
|
2004-03-25 15:55:25 +01:00
|
|
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-03-11 10:32:12 +01:00
|
|
|
#ifndef __VCGLIB_WRAP_GUI_VIEW_H
|
|
|
|
#define __VCGLIB_WRAP_GUI_VIEW_H
|
2004-03-25 15:50:08 +01:00
|
|
|
|
2004-05-07 14:46:08 +02:00
|
|
|
/**********************
|
|
|
|
WARNING
|
|
|
|
Everything here assumes the opengl window coord system.
|
|
|
|
the 0,0 is bottom left
|
|
|
|
y is upward!
|
|
|
|
**********************/
|
2004-03-25 15:50:08 +01:00
|
|
|
|
2004-05-07 14:46:08 +02:00
|
|
|
#include <vcg/space/point3.h>
|
|
|
|
#include <vcg/space/plane3.h>
|
|
|
|
#include <vcg/space/line3.h>
|
|
|
|
#include <vcg/math/matrix44.h>
|
2008-10-29 12:17:11 +01:00
|
|
|
#include <wrap/gl/math.h>
|
2004-03-25 15:50:08 +01:00
|
|
|
|
2004-09-28 12:22:00 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2007-02-22 10:17:29 +01:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <OpenGL/gl.h>
|
|
|
|
#else
|
2004-09-28 12:22:00 +02:00
|
|
|
#include <GL/gl.h>
|
2007-02-22 10:17:29 +01:00
|
|
|
#endif
|
|
|
|
|
2004-03-25 15:50:08 +01:00
|
|
|
namespace vcg {
|
2004-05-07 14:46:08 +02:00
|
|
|
/**
|
2013-03-19 17:59:45 +01:00
|
|
|
This class represent the viewing parameters under opengl.
|
2004-05-07 14:46:08 +02:00
|
|
|
Mainly it stores the projection and modelview matrix and the viewport
|
|
|
|
and it is used to simply project back and forth points, computing line of sight, planes etc.
|
2009-03-11 10:32:12 +01:00
|
|
|
|
|
|
|
Note: mainly it is used only by the TrackBall.
|
|
|
|
|
2004-05-07 14:46:08 +02:00
|
|
|
*/
|
2004-03-25 15:50:08 +01:00
|
|
|
|
|
|
|
template <class T> class View {
|
|
|
|
public:
|
2014-11-05 00:16:04 +01:00
|
|
|
View();
|
2004-03-25 15:50:08 +01:00
|
|
|
void GetView();
|
2010-04-20 20:23:51 +02:00
|
|
|
void SetView(const float *_proj, const float *_modelview, const int *_viewport);
|
2004-03-25 15:50:08 +01:00
|
|
|
Point3<T> Project(const Point3<T> &p) const;
|
|
|
|
Point3<T> UnProject(const Point3<T> &p) const;
|
2004-05-14 05:15:40 +02:00
|
|
|
Point3<T> ViewPoint() const;
|
2004-05-07 14:46:08 +02:00
|
|
|
|
|
|
|
/// Return the plane perpendicular to the view axis and passing through point P.
|
|
|
|
Plane3<T> ViewPlaneFromModel(const Point3<T> &p);
|
2004-05-14 05:15:40 +02:00
|
|
|
|
|
|
|
/// Return the line of view passing through point P.
|
|
|
|
Line3<T> ViewLineFromModel(const Point3<T> &p);
|
2013-03-19 17:59:45 +01:00
|
|
|
|
2004-05-07 14:46:08 +02:00
|
|
|
/// Return the line passing through the point p and the observer.
|
|
|
|
Line3<T> ViewLineFromWindow(const Point3<T> &p);
|
2013-03-19 17:59:45 +01:00
|
|
|
|
2004-05-07 14:46:08 +02:00
|
|
|
/// Convert coordinates from the range -1..1 of Normalized Device Coords to range 0 viewport[2]
|
|
|
|
Point3<T> NormDevCoordToWindowCoord(const Point3<T> &p) const;
|
2013-03-19 17:59:45 +01:00
|
|
|
|
2004-05-07 14:46:08 +02:00
|
|
|
/// Convert coordinates from 0--viewport[2] to the range -1..1 of Normalized Device Coords to
|
|
|
|
Point3<T> WindowCoordToNormDevCoord(const Point3<T> &p) const;
|
2004-03-25 15:50:08 +01:00
|
|
|
|
|
|
|
Matrix44<T> proj;
|
|
|
|
Matrix44<T> model;
|
|
|
|
Matrix44<T> matrix;
|
2013-03-19 17:59:45 +01:00
|
|
|
Matrix44<T> inverse;
|
|
|
|
int viewport[4];
|
2014-11-05 00:16:04 +01:00
|
|
|
bool isOrtho;
|
2004-03-25 15:50:08 +01:00
|
|
|
};
|
|
|
|
|
2014-11-05 00:16:04 +01:00
|
|
|
template <class T> View<T>::View() {
|
|
|
|
isOrtho=false;
|
|
|
|
}
|
|
|
|
|
2004-03-25 15:50:08 +01:00
|
|
|
template <class T> void View<T>::GetView() {
|
2014-11-05 00:16:04 +01:00
|
|
|
glGetv(GL_PROJECTION_MATRIX,proj);
|
|
|
|
glGetv(GL_MODELVIEW_MATRIX,model);
|
|
|
|
glGetIntegerv(GL_VIEWPORT, (GLint*)viewport);
|
|
|
|
|
|
|
|
if(proj[3][3]==0) isOrtho = false;
|
|
|
|
else isOrtho = true;
|
2008-10-30 12:31:28 +01:00
|
|
|
|
2014-11-05 00:16:04 +01:00
|
|
|
matrix = proj*model;
|
|
|
|
inverse = vcg::Inverse(matrix);
|
2004-03-25 15:50:08 +01:00
|
|
|
}
|
|
|
|
|
2011-10-05 17:04:40 +02:00
|
|
|
template <class T> void View<T>::SetView(const float *_proj,
|
|
|
|
const float *_modelview,
|
|
|
|
const int *_viewport) {
|
2010-04-20 20:23:51 +02:00
|
|
|
for(int i = 0; i < 4; i++) {
|
|
|
|
for(int k =0; k < 4; k++) {
|
|
|
|
proj[i][k] = _proj[4*i+k];
|
|
|
|
model[i][k] = _modelview[4*i+k];
|
|
|
|
}
|
|
|
|
viewport[i] = _viewport[i];
|
|
|
|
}
|
|
|
|
matrix = proj*model;
|
|
|
|
inverse = matrix;
|
|
|
|
Invert(inverse);
|
2004-03-25 15:50:08 +01:00
|
|
|
}
|
|
|
|
|
2004-05-14 05:15:40 +02:00
|
|
|
template <class T> Point3<T> View<T>::ViewPoint() const {
|
2014-11-05 00:16:04 +01:00
|
|
|
if(isOrtho) return vcg::Inverse(model)* Point3<T>(0, 0, 3);
|
2013-03-19 17:59:45 +01:00
|
|
|
return vcg::Inverse(model)* Point3<T>(0, 0, 0);
|
2004-05-07 14:46:08 +02:00
|
|
|
}
|
|
|
|
// Note that p it is assumed to be in model coordinate.
|
|
|
|
template <class T> Plane3<T> View<T>::ViewPlaneFromModel(const Point3<T> &p)
|
|
|
|
{
|
2010-11-15 01:09:48 +01:00
|
|
|
//compute normal, pointing away from view.
|
2013-03-19 17:59:45 +01:00
|
|
|
Matrix44<T> imodel = vcg::Inverse(model);
|
2004-05-07 14:46:08 +02:00
|
|
|
Point3<T> vp=ViewPoint();
|
2010-11-15 01:09:48 +01:00
|
|
|
vcg::Point3f n = imodel * vcg::Point3<T>(0.0f, 0, -1.0f) - vp;
|
|
|
|
|
|
|
|
Plane3<T> pl;
|
|
|
|
pl.Init(p, n);
|
2004-05-07 14:46:08 +02:00
|
|
|
return pl;
|
|
|
|
}
|
2004-05-14 05:15:40 +02:00
|
|
|
|
|
|
|
// Note that p it is assumed to be in model coordinate.
|
|
|
|
template <class T> Line3<T> View<T>::ViewLineFromModel(const Point3<T> &p)
|
|
|
|
{
|
|
|
|
Line3<T> line;
|
2014-11-05 00:16:04 +01:00
|
|
|
Point3<T> vp=ViewPoint();
|
|
|
|
if(isOrtho){
|
|
|
|
line.SetOrigin(p);
|
|
|
|
line.SetDirection(- vp );
|
|
|
|
} else {
|
|
|
|
line.SetOrigin(vp);
|
|
|
|
line.SetDirection(p - vp);
|
|
|
|
}
|
2004-05-14 05:15:40 +02:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2004-05-07 14:46:08 +02:00
|
|
|
// Note that p it is assumed to be in window coordinate.
|
|
|
|
template <class T> Line3<T> View<T>::ViewLineFromWindow(const Point3<T> &p)
|
|
|
|
{
|
2014-11-05 00:16:04 +01:00
|
|
|
Line3<T> line; // plane perpedicular to view direction and passing through manip center
|
|
|
|
Point3<T> vp=ViewPoint();
|
|
|
|
Point3<T> pp=UnProject(p);
|
|
|
|
|
|
|
|
if(isOrtho){
|
|
|
|
line.SetOrigin(pp);
|
|
|
|
line.SetDirection(- vp );
|
|
|
|
} else {
|
|
|
|
line.SetOrigin(vp);
|
|
|
|
line.SetDirection(pp-vp);
|
|
|
|
}
|
|
|
|
return line;
|
2004-03-25 15:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T> Point3<T> View<T>::Project(const Point3<T> &p) const {
|
|
|
|
Point3<T> r;
|
2008-10-27 20:35:17 +01:00
|
|
|
r = matrix * p;
|
2004-05-07 14:46:08 +02:00
|
|
|
return NormDevCoordToWindowCoord(r);
|
|
|
|
}
|
2004-03-25 15:50:08 +01:00
|
|
|
|
|
|
|
template <class T> Point3<T> View<T>::UnProject(const Point3<T> &p) const {
|
2004-05-07 14:46:08 +02:00
|
|
|
Point3<T> s = WindowCoordToNormDevCoord(p);
|
|
|
|
s = inverse * s ;
|
2008-10-27 20:35:17 +01:00
|
|
|
return s;
|
2004-03-25 15:50:08 +01:00
|
|
|
}
|
|
|
|
|
2004-05-07 14:46:08 +02:00
|
|
|
// Come spiegato nelle glspec
|
2013-03-19 17:59:45 +01:00
|
|
|
// dopo la perspective division le coordinate sono dette normalized device coords ( NDC ).
|
2004-05-07 14:46:08 +02:00
|
|
|
// Per passare alle window coords si deve fare la viewport transformation.
|
|
|
|
// Le coordinate di viewport stanno tra -1 e 1
|
|
|
|
|
|
|
|
template <class T> Point3<T> View<T>::NormDevCoordToWindowCoord(const Point3<T> &p) const {
|
2004-03-25 15:50:08 +01:00
|
|
|
Point3<T> a;
|
|
|
|
a[0] = (p[0]+1)*(viewport[2]/(T)2.0)+viewport[0];
|
2013-03-19 17:59:45 +01:00
|
|
|
a[1] = (p[1]+1)*(viewport[3]/(T)2.0)+viewport[1];
|
2004-05-07 14:46:08 +02:00
|
|
|
//a[1] = viewport[3] - a[1];
|
2005-10-15 17:23:32 +02:00
|
|
|
a[2] = (p[2]+1)/2;
|
2004-03-25 15:50:08 +01:00
|
|
|
return a;
|
|
|
|
}
|
2004-05-07 14:46:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
template <class T> Point3<T> View<T>::WindowCoordToNormDevCoord(const Point3<T> &p) const {
|
2004-03-25 15:50:08 +01:00
|
|
|
Point3<T> a;
|
|
|
|
a[0] = (p[0]- viewport[0])/ (viewport[2]/(T)2.0) - 1;
|
2013-03-19 17:59:45 +01:00
|
|
|
a[1] = (p[1]- viewport[1])/ (viewport[3]/(T)2.0) - 1;
|
2004-05-07 14:46:08 +02:00
|
|
|
//a[1] = -a[1];
|
2013-03-19 17:59:45 +01:00
|
|
|
a[2] = 2*p[2] - 1;
|
2004-03-25 15:50:08 +01:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}//namespace
|
|
|
|
|
2004-05-14 05:15:40 +02:00
|
|
|
#endif
|