view space is now also z-up. The projection matrix now transforms z-up to y-up.

This makes it more intuitive to use the camera matrix for cpu-side computations. (see #323)
This commit is contained in:
IntegratedQuantum 2024-04-15 11:24:59 +02:00
parent 413372a321
commit 3d3a1322cb
5 changed files with 19 additions and 29 deletions

View File

@ -65,7 +65,7 @@ float zFromDepth(float depthBufferValue) {
float calculateFogDistance(float dist, float fogDensity) {
float distCameraTerrain = dist*fogDensity;
float distFromCamera = abs(mvVertexPos.z)*fogDensity;
float distFromCamera = abs(mvVertexPos.y)*fogDensity;
float distFromTerrain = distFromCamera - distCameraTerrain;
if(distCameraTerrain < 10) { // Resolution range is sufficient.
return distFromTerrain;
@ -108,7 +108,7 @@ void main() {
float animatedTextureIndex = animatedTexture[textureIndex];
vec3 textureCoords = vec3(uv, animatedTextureIndex);
float normalVariation = lightVariation(normal);
float densityAdjustment = sqrt(dot(mvVertexPos, mvVertexPos))/abs(mvVertexPos.z);
float densityAdjustment = sqrt(dot(mvVertexPos, mvVertexPos))/abs(mvVertexPos.y);
float dist = zFromDepth(texelFetch(depthTexture, ivec2(gl_FragCoord.xy), 0).r);
float fogDistance = calculateFogDistance(dist, fogData[int(animatedTextureIndex)].fogDensity*densityAdjustment);
float airFogDistance = calculateFogDistance(dist, fog.density*densityAdjustment);

View File

@ -41,12 +41,7 @@ pub const camera = struct {
pub fn updateViewMatrix() void {
direction = vec.rotateZ(vec.rotateX(Vec3f{0, 1, 0}, -rotation[0]), -rotation[2]);
viewMatrix = Mat4f.identity().mul(.{.rows = .{
.{1, 0, 0, 0},
.{0, 0, 1, 0},
.{0,-1, 0, 0},
.{0, 0, 0, 1},
}}).mul(Mat4f.rotationX(rotation[0])).mul(Mat4f.rotationZ(rotation[2]));
viewMatrix = Mat4f.identity().mul(Mat4f.rotationX(rotation[0])).mul(Mat4f.rotationZ(rotation[2]));
}
};

View File

@ -1864,12 +1864,7 @@ pub fn generateBlockTexture(blockType: u16) Texture {
const projMatrix = Mat4f.perspective(0.013, 1, 64, 256);
const oldViewMatrix = main.game.camera.viewMatrix;
main.game.camera.viewMatrix = Mat4f.identity().mul(.{.rows = .{
.{1, 0, 0, 0},
.{0, 0, 1, 0},
.{0,-1, 0, 0},
.{0, 0, 0, 1},
}}).mul(Mat4f.rotationX(std.math.pi/4.0)).mul(Mat4f.rotationZ(-3.0*std.math.pi/4.0));
main.game.camera.viewMatrix = Mat4f.identity().mul(Mat4f.rotationX(std.math.pi/4.0)).mul(Mat4f.rotationZ(-3.0*std.math.pi/4.0));
defer main.game.camera.viewMatrix = oldViewMatrix;
if(block.transparent()) {
c.glBlendEquation(c.GL_FUNC_ADD);

View File

@ -294,7 +294,7 @@ pub fn renderWorld(world: *World, ambientLight: Vec3f, skyColor: Vec3f, playerPo
}
c.glUniform1f(deferredUniforms.zNear, zNear);
c.glUniform1f(deferredUniforms.zFar, zFar);
c.glUniform2f(deferredUniforms.tanXY, 1.0/game.projectionMatrix.rows[0][0], 1.0/game.projectionMatrix.rows[1][1]);
c.glUniform2f(deferredUniforms.tanXY, 1.0/game.projectionMatrix.rows[0][0], 1.0/game.projectionMatrix.rows[1][2]);
c.glBindFramebuffer(c.GL_FRAMEBUFFER, activeFrameBuffer);
@ -367,7 +367,7 @@ const Bloom = struct {
}
c.glUniform1f(colorExtractUniforms.zNear, zNear);
c.glUniform1f(colorExtractUniforms.zFar, zFar);
c.glUniform2f(colorExtractUniforms.tanXY, 1.0/game.projectionMatrix.rows[0][0], 1.0/game.projectionMatrix.rows[1][1]);
c.glUniform2f(colorExtractUniforms.tanXY, 1.0/game.projectionMatrix.rows[0][0], 1.0/game.projectionMatrix.rows[1][2]);
c.glBindVertexArray(graphics.draw.rectVAO);
c.glDrawArrays(c.GL_TRIANGLE_STRIP, 0, 4);
}
@ -449,15 +449,15 @@ pub const MenuBackGround = struct {
// 4 sides of a simple cube with some panorama texture on it.
const rawData = [_]f32 {
-1, -1, -1, 1, 1,
-1, 1, -1, 1, 0,
-1, -1, 1, 0.75, 1,
-1, -1, 1, 1, 0,
-1, 1, -1, 0.75, 1,
-1, 1, 1, 0.75, 0,
1, -1, 1, 0.5, 1,
1, 1, -1, 0.5, 1,
1, 1, 1, 0.5, 0,
1, -1, -1, 0.25, 1,
1, 1, -1, 0.25, 0,
1, -1, 1, 0.25, 0,
-1, -1, -1, 0, 1,
-1, 1, -1, 0, 0,
-1, -1, 1, 0, 0,
};
const indices = [_]c_int {
@ -527,11 +527,11 @@ pub const MenuBackGround = struct {
if(texture.textureID == 0) return;
c.glDisable(c.GL_CULL_FACE); // I'm not sure if my triangles are rotated correctly, and there are no triangles facing away from the player anyways.
// Use a simple rotation around the y axis, with a steadily increasing angle.
// Use a simple rotation around the z axis, with a steadily increasing angle.
const newTime = std.time.nanoTimestamp();
angle += @as(f32, @floatFromInt(newTime - lastTime))/2e10;
lastTime = newTime;
const viewMatrix = Mat4f.rotationY(angle);
const viewMatrix = Mat4f.rotationZ(angle);
shader.bind();
c.glUniformMatrix4fv(uniforms.viewMatrix, 1, c.GL_TRUE, @ptrCast(&viewMatrix));
@ -612,8 +612,8 @@ pub const Frustum = struct {
pub fn init(cameraPos: Vec3f, rotationMatrix: Mat4f, fovY: f32, width: u31, height: u31) Frustum {
const invRotationMatrix = rotationMatrix.transpose();
const cameraDir = vec.xyz(invRotationMatrix.mulVec(Vec4f{0, 0, 1, 1}));
const cameraUp = vec.xyz(invRotationMatrix.mulVec(Vec4f{0, 1, 0, 1}));
const cameraDir = vec.xyz(invRotationMatrix.mulVec(Vec4f{0, 1, 0, 1}));
const cameraUp = vec.xyz(invRotationMatrix.mulVec(Vec4f{0, 0, 1, 1}));
const cameraRight = vec.xyz(invRotationMatrix.mulVec(Vec4f{1, 0, 0, 1}));
const halfVSide = std.math.tan(std.math.degreesToRadians(fovY)*0.5);

View File

@ -148,10 +148,10 @@ pub const Mat4f = struct {
const tanX = aspect*tanY;
return Mat4f {
.rows = [4]Vec4f {
Vec4f{1/tanX, 0, 0, 0},
Vec4f{0, 1/tanY, 0, 0},
Vec4f{0, 0, (far + near)/(near - far), 2*near*far/(near - far)},
Vec4f{0, 0, -1, 0},
Vec4f{1/tanX, 0, 0, 0},
Vec4f{0, 0, 1/tanY, 0},
Vec4f{0, -(far + near)/(near - far), 0, 2*near*far/(near - far)},
Vec4f{0, 1, 0, 0},
}
};
}