Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: camera frustum culling plane added

File size: 5.9 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  this->setClassID(CL_LENSE_FLARE, "LenseFlare");
49
50  this->flareMatrix = new float[14];
51  /*          length                      image scale */
52  this->flareMatrix[0] = 1.0f; this->flareMatrix[1] = 1.0f;
53  this->flareMatrix[2] = 0.5f; this->flareMatrix[3] = 0.5f;
54  this->flareMatrix[4] = 0.33f; this->flareMatrix[5] = 0.25f;
55  this->flareMatrix[6] = 0.125f; this->flareMatrix[7] = 1.0f;
56  this->flareMatrix[8] = -0.5f; this->flareMatrix[9] = 0.5f;
57  this->flareMatrix[10] = -0.25f; this->flareMatrix[11] = 0.15f;
58  this->flareMatrix[12] = -1.82f; this->flareMatrix[13] = 0.25f;
59
60  this->lightSource = (LightManager::getInstance())->getLight(0);
61  PRINTF(0)("light is: %p\n", this->lightSource);
62
63  if (root != NULL)
64  {
65    this->loadParams(root);
66    this->activate();
67  }
68}
69
70
71/**
72 *  destroys a LenseFlare
73 */
74LenseFlare::~LenseFlare()
75{
76  std::vector<Billboard*>::iterator it;
77  for( it = flares.begin(); it != flares.end(); it++)
78    delete (*it);
79}
80
81
82/**
83 * @param root The XML-element to load the LenseFlare from
84 */
85void LenseFlare::loadParams(const TiXmlElement* root)
86{
87  GraphicsEffect::loadParams(root);
88
89  LOAD_PARAM_START_CYCLE(root, element);
90  {
91    LoadParam_CYCLE(element, "add-flare-texture", this, LenseFlare, addFlare)
92        .describe("adds a lensflare texture to the engine");
93  }
94  LOAD_PARAM_END_CYCLE(element);
95}
96
97
98/**
99 * initializes the fog effect
100 */
101bool LenseFlare::init()
102{}
103
104
105/**
106 * activates the fog effect
107 */
108bool LenseFlare::activate()
109{
110  this->bActivated = true;
111}
112
113
114/**
115 * deactivates the fog effect
116 */
117bool LenseFlare::deactivate()
118{
119  this->bActivated = false;
120}
121
122
123/**
124 * converts a gl mode char to a GLint
125 * @param mode the mode character
126 */
127GLint LenseFlare::charToFogMode(const char* mode)
128{}
129
130
131/**
132 * adds a texture flare
133 * @param textureName the name of the flare texture
134 *
135 *  1st: Texture of the Sun/Light source itself
136 *  2nd: Texture of the fist halo
137 *  3rd: Texture of small burst
138 *  4th: Texture of the second halo
139 *  5th: Texutre of the second burst
140 *  6th: Texture of the third halo
141 *  7th: Texture of the third burst
142 */
143void LenseFlare::addFlare(const char* textureName)
144{
145  if( this->flares.size() > LF_MAX_FLARES)
146  {
147    PRINTF(2)("You tried to add more than %i lense flares, ignoring\n", LF_MAX_FLARES);
148    return;
149  }
150
151  Billboard* bb = new Billboard(NULL);
152  bb->setTexture(textureName);
153  bb->setSize(50, 50);
154  this->flares.push_back(bb);
155  PRINTF(0)("Added a Lenseflare Billboard with texture %s\n", textureName);
156
157  // the first flare belongs to the light source
158  if( this->flares.size() == 1 && this->lightSource != NULL)
159  {
160    bb->setBindNode(static_cast<PNode*>(this->lightSource));
161    bb->setVisibility(true);
162  }
163  PRINTF(0)("Finished adding\n", textureName);
164}
165
166
167bool LenseFlare::sourceVisible() const
168{
169
170  float dist = this->frustumPlane.distancePoint(this->lightSource->getAbsCoor());
171  if( dist < 0.0f)
172  {
173    std::vector<Billboard*>::const_iterator it = flares.begin(); it++;
174    for(; it != flares.end(); it++)
175      (*it)->setVisibility(true);
176    return true;
177  }
178
179  std::vector<Billboard*>::const_iterator it = flares.begin(); it++;
180  for(; it != flares.end(); it++)
181    (*it)->setVisibility(false);
182
183  return false;
184}
185
186
187/**
188 * tick the effect
189 */
190void LenseFlare::tick(float dt)
191{
192  if( unlikely(!this->bActivated || this->flares.size() == 0))
193    return;
194
195  // refetch light source information if needed
196  if( unlikely( this->lightSource == NULL))
197  {
198    this->lightSource = (LightManager::getInstance())->getLight(0);
199    if( this->flares.size() > 0)
200      this->flares[0]->setBindNode(static_cast<PNode*>(this->lightSource));
201  }
202
203    //set the frustum plane
204  Vector psTarget = (State::getCameraTarget())->getAbsCoor();
205  Vector psCamera = (State::getCamera())->getAbsCoor();
206  Vector psCameraDir = psCamera - psTarget;
207  //PRINTF(0)("camera Dir: %f %f %f, camera: %f %f %f\n", psCameraDir.x, psCameraDir.y, psCameraDir.z, psCamera.x, psCamera.y, psCamera.z);
208  this->frustumPlane = Plane(psCameraDir, Vector(-70.0,0.0,0.0));
209
210
211  // always update the screen center, it could be, that the window is resized
212  this->screenCenter = Vector(State::getResX()/2.0f, State::getResY()/2.0f, 0.0f);
213  // flare vector is the direction from the center to the light source
214  this->flareVector = this->flares[0]->getAbsCoor2D() - this->screenCenter;
215  this->flareVector.z = 0.0f;
216  this->distance = this->flareVector.len();
217  this->flareVector.normalize();
218
219  // now calculate the new coordinates of the billboards
220  std::vector<Billboard*>::iterator it;
221  int i;
222  for( it = flares.begin(), i = 0; it != flares.end(); it++, i++)
223  {
224    // set the new position
225    if( i == 0)
226      continue;
227
228    (*it)->setAbsCoor2D( this->screenCenter + this->flareVector * this->flareMatrix[i * 2] * this->distance);
229    (*it)->setSize2D(50.0f * this->flareMatrix[i * 2 + 1], 50.0f * this->flareMatrix[i * 2 + 1]);
230    PRINTF(5)("Tick flare %i @ (%f, %f)\n", i, (*it)->getAbsCoor2D().x, (*it)->getAbsCoor2D().y);
231    // tick them
232    (*it)->tick(dt);
233  }
234}
235
236
237/**
238 * draws the LenseFlares
239 */
240void LenseFlare::draw() const
241{
242  if( !this->bActivated)
243    return;
244
245  this->sourceVisible();
246}
Note: See TracBrowser for help on using the repository browser.