[英]OpenGL image display in 3d space
我想使用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)
但是,如果我改用wall(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)需要在glGenTextures
之前調用pygame.init()
和pygame.display.set_mode
(這是main()
)。
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.