begin process at 2012 02 12 19:27:24
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

OpenGL

 > OPENGL: CLASSE CAMERA QUI GÈRE LE GLULOOKAT

OPENGL: CLASSE CAMERA QUI GÈRE LE GLULOOKAT


 Information sur la source

Note :
Aucune note
Catégorie :OpenGL Classé sous :camera, 3d, opengl, glulookat, classe Niveau :Débutant Date de création :04/03/2007 Date de mise à jour :05/03/2007 17:07:04 Vu / téléchargé :8 267 / 609

Auteur : acx01b

Ecrire un message privé
Site perso
Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

 Description

Bonjour.

c'est une petite classe pour gérer la Camera qui se veut être suffisante pour tous les programmes opengl que j'aurais envie d'écrire

il manque principalement l'option Lock pour les rotations et les déplacement mais je pense que cette option s'implémente facilement au cas par cas

je me sers de la classe "vecteur 3d" NxVec3 du physx SDK d'ageia

Source

  • #define NxReal double
  • NxReal from[3] = {-10,0,0};
  • NxReal to[3] = {0,0,0};
  • NxReal up[3] = {0,1,0};
  • camera = new Camera(from,to,up);
  • camera->setModeLock(true,true,false);
  • camera->setMinMaxCurrentUpDown(-M_PI/6,M_PI/6,0);
  • camera->setMinMaxCurrentLeftRight(-M_PI/12,M_PI/12,0);
  • /************************************************************************/
  • switch ( key ) {
  • case 'q':
  • case 27: // la touche Echappement
  • exit( 0 );
  • break;
  • case 'o':
  • camera->rotateUpLockSrc();
  • break;
  • case 'l':
  • camera->rotateDownLockSrc();
  • break;
  • case 'i':
  • camera->rotateUpLockDst();
  • break;
  • case 'k':
  • camera->rotateDownLockDst();
  • break;
  • case 'u':
  • camera->rotateSideLeft();
  • break;
  • case 'j':
  • camera->rotateSideRight();
  • break;
  • case 'r':
  • camera->moveFront(0.5);
  • break;
  • case 'f':
  • camera->moveBack(0.5);
  • break;
  • case 'd':
  • camera->moveLeft(0.5);
  • break;
  • case 'g':
  • camera->moveRight(0.5);
  • break;
  • case 'w':
  • camera->rotateLeftLockSrc();
  • break;
  • case 'x':
  • camera->rotateRightLockSrc();
  • break;
  • case 'h':
  • camera->rotateUpLockSrcKeepUpAndFront();
  • break;
  • case 'n':
  • camera->rotateDownLockSrcKeepUpAndFront();
  • break;
  • case '+':
  • camera->setDistanceFromToLockTo(camera->distanceFromTo/1.3);
  • break;
  • case '-':
  • camera->setDistanceFromToLockTo(camera->distanceFromTo*1.3);
  • break;
  • }
  • /************************************************************************/
  • class Camera {
  • public:
  • Camera(NxReal *_from, NxReal *_to, NxReal *_up);
  • void lookAt();
  • void moveFront();
  • void moveBack();
  • void moveUp();
  • void moveDown();
  • void moveLeft();
  • void moveRight();
  • void rotateUpLockSrc();
  • void rotateDownLockSrc();
  • void rotateUpLockSrcKeepUpAndFront();
  • void rotateDownLockSrcKeepUpAndFront();
  • void rotateLeftLockSrc();
  • void rotateRightLockSrc();
  • void rotateUpLockDst();
  • void rotateDownLockDst();
  • void rotateLeftLockDst();
  • void rotateRightLockDst();
  • void rotateSideLeft();
  • void rotateSideRight();
  • void move(NxReal *_direction);
  • void moveFront(NxReal _distance);
  • void moveBack(NxReal _distance);
  • void moveUp(NxReal _distance);
  • void moveDown(NxReal _distance);
  • void moveLeft(NxReal _distance);
  • void moveRight(NxReal _distance);
  • void rotateUpLockSrc(NxReal _angle);
  • void rotateDownLockSrc(NxReal _angle);
  • void rotateUpLockSrcKeepUpAndFront(NxReal _angle);
  • void rotateDownLockSrcKeepUpAndFront(NxReal _angle);
  • void rotateLeftLockSrc(NxReal _angle);
  • void rotateRightLockSrc(NxReal _angle);
  • void rotateUpLockDst(NxReal _angle);
  • void rotateDownLockDst(NxReal _angle);
  • void rotateLeftLockDst(NxReal _angle);
  • void rotateRightLockDst(NxReal _angle);
  • void rotateSideLeft(NxReal _angle);
  • void rotateSideRight(NxReal _angle);
  • void setDistanceStep(NxReal _magnitudeStepUp, NxReal _magnitudeStepFront, NxReal _magnitudeStepLeft);
  • void setAllAngleStep(NxReal _angle);
  • void setAngleStep(NxReal _stepAngleUpDown, NxReal _stepAngleLeftRight, NxReal _stepAngleSide);
  • void setFrom(NxReal *_from);
  • void setTo(NxReal *_to);
  • void setUp(NxReal *_up);
  • void setFromToUp(NxReal *_from, NxReal *_to, NxReal *_up);
  • void setDistanceFromToLockFrom(NxReal _distanceFromTo);
  • void setDistanceFromToLockTo(NxReal _distanceFromTo);
  • void setModeLock(bool _modeLockUpDown, bool _modeLockLeftRight, bool _modeLockSide);
  • void setMinMaxCurrentUpDown(NxReal _minUpDown, NxReal _maxUpDown, NxReal _currentUpDown);
  • void setMinMaxCurrentLeftRight(NxReal _minLeftRight, NxReal _maxLeftRight, NxReal _currentLeftRight);
  • void setMinMaxCurrentSide(NxReal _minSide, NxReal _maxSide, NxReal _currentSide);
  • void setAllMinMaxCurrent(NxReal _angle);
  • void calculateStepVectors();
  • NxVec3 // points:
  • from,
  • to,
  • // normalized vectors:
  • up,
  • left,
  • front,
  • // balanced vectors:
  • stepUp,
  • stepLeft,
  • stepFront;
  • NxReal sinUpDown,
  • cosUpDown,
  • stepAngleUpDown,
  • sinLeftRight,
  • cosLeftRight,
  • stepAngleLeftRight,
  • sinSide,
  • cosSide,
  • stepAngleSide;
  • // distance and vector from - to :
  • NxReal distanceFromTo;
  • NxVec3 fromTo;
  • bool modeLockUpDown,
  • modeLockLeftRight,
  • modeLockSide;
  • NxReal magnitudeStepUp,
  • magnitudeStepFront,
  • magnitudeStepLeft,
  • maxUpDown,
  • minUpDown,
  • currentUpDown,
  • maxLeftRight,
  • minLeftRight,
  • currentLeftRight,
  • maxSide,
  • minSide,
  • currentSide;
  • };
  • /************************************************************************/
  • class NxVec3
  • {
  • public:
  • NxVec3 ();
  • NxVec3 (NxReal a);
  • NxVec3 (NxReal nx, NxReal ny, NxReal nz);
  • NxVec3 (const NxReal v[]);
  • NxVec3 (const NxReal v[], bool normalized);
  • NxVec3 (const NxVec3& v);
  • NxVec3 (const NxVec3& v, bool normalized);
  • const NxVec3& operator=(const NxVec3&);
  • const NxReal *get() const;
  • NxReal* get();
  • bool operator< (const NxVec3&) const;
  • bool operator==(const NxVec3&) const;
  • bool operator!=(const NxVec3&) const;
  • void set(const NxVec3 &);
  • void setx(const NxReal & d);
  • void sety(const NxReal & d);
  • void setz(const NxReal & d);
  • void setNegative(const NxVec3 &a);
  • void setNegative();
  • void set(NxReal, NxReal, NxReal);
  • void set(NxReal);
  • void zero();
  • void setPlusInfinity();
  • void setMinusInfinity();
  • void add(const NxVec3 & a, const NxVec3 & b);
  • void subtract(const NxVec3 &a, const NxVec3 &b);
  • void multiply(NxReal s, const NxVec3 & a);
  • void arrayMultiply(const NxVec3 &a, const NxVec3 &b);
  • void multiplyAdd(NxReal s, const NxVec3 & a, const NxVec3 & b);
  • NxReal normalize();
  • void setMagnitude(NxReal);
  • bool isFinite() const;
  • NxReal dot(const NxVec3 &other) const;
  • bool sameDirection(const NxVec3 &) const;
  • NxReal magnitude() const;
  • NxReal magnitudeSquared() const;
  • NxReal distance(const NxVec3 &) const;
  • NxReal distanceSquared(const NxVec3 &v) const;
  • void cross(const NxVec3 &left, const NxVec3 & right);
  • void cross(const NxVec3 &left, const NxVec3 & right, bool normalized);
  • bool equals(const NxVec3 &, NxReal epsilon) const;
  • void rotate (NxReal angle, const NxVec3 &axe);
  • void rotate (NxReal sin_angle, NxReal cos_angle, const NxVec3 &axe);
  • void rotate (const NxVec3 &axe);
  • NxVec3 operator -() const;
  • NxVec3 operator +(const NxVec3 & v) const;
  • NxVec3 operator -(const NxVec3 & v) const;
  • NxVec3 operator *(NxReal f) const;
  • NxVec3 operator /(NxReal f) const;
  • NxVec3 &operator +=(const NxVec3& v);
  • NxVec3 &operator -=(const NxVec3& v);
  • NxVec3 &operator *=(NxReal f);
  • NxVec3 &operator /=(NxReal f);
  • NxVec3 cross(const NxVec3& v) const;
  • NxVec3 operator^(const NxVec3& v) const;
  • NxReal operator|(const NxVec3& v) const;
  • NxReal x,y,z;
  • };
#define NxReal double

NxReal from[3] = {-10,0,0};
NxReal to[3] = {0,0,0};
NxReal up[3] = {0,1,0};
camera = new Camera(from,to,up);
camera->setModeLock(true,true,false);
camera->setMinMaxCurrentUpDown(-M_PI/6,M_PI/6,0);
camera->setMinMaxCurrentLeftRight(-M_PI/12,M_PI/12,0);

/************************************************************************/
switch ( key ) {
    case 'q':
    case 27:     // la touche Echappement
      exit( 0 );
      break;
     case 'o':
      camera->rotateUpLockSrc();
      break;
     case 'l':
      camera->rotateDownLockSrc();
      break;
     case 'i':
      camera->rotateUpLockDst();
      break;
     case 'k':
      camera->rotateDownLockDst();
      break;
     case 'u':
      camera->rotateSideLeft();
      break;
     case 'j':
      camera->rotateSideRight();
      break;
     case 'r':
      camera->moveFront(0.5);
      break;
     case 'f':
      camera->moveBack(0.5);
      break;
     case 'd':
      camera->moveLeft(0.5);
      break;
     case 'g':
      camera->moveRight(0.5);
      break;
     case 'w':
      camera->rotateLeftLockSrc();
      break;
     case 'x':
      camera->rotateRightLockSrc();
      break;
     case 'h':
      camera->rotateUpLockSrcKeepUpAndFront();
      break;
     case 'n':
      camera->rotateDownLockSrcKeepUpAndFront();
      break;
     case '+':
      camera->setDistanceFromToLockTo(camera->distanceFromTo/1.3);
      break;
     case '-':
      camera->setDistanceFromToLockTo(camera->distanceFromTo*1.3);
      break;
}
/************************************************************************/
class Camera {
   public:
    Camera(NxReal *_from, NxReal *_to, NxReal *_up);
    void lookAt();
    void moveFront();
    void moveBack();
    void moveUp();
    void moveDown();
    void moveLeft();
    void moveRight();
    void rotateUpLockSrc();
    void rotateDownLockSrc();
    void rotateUpLockSrcKeepUpAndFront();
    void rotateDownLockSrcKeepUpAndFront();
    void rotateLeftLockSrc();
    void rotateRightLockSrc();
    void rotateUpLockDst();
    void rotateDownLockDst();
    void rotateLeftLockDst();
    void rotateRightLockDst();
    void rotateSideLeft();
    void rotateSideRight();
    void move(NxReal *_direction);
    void moveFront(NxReal _distance);
    void moveBack(NxReal _distance);
    void moveUp(NxReal _distance);
    void moveDown(NxReal _distance);
    void moveLeft(NxReal _distance);
    void moveRight(NxReal _distance);
    void rotateUpLockSrc(NxReal _angle);
    void rotateDownLockSrc(NxReal _angle);
    void rotateUpLockSrcKeepUpAndFront(NxReal _angle);
    void rotateDownLockSrcKeepUpAndFront(NxReal _angle);
    void rotateLeftLockSrc(NxReal _angle);
    void rotateRightLockSrc(NxReal _angle);
    void rotateUpLockDst(NxReal _angle);
    void rotateDownLockDst(NxReal _angle);
    void rotateLeftLockDst(NxReal _angle);
    void rotateRightLockDst(NxReal _angle);
    void rotateSideLeft(NxReal _angle);
    void rotateSideRight(NxReal _angle);
    void setDistanceStep(NxReal _magnitudeStepUp, NxReal _magnitudeStepFront, NxReal _magnitudeStepLeft);
    void setAllAngleStep(NxReal _angle);
    void setAngleStep(NxReal _stepAngleUpDown, NxReal _stepAngleLeftRight, NxReal _stepAngleSide);
    void setFrom(NxReal *_from);
    void setTo(NxReal *_to);
    void setUp(NxReal *_up);
    void setFromToUp(NxReal *_from, NxReal *_to, NxReal *_up);
    void setDistanceFromToLockFrom(NxReal _distanceFromTo);
    void setDistanceFromToLockTo(NxReal _distanceFromTo);
    void setModeLock(bool _modeLockUpDown, bool _modeLockLeftRight, bool _modeLockSide);
    void setMinMaxCurrentUpDown(NxReal _minUpDown, NxReal _maxUpDown, NxReal _currentUpDown);
    void setMinMaxCurrentLeftRight(NxReal _minLeftRight, NxReal _maxLeftRight, NxReal _currentLeftRight);
    void setMinMaxCurrentSide(NxReal _minSide, NxReal _maxSide, NxReal _currentSide);
    void setAllMinMaxCurrent(NxReal _angle);
    void calculateStepVectors();
    NxVec3  // points:
            from,
            to,
            // normalized vectors:
            up,
            left,
            front,
            // balanced vectors:
            stepUp,
            stepLeft,
            stepFront;

