Z维度在moderngl中消失了吗?

Z dimension disappears in moderngl?

我一直在尝试旋转 object 但在旋转时,我意识到它只是平的。奇怪的是,我可以清楚地看到 z dim 的输入在那里,只是没有被考虑在内。这是我的代码:

import moderngl
from PyQt5 import QtOpenGL, QtCore, QtGui
from PyQt5.QtCore import Qt, pyqtSignal
import numpy as np
from pyrr import matrix44

cube_verts4 = np.array([
    -1.0, 1.0, -1.0, 1.0,
    -1.0, -1.0, -1.0, 1.0,
    1.0, -1.0, -1.0, 1.0,
    1.0, 1.0, -1.0, 1.0,
    -1.0, 1.0, 1.0, 1.0,
    -1.0, -1.0, 1.0, 1.0,
    1.0, -1.0, 1.0, 1.0,
    1.0, 1.0, 1.0, 1.0,
], dtype=np.float32)

cube_ibo_idxs = np.array([
    0, 1, 2,
    2, 3, 1,
    3, 2, 6,
    6, 5, 3,
    5, 6, 7,
    7, 4, 5,
    4, 7, 1,
    1, 0, 4,
    0, 3, 5,
    5, 4, 0,
    1, 7, 6,
    6, 2, 1

], dtype=np.int32)


class OpenGLWindowWidget(QtOpenGL.QGLWidget):
    vsync = True
    remove_event = pyqtSignal(str)

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.gl_version = (4, 3)
        fmt = QtOpenGL.QGLFormat()
        # need compute shader stuff
        fmt.setVersion(self.gl_version[0], self.gl_version[1])
        fmt.setProfile(QtOpenGL.QGLFormat.CoreProfile)
        fmt.setDepthBufferSize(24)
        fmt.setDoubleBuffer(True)
        fmt.setSwapInterval(1 if self.vsync else 0)
        self.ctx = None
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.update)
        self.timer.start(16)
        self.last_mouse_pos = None

        self.rotation_x = 0
        self.rotation_y = 0

    def mousePressEvent(self, event: QtGui.QMouseEvent) -> None:
        self.last_mouse_pos = event.pos()

    def mouseMoveEvent(self, event: QtGui.QMouseEvent) -> None:
        dx = event.x() - self.last_mouse_pos.x()
        dy = event.y() - self.last_mouse_pos.y()
        if event.buttons() & Qt.LeftButton:
            self.rotation_x += dx * 0.01
            self.rotation_y += dy * 0.01

        self.last_mouse_pos = event.pos()

    @property
    def gl_version_code(self) -> int:
        return self.gl_version[0] * 100 + self.gl_version[1] * 10

    @property
    def aspect_ratio(self):
        return self.width() / self.height()

    def initializeGL(self) -> None:
        self.ctx = moderngl.create_context(
            require=self.gl_version_code)
        self.prog = self.ctx.program(
            vertex_shader='''
                               #version 330
                               in vec4 vertex;
                               in float power;
                               uniform mat4 mvp_matrix;
                               void main() {
                                   gl_Position = vec4(mvp_matrix * vertex);
                               }
                           ''',
            fragment_shader='''
                                   #version 330
                                   out vec4 color;
                                   void main() {
                                       color = vec4(0.0, 0.0, 0.0, 1.0);
                                   }
                                   ''',
        )

        self.mvp_matrix = self.prog["mvp_matrix"]

        self.vbo = self.ctx.buffer(
            cube_verts4.astype('f4').tobytes())
        self.ibo = self.ctx.buffer(
            cube_ibo_idxs.astype('i4').tobytes())
        vao_content = [
            # 4 floats are assigned to the 'in' variable named 'vertex' in the shader code
            (self.vbo, '4f', 'vertex'),
        ]
        self.vao = self.ctx.vertex_array(self.prog, vao_content,
                                         self.ibo)

    def paintGL(self):
        target_width = 2
        target_height = 2
        r_aspect_ratio = target_width / target_height
        if self.aspect_ratio > r_aspect_ratio:
            v_a = self.aspect_ratio / r_aspect_ratio
            projection = matrix44.create_orthogonal_projection_matrix(
                -v_a * target_width / 2.0,
                v_a * target_width / 2.0, -target_height / 2.0,
                target_height / 2.0, 0, 100, dtype=np.float32)
        else:
            a_v = r_aspect_ratio / self.aspect_ratio
            projection = matrix44.create_orthogonal_projection_matrix(
                -target_width / 2.0, target_width / 2.0,
                -a_v * target_height / 2.0,
                a_v * target_height / 2.0,
                0, 100, dtype=np.float32)

        rotate = matrix44.create_from_y_rotation(
            self.rotation_x) * matrix44.create_from_x_rotation(
            self.rotation_y)

        self.mvp_matrix.write((rotate * projection).astype('f4').tobytes())
        self.ctx.viewport = (0, 0, self.width(), self.height())
        self.ctx.clear(0.0, 1.0, 1.0)
        self.vao.render()
        self.ctx.finish()

