PDA

View Full Version : Transparent Texture Effect



H.Stony
02-17-2005, 06:29 AM
the structure is of a openGL book. the book is bad and i don't believe that the transparent texture structure is good.

glBlendFunc(GL_DST_COLOR, GL_ZERO);
glBindTexture(..., mask);
drawObject();

glBlendFunc(GL_ONE, GL_ONE);
glBindTexture(..., texture);
drawObject();

--> it works but i have to draw the object 2 times.
is there a faster way?

(mask is a texture where the transparent texels are white and the other pexels black)
(in texture the transparent texels are black)

Aeluned
02-17-2005, 06:58 AM
What?
use a texture with an alpha channel.

H.Stony
02-17-2005, 07:04 AM
which file format??? now i am using .raw files! i think most of the picture formats which support alpha channels are hard to read!

02-17-2005, 07:29 AM
You could use .raw files with 32 bit components, otherwise I find .tga files very easy to read & write.

BMP supports 32 bit as well but not every imagine software supports that.

H.Stony
02-17-2005, 08:03 AM
in photoshop i cannot save a raw with alpha channels...if photoshop doesn't support this no imagine software will do this....

how do you save 32 bit raws with alpha channel????

H.Stony
02-17-2005, 08:18 AM
i have tried it in graphic converter and i was able to export RGBA textures - but my raw reading routine doesn't work any more.. -->clear

are there any tutorials in reading a tga file or how can i interprete the file?

02-17-2005, 08:38 AM
Originally posted by H.Stony:
if photoshop doesn't support this no imagine software will do this....
Sure... :rolleyes:

As for .tga files there are tons of tutorials out there for example this:

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=33

02-17-2005, 09:18 AM
First, post your RAW Loading Function so that we can tell you where to toggle the 4th channel(alpha). If you know for sure your converted RAW texture includes the ALPHA channel, then it should'nt be to hard to modify it.

For example, when you allocate your buffer in your RAW loading function after widthTexture, heightTexture, 4 , so on... The four instructs the space to hold the 4ht Alpha channel.

Likewise, when you read it include a 4 after the widthTexture and heightTexture parameters.

Finally, when you build your MipMap, at the end of this function, do something like the following:

gluBuild2DMipmaps( GL_TEXTURE_2D, 4, textureWidth,
textureHeight, GL_RGBA, GL_UNSIGNED_BYTE, raw_data );

GL_RGBA instead of GL_RGB and a 4 insteadof a 3 after GL_TEXTURE_2D.

If you dont change the previous parts you "will" get errors or worse,your compiler will freeze or crash.

But, post your texture loading function anyways...I'm sure somebody here can jerry rig it up for you, really quick.

H.Stony
02-17-2005, 09:40 AM
GLuint sz;
FILE* file;
long fsize;
GLubyte* p;

refimg->w = (GLuint) w;
refimg->h = (GLuint) h;
sz = (((3*refimg->w+3)>>2)<<2)*refimg->h;
refimg->data = new GLubyte [sz];
if (refimg->data == NULL) {
environment.error = 61;
sprintf(awx_temp_string,"Fehler in AWX-RAW-Loader: %s.",file_name);
environment.error_message=awx_temp_string;
}


file = fopen(file_name , "rb");
if (!file)
{
environment.error = 2;
sprintf(awx_temp_string,"Die Texturdatei: %s nicht gefunden.",file_name);
environment.error_message=awx_temp_string;
}
environment.getError();
fseek(file, 0L, SEEK_END);
fsize = ftell(file);
if (fsize != (long)sz) {
environment.error = 62;
sprintf(awx_temp_string,"Die Texturdatei: %s ist nicht in der richtigen Auflösung",file_name);
environment.error_message=awx_temp_string;
fclose(file);
environment.getError();
}
fseek(file, 0L, SEEK_SET);
p = refimg->data;
while (fsize > 0) {
fread(p, 1, 1, file);
p++;
fsize--;
}
fclose(file); i hope anybody can modify it for a 32 bit textre with alpha channel

02-17-2005, 09:54 AM
How are you building your MIpMap? Post that as well, if you can. It should pretty much be a matter of including in buildMipMap a 4 instead of a 3 and a GL_RGBA instead of a GL_RGB.

