PyOpenGL glGenTextures返回0

时间:2015-01-31 02:21:13

标签: python opengl textures pyopengl

我在pygame中使用pyopengl,我想在四边形上显示图像。每次我使用glGenTextures(1,img)它都会返回0.我已经读过需要在上下文中调用此函数但我不知道如何设置它。

这是我的代码,直到使用glGenTexures:

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('myPixelArt2.bmp')
glGenTextures(1,img)

print glGenTextures(1,img)
print img

打印出来

0
<PIL.BmpImagePlugin.BmpImageFile image mode=RGB size=64x64 at 0x3A4E9B8>

如何让glGenTextures返回1? 我试过调用glGenTexures(1),尝试过jpg图像,但都没有改变任何东西。

2 个答案:

答案 0 :(得分:0)

  

我已经读过需要在上下文中调用此函数但是我   不知道如何设置一个。

但这是关键点。没有OpenGL上下文,你不能使用OepnGL。通常,有一些特定于平台的API允许您创建和管理GL上下文并将它们“连接”到窗口,以便您可以使用GL实际获得屏幕上可见的任何内容。

通常,人们不会使用这些低级别的平台特定API,而是使用一些更高级别的抽象,这些抽象内部处理所有这些特定于平台的细节。由于您使用python和PyGame,您可能希望使用PyOpenGL,它可以为您创建上下文。但OpenGL是一个复杂的主题,学习它将需要付出很多努力。

答案 1 :(得分:0)

我不知道PyOpenGL,想到创建一个上下文作为启动OpenGL引擎。您可以在此处查看如何创建上下文:

http://pyopengl.sourceforge.net/context/tutorials/shader_1.html (使用pyopengl的OpenGLContext

glGenTextures要求系统返回可用作纹理ID的整数。第一个参数是您想要的纹理ID整数的数量,它返回那么多ID。

这里有关于纹理映射的NeHe教程的pyopengl翻译:http://pyopengl.sourceforge.net/context/tutorials/nehe6.html

相关问题