3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "particles.h"
21 #include "constants.h"
23 #include "main.h" // For g_profiler and g_settings
27 #include "collision.h"
29 #include "util/numeric.h"
31 #include "environment.h"
32 #include "clientmap.h"
37 scene::ISceneManager* smgr,
47 scene::ISceneNode(smgr->getRootSceneNode(), smgr, id)
53 m_material.setFlag(video::EMF_LIGHTING, false);
54 m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
55 m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
56 m_material.setFlag(video::EMF_FOG_ENABLE, true);
57 m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
58 m_material.setTexture(0, ap.atlas);
65 m_velocity = velocity;
66 m_acceleration = acceleration;
67 m_expiration = expirationtime;
72 // Irrlicht stuff (TODO)
73 m_collisionbox = core::aabbox3d<f32>(-size/2,-size/2,-size/2,size/2,size/2,size/2);
74 this->setAutomaticCulling(scene::EAC_OFF);
81 void Particle::OnRegisterSceneNode()
85 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
86 SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
89 ISceneNode::OnRegisterSceneNode();
92 void Particle::render()
94 // TODO: Render particles in front of water and the selectionbox
96 video::IVideoDriver* driver = SceneManager->getVideoDriver();
97 driver->setMaterial(m_material);
98 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
99 video::SColor c(255, m_light, m_light, m_light);
101 video::S3DVertex vertices[4] =
103 video::S3DVertex(-m_size/2,-m_size/2,0, 0,0,0, c, m_ap.x0(), m_ap.y1()),
104 video::S3DVertex(m_size/2,-m_size/2,0, 0,0,0, c, m_ap.x1(), m_ap.y1()),
105 video::S3DVertex(m_size/2,m_size/2,0, 0,0,0, c, m_ap.x1(), m_ap.y0()),
106 video::S3DVertex(-m_size/2,m_size/2,0, 0,0,0, c ,m_ap.x0(), m_ap.y0()),
109 for(u16 i=0; i<4; i++)
111 vertices[i].Pos.rotateYZBy(m_player->getPitch());
112 vertices[i].Pos.rotateXZBy(m_player->getYaw());
113 m_box.addInternalPoint(vertices[i].Pos);
114 vertices[i].Pos += m_pos*BS;
117 u16 indices[] = {0,1,2, 2,3,0};
118 driver->drawVertexPrimitiveList(vertices, 4, indices, 2,
119 video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
122 void Particle::step(float dtime, ClientEnvironment &env)
124 core::aabbox3d<f32> box = m_collisionbox;
125 v3f p_pos = m_pos*BS;
126 v3f p_velocity = m_velocity*BS;
127 v3f p_acceleration = m_acceleration*BS;
128 collisionMoveSimple(&env.getClientMap(), m_gamedef,
131 p_pos, p_velocity, p_acceleration);
133 m_velocity = p_velocity/BS;
134 m_acceleration = p_acceleration/BS;
145 MapNode n = env.getClientMap().getNode(p);
146 light = n.getLightBlend(env.getDayNightRatio(), m_gamedef->ndef());
148 catch(InvalidPositionException &e){
149 light = blend_light(env.getDayNightRatio(), LIGHT_SUN, 0);
151 m_light = decode_light(light);
154 std::vector<Particle*> all_particles;
156 void allparticles_step (float dtime, ClientEnvironment &env)
158 for(std::vector<Particle*>::iterator i = all_particles.begin(); i != all_particles.end();)
160 if ((*i)->get_expired())
164 all_particles.erase(i);
168 (*i)->step(dtime, env);
174 void addDiggingParticles(IGameDef* gamedef, scene::ISceneManager* smgr, LocalPlayer *player, v3s16 pos, const TileSpec tiles[])
176 for (u16 j = 0; j < 32; j++) // set the amount of particles here
178 addNodeParticle(gamedef, smgr, player, pos, tiles);
182 void addPunchingParticles(IGameDef* gamedef, scene::ISceneManager* smgr, LocalPlayer *player, v3s16 pos, const TileSpec tiles[])
184 addNodeParticle(gamedef, smgr, player, pos, tiles);
187 // add a particle of a node
188 // used by digging and punching particles
189 void addNodeParticle(IGameDef* gamedef, scene::ISceneManager* smgr, LocalPlayer *player, v3s16 pos, const TileSpec tiles[])
192 u8 texid = myrand_range(0,5);
193 AtlasPointer ap = tiles[texid].texture;
194 float size = rand()%64/512.;
195 float visual_size = BS*size;
196 float texsize = size*2;
201 ap.size.X = (ap.x1() - ap.x0()) * texsize;
202 ap.size.Y = (ap.x1() - ap.x0()) * texsize;
204 ap.pos.X = ap.x0() + (x1 - ap.x0()) * ((rand()%64)/64.-texsize);
205 ap.pos.Y = ap.y0() + (y1 - ap.y0()) * ((rand()%64)/64.-texsize);
208 v3f velocity((rand()%100/50.-1)/1.5, rand()%100/35., (rand()%100/50.-1)/1.5);
209 v3f acceleration(0,-9,0);
210 v3f particlepos = v3f(
211 (f32)pos.X+rand()%100/200.-0.25,
212 (f32)pos.Y+rand()%100/200.-0.25,
213 (f32)pos.Z+rand()%100/200.-0.25
216 Particle *particle = new Particle(
224 rand()%100/100., // expiration time
228 all_particles.push_back(particle);