gKit2 light
Public Member Functions | Protected Attributes | List of all members

representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet. More...

#include <orbiter.h>

Public Member Functions

 Orbiter ()
 cree une camera par defaut. observe le centre (0, 0, 0) a une distance 5. More...
 
 Orbiter (const Point &center, const float size)
 cree une camera. observe le point center a une distance size. More...
 
 Orbiter (const Point &pmin, const Point &pmax)
 cree une camera. observe une boite alignee sur les axes. More...
 
void lookat (const Point &center, const float size)
 observe le point center a une distance size. More...
 
void lookat (const Point &pmin, const Point &pmax)
 observe le centre d'une boite englobante. More...
 
void rotation (const float x, const float y)
 change le point de vue / la direction d'observation. More...
 
void translation (const float x, const float y)
 deplace le centre / le point observe. More...
 
void move (const float z)
 rapproche / eloigne la camera du centre. More...
 
Transform view () const
 renvoie la transformation vue. More...
 
Transform projection (const float width, const float height, const float fov) const
 renvoie la projection reglee pour une image d'aspect width / height, et une ouverture de fov degres. More...
 
void frame (const float width, const float height, const float z, const float fov, Point &dO, Vector &dx, Vector &dy) const
 
Point position ()
 renvoie la position de la camera dans le repere du monde. More...
 
int read_orbiter (const char *filename)
 relit la position de l'orbiter depuis un fichier texte. More...
 
int write_orbiter (const char *filename)
 enregistre la position de l'orbiter dans un fichier texte. More...
 

Protected Attributes

Point m_center
 
vec2 m_position
 
vec2 m_rotation
 
float m_size
 
float m_radius
 

Detailed Description

representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.

Definition at line 16 of file orbiter.h.

Constructor & Destructor Documentation

Orbiter::Orbiter ( )
inline

cree une camera par defaut. observe le centre (0, 0, 0) a une distance 5.

Definition at line 20 of file orbiter.h.

20 : m_center(), m_position(), m_rotation(), m_size(5.f), m_radius(5.f) {}
Orbiter::Orbiter ( const Point center,
const float  size 
)
inline

cree une camera. observe le point center a une distance size.

Definition at line 22 of file orbiter.h.

22 : m_center(center), m_position(), m_rotation(), m_size(size), m_radius(size) {}
Orbiter::Orbiter ( const Point pmin,
const Point pmax 
)
inline

cree une camera. observe une boite alignee sur les axes.

Definition at line 24 of file orbiter.h.

24 : m_center(center(pmin, pmax)), m_position(), m_rotation(), m_size(distance(pmin, pmax)) {}
float distance(const Point &a, const Point &b)
renvoie la distance etre 2 points.
Definition: vec.cpp:7
Point center(const Point &a, const Point &b)
renvoie le milieu du segment ab.
Definition: vec.cpp:17

Member Function Documentation

void Orbiter::lookat ( const Point center,
const float  size 
)

observe le point center a une distance size.

Definition at line 7 of file orbiter.cpp.

8 {
9  m_center= center;
10  m_position= vec2(0, 0);
11  m_rotation= vec2(0, 0);
12  m_size= size;
13  m_radius= size;
14 }
vecteur generique, utilitaire.
Definition: vec.h:94
Point center(const Point &a, const Point &b)
renvoie le milieu du segment ab.
Definition: vec.cpp:17
void Orbiter::lookat ( const Point pmin,
const Point pmax 
)

observe le centre d'une boite englobante.

Definition at line 16 of file orbiter.cpp.

17 {
18  lookat(center(pmin, pmax), distance(pmin, pmax));
19 }
void lookat(const Point &center, const float size)
observe le point center a une distance size.
Definition: orbiter.cpp:7
float distance(const Point &a, const Point &b)
renvoie la distance etre 2 points.
Definition: vec.cpp:7
Point center(const Point &a, const Point &b)
renvoie le milieu du segment ab.
Definition: vec.cpp:17
void Orbiter::rotation ( const float  x,
const float  y 
)

