Back to index

supertuxkart  0.5+dfsg1
Public Types | Public Member Functions | Protected Attributes
Camera Class Reference

#include <camera.hpp>

Collaboration diagram for Camera:
Collaboration graph
[legend]

List of all members.

Public Types

enum  Mode {
  CM_NORMAL, CM_CLOSEUP, CM_DRIFTING, CM_LEADER_MODE,
  CM_REVERSE, CM_SIMPLE_REPLAY
}

Public Member Functions

 Camera (int camera_index, const Kart *kart)
void setMode (Mode mode_)
void setScreenPosition (int pos)
 Set the camera to the given mode.
void reset ()
 Reset is called when a new race starts.
void update (float dt)
void apply ()

Protected Attributes

ssgContext * m_context
sgCoord m_current_pos
const Kartm_kart
Mode m_mode
float m_x
float m_y
float m_w
float m_h
float m_current_speed
float m_last_pitch
float m_distance

Detailed Description

Definition at line 30 of file camera.hpp.


Member Enumeration Documentation

Enumerator:
CM_NORMAL 
CM_CLOSEUP 
CM_DRIFTING 
CM_LEADER_MODE 
CM_REVERSE 
CM_SIMPLE_REPLAY 

Definition at line 33 of file camera.hpp.

              {
        CM_NORMAL,        // Normal camera mode
        CM_CLOSEUP,       // Normal camera, closer to kart
        CM_DRIFTING,      // FIXME: drifting behind when accelerating = not yet implemented
        CM_LEADER_MODE,   // for deleted player karts in follow the leader
        CM_REVERSE,       // Camera is pointing backwards
        CM_SIMPLE_REPLAY
    };

Constructor & Destructor Documentation

Camera::Camera ( int  camera_index,
const Kart kart 
)

Definition at line 30 of file camera.cpp.

{
    m_mode              = CM_NORMAL;
    m_context           = new ssgContext ;
    m_distance          = kart->getKartProperties()->getCameraDistance();
    m_kart              = kart;

    btVector3 start_pos = m_kart->getPos();
    sgSetVec3(m_current_pos.xyz, start_pos.getX(), start_pos.getY(), start_pos.getZ());
    sgSetVec3(m_current_pos.hpr, 0, 0, 0);

    // FIXME: clipping should be configurable for slower machines
    const Track* track  = world->getTrack();
    if (track->useFog())
        m_context -> setNearFar ( 0.05f, track->getFogEnd() ) ;
    else
        m_context -> setNearFar ( 0.05f, 1000.0f ) ;

    setScreenPosition(camera_index);
}   // Camera

Here is the call graph for this function:


Member Function Documentation

void Camera::apply ( )

Definition at line 186 of file camera.cpp.

{
    int width  = user_config->m_width ;
    int height = user_config->m_height;

    glViewport ( (int)((float)width  * m_x),
                 (int)((float)height * m_y),
                 (int)((float)width  * m_w),
                 (int)((float)height * m_h) ) ;

    m_context -> makeCurrent () ;
}   // apply
void Camera::reset ( )

Reset is called when a new race starts.

Make sure that the camera is aligned neutral, and not like in the previous race

Definition at line 111 of file camera.cpp.

{
    m_last_pitch = 0.0f;
}   // reset
void Camera::setMode ( Mode  mode_)

Definition at line 97 of file camera.cpp.

{
    m_mode       = mode;
    m_last_pitch = 0.0f;
    if(m_mode==CM_CLOSEUP)
        m_distance = 2.5f;
    else
        m_distance = m_kart->getKartProperties()->getCameraDistance();
}   // setMode

Here is the call graph for this function:

Here is the caller graph for this function:

void Camera::setScreenPosition ( int  pos)

Set the camera to the given mode.

Definition at line 52 of file camera.cpp.

{
    const int num_players = race_manager->getNumPlayers();
    assert(camera_index >= 0 && camera_index <= 3);

    if (num_players == 1)
    {
        m_context -> setFOV ( 75.0f, 0.0f ) ;
        m_x = 0.0f; m_y = 0.0f; m_w = 1.0f; m_h = 1.0f ;
    }
    else if (num_players == 2)
    {
        m_context -> setFOV ( 85.0f, 85.0f*3.0f/8.0f ) ;
        switch ( camera_index )
        {
        case 0 : m_x = 0.0f; m_y = 0.5f; m_w = 1.0f; m_h = 0.5f; break;
        case 1 : m_x = 0.0f; m_y = 0.0f; m_w = 1.0f; m_h = 0.5f; break;
        }
    }
    else if (num_players == 3)
    {
        m_context -> setFOV ( 50.0f, 0.0f );
        switch ( camera_index )
        {
        case 0 : m_x = 0.0f; m_y = 0.5f; m_w = 0.5f; m_h = 0.5f; break;
        case 1 : m_x = 0.5f; m_y = 0.5f; m_w = 0.5f; m_h = 0.5f; break;
        case 2 : m_x = 0.0f; m_y = 0.0f; m_w = 1.0f; m_h = 0.5f;
                 m_context -> setFOV ( 85.0f, 85.0f*3.0f/8.0f ); break;
        }
    }
    else if (num_players == 4)
    {
        m_context -> setFOV ( 50.0f, 0.0f );
        switch ( camera_index )
        {
        case 0 : m_x = 0.0f; m_y = 0.5f; m_w = 0.5f; m_h = 0.5f; break;
        case 1 : m_x = 0.5f; m_y = 0.5f; m_w = 0.5f; m_h = 0.5f; break;
        case 2 : m_x = 0.0f; m_y = 0.0f; m_w = 0.5f; m_h = 0.5f; break;
        case 3 : m_x = 0.5f; m_y = 0.0f; m_w = 0.5f; m_h = 0.5f; break;
        }
    }
    m_last_pitch = 0.0f;
}  // setScreenPosition

