Page 1 of 1

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

Posted: Mon Jul 15, 2024 6:26 pm
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.

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

Posted: Tue Jul 16, 2024 11:38 am
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.

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

Posted: Tue Jul 16, 2024 1:59 pm
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.)

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

Posted: Tue Jul 16, 2024 2:16 pm
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!

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

Posted: Wed Jul 17, 2024 10:14 am
by pgimeno
Well, you have this variant just for that: https://love2d.org/wiki/Shader:send#Function_6