    NxReal  sinUpDown,
            cosUpDown,
            stepAngleUpDown,
            sinLeftRight,
            cosLeftRight,
            stepAngleLeftRight,
            sinSide,
            cosSide,
            stepAngleSide;
            // distance and vector from - to :
    NxReal  distanceFromTo;
    NxVec3  fromTo;

    bool    modeLockUpDown,
            modeLockLeftRight,
            modeLockSide;

    NxReal  magnitudeStepUp,
            magnitudeStepFront,
            magnitudeStepLeft,
            maxUpDown,
            minUpDown,
            currentUpDown,
            maxLeftRight,
            minLeftRight,
            currentLeftRight,
            maxSide,
            minSide,
            currentSide;
};
/************************************************************************/
class NxVec3
{
	public:
	NxVec3  ();
	NxVec3  (NxReal a);
	NxVec3  (NxReal nx, NxReal ny, NxReal nz);
	NxVec3  (const NxReal v[]);
	NxVec3  (const NxReal v[], bool normalized);
	NxVec3  (const NxVec3& v);
	NxVec3  (const NxVec3& v, bool normalized);
	const   NxVec3& operator=(const NxVec3&);
	const   NxReal *get() const;
	NxReal* get();
	bool    operator< (const NxVec3&) const;
	bool    operator==(const NxVec3&) const;
	bool    operator!=(const NxVec3&) const;
	void    set(const NxVec3 &);
	void    setx(const NxReal & d);
	void    sety(const NxReal & d);
	void    setz(const NxReal & d);
	void    setNegative(const NxVec3 &a);
	void    setNegative();
	void    set(NxReal, NxReal, NxReal);
	void    set(NxReal);
	void    zero();
	void    setPlusInfinity();
	void    setMinusInfinity();
        void    add(const NxVec3 & a, const NxVec3 & b);
	void    subtract(const NxVec3 &a, const NxVec3 &b);
	void    multiply(NxReal s,  const NxVec3 & a);
	void    arrayMultiply(const NxVec3 &a, const NxVec3 &b);
	void    multiplyAdd(NxReal s, const NxVec3 & a, const NxVec3 & b);
	NxReal  normalize();
	void	setMagnitude(NxReal);
	bool    isFinite() const;
	NxReal  dot(const NxVec3 &other) const;
	bool    sameDirection(const NxVec3 &) const;
	NxReal  magnitude() const;
	NxReal  magnitudeSquared() const;
	NxReal  distance(const NxVec3 &) const;
	NxReal  distanceSquared(const NxVec3 &v) const;
	void    cross(const NxVec3 &left, const NxVec3 & right);
	void    cross(const NxVec3 &left, const NxVec3 & right, bool normalized);
	bool    equals(const NxVec3 &, NxReal epsilon) const;
	void    rotate (NxReal angle, const NxVec3 &axe);
	void    rotate (NxReal sin_angle, NxReal cos_angle, const NxVec3 &axe);
	void    rotate (const NxVec3 &axe);
	NxVec3  operator -() const;
	NxVec3  operator +(const NxVec3 & v) const;
	NxVec3  operator -(const NxVec3 & v) const;
	NxVec3  operator *(NxReal f) const;
	NxVec3  operator /(NxReal f) const;
	NxVec3  &operator +=(const NxVec3& v);
        NxVec3  &operator -=(const NxVec3& v);
	NxVec3  &operator *=(NxReal f);
	NxVec3  &operator /=(NxReal f);
	NxVec3  cross(const NxVec3& v) const;
	NxVec3  operator^(const NxVec3& v) const;
	NxReal  operator|(const NxVec3& v) const;
	NxReal  x,y,z;
};

 Conclusion

