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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - wobbles

Pages: [1]
1
D / Light shader in (D)SFML
« on: February 10, 2016, 12:05:10 am »
Hi,

I'm only starting out with SFML (and DSFML).
I'm attempting to implement a simple lighting engine, with shadows etc.
However, I'm falling at the first hurdle.

Can you see what's going on with this code?
The shaders have been taken from http://nottutorials.blogspot.ie/2009/09/dynamic-2d-soft-shadows.html, so I know they work, so it's something to do with what I'm passing down to them.

Maybe this is an easy problem solve and I'm going about it the wrong way?
Thanks in advance!

Edit: I've modified the code slightly to make it more idiomatic SFML (i.e. my light is now a Drawable and Transformable), as I suspected the gl_Vertex vector wasn't getting set correctly, it should be now though... right?  ???
I'm still a little unsure as to what SFML is actually sending to the shaders - which I guess is what I'll have to read up on next.

import dsfml.graphics;
import std.stdio;

enum lightVert = q{
varying vec2 pos;
void main(){
    pos = gl_Vertex.xy;
    gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
}
};

enum lightFrag = q{
varying vec2 pos;
uniform vec4 color;

void main(){
    float t = 1.0 - sqrt(pos.x*pos.x + pos.y*pos.y);
    t = 1.0 / (1.0 + exp (-(t*12.0 - 6.0)));
    //intensity is in color.a
    gl_FragColor = color * t;
}
};

class Light : Drawable, Transformable{
    mixin NormalTransformable;
    Vector3f color;
    Vector2f position;
    float radius;
    private VertexArray m_vertices;
    float m_intensity;
    Shader shader;
    this(){
        color = Vector3f(
            1.0f, 1.0f, 1.0f // white light
        );
        position = Vector2f(0, 0);
        radius = 600;
        this.m_vertices = new VertexArray(PrimitiveType.Quads, 4);
        this.m_vertices[0].position = (Vector2f(0f, 0f));
        this.m_vertices[1].position = (Vector2f(0f, 1f));
        this.m_vertices[2].position = (Vector2f(1f, 1f));
        this.m_vertices[3].position = (Vector2f(1f, 0f));
        shader = new Shader();
        shader.loadFromMemory(lightVert, lightFrag);
    }
    @property float intensity(){
        return this.m_intensity; }
    @property void intensity(float i){
        this.m_intensity = i;
    }

    override void draw(RenderTarget target, RenderStates states = RenderStates.Default){
        shader.setParameter("color", this.color.x, this.color.y, this.color.z, this.intensity);
        states.transform *= getTransform();
        states.shader = shader;
        states.blendMode = states.blendMode.Add;
        target.draw(m_vertices, states);
    }
}

void main(){
    auto window = new RenderWindow(VideoMode(800, 600), "Lighting");
    Light l = new Light();
    while(window.isOpen()){
        Event event;
        while(window.pollEvent(event)){
            if(event.type == event.EventType.Closed){
                 window.close();
            }
            if(event.type == event.EventType.KeyReleased){
                if(event.key.code == Keyboard.Key.Q){
                    window.close();
                }
            }
        }
        l.position = Vector2f(15,15);
        l.intensity = (l.intensity + 0.01) % 1.0;

        window.clear(Color.Black);
        window.draw(l); //.render(window, 1.0);
        window.display();
    }
}

 

Pages: [1]
anything