opengl glProgramUniformMatrix3fv不工作除外

fivyi3re  于 2023-03-29  发布在  其他
关注(0)|答案(1)|浏览(134)

我用的是python3.6.7和opengl 3.2。
我使用glProgramUniformMatrix3fv(program, location, count, transpose, value) functoin,但是当我输入一些数据时,它报告value应该是12字节,但我认为它应该是334= 36字节。
错误为流动:

Traceback (most recent call last):
  File "opengl_program_pipline_exampled_debug.py", line 67, in <module>
    gl.glProgramUniformMatrix3fv(VP, loc_matrix,1, True, matrix)
  File "latebind.pyx", line 32, in OpenGL_accelerate.latebind.LateBind.__call__ (src/latebind.c:989)
  File "wrapper.pyx", line 299, in OpenGL_accelerate.wrapper.Wrapper.__call__ (src/wrapper.c:6294)
  File "wrapper.pyx", line 161, in OpenGL_accelerate.wrapper.PyArgCalculator.c_call (src/wrapper.c:4241)
  File "wrapper.pyx", line 128, in OpenGL_accelerate.wrapper.PyArgCalculatorElement.c_call (src/wrapper.c:3601)
  File "wrapper.pyx", line 114, in OpenGL_accelerate.wrapper.PyArgCalculatorElement.c_call (src/wrapper.c:3409)
  File "arraydatatype.pyx", line 421, in OpenGL_accelerate.arraydatatype.AsArrayTypedSizeChecked.c_call (src/arraydatatype.c:8869)
ValueError: ('Expected 12 byte array, got 36 byte array', array([ 0.98480775, -0.17364818,  0.        ,  0.17364818,  0.98480775,
        0.        ,  0.        ,  0.        ,  1.        ]), None)

我尝试glProgramUniformMatrix4fv,它报告value应该是16字节,但我认为它应该是444 = 64字节。
它似乎接收到vec{3,4}而不是mat{3,4}。
这是python wrapper的bug吗?
下面是我的测试代码:

#!/bin/python3
import glfw
import OpenGL.GL as gl
from OpenGL.GL import shaders
import numpy as np
import pyquaternion

vertex_code = '''
#version 150
in vec3 position;
uniform mat4 rotate_matrix;
void main(){
    gl_Position = vec4(position, 1.0);
}
'''python3

glfw.init()
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
window_size = (800, 800)
window = glfw.create_window(*window_size, "test", None, None)
glfw.make_context_current(window)

VS = shaders.compileShader(vertex_code, gl.GL_VERTEX_SHADER)

VP = shaders.compileProgram(VS, separable=True)

L1 = gl.GLuint()
gl.glGenProgramPipelines(1, L1)
gl.glUseProgramStages(L1, gl.GL_VERTEX_SHADER_BIT, VP)

data1 = np.float32(
    (
        (-1.0, 0.0, 0.0),
        (-0.5, 0.5, 0.0),
        (-0.5, -0.5, 0.0),
    )
)

vbo1 = gl.glGenBuffers(1)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo1)
gl.glBufferData(gl.GL_ARRAY_BUFFER, data1.nbytes, data1, gl.GL_STATIC_DRAW)

vao1 = gl.glGenVertexArrays(1)
gl.glBindVertexArray(vao1)
loc_position = gl.glGetAttribLocation(VP, "position")
gl.glEnableVertexAttribArray(loc_position)
gl.glVertexAttribPointer(loc_position, 3, gl.GL_FLOAT, True, 4*3, None)

while True:
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)

    gl.glBindProgramPipeline(L1)
    loc_matrix = gl.glGetUniformLocation(VP, "rotate_matrix")
    matrix = pyquaternion.Quaternion(axis=(0,0,1), degrees=10).rotation_matrix.ravel()
    gl.glProgramUniformMatrix3fv(VP, loc_matrix,1, True, matrix)
    gl.glBindVertexArray(vao1)
    gl.glDrawArrays(gl.GL_TRIANGLE_FAN, 0, 3)

    glfw.swap_buffers(window)
    glfw.poll_events()
5ktev3wc

5ktev3wc1#

我同样对函数接受数据的方式感到困惑,下面是我在这个函数上运行的实验结果。函数glProgramUniformMatrix3fv确实接受了一个矩阵(3x 3),但传递给函数的方式是通过指针。
比如说

matrix = np.array([(1, 0, 0), (0, 1, 0), (0, 0 ,1)], dtype=np.float32) # 3x3 identity
glProgramUniformMatrix3fv(Vertex_shader, uniform_loc, 1, GL_TRUE, *matrix)

这里Vertex_shader是着色器程序,uniform_loc是传递给着色器的矩阵uniform的位置。
如果改为执行此操作:

glProgramUniformMatrix3fv(Vertex_shader, uniform_loc, 1, GL_TRUE, matrix)

你会得到错误“ValueError:('应为12字节数组,得到36字节数组'...”
这与所有其他glProgramUniformMatrix*fv函数的情况类似。
免责声明:我还在学习,只是分享的东西,为我工作.干杯!

相关问题