3D rendering "clips" outside a very small depth range

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
oatmealine
Prole
Posts: 3
Joined: Mon Jul 15, 2024 6:12 pm

3D rendering "clips" outside a very small depth range

Post by oatmealine »

Hello! I've been trying to do simple 3D rendering with LOVE, and have gotten pretty close, but got stuck on a certain issue.

For some reason, the mesh seems to very weirdly clip on depths too high or too low; additionally, perspective projection seems to not behave correctly with the camera's Z value (not scaling the mesh at all, just clipping it as described earlier).

With how peculiar and strange these issues are, I can only assume it's some simple mistake, like something I have to toggle or something I forgot to disable, but I haven't been able to figure it out at all. I'd really appreciate some help from someone who knows anything about how to do 3D in LOVE.

I've attached what I have so far - it's very minimal, but shows the issue. It slowly rotates the model on the X axis, but clips it at the edges. It does not have a Z buffer, but this is intentional as I don't plan to render anything more than a single plane, and adding it does not do anything.
Attachments
3d.love
(57.34 KiB) Downloaded 173 times
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: 3D rendering "clips" outside a very small depth range

Post by pgimeno »

I haven't downloaded your project, but from the description of the problem, it looks like your projection matrix isn't right (if present at all). The projection matrix handles both 3D projection and clipping.
User avatar
oatmealine
Prole
Posts: 3
Joined: Mon Jul 15, 2024 6:12 pm

Re: 3D rendering "clips" outside a very small depth range

Post by oatmealine »

Projection matrix should be present; I create it with CPML like

Code: Select all

cpml.mat4.from_perspective(160, love.graphics.getWidth() / love.graphics.getHeight(), 0.1, 1000.0)
and pass it into the vertex shader as

Code: Select all

projShader:send('u_viewproj', (v * p):to_vec4s())
(after multiplying it with my view matrix). (For what it's worth, letting the shader multiply them instead by passing them as separate uniforms doesn't seem to do anything either.)
User avatar
oatmealine
Prole
Posts: 3
Joined: Mon Jul 15, 2024 6:12 pm

Re: 3D rendering "clips" outside a very small depth range

Post by oatmealine »

I think I seem to have figured it out; CPML's mat4 module is column-major, while LOVE's Shader:send function is row-major as of 11.0. I was following mostly love3d's examples for this project, which used CPML, but those were made before 11.0, which is why they worked at the time while my code doesn't. While cross-comparing the matrices I got out of g3d with CPML's, that's the only difference I noticed - they were diagonally mirrored in the bottom right quadrant, which just so happens to be where perspective-related math seems to be stored at.

I believe the only modification that needs to be done is to_vec4s must be altered to return row-major tables. Luckily, CPML also has a to_vec4s_cols, and switching one to the other fixes the issue entirely. What a headache!
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: 3D rendering "clips" outside a very small depth range

Post by pgimeno »

Well, you have this variant just for that: https://love2d.org/wiki/Shader:send#Function_6
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests