0
\$\begingroup\$

I'm building my own engine using python and pygame however my cube model is not able to be displayed correctly. This is my result after applying translation, scaling and projection matrices:

Something is wrong and I don't know what, below are the matrices I use and the function for multiplying vectors with matrices.

The cube model is correct since I was able to display it using another code.

The render function is only multiplying each vector with the translation, scaling and then projection matrices.

Projection matrix

self.aspect_ratio = screen_width/screen_height
n = 0.1
f = 100     

# projection matrix
self.projection[0][0] = (1 / (math.tan(math.radians(90/2)))) / self.aspect_ratio
self.projection[0][1] = 0
self.projection[0][2] = 0
self.projection[0][3] = 0

self.projection[1][0] = 0
self.projection[1][1] = 1 / math.tan(math.radians(90/2))
self.projection[1][2] = 0
self.projection[1][3] = 0

self.projection[2][0] = 0
self.projection[2][1] = 0
self.projection[2][2] = f / (f - n)
self.projection[2][3] = (-f * n) / (f - n)

self.projection[3][0] = 0
self.projection[3][1] = 0
self.projection[3][2] = 1
self.projection[3][3] = 0

Translation matrix

self.translation = [
    [1, 0, 0, 50],
    [0, 1, 0, 50],
    [0, 0, 1, 0],
    [0, 0, 0, 1]
]

Scaling matrix

scale = 50
self.scaling = [
    [scale, 0,     0,     0],
    [0,     scale, 0,     0],
    [0,     0,     scale, 0],
    [0,     0,     0,     1]
]

Function for multiplying vectors with matrices

def Matrix_MultiplyVector(self, i, m):

    vx = i[0] * m[0][0] + i[1] * m[0][1] + i[2] * m[0][2] + m[0][3]
    vy = i[0] * m[1][0] + i[1] * m[1][1] + i[2] * m[1][2] + m[1][3]
    vz = i[0] * m[2][0] + i[1] * m[2][1] + i[2] * m[2][2] + m[2][3]
    vw = i[0] * m[3][0] + i[1] * m[3][1] + i[2] * m[3][2] + m[3][3]

    if vw != 0:
        vx /= vw
        vy /= vw
        vz /= vw
    return [vx,vy,vz,1]
\$\endgroup\$
2
  • \$\begingroup\$ Your translation matrix leaves the cube at 0 on thr z axis, and your projection matrix looks along that axis, so at best your camera would be stuck inside the cube looking out, with half of it behind you, leading to the issues I mentioned to you last time about polygons crossing the camera plane. Because you've also shifted it on x and y, it's likely to get pushed off screen almost entirely, so you only see the tiny bit in the corner. Try translating the cube out in front of the camera on the z axis by a multiple of its size and see how that changes the resulting visual. \$\endgroup\$
    – DMGregory
    Aug 6 at 7:14
  • \$\begingroup\$ Also, don't do the divide by w in your matrix multiply function. There's basically only one place where you want to do that, which is after multiplying by the perspective matrix. Adding it to all matrix ops is unnecessarily expensive, and can make it harder to get desired results with direction vectors (bivectors) like normals, rather than positions. For those, you want to preserve w = 0, not overwrite it with 1 after each matrix multiplication. \$\endgroup\$
    – DMGregory
    Aug 6 at 7:16

0

You must log in to answer this question.

Browse other questions tagged .