si quelqu'un a des commentaires sur pourquoi il faut tout refaire, ou encore mieux des idées pour l'améliorer... :)

merci

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Historique

04 mars 2007 20:04:34 :
pas de mise à jour
05 mars 2007 17:07:05 :
ajout/modification de méthodes: ajout du mode lock pour les rotations, et de la rotation haut bas qui garde l'horizontale et la verticale (présente dans beaucoup de jeux)

 Sources du même auteur

Source avec une capture OPENGL - JEU DE BILLARD EN 3D PAS FINI
Source avec Zip (WIN32) PARSER DU LANGAGE C AVEC YACC
Source avec Zip Source avec une capture BIEN EXPLIQUE: EXEMPLE DE RESEAU DE NEURONE
Source avec Zip Source avec une capture SCREENSAVER HYPNOTISEUR
Source avec Zip LABYRINTHE EN 3D

 Sources de la même categorie

Source avec Zip Source avec une capture AFFICHER DES COURBES DE BEZIER par shorzy
Source avec Zip Source avec une capture BASE/MOTEUR 3D EN QT/OPENGL (COMPLET ET FONCTIONNEL!) POUR U... par envi33
Source avec Zip Source avec une capture CLASSE AVEC OPENGL - OBJETS 3D ET ANIMATIONS par rasta63
Source avec Zip Source avec une capture LETTRES 3D AVEC OPENGL ET QT par opossum_farceur
Source avec Zip CUBE 3D GLUT32 VC++ ET DEVC++ par bobby03

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture BASE/MOTEUR 3D EN QT/OPENGL (COMPLET ET FONCTIONNEL!) POUR U... par envi33
Source avec Zip Source avec une capture LETTRES 3D AVEC OPENGL ET QT par opossum_farceur
Source avec Zip Source avec une capture MOTEUR 3D : CASTOR3D par dragonjoker59
Source avec Zip Source avec une capture OPENGL SOUS LINUX par xave4552
Source avec Zip Source avec une capture DEMO OPENGL par JeanCodeS

