简体   繁体   English

在opengl中在3d模型后面绘制背景图像

[英]Drawing a background image beind 3d model in opengl

I am tring to draw 3d model (.obj) by this script我想通过这个脚本绘制 3d 模型 (.obj)
https://github.com/yarolig/OBJFileLoader/blob/master/OBJFileLoader/objloader.py https://github.com/yarolig/OBJFileLoader/blob/master/OBJFileLoader/objloader.py
and as bacgrund I want to draw video from the webcam.but when I am drawing the image and the 3d model only the image show up.作为 bacgrund,我想从网络摄像头绘制视频。但是当我绘制图像和 3d 模型时,只显示图像。
This is my Image loder class这是我的图像加载器类

class ImageLoader:

    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 0
        self.height = 0
        self.img_data = 0

    def load(self, image):
        im = image
        tx_image = cv2.flip(im, 0)
        tx_image = Image.fromarray(tx_image)
        self.width = tx_image.size[0]
        self.height = tx_image.size[1]
        self.img_data = tx_image.tobytes('raw', 'BGRX', 0, -1)

        self.Texture = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, self.Texture)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
        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, self.width, self.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, self.img_data)

    def draw(self):
        glEnable(GL_TEXTURE_2D)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glTranslate(self.x, self.y, 0)
        glBegin(GL_QUADS)
        glVertex(0, 0, 0)
        glTexCoord2f(0, 0)
        glVertex(self.width, 0, 0)
        glTexCoord2f(0, 1)
        glVertex(self.width, self.height, 0)
        glTexCoord2f(1, 1)
        glVertex(0, self.height, 0)
        glTexCoord2f(1, 0)
        glEnd()
        glDisable(GL_TEXTURE_2D)
        glFlush()

and here the code run.在这里运行代码。

 if __name__ == '__main__':

    cap = cv2.VideoCapture(0)
    width = int(cap.get(3))
    height = int(cap.get(4))

    pygame.init()
    pygame.display.set_mode((width, height), pygame.DOUBLEBUF | pygame.OPENGL)

    box = OBJ('3dglasses/oculos.obj', ) # from OBJFileLoader import OBJ

    gluPerspective(45, (width / height), 0.1, 50.0)
    glTranslate(0.0, 0.0, -5)
    glRotate(0, 0, 0, 0)

    if True:
        glClearColor(0.7, 0, 0, 1)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluOrtho2D(0, width, height, 0)
        im_loader = ImageLoader(0, 0)
        while True:
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()

            success, image = cap.read()
            im_loader.load(image)
            im_loader.draw()

            box.render()
            pygame.display.flip()

The image and the model use different projection and model view matrices.图像和模型使用不同的投影和模型视图矩阵。 Therefore, you need to set the matrices before drawing the objects.因此,您需要在绘制对象之前设置矩阵。

The model is not drawn because it does not pass the Depth Test .模型没有被绘制,因为它没有通过深度测试 Disable the depth test when you draw the image.绘制图像时禁用深度测试。 If the depth test is disabled, nothing is written to the depth buffer.如果禁用深度测试,则不会向深度缓冲区写入任何内容。

if __name__ == '__main__':
    cap = cv2.VideoCapture(0)
    width, height = int(cap.get(3)), int(cap.get(4))
    pygame.init()
    pygame.display.set_mode((width, height), pygame.DOUBLEBUF | pygame.OPENGL)
    box = OBJ('3dglasses/oculos.obj', ) # from OBJFileLoader import OBJ
    im_loader = ImageLoader(0, 0)
    angle = 0

    glClearColor(0.7, 0, 0, 1)
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run = False

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluOrtho2D(0, width, height, 0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        glDisable(GL_DEPTH_TEST)
        success, image = cap.read()
        if success:
            im_loader.load(image)
        glColor3f(1, 1, 1)
        im_loader.draw()
        
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45, (width / height), 0.1, 50.0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glTranslate(0.0, 0.0, -5)
        glRotate(angle, 0, 1, 0)
        angle += 1
        
        glEnable(GL_DEPTH_TEST)
        box.render()

        pygame.display.flip()

    pygame.quit()
    quit()

The current texture coordinates, is associated with the vertex when glVertex is called.当前纹理坐标,在调用glVertex时与顶点相关联。 Therefore, you need to specify the texture coordinate before specifying the vertex coordinate:因此,在指定顶点坐标之前需要指定纹理坐标:

class ImageLoader:
    # [...]

    def draw(self):
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glTranslate(self.x, self.y, 0)
        
        glEnable(GL_TEXTURE_2D)  
        glBegin(GL_QUADS)
        glTexCoord2f(0, 0)
        glVertex2f(0, 0)
        glTexCoord2f(1, 0)
        glVertex2f(self.width, 0)
        glTexCoord2f(1, 1)
        glVertex2f(self.width, self.height)
        glTexCoord2f(0, 1)
        glVertex2f(0, self.height)
        glEnd()
        glDisable(GL_TEXTURE_2D)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM