Support and default to reverse-z with infinite far plane;

This commit is contained in:
bjorn 2022-07-17 12:37:59 -07:00
parent 1df537294a
commit 83fa750a4d
8 changed files with 29 additions and 18 deletions

View File

@ -22,5 +22,5 @@ void main() {
vec3 ray = vec3(uv, -1.);
mat3 inverseViewOrientation = transpose(mat3(View));
Direction = normalize(inverseViewOrientation * (InverseProjection * vec4(ray, 1.)).xyz);
Position = vec4(uv, 1, 1);
Position = vec4(uv, 0, 1);
}

View File

@ -9,6 +9,6 @@ void main() {
float x = -1 + float((VertexIndex & 1) << 2);
float y = -1 + float((VertexIndex & 2) << 1);
UV = vec2(x, y) * .5 + .5;
Position = vec4(x, y, 0., 1.);
Position = vec4(x, y, 1., 1.);
PointSize = 1.f;
}

View File

@ -459,7 +459,7 @@ static Canvas luax_checkcanvas(lua_State* L, int index) {
.loads = { LOAD_CLEAR, LOAD_CLEAR, LOAD_CLEAR, LOAD_CLEAR },
.depth.format = FORMAT_D32F,
.depth.load = LOAD_CLEAR,
.depth.clear = 1.f,
.depth.clear = 0.f,
.samples = 4
};

View File

@ -1898,7 +1898,7 @@ static int l_lovrMat4Perspective(lua_State* L) {
float fovy = luax_checkfloat(L, 2);
float aspect = luax_checkfloat(L, 3);
float n = luax_checkfloat(L, 4);
float f = luax_checkfloat(L, 5);
float f = luax_optfloat(L, 5, 0.);
mat4_perspective(m, fovy, aspect, n, f);
lua_settop(L, 1);
return 1;
@ -1911,7 +1911,7 @@ static int l_lovrMat4Fov(lua_State* L) {
float up = luax_checkfloat(L, 4);
float down = luax_checkfloat(L, 5);
float n = luax_checkfloat(L, 6);
float f = luax_checkfloat(L, 7);
float f = luax_optfloat(L, 7, 0.);
mat4_fov(m, left, right, up, down, n, f);
lua_settop(L, 1);
return 1;

View File

@ -606,20 +606,25 @@ MAF mat4 mat4_orthographic(mat4 m, float left, float right, float bottom, float
return m;
}
// Flips Y and maps z = [-n,-f] to [0,1] after dividing by w
// Flips Y and maps z = [-n,-f] to [0,1] after dividing by w, f == 0 inverts z with infinite far
MAF mat4 mat4_perspective(mat4 m, float fovy, float aspect, float n, float f) {
float cotan = 1.f / tanf(fovy * .5f);
memset(m, 0, 16 * sizeof(float));
m[0] = cotan / aspect;
m[5] = -cotan;
m[10] = f / (n - f);
m[11] = -1.f;
m[14] = (n * f) / (n - f);
m[15] = 0.f;
if (f == 0.f) {
m[10] = 0.f;
m[11] = -1.f;
m[14] = n;
} else {
m[10] = f / (n - f);
m[11] = -1.f;
m[14] = (n * f) / (n - f);
}
return m;
}
// Flips Y and maps z = [-n,-f] to [0,1] after dividing by w
// Flips Y and maps z = [-n,-f] to [0,1] after dividing by w, f == 0 inverts z with infinite far
MAF mat4 mat4_fov(mat4 m, float left, float right, float up, float down, float n, float f) {
left = -tanf(left);
right = tanf(right);
@ -630,9 +635,15 @@ MAF mat4 mat4_fov(mat4 m, float left, float right, float up, float down, float n
m[5] = 2.f / (down - up);
m[8] = (right + left) / (right - left);
m[9] = (down + up) / (down - up);
m[10] = f / (n - f);
m[11] = -1.f;
m[14] = (n * f) / (n - f);
if (f == 0.f) {
m[10] = 0.f;
m[11] = -1.f;
m[14] = n;
} else {
m[10] = f / (n - f);
m[11] = -1.f;
m[14] = (n * f) / (n - f);
}
return m;
}

View File

@ -3064,7 +3064,7 @@ Pass* lovrGraphicsGetPass(PassInfo* info) {
.depth.format = canvas->depth.texture ? canvas->depth.texture->info.format : canvas->depth.format,
.multisample.count = canvas->samples,
.viewCount = main->depth,
.depth.test = GPU_COMPARE_LEQUAL,
.depth.test = GPU_COMPARE_GEQUAL,
.depth.write = true
};
@ -3094,7 +3094,7 @@ Pass* lovrGraphicsGetPass(PassInfo* info) {
pass->cameras = tempAlloc(pass->cameraCount * sizeof(Camera));
for (uint32_t i = 0; i < pass->cameraCount; i++) {
mat4_identity(pass->cameras[i].view);
mat4_perspective(pass->cameras[i].projection, 1.f, (float) main->width / main->height, .01f, 100.f);
mat4_perspective(pass->cameras[i].projection, 1.f, (float) main->width / main->height, .01f, 0.f);
}
pass->cameraDirty = true;

View File

@ -39,7 +39,7 @@ static void onFocus(bool focused) {
static bool desktop_init(float supersample, float offset, uint32_t msaa, bool overlay) {
state.offset = offset;
state.clipNear = .01f;
state.clipFar = 100.f;
state.clipFar = 0.f;
state.prevDisplayTime = os_get_time();
state.nextDisplayTime = state.prevDisplayTime;

View File

@ -783,7 +783,7 @@ static bool openxr_init(float supersample, float offset, uint32_t msaa, bool ove
}
state.clipNear = .01f;
state.clipFar = 100.f;
state.clipFar = 0.f;
state.frameState.type = XR_TYPE_FRAME_STATE;
return true;
}