Hand-made fake 3D - problems with rendering

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
Carotino
Prole
Posts: 3
Joined: Thu Feb 18, 2021 9:00 pm

Hand-made fake 3D - problems with rendering

Post by Carotino »

Hello to everybody.
I'm new here, but not so new to Löve.
I recently watched Top Gun so I decided to create something inspired to the old arcade Afterburner with its nice fake 3D.
I've been exploring 3D libraries for Löve, but in the end I decided that I don't want a true 3D environment. I want to retain all the Löve 2D goodness. Being more specific I want to know exactly where pixels are drawn in my window.
This is what I'm doing:
* I use groverburger's G3D matrix functions in order to create the matrices I need (view, projection, model)
* I define geometry in terms of x y z, within the usual bounds of -1, 1 etc.
* therefore I multiply the matrices and the vertices, obtaining a transformed point
* I convert this point to 2D coordinates by dividing by W and remapping to 2D coords
It's like I'm emulating a GPU.
So far, so good: it works.
A problem arise when I have a point that, when transformed by the matrices, get a negative Z. After a certain threshold the "rasterisation" fails, and the point is sent to the other side of the screen, screwing the rendering.

I've tried to put together a single file showcasing the whole contraption, with a 3D grid rendered on screen. I print on screen the coords of the bottom right point of the grid: untransformed, then transformed and finally converted to 2D.
Pressing L and K the camera is rotated on the Y axis.
When doing the same thing with G3D proper, obviously the GPU does the right thing...

I'd like to know if someone has a clue on why this error happens.

Thanks in advance

Carotino
main.lua
(6.7 KiB) Downloaded 120 times
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Hand-made fake 3D - problems with rendering

Post by 4vZEROv »

Code: Select all

cameraSettings.target[1] = cameraSettings.target[1] + amount
This doesn't seem right to me, in g3d camera.target is the normalized vec3 that the camera face, to turn the camera it use this function

Code: Select all

-- move and rotate the camera, given a point and a direction and a pitch (vertical direction)
function camera.lookInDirection(x,y,z, directionTowards,pitchTowards)
    camera.position[1] = x or camera.position[1]
    camera.position[2] = y or camera.position[2]
    camera.position[3] = z or camera.position[3]

    fpsController.direction = directionTowards or fpsController.direction
    fpsController.pitch = pitchTowards or fpsController.pitch

    -- convert the direction and pitch into a target point

    -- turn the cos of the pitch into a sign value, either 1, -1, or 0
    local sign = math.cos(fpsController.pitch)
    sign = (sign > 0 and 1) or (sign < 0 and -1) or 0

    -- don't let cosPitch ever hit 0, because weird camera glitches will happen
    local cosPitch = sign*math.max(math.abs(math.cos(fpsController.pitch)), 0.00001)

    camera.target[1] = camera.position[1]+math.sin(fpsController.direction)*cosPitch
    camera.target[2] = camera.position[2]-math.sin(fpsController.pitch)
    camera.target[3] = camera.position[3]+math.cos(fpsController.direction)*cosPitch

    -- update the camera in the shader
    camera.updateViewMatrix()
end
You should use this code to turn your camera.


To note: camera.direction is the angle of the camera Z axis, camera.pitch is Y axis
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Hand-made fake 3D - problems with rendering

Post by pgimeno »

It looks to me that what your code is missing is clipping to the near plane. That's something OpenGL does and your code doesn't, and I suspect that the lack of clipping may be causing that effect.
Carotino
Prole
Posts: 3
Joined: Thu Feb 18, 2021 9:00 pm

Re: Hand-made fake 3D - problems with rendering

Post by Carotino »

@4vZEROv you're right about the rotation of the camera

About the clipping, I think you're right. Maybe I can circumvent the problem somehow.

Thanks
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests