11.2.12 Le fichier SimpleCameraGl3.cpp en entier

Voilà le fichier SimpleCameraGl3.cpp complet :

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "simple_camera_gl3.h"
#include <iostream>

using namespace std;

SimpleCameraGl3::SimpleCameraGl3(){
	this->initialisation(VECT3F_NULL, 0.0, M_PI/2.0, M_PI/2.0);
}

SimpleCameraGl3::SimpleCameraGl3(const Vecteur3f& position, float theta, float phi, float rouli){
	this->initialisation(position, theta, phi, rouli);
}

SimpleCameraGl3::SimpleCameraGl3(const SimpleCameraGl3 & camera){
	this->copyCamera(camera);
}

SimpleCameraGl3::~SimpleCameraGl3(){}

void SimpleCameraGl3::see(Matrice4& matriceModelview){
	updateCameraGl3();
	matriceModelview.lookAt(p_position, p_visee, -p_planGauche, p_up);
}

void SimpleCameraGl3::setAll(const Vecteur3f& position, float theta, float phi, float rouli){
	this->initialisation(position, theta, phi, rouli);
}

void SimpleCameraGl3::setPosition(const Vecteur3f& position){p_position = position;}

void SimpleCameraGl3::setPosition(float x, float y, float z){
	p_position.setXYZ(x, y, z);
}

void SimpleCameraGl3::setAngles(float theta, float phi,float rouli){
	p_theta = theta;
	p_phi = phi;
	p_rouli = rouli;
}

void SimpleCameraGl3::setTheta(float theta){p_theta = theta;}

void SimpleCameraGl3::setPhi(float phi){p_phi = phi;}

void SimpleCameraGl3::setRouli(float rouli){p_rouli = rouli;}

void SimpleCameraGl3::setWindowID(SDL_WindowID id){p_windowId = id;}

void SimpleCameraGl3::move(const Vecteur3f& dm){p_position += dm;}

void SimpleCameraGl3::moveVisee(float dl){
	p_position += p_visee*dl;
}

void SimpleCameraGl3::moveGauche(float dl){
	p_position += p_planGauche*dl;
}

void SimpleCameraGl3::turnUp(float dPhi){p_phi -= dPhi;}

void SimpleCameraGl3::turnDown(float dPhi){p_phi += dPhi;}

void SimpleCameraGl3::turnRight(float dTheta){
	if(p_up.getz() > 0) p_theta -= dTheta;
	else p_theta += dTheta;
}

void SimpleCameraGl3::turnLeft(float dTheta){
	if(p_up.getz() > 0) p_theta += dTheta;
	else p_theta -= dTheta;
}

void SimpleCameraGl3::addTheta(float dTheta){p_theta += dTheta;}

void SimpleCameraGl3::addPhi(float dPhi){p_phi += dPhi;}

void SimpleCameraGl3::addRouli(float dRouli){p_rouli += dRouli;}

void SimpleCameraGl3::rotation(float dTheta, float dPhi,float dRouli){
	p_theta += dTheta;
	p_phi += dPhi;
	p_rouli += dRouli;
}

Vecteur3f SimpleCameraGl3::getPosition() const{return p_position;}

Vecteur3f SimpleCameraGl3::getVisee(){
	return p_visee;
}

Vecteur3f SimpleCameraGl3::getUp(){
	return p_up;
}

Vecteur3f SimpleCameraGl3::getPlanGauche(){
	return p_planGauche;
}

float SimpleCameraGl3::getTheta() const{return p_theta;}

float SimpleCameraGl3::getPhi() const{return p_phi;}

float SimpleCameraGl3::getRouli() const{return p_rouli;}

void SimpleCameraGl3::modeFPS(float mouseX, float mouseY, float midWidth, float midHeight, float coefSensibiliteX, float coefSensibiliteY, float phiMin, float phiMax){
	p_theta = ((float)(mouseX - midWidth))*coefSensibiliteX;
	p_phi = ((float)(mouseY - midHeight))*coefSensibiliteY;
	if(p_phi < phiMin){
		p_phi = phiMin;
		SDL_WarpMouseInWindow(p_windowId, midWidth + p_theta/coefSensibiliteX , midHeight + phiMin/coefSensibiliteY);
	}
	if(p_phi > phiMax){
		p_phi = phiMax;
		SDL_WarpMouseInWindow(p_windowId, midWidth + p_theta/coefSensibiliteX , midHeight + phiMax/coefSensibiliteY);
	}
	
	if(p_theta < -180){
		p_theta = 180 + (p_theta + 180);
		SDL_WarpMouseInWindow(p_windowId, midWidth + p_theta/coefSensibiliteX , midHeight + p_phi/coefSensibiliteY);
	}
	if(p_theta > 180){
		p_theta = -180 + (p_theta - 180);
		SDL_WarpMouseInWindow(p_windowId, midWidth + p_theta/coefSensibiliteX , midHeight + p_phi/coefSensibiliteY);
	}
// 	en fait on à fait les calculs à l'envers alors on change theta
	p_theta = -p_theta;
}

SimpleCameraGl3 & SimpleCameraGl3::operator = (const SimpleCameraGl3 & camera){
	this->copyCamera(camera);
	return *this;
}

void SimpleCameraGl3::copyCamera(const SimpleCameraGl3 & camera){
	p_position = camera.p_position;
	p_theta = camera.p_theta;
	p_phi = camera.p_phi;
	p_rouli = camera.p_rouli;
	p_up = camera.p_up;
	p_visee = camera.p_visee;
	p_planGauche = camera.p_planGauche;
	p_windowId = camera.p_windowId;
}

void SimpleCameraGl3::initialisation(const Vecteur3f& position, float theta, float phi, float rouli){
	p_position = position;
	p_theta = theta;
	p_phi = phi;
	p_rouli = rouli;
	p_windowId = NULL;
	updateCameraGl3();
}

void SimpleCameraGl3::updateCameraGl3(){
	p_visee.setXYZ(cos(p_theta)*sin(p_phi), sin(p_theta)*sin(p_phi), cos(p_phi)); //le Vecteur de visée est bon
	p_planGauche.setXYZ(-sin(p_theta)*sin(p_rouli), cos(p_theta)*sin(p_rouli), cos(p_rouli));//Vecteur unit bon (avec p_rouli = M_PI/2.0 par défaut
	p_up = (p_visee^p_planGauche).getUnit();
}

Voilà, c'est fini pour la caméra.