2
votes

I am attempting to write a basic program in modern OpenGL using Python and pyglet. I am able to place a simple triangle on the screen with different colors for each of the corners. I am at the point where I am attempting to add the projection and view matrix so that I am able to move the "camera" around in 3D space, but it keeps black-screening when I add both matrices. I can add the projection matrix and it works as expected. I can simply just add the view matrix, and it still shows the triangle. However, when I multiply the view matrix and the projection matrix together it gives a black screen.

Since I am able to produce a colored triangle with the projection matrix, I think I can safely assume that all my OpenGL context is valid. So, maybe I must have misunderstood the mathematics.

Here is a summary of my relevant code:

import pyrr

fov = 60
aspect_ratio = 800 / 600 #width / height
near_clip = 0.1
far_clip = 100

#create a perspective matrix
projection_matrix = pyrr.matrix44.create_perspective_projection(
fov,
aspect_ratio,
near_clip,
far_clip
)

view_matrix = pyrr.matrix44.creat_look_at(
pyrr.vector3.create(0, 0, 1), #camera position
pyrr.vector3.create(0, 0, 0), #camera target
pyrr.vector3.create(0, 1, 0)  #camera up vector
)

mvp_matrix = projection_matrix * view_matrix

From here, I translate the mvp_matrix into a c_types array that OpenGL can understand. I don't think the code that does the translation is relevant since it works with just the projection matrix correctly.

Here are the matrices that pyrr is giving me:

Projection Matrix:
[[ 1.15470054  0.          0.          0.        ]
 [ 0.          1.73205081  0.          0.        ]
 [ 0.          0.         -1.002002   -1.        ]
 [ 0.          0.         -0.2002002   0.        ]]

View Matrix:
[[1.  0.  0.  0.]
 [0.  1.  0.  0.]
 [0.  0.  1.  0.]
 [0.  0. -1.  1.]]

MVP Matrix:
[[ 1.15470054  0.          0.          0.        ]
 [ 0.          1.73205081  0.          0.        ]
 [ 0.          0.         -1.002002   -0.        ]
 [ 0.          0.         -0.          0.        ]]

Any ideas on why just the projection matrix is able to show the triangle, but when I add in the view matrix it just shows black screens?

I've tried making the triangle really big on all the x, y, and z-axis, but still just a black screen. =/

1
The "MVP Matrix" seems to be not correct, because there is no translation along the z axis. That does not reflect the 1st parameter pyrr.vector3.create(0, 0, 1) (camera position).Rabbid76

1 Answers

0
votes

After some more testing I've discovered that the pyrr module seems to be performing matrix multiplication strangely. Pyrr is built on numpy however, so after using:

numpy.matmul(projection_matrix, view_matrix)

Then, it was able to create the MVP matrix correctly.