这是我旋转时得到的结果。

我本以为是立方体的轮廓,但我得到的是平面的轮廓。

我什至不确定是什么导致了这种效果。我最初认为这与 vec3 的对齐有关,但我将我的顶点 + vbo 代码替换为使用 vec4s,但它仍然不起作用。我不知道我的深度如何 "removed"。我对 pyrr 不熟悉,所以也许那里的矩阵变换有些不正确?

元素索引不构成立方体。使用以下索引:

cube_ibo_idxs = np.array([
    0, 1, 2,   0, 2, 3,
    3, 2, 6,   3, 6, 7,
    7, 6, 5,   7, 5, 4,
    7, 4, 0,   7, 0, 3,
    4, 5, 1,   4, 1, 0,
    1, 5, 6,   1, 6, 2
], dtype=np.int32)

pyrr Matrix44 operations return a numpy.array.
对于数组,* 表示逐元素乘法,而 @ 表示矩阵乘法。参见 array. So you'Ve to use @ isntead of *. Alternatively you can use numpy.matmul

在正射投影中,近平面设置为 0,远平面设置为 100。

projection = matrix44.create_orthogonal_projection_matrix(
               v_a * -target_width / 2.0, v_a * target_width / 2.0,
               -target_height / 2.0, target_height / 2.0,
               0, 100, dtype=np.float32)

由于几何中心位于 (0, 0, 0),立方体网格被立方体视体积的近平面部分裁剪。要么更改近平面(例如 -100),要么在近平面和远平面之间绘制立方体。这意味着您必须沿 z 轴平移网格。由于 (view space) z 轴指向视口外(在 Right-handed 系统中),您必须在负 z 方向上平移网格(例如 -3):

rotate = matrix44.create_from_y_rotation(-self.rotation_x) @ \
         matrix44.create_from_x_rotation(-self.rotation_y) @ \
         matrix44.create_from_translation(np.array([0, 0, -3], dtype=np.float32)) 

此外,我建议启用 Depth Test. See ModernGL - Context:

self.ctx.enable(moderngl.DEPTH_TEST)

使用以下函数绘制几何图形:

def paintGL(self):
    target_width = 4
    target_height = 4
    r_aspect_ratio = target_width / target_height
    if self.aspect_ratio > r_aspect_ratio:
        v_a = self.aspect_ratio / r_aspect_ratio
        projection = matrix44.create_orthogonal_projection_matrix(
            v_a * -target_width / 2.0, v_a * target_width / 2.0,
            -target_height / 2.0, target_height / 2.0,
            0, 100, dtype=np.float32)
    else:
        a_v = r_aspect_ratio / self.aspect_ratio
        projection = matrix44.create_orthogonal_projection_matrix(
            -target_width / 2.0, target_width / 2.0,
            -a_v * target_height / 2.0, a_v * target_height / 2.0,
            0, 100, dtype=np.float32)

    rotate = matrix44.create_from_y_rotation(-self.rotation_x) @ \
                matrix44.create_from_x_rotation(-self.rotation_y) @ \
                matrix44.create_from_translation(np.array([0, 0, -3], dtype=np.float32))      

    self.mvp_matrix.write((rotate @ projection).astype('f4').tobytes())
    self.ctx.viewport = (0, 0, self.width(), self.height())
    self.ctx.clear(0.0, 1.0, 1.0)
    self.ctx.enable(moderngl.DEPTH_TEST)
    self.vao.render()
    self.ctx.finish()

如果你使用下面的vertex shader

#version 330
in vec4 vertex;
in float power;
out vec4 v_clip_pos; 
uniform mat4 mvp_matrix;
void main() {
    v_clip_pos = mvp_matrix * vertex;
    gl_Position = v_clip_pos;
}

和片段着色器

#version 330
in vec4 v_clip_pos; 
out vec4 color;
void main() {

    vec3  ndc_pos = v_clip_pos.xyz / v_clip_pos.w;
    vec3  dx      = dFdx( ndc_pos );
    vec3  dy      = dFdy( ndc_pos );

    vec3 N = normalize(cross(dx, dy));
    N *= sign(N.z);
    vec3 L = vec3(0.0, 0.0, 1.0); 
    float NdotL = dot(N, L); 

    vec3 diffuse_color = vec3(0.5) * NdotL;
    color              = vec4( diffuse_color.rgb, 1.0 );
}

然后你可以实现轻微的 3D 效果。