首页 > 解决方案 > 使用重心函数插入像素的 z 值后如何获得深度值?

问题描述

我知道我可以使用重心函数来插值 tri 顶点的深度,但我不知道如何访问该插值,因为该函数返回一个向量。

我还没有完全优化代码,因为我想在优化之前完全理解这个过程——用 c++ 编写,呵呵)。

编辑:我认为插值深度只是depth = v.x*tri.verts[0].z + v.y*tri.verts[1].z + v.z*tri.verts[2].z. 但是现在当我运行程序时,并不是所有应该被遮挡的像素都被遮挡了。它看起来像一个低样本透明度效果。

//for each pixel in tri's bounding box
for (int x = minx; x <= maxx; x++)
{
    for (int y = miny; y <= maxy; y++)
    {
        // the pixel coordinate in linear space
        int coord = (int)(x+y*screenWidth);
                            
        // Barycentric function with output weight
        Ops.computeBarycentricCoordinates(tri.verts[0], tri.verts[1], tri.verts[2], new Vec3D(x,y,0,1), weight);
        weight.normalize(); // Do i even need this? 
           
        // if the triangle contains the pixel
        if(weight.x>=0 && weight.y>0 && weight.z>=0)
        {
            float depth = weight.x * tri.verts[0].z + weight.y * tri.verts[1].z  + weight.z * tri.verts[2].z;
                                
            if(depth<zbuffer[coord])
            {
                zbuffer[coord] = depth;
                                    
                int rgb = (int)((depth/2)*255);
                screenPixels[coord] = new Color(rgb,rgb,rgb).getRGB();
            }
        }
    }
}
 

标签: java3drasterdepth-bufferzbuffer

解决方案


推荐阅读