Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/effects/wobblegrid.cc @ 10556

Last change on this file since 10556 was 10556, checked in by bknecht, 17 years ago

wobblegrid orient improved

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