我是OpenGL的新手,我想让它在窗口背景中显示我从相机中获得的帧。因此我创建了一个四边形,将框架转换为纹理,我试图将其设置为我的四边形纹理,但我似乎无法找到正确的方法。我尝试过像教程5中的opengl-tutorial.org所描述的那样。我正在使用Python => PyOpenGL和来自https://github.com/Jerdak/opengl_tutorials_python/的python脚本起作用。 但是当我尝试将它与来自https://rdmilligan.wordpress.com/2016/04/15/display-video-using-opengl/的脚本相结合时,他将相机框架转换为纹理,我无法使其工作。 这是我的代码,目前它只是在彩色四边形前面显示一个彩色立方体,但四边形应该有一个纹理:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glUseProgram(program_id)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
...
mvp = ProjectionMatrix * ViewMatrix * ModelMatrix
glUniformMatrix4fv(cube_matrix_id, 1, GL_FALSE, mvp.data)
# 1rst attribute buffer : vertices
glEnableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, cube_vertex_buffer)
glVertexAttribPointer(
0, # attribute 0. No particular reason for 0, but must match the layout in the shader.
3, # len(vertex_data)
GL_FLOAT, # type
GL_FALSE, # ormalized?
0, # stride
null # array buffer offset (c_type == void*)
)
# 2nd attribute buffer : colors
glEnableVertexAttribArray(1)
glBindBuffer(GL_ARRAY_BUFFER, cube_color_buffer)
glVertexAttribPointer(
1, # attribute 0. No particular reason for 0, but must match the layout in the shader.
3, # len(vertex_data)
GL_FLOAT, # type
GL_FALSE, # normalized?
0, # stride
null # array buffer offset (c_type == void*)
)
# Draw the triangles !
glDrawArrays(GL_TRIANGLES, 0, 12 * 3) # 3 indices starting at 0 -> 1 triangle
# Not strictly necessary because we only have
glDisableVertexAttribArray(0)
glDisableVertexAttribArray(1)
# ----------------------------------------------------------------------------------------------------------------
# Draw Background
array_type = GLfloat * len(background_vertex_data)
glBindBuffer(GL_ARRAY_BUFFER, background_vertex_buffer)
glBufferData(GL_ARRAY_BUFFER, len(background_vertex_data) * 4, array_type(*background_vertex_data), GL_STATIC_DRAW)
array_type = GLfloat * len(background_color_data)
glBindBuffer(GL_ARRAY_BUFFER, background_color_buffer)
glBufferData(GL_ARRAY_BUFFER, len(background_color_data) * 4, array_type(*background_color_data), GL_STATIC_DRAW)
ViewMatrix = np.identity(4)
ModelMatrix = np.identity(4)
mvp = ProjectionMatrix * ViewMatrix * ModelMatrix
glUniformMatrix4fv(background_matrix_id, 1, GL_FALSE, mvp.data)
# Turning a Videostream in a Texture --------------------------
# convert image to OpenGL texture format
tx_image = cv2.flip(left_frame, 0)
tx_image = Image.fromarray(tx_image)
ix = tx_image.size[0]
iy = tx_image.size[1]
tx_image = tx_image.tobytes('raw', 'RGBX', 0, -1) # BGRX
# tx_image = np.asarray(tx_image)
# create texture
texture_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture_id)
# glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
# Filtering and mipmapping of the model
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
# glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, tx_image)
# glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ix, iy, 0, GL_RGB, GL_UNSIGNED_BYTE, tx_image)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ix, iy, 0, GL_RGB, GL_UNSIGNED_BYTE, tx_image)
# Generate mipmaps
glGenerateMipmap(GL_TEXTURE_2D)
# glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_BGRA, GL_UNSIGNED_BYTE, tx_image)
# ----------------------------------------------------------
# 1rst attribute buffer : vertices
glEnableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, background_vertex_buffer)
glVertexAttribPointer(
0, # attribute 0. No particular reason for 0, but must match the layout in the shader.
3, # len(vertex_data)
GL_FLOAT, # type
GL_FALSE, # ormalized?
0, # stride
null # array buffer offset (c_type == void*)
)
# 2nd attribute buffer : colors
glEnableVertexAttribArray(1)
glBindBuffer(GL_ARRAY_BUFFER, background_color_buffer)
glVertexAttribPointer(
1, # attribute 0. No particular reason for 0, but must match the layout in the shader.
2, # len(vertex_data)
GL_FLOAT, # type
GL_FALSE, # normalized?
0, # stride
null # array buffer offset (c_type == void*)
)
# Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 12 * 3) # 3 indices starting at 0 -> 1 triangle
# Not strictly necessary because we only have
glDisableVertexAttribArray(0)
glDisableVertexAttribArray(1)
# Swap front and back buffers
glfw.swap_buffers(window)
# Poll for and process events
glfw.poll_events()
我还是个新秀,我很欣赏任何建议 构建代码也是如此。