Page 1 of 1

Projection matrix issue

Posted: Fri Aug 30, 2019 3:02 am
by Papaew
Hello! I'm trying to create simple 3d render engine, but i'm stuck on projection matrix.
I don't know why but it do not give a result (i tried several examples from different sources)
Can someone help me with it?

p.s. I use my own vector and matrix libraries. They are works fine (i guess)

Re: Projection matrix issue

Posted: Fri Aug 30, 2019 10:11 pm
by Papaew
Okay, some good people helped me on love's discord server (thank you again ^^)

As expected problem was in my perspective matrix. It was column-major, not row-major.
There is a multiple variants to resolve this priblem:
a) In shader:send use "column" parameter instead "row"
b) Use row-major perspective projection matrix:

Code: Select all

function perspective_projection(fov, aspect, near, far)
local t = tan(rad(fov)/2)
return mat4(1/(aspect*t),0,0,0,
            0,1/t,0,0,
            0,0,-(far+near)/(far-near),-(2*far*near)/(far-near),
            0,0,-1,0)
end
c) Convert matrix from column-major to row-major like this:

Code: Select all

projection = columnToRow(projection:unpack())
shader:send("proj", projection)

function columnToRow(matrix)
    -- Column -> Row major
    if love._version_major > 0 then
        matrix[2], matrix[5] = matrix[5], matrix[2]
        matrix[3], matrix[9] = matrix[9], matrix[3]
        matrix[4], matrix[13] = matrix[13], matrix[4]
        matrix[7], matrix[10] = matrix[10], matrix[7]
        matrix[8], matrix[14] = matrix[14], matrix[8]
        matrix[12], matrix[15] = matrix[15], matrix[12]
    end
    return matrix
end
And also need to change model position -10 units by z-axis to make sure it's in front of the camera
uneven 3dlib.zip
There is a fixed code here
(38.71 KiB) Downloaded 144 times