Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: texture question

  1. #1
    Intern Newbie
    Join Date
    Nov 2002
    Posts
    37

    texture question

    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

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Oct 2000
    Location
    Belgium
    Posts
    857

    Re: texture question

    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

  3. #3
    Intern Newbie
    Join Date
    Nov 2002
    Posts
    37

    Re: texture question

    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?

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2000
    Location
    Melbourne,Victoria,Australia
    Posts
    767

    Re: texture question

    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)...

  5. #5
    Intern Newbie
    Join Date
    Nov 2002
    Posts
    37

    Re: texture question

    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.

  6. #6
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    2,704

    Re: texture question

    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.
    "If you can't afford to do something right,
    you'd better make sure you can afford to do it wrong!"

  7. #7
    Member Regular Contributor
    Join Date
    Jan 2002
    Posts
    296

    Re: texture question

    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...

    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?

  8. #8
    Intern Newbie
    Join Date
    Nov 2002
    Posts
    37

    Re: texture question

    Thanks but I can't use vertex alpha because I need per-pixel. The 2 textures will do tho.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •