1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 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: Patrick Boenzli |
---|
13 | */ |
---|
14 | |
---|
15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
16 | |
---|
17 | #include "billboard.h" |
---|
18 | |
---|
19 | #include "load_param.h" |
---|
20 | #include "factory.h" |
---|
21 | |
---|
22 | #include "graphics_engine.h" |
---|
23 | #include "material.h" |
---|
24 | #include "glincl.h" |
---|
25 | #include "state.h" |
---|
26 | #include "p_node.h" |
---|
27 | |
---|
28 | |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | |
---|
32 | CREATE_FACTORY(Billboard, CL_IMAGE_ENTITY); |
---|
33 | |
---|
34 | |
---|
35 | /** |
---|
36 | * standart constructor |
---|
37 | */ |
---|
38 | Billboard::Billboard (const TiXmlElement* root) |
---|
39 | { |
---|
40 | this->init(); |
---|
41 | |
---|
42 | if( root) |
---|
43 | this->loadParams(root); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | /** |
---|
48 | * destroys a Billboard |
---|
49 | */ |
---|
50 | Billboard::~Billboard () |
---|
51 | { |
---|
52 | if (this->material) |
---|
53 | delete this->material; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | * initializes the Billboard |
---|
59 | */ |
---|
60 | void Billboard::init() |
---|
61 | { |
---|
62 | this->setClassID(CL_BILLBOARD, "Billboard"); |
---|
63 | this->setName("Billboard"); |
---|
64 | |
---|
65 | this->setLayer(E2D_LAYER_TOP); |
---|
66 | this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0, GraphicsEngine::getInstance()->getResolutionY()/10.0); |
---|
67 | |
---|
68 | //this->setBindNode(this); |
---|
69 | this->material = new Material(); |
---|
70 | this->setTexture("pictures/error_texture.png"); |
---|
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, Billboard, setTexture) |
---|
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->setSize2D(sizeX, sizeY); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | /** |
---|
99 | * sets the material to load |
---|
100 | * @param textureFile The texture-file to load onto the crosshair |
---|
101 | */ |
---|
102 | void Billboard::setTexture(const char* textureFile) |
---|
103 | { |
---|
104 | this->material->setDiffuseMap(textureFile); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /** |
---|
109 | * attaches this billboard to a parent |
---|
110 | * @param pNode node to attach to |
---|
111 | */ |
---|
112 | void Billboard::attachTo(PNode* pNode) |
---|
113 | { |
---|
114 | this->source->setParent(pNode); |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | /** |
---|
119 | * ticks the Billboard |
---|
120 | * @param dt the time to ticks |
---|
121 | */ |
---|
122 | void Billboard::tick(float dt) |
---|
123 | { |
---|
124 | float z = 0.0f; |
---|
125 | glReadPixels ((int)this->getAbsCoor2D().x, |
---|
126 | GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, |
---|
127 | 1, |
---|
128 | 1, |
---|
129 | GL_DEPTH_COMPONENT, |
---|
130 | GL_FLOAT, |
---|
131 | &z); |
---|
132 | |
---|
133 | GLdouble objX=.0, objY=.0, objZ=.0; |
---|
134 | gluUnProject(this->getAbsCoor2D().x, |
---|
135 | GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, |
---|
136 | .99, // z |
---|
137 | GraphicsEngine::modMat, |
---|
138 | GraphicsEngine::projMat, |
---|
139 | GraphicsEngine::viewPort, |
---|
140 | &objX, |
---|
141 | &objY, |
---|
142 | &objZ ); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | /** |
---|
147 | * draws the billboard |
---|
148 | */ |
---|
149 | void Billboard::draw() const |
---|
150 | { |
---|
151 | glPushMatrix(); |
---|
152 | |
---|
153 | glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); |
---|
154 | this->material->select(); |
---|
155 | |
---|
156 | glBegin(GL_TRIANGLE_STRIP); |
---|
157 | glTexCoord2f(0, 0); |
---|
158 | glVertex2f(-this->getSizeX2D(), -this->getSizeY2D()); |
---|
159 | glTexCoord2f(1, 0); |
---|
160 | glVertex2f(this->getSizeX2D(), -this->getSizeY2D()); |
---|
161 | glTexCoord2f(0, 1); |
---|
162 | glVertex2f(-this->getSizeX2D(), this->getSizeY2D()); |
---|
163 | glTexCoord2f(1, 1); |
---|
164 | glVertex2f(this->getSizeX2D(), this->getSizeY2D()); |
---|
165 | glEnd(); |
---|
166 | |
---|
167 | glPopMatrix(); |
---|
168 | } |
---|