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.


Topics - Jove

Pages: [1] 2
1
Graphics / Applying a blur shader gives black square
« on: June 21, 2020, 08:11:23 pm »
Hi.

This shader will work if I apply it to, for example, a full screen sf::RenderTexture, but sadly not on a small object such as a sf::Sprite.

Not sure if it's the shader or something else I'm doing incorrectly. It originates from https://www.shadertoy.com/view/XdfGDH

SHADER:
uniform sampler2D       texture;
uniform vec2            textureSize;
uniform float           blurFactor;

float normpdf(in float x, in float sigma)
{
        return 0.39894*exp(-0.5*x*x/(sigma*sigma))/sigma;
}

void main()
{
        vec3 c = texture2D(texture, gl_FragCoord.xy / textureSize.xy).rgb;
       
        //declare stuff
        const int mSize = 11;
        const int kSize = (mSize-1)/2;
        float kernel[mSize];
        vec3 final_colour = vec3(0.0);
       
        //create the 1-D kernel
        float sigma = blurFactor;
        float Z = 0.0;
        for (int j = 0; j <= kSize; ++j)
        {
                kernel[kSize+j] = kernel[kSize-j] = normpdf(float(j), sigma);
        }
               
        //get the normalization factor (as the gaussian has been clamped)
        for (int j = 0; j < mSize; ++j)
        {
                Z += kernel[j];
        }
               
        //read out the texels
        for (int i=-kSize; i <= kSize; ++i)
        {
                for (int j=-kSize; j <= kSize; ++j)
                {
                        final_colour += kernel[kSize+j]*kernel[kSize+i]*texture2D(texture, (gl_FragCoord.xy+vec2(float(i),float(j))) / textureSize.xy).rgb;
                }
        }
               
        gl_FragColor = vec4(final_colour/(Z*Z), 1.0);
}
 


C++:
#include <SFML\Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(1920, 1080), "SFML Testing");
        window.setVerticalSyncEnabled(true);
        window.setKeyRepeatEnabled(false);

        sf::Texture texture;
        texture.loadFromFile("player.png");
        texture.setSmooth(true);
       
        sf::Sprite sprite;
        sprite.setTexture(texture);
        sprite.setPosition(static_cast<float>(window.getSize().x / 2), static_cast<float>(window.getSize().y / 2));
       
        sf::Shader shader;
        shader.loadFromFile("blur.frag", sf::Shader::Fragment);
        shader.setUniform("texture", texture);
        shader.setUniform("textureSize", static_cast<sf::Glsl::Vec2>(texture.getSize()));
        shader.setUniform("blurFactor", 5.f);
       
        sf::RenderStates states;
        states.texture = &texture;
        states.shader = &shader;

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
                                window.close();
                }

                window.clear(sf::Color::Yellow);
                window.draw(sprite, states);
                window.display();
        }

        return EXIT_SUCCESS;
}

2
I'm just getting more involved with shaders and SFML and really fell in love with this effect on shadertoy.com which is perfect for my explosions.

https://www.shadertoy.com/view/llj3Dz#

Now shader code is new to me but I took a stab at converting it. The effect runs alright, but the source texture is upside down, mirrored and only shows half the screen.

Really starting to see the power of shaders now and thought this would be a good intro to learning more. Any pointers on what I've done wrong would be great.

Here's the test image:




and the result from my testing code:




Here's the .cpp

#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window                 (sf::VideoMode(1920, 1080), "SFML window");
        window.setVerticalSyncEnabled   (true);
        window.setKeyRepeatEnabled              (false);

        sf::Texture bgt;
        bgt.loadFromFile("testcard.jpg");
       
        sf::Sprite bgs(bgt);

        sf::Shader shader;
        shader.loadFromFile("shockwave.frag", sf::Shader::Type::Fragment);

        sf::RenderStates states;
        states.shader = &shader;

        sf::Clock clock;

        while (window.isOpen())
        {
                sf::Event event;

                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
                                window.close();
                }

                shader.setParameter("source",           sf::Shader::CurrentTexture);
        shader.setParameter("sourceSize",       sf::Vector2f(1920.f, 1080.f));
        shader.setParameter("time",                     clock.getElapsedTime().asSeconds());

                window          .clear  ();
                window          .draw   (bgs, states);
                window          .display();
        }

        return EXIT_SUCCESS;
}


...and my attempt to change the fragment shader from shadertoy format.

uniform sampler2D       source;
uniform vec2            sourceSize;
uniform float           time;


void main( void )
{
        //Sawtooth function to pulse from centre.
       
        float offset = (time- floor(time)) / time;
        float CurrentTime = (time)*(offset);
   
        vec3 WaveParams = vec3(10.0, 0.8, 0.1 );
   
        float ratio = sourceSize.y / sourceSize.x;
   
vec2 WaveCentre = vec2(0.5, 0.5);
        WaveCentre.y *= ratio;
   
        vec2 texCoord = gl_FragCoord.xy / sourceSize.xy;      
        texCoord.y *= ratio;
        float Dist = distance(texCoord, WaveCentre);
       
        vec4 Color = texture2D(source, texCoord);
   
        //Only distort the pixels within the parameter distance from the centre
        if (    (Dist <= ((CurrentTime) + (WaveParams.z))) &&
                (Dist >= ((CurrentTime) - (WaveParams.z))))
        {
        //The pixel offset distance based on the input parameters
        float Diff = (Dist - CurrentTime);
        float ScaleDiff = (1.0 - pow(abs(Diff * WaveParams.x), WaveParams.y));
        float DiffTime = (Diff  * ScaleDiff);
       
        //The direction of the distortion
        vec2 DiffTexCoord = normalize(texCoord - WaveCentre);        
       
        //Perform the distortion and reduce the effect over time
        texCoord += ((DiffTexCoord * DiffTime) / (CurrentTime * Dist * 40.0));
        Color = texture2D(source, texCoord);
       
        //Blow out the color and reduce the effect over time
        Color += (Color * ScaleDiff) / (CurrentTime * Dist * 40.0);
        }
   
        gl_FragColor = Color;
}

3
Graphics / Repeating texture question
« on: July 20, 2014, 11:42:06 am »
For the purpose of explaining, I will use the simple example of a very small starfield.

I've tested rendering it into a RenderTexture set to Repeat. It would look something like this:

http://imgur.com/E9QtGaK

...which is then drawn with a sprite in tiled fashion multiple times to create a larger, seamless effect.

http://imgur.com/CRoR7Lx

Given that the stars that say, go off the top, are repositioned at the bottom, there is a moment where they are sliced in half which destroys the illusion.

My question : Is there a way of making things drawn to the texture 'wrap-around' (Pretty sure this isn't possible with my knowledge of the library).

Sure I could re-draw the ones that are partially off-screen to the opposite side in order to maintain the illusion, but my intention is to do this with considerably larger "stars" and there would be a lot of re-drawing going on!

Any good ideas would be appreciated.

4
...in the case of toggling full-screen after the app is running.

Noticed it today, similar to the vsync issue?
Didn't see it in the Issues list.

5
Graphics / How do I set a View position absolutley?
« on: August 09, 2012, 03:00:05 pm »
...or rather, what do you manipulate to do this?

With view zooming, it's setSize.

Thanks.

6
Graphics / Sprite getRotation returning negative (2RC)
« on: July 15, 2012, 02:26:05 pm »
I looked around first, search wasn't working for me and didn't see anything on the tracker.

When I do a getRotation from a sprite, the result isn't between 0-360.

This...
sf::Sprite test;
test.rotate(-10);
cout << test.getRotation();

...will return a -10 instead of 350.

Getting a View's rotation is ok though.

Apologies if I've missed something, 1.6 had the documented behaviour.

7
Window / Breaking vertical sync
« on: July 07, 2012, 01:25:17 pm »
I just noticed that when using Windows 7 basic theme and running a Fullscreen window, alt-tabbing turns off vsync.

Thought I'd drop it in here in case it turns out to be bug for the tracker.

8
Window / How to disable mouse but still read it?
« on: July 04, 2012, 02:45:16 pm »
When my GUI is in use I would like the OS cursor to be visible and work outside my window (normal).

In-game I'd like the OS pointer to be invisible and not able to be seen outside the window, just read the mouse so I can change the position of my game cursor which controls the direction of the main sprite.

Currently I'm doing this by disabling the cursor visibility and repositioning it's x & y if they go outside the window bounds. It works, but in windowed mode the OS cursor can still creep outside the window and register a click, like a right click, which then brings up the context menu. Pretty shabby and interrupts proceedings.

I've seen other games accomplish this, any help on how to do this in SFML(2RC)?

9
Wow!  Two posts in one day, that must be a record!

Today I've been learning a little about Vertex Arrays and in my testing I came across something I've noticed before, but narrowed it down to a minimal example so I can ask what it's all about (it has nothing to do with Vertex Arrays though).

Here's some code:

#include <SFML\Graphics.hpp>
#include "Random.h"

int main()
{
    sf::RenderWindow App(sf::VideoMode(1024, 768, 32), "SFML");

        App.EnableVerticalSync(true);
        //App.SetFramerateLimit(60);

        const unsigned int NOOF_DOTS = 128;

        sf::VertexArray dots(sf::Quads, NOOF_DOTS * 4);

        for (int i=0; i<NOOF_DOTS*4; i+=4)
        {
                Random gen;

                float x = gen.randomF(0,1024);
                float y = gen.randomF(0,768);

                dots[i]  .Position = sf::Vector2f( x,   y   );
                dots[i+1].Position = sf::Vector2f( x,   y+2 );
                dots[i+2].Position = sf::Vector2f( x+2, y+2 );
                dots[i+3].Position = sf::Vector2f( x+2, y   );
        }

        while (App.IsOpen())
        {
                sf::Event Event;
                while (App.PollEvent(Event))
                {
                        if (Event.Type == sf::Event::Closed)
                                App.Close();
                }

                App.Clear();
                App.Draw (dots);
                App.Display();
        }

        return EXIT_SUCCESS;
}

NOTE 1: The 'Random' thing is just a little class I made to replace the old 1.6 Randomizer, it isn't really necessary.
NOTE 2: Running SFML2 from the Release .exe
NOTE 3: The snapshot I'm using is one just prior to the naming convention change.

The purpose of this test is simply to throw lots of tiny squares on the screen as a prelude to using this new aspect of SFML2 in a particle system I'm planning to re-create.

I noticed however (something I've seen before), some odd readings from the CPU.



With Vsync enabled and running 128 dots, CPU is low.
Change up by one to 129 dots and it leaps up to 25%. ???

Removing Vsync and using just frame limiting at 60, I can nearly white-out the screen with thousands of dots and the CPU appears to be yawning at 0%!  Naturally, I'd prefer the VSync for smoothness, but I find this behaviour a little strange and it has confused me for some time now.

What exactly is it here that I'm not understanding correctly?

*Edit - changed title for future searches

10
Graphics / A little help with sf::Vertex texturing
« on: April 28, 2012, 02:19:50 pm »
I've taken a step towards working with Vertex Arrays today, but am slightly confused about texturing.

// define a 100x100 square, red, with a 10x10 texture mapped on it
 sf::Vertex vertices[] =
 {
     sf::Vertex(sf::Vector2f(  0,   0), sf::Color::Red, sf::Vector2f( 0,  0)),
     sf::Vertex(sf::Vector2f(  0, 100), sf::Color::Red, sf::Vector2f( 0, 10)),
     sf::Vertex(sf::Vector2f(100, 100), sf::Color::Red, sf::Vector2f(10, 10)),
     sf::Vertex(sf::Vector2f(100,   0), sf::Color::Red, sf::Vector2f(10,  0))
 };

 // draw it
 window.draw(vertices, 4, sf::Quads);

The example from the documentation is simple enough, but how do you tell it what/where the texture is that these coordinates (10x10) refer to?

Thanks.

11
General / Visual Studio 2010 forgets open files in my project
« on: April 07, 2012, 04:44:00 pm »
This has only just started happening today with one particular project and I was thinking someone might have experienced it before.

So I have some open files in VS, but when I close VS, reopen it and open that project again... no files are open.

Has it been corrupted in some way?

I noticed the exact question on Experts Exchange, but I'm not subscribed to that site.

12
General / Visual Studio and custom User Type Names
« on: December 26, 2011, 09:47:16 pm »
Guys, has anyone sucessfully managed to make your own type names in VS (2010) show up as blue?

You know, like defining a Uint8 or a sf::Vector2f so they appear as blue in your project like the standard types do?

It's only a little thing, but it would make the code that much more readable, don't you agree?

I've searched the topic way back and again tonight, still can't find a satisfactory solution. I'm not the best programmer, but I'm a sucker for clean, readable formatting.

13
SFML projects / [WIP] JoE Shooter
« on: August 27, 2011, 05:22:48 pm »
Here's some video of a game I've been working on, no title yet and not sure if I'll eventually finish it.
It's the PC continuation of a game I was making on the C64 a long time ago!  :lol:

Started learning C++ at the beginning of March and was lucky enough to come across SFML, which greatly accelerated the development process! Thanks Laurent!  :D

Uses SFML 1.6, graphics and sounds by me apart from the ship. Gilby is used with the enthusiastic permission of Jeff Minter at Llamasoft.

JustinTV clip:
http://justin.tv/computer_store_cat/b/293610941

Next up will probably be some attack waves for cannon fodder, then some more interesting entities; one of which will work in packs to steal the colours you have collected.

14
Graphics / sf::Texture - Unhandled Exception
« on: August 22, 2011, 09:28:37 pm »
I've just changed to SFML2 from 1.6. Previously in my code I declared all of my sf::Image sprite images outside main() so I don't have to keep passing around loads of refs.

Doing the same with sf::Texture, like this:

Code: [Select]
sf::Texture texture;

int main()
 {
     // Create the main window
     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

     // Load a sprite to display
// sf::Texture texture;

     if (!texture.LoadFromFile("ball.png"))
         return EXIT_FAILURE;

sf::Sprite sprite(texture);


causes the first line above to throw "Unhandled exception at 0x775f22c2 in Window Events.exe: 0xC0000005: Access violation writing location 0x00000004." at :

Code: [Select]
void MutexImpl::Lock()
{
    EnterCriticalSection(&myMutex);
}



Unusual? Or, just me being a lousy programmer?  :oops:

15
Graphics / Blur post-effect and cpu
« on: May 13, 2011, 09:03:16 pm »
Just a quickie. I've dropped in the tutorial blur effect to soften up my overall output image, but is it normal behaviour for it to increase CPU usage by quite a lot?

Running in Release, it was usually < 5% usage, with the post-effect it's about 20%.

I'm on SFML 1.6.

Oh and my usual CPU 'spike' in the project is not added on top of that 20%.

Mark

Pages: [1] 2