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

Author Topic: How to implement this shader correct?  (Read 1827 times)

0 Members and 1 Guest are viewing this topic.

Sajmon

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to implement this shader correct?
« on: May 23, 2016, 06:33:33 pm »
Hey, I don't feel like I'm doing shader's the correct way.

This is the shader;

uniform vec2 frag_LightOrigin;                 
uniform vec3 frag_LightColor;                  
uniform float frag_LightAttenuation;

void main()
{      
        vec2 baseDistance               = gl_FragCoord.xy;                                                                           
        vec2 distance                   = frag_LightOrigin - baseDistance;                                           
        float linear_distance   = length(distance);                                                                            
        float attenuation               = 1.0 / (frag_LightAttenuation * linear_distance +
                                                                         frag_LightAttenuation * linear_distance);                                     
        vec4 lightColor                 = vec4(frag_LightColor, 1.0);
        vec4 color                              = vec4(attenuation, attenuation, attenuation, 0.75) * lightColor;      

        gl_FragColor                    = color;                                                                                                   
}
 


The following is in the draw-function of all particles;

                rTexture.clear(sf::Color(0, 0, 0, 255));

                for (size_t i = 0; i < particles.size(); i++)
                {
                        shader.setParameter("frag_LightOrigin", particles.at(i)->position);
                        shader.setParameter("frag_LightColor", particles.at(i)->color);
                        shader.setParameter("frag_LightAttenuation", particles.at(i)->strength);
                        rTexture.draw(painting, states);
                }

                rTexture.display();
 

(painting is a sf::RectangleShape covering the whole screen)

Currently, I'm drawing each new state of the shader on a sf::RectangleShape(painting) for as many lightsources (particles).
This makes the whole game lag so much when there's many lights.

The game works and all, but I feel like something is wrong.
Is there a way of drawing each particle on the same shape or something?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to implement this shader correct?
« Reply #1 on: May 25, 2016, 10:15:58 pm »
Wouldn't the uniform for the colour be a vec4?

It is likely that it's slowing down when there are a lot of particles because each particle causes a draw call, which means that there are a lot of draw calls. Also, larger shapes can take longer.

First thing to consider is only using a rectangle "painting" the shape and size of the content that it's about to draw.

Second thing would be to consider batching all of the particles into a vertex array, where they could all be drawn with a single draw call. This would mean that the shader would need to be a lot more flexible and probably a lot more complicated though, but would likely solve the problem.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything