Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to avoid depth fighting


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Freeky   Members   -  Reputation: 164

Like
0Likes
Like

Posted 11 August 2013 - 08:41 AM

Hello,

 

I am creating my first 3d game in java using the Lightweight Java Game Library and OpenGL.

 

I'm afraid I have just encountered the first problem so far for wich I can't find the answer on Google, so I decided to ask it here.

 

I have drawn four textured walls wich represent a house, and this works perfectly fine. However, Now I want to add a door to my house.

A door in my game is an entity wich is rendered as a rectangle just as heigh as a wall but with a door texture on it.

 

I was hoping that by drawing it later than the wall, it would just 'override' that part of the wall. This was not the case and the door and the wall started z-figting. So now it looks like this:

t6446d.png

The door starts to flicker when the camera changes position.

 

If I disable GL_DEPTH_TEST, it looks fine from the right side, but if I stand on the other side of the house, I'll still see the door through the house, like this:

4kwava.png

 

I understand this can be solved by drawing the part left and the part right of the wall seperately with the wall in between them, but I was hoping there is another way to solve this problem.

 

Many thanks,

 

Freek



Sponsor:

#2 warcod3   Members   -  Reputation: 98

Like
0Likes
Like

Posted 11 August 2013 - 09:35 AM

One thing you can do is to use an object which does not draw itself for the door, and let the wall draw the door too. (I think this would be the best.)

And another possible thing you can do, is to move the door outside the house by say 0.0001.

Or maybe don't have a door object at all, use a texture which includes a door for the house, and check collision with that part of the house.



#3 Sik_the_hedgehog   Crossbones+   -  Reputation: 1503

Like
2Likes
Like

Posted 11 August 2013 - 10:01 AM

0.0001 won't be enough, depends on the resolution of the depth buffer and the depth range too.

 

Two options which in my opinion would be the best:

  1. Split that wall into several polygons and then cram the door as one of those polygons. No depth fighting when there aren't any polygons overlapping! This is the best option if it's to remain like that.
  2. If the door can be opened in the future, consider poking a hole in the wall so the door fits well in it. The door would be a separate object, as you can imagine.

Even better if you're willing to add some more detail, make the door sightly inside the wall (which implies making a hole in the wall and then extend it inside the hole), instead of being a flat decal on top of it. Will make it look more 3D-ish and feel more natural =O)


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#4 Freeky   Members   -  Reputation: 164

Like
1Likes
Like

Posted 11 August 2013 - 10:41 AM

Thanks for the answers.

 

I currently render the door 0.001 outside of the wall, and depth fighting now only happens when I'm far away from the wall. So it works, but it's not a permanent solution.

 

Both the walls and the door are just entities in my game. Only the wall takes 2 position parameters intead of 2 (the wall being rendered from the first to the second position) and I really like the simplicity of that but I'm afraid I'm gonna have to come op with a more complicated system.

 

Is there a way in openGl to 'draw' a hole in something? It would be a great solution for this and some other problems I will encounter.

For example: can I tell OpenGL to draw a rectangle (using GL_QUADS) from 0,0,0 to 20,0,20, and then tell it to 'dedraw' a rectangle 5,0,5 to 10,0,10? It would be great but I don't think this is possible this way except by breaking up the first rectangle into 4 rectangles surrounding the hole.

 

And Sik, I understand what you mean, but I was actually trying to avoid adding detail as much as possible to keep this project simple. That's why I chose a retro-look and made the player unable to look up and down. (The camera can only turn left and right)

 

By the way, it won't be possible to open the door, you just press the action key when you're close enough and will be teleported to the inside of the house, wich is just a seperate room.


#5 warcod3   Members   -  Reputation: 98

Like
0Likes
Like

Posted 11 August 2013 - 12:14 PM

If I'm not wrong, there is no function like 'dedraw'. But why not draw one of the walls with a texture which includes the door? That would be simple too -if I'm not missing something.

