[英]OpenGL image display in 3d space
I would like to display an image onto a rectangle in 3d space with pyOpengl and pygame. 我想使用pyOpengl和pygame在3d空间中的矩形上显示图像。
I don't mind if the image is disorted for now. 我不介意目前图像是否失真。 Here is my code with the few opengl functions that I know of for image display:
这是我的代码,其中包含一些我知道的用于图像显示的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)
However, if i call wall(img) instead, I get 但是,如果我改用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
I got it working, fixed a few things: 我成功了,修复了一些问题:
1) needed to call pygame.init()
and pygame.display.set_mode
(which is main()
) before glGenTextures
. 1)需要在
glGenTextures
之前调用pygame.init()
和pygame.display.set_mode
(这是main()
)。
2) used pygame.image.load
instead of Image.open
2)使用
pygame.image.load
代替Image.open
3) used pygame.image.tostring(img, "RGB", 1)
instead of numpy.array(list(img.getdata()), numpy.int8)
3)使用
pygame.image.tostring(img, "RGB", 1)
代替numpy.array(list(img.getdata()), numpy.int8)
4) need to call glBindTexture
before glTexParameteri
4)需要在
glBindTexture
之前调用glTexParameteri
This is my working example: 这是我的工作示例:
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.