Last active
August 19, 2025 18:51
-
-
Save Pikachuxxxx/750036c9c3f63db078b28247b720aae6 to your computer and use it in GitHub Desktop.
Love2D new setDepthClamp API tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local love = require("love") | |
-- Test case for setDepthClamp function | |
local TestDepthClamp = {} | |
function love.conf(t) | |
t.renderers = {"opengl"} | |
end | |
function TestDepthClamp:load() | |
love.window.setTitle("Depth Clamp Test") | |
-- Set window mode with depth buffer | |
love.window.setMode(800, 600, {depth=true}) | |
-- Enable depth testing | |
love.graphics.setDepthMode("less", true) | |
-- Current Renderer Info | |
local renderername = love.graphics.getRendererInfo() | |
love.window.setTitle(love.window.getTitle() .. " - " .. renderername) | |
-- Camera settings | |
self.camera = { | |
x = 0, y = 0, z = 5 | |
} | |
-- Simple cube vertices (no scaling, unit cube) | |
local vertices = { | |
-- Front face | |
{-1, -1, 1, 0, 0, 1, 0, 0}, | |
{ 1, -1, 1, 0, 0, 1, 1, 0}, | |
{ 1, 1, 1, 0, 0, 1, 1, 1}, | |
{-1, 1, 1, 0, 0, 1, 0, 1}, | |
-- Back face | |
{-1, -1, -1, 0, 0, -1, 1, 0}, | |
{-1, 1, -1, 0, 0, -1, 1, 1}, | |
{ 1, 1, -1, 0, 0, -1, 0, 1}, | |
{ 1, -1, -1, 0, 0, -1, 0, 0}, | |
-- Top face | |
{-1, 1, -1, 0, 1, 0, 0, 1}, | |
{-1, 1, 1, 0, 1, 0, 0, 0}, | |
{ 1, 1, 1, 0, 1, 0, 1, 0}, | |
{ 1, 1, -1, 0, 1, 0, 1, 1}, | |
-- Bottom face | |
{-1, -1, -1, 0, -1, 0, 1, 1}, | |
{ 1, -1, -1, 0, -1, 0, 0, 1}, | |
{ 1, -1, 1, 0, -1, 0, 0, 0}, | |
{-1, -1, 1, 0, -1, 0, 1, 0}, | |
-- Right face | |
{ 1, -1, -1, 1, 0, 0, 1, 0}, | |
{ 1, 1, -1, 1, 0, 0, 1, 1}, | |
{ 1, 1, 1, 1, 0, 0, 0, 1}, | |
{ 1, -1, 1, 1, 0, 0, 0, 0}, | |
-- Left face | |
{-1, -1, -1, -1, 0, 0, 0, 0}, | |
{-1, -1, 1, -1, 0, 0, 1, 0}, | |
{-1, 1, 1, -1, 0, 0, 1, 1}, | |
{-1, 1, -1, -1, 0, 0, 0, 1}, | |
} | |
-- Indices for the cube faces | |
local indices = { | |
1, 2, 3, 1, 3, 4, -- Front | |
5, 6, 7, 5, 7, 8, -- Back | |
9, 10, 11, 9, 11, 12, -- Top | |
13, 14, 15, 13, 15, 16, -- Bottom | |
17, 18, 19, 17, 19, 20, -- Right | |
21, 22, 23, 21, 23, 24 -- Left | |
} | |
-- Create vertex format | |
local vertex_format = { | |
{format = "floatvec3", location = 0}, -- Position | |
{format = "floatvec3", location = 1}, -- Normal | |
{format = "floatvec2", location = 2} -- TexCoord | |
} | |
-- Create mesh | |
self.cube_mesh = love.graphics.newMesh(vertex_format, vertices, "triangles") | |
self.cube_mesh:setVertexMap(indices) | |
-- Simple 3D shader | |
self.shader = love.graphics.newShader([[ | |
uniform mat4 model; | |
uniform mat4 view; | |
uniform mat4 projection; | |
varying vec3 worldPos; | |
vec4 position(mat4 transform_projection, vec4 vertex_position) { | |
vec4 worldPosition = model * vec4(vertex_position.xyz, 1.0); | |
worldPos = worldPosition.xyz; | |
return projection * view * worldPosition; | |
} | |
]], [[ | |
varying vec3 worldPos; | |
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) { | |
// Simple color based on world position | |
vec3 finalColor = 0.5 + 0.5 * sin(worldPos * 3.0); | |
return vec4(finalColor, 1.0); | |
} | |
]]) | |
-- Test state | |
self.depth_clamp_enabled = false | |
self.rotation_x = 0 | |
-- UI font | |
self.font = love.graphics.newFont(14) | |
end | |
function TestDepthClamp:update(dt) | |
-- Rotate around X-axis to show depth clipping | |
self.rotation_x = self.rotation_x + dt * 1.0 | |
-- Toggle depth clamp with space key | |
if love.keyboard.wasPressed("space") then | |
self.depth_clamp_enabled = not self.depth_clamp_enabled | |
love.graphics.setDepthClamp(self.depth_clamp_enabled) | |
print("Depth clamp:", self.depth_clamp_enabled and "ENABLED" or "DISABLED") | |
end | |
-- Camera movement controls | |
if love.keyboard.isDown("w") then | |
self.camera.z = self.camera.z - dt * 2.0 | |
elseif love.keyboard.isDown("s") then | |
self.camera.z = self.camera.z + dt * 2.0 | |
end | |
if love.keyboard.isDown("a") then | |
self.camera.x = self.camera.x - dt * 2.0 | |
elseif love.keyboard.isDown("d") then | |
self.camera.x = self.camera.x + dt * 2.0 | |
end | |
if love.keyboard.isDown("q") then | |
self.camera.y = self.camera.y + dt * 2.0 | |
elseif love.keyboard.isDown("e") then | |
self.camera.y = self.camera.y - dt * 2.0 | |
end | |
end | |
function TestDepthClamp:draw() | |
love.graphics.clear(0.2, 0.2, 0.3, 1.0) | |
-- Set up 3D rendering | |
local width, height = love.graphics.getDimensions() | |
local aspect = width / height | |
-- Tight projection matrix for obvious clipping | |
local fov = math.rad(45) | |
local near = 1.0 | |
local far = 6.0 -- Very tight far plane | |
local f = 1.0 / math.tan(fov * 0.5) | |
local projection = { | |
f / aspect, 0, 0, 0, | |
0, f, 0, 0, | |
0, 0, (far + near) / (near - far), (2 * far * near) / (near - far), | |
0, 0, -1, 0 | |
} | |
-- View matrix | |
local view = { | |
1, 0, 0, -self.camera.x, | |
0, 1, 0, -self.camera.y, | |
0, 0, 1, -self.camera.z, | |
0, 0, 0, 1 | |
} | |
-- Model matrix - simple X rotation | |
local cos_x = math.cos(self.rotation_x) | |
local sin_x = math.sin(self.rotation_x) | |
local model = { | |
1, 0, 0, 0, | |
0, cos_x, -sin_x, 0, | |
0, sin_x, cos_x, 0, | |
0, 0, 0, 1 | |
} | |
-- Render cube with depth testing | |
love.graphics.setShader(self.shader) | |
self.shader:send("model", model) | |
self.shader:send("view", view) | |
self.shader:send("projection", projection) | |
love.graphics.draw(self.cube_mesh) | |
love.graphics.setShader() | |
-- Draw UI on top (disable depth testing for UI) | |
love.graphics.setDepthMode() -- Disable depth testing for UI | |
love.graphics.setFont(self.font) | |
love.graphics.setColor(1, 1, 1, 1) | |
local y = 10 | |
love.graphics.print("Depth Clamp Test", 10, y) | |
y = y + 25 | |
love.graphics.print("Controls:", 10, y) | |
y = y + 20 | |
love.graphics.print(" SPACE: Toggle depth clamp (" .. | |
(self.depth_clamp_enabled and "ENABLED" or "DISABLED") .. ")", 10, y) | |
y = y + 20 | |
love.graphics.print(" WASD: Move camera, Q/E: Up/Down", 10, y) | |
y = y + 30 | |
love.graphics.print("Camera: (" .. string.format("%.1f", self.camera.x) .. ", " .. | |
string.format("%.1f", self.camera.y) .. ", " .. | |
string.format("%.1f", self.camera.z) .. ")", 10, y) | |
y = y + 20 | |
love.graphics.print("Near: 1.0, Far: 6.0", 10, y) | |
y = y + 30 | |
love.graphics.print("Watch cube edges extend beyond clip planes", 10, y) | |
y = y + 20 | |
love.graphics.print("Without clamp: Parts disappear", 10, y) | |
y = y + 20 | |
love.graphics.print("With clamp: Full cube visible", 10, y) | |
-- Re-enable depth testing for next frame | |
love.graphics.setDepthMode("less", true) | |
end | |
-- Main Love2D callbacks | |
function love.load() | |
TestDepthClamp:load() | |
end | |
function love.update(dt) | |
TestDepthClamp:update(dt) | |
end | |
function love.draw() | |
TestDepthClamp:draw() | |
end | |
-- Keyboard handling | |
local keys_pressed = {} | |
function love.keypressed(key) | |
keys_pressed[key] = true | |
end | |
function love.keyboard.wasPressed(key) | |
local pressed = keys_pressed[key] | |
keys_pressed[key] = false | |
return pressed | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment