PDA

View Full Version : texture question



Melekor
11-25-2002, 09:47 PM
ok new thread - the old one was getting stale because the title no longer properly described the question.

How can you set up opengl to use the alpha from one texture (or the color of that texture as an alpha) - To render another texture with?

eg. texture 1: sprite image, 8 bit paletted with the paletted_texture extension.

texture 2: sprite alpha, 8 bit paletted with the paletted_texture extension, but using only greys (yes this must be paletted and not simply greyscale)

now I want to use texture 2 as the alpha for texture 1. how?

*** I am using palettes because of the effects that can be done with them, so I don't want to hear that I should be using rgba please ***

Thanks in advance for your help, and taking the time to read this sortof long post.
-Melekor

Tom Nuydens
11-26-2002, 01:33 AM
Use GL_ARB_texture_env_combine. Set GL_TEXTURE_ENV_MODE to GL_COMBINE, then set up your second texture unit to take RGB from the first unit (GL_OPERAND0_RGB = GL_PREVIOUS) and alpha from the second unit (GL_OPERAND0_ALPHA = GL_TEXTURE). The combine mode is GL_REPLACE for both RGB and alpha.

-- Tom

Melekor
11-26-2002, 04:49 PM
Ok after a while I figured out how to use the combine thing, but it was extremely confusing. I got it working by saying

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

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_Texture[0]);

glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_Texture[1]);

glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_CO MBINE);
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_REPLACE );
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_PREVIOU S);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_ALPHA,GL_SRC_ COLOR);

glBegin(GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0,0,0);
glMultiTexCoord2f(GL_TEXTURE1,0,0);
glVertex2i(0,0);
glMultiTexCoord2f(GL_TEXTURE0,1,0);
glMultiTexCoord2f(GL_TEXTURE1,1,0);
glVertex2i(256,0);
glMultiTexCoord2f(GL_TEXTURE0,1,1);
glMultiTexCoord2f(GL_TEXTURE1,1,1);
glVertex2i(256,256);
glMultiTexCoord2f(GL_TEXTURE0,0,1);
glMultiTexCoord2f(GL_TEXTURE1,0,1);
glVertex2i(0,256);
glEnd();

but I couldnt make it work with only single texture coords (gltexcoord2f). Do you have to use the multitexcoords to do this?

rgpc
11-26-2002, 07:14 PM
There's nothing to stop you from creating the necessary RGBA data in memory (ie. Load Tex1, Load Tex2, Create a dummy array [Tex3] for each RGBA set Tex3.RGB=Tex1.RGB, Tex3.A=Tex2.A)

Other than than Multitex is your only option (as you are using Multiple textures)...

Melekor
11-26-2002, 07:18 PM
oh ok using 2 is fine, I just thought since you combine them into one why cant you only specify one set of texture coordinates but it doesnt really matter, Its working now, thanks everyone.

jwatte
11-26-2002, 08:49 PM
Note that you can specify the SAME texture coordinate array for both texture units. You pass the same pointer to GL twice. The card will figure it out and only pull the data across the bus once.

Miguel_dup1
11-26-2002, 10:35 PM
You can try this for multitexturing with the alpha channel (and or color if you want a third texture).

// Texture 1
glActiveTexture(GL_TEXTURE0 );
glEnable( GL_TEXTURE_2D ); glBindTexture( GL_TEXTURE_2D, textures.unit[0] );
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
glEnable( GL_TEXTURE_GEN_S );

glEnable( GL_TEXTURE_GEN_T );

glTexGenfv( GL_S, GL_OBJECT_PLANE, sPlane01 );
glTexGenfv( GL_T, GL_OBJECT_PLANE, tPlane01 );

// Texture 2
glActiveTexture( GL_TEXTURE1 ); glEnable( GL_TEXTURE_2D ); glBindTexture( GL_TEXTURE_2D, textures.unit[1] );
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE ); // Setting up Source0*Source2 + Source1*(1-Source2)
glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS );
glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR ); // Gets the RGB of the selected texture (texture00)
glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE ); // Selects the current texture.
glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR ); // Gets the RGB of the selected texture (texture01)
glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PRIMARY_COLOR ); // Selects the primary colors of both textures
glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA ); // Combines both primary colors from each texture with the given alpha value

glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR ); // Configuring texture coordinate generation
glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR ); // Configuring texture coordinate generation
glEnable( GL_TEXTURE_GEN_S );
glEnable( GL_TEXTURE_GEN_T );
glTexGenfv( GL_S, GL_OBJECT_PLANE,
sPlane02 );
glTexGenfv( GL_T, GL_OBJECT_PLANE,
tPlane02 );


Draw the object and provide your alpha value through the glColor4xx

glColor4ub( 255, 255, 255, AlphaMap[ INDEX01 ] ); // Providing the alpha and color factors for texturing
glVertex3i( ApexX, HeightMap[ INDEX01 ], ApexZ ); // Vertex 1 (Apex).


glColor4ub( 255, 255, 255, AlphaMap[ INDEX02 ] ); // Providing the alpha and color factors for texturing
glVertex3i( RightX, HeightMap[ INDEX02 ], RightZ ); // Vertex 2 (Right vertex)

glColor4ub( 255, 255, 255, AlphaMap[ INDEX03 ] ); // Providing the alpha and color factors for texturing
glVertex3i( LeftX, HeightMap[ INDEX03 ], LeftZ ); // Vertex 3 (Left vexter)

Disable what you enabled... http://www.opengl.org/discussion_boards/ubb/smile.gif

This is taken from my terrain engine, so you may have to change a thing or two for you own purposes.



Originally posted by Melekor:
Ok after a while I figured out how to use the combine thing, but it was extremely confusing. I got it working by saying

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

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_Texture[0]);

glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_Texture[1]);

glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_CO MBINE);
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_REPLACE );
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_PREVIOU S);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_ALPHA,GL_SRC_ COLOR);

glBegin(GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0,0,0);
glMultiTexCoord2f(GL_TEXTURE1,0,0);
glVertex2i(0,0);
glMultiTexCoord2f(GL_TEXTURE0,1,0);
glMultiTexCoord2f(GL_TEXTURE1,1,0);
glVertex2i(256,0);
glMultiTexCoord2f(GL_TEXTURE0,1,1);
glMultiTexCoord2f(GL_TEXTURE1,1,1);
glVertex2i(256,256);
glMultiTexCoord2f(GL_TEXTURE0,0,1);
glMultiTexCoord2f(GL_TEXTURE1,0,1);
glVertex2i(0,256);
glEnd();

but I couldnt make it work with only single texture coords (gltexcoord2f). Do you have to use the multitexcoords to do this?

Melekor
11-27-2002, 06:57 PM
Thanks but I can't use vertex alpha because I need per-pixel. The 2 textures will do tho.