Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Lucas

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Simple camera class in FTS - lookAt function.

// Camera class for FTS

   1  
   2  /// Makes the camera look at a certain point.
   3  /** This function makes the camera look at a certain point. If that point is the same as its
   4   *  current position, the camera moves back the (current) distance |camera-target|.
   5   *
   6   * \param in_vTgt The position where the camera should look at.
   7   *
   8   * \return If successfull: ERR_OK
   9   * \return If failed:      An error code < 0
  10   *
  11   * \author Pompei2
  12   */
  13  CFTSCamera *CFTSCamera::lookAt(const CFTSVector & in_vTgt)
  14  {
  15      // Calculate the new front vector that points to the target.
  16      CFTSVector vNewFront = (in_vTgt - m_vPos).normalize();
  17  
  18      // The two vectors are in one plane. This gives us the cosine of the angle between
  19      // these two vectors.
  20      float fDotProd = vNewFront.dot(m_vFront);
  21      if(fDotProd > 1.0f || fDotProd < -1.0f)
  22          return this;
  23  
  24      // By getting the arc cosine of this, we get the angle in radians between the two
  25      // vectors. But we only get an angle between 0 and pi, that will be a problem.
  26      float fAlpha = acosf(fDotProd);
  27  
  28      // Here we get the normal to these two vectors, we will need to rotate our front
  29      // vector around this normal.
  30      CFTSVector vNormal = vNewFront.cross(m_vFront).normalize();
  31  
  32      // As I said, we have a problem: as we only get an angle between 0 and pi,
  33      // we don't know if we need to rotate clockwise or counterclockwise ...
  34      // We solve this problem by just trying it out.
  35      CFTSVector vFrontTest = m_vFront.rotate(vNormal, fAlpha);
  36  
  37      // If the dot product is near to one, it means we were right. with our guess
  38      // rotating counterclockwise.
  39      // Else, we must rotate clockwise.
  40      fDotProd = vFrontTest.dot(vNewFront);
  41      if(fabs(fDotProd) > (1.0f - D_FTS_DELTA)) {
  42              m_vFront = vFrontTest;
  43              m_vUp = m_vUp.rotate(vNormal, fAlpha);
  44              m_vRight = m_vRight.rotate(vNormal, fAlpha);
  45      } else {
  46              m_vFront = m_vFront.rotate(vNormal, -fAlpha);
  47              m_vUp = m_vUp.rotate(vNormal, -fAlpha);
  48              m_vRight = m_vRight.rotate(vNormal, -fAlpha);
  49      }
  50  
  51      // If the dot product is near to -1, it means we look the wrong way.
  52      // To correct this, we just rotate pi radians (a half circle).
  53      if(fDotProd < -(1.0f - D_FTS_DELTA))
  54          this->rotateYRadians(M_PI);
  55  
  56      return this;
  57  }

Simple camera class in FTS

// Simple camera class in FTS

   1  
   2  /**
   3   * \file camera.cpp
   4   * \author Pompei2
   5   * \date 15 July 2006
   6   * \brief This file contains the camera class implementation.
   7   **/
   8  
   9  #include "3d/camera.h"
  10  
  11  #include <GL/gl.h>
  12  
  13  #ifdef DEBUG
  14  #  define new new(__FILE__,__LINE__)
  15  #endif
  16  
  17  /// Constructor
  18  CFTSCamera::CFTSCamera( void )
  19  {
  20      m_vRight = CFTSVector(1.0f,0.0f,0.0f);
  21      m_vUp    = CFTSVector(0.0f,1.0f,0.0f);
  22      m_vFront = CFTSVector(0.0f,0.0f,-1.0f);
  23  }
  24  
  25  /// Destructor
  26  CFTSCamera::~CFTSCamera( void )
  27  {
  28  }
  29  
  30  /// Places the camera at a certain point.
  31  /** This function places the camera at a certain point in space.
  32   *
  33   * \param in_vPos The new position of the camera.
  34   *
  35   * \return If successfull: ERR_OK
  36   * \return If failed:      An error code < 0
  37   *
  38   * \author Pompei2
  39   */
  40  CFTSCamera *CFTSCamera::pos(const CFTSVector & in_vPos)
  41  {
  42      m_vPos = in_vPos;
  43      return this;
  44  }
  45  
  46  /// Moves the camera to the right.
  47  /** This moves the camera to the right. The right is relative to the
  48   *  camera's orientation ! To move to the left, put a negative value here.
  49   *
  50   * \param in_fAmount The amount of units to move to the right.
  51   *
  52   * \author Pompei2
  53   */
  54  CFTSCamera *CFTSCamera::moveRight(float in_fAmount)
  55  {
  56      // We have a vector pointing to the right,
  57      // so moving to the right is an easy thing:
  58      // Just add a multiple of this vector to the current position.
  59      m_vPos = m_vPos + m_vRight.scalar(in_fAmount);
  60  
  61      return this;
  62  }
  63  
  64  /// Moves the camera to the front.
  65  /** This moves the camera to the front. The right is relative to the
  66   *  camera's orientation and to be exact, it is where the camera looks at.
  67   *  Moving to the front is a bit like zooming in.
  68   *  To move back (like zoom out), put a negative value here.
  69   *
  70   * \param in_fAmount The amount of units to move to the front.
  71   *
  72   * \author Pompei2
  73   */
  74  CFTSCamera *CFTSCamera::moveFront(float in_fAmount)
  75  {
  76      // Same as for moveRight.
  77      m_vPos = m_vPos + m_vFront.scalar(in_fAmount);
  78  
  79      return this;
  80  }
  81  
  82  /// Moves the camera upwards.
  83  /** This moves the camera upwards. Upwards is the direction that is perpendicular
  84   *  To your right and to your front, and shows up :) It is like the top of your head.
  85   *  To move downwards (to your feets), put a negative value here.
  86   *
  87   * \param in_fAmount The amount of units to move up.
  88   *
  89   * \author Pompei2
  90   */
  91  CFTSCamera *CFTSCamera::moveUp(float in_fAmount)
  92  {
  93      // Same as for moveRight.
  94      m_vPos = m_vPos + m_vUp.scalar(in_fAmount);
  95  
  96      return this;
  97  }
  98  
  99  /// Rotates the camera around ITS x axis.
 100  /** This rotates the camera around THE CAMERA's x axis.
 101   *  This basically means it looks up or down. As seen from the right,
 102   *  a positive value rotates counterclockwise (look up) and a negative
 103   *  value rotates clockwise (look down).
 104   *
 105   * \param in_fAmount The amount of degrees to rotate.
 106   *
 107   * \author Pompei2
 108   */
 109  CFTSCamera *CFTSCamera::rotateX(float in_fAmount)
 110  {
 111      Lib3dsMatrix mRotation;
 112      Lib3dsQuat qRotation;
 113  
 114      // First, clean the matrix.
 115      lib3ds_matrix_identity(mRotation);
 116  
 117      // Create the quaternion to rotate, we need the angle in radians.
 118      lib3ds_quat_axis_angle(qRotation, m_vRight.lib3ds(), -RADIANS(in_fAmount));
 119  
 120      // Apply the rotation to the identity matrix, creating a transformation matrix.
 121      lib3ds_matrix_rotate(mRotation, qRotation);
 122  
 123      // Rotate our front and up vectors, this makes the camera rotate.
 124      m_vFront = m_vFront.transform(mRotation).normalize();
 125      m_vUp = m_vUp.transform(mRotation).normalize();
 126  
 127      return this;
 128  }
 129  
 130  /// Rotates the camera around ITS y axis.
 131  /** This rotates the camera around THE CAMERA's y axis.
 132   *  This basically means it looks left or right. As seen from the top,
 133   *  a positive value rotates counterclockwise (look left) and a negative
 134   *  value rotates clockwise (look right).
 135   *
 136   * \param in_fAmount The amount of degrees to rotate.
 137   *
 138   * \author Pompei2
 139   */
 140  CFTSCamera *CFTSCamera::rotateY(float in_fAmount)
 141  {
 142      Lib3dsMatrix mRotation;
 143      Lib3dsQuat qRotation;
 144  
 145      // First, clean the matrix.
 146      lib3ds_matrix_identity(mRotation);
 147  
 148      // Create the quaternion to rotate, we need the angle in radians.
 149      lib3ds_quat_axis_angle(qRotation, m_vUp.lib3ds(), -RADIANS(in_fAmount));
 150  
 151      // Apply the rotation to the identity matrix, creating a transformation matrix.
 152      lib3ds_matrix_rotate(mRotation, qRotation);
 153  
 154      // Rotate our front and up vectors, this makes the camera rotate.
 155      m_vFront = m_vFront.transform(mRotation).normalize();
 156      m_vRight = m_vRight.transform(mRotation).normalize();
 157  
 158      return this;
 159  }
 160  
 161  /// Rotates the camera around ITS z axis.
 162  /** This rotates the camera around THE CAMERA's z axis.
 163   *  This is the same effect as if you rotate your head to bring an ear
 164   *  close to the shoulder. A positive value rotates counterclockwise (left shoulder)
 165   *  and a negative value rotates clockwise (right shoulder).
 166   *
 167   * \param in_fAmount The amount of degrees to rotate.
 168   *
 169   * \author Pompei2
 170   */
 171  CFTSCamera *CFTSCamera::rotateZ(float in_fAmount)
 172  {
 173      Lib3dsMatrix mRotation;
 174      Lib3dsQuat qRotation;
 175  
 176      // First, clean the matrix.
 177      lib3ds_matrix_identity(mRotation);
 178  
 179      // Create the quaternion to rotate, we need the angle in radians.
 180      lib3ds_quat_axis_angle(qRotation, m_vFront.lib3ds(), -RADIANS(in_fAmount));
 181  
 182      // Apply the rotation to the identity matrix, creating a transformation matrix.
 183      lib3ds_matrix_rotate(mRotation, qRotation);
 184  
 185      // Rotate our front and up vectors, this makes the camera rotate.
 186      m_vUp = m_vUp.transform(mRotation).normalize();
 187      m_vRight = m_vRight.transform(mRotation).normalize();
 188  
 189      return this;
 190  }
 191  
 192  /// Renders the camera.
 193  /** This function applies all transformations to the current matrix
 194   *  that are needed to place the camera.
 195   *
 196   * \author Pompei2
 197   */
 198  CFTSCamera *CFTSCamera::render(void)
 199  {
 200      // gluLookAt wants a point in space to look at, not just a direction.
 201      // Calculate this point.
 202      CFTSVector vLookAt = m_vPos + m_vFront;
 203  
 204      // Let gluLookAt do all the matrix calculation stuff.
 205      gluLookAt(m_vPos.x(), m_vPos.y(), m_vPos.z(),
 206                vLookAt.x(), vLookAt.y(), vLookAt.z(),
 207                m_vUp.x(), m_vUp.y(), m_vUp.z());
 208  
 209      return this;
 210  }
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS