Textures

I have a texture that I would like to use for some simple trees. Some parts of the texture is black (the border) and I don’t want that to show. Can I blend it or something when I render the trees so that only the black parts dissapear? I know some about alpha blending but if I do that the whole texture gets transparent and I want the rest of the texture to be solid.
Thanks

Look at the nehe tutor (I think it is 33) in which he show you how to create a transparentcy mask that will make only selected areas in your texture transparent.

Originally posted by ArchMiffo:
I have a texture that I would like to use for some simple trees. Some parts of the texture is black (the border) and I don’t want that to show. Can I blend it or something when I render the trees so that only the black parts dissapear? I know some about alpha blending but if I do that the whole texture gets transparent and I want the rest of the texture to be solid.
Thanks

[This message has been edited by nexusone (edited 11-09-2002).]

I’ve been trying to write a function that looks at the color of every pixel in the texture while loading it. If the color is black it sets the alpha value to 0. This however does not seem to have any effect. Do I have to do something special in order to make opengl look at the alpha values? If you want to I could post the code I use to load the texture.

You can look at the TGA loader code on my site, it loads the alpha channel mask to for the texture.

Blackjack has the file inside of it.
http://www.angelfire.com/linux/nexusone/

Originally posted by ArchMiffo:
I’ve been trying to write a function that looks at the color of every pixel in the texture while loading it. If the color is black it sets the alpha value to 0. This however does not seem to have any effect. Do I have to do something special in order to make opengl look at the alpha values? If you want to I could post the code I use to load the texture.

Thanks, but I’m afraid it didn’t help very much. I want to be able to understand the code I use and there was a whole lot strange code in your function. Besides, I want to load bmp-files. Here’s the code I’m using now. The second argument it takes just holds an rgb value. That color should be the transparent one. This is just an experiment and it doesn’t work yet. But shoulden’t it possible to set the alpha value of all the pixels with that color to 0 to make them transparent? Right now I do that in an if statement in the end of the for loop. This however has no effect. Am I on the wrong track here? Is it impossible to do what I’m trying to do?

void CreateTexture(const char* filename, aul::Color col, aul::TEXTURE& tex)
{
SDL_Surface *image; // This will hold the image we are holding

image = SDL_LoadBMP(filename); // Load the image

// Generate a texture with the associative texture ID stored in the array
glGenTextures(1, &(tex.tID)); // create a texture
glBindTexture(GL_TEXTURE_2D, tex.tID); // The texture is going to be a

// What we do next is fix the colors of the texture
int width = image->w;
int height = image->h;
unsigned char* data = (unsigned char )(image->pixels);
unsigned char * newData = new unsigned char[width
height*4];
int channels = 4;
int BytesPerPixel = image->format->BytesPerPixel;

for(int i = 0; i < (width * height); ++i)
{
unsigned char r,g,b,a;

Uint32 pixel_value = 0;
    
for(int j = BytesPerPixel - 1; j &gt;=0; --j)
{
  pixel_value = pixel_value &lt;&lt; 8;
  pixel_value = pixel_value | data[(i * BytesPerPixel) + j];
}

SDL_GetRGBA(pixel_value, image-&gt;format, (Uint8*)&r, (Uint8*)&g, (Uint8*)&b, (Uint8*)&a);

newData[(i * channels) + 0] = r;
newData[(i * channels) + 1] = g;
newData[(i * channels) + 2] = b;

if(r == col.r && g == col.g && b == col.b)
  newData[(i * channels) + 3] = 0;
else
  newData[(i * channels) + 3] = 255;

pixel_value = 0;

} // end for

// Specify the 2D texture map
glTexImage2D(GL_TEXTURE_2D, 0, 4, image->w, image->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, newData);

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

// Free Memory
delete[] newData;
SDL_FreeSurface(image);
} // end CreateTexture

As you can see I’m using SDL. Does that complicate things? Any help would be very nice.

Don’t know anything about SDL,but

1)check if your window is open in RGBA mode.

2)Do you activate the alpha channel testing ?

//Set Alpha channel test parameter
glAlphaFunc(GL_GREATER, 0.3f);
//Enable Alpha channel test
glEnable(GL_ALPHA_TEST);

Hope this helps Claude

It did help, just not enough . It looks like that was sort of what I was looking for but I do not seam to have gotten the code for reading the texture right. I will keep at it and if anyone have any ideas feel free to share them. Thanks for the help so far.

Damn, I still haven’t got this right. Can anyone help me find out whats wrong with my code? I want to load a bmp-file and if a pixel is black I want to make that pixel transparent. This way I can just select a texture and the parts that should be visible is solid and the rest (the black) is transparent. Shouldn’t there be some easy way to do this that’s similar to the code above?
Thanks

Why the hell do you have a border for your texture, when you don’t need it? Remember a texture does not have to have a border.

It’s not a border. The texture is square, the picture on the texture is not square, there is black stuff where there is no picture, that’s the border (in lack of a better word). Get it?

Oh… sorry. You could try to create a polygon with the same shape, if it’s convex. Otherwise, yes, use alpha testing.