As for the rest, meanwhile to store and read the texture try toggling a 4 insteadof a 3....and lets see if your compiler does'nt barf.

02-17-2005, 10:01 AM
As for storing the texture:
instead of :
sz = (((3*refimg->w+3)>>2)<<2)*refimg->h;

Try :

sz = (((4*refimg->w+4)>>3)<<3)*refimg->h;

then modify the read part as well.

Finally, do what I suggested in building the mipMap.

Fiddle with it....let's see what happens.

02-17-2005, 10:19 AM
Wait, a minute...You may not have to modify how the texture is being read:

Leave it alone , for now. As is.... :
:p = refimg->data; while (fsize > 0) { fread(p, 1, 1, file); p++; fsize--;

but, find out what the MipMappart of it is doing..

Probably, none of the other parameters of the function need to be changed. like parameterf, ect.

uruk
02-17-2005, 11:34 AM
Originally posted by H.Stony:
in photoshop i cannot save a raw with alpha channels...if photoshop doesn't support this no imagine software will do this....

how do you save 32 bit raws with alpha channel????In Photoshop you CAN save a raw file with alpha channels. Just make sure your image has an alpha channel.



f=new TFileStream("teanc.raw",fmOpenRead);
tex._img=(TKolor *)malloc(1024*1024*4);
f->Read(tex._img,1024*1024*4);
tex._width=1024;
tex._height=1024;
delete f;
glGenTextures(1,&amp;texnam);
glBindTexture(GL_TEXTURE_2D,texnam);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D,4,1024,1024,GL_RGB A,GL_UNSIGNED_BYTE,tex._img);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MO DULATE);
free(tex._img);This works just fine for me.
File "teanc.raw" was made in Photoshop.

Aeluned
02-17-2005, 12:05 PM
Hey uruk,
I was just wondering...

scrii de-la lucru sau de-acasa?

couldn't resist.

Go RO!

uruk
02-17-2005, 12:29 PM
@Aeluned : right now i'm at work :(

H.Stony
02-18-2005, 12:29 AM
the forgotten struct of my reading function to complete it:

typedef struct _RGBIMG {
GLuint w;
GLuint h;
GLubyte* data;
} RGBIMG; i don't have to change this - right?
i create the texture with:

glTexImage2D(GL_TEXTURE_2D, 0, 3, img.w, img.h, 0, GL_RGB, GL_UNSIGNED_BYTE, img.data); to make sure i understood all your posts - a summary of the changes:
glTexImage2D(GL_TEXTURE_2D, 0, 3, img.w, img.h, 0, GL_RGB, GL_UNSIGNED_BYTE, img.data);
to
glTexImage2D(GL_TEXTURE_2D, 0, 4, img.w, img.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img.data); and in the reading function:

sz = (((3*refimg->w+3)>>2)<<2)*refimg->h;
to:
sz = (((4*refimg->w+4)>>3)<<3)*refimg->h;i think in this loop i will have to change something (how should i modify this exactly?):

while (fsize > 0) {
fread(p, 1, 1, file);
p++;
fsize--;
}--> is this everything?

another question: what are the advanteges to use gluBuild2DMipmaps instead of glTexImage2D ...

sorry for the stupid questions
thank you very much!!! :p

H.Stony
02-18-2005, 03:37 AM
it doesn't work - i am not sure about the texture file

could someone be so kind as to give me a .raw file with a alpha channel which is working with someones texture reader?
h.stony@awx.at

thank you

uruk
02-18-2005, 04:05 AM
Ok, here goes:

(I use Adobe Photoshop 7.0)

1. make sure your texture has an alpha channel

http://img165.exs.cx/img165/7936/channels4qh.jpg

2. File -> Save As

http://img165.exs.cx/img165/7524/saveas0et.jpg

and select *.RAW file format

3. I don't think you need header info

http://img207.exs.cx/img207/4310/rawoptions5ej.jpg

click "OK"

Now you are the proud owner of a texture with alpha channel. It's not that hard.

Oh, one more think:

Look at the tuts at NeHe (http://nehe.gamedev.net/default.asp)

They are realy good, I learned lots of cool stuff from them.

H.Stony
02-18-2005, 05:28 AM
thank you..... sure..it's easy!! thank you