/* -*- Objc -*- */ /* * $Id: Particle.m,v 1.1 2003/07/15 13:59:10 madruon Exp $ * * Copyright (C) 2003 Free Software Foundation, Inc. * * This file is part of GNU Hégémonie. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include "ParticleSystem/Particle.h" /** * This class represents one particle. */ @implementation Particle /** * Initialise a new particle. */ - (id) initWithPosition: (coord_t)position direction: (coord_t)direction life: (double)life speed: (double)speed size: (double)size color: (NSColor *)color shininess: (double)shininess { NSParameterAssert (life >= 0.0); NSParameterAssert (size > 0.0); NSParameterAssert (color != nil); self = [super init]; if (self != nil) { _position = position; _direction = direction; _life = life; _speed = speed; _size = size; _color = color; _shininess = shininess; _isAlive = YES; } return self; } /** * Returns "true" if the particle is still alive (_life > 0.0) */ - (BOOL) isAlive { return _isAlive; } /** * Moves the particle in function of the elapsed time. * The life of the particule is also modified. */ - (void) move: (double)elapsedTime { _position.x = _position.x + (_direction.x * elapsedTime * _speed); _position.y = _position.y + (_direction.y * elapsedTime * _speed); _position.z = _position.z + (_direction.z * elapsedTime * _speed);; _life -= elapsedTime; if (_life <= 0.0) _isAlive = NO; } /** * Draws the particle. * Vectors "right" and "up" give the orientation of the camera for billboarding */ - (void) drawRight: (coord_t)right up: (coord_t)up lifeMax: (double)lifeMax { glPushMatrix (); { // (_life / lifeMax) : to have a number between 0.0 and 1.0 glColor4f ([_color redComponent], [_color greenComponent], [_color blueComponent], (_life / lifeMax) * _shininess); glBegin (GL_QUADS); { glTexCoord2f (0.0f, 0.0f); glVertex3f (_position.x - right.x - up.x, _position.y - right.y - up.y - (_size / 2.0), _position.z - right.z - up.z); glTexCoord2f (1.0f, 0.0f); glVertex3f (_position.x + right.x - up.x + (_size / 2.0), _position.y + right.y - up.y - (_size / 2.0), _position.z + right.z - up.z); glTexCoord2f (1.0f, 1.0f); glVertex3f (_position.x + right.x + up.x + (_size / 2.0), _position.y + right.y + up.y, _position.z + right.z + up.z); glTexCoord2f (0.0f, 1.0f); glVertex3f (_position.x - right.x + up.x, _position.y - right.y + up.y, _position.z - right.z + up.z); } glEnd (); } glPopMatrix (); } @end