| 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 "cameraman.h" | 
|---|
| 27 | #include "camera.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include <assert.h> | 
|---|
| 30 | #include "debug.h" | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 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(); | 
|---|
| 86 |   this->material->setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|---|
| 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; | 
|---|
| 94 |   this->setModel(this->grid); | 
|---|
| 95 |  | 
|---|
| 96 |   this->setUpdateFunction((*sinf)); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 |  | 
|---|
| 100 | /** | 
|---|
| 101 |  *  load params | 
|---|
| 102 |  * @param root TiXmlElement object | 
|---|
| 103 |  */ | 
|---|
| 104 | void Wobblegrid::loadParams(const TiXmlElement* root) | 
|---|
| 105 | { | 
|---|
| 106 |   /*LoadParam(root, "texture", this->material, Material, setDiffuseMap) | 
|---|
| 107 |       .describe("the texture-file to load onto the Wobblegrid"); | 
|---|
| 108 |  | 
|---|
| 109 |   LoadParam(root, "size", this, Wobblegrid, setSize) | 
|---|
| 110 |   .describe("the size of the Wobblegrid in Pixels");*/ | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 | /** | 
|---|
| 116 |  * sets the material to load | 
|---|
| 117 |  * @param textureFile The texture-file to load | 
|---|
| 118 |  */ | 
|---|
| 119 | void Wobblegrid::setTexture(const std::string& textureFile) | 
|---|
| 120 | { | 
|---|
| 121 |   this->material->setBlendFunc(GL_SRC_ALPHA,GL_ONE); | 
|---|
| 122 |   this->material->setDiffuseMap(textureFile); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 |  | 
|---|
| 126 | /** | 
|---|
| 127 |  * ticks the Wobblegrid | 
|---|
| 128 |  * @param dt the time to ticks | 
|---|
| 129 |  */ | 
|---|
| 130 | void Wobblegrid::tick(float dt) | 
|---|
| 131 | { | 
|---|
| 132 |   angle += dt * angularSpeed; | 
|---|
| 133 |   if (angle > 2 * M_PI) | 
|---|
| 134 |     angle -= 2 * M_PI; | 
|---|
| 135 |  | 
|---|
| 136 |   //Vector vec; | 
|---|
| 137 |   float fac = 1.0 / (this->subdivision - 1); | 
|---|
| 138 |   for( int z=1; z < 4; z++) | 
|---|
| 139 |   { | 
|---|
| 140 |     for( int x=1; x < 4; x++) | 
|---|
| 141 |     { | 
|---|
| 142 |       //if(x==2 && z == 2) | 
|---|
| 143 |       //  continue; | 
|---|
| 144 |  | 
|---|
| 145 |       Vector2D& vec = this->grid->texCoord(z*this->subdivision + x); | 
|---|
| 146 |       vec.y  = (x * fac + sgn(x-2)*updateWobble(angle)*fac/2.0); | 
|---|
| 147 |       vec.x  = (z * fac + sgn(z-2)*updateWobble(angle)*fac/2.0); | 
|---|
| 148 |     } | 
|---|
| 149 |   } | 
|---|
| 150 |   //this->grid->finalize(); | 
|---|
| 151 |   this->orient(); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | /** | 
|---|
| 156 |  * draws the billboard | 
|---|
| 157 |  */ | 
|---|
| 158 | void Wobblegrid::draw() const | 
|---|
| 159 | { | 
|---|
| 160 |  | 
|---|
| 161 | //   this->material->select(); | 
|---|
| 162 | //   WorldEntity::draw(); | 
|---|
| 163 | //   return; | 
|---|
| 164 |  | 
|---|
| 165 |  | 
|---|
| 166 |   if( !this->isVisible()) | 
|---|
| 167 |     return; | 
|---|
| 168 |  | 
|---|
| 169 |   glPushAttrib(GL_ENABLE_BIT); | 
|---|
| 170 |   glDisable(GL_LIGHTING); | 
|---|
| 171 |   glDisable(GL_FOG); | 
|---|
| 172 |  | 
|---|
| 173 |   glMatrixMode(GL_MODELVIEW); | 
|---|
| 174 |   glPushMatrix(); | 
|---|
| 175 |  | 
|---|
| 176 |   //glTranslatef(this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z); | 
|---|
| 177 |   //glTranslatef(0,0,0); | 
|---|
| 178 |   this->material->select(); | 
|---|
| 179 |  | 
|---|
| 180 |   glDisable(GL_BLEND); | 
|---|
| 181 |   glEnable( GL_ALPHA_TEST); | 
|---|
| 182 |   glAlphaFunc( GL_GEQUAL, .5); | 
|---|
| 183 |  | 
|---|
| 184 |   glTranslatef (this->getAbsCoor ().x, | 
|---|
| 185 |                   this->getAbsCoor ().y, | 
|---|
| 186 |                   this->getAbsCoor ().z); | 
|---|
| 187 |  | 
|---|
| 188 |   Vector tmpRot = this->getAbsDir().getSpacialAxis(); | 
|---|
| 189 |   glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); | 
|---|
| 190 |  | 
|---|
| 191 |   //Quaternion  dir = Quaternion(this->getAbsDir().getSpacialAxisAngle(),view); | 
|---|
| 192 |   //this->setAbsDir(dir); | 
|---|
| 193 |   //glRotatef(this->angle, 0.0, 0.0, 1.0); | 
|---|
| 194 |   this->grid->draw(); | 
|---|
| 195 |  | 
|---|
| 196 |   glPopMatrix(); | 
|---|
| 197 |  | 
|---|
| 198 |   glPopAttrib(); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | void Wobblegrid::orient() | 
|---|
| 202 | { | 
|---|
| 203 |       | 
|---|
| 204 |      CameraMan* cman = State::getCameraman(); | 
|---|
| 205 |      Camera* c = cman->getCurrentCam(); | 
|---|
| 206 |  | 
|---|
| 207 |   Vector view = this->getAbsCoor() - c->getAbsCoor(); | 
|---|
| 208 | //   view.normalize(); | 
|---|
| 209 |  | 
|---|
| 210 |   Vector up = Vector(0, 1, 0); | 
|---|
| 211 | //   if ( up.dot(view) == 0 ) | 
|---|
| 212 | //     up = Vector(1, 0, 0); | 
|---|
| 213 | //   if ( up.dot(view) == 0 ) | 
|---|
| 214 | //     up = Vector (0, 0, 1); | 
|---|
| 215 |  | 
|---|
| 216 |   Vector h = up.cross(view.getNormalized()); | 
|---|
| 217 |   up = h.cross(view.getNormalized()); | 
|---|
| 218 |   Quaternion dir = Quaternion::lookAt( this->getAbsCoor(), this->getAbsCoor() + up, view); | 
|---|
| 219 | //   Quaternion dir = Quaternion::lookAt( Vector(), view, up); | 
|---|
| 220 |   this->setAbsDir(dir); | 
|---|
| 221 | } | 
|---|