Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/particles/sprite_particles.cc @ 6621

Last change on this file since 6621 was 6621, checked in by bensch, 18 years ago

orxonox/trunk: particle derivate… still working on this

File size: 4.5 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 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: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
17
18#include "sprite_particles.h"
19
20#include "load_param.h"
21#include "factory.h"
22#include "material.h"
23#include "state.h"
24#include "shell_command.h"
25
26#include "parser/tinyxml/tinyxml.h"
27#include <algorithm>
28
29
30CREATE_FACTORY(SpriteParticles, CL_SPRITE_PARTICLES);
31
32SHELL_COMMAND(texture, SpriteParticles, setMaterialTexture)
33    ->defaultValues(1, "maps/evil-flower.png");
34
35using namespace std;
36
37/**
38 *  standard constructor
39 * @param maxCount the Count of particles in the System
40 * @param type The Type of the SpriteParticles
41*/
42SpriteParticles::SpriteParticles (unsigned int maxCount)
43  : ParticleSystem(maxCount)
44{
45  this->init();
46}
47
48/**
49 * @brief creates a Particle System out of a XML-element
50 * @param root: the XML-element to load from
51 */
52SpriteParticles::SpriteParticles(const TiXmlElement* root)
53{
54  this->init();
55
56  this->loadParams(root);
57}
58
59/**
60 *  standard deconstructor
61*/
62SpriteParticles::~SpriteParticles()
63{
64  // deleting all the living Particles
65  while (this->particles)
66  {
67    Particle* tmpDelPart = this->particles;
68    this->particles = this->particles->next;
69    delete tmpDelPart;
70  }
71
72  // deleting all the dead particles
73  while (this->deadList)
74  {
75    Particle* tmpDelPart = this->deadList;
76    this->deadList = this->deadList->next;
77    delete tmpDelPart;
78  }
79}
80
81/**
82 * @brief initializes the SpriteParticles with default values
83*/
84void SpriteParticles::init()
85{
86  this->setClassID(CL_SPRITE_PARTICLES, "SpriteParticles");
87
88  this->material = NULL;
89}
90
91
92/**
93 * loads Parameters from a TiXmlElement
94 * @param root the XML-element to load from.
95 */
96void SpriteParticles::loadParams(const TiXmlElement* root)
97{
98  SpriteParticles::loadParams(root);
99
100  LoadParam(root, "texture", this, SpriteParticles, setMaterialTexture);
101}
102
103// setting properties
104/**
105 * @brief sets the material to an external material
106 * @param material: the material to set this material to.
107 *
108 * !! important if the extern material gets deleted it MUST be unregistered here or segfault !!
109*/
110void SpriteParticles::setMaterial(Material* material)
111{
112  this->material = *material;
113}
114
115void SpriteParticles::setMaterialTexture(const char* textureFile)
116{
117  this->material.setDiffuseMap(textureFile);
118}
119
120/**
121 * @brief draws all the Particles of this System
122 *
123 * The Cases in this Function all do the same:
124 * Drawing all the particles with the appropriate Type.
125 * This is just the fastest Way to do this, but will most likely be changed in the future.
126 */
127void SpriteParticles::draw() const
128{
129  glPushAttrib(GL_ENABLE_BIT);
130
131  Particle* drawPart = particles;
132
133  glDisable(GL_LIGHTING);
134  glMatrixMode(GL_MODELVIEW);
135  glDepthMask(GL_FALSE);
136
137  material.select();
138  glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
139
140  //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE);
141
142
143  while (likely(drawPart != NULL))
144  {
145    glColor4fv(drawPart->color);
146    //! @todo implement a faster code for the look-at Camera algorithm.
147
148    const PNode* camera = State::getCamera();  //!< @todo MUST be different
149    Vector cameraPos = camera->getAbsCoor();
150    Vector cameraTargetPos = State::getCameraTarget()->getAbsCoor();
151    Vector view = cameraTargetPos - cameraPos;
152    Vector up = Vector(0, 1, 0);
153    up = camera->getAbsDir().apply(up);
154    Vector h = up.cross(view);
155    Vector v = h.cross(view);
156    h.normalize();
157    v.normalize();
158    v *= .5 * drawPart->radius;
159    h *= .5 * drawPart->radius;
160
161    glBegin(GL_TRIANGLE_STRIP);
162    glTexCoord2i(1, 1);
163    glVertex3f(drawPart->position.x - h.x - v.x,
164               drawPart->position.y - h.y - v.y,
165               drawPart->position.z - h.z - v.z);
166    glTexCoord2i(0, 1);
167    glVertex3f(drawPart->position.x - h.x + v.x,
168               drawPart->position.y - h.y + v.y,
169               drawPart->position.z - h.z + v.z);
170    glTexCoord2i(1, 0);
171    glVertex3f(drawPart->position.x + h.x - v.x,
172               drawPart->position.y + h.y - v.y,
173               drawPart->position.z + h.z - v.z);
174    glTexCoord2i(0, 0);
175    glVertex3f(drawPart->position.x + h.x + v.x,
176               drawPart->position.y + h.y + v.y,
177               drawPart->position.z + h.z + v.z);
178
179    glEnd();
180
181    drawPart = drawPart->next;
182  }
183  glDepthMask(GL_TRUE);
184  glPopAttrib();
185}
Note: See TracBrowser for help on using the repository browser.