在3d空间的OpenGL图象显示

时间:2015-01-23 07:57:38

标签: python opengl texture-mapping

我想用pyOpengl和pygame在3d空间的矩形上显示图像。

我不介意图像暂时中断。 这是我的代码,其中包含我所知道的几个用于图像显示的opengl函数:

import pygame, OpenGL, math, numpy
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from PIL import Image #have Pillow instead of PIL

img = Image.open('myPixelArt.bmp')
img_data = numpy.array(list(img.getdata()), numpy.int8)
im = glGenTextures(1,img)
glPixelStorei(GL_UNPACK_ALIGNMENT,4)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 5, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, img.size[0], img.size[1], GL_RGB, GL_UNSIGNED_BYTE, img_data );
def wall(image): #I would like the image on this wall
    glColor((1,0,0))
    glBindTexture(GL_TEXTURE_2D,image) 
    glBegin(GL_QUADS)
    glTexCoord2f(0,0)
    glVertex3f(-4,-4,-8)
    glTexCoord2f(1,0)
    glVertex3f(-4,4,-8)
    glTexCoord2f(1,1)
    glVertex3f(4,4,-8)
    glTexCoord2f(0,1)
    glVertex3f(4,-4,-8)
    glEnd()

def main():
    pygame.init()
    display = (600,600)
    screen = pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
main()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    glLoadIdentity()
    gluPerspective(45, 1, 0.05, 100)
    glEnable(GL_TEXTURE_2D)

    glTranslatef(0,0,-5)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    wall(im)

    pygame.display.flip()
    pygame.time.wait(50)

但是,如果我打电话给墙(img),我会得到

File "C:\Users\Matt\Desktop\MattPython\Stackoverflow.py", line 19, in wall
    glBindTexture(GL_TEXTURE_2D,image)
ArgumentError: argument 2: <type 'exceptions.TypeError'>: wrong type

1 个答案:

答案 0 :(得分:0)

我开始工作了,修了几件事:

1)需要在pygame.init()之前致电pygame.display.set_modemain()glGenTextures)。

2)使用pygame.image.load代替Image.open

3)使用pygame.image.tostring(img, "RGB", 1)代替numpy.array(list(img.getdata()), numpy.int8)

4)需要在glBindTexture

之前致电glTexParameteri

这是我的工作范例:

import pygame, OpenGL
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

def main():
    pygame.init()
    pygame.display.set_mode((600,600), DOUBLEBUF|OPENGL)
main()

img = pygame.image.load('myPixelArt.bmp')
textureData = pygame.image.tostring(img, "RGB", 1)
width = img.get_width()
height = img.get_height()

im = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, im)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData)
glEnable(GL_TEXTURE_2D)

def wall(image): 
    glBegin(GL_QUADS)
    glTexCoord2f(0,0)
    glVertex3f(-4,-4,-16)
    glTexCoord2f(0,1)
    glVertex3f(-4,4,-16)
    glTexCoord2f(1,1)
    glVertex3f(4,4,-8)
    glTexCoord2f(1,0)
    glVertex3f(4,-4,-8)
    glEnd()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    glLoadIdentity()
    gluPerspective(45, 1, 0.05, 100)
    glTranslatef(0,0,-5)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    wall(im)

    pygame.display.flip()
    pygame.time.wait(50)