Advertisement

depthtesting with blended particals

Started by June 24, 2003 11:15 PM
7 comments, last by skow 21 years, 8 months ago
I have many partical effects for wepions, explotions and ship trails. I'm unable to use depth testing for the particals, so i have been doing a test, that will find the order of how close objects are, then draw them in in a order farthest to cloesist. The more advanced my game is getting the less and less preactical this method is getting. I've been working on projecting and doing a pixel check to see if anything is infront but that is not working. Is there an easier method to solve this problem? Below is the check code for a paticluar partical. It doesnt work it shows there is always somthing infront of the partical. Help with an alternative, or a fix to my code would really make my day


	        double sx,sy,sz, depth;
                GLdouble modelMatrix[16]; // The model matrix.

                GLdouble projMatrix[16];  // The projection matrix.

                GLint viewport[4];        // The viewport.


                glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix); // Load the matricies and viewport.

                glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
		glGetIntegerv(GL_VIEWPORT, viewport);


			//Find partical on screen

		gluProject(plas1p[0].Gxpos(),plas1p[0].Gypos(),plas1p[0].Gzpos(),modelMatrix,projMatrix,viewport,&sx,&sy,&sz);
			//If the partical is on screen

		if(sx < viewport[2] && sx >= 0 && sy < viewport[3] && sy >= 0 )
		{
				//Read pixels

			glReadPixels((sx),(int)(sy),1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&depth);  
				//somthing is infront of the partical

			if(depth < sz)
					//apply a black texture

				glBindTexture(GL_TEXTURE_2D,g_Texture[9]);
			else
					//apply the proper texture

				glBindTexture(GL_TEXTURE_2D,g_Texture[10]);
		}

[edited by - skow on June 24, 2003 12:21:24 AM]
doh after a few hours i found out i needed the sx-sz to be GLdouble''s not floats, so my code now works.


I would still like to know if any one knows a better way of testing.
Advertisement
If you use additive blending, like glBlendFunc(GL_SRC_ALPHA, GL_ONE); than you can simply leave depthtesting on and set glDepthMask() to true and false. There''s no need to sort your particles far-to-near. So:

DrawLevel();
glDepthMask(GL_TRUE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_BLEND);
DrawParticles();
glDisable(GL_BLEND);
glDepthMask(GL_FALSE);

maybe you need to switch GL_TRUE and GL_FALSE though... I''m not 100% sure of their order.

Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]


GSACP: GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

the default is true, so the first one must be GL_FALSE.
anyway thank you a lot, this helped me a lot too!



There aren''''t problems that can''''t be solved with a gun...
There aren''t problems that can''t be solved with a gun...
Wow, i wish i had known this before i spent so much time doing stupid draw order algorithums. What a waste of time.

Thanks alot man this really helps.
Hey Skow,

do you work on a black background ?
Just asking because I am doing much the same as you are.
What happens to your particles if you choose a white background ?
Cause I got an unnerving problem (see "Blending particles" thread).

By the way ... another problem. If I use particles with DepthMask set to FALSE. and then while the program is running and particles flying I change the particle objects to be drawn (say from texturized particles to simple spheres) and for THEM DepthMask is TRUE again.
If I now look at the scene from an angle where the first particles are nearest to the camera, they will be painted behind the particles that are further away, nonetheless !

Any idea ?

Cheers

Twist
Advertisement
Draw all alpha blended objects last.
Thanks man, that was it. Even if I feel a little stupid, seeing the easy answer .
That is making my engine a little slower than before, though.
Anyway .. thanks again .

Twist
If you want to prevent semitransparent objects from messing up other semitransparent ones, you must depth-sort them before you render them.

This topic is closed to new replies.

Advertisement