Transparent Texture Effect

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)

What?
use a texture with an alpha channel.

which file format??? now i am using .raw files! i think most of the picture formats which support alpha channels are hard to read!

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.

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

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?

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

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.

 	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

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.

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.

Wait, a minute…You may not have to modify how the texture is being read:

Leave it alone , for now. As is… :
:stuck_out_tongue: = 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.

Originally posted by H.Stony:
[b]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???[/b]
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,&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_FILTER,GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
 gluBuild2DMipmaps(GL_TEXTURE_2D,4,1024,1024,GL_RGBA,GL_UNSIGNED_BYTE,tex._img);
 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
 free(tex._img);

This works just fine for me.
File “teanc.raw” was made in Photoshop.

Hey uruk,
I was just wondering…

scrii de-la lucru sau de-acasa?

couldn’t resist.

Go RO!

@Aeluned : right now i’m at work :frowning:

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!!! :stuck_out_tongue:

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

Ok, here goes:

(I use Adobe Photoshop 7.0)

  1. make sure your texture has an alpha channel

  1. File -> Save As

and select *.RAW file format

  1. I don’t think you need header info

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

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

thank you… sure…it’s easy!! thank you