Advertisement

Transparency

Started by August 10, 2003 12:35 PM
8 comments, last by QReboundSoftware 21 years, 6 months ago
Hey, i am just about to start on NeHe lesson 9. I also built all the previous lessons with both the cube and the pyramid, so in the lighting tutorial i did figure out the normal of the pyramid sides :-D Anyway, i thaught it would be cull to place the "particle effects" from lesson 9 inside the transparent cube, but i cant figure out how to make the cube transparent, and the pyarmid opaqe. I have tried just setting the transparency and colors in the render fucntion, and it renders most of the cube, all except for the top side, and half the pyramid transparent. The sides on the pyarmid that are transparent are the right and back sides. Here is my render function:

int GLDrawScene(GLvoid)														//Draws the OpenGL Scene

{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);						//Clear the Screen each frame

	//World Stuff

	glLoadIdentity();														//Load the Identity

	glTranslatef(-1.5f, 0.0f, -12.0f);										//Drawing

	glRotatef(RotX, -1.0f, 0.0f, 0.0f);
	glRotatef(RotY, 0.0f, -1.0f, 0.0f);
	glBindTexture(GL_TEXTURE_2D, Texture[Filter]);
	//Pyramid

	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	
	glBegin(GL_TRIANGLES);
	glNormal3f(0.0f, 0.447214f, 0.894427f);
	//Top Point(front)

	glTexCoord2f(0.5f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f);

	//Bottom Left hand corner(front)

	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,-1.0f,1.0f);
	
	//Bottom Right Hand Corner(front)

	glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, -1.0f,1.0f);
	
	//Top Point(Right)

	glNormal3f(0.894427191f, 0.44721f, 0.0f);
	glTexCoord2f(0.5f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f);
	
	//Bottom Left hand corner(Right)

	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
	
	//Bottom Right Hand Corner(Right)

	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f,1.0f);
	glNormal3f(0.0f, 0.44721f, -0.894427f);
	
	//Top Point(back)


	glTexCoord2f(0.5f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f);
	
	//Bottom Left hand corner(back)

	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,-1.0f, -1.0f);
	
	//Bottom Right Hand Corner(back)

	glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, -1.0f,-1.0f);
	
	glNormal3f(-0.894427191f, 0.44721f, 0.0f);
	//Top Point(left)

	glTexCoord2f(0.5f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f);
	
	//Bottom Left hand corner(left)

	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,-1.0f,-1.0f);
	
	//Bottom Right Hand Corner(left)

	glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, -1.0f,1.0f);

	glEnd();
	//World Stuff

	glLoadIdentity();
	glTranslatef(1.5f, 0.0f, Z);
	glRotatef(RotX, 1.0f, 0.0f, 0.0f);
	glRotatef(RotY, 0.0f, 1.0f, 0.0f);
	glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
	//Cube

	glBegin(GL_QUADS);
	//Top

	
	
	glNormal3f(0.0f, 1.0f, 0.0f);
	glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f, 1.0f,1.0f);
	
	glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0f, 1.0f,1.0f);
	
	glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0f, 1.0f,-1.0f);
	
	glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f, 1.0f,-1.0f);
	//Front

	glNormal3f(0.0f, 0.0f, 1.0f);
	glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f,-1.0f, 1.0f);
	
	glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0f,-1.0f, 1.0f);

	glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
	
	glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
	//Left

	glNormal3f( -1.0f, 0.0f, 0.0f);
	glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f,-1.0f, 1.0f);
	
	glTexCoord2f(1.0f,0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
	
	glTexCoord2f(1.0f,1.0f); glVertex3f(-1.0f, 1.0f,-1.0f);

	glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
	//Bottom

	glNormal3f(0.0f,-1.0f, 0.0f);
	glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f,-1.0f, 1.0f);
	
	glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0f,-1.0f, 1.0f);
	
	glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0f,-1.0f,-1.0f);
	
	glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
	//Back

	glNormal3f(0.0f, 0.0f,-1.0f);
	glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f, 1.0f,-1.0f);
	
	glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0f, 1.0f,-1.0f);
	
	glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0f,-1.0f,-1.0f);

	glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
	//Right

	glNormal3f(1.0f, 0.0f, 0.0f);
	glTexCoord2f(0.0f,0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
	
	glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
	
	glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0f, -1.0f,-1.0f);
	
	glTexCoord2f(0.0f,1.0f); glVertex3f( 1.0f, 1.0f,-1.0f);
	glEnd();
	
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	//Rotation

	RotX+=RotXSpeed;
	RotY+=RotYSpeed;
	
	return(true);															//Return Success

}
Tell me if you can see the problem! Thank you! [edit] Update: After some testing, i found that in fact on the pyarmid, only one face was being rendered opaque. The front face. Which is strange coicidence, because the top face on the cube is being rendered opaque also, and they are both the first faces i draw of each object. ______________________________ Quantum CEO of Quantum Rebound Software Website Up Soon [edited by - QReboundSoftware on August 10, 2003 3:59:10 PM]
______________________________QuantumCEO of Quantum Rebound SoftwareWebsite Up Soon
er.. this wont really help you but I thought glColor4f changed the brightness not the transparency? GL_BLEND is used for what I thought but anyways good luck with that.
My fellow Americans I have just signed legislation that outlaws Russia forever. Bombing will commence in five minutes.
Advertisement
What? no Color4f(r,g,b, A) Alpha being the transparency... right?


