View Full Version : How to draw a line between GL_NEAREST 1D texture colors ?

11-07-2012, 01:03 AM
I have a 1d texture with GL_NEAREST filtering.
I have also a textured mesh like the following picture:


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


How can i achived that?

11-07-2012, 02:56 PM
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.

11-07-2012, 09:58 PM
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 :

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

11-07-2012, 11:50 PM
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.

11-08-2012, 02:36 AM
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.

11-08-2012, 01:55 PM
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.

11-09-2012, 02:45 PM
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?

11-09-2012, 06:01 PM
Sorry I can't help you there - someone else might have an idea