Results 1 to 3 of 3

Thread: Help with coordinates problem.

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2018
    Posts
    6

    Help with coordinates problem.

    Hi!
    Im trying to apply this radial shader in multiples spheres, but unsuccessfully.
    the problem is related with screen coordinates. The shader only works if the sphere is in the center of the screen, just like show this pictures:

    frameCount_011086.jpg

    but if the center is not align to camera, this is what happen:

    frameCount_004968.jpg

    as you see, you can see the samples compute in the fragment shader:

    Code :
    #version 150
     
    in vec2 varyingtexcoord;
    uniform sampler2DRect tex0;
    uniform sampler2DRect depth;
     
    uniform vec3 ligthPos;
     float exposure = 0.19;
     float decay = 0.9;
     float density = 2.0;
     float weight = 1.0;
     int samples = 25;
     
    out vec4 fragColor;
    const int MAX_SAMPLES = 100;
    void main()
    {
    //        float sampleDepth = texture(depth, varyingtexcoord).r;
    //    
    //        vec4 H = vec4 (varyingtexcoord.x *2 -1, (1- varyingtexcoord.y)*2 -1, sampleDepth, 1);
    //    
    //    vec4 = mult(H, gl_ViewProjectionMatrix);
     
        vec2 texCoord = varyingtexcoord;
        vec2 deltaTextCoord = texCoord - ligthPos.xy;
        deltaTextCoord *= 1.0 / float(samples) * density;
        vec4 color = texture(tex0, texCoord);
        float illuminationDecay = .6;
        for(int i=0; i < MAX_SAMPLES; i++)
        {
            if(i == samples){
                break;
            }
            texCoord -= deltaTextCoord;
            vec4 sample = texture(tex0, texCoord);
            sample *= illuminationDecay * weight;
            color += sample;
            illuminationDecay *= decay;
        }
        fragColor = color * exposure;
    }

    the shader has an uniform input called ligthPos. I discovered a function in openframeworks called cam.worldToScreen(); if i send the result of this function to the shader, then the sphere is moving in all over the screen without problem. The problem is that i want more than just one sphere, so the logic of this shader is not correct. What you suggest? i want to apply this radial blur (god ray style) to a undefine number of spheres and other shapes. this is an example:

    Captura de pantalla 2019-01-23 a las 6.16.27 a.m..jpg

    what do you think? i notice that the post processing shader (in the first pass i render the geometry, then i pass that buffer to a other fbo with this shader) affects the all screen, but i need a method to fix this. please, i need suggestions. im new in shader, so a didactic example would be great. thanks! O.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,650
    Quote Originally Posted by hgwells View Post
    Im trying to apply this radial shader in multiples spheres, but unsuccessfully.
    ...
    the shader has an uniform input called ligthPos. I discovered a function in openframeworks called cam.worldToScreen(); if i send the result of this function to the shader, then the sphere is moving in all over the screen without problem.

    The problem is that i want more than just one sphere ... What you suggest?
    Well, ignoring optimizations...

    Since you're currently passing in one light to generate one sphere, the simplest thing to do is pass in an array of lights to generate N spheres.

    Alternatively, you could multi-pass this (i.e. do one draw+shader pass per light), but that gets expensive after a point.

    Also keep in mind that if the extent of the sphere's isn't a significant fraction of the area on the screen, you can save quite a bit of fill by limiting the bounds of the quads you're rendering.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2018
    Posts
    6
    thanks. i actually using an array of locations for each lightPos. But this is my problem now. I understand that this kind of post processing shader affect all pixels of the texture that i send it. So, look whats happens: Captura de pantalla 2019-02-05 a las 10.06.12 p.m..jpg

    notice how the effect is computed well for every iteration, but i cant avoid those others samples generated for each iteration. this is the shader:

    Code glsl:
    #version 150
     
    in vec2 varyingtexcoord;
    uniform sampler2DRect tex0;
     
    uniform int size;
     
     
     float exposure = 0.79;
     float decay = 0.9;
     float density = .9;
     float weight = .1;
     int samples = 25;
     
     
    out vec4 fragColor;
    const int MAX_SAMPLES = 25;
    const int N = 3;
    uniform vec2 ligthPos [N];
     
     
    int a = 1;
     
     
    vec4 halo(vec2 pos){
     
     
        float illuminationDecay = 1.2;
        vec2 texCoord = varyingtexcoord;
        vec2 current = pos.xy;
        vec2  deltaTextCoord = texCoord - current;
     
        deltaTextCoord *= 1.0 / float(samples) * density;
        vec4 color = texture(tex0, texCoord);
     
     
        for(int i=0; i < MAX_SAMPLES; i++){
     
            texCoord -= deltaTextCoord;
     
            vec4 sample = texture(tex0, texCoord);
            sample *= illuminationDecay * weight;
            color += sample;
            illuminationDecay *= decay;
     
        }
        return color;
     
    }
     
     
    void main(){
     
        vec2 uv = varyingtexcoord;
     
        vec4 color = texture(tex0, uv);
     
     
     
            vec4 accum = vec4(0.0);
     
            for(int e = 0; e < N;e++){
     
                vec2 current =ligthPos[e];
     
                accum += halo(current);
     
     
     
            }
     
            fragColor = (accum) * exposure;
     
    }
    Last edited by Dark Photon; 02-07-2019 at 08:29 AM.

Similar Threads

  1. coordinates problem
    By bhogasena in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 02-02-2009, 07:09 AM
  2. OpenGL Coordinates Problem
    By Shari in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 08-05-2008, 06:19 PM
  3. problem converting from win coordinates to object coordinates
    By openujs in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 09-07-2006, 01:27 PM
  4. Problem with large coordinates
    By berb in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 06-06-2004, 11:53 PM
  5. coordinates problem
    By in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 10-05-2000, 07:58 PM

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