翻译对象未按预期工作

发布于 2025-01-09 02:09:43 字数 4329 浏览 0 评论 0原文

当我尝试创建一个可以使用平移矩阵在 z 轴上移动的立方体时,立方体的一半不会渲染,当我尝试更改 z 值时,FOV 似乎会增加。

是因为翻译矩阵的原因,还是这里发生了其他错误,我该如何修复它?

完整代码:

import glfw
import numpy
import pyrr
from OpenGL.GL import *
from OpenGL.GL.shaders import *
width, height = 500, 500

rot_x_var = 0
rot_y_var = 0
z = 0

def draw():
    global shader, rot_x_var, rot_y_var, z
    cube = [-0.5, -0.5,  0.5, 1.0, 0.0, 0.0, #red
            0.5, -0.5,  0.5, 0.0, 1.0, 0.0, #green
            0.5,  0.5,  0.5, 0.0, 0.0, 1.0, #blue
            -0.5,  0.5,  0.5, 1.0, 1.0, 0.0, #yellow
            -0.5, -0.5, -0.5, 0.0, 1.0, 1.0, #cyan
            0.5, -0.5, -0.5, 1.0, 0.0, 1.0, #pink
            0.5,  0.5, -0.5, 0.0, 0.5, 0.0, #half-green
            -0.5,  0.5, -0.5, 1.0, 1.0, 1.0] #white

    cube = numpy.array(cube, dtype = numpy.float32)

    indices = [0, 3, 2, 2, 0, 1,
               1, 2, 6, 6, 1, 5,
               5, 1, 0, 0, 4, 5,
               5, 4, 7, 7, 6, 5,
               2, 6, 7, 7, 3, 2,
               0, 4, 7, 7, 3, 0]


    indices = numpy.array(indices, dtype= numpy.uint32)

    vertex_shader_ = """
        #version 140
        in vec4 position;
        in vec3 color;
        uniform mat4 transform;
        out vec4 out_color;
        uniform ivec2 dim;
        uniform mat4 z;
        void main(){

            gl_Position = transform*vec4(position.xyz, dim.x/dim.y)*z;
            out_color = vec4(color.rgb, 0.9);

        };

    """

    fragment_shader_ = """
        #version 140
        in vec4 out_color;

        void main(){

            gl_FragColor = out_color;
        };

    """

    shader = compileProgram(compileShader(vertex_shader_, GL_VERTEX_SHADER),
                            compileShader(fragment_shader_, GL_FRAGMENT_SHADER))
    glUseProgram(shader)
    glLinkProgram(shader)
    dim = glGetUniformLocation(shader, "dim")
    glUniform2i(dim, int(width), int(height))

    z_matrix = glGetUniformLocation(shader, "z")
    z_trans = numpy.array([0, 0, z, 0])
    glUniformMatrix4fv(z_matrix, 1, GL_FALSE, z_trans)

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, cube.nbytes, cube, GL_STATIC_DRAW)

    EBO = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)


    position = glGetAttribLocation(shader, "position")
    glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
    glEnableVertexAttribArray(position)
    color = glGetAttribLocation(shader, "color")
    glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
    glEnableVertexAttribArray(color)


    glEnable(GL_DEPTH_TEST)
    rot_x = pyrr.Matrix44.from_x_rotation(rot_x_var)
    rot_y = pyrr.Matrix44.from_y_rotation(rot_y_var)
    transformLoc = glGetUniformLocation(shader, "transform")
    glUniformMatrix4fv(transformLoc, 1, GL_FALSE, rot_x * rot_y)






def Screen():
    glClearColor(0, 0, 0, 1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
    glDrawElements(GL_TRIANGLES, 64, GL_UNSIGNED_INT, None)
    draw()
    glViewport(0, 0, width, height)


def main():
    global width, height, rot_x_var, rot_y_var, z
    if not glfw.init():
        return
    window = glfw.create_window(500, 500, "PyOpenGL GLFW", None, None)
    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        if glfw.get_key(window, glfw.KEY_DOWN) == glfw.PRESS:
            rot_x_var += 0.01
        if glfw.get_key(window, glfw.KEY_UP) == glfw.PRESS:
            rot_x_var -= 0.01
        if glfw.get_key(window, glfw.KEY_RIGHT) == glfw.PRESS:
            rot_y_var += 0.01
        if glfw.get_key(window, glfw.KEY_LEFT) == glfw.PRESS:
            rot_y_var -= 0.01
        if glfw.get_key(window, glfw.KEY_E) == glfw.PRESS:
            z += 0.1
        if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS:
            z -= 0.1
        Screen()
        width, height = glfw.get_window_size(window)



        glfw.swap_buffers(window)

    glfw.terminate()


if __name__ == '__main__':
    main()

更新:我已经对代码进行了调整,现在立方体没有被显示,而不是立方体被变形。有什么办法可以解决这个问题吗?

When I try to create a cube that can be moved around using the z-axis using a translation matrix, half of the cube doesn't render and when I try to change the z-value, the FOV seems to increase.

Is it because of the translation matrix, or is there some other error going on here, and how would I fix it?

Full code:

import glfw
import numpy
import pyrr
from OpenGL.GL import *
from OpenGL.GL.shaders import *
width, height = 500, 500

rot_x_var = 0
rot_y_var = 0
z = 0

def draw():
    global shader, rot_x_var, rot_y_var, z
    cube = [-0.5, -0.5,  0.5, 1.0, 0.0, 0.0, #red
            0.5, -0.5,  0.5, 0.0, 1.0, 0.0, #green
            0.5,  0.5,  0.5, 0.0, 0.0, 1.0, #blue
            -0.5,  0.5,  0.5, 1.0, 1.0, 0.0, #yellow
            -0.5, -0.5, -0.5, 0.0, 1.0, 1.0, #cyan
            0.5, -0.5, -0.5, 1.0, 0.0, 1.0, #pink
            0.5,  0.5, -0.5, 0.0, 0.5, 0.0, #half-green
            -0.5,  0.5, -0.5, 1.0, 1.0, 1.0] #white

    cube = numpy.array(cube, dtype = numpy.float32)

    indices = [0, 3, 2, 2, 0, 1,
               1, 2, 6, 6, 1, 5,
               5, 1, 0, 0, 4, 5,
               5, 4, 7, 7, 6, 5,
               2, 6, 7, 7, 3, 2,
               0, 4, 7, 7, 3, 0]


    indices = numpy.array(indices, dtype= numpy.uint32)

    vertex_shader_ = """
        #version 140
        in vec4 position;
        in vec3 color;
        uniform mat4 transform;
        out vec4 out_color;
        uniform ivec2 dim;
        uniform mat4 z;
        void main(){

            gl_Position = transform*vec4(position.xyz, dim.x/dim.y)*z;
            out_color = vec4(color.rgb, 0.9);

        };

    """

    fragment_shader_ = """
        #version 140
        in vec4 out_color;

        void main(){

            gl_FragColor = out_color;
        };

    """

    shader = compileProgram(compileShader(vertex_shader_, GL_VERTEX_SHADER),
                            compileShader(fragment_shader_, GL_FRAGMENT_SHADER))
    glUseProgram(shader)
    glLinkProgram(shader)
    dim = glGetUniformLocation(shader, "dim")
    glUniform2i(dim, int(width), int(height))

    z_matrix = glGetUniformLocation(shader, "z")
    z_trans = numpy.array([0, 0, z, 0])
    glUniformMatrix4fv(z_matrix, 1, GL_FALSE, z_trans)

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, cube.nbytes, cube, GL_STATIC_DRAW)

    EBO = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)


    position = glGetAttribLocation(shader, "position")
    glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
    glEnableVertexAttribArray(position)
    color = glGetAttribLocation(shader, "color")
    glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
    glEnableVertexAttribArray(color)


    glEnable(GL_DEPTH_TEST)
    rot_x = pyrr.Matrix44.from_x_rotation(rot_x_var)
    rot_y = pyrr.Matrix44.from_y_rotation(rot_y_var)
    transformLoc = glGetUniformLocation(shader, "transform")
    glUniformMatrix4fv(transformLoc, 1, GL_FALSE, rot_x * rot_y)






def Screen():
    glClearColor(0, 0, 0, 1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
    glDrawElements(GL_TRIANGLES, 64, GL_UNSIGNED_INT, None)
    draw()
    glViewport(0, 0, width, height)


def main():
    global width, height, rot_x_var, rot_y_var, z
    if not glfw.init():
        return
    window = glfw.create_window(500, 500, "PyOpenGL GLFW", None, None)
    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        if glfw.get_key(window, glfw.KEY_DOWN) == glfw.PRESS:
            rot_x_var += 0.01
        if glfw.get_key(window, glfw.KEY_UP) == glfw.PRESS:
            rot_x_var -= 0.01
        if glfw.get_key(window, glfw.KEY_RIGHT) == glfw.PRESS:
            rot_y_var += 0.01
        if glfw.get_key(window, glfw.KEY_LEFT) == glfw.PRESS:
            rot_y_var -= 0.01
        if glfw.get_key(window, glfw.KEY_E) == glfw.PRESS:
            z += 0.1
        if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS:
            z -= 0.1
        Screen()
        width, height = glfw.get_window_size(window)



        glfw.swap_buffers(window)

    glfw.terminate()


if __name__ == '__main__':
    main()

Update: I have tweaked around with the code, and now the cube is not being shown, instead of the cube being deformed. Is there any way to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别闹i 2025-01-16 02:09:43

我已经发现了错误。正是这一行:

z_trans = numpy.array([0, 0, z, 0])

我必须将其转换为矩阵而不是数组。

z_trans = pyrr.Matrix44([1, 0, 0, 0,
                         0, 1, 0, 0,
                         0, 0, 1, z,
                         0, 0, 0, 1])

I had found the error. It was with this line:

z_trans = numpy.array([0, 0, z, 0])

I had to convert it to a matrix instead of an array.

z_trans = pyrr.Matrix44([1, 0, 0, 0,
                         0, 1, 0, 0,
                         0, 0, 1, z,
                         0, 0, 0, 1])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文