Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: How to draw a line between GL_NEAREST 1D texture colors ?

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    6

    How to draw a line between GL_NEAREST 1D texture colors ?

    I have a 1d texture with GL_NEAREST filtering.
    I have also a textured mesh like the following picture:

    Click image for larger version. 

Name:	lala0.5.jpg 
Views:	120 
Size:	10.8 KB 
ID:	899

    I'm trying to draw a black line (1px width) in between texture colors like the following picture:

    Click image for larger version. 

Name:	lala0.6.jpg 
Views:	136 
Size:	16.2 KB 
ID:	900

    How can i achived that?

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    You could do it with 2 passes. Render the coloured image to a texture then render whole window triangle (or quad) and in the fragment shader read the texture pixels around the fragment and see if they are different colours.

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    6
    The texture pixels can have different color because of the lighting. It may work in some areas but i don't think it will work in general.
    I was thinking of something like a fragment shader :


    Code :
    uniform sampler1D texture;
     
    void main(void){
      if(gl_TexCoord[0].x > 0.1 && gl_TexCoord[0].x < 0.11 ){
        gl_FragColor = vec4(0); // black pixel in the area between texture colors 1 & 2
      }else if( gl_TexCoord[0].x > 0.2 && gl_TexCoord[0].x < 0.21 ){
        gl_FragColor = vec4(0); // black pixel in the area between texture colors 2 & 3
      }....{
      }else{
        gl_FragColor = gl_Color*texture1D(texture,gl_TexCoord[0].x); // area not between texture colors
      }
    }

    The problem is that the "black" transition areas are not 1pixel in width. Their size depends on the view.

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    I think you are right - you could achieve that more cheaply by having a black pixel between each colour change in the 1D texture.

    I know it is wasteful but you could render 2 texture outputs one with lighting and one without in the first pass. Then use the unlit texture to find an edge and update the other with the black pixel or none.

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    6
    I thought of the 1px black color in texture. But this does not solve the view depended size problem.

    The number of triangles is > 10 million so i believe the 2 passes or 2 texture outputs will affect performance.

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    The texture size is only dependant on the screen size not the number of triangles, and the second render is a simple 3 vertex triangle covering the whole screen so it is quite quick. I regularly do this for contour lines with 7-8 million triangles. If you are rendering 10+ million you need to look at some level of detail options or you will hit other performance problems.

    I had another thought last night. You could look at the dFdx/dFdy of the texture uv values. If these are less than a small delta adjacent pixels will be selecting the same colour otherwise they will be selecting different colours so you are at a boundary. I am not sure how accurate this would be but it is pretty simple to test. You may need to have the 1D texture have 5-10 pixels of the same colour so the boundary is more accurate.

  7. #7
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    6
    The dFdx method for the texture colors works but the black lines are segmented. probably the accuracy problem you mention already. How can I improve the accuracy?

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Sorry I can't help you there - someone else might have an idea

Posting Permissions

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