# Circular 1D lookup texture

Hi guys,

I got a difficulty showing a triangle using a lookup in a ‘circular’ texture. Allow me to explain it with the following picture:

As you can see the standard OpenGL linear interpolation of texture coordinates will not be sufficient for my goal. I hope someone else already conquered this problem or someone has an idea on how to solve this.
Although I am hoping for a solution that uses OpenGL fixed functionality, I have no problem using shaders.

Regards Ronald.

So you don’t want to interpolate texture coordinates, but colors directly?
Just disable the texture and apply colors to vertices.

In the simple example I gave that would be a correct solution. But with a more complex lookup texture ( more colors ) the result would not be correct. I would like to see all the colors from the lookup texture.

You want something weird. You had all colors in the first try when interpolating texture coordinates - you were not satisfied.

First, tell us what exactly do you want to achieve in a general case.

I agree,

Bear with me. You will catch my drift.

In the OP example, if you use OpengGL for linear interpolating of the texture coordinates, it will interpolate the bottom side of the triangle from 0.1->0.9 like this:
0.1,…,0.2,…,0.3,…,0.4,…,0.5,…,0.6,…0.7,…0.8,…0.9.
What I would like to see is:
0.1,…,0.0/1.0,…,0.9.

Take a unit circle for instance when you go round till 360 deg, you will start at 0 again.
Suppose that my 0.1 in the example is 0.1 * 360 = 36 degrees and the 0.9 in the example is 0.9 * 360 degrees = 324.
When I have a normalized lookup texture with x colors for this unit circle. I can map each value to a color in the lookup texture.
I know, OpenGL doesn’t, that when I have a vertex with 0.1 (36 degrees) and that is connected to a vertex with 0.9 (324 degrees) that in-between I want interpolation going past 360/0 degrees. To be exact on the half of the line connecting these vertices I want the lookup to be 1.0/0.0.
Notice that I write 1.0/0.0 this is not a mistake. In order to have a correct effect, you will have to have a lookup texture that is ‘circular’ too. Meaning the color of the start & end point are the same.

Do I make any sense now?

Yes, it makes a kind of sense, thanks:)

However, I’m not aware of how to do circular mapping in general case. If I come to some idea, I’ll get back here.

Well you should be able to do that with more explicit texcoords :
“What I would like to see is:
0.1,…,0.0/1.0,…,0.9.”

use 0.1-1 (ie. -0.9)as first texcoord and it will automagically work, thanks to the default GL_REPEAT mode of texture mapping.

Not really. Setting -0.9 on the first vertex will produce incorrect interpolation to 0.5 one.

Right, sorry I still do not understand what the OP wants…

Circular mapping is ancient history dude. Something from from the Geforce 256 IIRC.
You could try to insert a new vertex like it was already suggested or do a dependent texture read.
newtexcoord = texture2D(Tex0, Texcoord);
texel = texture2D(Tex1, newtexcoord);

where newtexcoord is the values that you want.

So, wyou want interpolation from 0.1 to 0.9 to wrap aroun 0.0?

There’s a simple way of forcing it - simply pass 1.1 and 0.9 as coordinates.
A simple rule is to check if distance between coords is greater than 0.5 - if it is, then add 1.0 to lower coordinate.
This produces discontinuity though - as your texcoords move away from each other, they will suddenly flip when distance exceeds 0.5.

And yes - it is weird. I don’t know what you need it for, but I’m sure there’s a better way of achieving your goal.

Many thanks all for the replies.

@ k_szczech:

Yes, you’re completely right. At the center of the triangle something strange will happen. Maybe the color will go to white. Don’t know yet.

What I am really curious about is to test what you said. The problem I have is that I have no idea on how to check at this distance.
If I am correct, the vertex shader doesn’t know about another vertex. And the fragment shader doesn’t know about any other fragment. So I would need some help here.

Also:

And yes - it is weird. I don’t know what you need it for, but I’m sure there’s a better way of achieving your goal.

I was thinking the same, part of the reason why I post this here.

I don’t know about the ancient history. I need this functionality in my application though.

Adding vertices is not an option I think, since this would be an infinite amount of them.
And about the dependent texture read. I still would have to know distance of the texture coords between two vertices. That’s something I am not sure about on how to implement.

You might try and combine k_szczech’s suggestion with geometry shaders (if you are willing to use them):

In the geometry shader, check two texture coordinates of the primitive and add 1 to the texture coordinate if necessary. You will need some way to determine whether you want the behavior shown in the first image, or the behavior shown in the second image. If you don’t have a way to determine this from the primitive data and its texture coordinates, you will have to reevaluate your choices for texture coordinates.

A simple algorithm could be like k_szczech said: always take the shortest route from one texture coordinate to another. Other options could be: always go clockwise/anti-clockwise over the lookup texture or always take the longest route.

Thank you Heiko,

What you said could be a solution but too bad geometry shader is not an option at the moment, since the application has to run on NVida 6800 and up.

I just stumbled upon GL_MIRRORED_REPEAT as value for texture wrapping.

I’ve never used it, but it kinda sounds like it could be what you need. From the description I make up the following:

The fractional part of the coordinate if the integer part is even (so 0.1/2.1/4.1 becomes 0.1) and 1 minus the fractional part of the coordinate if the integer part is odd (so 1.1/3.1/5.1 becomes 1 - 0.1 = 0.9).

Let me know if this is what you’re looking for.

Perhaps if this is not working for you, it might be working if you change your lookup texture and the texture coordinates as well. If you would use GL_MIRRORED_REPEAT in combination with half the lookup texture you have (scale the 0.0 - 0.5 part over 0.0 - 1.0), what would happen than? Say: your lookup texture is:

``````
texture coordinates 0    1
colour values       0.0  1.0 (say ranging from red to white)

``````

Now texturing gives the following:

``````
texture coordinates: 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4, ...
colour values:       0.0 0.2 0.4 0.6 0.8 1.0 0.8 0.6, ...

``````

With such a scheme, your new tex coordinates must be (just multiply them by 2):

``````
1.0
/ \
/   \
/     \
/       \
0.2 ----- 1.8

``````

When doing a lookup with GL_MIRRORED_REPEAT this becomes:

``````
1.0     (which is now white in your lookup texture)
/ \
/   \
/     \
/       \
0.2 ----- 0.2  (which is red in your lookup texture)

``````

Which I believe is what you would like.

edit: thinking somewhat more about this, it probably won’t work eiter, because in the middle of the bottom part the texture coordinate is 1.0 (between 0.2 and 1.8) which becomes just 1.0 (and thus you will still have the undesired effect).

Heiko, thanks for the reply. No time to go into this now since I am doing something different with raytracing atm. Less work please …

I will get back here asap though. This is still something I have not found a solution for.