我尝试在立方体上获取视频纹理。我看到了纹理,但是图像变形了:
我使用了2个在网上找到的教程,但似乎我错误地将它们合并了。我在其他帖子上看到它可能来自视频格式,但事实并非如此。
以下是我使用的代码:
window6.py
from pyglet.gl import *
from Cube import Cube
class MyWindow(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_minimum_size(400, 300)
glClearColor(0.2, 0.3, 0.2, 1.0)
glEnable(GL_DEPTH_TEST)
self.cube = Cube()
def on_draw(self):
self.clear()
# glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glDrawElements(GL_TRIANGLES, len(self.cube.indices), GL_UNSIGNED_INT, None)
def on_resize(self, width, height):
glViewport(0, 0, width, height)
def update(self, dt):
self.cube.rotate()
if __name__ == "__main__":
window = MyWindow(1280, 720, "My Pyglet Window", resizable=True)
pyglet.clock.schedule_interval(window.update, 1/25.0)
pyglet.app.run()
cube.py
from pyglet.gl import *
import ctypes
import pyrr
import time
import cv2
from PIL import Image
rightStream = cv2.VideoCapture("../res/test2.mp4")
class Cube:
def __init__(self):
self.cube = [-0.5, -0.5, 0.5, 0.0, 0.0,
0.5, -0.5, 0.5, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 1.0,
-0.5, 0.5, 0.5, 0.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0,
0.5, 0.5, -0.5, 1.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 1.0,
0.5, -0.5, -0.5, 0.0, 0.0,
0.5, 0.5, -0.5, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 1.0,
0.5, -0.5, 0.5, 0.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 0.0,
-0.5, -0.5, -0.5, 1.0, 0.0,
-0.5, -0.5, 0.5, 1.0, 1.0,
-0.5, 0.5, 0.5, 0.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0,
0.5, -0.5, 0.5, 1.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 1.0,
0.5, 0.5, -0.5, 0.0, 0.0,
-0.5, 0.5, -0.5, 1.0, 0.0,
-0.5, 0.5, 0.5, 1.0, 1.0,
0.5, 0.5, 0.5, 0.0, 1.0]
self.indices = [0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4,
8, 9, 10, 10, 11, 8,
12, 13, 14, 14, 15, 12,
16, 17, 18, 18, 19, 16,
20, 21, 22, 22, 23, 20]
self.vertex_shader_source = b"""
#version 330
in layout(location = 0) vec3 position;
in layout(location = 1) vec2 textureCoords;
uniform mat4 rotate;
out vec2 textures;
void main()
{
gl_Position = rotate * vec4(position, 1.0f);
textures = textureCoords;
}
"""
self.fragment_shader_source = b"""
#version 330
in vec2 textures;
uniform sampler2D sampTexture;
out vec4 outColor;
void main()
{
outColor = texture(sampTexture, textures);
}
"""
vertex_buff = ctypes.create_string_buffer(self.vertex_shader_source)
c_vertex = ctypes.cast(ctypes.pointer(ctypes.pointer(vertex_buff)), ctypes.POINTER(ctypes.POINTER(GLchar)))
vertex_shader = glCreateShader(GL_VERTEX_SHADER)
glShaderSource(vertex_shader, 1, c_vertex, None)
glCompileShader(vertex_shader)
fragment_buff = ctypes.create_string_buffer(self.fragment_shader_source)
c_fragment = ctypes.cast(ctypes.pointer(ctypes.pointer(fragment_buff)), ctypes.POINTER(ctypes.POINTER(GLchar)))
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(fragment_shader, 1, c_fragment, None)
glCompileShader(fragment_shader)
shader = glCreateProgram()
glAttachShader(shader, vertex_shader)
glAttachShader(shader, fragment_shader)
glLinkProgram(shader)
glUseProgram(shader)
vbo = GLuint(0)
glGenBuffers(1, vbo)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, len(self.cube) * 4, (GLfloat * len(self.cube))(*self.cube), GL_STATIC_DRAW)
ebo = GLuint(0)
glGenBuffers(1, ebo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, len(self.indices)*4, (GLuint * len(self.indices))(*self.indices), GL_STATIC_DRAW)
# positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)
# textures
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(12))
glEnableVertexAttribArray(1)
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, GL_REPLACE)
texture = GLuint(0)
glGenTextures(1, texture)
glBindTexture(GL_TEXTURE_2D, texture)
# set the texture wrapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
# set the texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
# crate = pyglet.image.load('../res/nat.jpg')
# image_data: object = crate.get_data('RGB', crate.pitch)
ret, frame = rightStream.read()
tx_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
#cv2.imshow("Right Frame", tx_image)
tx_image = Image.fromarray(tx_image)
ix = tx_image.size[0]
iy = tx_image.size[1]
tx_image = tx_image.tobytes('raw', 'BGRX', 0, -1)
print(ix,iy)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ix, iy, 0, GL_RGB, GL_UNSIGNED_BYTE, tx_image)
self.rotate_loc = glGetUniformLocation(shader, b'rotate')
self.rot_x = pyrr.Matrix44.identity()
self.rot_y = pyrr.Matrix44.identity()
def rotate(self):
ct = time.clock()
ret, frame = rightStream.read()
tx_image = Image.fromarray(frame)
ix = tx_image.size[0]
iy = tx_image.size[1]
tx_image = tx_image.tobytes('raw', 'BGRX', 0, -1)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ix, iy, 0, GL_BGR, GL_UNSIGNED_BYTE, tx_image)
self.rot_x = pyrr.Matrix44.from_x_rotation(ct/10)
self.rot_y = pyrr.Matrix44.from_y_rotation(ct/10)
rotate = (self.rot_x * self.rot_y).flatten()
c_rotate = (GLfloat * len(rotate))(*rotate)
glUniformMatrix4fv(self.rotate_loc, 1, GL_FALSE, c_rotate)
谢谢