密切关注转换反馈的this教程,但是在python中。给出一个简单的变换反馈顶点着色器,
#version 400 core
in float inValue;
out float outValue;
void main()
{
outValue = 5*inValue+6;
}
和周围的python代码来调用它,
...
varyings = ctypes.create_string_buffer('outValue')
varyings_pp = POINTER(POINTER(c_char))(ctypes.cast(varyings, POINTER(c_char)))
gl.glTransformFeedbackVaryings(program, 1, varyings_pp, gl.GL_INTERLEAVED_ATTRIBS);
gl.glLinkProgram(program)
gl.glUseProgram(program)
vao = gl.glGenVertexArrays(1)
gl.glBindVertexArray(vao)
data = np.array([1.,2.,3.,4.,5.], dtype=np.float32)
vbo = gl.glGenBuffers(1)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo)
gl.glBufferData(gl.GL_ARRAY_BUFFER, data.nbytes, data, gl.GL_STATIC_DRAW)
inputAttrib = gl.glGetAttribLocation(program, "inValue")
gl.glEnableVertexAttribArray(inputAttrib)
gl.glVertexAttribPointer(inputAttrib, 1, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
data = np.array([-1.,-2.,-3.,-4.,-5.], dtype=np.float32)
tbo = gl.glGenBuffers(1)
gl.glBindBuffer(gl.GL_TRANSFORM_FEEDBACK_BUFFER, tbo)
gl.glBufferData(gl.GL_TRANSFORM_FEEDBACK_BUFFER, data.nbytes, data, gl.GL_STATIC_READ)
gl.glEnable(gl.GL_RASTERIZER_DISCARD)
gl.glBindBufferBase(gl.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tbo)
gl.glBeginTransformFeedback(gl.GL_POINTS)
gl.glDrawArrays(gl.GL_POINTS, 0, 3)
gl.glEndTransformFeedback()
gl.glFlush()
r_data = np.empty_like(data)
gl.glGetBufferSubData(gl.GL_TRANSFORM_FEEDBACK_BUFFER, 0, data.nbytes, r_data)
print r_data
我得到输出[6. 6. 6. -4. -5.]
。因此,似乎inValue
始终为0.但是,如果我回读GL_ARRAY_BUFFER,我会得到输入数据。我是否错误地将缓冲区绑定到顶点数组?在任何情况下都不会引发任何glError
,并且着色器没有编译错误(出于审美目的而省略了错误检查代码)。
答案 0 :(得分:0)
解决方案很简单,因动态打字而容易错过。这一行
gl.glVertexAttribPointer(inputAttrib, 1, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
应该阅读
gl.glVertexAttribPointer(inputAttrib, 1, gl.GL_FLOAT, gl.GL_FALSE, 0, ctypes.c_void_p(0))
答案 1 :(得分:0)
请使用以下代码。
r_data = (c_float * data.size)()
gl.glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, data.nbytes, byref(r_data))
r_data = np.frombuffer(r_data, dtype=np.float32)
print(r_data)