Я новичок в OpenGL и хочу, чтобы он отображал кадры, которые я получаю от моей камеры, на заднем плане окна. Поэтому я создал квад, преобразовал фрейм в текстуру, и я пытаюсь установить его как текстуру для своего квадратора, но я не могу найти правильный путь. Я попытался сделать то же самое, как описано в opengl-tutorial.org в учебном пособии 5. Я использую Python => PyOpenGL, и скрипты python из https://github.com/Jerdak/opengl_tutorials_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()
Я все еще большой новичок, и я был бы признателен за любые советы по
структурирование кода тоже.