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