gpt4 book ai didi

python - 使用 PyOpenGL 和 PyQt5 转换对象时出错

转载 作者:行者123 更新时间:2023-12-01 18:57:06 36 4
gpt4 key购买 nike

我正在尝试使用矢量着色器转换对象。但 glUniformMatrixfv 在执行时会引发错误。

vertex_shader = shaders.compileShader(
"""
#version 330
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 color;
uniform mat4 movement;
out vec4 f_color;
void main()
{
gl_Position = movement * vec4(pos, 1.0);
f_color = vec4(color, 1.0);
}
"""
, GL_VERTEX_SHADER)


fragment_shader = shaders.compileShader(
"""
#version 330
in vec4 f_color;
out vec4 gl_FragColor;
void main()
{
gl_FragColor = f_color;
}
"""
, GL_FRAGMENT_SHADER
)

使用这些着色器的程序代码是:

def draw_object(self):
self.vao = glGenVertexArrays(1)
glBindVertexArray(self.vao)

self.vbo_buffer = np.array([
-0.4, 0.2, 0.0, 0.0, 1.0, 0.0,
-0.8, -0.2, 0.0, 1.0, 1.0, 0.0,
0.0, -0.2, 0.0, 0.0, 1.0, 1.0,
0.2, -0.4, 0.0, 1.0, 0.0, 0.0,
0.8, -0.4, 0.0, 0.0, 0.0, 1.0,
0.8, 0.4, 0.0, 0.0, 1.0, 1.0,
0.2, -0.4, 0.0, 1.0, 0.0, 0.0,
0.8, 0.4, 0.0, 0.0, 0.0, 1.0,
0.2, 0.4, 0.0, 0.0, 1.0, 0.0
], dtype=np.float32)

self.vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
glBufferData(GL_ARRAY_BUFFER, self.vbo_buffer.nbytes, self.vbo_buffer, GL_DYNAMIC_DRAW)

self.vertex_position = glGetAttribLocation(self.shader, bytestr('pos'))
glVertexAttribPointer(self.vertex_position, 3, GL_FLOAT, GL_FALSE, 24, None)
glEnableVertexAttribArray(self.vertex_position)

self.vertex_color = glGetAttribLocation(self.shader, bytestr('color'))
glVertexAttribPointer(self.vertex_color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(self.vertex_color)

self.movement = glGetUniformLocation(self.shader, 'movement')
glUniformMatrix4fv(self.movement, 1, GL_FALSE, glm.value_ptr(self.svp_matrix))

glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0)


def calculate_mvp(self):
self.projection_matrix = glm.perspective(glm.radians(self.fieldOfView), self.glWinWidth / self.glWinHeight,
self.nearPlane, self.farPlane)
self.view_matrix = glm.rotate(glm.mat4(1.0), float(self.xRot), glm.vec3(1.0, 0.0, 0.0)) * \
glm.rotate(glm.mat4(1.0), float(self.yRot), glm.vec3(0.0, 1.0, 0.0)) * \
glm.rotate(glm.mat4(1.0), float(self.zRot), glm.vec3(0.0, 0.0, 1.0)) * \
glm.translate(glm.mat4(1.0), glm.vec3(self.xTranslate, self.yTranslate, self.zTranslate))
self.scale_matrix = glm.scale(glm.mat4(1.0), glm.vec3(self.scale_factor))

self.svp_matrix = self.projection_matrix * self.view_matrix * self.scale_matrix


def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glUseProgram(self.shader)
try:
glBindVertexArray(self.vao)
glDrawArrays(GL_TRIANGLES, 0, 9)
finally:
glBindVertexArray(0)
glUseProgram(0)
glFlush()

我得到的错误是:

File "C:\Program Files\Python37\lib\site-packages\OpenGL\platform\baseplatform.py", line 415, in __call__

return self( *args, **named )

File "src\errorchecker.pyx", line 58, in OpenGL_accelerate.errorchecker._ErrorChecker.glCheckError OpenGL.error.GLError: GLError(

   err = 1282,
description = b'invalid operation',
baseOperation = glUniformMatrix4fv,
pyArgs = (
0,
1,
GL_FALSE,
<OpenGL.GLU.tess.LP_c_float object at 0x0000013A37B1B4C8>,
),
cArgs = (
0,
1,
GL_FALSE,
<OpenGL.GLU.tess.LP_c_float object at 0x0000013A37B1B4C8>,
),
cArguments = (
0,
1,
GL_FALSE,
<OpenGL.GLU.tess.LP_c_float object at 0x0000013A37B1B4C8>,
)

)

如果您知道如何解决此问题,请在下面分享。谢谢

最佳答案

glUniformMatrix4fv设置当前安装程序的默认统一 block 中统一变量的值。
这意味着该程序必须通过 glUseProgram 安装,然后才能设置变量。如果没有当前程序对象,则会生成 GL_INVALID_OPERATION 错误。

glUniformMatrix4fv之前安装程序:

self.movement = glGetUniformLocation(self.shader, 'movement')

glUseProgram(self.shader)
glUniformMatrix4fv(self.movement, 1, GL_FALSE, glm.value_ptr(self.svp_matrix))

关于python - 使用 PyOpenGL 和 PyQt5 转换对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59857043/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com