java - 使用重心函数插入像素的 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();
}
}
}
}
解决方案
推荐阅读
- java - 计算字符串中以大写字母开头的所有单词
- reactjs - 如何在jsx中使用if else进行媒体查询
- python - python中打印错误中的日期格式之间的天数
- html - 点击鼠标翻转卡片
- tags - terraform 覆盖子模块中的标签值
- c# - 签入条件时如何填写列表或数组?
- discord.js - 如何缓存消息?
- react-native - 无法从 React Native 中的另一个组件更新变量
- excel - 如何在宏 VBA 中使用循环进行分配?
- php - SQLSTATE [01000] SQLDriverConnect:0 [unixODBC] [驱动程序管理器]无法打开库“IBMDB2”:找不到文件