change le point de vue / la direction d'observation.

Definition at line 21 of file orbiter.cpp.

22 {
23  m_rotation.x= m_rotation.x + y;
24  m_rotation.y= m_rotation.y + x;
25 }
void Orbiter::translation ( const float  x,
const float  y 
)

deplace le centre / le point observe.

Definition at line 27 of file orbiter.cpp.

28 {
29  m_position.x= m_position.x - m_size * x;
30  m_position.y= m_position.y + m_size * y;
31 }
void Orbiter::move ( const float  z)

rapproche / eloigne la camera du centre.

Definition at line 33 of file orbiter.cpp.

34 {
35  m_size= m_size - m_size * 0.01f * z;
36  if(m_size < 0.01f)
37  m_size= 0.01f;
38 }
Transform Orbiter::view ( ) const

renvoie la transformation vue.

Definition at line 40 of file orbiter.cpp.

41 {
42  return Translation( -m_position.x, -m_position.y, -m_size )
43  * RotationX(m_rotation.x) * RotationY(m_rotation.y)
44  * Translation( - Vector(m_center) );
45 }
representation d'un vecteur 3d.
Definition: vec.h:42
Transform RotationX(const float a)
renvoie la matrice representation une rotation de angle degree autour de l'axe X. ...
Definition: mat.cpp:146
Transform Translation(const Vector &v)
renvoie la matrice representant une translation par un vecteur.
Definition: mat.cpp:132
Transform RotationY(const float a)
renvoie la matrice representation une rotation de a degree autour de l'axe Y.
Definition: mat.cpp:158
Transform Orbiter::projection ( const float  width,
const float  height,
const float  fov 
) const

renvoie la projection reglee pour une image d'aspect width / height, et une ouverture de fov degres.

Definition at line 47 of file orbiter.cpp.

48 {
49  // calcule la distance entre le centre de l'objet et la camera
50  //~ Transform t= view();
51  //~ Point c= t(m_center);
52  //~ float d= -c.z;
53  float d= distance(m_center, Point(m_position.x, m_position.y, m_size)); // meme resultat plus rapide a calculer
54 
55  // regle near et far en fonction du centre et du rayon englobant l'objet
56  return Perspective(fov, width / height, std::max(0.1f, d - m_radius), std::max(1.f, d + m_radius));
57 }
Transform Perspective(const float fov, const float aspect, const float znear, const float zfar)
renvoie la matrice representant une transformation projection perspective.
Definition: mat.cpp:208
float distance(const Point &a, const Point &b)
renvoie la distance etre 2 points.
Definition: vec.cpp:7
representation d'un point 3d.
Definition: vec.h:19
void Orbiter::frame ( const float  width,
const float  height,
const float  z,
const float  fov,
Point dO,
Vector dx,
Vector dy 
) const

renvoie les coorodnnees de l'origine d0 et les axes dx, dy du plan image dans le repere du monde. permet de construire un rayon pour le pixel x, y :

  • l'extremite : un point dans le plan image avec z = 0 :
    Point d0;
    Vector dx0, dy0;
    camera.frame(width, height, 0, fov, d0, dx0, dy0);
    Point e= d0 + x*dx0 + y*dy0;
  • l'origine :
    Point o= camera.position();

ou autre solution, reproduire la projection openGL :

  • extremite : un point dans le plan image avec z = 1 :
    Point d1;
    Vector dx1, dy1;
    camera.frame( width, height, 1, fov, d1, dx1, dy1);
    Point e= d1 + x*dx1 + y*dy1;
  • origine : un point dans le plan image avec z = 0 :
    Point d0;
    Vector dx0, dy0;
    camera.frame(width, height, 0, fov, d0, dx0, dy0);
    Point o= d0 + x*dx0 + y*dy0;

Definition at line 59 of file orbiter.cpp.

