Jump to content
  • Advertisement
Sign in to follow this  
Merlino

OpenGL Multitexturing, Environnements How To?

This topic is 4287 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am quite new to OpenGL and I was trying to render some textures using multitexturing... So, I have 2 24-bit targa images loaded in 2 different texture objects.. The first image is a grayscale image and the second one a colored image. So lets say: image1 = grayscale -> objectname = Alpha image2 = colored -> objectname = RGB What I wanted to do was to create a texture using the Alpha object as a alpha value and the RGB object as a RGB value... So here's how I set up the textures:
Init()
{
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
Alpha = genTexture("grayscaleImage", true);

glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
RGB = genTexture("coloredImage", true);
}

genTexture(char * filename, bool mipmaps)
{
   if (filename != NULL) this->Load(filename);

    glGenTextures(1, &this->textureObject);
    glBindTexture(GL_TEXTURE_2D, this->textureObject);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    if (mipmaps == true) gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, this->GetWidth(), this->GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, this->GetImage());
    else glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, this->GetWidth(), this->GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, this->GetImage());

    return this->textureObject;
}


Next I tried to use the glTexEnvf() also with GL_COMBINE but with no result, as I didn't really undestand how the different function values work.. ^^ I'm sure I have to use this function in order to make the texturing work, but i don't know how to do it. Can anybody tell me? Thanks, Merlino. [Edited by - Merlino on October 21, 2006 12:31:41 PM]

Share this post


Link to post
Share on other sites
Advertisement
depends with glsl u would use
gl_FragColor = vec4( texture2D( tex0, texcoords ).xyz, texture2D( tex1, texcoords ).w );

Share this post


Link to post
Share on other sites
If you are just loading two textures, it would be much better to combine the two images into a single RGBA image (on the CPU), and use it as a single texture.

However, if you have many greyscale images and many RGB images, and you want to mix and match at runtime, the total number of combined textures could grow too large.

So, with that:




// use a COMBINE texture environment
glActiveTexture(GL_TEXTURE1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

// get RGB values from texture unit 0
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE0);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);

// get ALPHA values from texture unit 1
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE1);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);

// this is all done in texture unit 1. Because the combine modes are REPLACE, we really don't care what the product of texture unit 0 is.





Warning: I've not tried this, and I really haven't used these kind of complex texture environments before. This comes to you, untested, from a reading of the OpenGL 2.0 spec.

This requires ARB_texture_env_crossbar, or OpenGL 1.4

And, as zedzeek said: get to know GLSL. It makes things like this much easier (and you can do a lot more with it). It is a very good thing.

[Edited by - RichardS on October 21, 2006 10:20:55 PM]

Share this post


Link to post
Share on other sites
This is even better, it only requires ARB_texture_env_combine (or OpenGL 1.3). It will work on a wider variety of hardware.


// get RGB values from texture unit 0
glActiveTexture(GL_TEXTURE0);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

// get ALPHA values from texture unit 1
glActiveTexture(GL_TEXTURE1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB GL_SRC_COLOR;

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);




Note that for both of these examples, you'll need to load the alpha texture with an internal format of GL_ALPHA, not GL_RGB.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!