我正在将python3.6.7与opengl 3.2一起使用。
我使用glProgramUniformMatrix3fv(program, location, count, transpose, value)
功能。但是,当我在其中输入一些数据时,它报告value
应该是12个字节,但是我认为应该是3 * 3 * 4 = 36bytes。
错误如流
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个字节,但我认为应该是4 * 4 * 4 = 64个字节。
看来它收到的是vec {3,4}而不是mat {3,4}。
这是python包装器的错误吗?
这是我的测试代码。
#!/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()