1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2006 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: |
---|
12 | main-programmer: David Hasenfratz |
---|
13 | co-programmer: Nicolas Schlumberger |
---|
14 | */ |
---|
15 | |
---|
16 | #include "billboard.h" |
---|
17 | |
---|
18 | #include "util/loading/load_param.h" |
---|
19 | #include "util/loading/factory.h" |
---|
20 | |
---|
21 | #include "graphics_engine.h" |
---|
22 | #include "material.h" |
---|
23 | #include "glincl.h" |
---|
24 | #include "state.h" |
---|
25 | #include "tools/cameraman.h" |
---|
26 | #include "tools/camera.h" |
---|
27 | |
---|
28 | #include "debug.h" |
---|
29 | |
---|
30 | |
---|
31 | ObjectListDefinition(Billboard); |
---|
32 | CREATE_FACTORY(Billboard); |
---|
33 | |
---|
34 | /** |
---|
35 | * standart constructor |
---|
36 | */ |
---|
37 | Billboard::Billboard (const TiXmlElement* root) |
---|
38 | { |
---|
39 | this->init(); |
---|
40 | |
---|
41 | if( root) |
---|
42 | this->loadParams(root); |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | /** |
---|
47 | * destroys a Billboard |
---|
48 | */ |
---|
49 | Billboard::~Billboard () |
---|
50 | { |
---|
51 | if (this->material) |
---|
52 | delete this->material; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | * initializes the Billboard |
---|
58 | */ |
---|
59 | void Billboard::init() |
---|
60 | { |
---|
61 | this->registerObject(this, Billboard::_objectList); |
---|
62 | this->setName("Billboard"); |
---|
63 | |
---|
64 | this->toList(OM_COMMON); |
---|
65 | |
---|
66 | this->material = new Material(); |
---|
67 | this->setAbsCoor(0, 0, 0); |
---|
68 | //this->setVisibiliy(true); |
---|
69 | this->setSize(5, 5); |
---|
70 | |
---|
71 | this->texColor = NULL; |
---|
72 | |
---|
73 | this->pulseMagnitude = .5; |
---|
74 | this->bPulse = false; |
---|
75 | |
---|
76 | this->angularSpeed = M_2_PI; //360; |
---|
77 | this->angle = 0; |
---|
78 | |
---|
79 | this->setUpdateFunction((*sinf)); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | /** |
---|
84 | * load params |
---|
85 | * @param root TiXmlElement object |
---|
86 | */ |
---|
87 | void Billboard::loadParams(const TiXmlElement* root) |
---|
88 | { |
---|
89 | /*LoadParam(root, "texture", this->material, Material, setDiffuseMap) |
---|
90 | .describe("the texture-file to load onto the Billboard"); |
---|
91 | |
---|
92 | LoadParam(root, "size", this, Billboard, setSize) |
---|
93 | .describe("the size of the Billboard in Pixels");*/ |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | /** |
---|
98 | * sets the size of the Billboard. |
---|
99 | * @param size the size in pixels |
---|
100 | */ |
---|
101 | void Billboard::setSize(float sizeX, float sizeY) |
---|
102 | { |
---|
103 | this->sizeX = sizeX; |
---|
104 | this->sizeY = sizeY; |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /** |
---|
109 | * sets the material to load |
---|
110 | * @param textureFile The texture-file to load |
---|
111 | */ |
---|
112 | void Billboard::setTexture(const std::string& textureFile) |
---|
113 | { |
---|
114 | this->material->setDiffuseMap(textureFile); |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | /** |
---|
119 | * ticks the Billboard |
---|
120 | * @param dt the time to ticks |
---|
121 | */ |
---|
122 | void Billboard::tick(float dt) |
---|
123 | { |
---|
124 | this->angle += dt * this->angularSpeed; |
---|
125 | if (this->angle > M_2_PI) |
---|
126 | this->angle -= M_2_PI; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /** |
---|
131 | * draws the billboard |
---|
132 | */ |
---|
133 | void Billboard::draw() const |
---|
134 | { |
---|
135 | if( !this->isVisible()) |
---|
136 | return; |
---|
137 | |
---|
138 | glPushAttrib(GL_ENABLE_BIT); |
---|
139 | glDisable(GL_LIGHTING); |
---|
140 | glDisable(GL_FOG); |
---|
141 | |
---|
142 | glPushMatrix(); |
---|
143 | |
---|
144 | //glTranslatef(this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z); |
---|
145 | //glTranslatef(0,0,0); |
---|
146 | this->material->select(); |
---|
147 | |
---|
148 | const Camera* camera = State::getCamera(); |
---|
149 | Vector cameraPos = camera->getAbsCoor(); |
---|
150 | Vector cameraTargetPos = camera->getTarget()->getAbsCoor(); |
---|
151 | Vector view = cameraTargetPos - cameraPos; |
---|
152 | Vector up = Vector(0, 1, 0); |
---|
153 | up = camera->getAbsDir().apply(up); |
---|
154 | Vector h = up.cross(view); |
---|
155 | Vector v = h.cross(view); |
---|
156 | h.normalize(); |
---|
157 | v.normalize(); |
---|
158 | |
---|
159 | float tmp = 1; |
---|
160 | if (this->bPulse) |
---|
161 | tmp += this->pulseMagnitude * updatePulse(this->angle); |
---|
162 | |
---|
163 | v *= sizeX * tmp; |
---|
164 | h *= sizeY * tmp; |
---|
165 | |
---|
166 | //v += this->getAbsCoor(); |
---|
167 | //PRINTF(0)("sizeX: %f sizeY: %f\n", sizeX, sizeY); |
---|
168 | |
---|
169 | // changes the color of the texture (if any is set) |
---|
170 | if(this->texColor != NULL) |
---|
171 | glColor4ub((GLubyte)(this->texColor->r()*255.0f), (GLubyte)(this->texColor->g()*255.0f), (GLubyte)(this->texColor->b()*255.0f), (GLubyte)(this->texColor->a()*255)); |
---|
172 | |
---|
173 | glBegin(GL_QUADS); |
---|
174 | glTexCoord2f(0.0f, 0.0f); |
---|
175 | glVertex3f(this->getAbsCoor().x - h.x - v.x, |
---|
176 | this->getAbsCoor().y - h.y - v.y, |
---|
177 | this->getAbsCoor().z - h.z - v.z); |
---|
178 | glTexCoord2f(1.0f, 0.0f); |
---|
179 | glVertex3f( this->getAbsCoor().x + h.x - v.x, |
---|
180 | this->getAbsCoor().y + h.y - v.y, |
---|
181 | this->getAbsCoor().z + h.z - v.z); |
---|
182 | glTexCoord2f(1.0f, 1.0f); |
---|
183 | glVertex3f(this->getAbsCoor().x + h.x + v.x, |
---|
184 | this->getAbsCoor().y + h.y + v.y, |
---|
185 | this->getAbsCoor().z + h.z + v.z); |
---|
186 | glTexCoord2f(0.0f, 1.0f); |
---|
187 | glVertex3f(this->getAbsCoor().x - h.x + v.x, |
---|
188 | this->getAbsCoor().y - h.y + v.y, |
---|
189 | this->getAbsCoor().z - h.z + v.z); |
---|
190 | glEnd(); |
---|
191 | |
---|
192 | |
---|
193 | glPopMatrix(); |
---|
194 | |
---|
195 | glPopAttrib(); |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | /** |
---|
200 | * changes the texture color |
---|
201 | * @param col color for the texture |
---|
202 | */ |
---|
203 | void Billboard::colorTexture(const Color col) |
---|
204 | { |
---|
205 | this->texColor = new Color(col.r(), col.g(), col.b(), col.a()); |
---|
206 | } |
---|