diff options
author | chai <chaifix@163.com> | 2018-11-26 08:37:17 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-11-26 08:37:17 +0800 |
commit | 24b712a956a48f43788e1c11e121aedf52e22278 (patch) | |
tree | 0bbccc98f1cb24c44cfb58ca99ce305b9f513e09 | |
parent | 1d54138b94a384917d47abd8109c586b1b7ac323 (diff) |
*粒子系统
33 files changed, 123 insertions, 43 deletions
diff --git a/bin/ParticleSystem.exe b/bin/ParticleSystem.exe Binary files differindex d66f7ed..f2abb76 100644 --- a/bin/ParticleSystem.exe +++ b/bin/ParticleSystem.exe diff --git a/bin/dust/s_dust_A_0.png b/bin/dust/s_dust_A_0.png Binary files differnew file mode 100644 index 0000000..79efc70 --- /dev/null +++ b/bin/dust/s_dust_A_0.png diff --git a/bin/dust/s_dust_A_1.png b/bin/dust/s_dust_A_1.png Binary files differnew file mode 100644 index 0000000..acc2100 --- /dev/null +++ b/bin/dust/s_dust_A_1.png diff --git a/bin/dust/s_dust_A_10.png b/bin/dust/s_dust_A_10.png Binary files differnew file mode 100644 index 0000000..e422644 --- /dev/null +++ b/bin/dust/s_dust_A_10.png diff --git a/bin/dust/s_dust_A_2.png b/bin/dust/s_dust_A_2.png Binary files differnew file mode 100644 index 0000000..1ef2056 --- /dev/null +++ b/bin/dust/s_dust_A_2.png diff --git a/bin/dust/s_dust_A_3.png b/bin/dust/s_dust_A_3.png Binary files differnew file mode 100644 index 0000000..ca2260a --- /dev/null +++ b/bin/dust/s_dust_A_3.png diff --git a/bin/dust/s_dust_A_4.png b/bin/dust/s_dust_A_4.png Binary files differnew file mode 100644 index 0000000..1f67b79 --- /dev/null +++ b/bin/dust/s_dust_A_4.png diff --git a/bin/dust/s_dust_A_5.png b/bin/dust/s_dust_A_5.png Binary files differnew file mode 100644 index 0000000..3969a04 --- /dev/null +++ b/bin/dust/s_dust_A_5.png diff --git a/bin/dust/s_dust_A_6.png b/bin/dust/s_dust_A_6.png Binary files differnew file mode 100644 index 0000000..d2d6df0 --- /dev/null +++ b/bin/dust/s_dust_A_6.png diff --git a/bin/dust/s_dust_A_7.png b/bin/dust/s_dust_A_7.png Binary files differnew file mode 100644 index 0000000..02ba310 --- /dev/null +++ b/bin/dust/s_dust_A_7.png diff --git a/bin/dust/s_dust_A_8.png b/bin/dust/s_dust_A_8.png Binary files differnew file mode 100644 index 0000000..700052c --- /dev/null +++ b/bin/dust/s_dust_A_8.png diff --git a/bin/dust/s_dust_A_9.png b/bin/dust/s_dust_A_9.png Binary files differnew file mode 100644 index 0000000..8de4fcf --- /dev/null +++ b/bin/dust/s_dust_A_9.png diff --git a/examples/particle_system/main.cpp b/examples/particle_system/main.cpp index 1d47689..1c76ee6 100644 --- a/examples/particle_system/main.cpp +++ b/examples/particle_system/main.cpp @@ -39,8 +39,17 @@ const char* shader_code = R"( #END_FRAGMENT_SHADER )"; const float Pi = 3.14f; +Texture* textures[10]; +Sprite* sprites[10]; void onLoad() { + for (int i = 0; i < 10; ++i) + { + string name = string("dust/s_dust_A_") + std::to_string(i) + ".png"; + textures[i] = Texture::createTexture(name.c_str()); + sprites[i] = new Sprite(textures[i], Origin::MIDDLECENTER); + } + tex = Texture::createTexture("particle.png"); spr = new Sprite(tex, Origin::MIDDLECENTER); tex1 = Texture::createTexture("texture.png"); @@ -95,25 +104,29 @@ void onLoad() def.particleDef.scaleDef.overTime.value.addPoint(1, 0.7); */ p = new ParticleSystem(); - p->setEmitRate(0.01); - p->setEmitForce(80, 150); + p->setEmitRate(0.02); + p->setEmitForce(80, 120); //p->setEmitPosition(Vector2<float>(-2, -2), Vector2<float>(2, 2)); p->setEmitPosition(Vector2<float>()); - p->setEmitDirection(-Pi / 8 - Pi / 2, Pi / 8 - Pi / 2); + p->setEmitDirection(-Pi / 10 - Pi / 2, Pi / 10 - Pi / 2); p->setParticleLinearAccelaration(Vector2<float>(0, 10)); - p->setParticleSpritesMode(SpriteMode::RANDOM); - p->addParticleSprites(2, spr, spr1, spr2); - p->addParticleColorPoint(Color(255, 100, 0, 255), 0); - p->addParticleColorPoint(Color(255, 255, 0, 100), 0.3); - p->addParticleColorPoint(Color(0, 255, 100, 255), 0.5); - p->addParticleColorPoint(Color(255, 0, 0, 255), 1); - //p->setParticleColor(Color(255, 30, 0, 255)); + p->setParticleSpritesMode(SpriteMode::ANIMATED); + //p->addParticleSprites(2, spr, spr1, spr2); + for (int i = 0; i < 10; ++i) + p->addParticleSprite(sprites[i]); + //p->addParticleColorPoint(Color(255, 100, 0, 255), 0); + //p->addParticleColorPoint(Color(0, 255, 100, 255), 0.5); + //p->addParticleColorPoint(Color(255, 0, 0, 255), 1); + p->setParticleColor(Color(255, 30, 0, 255)); p->addParticleTransparencyPoint(1, 0); - p->addParticleTransparencyPoint(0.7, 1); + p->addParticleTransparencyPoint(0, 0.5); p->enableParticleBlendAdditive(true); p->setParticleLife(0.5, 2); - p->addParticleScalePoint(1, 0); - p->addParticleScalePoint(0.7, 1); + p->addParticleScalePoint(3, 0); + p->addParticleScalePoint(0, 1); + timer.every(3, [=](void*)->void { + //p->setEmitForce() + }); } void onEvent(Event* e) diff --git a/src/libjin/graphics/animations/je_animator.cpp b/src/libjin/graphics/animations/je_animator.cpp index e15dc90..449799d 100644 --- a/src/libjin/graphics/animations/je_animator.cpp +++ b/src/libjin/graphics/animations/je_animator.cpp @@ -118,6 +118,11 @@ namespace JinEngine return mSpeed; } + uint Animator::getFrameCount() + { + return mAnimation->getFrameCount(); + } + } } }
\ No newline at end of file diff --git a/src/libjin/graphics/animations/je_animator.h b/src/libjin/graphics/animations/je_animator.h index bee3d7d..e2369cf 100644 --- a/src/libjin/graphics/animations/je_animator.h +++ b/src/libjin/graphics/animations/je_animator.h @@ -15,7 +15,7 @@ namespace JinEngine namespace Animations { - class Animator : public Object, public IRenderable + class Animator : public Object, public Renderable { public: Animator(); @@ -46,6 +46,8 @@ namespace JinEngine float getSpeed(); + uint getFrameCount(); + private: const Animation* mAnimation; diff --git a/src/libjin/graphics/fonts/je_font.h b/src/libjin/graphics/fonts/je_font.h index 3f72a13..e8ce2c5 100644 --- a/src/libjin/graphics/fonts/je_font.h +++ b/src/libjin/graphics/fonts/je_font.h @@ -25,7 +25,7 @@ namespace JinEngine /// /// Base Font class. /// - class Font : public Object, public IRenderable + class Font : public Object, public Renderable { public: /// diff --git a/src/libjin/graphics/fonts/je_ttf.cpp b/src/libjin/graphics/fonts/je_ttf.cpp index 1f79558..d62798b 100644 --- a/src/libjin/graphics/fonts/je_ttf.cpp +++ b/src/libjin/graphics/fonts/je_ttf.cpp @@ -161,7 +161,7 @@ namespace JinEngine { } - ///*static*/ TTF* TTF::createTTF(TTFData* fontData, unsigned int fontSzie) + //TTF* TTF::createTTF(TTFData* fontData, unsigned int fontSzie) //{ // TTF* ttf; // try diff --git a/src/libjin/graphics/je_bitmap.cpp b/src/libjin/graphics/je_bitmap.cpp index cdab46d..a6db55e 100644 --- a/src/libjin/graphics/je_bitmap.cpp +++ b/src/libjin/graphics/je_bitmap.cpp @@ -30,7 +30,7 @@ namespace JinEngine return bitmap; } - /*static*/ Bitmap* Bitmap::createBitmap(const void* imgData, size_t size) + Bitmap* Bitmap::createBitmap(const void* imgData, size_t size) { if (imgData == nullptr) return nullptr; @@ -48,7 +48,7 @@ namespace JinEngine return bitmap; } - /*static*/ Bitmap* Bitmap::createBitmap(int w, int h, Color color) + Bitmap* Bitmap::createBitmap(int w, int h, Color color) { Bitmap* bitmap = new Bitmap(w, h); if (color != Color::BLACK) @@ -56,7 +56,7 @@ namespace JinEngine return bitmap; } - /*static*/ Bitmap* Bitmap::createBitmap(int width, int height, std::function<Color(int, int, int, int)> drawer) + Bitmap* Bitmap::createBitmap(int width, int height, std::function<Color(int, int, int, int)> drawer) { Bitmap* bitmap = new Bitmap(width, height); for (int y = 0; y < height; ++y) diff --git a/src/libjin/graphics/je_graphic.cpp b/src/libjin/graphics/je_graphic.cpp index 6cd05a1..a01526e 100644 --- a/src/libjin/graphics/je_graphic.cpp +++ b/src/libjin/graphics/je_graphic.cpp @@ -137,6 +137,32 @@ namespace JinEngine // glTexParameteri(GL_) //} + Shaders::Shader* Graphic::getDefaultShader() + { + const char* default_shader = R"( + #VERTEX_SHADER + + Vertex vert(Vertex v) + { + return v; + } + + #END_VERTEX_SHADER + + #FRAGMENT_SHADER + + Color frag(Color col, Texture tex, Vertex v) + { + return col * texel(tex, v.uv); + } + + #END_FRAGMENT_SHADER + )"; + static Shader* shader = Shader::createShader(default_shader); + if(shader != nullptr) + return shader; + } + } // namespace Graphics } // namespace JinEngine diff --git a/src/libjin/graphics/je_graphic.h b/src/libjin/graphics/je_graphic.h index d48ba3c..53eacee 100644 --- a/src/libjin/graphics/je_graphic.h +++ b/src/libjin/graphics/je_graphic.h @@ -21,7 +21,7 @@ namespace JinEngine /// Class inherites Graphic doesn't keep any state such as origin, scale and other properties. Very low /// level visualized resources. /// - class Graphic : public Object, public IRenderable + class Graphic : public Object, public Renderable { public: /// @@ -85,6 +85,8 @@ namespace JinEngine Math::Vector2<uint> mSize; private: + static Shaders::Shader* getDefaultShader(); + GLuint mTexture; }; diff --git a/src/libjin/graphics/je_image.cpp b/src/libjin/graphics/je_image.cpp index 6baf16d..2d8ceae 100644 --- a/src/libjin/graphics/je_image.cpp +++ b/src/libjin/graphics/je_image.cpp @@ -11,7 +11,7 @@ namespace JinEngine using namespace Filesystem; - /*static*/ Image* Image::createImage(const void* imgData, size_t size) + Image* Image::createImage(const void* imgData, size_t size) { if (imgData == nullptr) return nullptr; diff --git a/src/libjin/graphics/je_renderable.h b/src/libjin/graphics/je_renderable.h index ca785c3..0e29556 100644 --- a/src/libjin/graphics/je_renderable.h +++ b/src/libjin/graphics/je_renderable.h @@ -6,6 +6,8 @@ namespace JinEngine namespace Graphics { + namespace Shaders { class Shader; }; + enum class Origin { TOPLEFT, @@ -19,13 +21,18 @@ namespace JinEngine BOTTOMRIGHT }; - class IRenderable + class Renderable { public: + void setShader(const Shaders::Shader* shader); + virtual void render(float x, float y, float sx, float sy, float r) const {}; virtual void render(float x, float y, float sx, float sy, float r, float ox, float oy) const {}; virtual void render(float x, float y, float sx, float sy, float r, Origin origin) const {}; + protected: + const Shaders::Shader* mShader; + }; } diff --git a/src/libjin/graphics/je_sprite.h b/src/libjin/graphics/je_sprite.h index de2117e..70e8f7b 100644 --- a/src/libjin/graphics/je_sprite.h +++ b/src/libjin/graphics/je_sprite.h @@ -16,7 +16,7 @@ namespace JinEngine /// /// A sprite is unit of rendering. Animation is based on sprite, but not texture or other graphic stuff. /// - class Sprite : public Object, public IRenderable + class Sprite : public Object, public Renderable { public: diff --git a/src/libjin/graphics/je_sprite_batch.h b/src/libjin/graphics/je_sprite_batch.h index d58489c..a8109cc 100644 --- a/src/libjin/graphics/je_sprite_batch.h +++ b/src/libjin/graphics/je_sprite_batch.h @@ -3,16 +3,23 @@ #include "../common/je_object.h" +#include "shaders/je_shader.h" +#include "je_renderable.h" + namespace JinEngine { namespace Graphics { - class SpriteBatch : public Object + class SpriteBatch : public Object, public Renderable { public: + private: + static void createDefaultShader(); + + //Shaders::Shader }; diff --git a/src/libjin/graphics/je_texture.cpp b/src/libjin/graphics/je_texture.cpp index 8aa3f9a..7f02c84 100644 --- a/src/libjin/graphics/je_texture.cpp +++ b/src/libjin/graphics/je_texture.cpp @@ -23,7 +23,7 @@ namespace JinEngine return texture; } - /*static*/ Texture* Texture::createTexture(Bitmap* bitmap) + Texture* Texture::createTexture(Bitmap* bitmap) { Texture* tex = new Texture(bitmap); return tex; diff --git a/src/libjin/graphics/particles/je_particle.cpp b/src/libjin/graphics/particles/je_particle.cpp index 00e593f..fc3dc54 100644 --- a/src/libjin/graphics/particles/je_particle.cpp +++ b/src/libjin/graphics/particles/je_particle.cpp @@ -1,3 +1,4 @@ +#include "../../utils/je_log.h" #include "../../common/je_array.hpp" #include "../../math/je_math.h" #include "../../math/je_random.h" @@ -129,9 +130,9 @@ namespace JinEngine color = def->colorDef.overTime.value.getColor(t); // transparency if (def->transparencyDef.overTime.enable) - color.a *= def->transparencyDef.overTime.value.getValue(t); + color.a = 0xff * def->transparencyDef.overTime.value.getValue(t); else - color.a *= def->transparencyDef.transparency.value; + color.a = 0xff * def->transparencyDef.transparency.value; if ((updateFlags & UPDATE_SCALE) != 0) { // Lerp scale. @@ -156,8 +157,7 @@ namespace JinEngine int n = def->spritesDef.sprites.size(); if (def->spritesDef.mode == SpriteMode::ANIMATED) spriteIndex = lerp<int>(0, n - 1, t); - //else - // spriteIndex = rng.rand(0, n - 1); + //jin_log_info("sprite index %d", spriteIndex); } life += dt; alive = life < lifeTime; diff --git a/src/libjin/graphics/particles/je_particle.h b/src/libjin/graphics/particles/je_particle.h index bef3e6d..2b5966b 100644 --- a/src/libjin/graphics/particles/je_particle.h +++ b/src/libjin/graphics/particles/je_particle.h @@ -155,7 +155,7 @@ namespace JinEngine /// A single particle contains various properties of particle, such as position, accelaration, color /// and other attributes changed over time. /// - class Particle : public IRenderable, public Game::GameObject + class Particle : public Renderable, public Game::GameObject { public: enum ParticleUpdateMask @@ -189,7 +189,7 @@ namespace JinEngine /// void render(); - ////////////////////////////////////////////////////////////////////////////////////////////////// + // Methods end. ParticleDef* def; diff --git a/src/libjin/graphics/particles/je_particle_system.cpp b/src/libjin/graphics/particles/je_particle_system.cpp index 2c2f9bb..a6e9460 100644 --- a/src/libjin/graphics/particles/je_particle_system.cpp +++ b/src/libjin/graphics/particles/je_particle_system.cpp @@ -1,5 +1,7 @@ #include <stdarg.h> +#include "../../utils/je_log.h" + #include "je_particle_system.h" namespace JinEngine @@ -60,6 +62,7 @@ namespace JinEngine { Particle* p = new (mParticlePool.GetNextWithoutInitializing()) Particle(); mAliveParticles.push_back(p); + jin_log_info("particle count %d", mAliveParticles.size()); return p; } @@ -216,16 +219,16 @@ namespace JinEngine mDef.particleDef.scaleDef.overTime.value.removePoint(i); } - void ParticleSystem::setParticleColor(Color color) + void ParticleSystem::setParticleColor(Color tint) { mDef.particleDef.colorDef.overTime.enable = false; - mDef.particleDef.colorDef.color = color; + mDef.particleDef.colorDef.color = tint; } - void ParticleSystem::addParticleColorPoint(Color color, float t) + void ParticleSystem::addParticleColorPoint(Color tint, float t) { mDef.particleDef.colorDef.overTime.enable = true; - mDef.particleDef.colorDef.overTime.value.addColor(color, t); + mDef.particleDef.colorDef.overTime.value.addColor(tint, t); } void ParticleSystem::removeParticleColorPoint(uint i) diff --git a/src/libjin/graphics/particles/je_particle_system.h b/src/libjin/graphics/particles/je_particle_system.h index 20f39dc..b3640d4 100644 --- a/src/libjin/graphics/particles/je_particle_system.h +++ b/src/libjin/graphics/particles/je_particle_system.h @@ -33,7 +33,7 @@ namespace JinEngine /// /// Particle emitter, handle all particles it emitts. /// - class ParticleSystem : public IRenderable, public Game::GameObject + class ParticleSystem : public Renderable, public Game::GameObject { public: /// @@ -49,11 +49,26 @@ namespace JinEngine ParticleSystem(uint maxCount = 64); /// + /// + /// + ParticleSystem(const std::string & config); + + /// /// Particle system destructor. /// ~ParticleSystem(); /// + /// + /// + void setDefinition(const ParticleSystemDef& def); + + /// + /// Load definition from config. + /// + void setDefinition(const std::string& config); + + /// /// Update particle system and all alive particles. /// void update(float dt); @@ -124,8 +139,8 @@ namespace JinEngine void addParticleScalePoint(float scale, float t); void removeParticleScalePoint(uint i); - void setParticleColor(Color color); - void addParticleColorPoint(Color color, float t); + void setParticleColor(Color tint); + void addParticleColorPoint(Color tint, float t); void removeParticleColorPoint(uint i); void setParticleTransparency(float transparency); diff --git a/src/libjin/graphics/shaders/je_shader.cpp b/src/libjin/graphics/shaders/je_shader.cpp index c1abbf0..3748de6 100644 --- a/src/libjin/graphics/shaders/je_shader.cpp +++ b/src/libjin/graphics/shaders/je_shader.cpp @@ -52,7 +52,7 @@ namespace JinEngine // const int DEFAULT_TEXTURE_UNIT = 0; - /*static*/ Shader* Shader::CurrentShader = nullptr; + Shader* Shader::CurrentShader = nullptr; Shader* Shader::createShader(const string& program) { @@ -134,7 +134,7 @@ if (success == GL_FALSE) \ sendInt(SHADER_MAIN_TEXTURE, DEFAULT_TEXTURE_UNIT); } - /*static*/ void Shader::unuse() + void Shader::unuse() { glUseProgram(0); CurrentShader = nullptr; diff --git a/src/libjin/time/je_timer.h b/src/libjin/time/je_timer.h index 31cd322..f9feebe 100644 --- a/src/libjin/time/je_timer.h +++ b/src/libjin/time/je_timer.h @@ -71,7 +71,7 @@ namespace JinEngine /// /// /// - Handler* every(float dt, TimerCallback callback, void* paramters, FinishCallback finish); + Handler* every(float dt, TimerCallback callback, void* paramters = nullptr, FinishCallback finish = nullptr); /// /// diff --git a/src/lua/common/je_lua.cpp b/src/lua/common/je_lua.cpp index a08414e..020599d 100644 --- a/src/lua/common/je_lua.cpp +++ b/src/lua/common/je_lua.cpp @@ -132,7 +132,7 @@ namespace JinEngine luax_getobject(L, dep); if (luax_isnil(L, -1)) { - luax_pop(L, 3); // Pop nil, dep, reftbl + luax_pop(L, 3); // Pop nil, dep, reftbl. return false; } luax_settable(L, -3); diff --git a/src/lua/common/je_lua.h b/src/lua/common/je_lua.h index 66647aa..1396970 100644 --- a/src/lua/common/je_lua.h +++ b/src/lua/common/je_lua.h @@ -23,7 +23,7 @@ namespace JinEngine Proxy* luax_newinstance(lua_State* L, const char* type, SharedBase* shared); /// - /// + /// Access lua object by object pointer. /// int luax_getobject(lua_State* L, SharedBase* shared); |