Here is the call graph for this function:

Here is the caller graph for this function:

void Camera::update ( float  dt)

Definition at line 117 of file camera.cpp.

{
    sgCoord kartcoord;
    const Kart *kart;
    
    // First define the position of the kart
    if(m_mode==CM_LEADER_MODE)
    {
        kart=world->getKart(0);
        sgCopyCoord(&kartcoord, kart->getCoord());
    }
    else
    {
        kart = m_kart;
        sgCopyCoord(&kartcoord, kart->getCoord());

        // Use the terrain pitch to avoid the camera following a wheelie the kart is doing
        kartcoord.hpr[1]=RAD_TO_DEGREE(m_kart->getTerrainPitch(DEGREE_TO_RAD(kartcoord.hpr[0])) );
        kartcoord.hpr[2] = 0;
        // Only adjust the pitch if it's not the first frame (which is indicated by having
        // dt=0). Otherwise the camera will change pitch during ready-set-go.
        if(dt>0)
        {
            // If the terrain pitch is 'significantly' different from the camera angle,
            // start adjusting the camera. This helps with steep declines, where
            // otherwise the track is not visible anymore.
            if(fabsf(kartcoord.hpr[1]-m_last_pitch)>1.0f) {
                kartcoord.hpr[1] = m_last_pitch + (kartcoord.hpr[1]-m_last_pitch)*2.0f*dt;
            }
            else
            {
                kartcoord.hpr[1]=m_last_pitch;
            }
        }   //  dt>0.0
        m_last_pitch = kartcoord.hpr[1];
    }   // m_mode!=CM_LEADER_MODE
    if(m_mode==CM_SIMPLE_REPLAY) kartcoord.hpr[0] = 0;

    // Set the camera position relative to the kart
    // --------------------------------------------
    sgMat4 cam_pos;

    // The reverse mode and the cam used in follow the leader mode (when a
    // kart has been eliminated) are facing backwards:
    bool reverse= m_mode==CM_REVERSE || m_mode==CM_LEADER_MODE;
    sgMakeTransMat4(cam_pos, 0.f, -m_distance, reverse ? 0.75f : 1.5f);
    
    // Set the camera rotation
    // -----------------------
    sgMat4 cam_rot;
    sgMakeRotMat4(cam_rot, reverse            ? 180.0f : 0.0f,
                           m_mode==CM_CLOSEUP ? -15.0f : -5.0f,
                           0);
    
    // Matrix that transforms stuff to kart-space
    sgMat4 tokart;
    sgMakeCoordMat4 (tokart, &kartcoord);

    sgMat4 relative;
    sgMultMat4(relative, cam_pos, cam_rot);
    sgMat4 result;
    sgMultMat4(result, tokart, relative);

    sgSetCoord(&m_current_pos, result);

    m_context -> setCamera (&m_current_pos) ;
}   // update

Here is the call graph for this function:


Member Data Documentation

ssgContext* Camera::m_context [protected]

Definition at line 42 of file camera.hpp.

sgCoord Camera::m_current_pos [protected]

Definition at line 43 of file camera.hpp.

float Camera::m_current_speed [protected]

Definition at line 47 of file camera.hpp.

float Camera::m_distance [protected]

Definition at line 49 of file camera.hpp.

float Camera::m_h [protected]

Definition at line 46 of file camera.hpp.

const Kart* Camera::m_kart [protected]

Definition at line 44 of file camera.hpp.

float Camera::m_last_pitch [protected]

Definition at line 48 of file camera.hpp.

Mode Camera::m_mode [protected]

Definition at line 45 of file camera.hpp.

float Camera::m_w [protected]

Definition at line 46 of file camera.hpp.

float Camera::m_x [protected]

Definition at line 46 of file camera.hpp.

float Camera::m_y [protected]

Definition at line 46 of file camera.hpp.


The documentation for this class was generated from the following files: