/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Christian Meyer co-programmer: ... */ #include "camera.h" #include "world.h" #include "world_entity.h" using namespace std; /** \brief creates a Camera This standard constructor sets all parameters to zero */ Camera::Camera (World* world) { this->world = world; coord = new Coordinate(); bound = NULL; /* give it some physical live */ mass = 10; acceleration = new Vector(0.0, 0.0, 0.0); velocity = new Vector(0.0, 0.0, 0.0); cameraMode = NORMAL; deltaTime = 3000.0; cameraOffset = 1.0; cameraOffsetZ = 10.0; t = 0.0; actual_place.pos.x = 0.0; actual_place.pos.y = 10.0; actual_place.pos.z = -5.0; // Initializing Target target = new CameraTarget (Vector (0,0,0)); } /** \brief default destructor */ Camera::~Camera () { } /** \brief time based actualisation of camera parameters \param deltaT: The amount of time that has passed in milliseconds This is called by the World in every time_slice, use it to do fancy time dependant effects (such as smooth camera movement or swaying). */ void Camera::time_slice (Uint32 deltaT) { if(t <= deltaTime) {t += deltaT;} //printf("time is: t=%f\n", t ); update_desired_place (); jump (NULL); } /** \brief this calculates the location where the track wants the camera to be This refreshes the placement the camera should have according to the bound entity's position on the track. */ void Camera::update_desired_place () { switch(cameraMode) { case NORMAL: coord->setPosition(world->track->getPos() - world->track->getDir() *10 +Vector (0,0,5), WORLD); target->coord->setPosition(world->track->getPos(), WORLD); Location lookat; if( bound != NULL && world != NULL ) { bound->get_lookat (&lookat); world->calc_camera_pos (&lookat, &desired_place); } else { desired_place.pos = Vector (0,0,0); desired_place.rot = Quaternion (); } break; } } /** \brief initialize rendering perspective according to this camera This is called immediately before the rendering cycle starts, it sets all global rendering options as well as the GL_PROJECTION matrix according to the camera. */ void Camera::apply () { glMatrixMode (GL_PROJECTION); glLoadIdentity (); // view // TO DO: implement options for frustum generation glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0); // ===== first camera control calculation option // rotation Vector eye = coord->getPosition(WORLD); Vector targetV = target->coord->getPosition(WORLD); gluLookAt (eye.x, eye.y, eye.z, targetV.x, targetV.y, targetV.z, 0,0,1); float matrix[4][4]; actual_place.rot.conjugate().matrix (matrix); /* orientation and */ // glMultMatrixf ((float*)matrix); /* translation */ // glTranslatef (-actual_place.pos.x, -actual_place.pos.y,- actual_place.pos.z); //Placement *plBound = bound->get_placement(); // ===== second camera control calculation option /* gluLookAt(actual_place.pos.x, actual_place.pos.y, actual_place.pos.z, bound->pos.x, bound->pos.y, bound->pos.z, 0.0, 0.0, 1.0); */ glMatrixMode (GL_MODELVIEW); glLoadIdentity (); } /** \brief set the camera position \param plc: The Placement to set the camera to This will set the actual and desired placement of the camera to plc */ void Camera::jump (Placement* plc = NULL) { if( plc == NULL) { actual_place = desired_place; //printf("Camera|jump: camer@ %f, %f, %f\n\n", actual_place.r.x, actual_place.r.y, actual_place.r.z); } else { desired_place = *plc; actual_place = *plc; } } /** \brief bind the camera to an entity \param entity: The enitity to bind the camera to This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's Location and get_lookat() to determine the viewpoint the camera will render from. Note that you cannot bind a camera to a free entity. */ void Camera::bind (WorldEntity* entity) { if( entity != NULL) { if( entity->isFree ()) printf("Cannot bind camera to free entity"); else { bound = entity; } } } void Camera::setWorld(World* world) { this->world = world; } CameraTarget::CameraTarget () { coord = new Coordinate(); } CameraTarget::CameraTarget (Vector pos) { coord->setPosition (pos, WORLD); }