Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Better way to create particle as Vertex Array?  (Read 2559 times)

0 Members and 1 Guest are viewing this topic.

SkillsXP

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Better way to create particle as Vertex Array?
« on: February 04, 2015, 05:03:52 pm »
does someone know any solution to create pixel as vertex array?
I used them but if I create more than 1024 pixel, it will get slower.

I made 4x4 Pixel, so I have 64 Particle and I becomes slower

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Better way to create particle as Vertex Array?
« Reply #1 on: February 04, 2015, 07:00:40 pm »
Thousands of point primitives should be ok for your graphics card. Can you show your code?
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Better way to create particle as Vertex Array?
« Reply #2 on: February 05, 2015, 08:13:54 am »
The Thor library contains a working particle system module.
See these links:
http://www.bromeon.ch/libraries/thor/v2.0/doc/group___particles.html
http://www.bromeon.ch/libraries/thor/v2.0/tutorial-particles.html

Maybe you could look at the source for inspiration (or just use the library if all you really want is just working particles).

SkillsXP

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Better way to create particle as Vertex Array?
« Reply #3 on: February 05, 2015, 04:54:13 pm »
.h


#ifndef PIXELCREATOR_HPP
#define PIXELCREATOR_HPP

#include <SFML\Graphics.hpp>
#include <iostream>




class Pixelcreator
{
public:
        Pixelcreator(int posx, int posy);

        ~Pixelcreator();

        void update(float frametime);
        void handle();
        void render(sf::RenderWindow *rnd);

        const bool getIsAlive() {return mIsAlive;};
        void setIsAlive(bool alive) {mIsAlive = alive;};

        void set_alpha(float alph) {iAlpha = alph;};

        const float get_ran_0() {return ran[0];};
        void set_ran_0(float ran_0) {ran[0] = ran_0;};

        const float get_ran_1() {return ran[1];};
        void set_ran_1(float ran_1) {ran[1] = ran_1;};

        const bool get_bX() {return bX;};
        const bool get_bY() {return bY;};

        void set_bX(bool bx) {bX = bx;};
        void set_bY(bool by) {bY = by;};

        const float get_grav() {return fgrav;};
        void set_grav(float gra) {fgrav = gra;};

        void set_default() {ran[0] = defran_0; ran[1] = defran_1;};

        const float get_horiz() {return horiz;};
        void set_horiz(float hor) {horiz = hor;};

        const float get_vert() {return vert;};
        void set_vert(float ver) {vert = ver;};

private:
       
        sf::VertexArray pointmap;
        sf::Clock *pClock;

        float defran_0, defran_1;


       
       
        bool mIsAlive;
        int iAlpha;
        int irand;
        float horiz;
        float vert;
        float fgrav;
        bool bX;
        bool bFirst;
        bool bY;
        float ran[];

         

        float rand_dom(float a, float b)
        {
        return ((b-a)*((float)rand()/RAND_MAX))+a;
        }
};


#endif

 



.cpp


#include "pixelcreator.h"

using namespace std;

template<class T>
        T roundd(T a){
                if(a < 0.05) {a = 0;
                }
                else
                if(a > 0.05 && a < 0.01)        {a = 0.01;
                }
                else
                if(a > 0.01 && a < 0.02)        {a = 0.02;
                }
                else
                if(a > 0.02 && a < 0.03)        {a = 0.03;
                }
                else
                if(a > 0.03 && a < 0.04)        {a = 0.04;
                }
                else
                if(a > 0.04 && a < 0.05)        {a = 0.05;
                }
                else
                if(a > 0.05 && a < 0.06)        {a = 0.06;
                }
                else
                if(a > 0.06 && a < 0.07)        {a = 0.07;
                }
                else
                if(a > 0.07 && a < 0.08)        {a = 0.08;
                }
                else
                if(a > 0.08 && a < 0.09)        {a = 0.09;
                }

                T fin = a;
                return fin;
        }

Pixelcreator::Pixelcreator(int posx, int posy){
       
       
        pClock = new sf::Clock();
        pClock->restart();


        setIsAlive(true);

        horiz = posx;
        vert = posy;

        for( int a = 0; a < 16; a++) {

                pointmap.append(sf::Vertex(sf::Vector2f((a % 4) + horiz, (a / 4) + vert), sf::Color::Green));
                pointmap[a].color.a = static_cast<sf::Uint8>(iAlpha);
        }

        fgrav = 0.04;

        ran[0] = roundd(ran[0]);
        ran[1] = roundd(ran[1]);

        ran[0] = rand_dom(0.0,0.09);
        ran[1] = rand_dom(0.0,0.09);

        defran_0 = ran[0];
        defran_1 = ran[1];

                switch(rand() % 4)
                {
                case 0: iAlpha = 255; break;
                case 1: iAlpha = 220; break;
                case 2: iAlpha = 160; break;
                case 3: iAlpha = 150; break;
                case 4: iAlpha = 90; break;
               
                }

                irand = rand() % 4;

                switch(irand)
                {
                case 0: bX = true; bY = false; break;
                case 1: bX = false; bY = true; break;
                case 2: bX = false; bY = false; break;
                case 3: bX = true; bY = true; break;
                }


                bFirst = true;
               


}

Pixelcreator::~Pixelcreator(){
        delete pClock;
        pClock = nullptr;

}

void Pixelcreator::update(float frametime){


        if(ran[1] <= 0 && bY == false) {bY == true;};

        if(horiz >= 634 ) {bX = false;}
        if(vert >= 634  ) {bY = false;}
       
        if(horiz <= 0 ) {bX = true;}
        if(vert <= 0  ) {bY = true;}

        if(bX == true)  horiz += ran[0];
        if(bX == false) horiz -= ran[0];
       
        if(bY == false) { vert -= (ran[1] = ran[1]-((ran[1]*1.5)+fgrav)*frametime);}
        if(bY == true)  vert += ran[1];

        if(pClock->getElapsedTime().asMilliseconds() > 50.f) {iAlpha -= 5; pClock->restart();}
        if(iAlpha < 0) iAlpha = 0;
       

        for(int u = 0; u != 16; u++){
                //pointmap.append(sf::Vertex(sf::Vector2f((u % 4) + horiz, (u / 4) + vert), sf::Color::Green));
                pointmap[u].position.x = (u % 4) + horiz;
                pointmap[u].position.y = (u / 4) + vert;
                pointmap[u].color.a = static_cast<sf::Uint8>(iAlpha);

        }

        if(iAlpha <= 20) setIsAlive(false);
        if(getIsAlive() == false) pointmap.clear();

}

void Pixelcreator::handle(){

}

void Pixelcreator::render(sf::RenderWindow *rnd){
        rnd->draw(pointmap);

}


 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Better way to create particle as Vertex Array?
« Reply #4 on: February 05, 2015, 10:00:40 pm »
Code that people could test would probably be more helpful :P

In your templated function "roundd()", a lot of the code doesn't make sense. Some of the logic is impossible.
For example, when would this ever be able to be true:
if (a > 0.05 && a < 0.01)
The following ifs are odd too as you've already confirmed with the first condition that a is higher than 0.05 but then you test for lots of things below that amount.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Josh_M

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Better way to create particle as Vertex Array?
« Reply #5 on: February 06, 2015, 02:25:59 am »
Try something like this instead for that function Hapax mentioned perhaps.

#include <cmath>

...

a = ceil(a * 100) / 100;

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Better way to create particle as Vertex Array?
« Reply #6 on: February 06, 2015, 05:18:01 am »
Just another note regarding roundd, its templated, but you assume that T is a number. You should look into type checking because if you pass anything that does not have < and > operators that take doubles (Or anything that can be cast from a double), it wont compile.

 

anything