Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/camera.cc @ 7605

Last change on this file since 7605 was 7347, checked in by bensch, 19 years ago

orxonox/trunk: some more stuff (now one can play around with them using the Shell →
GameWorld Urban Playmode …. Vertical/Full3D/Horizontal

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