PDA

View Full Version : Beginner interested in 2D fragment shaders



prince_polka
03-11-2017, 06:25 PM
Hello, I'm new here. I posted in GLSL but it seems it's not getting accepted.
Don't know if mod is slow or if they rejected it...
Anyways not intending to spam but anyways...
I have a bit unusual goal with learning GLSL, I wan't to eventually do things like this...
https://www.youtube.com/watch?v=cd-ZNymt6Ko

But I decided to start with something simpler to learn from, a post-processing anti-aliasing.
But even that is too hard.
I wan't to read from and write to a couple "persistent" textures aswell as gl_FragColor,
so that I can do multiple passes, but just one pass each frame.
Maybe buffer objects is what I need?
Here's what I came up with, which isn't much...



#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D texture;
uniform vec2 u_resolution;
uniform vec3 u_mouse;
uniform float u_time;
float x;
float y;
float p; // pixel
vec4 me;
vec4 n1;
vec4 n2;
vec4 n3;
vec4 merge;

void main () {
x = gl_FragCoord.x/u_resolution.x;
y = 1.0 - gl_FragCoord.y/u_resolution.y ; // flipping texture upside down
p = 1.0/256.0;
// i get error if I don't use u_time and u_mouse,
// but I don't wan't to use them thus min(0.0,max(0.0 ...
p+= min(0.0,max(0.0,u_time+u_mouse.x));
me =texture2D( texture , vec2( x+p , y ) );
n1 = texture2D( texture , vec2( x-p , y ) );
n2 = texture2D( texture , vec2( x , y+p ) );
n3 = texture2D( texture , vec2( x , y-p ) );
merge = (me+n1+n2+n3)/4.0;
gl_FragColor = merge;
}

OceanJeff40
03-20-2017, 11:33 PM
I've done something similar a while ago with MMX. I think it looks like a good algorithm. Correct me if I'm wrong, but this must be your fragment shader code.

Just thinking about this for a second, it's going to take your texture that you send to the shader, and transform it with this averaging algorithm. What you want is the changes to be persistent, so you don't just want to do this once, and save the changes, but kind of dynamically on the back buffer, every frame.

Interesting. Wish I could help, but I'm still just learning myself. I may have to fiddle with this type of code, and after I do, I'll let you know what I found out.

Regards,

Jeff

prince_polka
04-02-2017, 04:49 AM
I've done something similar a while ago with MMX. I think it looks like a good algorithm. Correct me if I'm wrong, but this must be your fragment shader code.

Just thinking about this for a second, it's going to take your texture that you send to the shader, and transform it with this averaging algorithm. What you want is the changes to be persistent, so you don't just want to do this once, and save the changes, but kind of dynamically on the back buffer, every frame.

Interesting. Wish I could help, but I'm still just learning myself. I may have to fiddle with this type of code, and after I do, I'll let you know what I found out.

Regards,

Jeff

I have yet not figured out how to use framers but found a way to fake it more efficiently than previously where I had to use very low resolution, 256*256.
This is now running at 1024*1024 resolution
https://www.youtube.com/watch?v=e5xWsQra6n8

GClements
04-02-2017, 05:23 AM
I wan't to read from and write to a couple "persistent" textures aswell as gl_FragColor,
so that I can do multiple passes, but just one pass each frame.
Maybe buffer objects is what I need?

You need framebuffer objects (FBOs). glGenFramebuffers, glBindFramebuffer, glFramebufferTexture, etc. FBOs allow rendering into a texture which can be read from in a subsequent rendering operation. They require at least OpenGL 3.0 or the EXT_framebuffer_object extension.