60 {
61  Transform v= view();
62  Transform p= projection(width, height, fov);
63  Transform viewport= Viewport(width, height);
64  Transform t= viewport * p * v; // passage monde vers image
65  Transform tinv= t.inverse(); // l'inverse, passage image vers monde
66 
67  // origine du plan image
68  dO= tinv(Point(0, 0, z));
69  // axe x du plan image
70  Point d1= tinv(Point(1, 0, z));
71  // axe y du plan image
72  Point d2= tinv(Point(0, 1, z));
73 
74  dx= Vector(dO, d1);
75  dy= Vector(dO, d2);
76 }
Transform inverse() const
renvoie l'inverse de la matrice.
Definition: mat.cpp:263
Transform Viewport(const float width, const float height)
renvoie la matrice representant une transformation viewport.
Definition: mat.cpp:221
Transform view() const
renvoie la transformation vue.
Definition: orbiter.cpp:40
representation d'un vecteur 3d.
Definition: vec.h:42
Transform projection(const float width, const float height, const float fov) const
renvoie la projection reglee pour une image d'aspect width / height, et une ouverture de fov degres...
Definition: orbiter.cpp:47
representation d'une transformation, une matrice 4x4, organisee par ligne / row major.
Definition: mat.h:20
representation d'un point 3d.
Definition: vec.h:19
Point Orbiter::position ( )

renvoie la position de la camera dans le repere du monde.

Definition at line 78 of file orbiter.cpp.

79 {
80  Transform t= view(); // passage monde vers camera
81  Transform tinv= t.inverse(); // l'inverse, passage camera vers monde
82 
83  return tinv(Point(0, 0, 0)); // la camera se trouve a l'origine, dans le repere camera...
84 }
Transform inverse() const
renvoie l'inverse de la matrice.
Definition: mat.cpp:263
Transform view() const
renvoie la transformation vue.
Definition: orbiter.cpp:40
representation d'une transformation, une matrice 4x4, organisee par ligne / row major.
Definition: mat.h:20
representation d'un point 3d.
Definition: vec.h:19
int Orbiter::read_orbiter ( const char *  filename)

relit la position de l'orbiter depuis un fichier texte.

Definition at line 86 of file orbiter.cpp.

87 {
88  FILE *in= fopen(filename, "rt");
89  if(in == NULL)
90  {
91  printf("[error] loading orbiter '%s'...\n", filename);
92  return -1;
93  }
94 
95  printf("loading orbiter '%s'...\n", filename);
96 
97  bool errors= false;
98  if(fscanf(in, "c %f %f %f \n", &m_center.x, &m_center.y, &m_center.z) != 3)
99  errors= true;
100  if(fscanf(in, "p %f %f\n", &m_position.x, &m_position.y) != 2)
101  errors= true;
102  if(fscanf(in, "r %f %f\n", &m_rotation.x, &m_rotation.y) != 2)
103  errors= true;
104  if(fscanf(in, "s %f\n", &m_size) != 1)
105  errors= true;
106 
107  fclose(in);
108  if(errors)
109  {
110  printf("[error] loading orbiter '%s'...\n", filename);
111  return -1;
112  }
113 
114  return 0;
115 }
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Definition: text.cpp:140
int Orbiter::write_orbiter ( const char *  filename)

enregistre la position de l'orbiter dans un fichier texte.

Definition at line 117 of file orbiter.cpp.

118 {
119  FILE *out= fopen(filename, "wt");
120  if(out == NULL)
121  return -1;
122 
123  printf("writing orbiter '%s'...\n", filename);
124 
125  fprintf(out, "c %f %f %f\n", m_center.x, m_center.y, m_center.z);
126  fprintf(out, "p %f %f\n", m_position.x, m_position.y);
127  fprintf(out, "r %f %f\n", m_rotation.x, m_rotation.y);
128  fprintf(out, "s %f\n", m_size);
129 
130  fclose(out);
131  return 0;
132 }
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Definition: text.cpp:140

The documentation for this class was generated from the following files: