سؤال

I was a bit curious to see if I could write a simple engine in Java2D to render a 3D cube. My attempt has, to my surprise, been successful so far. The cube looks pretty good so far: enter image description here

But, as you can see, I am seriously lacking depth testing. I am completely lost on how I could go about doing depth testing in Java2D though. From what I have found, you go about it by rendering each polygon to a bitmap then testing each pixel against every other pixel that is may conflict with. This is where I am lost.

I have the necessary depth values for each vertex of the polygons, but I don't have the depth values of each pixel. This is probably a side effect of letting Java2D fill each polygon for me using the fillPolygon(Polygon p) method. So this leads me to believe I cannot do depth testing if I render my polygons using the fillPolygon(..) method. My first question is that: Can I do z-buffering while still being lazy and letting Java do my pixel drawing?

If not, then my next question is: how in the world do I go about rendering the polygons on my own? I mean, I know well enough how to store pixels in a bitmap then render them to an image. I am wondering how I can take the four vertices of each polygon and render them appropriately.

هل كانت مفيدة؟

المحلول

Given the triangle (or any polygon) you can surely compute the z value at any pixel location but I don't think you can tell the java function fillPolygon(Polygon p) to only draw the polygon pixels that pass the depth test unless your library has some function that also takes a stencil mask. If not, you need to code your own rasterizer. This old paper presents a remarkable yet simple rasterization algorithm that is suitable for today parallel hardware.

نصائح أخرى

It all depends on what you would like to learn more about.

  • If you want to implement Z-Buffering, you need to do the step of rasterizing, which is currently done by Java2D. A good starting point may be the scanline algorithm for the rendering.
  • If you are more interested in hidden-surface removal and culling, you could start with backface culling
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top