Commentaires et avis

Commentaire de mrbozzo le 04/04/2007 12:09:33

Rien à redire, ça me semble très bien, par contre j'ai une question...

Qu'est-ce qu'il se passe si le vecteur "Up" de la caméra a une valeur opposée à l'orientation de la caméra???

Par exemple,
si la caméra est placée aux coordonnées P : {0, 0, 0}
la cible se trouve en C : {0, -1, 0}
et le vecteur "Up" a les coordonnées : {0, 1, 0}

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Moteur 3d OpenGL [ par Stormy ] Salut. Je recherche des personnes motives pour travailler sur un moteur 3d sous OpenGL. Les bases sont deja etablies (Detection de collisions, sons, m Objects 3D Opengl [ par mastave ] Comment inclure dans un fenêtre OpenGl un fichier 3D DXF ou3DS??? Camera Opengl [ par sniperfou ] Salut a tous,j'ai un probleme avec opengl car je voudrais faire tourner ma camera autour d'un cube mais je ne connait pas les fonctions qui permettent OpenGl et objets 3D [ par mshn ] bonjour, est ce qu'on peux céer des objets 3D sous OpenGl, et les manipuler (animer et déplacer) par programme en utilisant un environnement de C++ ? opengl bug [ par darmoor ] Voila je vien de debuter dans l'openGL et selon le tutorial jai fait ce qu'il falai et sa me marque une erreur:le code:#include &lt;windows.h&gt;#incl Charger un model 3D en C++ avec opengl [ par smoove5198 ] Bonjour,j'essaye depuis un moment de charger un model 3D avec opengl en C++, pour ca j'utilise le seul format de fichier que je connaisse et dont j'ai Projet OpenGl [ par barraq ] salut,Voila j'ai un projet a faire pour ma fac, je doit réaliser tout un environement 3d en opengl .Je doit pouvoir me deplacer dans une map donnée et 3d opengl [ par darvil ] Qui peu m'aider?je cherche de quoi developer sur mac en 3d . Je cherche une adreese ou tlecharger un compilateur des logiciels 3d. comment fair march Problème avec OpenGL [ par Xaviou ] Bonjour, Je suis en train de r&#233;aliser un prog permettant de visualiser des fichiers 3D au format STL. Si j'&#233;x&#233;cute mon prog depuis Visu


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 3,432 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales