Results 1 to 4 of 4

Thread: Beginner interested in 2D fragment shaders

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2017
    Posts
    8

    Beginner interested in 2D fragment shaders

    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...

    Code :
    #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;
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Dec 2016
    Location
    Ocean Shores, WA USA
    Posts
    119
    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

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2017
    Posts
    8
    Quote Originally Posted by OceanJeff40 View Post
    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

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,112
    Quote Originally Posted by prince_polka View Post
    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.

Similar Threads

  1. Replies: 0
    Last Post: 03-10-2017, 01:49 PM
  2. Help with beginner fragment shader code
    By Hathadar in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-27-2012, 05:57 PM
  3. Shaders do no give the right effect? Beginner
    By SkyLagoon in forum OpenGL: GLSL
    Replies: 4
    Last Post: 05-26-2010, 03:10 PM
  4. Beginner about multipass shaders
    By dorinm in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 11-01-2004, 06:12 AM
  5. Replies: 8
    Last Post: 03-21-2004, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean