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