2017-06-06 75 views
-1

緊隨this轉換反饋教程,但在Python中。給定一個簡單的變換反饋的頂點着色器,簡單的Python + OpenGL轉換反饋

#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

該解決方案很簡單,並且由於動態打字而容易錯失。這條線在這裏

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))