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 "wobblegrid.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 "grid.h" |
---|
25 | |
---|
26 | #include <assert.h> |
---|
27 | #include "debug.h" |
---|
28 | |
---|
29 | |
---|
30 | #include "class_id_DEPRECATED.h" |
---|
31 | ObjectListDefinition(Wobblegrid); |
---|
32 | CREATE_FACTORY(Wobblegrid); |
---|
33 | |
---|
34 | /** |
---|
35 | * Wobble Grids are grids which "wobble" |
---|
36 | * Wobbling is realized through fixed center and sin wave outwards |
---|
37 | * For the beginning the grid will be a 5x5 |
---|
38 | */ |
---|
39 | |
---|
40 | /** |
---|
41 | * standart constructor |
---|
42 | */ |
---|
43 | Wobblegrid::Wobblegrid (const TiXmlElement* root) |
---|
44 | { |
---|
45 | this->size = 5; |
---|
46 | |
---|
47 | this->init(); |
---|
48 | |
---|
49 | if( root) |
---|
50 | this->loadParams(root); |
---|
51 | } |
---|
52 | |
---|
53 | Wobblegrid::Wobblegrid (int size, const TiXmlElement* root) |
---|
54 | { |
---|
55 | this->size = size; |
---|
56 | |
---|
57 | this->init(); |
---|
58 | |
---|
59 | if( root) |
---|
60 | this->loadParams(root); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | * destroys a Wobblegrid |
---|
66 | */ |
---|
67 | Wobblegrid::~Wobblegrid () |
---|
68 | { |
---|
69 | if (this->material) |
---|
70 | delete this->material; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | /** |
---|
75 | * initializes the Wobblegrid |
---|
76 | */ |
---|
77 | void Wobblegrid::init() |
---|
78 | { |
---|
79 | this->registerObject(this, Wobblegrid::_objectList); |
---|
80 | this->setName("Wobblegrid"); |
---|
81 | |
---|
82 | this->toList(OM_COMMON); |
---|
83 | |
---|
84 | this->material = new Material(); |
---|
85 | this->setAbsCoor(0, 0, 0); |
---|
86 | //this->setVisibiliy(true); |
---|
87 | |
---|
88 | this->subdivision = 5; |
---|
89 | |
---|
90 | this->grid = new Grid( this->size, this->size, this->subdivision, this->subdivision); |
---|
91 | this->angularSpeed = M_PI; //180; |
---|
92 | this->setModel(this->grid); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | * load params |
---|
98 | * @param root TiXmlElement object |
---|
99 | */ |
---|
100 | void Wobblegrid::loadParams(const TiXmlElement* root) |
---|
101 | { |
---|
102 | /*LoadParam(root, "texture", this->material, Material, setDiffuseMap) |
---|
103 | .describe("the texture-file to load onto the Wobblegrid"); |
---|
104 | |
---|
105 | LoadParam(root, "size", this, Wobblegrid, setSize) |
---|
106 | .describe("the size of the Wobblegrid in Pixels");*/ |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | /** |
---|
112 | * sets the material to load |
---|
113 | * @param textureFile The texture-file to load |
---|
114 | */ |
---|
115 | void Wobblegrid::setTexture(const std::string& textureFile) |
---|
116 | { |
---|
117 | this->material->setDiffuseMap(textureFile); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | /** |
---|
122 | * ticks the Wobblegrid |
---|
123 | * @param dt the time to ticks |
---|
124 | */ |
---|
125 | void Wobblegrid::tick(float dt) |
---|
126 | { |
---|
127 | angle += dt * angularSpeed; |
---|
128 | if (angle > 2 * M_PI) |
---|
129 | angle -= 2 * M_PI; |
---|
130 | |
---|
131 | //Vector vec; |
---|
132 | float fac = 1.0 / (this->subdivision - 1); |
---|
133 | for( int z=1; z < 4; z++) |
---|
134 | { |
---|
135 | for( int x=1; x < 4; x++) |
---|
136 | { |
---|
137 | //if(x==2 && z == 2) |
---|
138 | // continue; |
---|
139 | |
---|
140 | Vector2D& vec = this->grid->texCoord(z*this->subdivision + x); |
---|
141 | vec.y = (x * fac + sgn(x-2)*sinf(angle)*fac/2.0); |
---|
142 | vec.x = (z * fac + sgn(z-2)*sinf(angle)*fac/2.0); |
---|
143 | } |
---|
144 | } |
---|
145 | //this->grid->finalize(); |
---|
146 | |
---|
147 | Vector view = this->getAbsCoor() - State::getCameraNode()->getAbsCoor(); |
---|
148 | view.normalize(); |
---|
149 | |
---|
150 | Vector up = Vector(0, 1, 0); |
---|
151 | Vector h = up.cross(view); |
---|
152 | up = h.cross(view); |
---|
153 | |
---|
154 | Quaternion dir = Quaternion::lookAt( this->getAbsCoor(), this->getAbsCoor() + up, view); |
---|
155 | this->setAbsDir(dir); |
---|
156 | |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | * draws the billboard |
---|
162 | */ |
---|
163 | void Wobblegrid::draw() const |
---|
164 | { |
---|
165 | |
---|
166 | // this->material->select(); |
---|
167 | // WorldEntity::draw(); |
---|
168 | // return; |
---|
169 | |
---|
170 | |
---|
171 | if( !this->isVisible()) |
---|
172 | return; |
---|
173 | |
---|
174 | glPushAttrib(GL_ENABLE_BIT); |
---|
175 | glDisable(GL_LIGHTING); |
---|
176 | glDisable(GL_FOG); |
---|
177 | |
---|
178 | glMatrixMode(GL_MODELVIEW); |
---|
179 | glPushMatrix(); |
---|
180 | |
---|
181 | //glTranslatef(this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z); |
---|
182 | //glTranslatef(0,0,0); |
---|
183 | this->material->select(); |
---|
184 | |
---|
185 | |
---|
186 | glTranslatef (this->getAbsCoor ().x, |
---|
187 | this->getAbsCoor ().y, |
---|
188 | this->getAbsCoor ().z); |
---|
189 | |
---|
190 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
191 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
192 | |
---|
193 | //Quaternion dir = Quaternion(this->getAbsDir().getSpacialAxisAngle(),view); |
---|
194 | //this->setAbsDir(dir); |
---|
195 | //glRotatef(this->angle, 0.0, 0.0, 1.0); |
---|
196 | this->grid->draw(); |
---|
197 | |
---|
198 | glPopMatrix(); |
---|
199 | |
---|
200 | glPopAttrib(); |
---|
201 | } |
---|