Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10695 was 10618, checked in by bknecht, 18 years ago

merged cleanup into trunk (only improvements)

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 "tools/cameraman.h"
27#include "tools/camera.h"
28
29#include <cassert>
30#include "debug.h"
31
32
33
34ObjectListDefinition(Wobblegrid);
35CREATE_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 */
46Wobblegrid::Wobblegrid (const TiXmlElement* root)
47{
48  this->size  = 5;
49
50  this->init();
51
52  if( root)
53    this->loadParams(root);
54}
55
56Wobblegrid::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 */
70Wobblegrid::~Wobblegrid ()
71{
72  if (this->material)
73    delete this->material;
74}
75
76
77/**
78 * initializes the Wobblegrid
79 */
80void 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->angle = 0;
97
98  this->setUpdateFunction((*sinf));
99}
100
101
102/**
103 *  load params
104 * @param root TiXmlElement object
105 */
106void 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 */
121void Wobblegrid::setTexture(const std::string& textureFile)
122{
123  this->material->setBlendFunc(GL_SRC_ALPHA,GL_ONE);
124  this->material->setDiffuseMap(textureFile);
125}
126
127
128/**
129 * ticks the Wobblegrid
130 * @param dt the time to ticks
131 */
132void 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);
148      vec.= (x * fac + sgn(x-2)*updateWobble(angle)*fac/2.0);
149      vec.= (z * fac + sgn(z-2)*updateWobble(angle)*fac/2.0);
150    }
151  }
152  //this->grid->finalize();
153  this->orient();
154}
155
156
157/**
158 * draws the billboard
159 */
160void Wobblegrid::draw() const
161{
162
163//   this->material->select();
164//   WorldEntity::draw();
165//   return;
166
167
168  if( !this->isVisible())
169    return;
170
171  glPushAttrib(GL_ENABLE_BIT);
172  glDisable(GL_LIGHTING);
173  glDisable(GL_FOG);
174
175  glMatrixMode(GL_MODELVIEW);
176  glPushMatrix();
177
178  //glTranslatef(this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z);
179  //glTranslatef(0,0,0);
180  this->material->select();
181
182  glDisable(GL_BLEND);
183  glEnable( GL_ALPHA_TEST);
184  glAlphaFunc( GL_GEQUAL, .5);
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}
202
203void Wobblegrid::orient()
204{
205
206  Camera* c = State::getCamera();
207
208  Vector view = this->getAbsCoor() - c->getAbsCoor();
209//   view.normalize();
210
211  Vector up = Vector(0, 1, 0);
212//   if ( up.dot(view) == 0 )
213//     up = Vector(1, 0, 0);
214//   if ( up.dot(view) == 0 )
215//     up = Vector (0, 0, 1);
216
217  Vector h = up.cross(view.getNormalized());
218  up = h.cross(view.getNormalized());
219  Quaternion dir = Quaternion::lookAt( this->getAbsCoor(), this->getAbsCoor() + up, view);
220//   Quaternion dir = Quaternion::lookAt( Vector(), view, up);
221  this->setAbsDir(dir);
222}
Note: See TracBrowser for help on using the repository browser.