diff --git a/assets/cubyz/shaders/bloom/color_extractor_downsample.vert b/assets/cubyz/shaders/bloom/color_extractor_downsample.vert index afbe93119d..9e50a18c3e 100644 --- a/assets/cubyz/shaders/bloom/color_extractor_downsample.vert +++ b/assets/cubyz/shaders/bloom/color_extractor_downsample.vert @@ -12,7 +12,7 @@ layout(location = 0) uniform mat4 invViewMatrix; layout(location = 1) uniform vec2 tanXY; void main() { - vec2 position = inTexCoords*2 - vec2(1, 1); + vec2 position = inTexCoords*2+vec2(-1, -1); direction = (invViewMatrix * vec4(position.x*tanXY.x, 1, position.y*tanXY.y, 0)).xyz; normalizedTexCoords = inTexCoords; texCoords = inTexCoords*textureSize(color, 0) - 0.25; diff --git a/assets/cubyz/shaders/bloom/first_pass.vert b/assets/cubyz/shaders/bloom/first_pass.vert index e098cfc5fc..1b5aaad3af 100644 --- a/assets/cubyz/shaders/bloom/first_pass.vert +++ b/assets/cubyz/shaders/bloom/first_pass.vert @@ -5,6 +5,10 @@ layout(location = 0) in vec2 inTexCoords; layout(location = 0) out vec2 texCoords; void main() { +#ifdef OPEN_GL + texCoords = vec2(inTexCoords.x, 1 - inTexCoords.y); +#else texCoords = inTexCoords; - gl_Position = vec4(inTexCoords*2 - vec2(1, 1), 0, 1); +#endif + gl_Position = vec4(inTexCoords*2 + vec2(-1, -1), 0, 1); } diff --git a/assets/cubyz/shaders/deferred_render_pass.vert b/assets/cubyz/shaders/deferred_render_pass.vert index 489b1d4478..6c477dc17a 100644 --- a/assets/cubyz/shaders/deferred_render_pass.vert +++ b/assets/cubyz/shaders/deferred_render_pass.vert @@ -13,7 +13,10 @@ void main() { directions[1] = (invViewMatrix * vec4(1*tanXY.x, 1, -1*tanXY.y, 0)).xyz; directions[2] = (invViewMatrix * vec4(-1*tanXY.x, 1, 1*tanXY.y, 0)).xyz; directions[3] = (invViewMatrix * vec4(-1*tanXY.x, 1, -1*tanXY.y, 0)).xyz; +#ifdef OPEN_GL + texCoords = vec2(inTexCoords.x, 1 - inTexCoords.y); +#else texCoords = inTexCoords; - vec2 position = inTexCoords*2 - vec2(1, 1); - gl_Position = vec4(position, 0, 1); +#endif + gl_Position = vec4(inTexCoords*2 + vec2(-1, -1), 0, 1); } diff --git a/assets/cubyz/shaders/fake_reflection.vert b/assets/cubyz/shaders/fake_reflection.vert index 992d667765..ee3c521920 100644 --- a/assets/cubyz/shaders/fake_reflection.vert +++ b/assets/cubyz/shaders/fake_reflection.vert @@ -7,6 +7,11 @@ layout(location = 0) out vec3 coords; layout(location = 0) uniform float reflectionMapSize; void main() { - coords = vec3((inTexCoords*2 - vec2(1, 1))*(reflectionMapSize + 1)/reflectionMapSize, 1); - gl_Position = vec4(inTexCoords*2 - vec2(1, 1), 0, 1); +#ifdef OPEN_GL + vec2 texCoords = vec2(inTexCoords.x, 1 - inTexCoords.y); + coords = vec3((texCoords*2 + vec2(-1, -1))*(reflectionMapSize + 1)/reflectionMapSize, 1); +#else + coords = vec3((inTexCoords*2 + vec2(-1, -1))*(reflectionMapSize + 1)/reflectionMapSize, 1); +#endif + gl_Position = vec4(inTexCoords*2 + vec2(-1, -1), 0, 1); } diff --git a/assets/cubyz/shaders/graphics/Image.vert b/assets/cubyz/shaders/graphics/Image.vert index d0954220d3..0c873ed7fa 100644 --- a/assets/cubyz/shaders/graphics/Image.vert +++ b/assets/cubyz/shaders/graphics/Image.vert @@ -26,10 +26,9 @@ layout(push_constant, std430) uniform _ { #endif void main() { - // Convert to opengl coordinates: vec2 position_percentage = (start + vec2(vertex_pos.x*size.x, size.y - vertex_pos.y*size.y))/screen; - vec2 position = vec2(position_percentage.x, -position_percentage.y)*2+vec2(-1, 1); + vec2 position = position_percentage*2+vec2(-1, -1); gl_Position = vec4(position, 0, 1); diff --git a/assets/cubyz/shaders/graphics/Line.vert b/assets/cubyz/shaders/graphics/Line.vert index f2eaf246fd..575224b751 100644 --- a/assets/cubyz/shaders/graphics/Line.vert +++ b/assets/cubyz/shaders/graphics/Line.vert @@ -21,10 +21,9 @@ layout(push_constant, std430) uniform _ { #endif void main() { - // Convert to opengl coordinates: vec2 position_percentage = (start + vertex_pos*direction)/screen; - vec2 position = vec2(position_percentage.x, -position_percentage.y)*2+vec2(-1, 1); + vec2 position = position_percentage*2+vec2(-1, -1); gl_Position = vec4(position, 0, 1); diff --git a/assets/cubyz/shaders/graphics/Rect.vert b/assets/cubyz/shaders/graphics/Rect.vert index 405b647061..2560484bb1 100644 --- a/assets/cubyz/shaders/graphics/Rect.vert +++ b/assets/cubyz/shaders/graphics/Rect.vert @@ -21,10 +21,9 @@ layout(push_constant, std430) uniform _ { #endif void main() { - // Convert to opengl coordinates: vec2 position_percentage = (start + vertex_pos*size)/screen; - vec2 position = vec2(position_percentage.x, -position_percentage.y)*2+vec2(-1, 1); + vec2 position = position_percentage*2+vec2(-1, -1); gl_Position = vec4(position, 0, 1); diff --git a/assets/cubyz/shaders/graphics/RectBorder.vert b/assets/cubyz/shaders/graphics/RectBorder.vert index db1c6e1cdb..fc0c8120d0 100644 --- a/assets/cubyz/shaders/graphics/RectBorder.vert +++ b/assets/cubyz/shaders/graphics/RectBorder.vert @@ -23,10 +23,9 @@ layout(push_constant, std430) uniform _ { #endif void main() { - // Convert to opengl coordinates: vec2 position_percentage = (start + vertex_pos.xy*size + vertex_pos.zw*lineWidth)/screen; - vec2 position = vec2(position_percentage.x, -position_percentage.y)*2+vec2(-1, 1); + vec2 position = position_percentage*2+vec2(-1, -1); gl_Position = vec4(position, 0, 1); diff --git a/assets/cubyz/shaders/graphics/Text.vert b/assets/cubyz/shaders/graphics/Text.vert index 5f59862ddb..dc09d62c6a 100644 --- a/assets/cubyz/shaders/graphics/Text.vert +++ b/assets/cubyz/shaders/graphics/Text.vert @@ -25,10 +25,9 @@ void main() { vertex_pos.x += vertex_pos.y/textureRect.z; } - // convert glyph coords to opengl coords vec4 rect = vec4(position_percentage, size_percentage); - vec2 position = vec2(rect.x+vertex_pos.x*rect.z, -rect.y+vertex_pos.y*rect.w)*2+vec2(-1, 1); + vec2 position = vec2(rect.x+vertex_pos.x*rect.z, rect.y-vertex_pos.y*rect.w)*2+vec2(-1, -1); gl_Position = vec4(position, 0, 1); frag_face_pos = face_pos; diff --git a/assets/cubyz/shaders/graphics/graph.vert b/assets/cubyz/shaders/graphics/graph.vert index 08f35280e8..ef4ffb4214 100644 --- a/assets/cubyz/shaders/graphics/graph.vert +++ b/assets/cubyz/shaders/graphics/graph.vert @@ -27,10 +27,9 @@ layout(std430, binding = 5) buffer _data void main() { float x = gl_VertexIndex; float y = -data[(gl_VertexIndex + offset)%points]; - // Convert to opengl coordinates: vec2 position_percentage = (start + dimension*vec2(x/points, y))/screen; - vec2 position = vec2(position_percentage.x, -position_percentage.y)*2 + vec2(-1, 1); + vec2 position = position_percentage*2+vec2(-1, -1); gl_Position = vec4(position, 0, 1); } diff --git a/assets/cubyz/shaders/item_texture_post.vert b/assets/cubyz/shaders/item_texture_post.vert index e098cfc5fc..ca54d63367 100644 --- a/assets/cubyz/shaders/item_texture_post.vert +++ b/assets/cubyz/shaders/item_texture_post.vert @@ -5,6 +5,12 @@ layout(location = 0) in vec2 inTexCoords; layout(location = 0) out vec2 texCoords; void main() { + +#ifdef OPEN_GL + texCoords = vec2(inTexCoords.x, 1 - inTexCoords.y); +#else texCoords = inTexCoords; - gl_Position = vec4(inTexCoords*2 - vec2(1, 1), 0, 1); +#endif + + gl_Position = vec4(inTexCoords*2 + vec2(-1, -1), 0, 1); } diff --git a/assets/cubyz/shaders/ui/button.frag b/assets/cubyz/shaders/ui/button.frag index d98b5717e1..d6706509c9 100644 --- a/assets/cubyz/shaders/ui/button.frag +++ b/assets/cubyz/shaders/ui/button.frag @@ -1,5 +1,9 @@ #version 460 +#ifdef OPEN_GL +layout(origin_upper_left) in vec4 gl_FragCoord; +#endif + layout(location = 0) out vec4 frag_color; layout(location = 0) in vec2 startCoord; @@ -20,6 +24,7 @@ layout(push_constant, std430) uniform _ { #endif void main() { - frag_color = texture(image, (gl_FragCoord.xy - startCoord)/(2*scale)/textureSize(image, 0)); + vec2 textureCoords = (gl_FragCoord.xy - startCoord)/(2*scale)/textureSize(image, 0); + frag_color = texture(image, vec2(textureCoords.x, -textureCoords.y)); frag_color *= fColor; } diff --git a/assets/cubyz/shaders/ui/button.vert b/assets/cubyz/shaders/ui/button.vert index f9e7bd30b8..23ac95f3b8 100644 --- a/assets/cubyz/shaders/ui/button.vert +++ b/assets/cubyz/shaders/ui/button.vert @@ -23,12 +23,10 @@ layout(push_constant, std430) uniform _ { #endif void main() { - // Convert to opengl coordinates: vec2 position_percentage = (start + vertex_pos*size)/screen; - startCoord.x = start.x; - startCoord.y = screen.y - start.y; + startCoord = start; - vec2 position = vec2(position_percentage.x, -position_percentage.y)*2+vec2(-1, 1); + vec2 position = position_percentage*2+vec2(-1, -1); gl_Position = vec4(position, 0, 1); diff --git a/assets/cubyz/shaders/ui/window_border.vert b/assets/cubyz/shaders/ui/window_border.vert index db0c0d657d..c1a081d3aa 100644 --- a/assets/cubyz/shaders/ui/window_border.vert +++ b/assets/cubyz/shaders/ui/window_border.vert @@ -25,14 +25,13 @@ layout(push_constant, std430) uniform _ { #endif void main() { - // Convert to opengl coordinates: vec2 position_percentage = (start + vertex_pos*size)/screen; startCoord.x = start.x; startCoord.y = screen.y - start.y - size.y; endCoord.x = start.x + size.x; endCoord.y = screen.y - start.y; - vec2 position = vec2(position_percentage.x, -position_percentage.y)*2+vec2(-1, 1); + vec2 position = position_percentage*2+vec2(-1, -1); gl_Position = vec4(position, 0, 1); diff --git a/src/graphics.zig b/src/graphics.zig index 79350a9ef5..7d1739985d 100644 --- a/src/graphics.zig +++ b/src/graphics.zig @@ -81,7 +81,7 @@ pub const draw = struct { // MARK: draw c.glGetIntegerv(c.GL_VIEWPORT, &viewport); var newClip = Vec4i{ std.math.lossyCast(i32, translation[0]), - viewport[3] - std.math.lossyCast(i32, translation[1] + clipRect[1]*scale), + std.math.lossyCast(i32, translation[1]), std.math.lossyCast(i32, clipRect[0]*scale), std.math.lossyCast(i32, clipRect[1]*scale), }; diff --git a/src/graphics/Window.zig b/src/graphics/Window.zig index ff55fb22e0..3957e5ff85 100644 --- a/src/graphics/Window.zig +++ b/src/graphics/Window.zig @@ -779,6 +779,7 @@ pub fn init() void { // MARK: init() if (c.gladLoadGL(c.glfwGetProcAddress) == 0) { @panic("Failed to load OpenGL functions from GLAD"); } + c.glClipControl(c.GL_UPPER_LEFT, c.GL_NEGATIVE_ONE_TO_ONE); reloadSettings(); c.glEnable(c.GL_DEBUG_OUTPUT); diff --git a/src/graphics/pipelines.zig b/src/graphics/pipelines.zig index 22c0430962..0c8dca8f3e 100644 --- a/src/graphics/pipelines.zig +++ b/src/graphics/pipelines.zig @@ -820,7 +820,11 @@ pub const Pipeline = struct { // MARK: Pipeline self.shader.bind(); if (scissor) |s| { c.glEnable(c.GL_SCISSOR_TEST); - c.glScissor(s.offset.x, s.offset.y, @intCast(s.extent.width), @intCast(s.extent.height)); + var viewport: [4]c_int = undefined; + c.glGetIntegerv(c.GL_VIEWPORT, &viewport); + const width: c_int = @intCast(s.extent.width); + const height: c_int = @intCast(s.extent.height); + c.glScissor(s.offset.x, viewport[1] + viewport[3] - (s.offset.y + height), width, height); } else { c.glDisable(c.GL_SCISSOR_TEST); } @@ -845,8 +849,8 @@ pub const Pipeline = struct { // MARK: Pipeline c.glDisable(c.GL_CULL_FACE); } c.glFrontFace(switch (self.rasterState.frontFace) { - .counterClockwise => c.GL_CCW, - .clockwise => c.GL_CW, + .counterClockwise => c.GL_CW, + .clockwise => c.GL_CCW, }); if (self.rasterState.depthBias) |depthBias| { c.glEnable(c.GL_POLYGON_OFFSET_FILL); diff --git a/src/renderer.zig b/src/renderer.zig index 09d68785bf..776c7902ee 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -324,7 +324,7 @@ pub fn renderWorld(world: *World, ambientLight: Vec3f, skyColor: Vec3f, playerPo c.glUniformMatrix4fv(deferredUniforms.invViewMatrix, 1, c.GL_TRUE, @ptrCast(&game.camera.viewMatrix.transpose())); 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][2]); + c.glUniform2f(deferredUniforms.tanXY, 1.0/game.projectionMatrix.rows[0][0], -1.0/game.projectionMatrix.rows[1][2]); c.glBindFramebuffer(c.GL_FRAMEBUFFER, activeFrameBuffer); diff --git a/src/systems/modelRenderer.zig b/src/systems/modelRenderer.zig index f0df76e22a..d01ab219f4 100644 --- a/src/systems/modelRenderer.zig +++ b/src/systems/modelRenderer.zig @@ -94,7 +94,7 @@ pub const client = struct { // MARK: client const projectedPos = Mat4f.fromGl(main.graphics.frame_uniforms.frameData().projectionMatrix).mulVec(rotatedPos); if (projectedPos[2] < 0) continue; const xCenter = (1 + projectedPos[0]/projectedPos[3])*@as(f32, @floatFromInt(main.Window.width/2)); - const yCenter = (1 - projectedPos[1]/projectedPos[3])*@as(f32, @floatFromInt(main.Window.height/2)); + const yCenter = (1 + projectedPos[1]/projectedPos[3])*@as(f32, @floatFromInt(main.Window.height/2)); const transparency = 38.0*std.math.log10(vec.lengthSquare(pos3d) + 1) - 80.0; const alpha: u32 = @trunc(std.math.clamp(0xff - transparency, 0, 0xff)); diff --git a/src/vec.zig b/src/vec.zig index d7b490be4a..08dbf6ce90 100644 --- a/src/vec.zig +++ b/src/vec.zig @@ -302,8 +302,8 @@ pub const Mat4f = struct { // MARK: Mat4f return Mat4f{ .rows = [4]Vec4f{ 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, 0, -1/tanY, 0}, + Vec4f{0, (far + near)/(far - near), 0, 2*near*far/(near - far)}, Vec4f{0, 1, 0, 0}, }, };