Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/camera.cc @ 10237

Last change on this file since 10237 was 10237, checked in by muellmic, 17 years ago

changed camera handling for the vertical-scroller playmode. the top-down view in vertical scroller is now a proper camera-viewmode. also added a bool-state in the camera class to override the camera's event-listener and handle the camera-events (view buttons) in other classes. i used this to handle the camera parenting with the space-ship class, so other views as the top-down get useful and nice-looking again…. *taking-a-look-at-the-watch* oops damnit… i hope i won't forget the last tram ;-)

File size: 5.5 KB
RevLine 
[4832]1/*
[2068]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
[2080]12   main-programmer: Christian Meyer
[6424]13   co-programmer: Benjamin Grauer
[2068]14*/
[5357]15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
[2068]16
17#include "camera.h"
[7868]18#include "key_mapper.h"
[9406]19#include "glincl.h"
[3608]20
[9869]21ObjectListDefinition(Camera);
22
[2096]23/**
[4836]24 *  creates a Camera
[2096]25*/
[4746]26Camera::Camera()
[2068]27{
[9869]28  this->registerObject(this, Camera::_objectList);
[4987]29  this->setName("camera");
[3635]30  this->target = new CameraTarget();
[3636]31
[7868]32  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0);
33  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1);
34  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2);
35  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3);
36  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4);
37  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5);
[4414]38
[3641]39  this->setFovy(90);
[3636]40  this->setAspectRatio(1.2f);
[9235]41  this->setClipRegion(.1, 10000);
[3641]42
[7347]43  this->setViewMode(Camera::ViewNormal);
[4987]44
[5004]45  this->setParentMode(PNODE_ALL);
[10237]46  this->eventHandling = true;
47
48
[2068]49}
50
[2096]51/**
[4836]52 *  default destructor
[2096]53*/
[4746]54Camera::~Camera()
[6424]55{}
[3543]56
57/**
[4836]58 *  focuses the Camera onto a Target
59 * @param target the new PNode the Camera should look at.
[2096]60*/
[3635]61void Camera::lookAt(PNode* target)
[2068]62{
[3635]63  this->target->setParent(target);
[2068]64}
65
[3638]66/**
[4836]67 * @returns The PNode of the Target (from there you can get position and so on
[3638]68*/
[7014]69PNode* Camera::getTargetNode() const
[2068]70{
[3635]71  return (PNode*)this->target;
[2068]72}
73
[2096]74/**
[4836]75 *  sets a new AspectRatio
76 * @param aspectRatio the new aspect ratio to set (width / height)
[3636]77*/
78void Camera::setAspectRatio(float aspectRatio)
79{
80  this->aspectRatio = aspectRatio;
81}
82
83/**
[4992]84 * Sets a new clipping region
85 * @param nearClip The near clip plane
86 * @param farClip The far clip plane
[3636]87*/
88void Camera::setClipRegion(float nearClip, float farClip)
89{
90  this->nearClip = nearClip;
91  this->farClip = farClip;
92}
93
[4490]94/**
[4836]95 *  sets the new VideoMode and initializes iteration to it.
96 * @param mode the mode to change to.
[4490]97*/
[3639]98void Camera::setViewMode(ViewMode mode)
99{
[6034]100  currentMode = mode;
[3639]101  switch (mode)
[6424]102  {
[3639]103    default:
[7347]104    case Camera::ViewNormal:
[10237]105    {
[3639]106      this->toFovy = 60.0;
[4992]107      this->setRelCoorSoft(-10, 5, 0);
108      this->target->setRelCoorSoft(0,0,0);
[3639]109      break;
[10237]110    }
[7347]111    case Camera::ViewBehind:
[3639]112      break;
[7347]113    case Camera::ViewFront:
[10237]114    {
[4992]115      this->toFovy = 120.0;
[6424]116      this->setRelCoorSoft(4, 0, 0, 5);
117      this->target->setRelCoorSoft(Vector(10,0,0), 5);
[3639]118      break;
[10237]119    }
[7347]120    case Camera::ViewLeft:
[10237]121    {
[3639]122      this->toFovy = 90;
[4992]123      this->setRelCoorSoft(0, 1, -10, .5);
124      this->target->setRelCoorSoft(0,0,0);
[3639]125      break;
[10237]126    }
[7347]127    case Camera::ViewRight:
[10237]128    {
[3639]129      this->toFovy = 90;
[4987]130      this->setRelCoorSoft(Vector(0, 1, 10));
[4992]131      this->target->setRelCoorSoft(0,0,0);
[3639]132      break;
[10237]133    }
[7347]134    case Camera::ViewTop:
[10237]135    {
[3643]136      this->toFovy= 120;
[10237]137      this->setRelCoor(Vector(-0.05, 40, 0));
138      this->target->setRelCoor(0,0,0);
139    }
[6424]140  }
[3639]141}
142
143
[3636]144/**
[4836]145 *  Updates the position of the camera.
146 * @param dt: The time that elapsed.
[3639]147*/
148void Camera::tick(float dt)
149{
[7009]150  //update frustum plane
[7014]151  this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized();
152  this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1);
[7009]153
154  this->upVector =  this->getAbsDirV();
155
156
[7173]157  float tmpFovy = (this->toFovy - this->fovy);
[6034]158  if (tmpFovy > 0.01)
[5354]159    this->fovy += tmpFovy * fabsf(dt);
[3639]160}
161
162
163/**
[4836]164 *  initialize rendering perspective according to this camera
[6772]165 *
166 * This is called immediately before the rendering cycle starts, it sets all global
167 * rendering options as well as the GL_PROJECTION matrix according to the camera.
168 */
[2068]169void Camera::apply ()
170{
[3636]171  // switching to Projection Matrix
[2551]172  glMatrixMode (GL_PROJECTION);
[2112]173  glLoadIdentity ();
[3635]174
[3636]175  gluPerspective(this->fovy,
[4832]176                 this->aspectRatio,
177                 this->nearClip,
178                 this->farClip);
[6778]179
180
181    // setting up the perspective
[3636]182  // speed-up feature
[7108]183  glMatrixMode (GL_MODELVIEW);
184  glLoadIdentity();
185
186
187}
188
189void Camera::project()
190{
[3635]191  Vector cameraPosition = this->getAbsCoor();
192  Vector targetPosition = this->target->getAbsCoor();
[2551]193
[7108]194       // Setting the Camera Eye, lookAt and up Vectors
[7009]195  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
[6965]196            targetPosition.x, targetPosition.y, targetPosition.z,
[7009]197            this->upVector.x, this->upVector.y, this->upVector.z);
[7108]198}
[3636]199
[6965]200
[4490]201/**
[4836]202 *  processes an event
203 * @param event: the event to process
[4490]204*/
[4414]205void Camera::process(const Event &event)
206{
[10237]207  if (eventHandling == true)
[6424]208  {
[10237]209    if( event.type == KeyMapper::PEV_VIEW0)
210    {
211      this->setViewMode(Camera::ViewNormal);
212    }
213    else if( event.type == KeyMapper::PEV_VIEW1)
214    {
215      this->setViewMode(Camera::ViewBehind);
216    }
217    else if( event.type == KeyMapper::PEV_VIEW2)
218    {
219      this->setViewMode(Camera::ViewFront);
220    }
221    else if( event.type == KeyMapper::PEV_VIEW3)
222    {
223      this->setViewMode(Camera::ViewLeft);
224    }
225    else if( event.type == KeyMapper::PEV_VIEW4)
226    {
227      this->setViewMode(Camera::ViewRight);
228    }
229    else if( event.type == KeyMapper::PEV_VIEW5)
230    {
231      this->setViewMode(Camera::ViewTop);
232    }
[6424]233  }
[4414]234}
[3365]235
[4414]236
[3635]237///////////////////
238// CAMERA-TARGET //
239///////////////////
[2636]240
[9869]241ObjectListDefinition(CameraTarget);
[3635]242CameraTarget::CameraTarget()
[2636]243{
[9869]244  this->registerObject(this, CameraTarget::_objectList);
[6424]245  //  this->setParentMode(PNODE_MOVEMENT);
[2636]246}
[3213]247
Note: See TracBrowser for help on using the repository browser.