PDA

View Full Version : problem of Multitexture



keepmoving
07-01-2010, 12:43 AM
I want to apply two textures to a single fragment,but I don't want to use the API glMultiTexCoord2f because I want to make it without the help of hardware. Then what should I do?
I have ever noticed that someone said in his blog that mutiple rendering can help me,but I don't know what's that mean.
I'm a beginners of opengl,so the answer of you is the more detailed the better.
thank you.

GL Starter
07-01-2010, 06:55 AM
U can use blending to combine two textures.

// Here enables blending
glEnable( GL_BLEND );
// give source factor as source color and Destination factor as dest color.
glBlendFunc( GL_SRC_COLOR, GL_DST_COLOR );

// Clears Framebuffer
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Draw first texture to framebuffer.
glBindTexture( GL_TEXTURE_2D, g_textureID );
glInterleavedArrays( GL_T2F_V3F, 0, g_quadVertices );
glDrawArrays( GL_QUADS, 0, 4 );

// Draw second texture to framebuffer. This drawing will combine previous data in framebuffer.
glBindTexture( GL_TEXTURE_2D, g_textureID1 );
glInterleavedArrays( GL_T2F_V3F, 0, g_quadVertices );
glDrawArrays( GL_QUADS, 0, 4 );

// Display to screen.
SwapBuffers( g_hDC );

If texture is alpha enabled, then u can use other blend functions.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

And also easy to combine two textures with a shader.
Here is a simple example of combining two textures with a shader.
http://www.codeproject.com/KB/openGL/GLSLShader.aspx

keepmoving
07-01-2010, 08:47 AM
[quote=GL Starter]U can use blending to combine two textures.

// Here enables blending
glEnable( GL_BLEND );
// give source factor as source color and Destination factor as dest color.
glBlendFunc( GL_SRC_COLOR, GL_DST_COLOR );

// Clears Framebuffer
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Draw first texture to framebuffer.
glBindTexture( GL_TEXTURE_2D, g_textureID );
glInterleavedArrays( GL_T2F_V3F, 0, g_quadVertices );
glDrawArrays( GL_QUADS, 0, 4 );

// Draw second texture to framebuffer. This drawing will combine previous data in framebuffer.
glBindTexture( GL_TEXTURE_2D, g_textureID1 );
glInterleavedArrays( GL_T2F_V3F, 0, g_quadVertices );
glDrawArrays( GL_QUADS, 0, 4 );

// Display to screen.
SwapBuffers( g_hDC );

If texture is alpha enabled, then u can use other blend functions.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

And also easy to combine two textures with a shader.

keepmoving
07-01-2010, 06:09 PM
[quote=GL Starter]U can use blending to combine two textures.

// Here enables blending
glEnable( GL_BLEND );
// give source factor as source color and Destination factor as dest color.
glBlendFunc( GL_SRC_COLOR, GL_DST_COLOR );

// Clears Framebuffer
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Draw first texture to framebuffer.
glBindTexture( GL_TEXTURE_2D, g_textureID );
glInterleavedArrays( GL_T2F_V3F, 0, g_quadVertices );
glDrawArrays( GL_QUADS, 0, 4 );

// Draw second texture to framebuffer. This drawing will combine previous data in framebuffer.
glBindTexture( GL_TEXTURE_2D, g_textureID1 );
glInterleavedArrays( GL_T2F_V3F, 0, g_quadVertices );
glDrawArrays( GL_QUADS, 0, 4 );

// Display to screen.
SwapBuffers( g_hDC );

If texture is alpha enabled, then u can use other blend functions.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

And also easy to combine two textures with a shader.

it seems that the web page has been changed,now i reedit it here.


Here is my code:
//calculate the texture coordinate *s_texture,*t_texture for texture[0] and *s_texture1,*t_texutre1 for texture[1]
CalTextureCoord();
CalTextureCoord1();

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_COLOR, GL_DST_COLOR );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textures[1]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBegin(GL_TRIANGLES);
for (i=0; i <numfaces/3; i++)
{
j = 3*i;
if(materialsapplied) glColor4f( materials[matfaces[i]].diffuseColor[0], materials[matfaces[i]].diffuseColor[1], materials[matfaces[i]].diffuseColor[2], 1/materials[matfaces[i]].transparency );
else glColor3f( 0.0f, 0.0f, 1.0f );
::glNormal3f( nx[i], ny[i], nz[i]);

::glTexCoord2f(s_texture1[faces[j]], t_texture1[faces[j]]);
::glVertex3f( x[faces[j]] , y[faces[j]], z[faces[j]]);
::glTexCoord2f(s_texture1[faces[j+1]], t_texture1[faces[j+1]]);
::glVertex3f( x[faces[j+1]], y[faces[j+1]], z[faces[j+1]]);
::glTexCoord2f(s_texture1[faces[j+2]], t_texture1[faces[j+2]]);
::glVertex3f( x[faces[j+2]], y[faces[j+2]], z[faces[j+2]]);

}
glEnd();

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textures[0]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBegin(GL_TRIANGLES);
for (i=0; i <numfaces/3; i++)
{
j = 3*i;
if(materialsapplied) glColor4f( materials[matfaces[i]].diffuseColor[0], materials[matfaces[i]].diffuseColor[1], materials[matfaces[i]].diffuseColor[2], 1/materials[matfaces[i]].transparency );
else glColor3f( 0.0f, 0.0f, 1.0f );
::glNormal3f( nx[i], ny[i], nz[i]);

::glTexCoord2f(s_texture[faces[j]], t_texture[faces[j]]);
::glVertex3f( x[faces[j]] , y[faces[j]] , z[faces[j]]);
::glTexCoord2f(s_texture[faces[j+1]], t_texture[faces[j+1]]);
::glVertex3f( x[faces[j+1]], y[faces[j+1]], z[faces[j+1]]);
::glTexCoord2f(s_texture[faces[j+2]], t_texture[faces[j+2]]);
::glVertex3f( x[faces[j+2]], y[faces[j+2]], z[faces[j+2]]);

}
glEnd();

what puzzled me is the second applied texture(here is texture[0]) is not be displayed.

GL Starter
07-02-2010, 08:48 PM
What is your texture format?
If its GL_RGBA, then you can use alpha blending to combine two textures.
If color of one texture is brighter, then above method
(glBlendFunc (GL_SRC_COLOR, GL_DST_COLOR );) will not create exact combination of two textutes.