kalos
kalos

Reputation: 783

How to apply texture to a part of a sphere

I am trying to put a texture in only a part of a sphere. I have a sphere representing the earth with its topography and a terrain texture for a part of the globe, say satellite map for Italy. I want to show that terrain over the part of the sphere where Italy is.

I'm creating my sphere drawing a set of triangle strips. As far as I understand, if I want to use a texture I need to specify a texture coord for each vertex (glTexCoord2*). But I do not have a valid texture for all of them. So how do I tell OpenGL to skip texture for those vertexes?

Upvotes: 1

Views: 842

Answers (1)

KillianDS
KillianDS

Reputation: 17176

I'll assume you have two textures or a color attribute for the remainder of the sphere ("not Italy").

The easiest way to do this would be to create a texture that covers the whole sphere, but use the alpha channel. For example, use alpha=1 for "not italy" and alpha=0 for "italy". Then you could do something like this in your fragment shader (pseudo-code, I did not test anything):

...
uniform sampler2D extra_texture;
in vec2 texture_coords;
out vec3 final_color;
...
void main() {
    ...
    // Assume color1 to be the base color for the sphere, no matter how you get it (attribute/texture), it has at least 3 components.
    vec4 color2 = texture(extra_texture, texture_coords);
    final_color = mix(vec3(color2), vec3(color1), color2.a);
}

The colors in mix are combined as follows, mix(x,y,a) = x*(1-a)+y*a, this is done component wise for vectors. So you can see that if alpha=1 ("not Italy"), color1 will be picked, and vice versa for alpha=0.

You could extend this to multiple layers using texture arrays or something similar, but I'd keep it simple 2-layer to begin with.

Upvotes: 2

Related Questions