Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/effects/lense_flare.cc @ 6976

Last change on this file since 6976 was 6976, checked in by patrick, 18 years ago

trunk: the graphicsengine is now loadable

File size: 5.6 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13### File Specific:
14   main-programmer: Patrick Boenzli
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
18
19#include "lense_flare.h"
20
21#include "load_param.h"
22#include "factory.h"
23
24#include "glincl.h"
25#include "texture.h"
26
27#include "light.h"
28#include "state.h"
29
30#include "render2D/billboard.h"
31
32#include "light.h"
33#include "camera.h"
34
35
36using namespace std;
37
38
39CREATE_FACTORY(LenseFlare, CL_LENSE_FLARE);
40
41
42/**
43 *  default constructor
44 * @param root The XML-element to load the LenseFlare from
45 */
46 LenseFlare::LenseFlare(const TiXmlElement* root)
47{
48  if (root != NULL)
49    this->loadParams(root);
50
51  this->flareMatrix = new float[14];
52  /*          length                      image scale */
53  this->flareMatrix[0] = 1.0f; this->flareMatrix[1] = 1.0f;
54  this->flareMatrix[2] = 0.5f; this->flareMatrix[3] = 0.5f;
55  this->flareMatrix[4] = 0.33f; this->flareMatrix[5] = 0.25f;
56  this->flareMatrix[6] = 0.125f; this->flareMatrix[7] = 1.0f;
57  this->flareMatrix[8] = -0.5f; this->flareMatrix[9] = 0.5f;
58  this->flareMatrix[10] = -0.25f; this->flareMatrix[11] = 0.15f;
59  this->flareMatrix[12] = -1.82f; this->flareMatrix[13] = 0.25f;
60
61  this->lightSource = (LightManager::getInstance())->getLight(0);
62}
63
64
65/**
66 *  destroys a LenseFlare
67 */
68LenseFlare::~LenseFlare()
69{
70  std::vector<Billboard*>::iterator it;
71  for( it = flares.begin(); it != flares.end(); it++)
72    delete (*it);
73}
74
75
76/**
77 * @param root The XML-element to load the LenseFlare from
78 */
79void LenseFlare::loadParams(const TiXmlElement* root)
80{
81  GraphicsEffect::loadParams(root);
82
83    LoadParam(root, "add-flare-texture", this, LenseFlare, addFlare)
84        .describe("adds a lensflare texture to the engine");
85}
86
87
88/**
89 * initializes the fog effect
90 */
91bool LenseFlare::init()
92{}
93
94
95/**
96 * activates the fog effect
97 */
98bool LenseFlare::activate()
99{
100  this->bActivated = true;
101}
102
103
104/**
105 * deactivates the fog effect
106 */
107bool LenseFlare::deactivate()
108{
109  this->bActivated = false;
110}
111
112
113/**
114 * converts a gl mode char to a GLint
115 * @param mode the mode character
116 */
117GLint LenseFlare::charToFogMode(const char* mode)
118{}
119
120
121/**
122 * adds a texture flare
123 * @param textureName the name of the flare texture
124 *
125 *  1st: Texture of the Sun/Light source itself
126 *  2nd: Texture of the fist halo
127 *  3rd: Texture of small burst
128 *  4th: Texture of the second halo
129 *  5th: Texutre of the second burst
130 *  6th: Texture of the third halo
131 *  7th: Texture of the third burst
132 */
133void LenseFlare::addFlare(const char* textureName)
134{
135  if( this->flares.size() > LF_MAX_FLARES)
136  {
137    PRINTF(2)("You tried to add more than %i lense flares, ignoring\n", LF_MAX_FLARES);
138    return;
139  }
140
141  Billboard* bb = new Billboard(NULL);
142  bb->setTexture(textureName);
143  bb->setSize(50, 50);
144  this->flares.push_back(bb);
145  PRINTF(0)("Added a Lenseflare Billboard with texture %s\n", textureName);
146
147  // the first flare belongs to the light source
148  if( this->flares.size() == 1 && this->lightSource != NULL)
149  {
150    bb->setBindNode(static_cast<PNode*>(this->lightSource));
151    bb->setVisibility(true);
152  }
153}
154
155
156bool LenseFlare::sourceVisible() const
157{
158
159  float dist = this->frustumPlane.distancePoint(this->lightSource->getAbsCoor());
160  if( dist < 0.0f)
161  {
162    std::vector<Billboard*>::const_iterator it = flares.begin(); it++;
163    for(; it != flares.end(); it++)
164      (*it)->setVisibility(true);
165    return true;
166  }
167
168  std::vector<Billboard*>::const_iterator it = flares.begin(); it++;
169  for(; it != flares.end(); it++)
170    (*it)->setVisibility(false);
171
172  return false;
173}
174
175
176/**
177 * tick the effect
178 */
179void LenseFlare::tick(float dt)
180{
181  if( unlikely(!this->bActivated || this->flares.size() == 0))
182    return;
183
184  // refetch light source information if needed
185  if( unlikely( this->lightSource == NULL))
186  {
187    this->lightSource = (LightManager::getInstance())->getLight(0);
188    if( this->flares.size() > 0)
189      this->flares[0]->setBindNode(static_cast<PNode*>(this->lightSource));
190  }
191
192    //set the frustum plane
193  Vector psTarget = (State::getCameraTarget())->getAbsCoor();
194  Vector psCamera = (State::getCamera())->getAbsCoor();
195  Vector psCameraDir = psCamera - psTarget;
196  //PRINTF(0)("camera Dir: %f %f %f, camera: %f %f %f\n", psCameraDir.x, psCameraDir.y, psCameraDir.z, psCamera.x, psCamera.y, psCamera.z);
197  this->frustumPlane = Plane(psCameraDir, Vector(-70.0,0.0,0.0));
198
199
200  // always update the screen center, it could be, that the window is resized
201  this->screenCenter = Vector(State::getResX()/2.0f, State::getResY()/2.0f, 0.0f);
202  // flare vector is the direction from the center to the light source
203  this->flareVector = this->flares[0]->getAbsCoor2D() - this->screenCenter;
204  this->flareVector.z = 0.0f;
205  this->distance = this->flareVector.len();
206  this->flareVector.normalize();
207
208  // now calculate the new coordinates of the billboards
209  std::vector<Billboard*>::iterator it;
210  int i;
211  for( it = flares.begin(), i = 0; it != flares.end(); it++, i++)
212  {
213    // set the new position
214    if( i == 0)
215      continue;
216
217    (*it)->setAbsCoor2D( this->screenCenter + this->flareVector * this->flareMatrix[i * 2] * this->distance);
218    (*it)->setSize2D(50.0f * this->flareMatrix[i * 2 + 1], 50.0f * this->flareMatrix[i * 2 + 1]);
219    PRINTF(5)("Tick flare %i @ (%f, %f)\n", i, (*it)->getAbsCoor2D().x, (*it)->getAbsCoor2D().y);
220    // tick them
221    (*it)->tick(dt);
222  }
223}
224
225
226/**
227 * draws the LenseFlares
228 */
229void LenseFlare::draw() const
230{
231  if( !this->bActivated)
232    return;
233
234  this->sourceVisible();
235}
Note: See TracBrowser for help on using the repository browser.