gKit2 light
orbiter.cpp
1 
2 #include <cstdio>
3 #include <algorithm>
4 
5 #include "orbiter.h"
6 
7 void Orbiter::lookat( const Point& center, const float size )
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 }
15 
16 void Orbiter::lookat( const Point& pmin, const Point& pmax )
17 {
18  lookat(center(pmin, pmax), distance(pmin, pmax));
19 }
20 
21 void Orbiter::rotation( const float x, const float y )
22 {
23  m_rotation.x= m_rotation.x + y;
24  m_rotation.y= m_rotation.y + x;
25 }
26 
27 void Orbiter::translation( const float x, const float y )
28 {
29  m_position.x= m_position.x - m_size * x;
30  m_position.y= m_position.y + m_size * y;
31 }
32 
33 void Orbiter::move( const float z )
34 {
35  m_size= m_size - m_size * 0.01f * z;
36  if(m_size < 0.01f)
37  m_size= 0.01f;
38 }
39 
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 }
46 
47 Transform Orbiter::projection( const float width, const float height, const float fov ) const
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 }
58 
59 void Orbiter::frame( const float width, const float height, const float z, const float fov, Point& dO, Vector& dx, Vector& dy ) const
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 }
77 
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 }
85 
86 int Orbiter::read_orbiter( const char *filename )
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 }
116 
117 int Orbiter::write_orbiter( const char *filename )
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 }
Transform inverse() const
renvoie l'inverse de la matrice.
Definition: mat.cpp:263
vecteur generique, utilitaire.
Definition: vec.h:94
void move(const float z)
rapproche / eloigne la camera du centre.
Definition: orbiter.cpp:33
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
int read_orbiter(const char *filename)
relit la position de l'orbiter depuis un fichier texte.
Definition: orbiter.cpp:86
Point position()
renvoie la position de la camera dans le repere du monde.
Definition: orbiter.cpp:78
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
Transform RotationX(const float a)
renvoie la matrice representation une rotation de angle degree autour de l'axe X. ...
Definition: mat.cpp:146
void lookat(const Point &center, const float size)
observe le point center a une distance size.
Definition: orbiter.cpp:7
Transform Translation(const Vector &v)
renvoie la matrice representant une translation par un vecteur.
Definition: mat.cpp:132
void translation(const float x, const float y)
deplace le centre / le point observe.
Definition: orbiter.cpp:27
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
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
void rotation(const float x, const float y)
change le point de vue / la direction d'observation.
Definition: orbiter.cpp:21
representation d'une transformation, une matrice 4x4, organisee par ligne / row major.
Definition: mat.h:20
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 frame(const float width, const float height, const float z, const float fov, Point &dO, Vector &dx, Vector &dy) const
Definition: orbiter.cpp:59
int write_orbiter(const char *filename)
enregistre la position de l'orbiter dans un fichier texte.
Definition: orbiter.cpp:117
Point center(const Point &a, const Point &b)
renvoie le milieu du segment ab.
Definition: vec.cpp:17
Transform RotationY(const float a)
renvoie la matrice representation une rotation de a degree autour de l'axe Y.
Definition: mat.cpp:158