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 | using namespace std; |
---|
33 | |
---|
34 | CREATE_FACTORY(LenseFlare, CL_LENSE_FLARE); |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | /** |
---|
39 | * default constructor |
---|
40 | * @param root The XML-element to load the LenseFlare from |
---|
41 | */ |
---|
42 | LenseFlare::LenseFlare(const TiXmlElement* root) |
---|
43 | { |
---|
44 | if (root != NULL) |
---|
45 | this->loadParams(root); |
---|
46 | |
---|
47 | this->flareMatrix = new float[14]; |
---|
48 | /* length image scale */ |
---|
49 | this->flareMatrix[0] = 1.0f; this->flareMatrix[1] = 1.0f; |
---|
50 | this->flareMatrix[2] = 0.5f; this->flareMatrix[3] = 0.5f; |
---|
51 | this->flareMatrix[4] = 0.33f; this->flareMatrix[5] = 0.25f; |
---|
52 | this->flareMatrix[6] = 0.125f; this->flareMatrix[7] = 1.0f; |
---|
53 | this->flareMatrix[8] = -0.5f; this->flareMatrix[9] = 0.5f; |
---|
54 | this->flareMatrix[10] = -0.25f; this->flareMatrix[11] = 0.25f; |
---|
55 | this->flareMatrix[12] = 1.82f; this->flareMatrix[13] = 0.25f; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | /** |
---|
60 | * destroys a LenseFlare |
---|
61 | */ |
---|
62 | LenseFlare::~LenseFlare() |
---|
63 | { |
---|
64 | std::vector<Billboard*>::iterator it; |
---|
65 | for( it = flares.begin(); it != flares.end(); it++) |
---|
66 | delete (*it); |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | /** |
---|
71 | * @param root The XML-element to load the LenseFlare from |
---|
72 | */ |
---|
73 | void LenseFlare::loadParams(const TiXmlElement* root) |
---|
74 | { |
---|
75 | GraphicsEffect::loadParams(root); |
---|
76 | |
---|
77 | LoadParam(root, "add-flare-texture", this, LenseFlare, addFlare) |
---|
78 | .describe("adds a lensflare texture to the engine"); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /** |
---|
83 | * initializes the fog effect |
---|
84 | */ |
---|
85 | bool LenseFlare::init() |
---|
86 | {} |
---|
87 | |
---|
88 | |
---|
89 | /** |
---|
90 | * activates the fog effect |
---|
91 | */ |
---|
92 | bool LenseFlare::activate() |
---|
93 | { |
---|
94 | this->bActivated = true; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | /** |
---|
99 | * deactivates the fog effect |
---|
100 | */ |
---|
101 | bool LenseFlare::deactivate() |
---|
102 | { |
---|
103 | this->bActivated = false; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | /** |
---|
108 | * converts a gl mode char to a GLint |
---|
109 | * @param mode the mode character |
---|
110 | */ |
---|
111 | GLint LenseFlare::charToFogMode(const char* mode) |
---|
112 | {} |
---|
113 | |
---|
114 | |
---|
115 | /** |
---|
116 | * adds a texture flare |
---|
117 | * @param textureName the name of the flare texture |
---|
118 | * |
---|
119 | * 1st: Texture of the Sun/Light source itself |
---|
120 | * 2nd: Texture of the fist halo |
---|
121 | * 3rd: Texture of small burst |
---|
122 | * 4th: Texture of the second halo |
---|
123 | * 5th: Texutre of the second burst |
---|
124 | * 6th: Texture of the third halo |
---|
125 | * 7th: Texture of the third burst |
---|
126 | */ |
---|
127 | void LenseFlare::addFlare(const char* textureName) |
---|
128 | { |
---|
129 | if( this->flares.size() < LF_MAX_FLARES) |
---|
130 | { |
---|
131 | PRINTF(2)("You tried to add more than %i lense flares, ignoring\n", LF_MAX_FLARES); |
---|
132 | return; |
---|
133 | } |
---|
134 | |
---|
135 | Billboard* bb = new Billboard(NULL); |
---|
136 | bb->setTexture(textureName); |
---|
137 | this->flares.push_back(bb); |
---|
138 | PRINTF(0)("Added a Lenseflare Billboard with texture %s\n", textureName); |
---|
139 | |
---|
140 | // the first flare belongs to the light source |
---|
141 | if( this->flares.size() == 1 && this->lightSource != NULL) |
---|
142 | { |
---|
143 | bb->setBindNode(static_cast<PNode*>(this->lightSource)); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | /** |
---|
150 | * tick the effect |
---|
151 | */ |
---|
152 | void LenseFlare::tick(float dt) |
---|
153 | { |
---|
154 | if( unlikely(!this->bActivated || this->flares.size() == 0)) |
---|
155 | return; |
---|
156 | // always update the screen center, it could be, that the window is resized |
---|
157 | this->screenCenter = Vector(State::getResX()/2.0f, State::getResY()/2.0f, 0.0f); |
---|
158 | // flare vector is the direction from the center to the light source |
---|
159 | this->flareVector = this->flares[0]->getAbsCoor2D(); |
---|
160 | this->distance = this->flareVector.len(); |
---|
161 | this->flareVector.normalize(); |
---|
162 | |
---|
163 | // now calculate the new coordinates of the billboards |
---|
164 | std::vector<Billboard*>::iterator it; |
---|
165 | int i; |
---|
166 | for( it = flares.begin(), i = 0; it != flares.end(); it++, i++) |
---|
167 | { |
---|
168 | // set the new position |
---|
169 | (*it)->setAbsCoor2D(this->flareVector * this->flareMatrix[i * 2]); |
---|
170 | PRINTF(0)("Drawing flare %i @ (%f, %f)\n", i, (this->flareVector * this->flareMatrix[i * 2]).x, (this->flareVector * this->flareMatrix[i * 2]).y); |
---|
171 | // tick them |
---|
172 | (*it)->tick(dt); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | /** |
---|
178 | * draws the LenseFlares |
---|
179 | */ |
---|
180 | void LenseFlare::draw() const |
---|
181 | { |
---|
182 | if( !this->bActivated) |
---|
183 | return; |
---|
184 | |
---|
185 | std::vector<Billboard*>::const_iterator it; |
---|
186 | for( it = flares.begin(); it != flares.end(); it++) |
---|
187 | (*it)->draw(); |
---|
188 | } |
---|