#6 Freeky   Members   -  Reputation: 164

Like
0Likes
Like

Posted 11 August 2013 - 01:34 PM

Yeah, maybe you're right, I guess that would be pretty simple. I was just happy about my current system and hoped I wouldn't have to turn it all around. 

 

The current wall texture is 16*16 pixels, wich is repeated over the entire wall. (Two times vertical and horizontal depending on the length of the wall (the distance between the two wall positions)) The house consists of four walls. This means I can just break up the wall on wich the door is placed into 2 walls: The part left and the part right of the door. Then I would simply place the door right between the 2 walls. This would only cost 1 line of code, so that's not the problem. I was just wondering if there was some kind of way to force OpenGL to 'override' the previously rendered stuff and to force one texture to win the depth fight. Sadly this is not the case so I'll have to do it the way I just explained.

 

Thanks for the help guys, I really appreciate it. ;)

 

Freek



#7 desdemian   Members   -  Reputation: 459

Like
2Likes
Like

Posted 11 August 2013 - 02:03 PM

There is a property you can set before drawing a polygon: glPolygonOffset.

 

Quoting: "glPolygonOffset is useful for rendering hidden-line images, for applying decals to surfaces (this is your case), and for rendering solids with highlighted edges."
 

What it does it "pulls" or "pushes" the polygon before doing the z test. Thus avoiding the z-fighting.

 

 

Good luck.


'Never let your schoolling interfere with your education' .-

#8 Freeky   Members   -  Reputation: 164

Like
0Likes
Like

Posted 11 August 2013 - 02:41 PM

Many thanks!

 

Though I have no idea what the parameters 'factor' and 'units' actually mean, setting them both to -1.0f seemed to do the trick and the depth fighting has now totally dissapeared. (:

 

Now I'm gonna have a look around on the internet to see what it is I just did.

 

Thanks again.



#9 markr   Crossbones+   -  Reputation: 1653

Like
0Likes
Like

Posted 12 August 2013 - 07:39 AM

Some suggestions:

 

1. Only draw the parts of the wall which are not behind the door - this makes the wall more complicated, but think about it - in the "real world", objects cannot intersect.

 

2. Draw the door as a "decal" using multitexturing

 

3. Incorporate the door into the wall texture

 

Mark



#10 mdias   Members   -  Reputation: 782

Like
0Likes
Like

Posted 12 August 2013 - 11:08 AM

While I never used it, I hear Depth Bias solves your kind of problem. Might be worth investigating.



#11 Vortez   Crossbones+   -  Reputation: 2688

Like
0Likes
Like

Posted 12 August 2013 - 12:15 PM

Or you could use the stencil buffer to so something like, don't draw the part of the wall that the door cover, so there's no zfighthing at all.

With Z-Culling enabled this should solve your problem, that's what i did for something similar in my game before.

 

You just use the door to make the stencil "mask", before drawing the walls, then draw the walls using the stencil buffer as a mask, then turn the stenciling off and draw the door again normaly.


Edited by Vortez, 12 August 2013 - 12:20 PM.


#12 wodinoneeye   Members   -  Reputation: 680

Like
0Likes
Like

Posted 13 August 2013 - 03:55 AM

Z-buffering with Offsetting special features away (sufficiently)  from the basic wall plane is probably the simplest.  Those if the door opens inward then you will need to have a gap in the continuous wall


--------------------------------------------Ratings are Opinion, not Fact

#13 MarekKnows.com   Members   -  Reputation: 442

Like
1Likes
Like

Posted 14 August 2013 - 10:49 AM

A stencil buffer can be used to prevent you from rendering at certain locations in your buffer, thus you can create "holes".  Have a look here for sample code:

http://en.wikipedia.org/wiki/Stencil_buffer


---
Free C++, OpenGL, and Game Development Video Tutorials @
www.MarekKnows.com
Play my free games: Ghost Toast, Zing, Jewel Thief





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS