-
Notifications
You must be signed in to change notification settings - Fork 1
/
Camera.cpp
138 lines (122 loc) · 4.21 KB
/
Camera.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "Camera.h"
#include "Matrix.h"
#include <math.h>
CCamera::CCamera(void)
{
}
CCamera::~CCamera(void)
{
}
CCamera::CCamera(GLfloat center_x, GLfloat center_y,GLfloat center_z,GLfloat haut_x,GLfloat haut_y,GLfloat haut_z,double loin)
: m_dRoll(0), m_dPitch(0), m_dHeading(0), m_dPhi(0),m_dTheta(0)
{
m_dFar=loin;
m_vPos_Cam.setCoordonnees(0,0,0);
m_vCenter_Cam.setCoordonnees(center_x,center_y,center_z);
m_vHaut_Cam.setCoordonnees(haut_x,haut_y,haut_z);
calculposition(0,0,0,0);
}
CCamera::CCamera(CObjet * Center_obj ,GLfloat haut_x,GLfloat haut_y,GLfloat haut_z,double loin)
: m_dRoll(0), m_dPitch(0), m_dHeading(0), m_dPhi(0), m_dTheta(0)
{
m_dFar=loin;
m_nCenterObj= Center_obj;
calculcentre();
m_vPos_Cam.setCoordonnees(0,0,0);
m_vHaut_Cam.setCoordonnees(haut_x,haut_y,haut_z);
calculposition(0,0,0,0);
}
void CCamera::setCenter(CVector3 center)
{
m_vCenter_Cam.setCoordonnees(center.x,center.y,center.z);
}
void CCamera::setHaut(CVector3 haut)
{
m_vHaut_Cam.setCoordonnees(haut.x,haut.y,haut.z);
}
void CCamera::setPosition(CVector3 pos)
{
m_vPos_Cam.setCoordonnees(pos.x,pos.y,pos.z);
}
void CCamera::calculposition(int x, int y,GLdouble w,GLdouble h)
{
// recuperation des changement en x et y
m_dTheta=(x-w)/100;
m_dPhi=(y-h)/100;
// Calcul de R
GLdouble X2=(m_dFar-m_vPos_Cam.x)*(m_dFar-m_vPos_Cam.x);
GLdouble Y2=(m_dFar-m_vPos_Cam.y)*(m_dFar-m_vPos_Cam.y);
GLdouble Z2=(m_dFar-m_vPos_Cam.z)*(m_dFar-m_vPos_Cam.z);
m_dR=sqrt(X2+Y2);
}
void CCamera::calculcentre()
{
//Recuperation du centre de l'objet Newton pointé
if(m_nCenterObj!=NULL){
CMatrix LockTmp;
NewtonBodyGetMatrix(m_nCenterObj->GetBody(),&LockTmp.m_Mat[0][0]);
m_vCenter_Cam = LockTmp.getPosition();
}
}
void CCamera::Dessiner()
{
calculcentre();
if (m_dPhi > 1.5)
m_dPhi=1.5;
if (m_dPhi < -1.0)
m_dPhi=-1.0;
// Calcul des coordonnées polaires
m_dPitch =m_dR*cos(m_dPhi)*cos(m_dTheta);
m_dRoll =m_dR*cos(m_dPhi)*sin(m_dTheta);
m_dHeading =m_dR*sin(m_dPhi);
//Empecher le retour Camera
if(m_dHeading <0)
m_dHeading = 0;
//Mise a jour de la matrice Newton de la camera
CMatrix matrice_Collision_Cam;
NewtonBodyGetMatrix(GetBody(), &matrice_Collision_Cam.m_Mat[0][0]);
matrice_Collision_Cam.m_Mat[3][0] = m_vCenter_Cam.x+m_dPitch;
matrice_Collision_Cam.m_Mat[3][1] = m_vCenter_Cam.y+m_dHeading;
matrice_Collision_Cam.m_Mat[3][2] = m_vCenter_Cam.z+m_dRoll;
NewtonBodySetMatrix(GetBody(), &matrice_Collision_Cam.m_Mat[0][0]);
//Placer la camera
gluLookAt(matrice_Collision_Cam.getPosition().x,matrice_Collision_Cam.getPosition().y,matrice_Collision_Cam.getPosition().z,m_vCenter_Cam.x,m_vCenter_Cam.y+1.5,m_vCenter_Cam.z, m_vHaut_Cam.x,m_vHaut_Cam.y,m_vHaut_Cam.z);
}
void CCamera::zoomer(GLfloat zoom)
{
m_dFar-=zoom;
if(m_dFar<2)
m_dFar=2;
Dessiner();
}
void CCamera::setTheta(GLfloat angle)
{
m_dTheta = angle;
}
void CCamera::Initialiser(NewtonWorld *nWorld,CVector3 taille)
{
// On initialise le vecteur de dimensions
m_vTailleCollisionCam.x = taille.x;
m_vTailleCollisionCam.y = taille.y;
m_vTailleCollisionCam.z = taille.z;
CMatrix matrice; // On créé une matrice
matrice.setIdentite();
// On définit la matrice de manière à ce que l'objet soit placé aux positions
// spécifiées en utilisant la dernière colonne de la matrice
matrice.m_Mat[3][0] = 0;
matrice.m_Mat [3][1] = 0;
matrice.m_Mat [3][2] = 0;
// On initialise la boîte de collision
NewtonCollision * collision = NULL;
// On créé la boite de collision aux dimensions de l'objet
collision = NewtonCreateBox (nWorld, m_vTailleCollisionCam.x, m_vTailleCollisionCam.y, m_vTailleCollisionCam.z, NULL);
// On initialise le corps avec la boite de collision
m_pBody = NewtonCreateBody (nWorld, collision);
if (m_pBody == NULL)
std::cerr << "Impossible d'initialiser le corps.";
// On détruit la boite de collision, on n'en a plus besoin
NewtonReleaseCollision (nWorld, collision);
// Enfin, on affecte notre matrice (qui représente donc sa position dans l'espace)
// à notre corps grâce à la fonction NewtonBodySetMatrix
NewtonBodySetMatrix (m_pBody, &matrice.m_Mat [0][0]);
}