简体   繁体   English

Java中的深度缓冲和多边形填充

[英]Depth buffering and polygon filling in Java

I was a bit curious to see if I could write a simple engine in Java2D to render a 3D cube. 我有点好奇是否可以用Java2D编写一个简单的引擎来渲染3D立方体。 My attempt has, to my surprise, been successful so far. 到目前为止,我的尝试令我感到惊讶,并取得了成功。 The cube looks pretty good so far: 到目前为止,多维数据集看起来还不错: 在此处输入图片说明

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. 我完全迷失了如何在Java2D中进行深度测试。 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. 这可能是让Java2D使用fillPolygon(Polygon p)方法为我填充每个多边形的副作用。 So this leads me to believe I cannot do depth testing if I render my polygons using the fillPolygon(..) method. 因此,这使我相信,如果我使用fillPolygon(..)方法渲染多边形,则无法进行深度测试。 My first question is that: Can I do z-buffering while still being lazy and letting Java do my pixel drawing? 我的第一个问题是:我可以在仍然懒惰的同时做z缓冲,让Java绘制像素吗?

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. 给定三角形(或任何多边形),您当然可以在任何像素位置计算z值,但我认为您不能告诉Java函数fillPolygon(Polygon p)仅绘制通过深度测试的多边形像素,除非您的库具有某些功能,也需要使用模板遮罩。 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. 如果要实现Z缓冲,则需要执行栅格化步骤,该步骤目前由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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM