PDA

View Full Version : Blend of opengl textures



Windsor
02-13-2014, 05:01 AM
Hi everyone.

In opengl, I don't know how to blend two texture. The effect is varing one texture to another.
Here are the functions of the effect. eg. f_1(x) is one texture, f_2(x) is another.
And the combined texture is g(x)= t * f_1(x) + (1-t) * f_2(x) t:[0,1].
How to make it reality?
Can you give me some methods.
Thanks .

Brokenmind
02-13-2014, 05:21 AM
Generally, with blending, you need to call some functions beforehand:



glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


The first line activates blending in general. The second line corresponds to your t and (1-t).

The rest is easiest done in a shader:



#version 330

uniform sampler2D texture1;
uniform sampler2D texture2;

// can also be uniform
const float partOf1 = 0.5f;

in vec2 texCoord;
out vec4 color;

void main()
{
color = texture(texture1, texCoord) * vec4(1.0f, 1.0f, 1.0f, partOf1) + texture(texture2, texCoord) * vec4(1.0f, 1.0f, 1.0f, 1.0f - partOf1);
}


In the shader, you can do much more with the values than just [t, 1-t].

Without a shader, the solution would look like this:



const float partOf1 = 0.5f;

glBindTexture(GL_TEXTURE_2D, texture1);
glColor4f(1.0f, 1.0f, 1.0f, partOf1 );
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f( -1.0f, -1.0f, 0.0);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0);
glTexCoord2f(0.0f, 1.0f); glVertex3f( -1.0f, 1.0f, 0.0);
glEnd();

glBindTexture(GL_TEXTURE_2D, texture2);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f - partOf1);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f( -1.0f, -1.0f, 0.0);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0);
glTexCoord2f(0.0f, 1.0f); glVertex3f( -1.0f, 1.0f, 0.0);
glEnd();

Windsor
02-13-2014, 05:36 AM
ok ,thanks very much ..