______________________________
Quantum
CEO of Quantum Rebound Software
Website Up Soon
______________________________QuantumCEO of Quantum Rebound SoftwareWebsite Up Soon
i looked it up and the fourth value is alpha not transparency. Transparency is created by enabling GL_BLEND. In order for openGL to create transparencies it samples things and the stuff directly behind it and combines the two or three colors for each pixel. The only problem is sorting the objects so that the ones farthest from the camera are drawn first (GL_BLEND will not calculate them if they are drawn out of order it''s lame).
My fellow Americans I have just signed legislation that outlaws Russia forever. Bombing will commence in five minutes.
But alpha is transparency... isnt it? And i do have GL_BLEND Enabled.


______________________________
Quantum
CEO of Quantum Rebound Software
Website Up Soon
______________________________QuantumCEO of Quantum Rebound SoftwareWebsite Up Soon
Alpha is brightness but its opaque brightness not transparent (or more accurately translucent). An alpha value of 0.0 would be no brightness and an Alpha value of 1.0 would be normal brightness. So using that logic glColor4f(1.0f, 1.0f, 1.0f, 0.5f) would be equivalent to glColor4f(0.5f, 0.5f, 0.5f, 1.0f). Anyways the reason I believe why the top part of the cube or whatever wasn''t appearing was because of the blending calculations.. I think theres an article on this site about depth testing and sorting objects to display them from farthest to nearest.
My fellow Americans I have just signed legislation that outlaws Russia forever. Bombing will commence in five minutes.
Advertisement
Try to activate Depth_Buffer! just a try!

"There are people who live in the reality. We recreate it!"
"There are people who live in the reality. We recreate it!"
ok, best you are put on the right foot straight away, so:

A quick revision of blending and alpha:


Almost all modern video cards these days display images in 32bit colour. This is nice since it''s a power of two number...

But hold on,

Red + Blue + Green = 8 bits of red, 8 of blue, 8 of green... thats 24 bit... Huh?!

So where are the extra 8 bits?..

well, simple.

They are the ''alpha'' colour, a special (and extremly useful) colour you cannot see on screen. Nothing more, nothing less. It''s not a majic ''brightness'' or ''transparency'' colour.. it''s just the remainder byte of the 32bits.

That said, you can take advantage of it in the blend function.
The blend function is set up as such:

when blending, you are adding the colour (RGBA, not RGB) of the pixel you are about to draw, with the pixel already on screen.

These are the source and destination pixels.

What you can do, however, is multiply each of these first, by another value. Eg, GL_SRC_ALPHA, for the alpha value of the pixel about to be drawn...

hence:

the blend function GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA comes down to this:

where SRC is the incomming pixel, and DST is the existing pixel

final pixel = SRC * SRC.alpha + DST * (1-SRC.alpha)

if you think about this, then, if the alpha colour of the pixel about to be drawn is 0, you get:

final pixel = DST

ie, No change... (fully transparent)

however, a value of 1 will result in, you guessed it,

final pixel = SRC

ie, the pixel about to be drawn (fully opaque)


NOTE: there is more to blending than just ''alpha blend'' (the example above)... you can, for example WRITE to the screen using a texture with an alpha channel (not using blending) then do another blended pass using GL_DST_ALPHA... This will blend with the alpha value already on screen. This can be very, very useful.
Also remember you can also do GL_DST_COLOUR, etc...

if you work it out, for example, this code

glBlendFunc(GL_DST_COLOUR,GL_ONE)

will brighten the area you are rendering to, based on the colour of what your drawing. All sorts of things like that.

Note there is also a nice extension (GL_EXT_blend_func_separate) that lets you set the blend function for alpha and RGB seperatly, and also GL_EXT_blend_subtract to make the equation a subtraction not an addition (very useful for smoke!) and even GL_EXT_blend_minmax for setting limits on the blend amount.

| - Project-X - On hold.. - | - adDeath - | - email me - |
Oh ok, thanks for the explanation RipTorn.
LoBravo: The GL_DEPTH_TEST is enabled.


______________________________
Quantum
CEO of Quantum Rebound Software
Website Up Soon
______________________________QuantumCEO of Quantum Rebound SoftwareWebsite Up Soon
Yay! You will be happy to know that i have solved the problem! I was trying to change the Alpha Value, instead of turning blending on and off, because i thaught turning it on and off would waste alot of speed. Oh, and i also changed changed it to glBlendFunc(GL_ONE, GL_ONE); because the tutorial on www.gametutorials.com told me to


______________________________
Quantum
CEO of Quantum Rebound Software
Website Up Soon
______________________________QuantumCEO of Quantum Rebound SoftwareWebsite Up Soon

This topic is closed to new replies.

Advertisement