# Is this interpolation possible in GLSL?

This topic is 2988 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm creating a full 3d terrain (voxel-based) and I'm now working on the shading.
Using triplanar texturing, the textures itself come from a tile texture atlas that contains 16 textures.
For each vertex a define a tileIndex. Everything works, as you can in the image below. Right now I'm using a 'flat varying' to keep a tileIndex constant along all fragments of a triangle. However, to get the result I want, I need to blend between the 3 tileIndices of a triangle.
Ideally I would receive the 3 tileIndices and somehow calculate the 3 blend factors. I'm not sure if this is something trivial and I'm forgetting something, or there's now way to do it with GLSL?

##### Share on other sites
Hi,

What the fragment shader does is interpolate between vertices defining geometry. So, you could have it interpolate between the texture indices. However, that's likely not what you want (what does the 1.6th texture look like?)

The way I would approach this is with weighting. Assign each triangle a list of texture indices to use (constant for each vertex). Then, set the weights of each of those indices in the vertex shader (potentially different for each vertex). E.g.:
varying vec3 weights = vec3(1.0,0.0,0.0)
Then, in the fragment shader, the weights will be interpolated across the fragment. E.g.:
weights == vec3(0.8,0.4,0.1)
Then, at each fragment, sample each of the three textures, and scale them by
weights
In practice, I would set a weight for each texture that you have:
varying float rock_weight, sand_weight, grass_weight, dirt_weight, etc_weight, ... ;
. . . and then simply define these weights at each vertex. The fragment shader will interpolate them, and then you can just multiply them by the relevant texture samples.

Interesting project,
-G

##### Share on other sites
In my engine I handle this scenario by rendering a triangle several times if it has more than one texture applied. In the worst case it has three textures applied, and so I render it once in black and once for each texture with additive blending. I interpolate alpha values from the vertices to control the blending.

Compared to Geometrian's solution, mine will support a greater number of texture maps with less work on the interpolators, but it also is less flexible in terms of allowing arbitrary mixtures of textures and surfaces. It's also probably more difficult to implement, requiring some work CPU side to duplicate the triangles (or in a geometry shader maybe).

If you are interested, I describe my system more fully in Game Engine Gems, Volume 1.

##### Share on other sites
Sorry for the late reply guys, thank you both!
I first tried Geometrian idea, but I find it somehow limited, having a weight for each texture is not an option, as the number of textures will vary from 1 to 32, or more.

PolyVox idea worked like a charm, however, I found that normal blending works better instead of additive blending. I don't know how you calculate the blends, but in my implementation is not necessary to render a black triagle first.

##### Share on other sites
Quote:
 Original post by RelfosPolyVox idea worked like a charm, however, I found that normal blending works better instead of additive blending. I don't know how you calculate the blends, but in my implementation is not necessary to render a black triagle first.

I needed the black triangle to avoid additivly blending with whatever was already existing in the framebuffer at that position. Maybe you can get around it with normal blending, or maybe by drawing your scene front to back. I'm a bit hazy on exactly what I tried as it was a while ago, but I'll keep your point in mind if/when I come back to it...

##### Share on other sites
Ahh, that's a good point, for now I only have terrain + sky, and the sky is rendered last, probably with more objects this will cause some problems.
However with additive blending it reachs saturation on some points, causing some parts of the terrain to have a strange whiteness. I'm going to investigate improvements to this solution.

##### Share on other sites
Quote:
 Original post by RelfosHowever with additive blending it reachs saturation on some points, causing some parts of the terrain to have a strange whiteness.

When you are drawing three triangles, each of which has and alpha of '1' at one corner and '0' at the other two corners, I don't think the interpolated values sum to one for all points on the triangle. It's similar to the way that, if you interpolate a normal across a triangle, you need to renormalise it for each fragment. I think you need to 'normalise' you combined alpha values.

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633269
• Total Posts
3011153
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!