~ubuntu-branches/ubuntu/precise/supertuxkart/precise

« back to all changes in this revision

Viewing changes to src/graphics/smoke.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2011-02-24 22:36:25 UTC
  • mfrom: (1.1.9 upstream) (6.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110224223625-ygrjfpg92obovuch
Tags: 0.7+dfsg1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
//  $Id: dust_cloud.cpp 1681 2008-04-09 13:52:48Z hikerstk $
 
1
//  $Id: smoke.cpp 1681 2008-04-09 13:52:48Z hikerstk $
3
2
//
4
3
//  SuperTuxKart - a fun racing game with go-kart
5
 
//  Copyright (C) 2006 SuperTuxKart-Team
 
4
//  Copyright (C) 2009  Joerg Henrichs
6
5
//
7
6
//  This program is free software; you can redistribute it and/or
8
7
//  modify it under the terms of the GNU General Public License
18
17
//  along with this program; if not, write to the Free Software
19
18
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
19
 
21
 
#include "smoke.hpp"
22
 
#include "material_manager.hpp"
 
20
#include "graphics/smoke.hpp"
 
21
 
 
22
#include "graphics/material_manager.hpp"
 
23
#include "graphics/irr_driver.hpp"
 
24
#include "io/file_manager.hpp"
23
25
#include "karts/kart.hpp"
24
26
#include "physics/btKart.hpp"
25
27
#include "utils/constants.hpp"
26
28
 
27
 
Smoke::Smoke(Kart* kart)
28
 
        : ParticleSystem(200, 0.0f, true, 0.75f),
29
 
        m_kart(kart)
 
29
Smoke::Smoke(Kart* kart) : m_kart(kart), m_particle_size(0.33f)
30
30
{
 
31
    m_node = irr_driver->addParticleNode();
31
32
#ifdef DEBUG
32
 
    setName("smoke");
 
33
    std::string debug_name = m_kart->getIdent()+" (smoke)";
 
34
    m_node->setName(debug_name.c_str());
33
35
#endif
34
 
    bsphere.setCenter(0, 0, 0);
35
 
    bsphere.setRadius(1000.0f);
36
 
    dirtyBSphere();
37
 
 
38
 
    m_smokepuff = new ssgSimpleState ();
39
 
    m_smokepuff->setTexture(material_manager->getMaterial("smoke.rgb")->getState()->getTexture());
40
 
    m_smokepuff -> setTranslucent    () ;
41
 
    m_smokepuff -> enable            ( GL_TEXTURE_2D ) ;
42
 
    m_smokepuff -> setShadeModel     ( GL_SMOOTH ) ;
43
 
    m_smokepuff -> disable           ( GL_CULL_FACE ) ;
44
 
    m_smokepuff -> enable            ( GL_BLEND ) ;
45
 
    m_smokepuff -> disable           ( GL_ALPHA_TEST ) ;
46
 
    m_smokepuff -> enable            ( GL_LIGHTING ) ;
47
 
    m_smokepuff -> setColourMaterial ( GL_EMISSION ) ;
48
 
    m_smokepuff -> setMaterial       ( GL_AMBIENT, 0, 0, 0, 1 ) ;
49
 
    m_smokepuff -> setMaterial       ( GL_DIFFUSE, 0, 0, 0, 1 ) ;
50
 
    m_smokepuff -> setMaterial       ( GL_SPECULAR, 0, 0, 0, 1 ) ;
51
 
    m_smokepuff -> setShininess      (  0 ) ;
52
 
    m_smokepuff->ref();
53
 
 
54
 
    setState(m_smokepuff);
 
36
    // Note: the smoke system is NOT child of the kart, since bullet
 
37
    // gives the position of the wheels on the ground in world coordinates.
 
38
    // So it's easier not to move the particle system with the kart, and set 
 
39
    // the position directly from the wheel coordinates.
 
40
    m_node->setPosition(core::vector3df(-m_kart->getKartWidth()*0.35f, 
 
41
                                        m_particle_size*0.25f, 
 
42
                                        -m_kart->getKartLength()*0.5f));
 
43
    Material *m= material_manager->getMaterial("smoke.png");
 
44
    m->setMaterialProperties(&(m_node->getMaterial(0)));
 
45
    m_node->setMaterialTexture(0, m->getTexture());
 
46
 
 
47
    m_emitter = m_node->createPointEmitter(core::vector3df(0, 0, 0),   // velocity in m/ms
 
48
                                           5, 10,
 
49
                                           video::SColor(255,0,0,0),
 
50
                                           video::SColor(255,255,255,255),
 
51
                                           300, 500,
 
52
                                           20  // max angle
 
53
                                           );
 
54
    m_emitter->setMinStartSize(core::dimension2df(m_particle_size/1.5f, m_particle_size/1.5f));
 
55
    m_emitter->setMaxStartSize(core::dimension2df(m_particle_size*1.5f, m_particle_size*1.5f));
 
56
    m_node->setEmitter(m_emitter); // this grabs the emitter
 
57
    m_emitter->drop();             // so we can drop our references
 
58
 
 
59
    scene::IParticleFadeOutAffector *af = m_node->createFadeOutParticleAffector(video::SColor(0, 255, 0, 0), 500);
 
60
    m_node->addAffector(af);
 
61
    af->drop();
55
62
 
56
63
}   // KartParticleSystem
57
64
 
58
65
//-----------------------------------------------------------------------------
 
66
/** Destructor, removes
 
67
 */
59
68
Smoke::~Smoke()
60
69
{
61
 
    ssgDeRefDelete(m_smokepuff);
 
70
    irr_driver->removeNode(m_node);
62
71
}   // ~Smoke
 
72
 
63
73
//-----------------------------------------------------------------------------
64
74
void Smoke::update(float t)
65
75
{
66
 
    ParticleSystem::update(t);
 
76
    // No particles to emit, no need to change the speed
 
77
    if(m_emitter->getMinParticlesPerSecond()==0)
 
78
        return;
 
79
    static int left=1;
 
80
    left = 1-left;
 
81
    const btWheelInfo &wi = m_kart->getVehicle()->getWheelInfo(2+left);
 
82
    Vec3 c=wi.m_raycastInfo.m_contactPointWS;
 
83
 
 
84
    // FIXME: the X position is not yet always accurate.
 
85
    m_node->setPosition(core::vector3df(c.getX()+m_particle_size*0.25f * (left?+1:-1),
 
86
                                        c.getY(),
 
87
                                        c.getZ()+m_particle_size*0.25f));
 
88
 
 
89
    // There seems to be no way to randomise the velocity for particles,
 
90
    // so we have to do this manually, by changing the default velocity.
 
91
    // Irrlicht expects velocity (called 'direction') in m/ms!!
 
92
    Vec3 dir(cos(DEGREE_TO_RAD*(rand()%180))*0.002f,
 
93
             sin(DEGREE_TO_RAD*(rand()%100))*0.002f,
 
94
             sin(DEGREE_TO_RAD*(rand()%180))*0.002f);
 
95
    m_emitter->setDirection(dir.toIrrVector());
67
96
}   // update
68
97
 
69
98
//-----------------------------------------------------------------------------
70
 
void Smoke::particle_create(int, Particle *p)
71
 
{
72
 
    sgSetVec4(p->m_col, 1, 1, 1, 1 ); /* initially white */
73
 
    sgSetVec3(p->m_vel, 0, 0, 0 );
74
 
    sgSetVec3(p->m_acc, 0, 0, 2.0f ); /* Gravity */
75
 
    p->m_size         = 0.5f;
76
 
    p->m_time_to_live = 0.4f;
77
 
 
78
 
    // Change from left to right wheel and back for each new particle
79
 
    static int wheel_number = 2;
80
 
    wheel_number            = 5 - wheel_number;
81
 
    Vec3 xyz=m_kart->getVehicle()->getWheelInfo(wheel_number).m_raycastInfo.m_contactPointWS;
82
 
 
83
 
    sgCopyVec3 (p->m_pos, xyz.toFloat());
84
 
    p->m_vel[0] += cos(DEGREE_TO_RAD(rand()%180));
85
 
    p->m_vel[1] += sin(DEGREE_TO_RAD(rand()%180));
86
 
    p->m_vel[2] += sin(DEGREE_TO_RAD(rand()%100));
87
 
 
88
 
    bsphere.setCenter ( xyz.getX(), xyz.getY(), xyz.getZ() ) ;
89
 
}   // particle_create
90
 
 
91
 
//-----------------------------------------------------------------------------
92
 
void Smoke::particle_update(float delta, int,
93
 
                            Particle * particle)
94
 
{
95
 
    particle->m_size    -= delta*.2f;
96
 
    particle->m_col[3]  -= delta * 2.0f;
97
 
}  // particle_update
98
 
 
 
99
void Smoke::setCreationRate(float f)
 
100
{
 
101
    m_emitter->setMinParticlesPerSecond(int(f));
 
102
    m_emitter->setMaxParticlesPerSecond(int(f));
 
103
}   // setCreationRate