提交 9890554b 编写于 作者: H happyfire

* small fix for viewport transform

上级 65ffb590
......@@ -156,7 +156,7 @@ namespace URasterizer
Mesh mesh = ro.mesh;
_matModel = ro.GetModelMatrix();
_matModel = ro.GetModelMatrix();
Matrix4x4 mvp = _matProjection * _matView * _matModel;
if(_config.FrustumCulling && URUtils.FrustumCulling(mesh.bounds, mvp)){
......@@ -248,8 +248,8 @@ namespace URasterizer
for (int k = 0; k < 3; k++)
{
var vec = v[k];
vec.x = 0.5f * _width * (vec.x + 1.0f);
vec.y = 0.5f * _height * (vec.y + 1.0f);
vec.x = 0.5f * (_width - 1) * (vec.x + 1.0f);
vec.y = 0.5f * (_height -1) * (vec.y + 1.0f);
//在硬件渲染中,NDC的z值经过硬件的透视除法之后就直接写入到depth buffer了,如果要调整需要在投影矩阵中调整
//由于我们是软件渲染,所以可以在这里调整z值。
......@@ -262,7 +262,7 @@ namespace URasterizer
//但是这么调整后,正好和Unity在DirectX平台的Reverse z一样,让near plane附近的z值的浮点数精度提高。
vec.z = vec.z * 0.5f + 0.5f;
v[k] = vec;
v[k] = vec;
}
Triangle t = new Triangle();
......
......@@ -61,8 +61,8 @@ void TriangleProcess(uint3 id : SV_DispatchThreadID)
for (k = 0; k < 3; k++)
{
float4 vec = v[k];
vec.x = 0.5f * frameBufferSize.x * (vec.x + 1.0f);
vec.y = 0.5f * frameBufferSize.y * (vec.y + 1.0f);
vec.x = 0.5f * (frameBufferSize.x - 1) * (vec.x + 1.0f);
vec.y = 0.5f * (frameBufferSize.y - 1)* (vec.y + 1.0f);
//在硬件渲染中,NDC的z值经过硬件的透视除法之后就直接写入到depth buffer了,如果要调整需要在投影矩阵中调整
//由于我们是软件渲染,所以可以在这里调整z值。
......
......@@ -82,17 +82,18 @@ namespace URasterizer
// ------- Viewport Transform ----------
//NDC to screen space
{
v0.x = 0.5f * screenWidth * (v0.x + 1.0f);
v0.y = 0.5f * screenHeight * (v0.y + 1.0f);
int max_w = screenWidth - 1;
int max_h = screenHeight - 1;
v0.x = 0.5f * max_w * (v0.x + 1.0f);
v0.y = 0.5f * max_h * (v0.y + 1.0f);
v0.z = v0.z * 0.5f + 0.5f;
v1.x = 0.5f * screenWidth * (v1.x + 1.0f);
v1.y = 0.5f * screenHeight * (v1.y + 1.0f);
v1.x = 0.5f * max_w * (v1.x + 1.0f);
v1.y = 0.5f * max_h * (v1.y + 1.0f);
v1.z = v1.z * 0.5f + 0.5f;
v2.x = 0.5f * screenWidth * (v2.x + 1.0f);
v2.y = 0.5f * screenHeight * (v2.y + 1.0f);
v2.x = 0.5f * max_w * (v2.x + 1.0f);
v2.y = 0.5f * max_h * (v2.y + 1.0f);
v2.z = v2.z * 0.5f + 0.5f;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册