首页 > 解决方案 > python中的Opengl和glut呈现不正确的结果,有什么建议吗?

问题描述

参考阅读源码所在的OpenGL Superbible的SMB格式文件,在一些帮助下创建了一个程序。现在我将结果与我实际得到的结果进行比较。

我得到了什么: 在此处输入图像描述

结果应该是什么样子: 在此处输入图像描述

关于为什么输出不正确的任何建议?在这一点上我很难过。谢谢。

标签: pythonopenglglut

解决方案


圆环被投影的近平面剪裁。

改变投影矩阵:

proj_matrix = m3dPerspective(m3dDegToRad(60.0), float(self.width) / float(self.height), 0.1, 100.0);

和视图矩阵:

mv_matrix = (GLfloat * 16)(*identityMatrix)
m3dRotationMatrix44(mv_matrix, currentTime * m3dDegToRad(45.0), 1.0, 0.0, 0.0)
m3dTranslateMatrix44(mv_matrix, 0.0, 0.0, -3.0)

如果要连接不同的矩阵,则必须实现矩阵“乘法”:

def m3dMultiply(A, B):
    C = (GLfloat * 16)(*identityMatrix)
    for k in range(0, 4):
        for j in range(0, 4):
            C[k*4+j] = A[0*4+j] * B[k*4+0] + A[1*4+j] * B[k*4+1] + \
                       A[2*4+j] * B[k*4+2] + A[3*4+j] * B[k*4+3]
    return C

例如连接一个平移矩阵和围绕 x 和 y 轴的旋转矩阵:

T = (GLfloat * 16)(*identityMatrix)
m3dTranslateMatrix44(T, 0, 0, -4)

RX = (GLfloat * 16)(*identityMatrix)
m3dRotationMatrix44(RX, currentTime * m3dDegToRad(17.0), 1.0, 0.0, 0.0)

RY = (GLfloat * 16)(*identityMatrix)
m3dRotationMatrix44(RY, currentTime * m3dDegToRad(13.0), 0.0, 1.0, 0.0)

mv_matrix = m3dMultiply(T, m3dMultiply(RY, RX))


请注意,如果您想使用numpy.matmul而不是m3dMultiply,那么您必须.reshape将 16 元素数组转换为 2 维 4x4 数组。例如:

R = np.matmul(np.array(RX).reshape(4,4), np.array(RY).reshape(4,4))
mv_matrix = np.matmul(R, np.array(T).reshape(4,4))

numpy.matrix-*运算符:

mv_matrix = np.matrix(RX).reshape(4,4) * np.matrix(RY).reshape(4,4) * np.matrix(T).reshape(4,4)

另请参阅Python:如何在 OpenGL Superbible 示例中让立方体旋转和移动


推荐阅读