Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Occlusion of shapes in 3D Mode #152

Closed
arihantparsoya opened this issue Apr 7, 2020 · 5 comments
Closed

Occlusion of shapes in 3D Mode #152

arihantparsoya opened this issue Apr 7, 2020 · 5 comments
Labels

Comments

@arihantparsoya
Copy link
Member

In P3D mode, the shapes are not occluded according to their depth in the scene.

For example, wen rendering the cone:
To Reproduce

from p5 import *

def setup():
	size(640, 360)
	background(204)

def draw():
	background(205, 102, 94)
	rotate_x(frame_count * 0.01)
	rotate_y(frame_count * 0.01)
	cone(40, 70)

run(mode="P3D")

All the triangles of the geometry are seen. the triangles closer to the camera are not occluding the triangles farther from the camera.

Screenshot 2020-04-07 at 12 57 50 PM

@tushar5526
Copy link
Member

Are we drawing triangles on both the sides of a face(one on the side which is facing the user one on the side which is not facing the user ) ? Or only the outer triangle (the one which is facing the user)

@arihantparsoya arihantparsoya mentioned this issue Apr 30, 2020
4 tasks
@arihantparsoya
Copy link
Member Author

@tushar5526 , once the cone is tessellated into smaller triangles (https://github.com/p5py/p5/blob/master/p5/core/primitives3d.py#L348), all the triangles are sent to OpenGL for rendering (https://github.com/p5py/p5/blob/master/p5/sketch/renderer3d.py#L200). The problem is that, OpenGL is unable to use the depth of each triangle in the scene while rendering them correctly. Checkout Z-buffering: https://en.wikipedia.org/wiki/Z-buffering

@ziyaointl
Copy link
Member

I managed to make some progress on this by explictly initializing the depth buffer.
However, z-fighting seems pretty bad. May need to make some modifications on how the lines are drawn.

Screenshot from 2020-06-02 00-43-24

@ziyaointl
Copy link
Member

After poking around some more, I realized that the default depth_func is 'less'. Since the lines are always drawn after the triangles, changing the depth_func to 'lequal' alleviates z-fighting quite a bit. There is still some left though... I shall look into transforming the lines towards the camera for an epsilon amount and check if that looks better.

Screenshot from 2020-06-03 22-47-40

@ziyaointl
Copy link
Member

ziyaointl commented Jun 4, 2020

Here are the results of transforming all line objects towards the camera before rendering:

Transformation used:

inv(self.lookat_matrix).dot(translation_matrix(0, 0, Z_EPSILON).dot(self.lookat_matrix))
  • Z_EPSILON = 0 (i.e. do nothing)
    0

  • Z_EPSILON = 0.5
    0 5

  • Z_EPSILON = 1.0
    1

  • Z_EPSILON = 1.5
    1 5

  • Z_EPSILON = 2.0
    2

  • Z_EPSILON = 100
    100

Z_EPSILON = 2.0 looks best to me using the default camera. I'll open a PR with this set to 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants