Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: texelFetchBuffer()

  1. #1
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    190

    texelFetchBuffer()

    Hi,
    I am newbie in OpenGL and need some help in texelFetchBuffer().

    The second parameter is specified as index into texel array. But i am not able to understand how to use this function and the indexes.
    I goggled it, but didn't get much info how to use it.
    Please help me..

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    Well, it's a one-dimensional buffer so index is simply in [0, elements * sizeof(type) - 1].

  3. #3
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    190
    Quote Originally Posted by thokra View Post
    Well, it's a one-dimensional buffer so index is simply in [0, elements * sizeof(type) - 1].
    Thank you so much for the reply..

    Now there is one more problem. I am passing vec3 attribute array to vertex shader with z component as a index into pixel array of texture for each corresponding vertices.
    GLfloat vertices[] =
    {
    -0.9f, -0.6f, 0.0f,
    -0.9f, -0.9f, 1.0f,
    -0.6f, -0.9f, 2.0f,
    -0.6f, -0.6f, 3.0f
    };
    Here z component is index into GLfloat pixels[]={0.0f,1.0f,0.0f,1.0f, 0.0f,0.0f,1.0f,1.0f, 1.0f,1.0f,1.0f,1.0f, 1.0f,0.0f,1.0f,1.0f}; My texture is of RGBA type.
    Now in vertex shader:
    out int index=int(a_position.z);

    and fragment shader:
    vec4 offset=texelFetch(out_offset,index);
    Color = offset;
    Now i am drawing GL_Traingle_fan and when i execute it, it draws square with 2 triangles one with white color and second with pink color. It is taking 3rd and 4th offset in pixel array instead taking each index for each vertex.


    I want to draw square with each vertex having color of each index in pixels array. Please correct me where i am going wrong above..

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    First of all, let me correct my previous statement: [0, elements * sizeof(type) - 1] is not the range the indices will be in. Of course, it's simply [0, 1, ..., elements - 1]. It is true, however, that bufferSize = elements * sizeof(type) and that the real offset into the buffer is calculated by sizeof(type) * index.

    Regarding your current issue: Why don't you simply fetch the color at the vertices where the indices are unique and uninterpolated? Simply move your fetch to the vertex shader and have the GPU interpolate the colors not the indices. I'm actually not sure at the moment how int values are interpolated but obviously they are not what you want afterwards.

  5. #5
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    190
    Quote Originally Posted by thokra View Post
    First of all, let me correct my previous statement: [0, elements * sizeof(type) - 1] is not the range the indices will be in. Of course, it's simply [0, 1, ..., elements - 1]. It is true, however, that bufferSize = elements * sizeof(type) and that the real offset into the buffer is calculated by sizeof(type) * index.

    Regarding your current issue: Why don't you simply fetch the color at the vertices where the indices are unique and uninterpolated? Simply move your fetch to the vertex shader and have the GPU interpolate the colors not the indices. I'm actually not sure at the moment how int values are interpolated but obviously they are not what you want afterwards.
    Now, i moved my texelFetch() to fragment shader and passing the fetched texel from vertex to fragment shader. But vertex shader gives compilation failure
    "ERROR: 0:10: error(#202) No matching overloaded function found texelFetch
    ERROR: 0:10: error(#160) Cannot convert from 'const float' to 'default out 4-component vector of float'
    ERROR: error(#273) 2 compilation errors. No code generated"

    My vertex shader:
    in vec3 a_position;
    uniform float offset_x;
    uniform isamplerBuffer out_offset;
    out vec4 offset;
    void main()
    {

    offset=texelFetch(out_offset,a_position.z,0);
    gl_Position = vec4(a_position.x + offset_x, a_position.y, 1.0, 1.0);

    }

    Let me know where i am going wrong

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    Oh man.

    Try the following code and compare it with your code to see how many errors slipped into your version. BTW, speaking of code, use '[code']...'[/code]' tags please the next time.

    Code :
    in vec3 a_position;
    uniform float offset_x;
     
    // that buffer has [I]nothing[/I] to do with offsets. it stores colors, name it accordingly. 
    // also, your buffer stores [I]float[/I] values, so make it a floating point sampler.
    uniform samplerBuffer color_buffer;
     
    // likewise for the vertex color
    out vec4 vertex_color;
     
    void main()
    { 
      // texelFetch on buffer samplers takes only 2 arguments
      vertex_color = texelFetch(color_buffer, int(a_position.z));
     
      gl_Position = vec4(a_position.x + offset_x, a_position.y, 1.0, 1.0);
    }

    This should work. Can't test atm.

  7. #7
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    190
    Quote Originally Posted by thokra View Post
    Oh man.

    Try the following code and compare it with your code to see how many errors slipped into your version. BTW, speaking of code, use '[code']...'[/code]' tags please the next time.

    Code :
    in vec3 a_position;
    uniform float offset_x;
     
    // that buffer has [I]nothing[/I] to do with offsets. it stores colors, name it accordingly. 
    // also, your buffer stores [I]float[/I] values, so make it a floating point sampler.
    uniform samplerBuffer color_buffer;
     
    // likewise for the vertex color
    out vec4 vertex_color;
     
    void main()
    { 
      // texelFetch on buffer samplers takes only 2 arguments
      vertex_color = texelFetch(color_buffer, int(a_position.z));
     
      gl_Position = vec4(a_position.x + offset_x, a_position.y, 1.0, 1.0);
    }

    This should work. Can't test atm.
    forgot to typecast float to int thanks for that ..
    Now, it draws white square. Dono why does it taking index 3 for all the vertices ..

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    I hope you didn't forget to adapt the fragment shader to the new vertex shader, i.e. something like that

    Code :
    in vec4 vertex_color;
    out vec4 frag_color;
     
    // ...
     
    void main()
    {
      // ...
      frag_color = vertex_color;
    }
    Last edited by thokra; 10-01-2012 at 02:23 AM.

  9. #9
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    190
    Quote Originally Posted by thokra View Post
    I hope you didn't forget to adapt the fragment shader to the new vertex shader, i.e. something like that

    Code :
    in vec4 vertex_color;
    out vec4 frag_color;
     
    // ...
     
    void main()
    {
      // ...
      frag_color = vertex color;
    }
    I did check it, but still same issue..

  10. #10
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    Please post both your vertex and fragment shader and you buffer setup code, both for geometry and colors.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •