Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/lib/particles/sprite_particles.cc @ 6639

Last change on this file since 6639 was 6628, checked in by bensch, 19 years ago

trunk: segfault-prevention, and minor cleanup

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  if (root != NULL)
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.setDiffuseMap("maps/radial-trans-noise.png");
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  ParticleSystem::loadParams(root);
99
100  LoadParam(root, "texture", this, SpriteParticles, setMaterialTexture);
101}
102
103/**
104 * @brief sets the Texutre that is placed onto the particles
105 * @param textureFile the Texture to load onto these SpriteParticles
106 */
107void SpriteParticles::setMaterialTexture(const char* textureFile)
108{
109  this->material.setDiffuseMap(textureFile);
110}
111
112/**
113 * @brief draws all the Particles of this System
114 *
115 * The Cases in this Function all do the same:
116 * Drawing all the particles with the appropriate Type.
117 * This is just the fastest Way to do this, but will most likely be changed in the future.
118 */
119void SpriteParticles::draw() const
120{
121  glPushAttrib(GL_ENABLE_BIT);
122
123  Particle* drawPart = particles;
124
125  GLboolean checkLight = false;
126  glGetBooleanv(GL_LIGHTING, &checkLight);
127  if (checkLight == GL_TRUE)
128    glDisable(GL_LIGHTING);
129  glMatrixMode(GL_MODELVIEW);
130  glDepthMask(GL_FALSE);
131
132  material.select();
133  glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
134
135  //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE);
136
137
138  while (likely(drawPart != NULL))
139  {
140    glColor4fv(drawPart->color);
141    //! @todo implement a faster code for the look-at Camera algorithm.
142
143    const PNode* camera = State::getCamera();  //!< @todo MUST be different
144    Vector cameraPos = camera->getAbsCoor();
145    Vector cameraTargetPos = State::getCameraTarget()->getAbsCoor();
146    Vector view = cameraTargetPos - cameraPos;
147    Vector up = Vector(0, 1, 0);
148    up = camera->getAbsDir().apply(up);
149    Vector h = up.cross(view);
150    Vector v = h.cross(view);
151    h.normalize();
152    v.normalize();
153    v *= .5 * drawPart->radius;
154    h *= .5 * drawPart->radius;
155
156    glBegin(GL_TRIANGLE_STRIP);
157    glTexCoord2i(1, 1);
158    glVertex3f(drawPart->position.x - h.x - v.x,
159               drawPart->position.y - h.y - v.y,
160               drawPart->position.z - h.z - v.z);
161    glTexCoord2i(0, 1);
162    glVertex3f(drawPart->position.x - h.x + v.x,
163               drawPart->position.y - h.y + v.y,
164               drawPart->position.z - h.z + v.z);
165    glTexCoord2i(1, 0);
166    glVertex3f(drawPart->position.x + h.x - v.x,
167               drawPart->position.y + h.y - v.y,
168               drawPart->position.z + h.z - v.z);
169    glTexCoord2i(0, 0);
170    glVertex3f(drawPart->position.x + h.x + v.x,
171               drawPart->position.y + h.y + v.y,
172               drawPart->position.z + h.z + v.z);
173
174    glEnd();
175
176    drawPart = drawPart->next;
177  }
178  glDepthMask(GL_TRUE);
179  glPopAttrib();
180}
Note: See TracBrowser for help on using the repository browser.