| 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 "util/loading/load_param.h" | 
|---|
| 21 | #include "util/loading/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 |  | 
|---|
| 30 | CREATE_FACTORY(SpriteParticles, CL_SPRITE_PARTICLES); | 
|---|
| 31 |  | 
|---|
| 32 | SHELL_COMMAND(texture, SpriteParticles, setMaterialTexture) | 
|---|
| 33 | ->defaultValues("maps/evil-flower.png"); | 
|---|
| 34 |  | 
|---|
| 35 | using 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 | */ | 
|---|
| 42 | SpriteParticles::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 | */ | 
|---|
| 52 | SpriteParticles::SpriteParticles(const TiXmlElement* root) | 
|---|
| 53 | { | 
|---|
| 54 | this->init(); | 
|---|
| 55 | if (root != NULL) | 
|---|
| 56 | this->loadParams(root); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | /** | 
|---|
| 60 | *  standard deconstructor | 
|---|
| 61 | */ | 
|---|
| 62 | SpriteParticles::~SpriteParticles() | 
|---|
| 63 | { } | 
|---|
| 64 |  | 
|---|
| 65 | /** | 
|---|
| 66 | * @brief initializes the SpriteParticles with default values | 
|---|
| 67 | */ | 
|---|
| 68 | void SpriteParticles::init() | 
|---|
| 69 | { | 
|---|
| 70 | this->setClassID(CL_SPRITE_PARTICLES, "SpriteParticles"); | 
|---|
| 71 |  | 
|---|
| 72 | this->material.setDiffuseMap("maps/radial-trans-noise.png"); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | /** | 
|---|
| 77 | * loads Parameters from a TiXmlElement | 
|---|
| 78 | * @param root the XML-element to load from. | 
|---|
| 79 | */ | 
|---|
| 80 | void SpriteParticles::loadParams(const TiXmlElement* root) | 
|---|
| 81 | { | 
|---|
| 82 | ParticleSystem::loadParams(root); | 
|---|
| 83 |  | 
|---|
| 84 | LoadParam(root, "texture", this, SpriteParticles, setMaterialTexture); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | /** | 
|---|
| 88 | * @brief sets the Texutre that is placed onto the particles | 
|---|
| 89 | * @param textureFile the Texture to load onto these SpriteParticles | 
|---|
| 90 | */ | 
|---|
| 91 | void SpriteParticles::setMaterialTexture(const std::string& textureFile) | 
|---|
| 92 | { | 
|---|
| 93 | this->material.setDiffuseMap(textureFile); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | /** | 
|---|
| 97 | * @brief draws all the Particles of this System | 
|---|
| 98 | * | 
|---|
| 99 | * The Cases in this Function all do the same: | 
|---|
| 100 | * Drawing all the particles with the appropriate Type. | 
|---|
| 101 | * This is just the fastest Way to do this, but will most likely be changed in the future. | 
|---|
| 102 | */ | 
|---|
| 103 | void SpriteParticles::draw() const | 
|---|
| 104 | { | 
|---|
| 105 |  | 
|---|
| 106 | Particle* drawPart = particles; | 
|---|
| 107 | this->material.select(); | 
|---|
| 108 |  | 
|---|
| 109 | GLboolean checkLight = false; | 
|---|
| 110 | glGetBooleanv(GL_LIGHTING, &checkLight); | 
|---|
| 111 | glPushAttrib(GL_LIGHTING_BIT); | 
|---|
| 112 | if (checkLight == GL_TRUE) | 
|---|
| 113 | glDisable(GL_LIGHTING); | 
|---|
| 114 |  | 
|---|
| 115 | glDepthMask(GL_FALSE); | 
|---|
| 116 | /* | 
|---|
| 117 | glClearDepth(1.0); | 
|---|
| 118 | glDepthFunc(GL_LEQUAL); | 
|---|
| 119 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|---|
| 120 | glEnable(GL_BLEND); | 
|---|
| 121 | glAlphaFunc(GL_GREATER,0.1); | 
|---|
| 122 | glEnable(GL_ALPHA_TEST); | 
|---|
| 123 | glEnable(GL_TEXTURE_2D); | 
|---|
| 124 | glEnable(GL_CULL_FACE); | 
|---|
| 125 | */ | 
|---|
| 126 | //glDepthMask(GL_FALSE); | 
|---|
| 127 | //glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA); | 
|---|
| 128 |  | 
|---|
| 129 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE); | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 | while (likely(drawPart != NULL)) | 
|---|
| 133 | { | 
|---|
| 134 | glColor4fv(drawPart->color); | 
|---|
| 135 | //! @todo implement a faster code for the look-at Camera algorithm. | 
|---|
| 136 |  | 
|---|
| 137 | const PNode* camera = State::getCameraNode();  //!< @todo MUST be different | 
|---|
| 138 | Vector cameraPos = camera->getAbsCoor(); | 
|---|
| 139 | Vector cameraTargetPos = State::getCameraTargetNode()->getAbsCoor(); | 
|---|
| 140 | Vector view = cameraTargetPos - cameraPos; | 
|---|
| 141 | Vector up = Vector(0, 1, 0); | 
|---|
| 142 | up = camera->getAbsDir().apply(up); | 
|---|
| 143 | Vector h = up.cross(view); | 
|---|
| 144 | Vector v = h.cross(view); | 
|---|
| 145 | h.normalize(); | 
|---|
| 146 | v.normalize(); | 
|---|
| 147 | v *= .5 * drawPart->radius; | 
|---|
| 148 | h *= .5 * drawPart->radius; | 
|---|
| 149 |  | 
|---|
| 150 | glBegin(GL_TRIANGLE_STRIP); | 
|---|
| 151 | glTexCoord2i(1, 1); | 
|---|
| 152 | glVertex3f(drawPart->position.x - h.x - v.x, | 
|---|
| 153 | drawPart->position.y - h.y - v.y, | 
|---|
| 154 | drawPart->position.z - h.z - v.z); | 
|---|
| 155 | glTexCoord2i(0, 1); | 
|---|
| 156 | glVertex3f(drawPart->position.x - h.x + v.x, | 
|---|
| 157 | drawPart->position.y - h.y + v.y, | 
|---|
| 158 | drawPart->position.z - h.z + v.z); | 
|---|
| 159 | glTexCoord2i(1, 0); | 
|---|
| 160 | glVertex3f(drawPart->position.x + h.x - v.x, | 
|---|
| 161 | drawPart->position.y + h.y - v.y, | 
|---|
| 162 | drawPart->position.z + h.z - v.z); | 
|---|
| 163 | glTexCoord2i(0, 0); | 
|---|
| 164 | glVertex3f(drawPart->position.x + h.x + v.x, | 
|---|
| 165 | drawPart->position.y + h.y + v.y, | 
|---|
| 166 | drawPart->position.z + h.z + v.z); | 
|---|
| 167 |  | 
|---|
| 168 | glEnd(); | 
|---|
| 169 |  | 
|---|
| 170 | drawPart = drawPart->next; | 
|---|
| 171 | } | 
|---|
| 172 | glDepthMask(GL_TRUE); | 
|---|
| 173 | glPopAttrib(); | 
|---|
| 174 | } | 
|---|