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

Author Topic: change shader origin from renderwindows to rendertexture  (Read 1312 times)

0 Members and 1 Guest are viewing this topic.

hechelion

  • Newbie
  • *
  • Posts: 24
    • View Profile
change shader origin from renderwindows to rendertexture
« on: July 31, 2022, 08:58:07 am »
Hi.

It is the first time that I use a shader and after reading several tutorials and a lot of trial and error I have managed to program a shader to imitate an LCD screen.

My game resolution is 160x144 pixels, so I use a rendertexture to draw all the game content and then I scale and center the rendertexture over the windowstexture which is created with a more compatible resolution. (see attached image).



The problem is that the shader uses the bottom-left corner of the windowstexture as the point of origin and depending on the resolution, when centering the rendertexture, it turns out that the shader does not synchronize with the game image.



One solution is to pass the position of the rendertexture to the shader, but since I'm new to shaders, I was wondering if there isn't a way to tell the shader that I want the origin point to be the bottom-left corner of the rendertexture?


Shader
Code: [Select]
uniform sampler2D texture;

uniform float brillo;
uniform float linea;
uniform float scale;

vec4 vecbrillo = vec4( brillo+0.5, brillo+0.5, brillo+0.5 , 1.0);
vec4 veclinea = vec4( linea, linea, linea , 1.0);

void main( void ) {
// lookup the pixel in the texture
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);

//vec2 xy = gl_FragCoord.xy;
vec2 xy = gl_FragCoord.xy;
if(floor(mod(xy.y,scale)) == 0 || floor(mod(xy.x,scale)) == 0){
        pixel = veclinea * pixel;
    }else{
if(pixel.r<0.05){ pixel.r = 0.1;}
if(pixel.g<0.05){ pixel.g = 0.1;}
if(pixel.b<0.05){ pixel.b = 0.1;}
}

gl_FragColor = vecbrillo * pixel;
}

C#
Code: [Select]
// ACTUALIZAR DISPLAY
                oGame.gameWindow.Display();
                sprGameWindow = new Sprite(oGame.gameWindow.Texture);
                sprGameWindow.Scale = new Vector2f(scalewindows,scalewindows);
                sprGameWindow.Position = new Vector2f(200,0);

                //fragShader.SetUniform("resolution", new Vector2f(400,400));
                //fragShader.SetUniform("time", clk.ElapsedTime.AsSeconds() );
                //fragShader.SetUniform("mouse", new Vector2f(Mouse.GetPosition().X, Mouse.GetPosition().Y));
                if (Keyboard.IsKeyPressed(Keyboard.Key.Up)) { brillo += (float)0.01; }
                if (Keyboard.IsKeyPressed(Keyboard.Key.Down)) { brillo -= (float)0.01; }
                if (brillo > 1) { brillo = 1;}
                if (brillo < 0) { brillo = 0;}
                fragShader.SetUniform("brillo", brillo);
                fragShader.SetUniform("linea", (float)0.1);
                fragShader.SetUniform("scale", (float)scalewindows);

                window.Draw(sprGameWindow, new RenderStates(fragShader));
               
                window.Display();

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: change shader origin from renderwindows to rendertexture
« Reply #1 on: August 02, 2022, 12:43:33 am »
Are you calling Display() on the render texture?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

hechelion

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: change shader origin from renderwindows to rendertexture
« Reply #2 on: August 02, 2022, 02:48:26 am »
Hi, as always, thanks for your answers


Short answer, yes.

But thanks to your answer, I realized something,  I need call for a display after pass the shader.

Originally my loop was:
Code: [Select]
- Draw sprite from game to a rendertexture named oGame.gameWindow.
- oGame.gameWindow.Display();
- Convert de renderTexture to a sprite named sprGameWindow.
- Scale and center the sprite sprGameWindow.

- Pass the new sprite and the shader to windowsrender:
window.Draw(sprGameWindow, new RenderStates(fragShader));

From what I understand, gl_FragCoord is returning the coordinate of the object that calls draw() (windowrender in this case) and not the object that I pass as a parameter (sprGameWindow).

With that in mind, I changed my loop, adding a temporary renderTexture that calls the draw function and now works it as intended.

New loop:
Code: [Select]
- Draw sprite from game to a rendertexture named oGame.gameWindow.
- oGame.gameWindow.Display();
- Convert de renderTexture to a sprite named sprGameWindow.
- Scale the sprite sprGameWindow.

- Pass the sprite sprGameWindow and the shader to tempRenderTexture:
tempRenderTexture.Draw(sprGameWindow, new RenderStates(fragShader));
tempRenderTexture.Display().

- Convert de temprenderTexture to a new sprite.
- Center the new sprite.

window.Draw(tempRenderTexture);

Thank you very much eXpl0it3r
« Last Edit: August 02, 2022, 02:52:15 am by hechelion »