PDA

View Full Version : Painful Rendering Problem ._.



Kittyous
12-23-2017, 10:46 PM
I've been having this odd rendering issue here:
2579
I am not sure what the problem is, because all those missing tris you can see are easily visible depending on the viewing conditions:
2580
Since I'm relatively new to opengl, I cannot diagnose whether this is the result of me missing a simple function call or some larger issue.

Thank you for reading. Any help or input would be appreciated. :)

GClements
12-24-2017, 04:09 AM
My first guess would be that it's related to depth testing. Check that the near plane (in gluPerspective, glFrustum, etc) isn't too close, as that dictates the accuracy of the depth buffer.

Dark Photon
12-24-2017, 01:18 PM
As a test, try glDisable( GL_CULL_FACE ). As part of that test, disable lighting and just render triangles with a constant color (easiest).

It looks like like you might have GL_CULL_FACE on "and" you are rendering triangles with an inconsistent winding order (assuming your meshes are not just wholesale "missing" triangles). This means that some of them which should be front-facing are correctly "front-facing", but the rest which "should" be front-facing are actually "back facing". Disabling GL_CULL_FACE basically says to the GPU: I don't care; just render them all regardless. This isn't the best fix (results in more fill), but it'll at least let you see if this is your problem. The better fix to this is to leave GL_CULL_FACE enabled and just fix the winding order of the triangle vertices in your mesh. Here's a wiki page on this: Face Culling (https://www.khronos.org/opengl/wiki/Face_Culling) (OpenGL Wiki).

Failing that, is it possible that your depth buffer has some left-over trash in it from some previous render? That's unlikely to be it though. Make sure you are calling glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) at the beginning of your frame.

Also verify that you have both depth test and depth writes enabled for both shapes. And that you do have a depth buffer allocated to your render target (it appears that you do).

You might also try drawing just one of these shapes without the other. Could give you some clues.

Though my best guess is that the winding order of the tris in the ring is just really messed up. For the ring triangles whose winding order is wrong, those get backface culled away, and you incorrectly "see through" to the cylinder, but elsewhere you don't.

Kittyous
12-24-2017, 05:20 PM
As a test, try glDisable( GL_CULL_FACE ). As part of that test, disable lighting and just render triangles with a constant color (easiest).
Well the problem with rendering only as a solid color is that the only detail you can now make out is the silhouette which doesn't provide much additional insight:
2581


It looks like like you might have GL_CULL_FACE on "and" you are rendering triangles with an inconsistent winding order (assuming your meshes are not just wholesale "missing" triangles). This means that some of them which should be front-facing are correctly "front-facing", but the rest which "should" be front-facing are actually "back facing".
Yeah that's exactly what I thought when I first got this problem, at this point it seems the mesh itself is fine and everything is front facing. I tried both with and without GL_CULL_FACE

Look, this is GL_CULL_FACE disabled:
2582
And this is enabled:
2583

After studying this behavior, looking around the model in all directions, I am pretty sure that triangles on the other side of the cylinder were being rendered AFTER some of the triangles.
and that culling those overdrawing back triangles result in the image above. It might be a little easier to see in this lower poly example:
2584

But the problem is... this is all ONE MESH and the cylinder is overdrawing onto the ring and all of this is done in one render only.
So is this some kind of depth buffer issue at this point? All I've done with that is to try and enable depth test and depth mask also I'm clearing the depth buffer, but it gives no better results than the last image above.

GClements
12-24-2017, 06:51 PM
So is this some kind of depth buffer issue at this point? All I've done with that is to try and enable depth test and depth mask also I'm clearing the depth buffer, but it gives no better results than the last image above.
Do you actually have a depth buffer? If you're using GLUT, you need to pass the GLUT_DEPTH flag to glutInitDisplayMode().

If glEnable(GL_DEPTH_TEST) doesn't have any effect, then you probably don't have a depth buffer (not having a depth buffer automatically disables depth tests; most of the basic mistakes regarding depth testing will at least produce a different result to not having depth tests).

Kittyous
12-24-2017, 07:42 PM
Thank all of you guys so much.
I was using SFML and I just didn't initialize the context's depth buffer properly.
It probably would've taken me a lot longer to catch that without your help.
<3

Unfortunately it was a bit elusive trying to figure out how to set up a context correctly, so let me
post it here so anyone else with this problem using SFML could maybe find this out easier:

This is how you correctly open your window and context:
width = 800;
height = 800;
sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 4;
settings.majorVersion = 3;
settings.minorVersion = 0;
window = new sf::Window(sf::VideoMode(width, height, 32), "SFML Window", sf::Style::Default, settings);
2585
after you do that and also call glEnable(GL_DEPTH_TEST); before rendering, it should correctly use the context's depth buffer!
Thank you!