Texturing a triangel with 3 weighted textures

Hi,
I want to texture a triangel with three textured which are weighted in a special way: Each texture “represents” a corner of the triangel, having a weight of 1 at that point. The closer to the other side of the triangel the less is the weight, until 0. Every point in the triangel has a summed up weight of 1, consisting of the weights of the three textures.
Anyone knows how to do it with OpenGL ?
A good way would be to create an alpha mask representing the weights, but how to create the alpha mask dynamically, maybe with texturing to the alpha mask by having (1,1,1) at one corner and (0,0,0) at the other two corners and doing gourod shading ?

Thx in advance,

Pubert

You can do it with three-pass rendering.

First you draw the triangle with one texture. Draw the triangle as usual, as if the triangles is supposed to be drawn with only one texture. The texture should cover then entitre triangle with 100% texture at each pixel.

Now, set blend function to glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA), and also enable blending.
Set depthfunction to glDepthFunc(GL_EQUAL), and also disable depthwrites if you want to, glDepthMask(GL_FALSE);

Bind the second texture, and draw the triangle again, but now you set the alphacomponent of the color to either 1.0 or 0.0. 1.0 for the corner where you want the texture to show up, and 0.0 in the other two corners. Do the same for the third texture aswell.

If I set the alpha to 1 in one corner and the alpha to 0 in the others then OpenGL will interpolate the alpha values, right ? Didnt think about that doh

Thx a lot !

Pubert

I’ve got a feeling (without diving into the math admittedly) that the weights in this instance would only add to one on the edge of the triangle. I believe the interior sums will go as low as 2/3.

Hmm, is that so?

Maybe you got me somewhat wrong, not sure though. If you draw the triangle three times, and assigning an alpha of 1.0 to only one vertex in each of the three passes, I can see that in the middle, the wieght will not be one. But won’t it be if you draw the first pass completely opaque, as I said?

Sure no problem with that. I’m simply pointing out that the final blending will not be uniform across the triangle. I did finally look at the math and the surface formed by the sum of weights for each pixel, looks sort of like a dimpled triangle. Now I’m actually looking for a multivariate function that given his boundary conditions would produce uniform blending. Sometimes a math problem hooks me and I feel I need to solve it for some unknown reason .

[This message has been edited by DFrey (edited 02-24-2001).]

Let me know ehen you come to a solution :slight_smile:

Umm, not only did I find a solution. I noticed there are infinite solutions. Now I’m trying to see which of these can be done without pixel shaders.

Well, starting to realize that my solution won’t give you an exact blending between three textures. However, it’s almost correct, right? And almost correct is sometimes good enough, especially if you want speed…

True :slight_smile: The proposed solution is sufficient for me, thx !

Pubert

Yep, definately go with the 3 pass approach. The method I finally came up with is a bit costly, it involves calculating 3 alpha filters for each triangle (1 for each pass). Further more it looks like the filters are unique for each triangle, though I suspect a single filter made for an equilateral triangle could approximate the actual set. Oh well, at least I got a cool texture out of this foray.

Nevertheless im interested in the costly solution to expand my knowledge a bit :slight_smile: Would you mail me or post your solution please ? Even if I wont implement the slow version, I still want to know it :wink:

Pubert

Look here: http://personal.lig.bellsouth.net/~dfrey/filter.gif
This picture shows 1 pass of the simple linear interpolation method and of the method I call first order normalized proportions. Basically what I did was to sum the distance that each pixel was from each vertex. Then divide the total sum into each distance to produce a set of proportions for that pixel (1 for each pass). I then scaled and inverted the values so that it used the full range [0,1]. You probably will not be able to see the difference in the two methods unless you look at the picture with a high magnification.

Though the differences are not worth the effort, I would think… :wink:

Do you know how to create the left picture as an alpha mask dynamically ? Maybe in hardware ? Im really interested !

Pubert