Salut,
j'ai implementé ma propore classe camera utilisant opengl, mais je costate que le mouvement est un peu ... bizarre .
je sens que je ne suis pas tout a fait à l'aise quand je me deplace dans la scene, mais je ne sais pas pourquoi.
Voici camera.h:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#ifndef CAMERA_H_INCLUDED
#define CAMERA_H_INCLUDED
 
#include "Vector3D.h"
#include <math.h>
#include <SFML/Window.hpp>
#include <GL/gl.h>
#include <GL/glu.h>
 
 
class Camera {
public:
    Camera(const Vector3D& = Vector3D(0, 0, -100));
    void Translate(const double, const double);
 
    void OnEvent(const sf::Input& , const double dt);
    void Look();
 
protected:
    void VectorFromAngles();
 
    Vector3D m_position;//position in the world
    Vector3D m_target;
    //directions
    Vector3D m_forward;
    Vector3D m_left;
    Vector3D m_up;
 
    //the traget of the camera
    Vector3D m_traget;
 
    //rotation
    double m_theta;
    double m_phi;
    double m_speed;
 
    double m_pi;
 
    double m_oldX;
    double m_oldY;
};
et le camera.cpp:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include "Camera.h"
 
Camera::Camera(const Vector3D& position): m_position(position), m_up(0, 1, 0), m_forward(0, 0, 1), m_left(1, 0, 0) {
    m_speed = .1;
    m_oldX = -1;
    m_oldY = -1;
 
    m_pi = 3.14;
 
    m_theta = 0;
    m_phi = 0;
 
}
 
 
void Camera::Translate(const double x, const double z) {
    m_position += m_left * x;
    m_position += m_forward * z;
}
 
 
 
void Camera::VectorFromAngles() {
    double xTemp = cos(m_theta * M_PI / 180);
 
    m_forward.Y = sin(m_theta * M_PI / 180);
    m_forward.X = cos(m_phi * M_PI / 180) * xTemp;
    m_forward.Z = sin(m_phi * M_PI / 180) * xTemp;
 
 
    m_left = m_up.crossProduct(m_forward).normalize();
 
    m_target = m_position + m_forward;
}
 
 
void Camera::Look() {
    gluLookAt(
        m_position.X, m_position.Y, m_position.Z,
        m_target.X, m_target.Y, m_target.Z,
        m_up.X, m_up.Y, m_up.Z
    );
 
}
 
void Camera::OnEvent(const sf::Input& input, const double dt) {
    double z = 0, x = 0, y = 0;
    if ( input.IsKeyDown(sf::Key::Z) )
        z += m_speed * dt;
    if ( input.IsKeyDown(sf::Key::S) )
        z -= m_speed * dt;
    if ( input.IsKeyDown(sf::Key::Q) )
        x += m_speed * dt;
    if ( input.IsKeyDown(sf::Key::D) )
        x -= m_speed * dt;
 
    Translate(x, z);
 
 
    x = input.GetMouseX();
    y = input.GetMouseY();
    if (m_oldX < 0) m_oldX = x;
    if (m_oldY < 0) m_oldY = y;
 
    m_phi += (x - m_oldX) * dt * m_speed * 0.1;
    m_theta -= (y - m_oldY) * dt * m_speed *  0.1;
    VectorFromAngles();
    m_oldX = x;
    m_oldY = y;
 
 
 
}
PS: j'utilise SFML pour le fenetrage, meme si je doute que cela puiise vraiment aider!