[PyOpenGL-Users] Drawing a texture into a pyglet window
Brought to you by:
mcfletch
|
From: Brice T. <bps...@gm...> - 2012-01-11 13:34:30
|
Good Afternoon,
I am trying to draw a texture using pyopengl into a pyglet window, but I
have not managed to do so for the moment. The reason for using the pyglet
window is that it allows me not to have to enter into an infinite loop and
it does not block my program. And I am using pyopengl because it is able to
deal with floating point numpy array. Below is the code I tried and but
could not make it to work. Any advices or suggestions will be greatly
appreciated.
Regards
Brice
import OpenGL.GL as gl
import OpenGL.GLUT as glut
import pyglet
import numpy
import ctypes
from PIL import Image
def drawTexture( x , y , w , h ):
gl.glBegin( gl.GL_QUADS )
gl.glTexCoord2f( 0, 0 ), gl.glVertex2f( x, y )
gl.glTexCoord2f( 0, 1 ), gl.glVertex2f( x, y + h )
gl.glTexCoord2f( 1, 1 ), gl.glVertex2f( x + w, y + h )
gl.glTexCoord2f( 1, 0 ), gl.glVertex2f( x + w, y )
gl.glEnd()
class DisplayImage:
def __init__(self , width , height):
self._OpenWindow()
def _OpenWindow(self):
# Find out on which platform it is used. Might not be needed!
platform = pyglet.window.get_platform()
# List all the screens attached to the computer.
display = platform.get_default_display()
screens = display.get_screens()
template = pyglet.gl.Config(alpha_size=8,double_buffer = True,
depth_size=8)
config = screens[1].get_best_config(template)
self.window = pyglet.window.Window(fullscreen=True, screen =
screens[1],config=config)
def DrawTexture_image(self, image):
# Load texture
self.texture_id = gl.glGenTextures( 1 )
gl.glBindTexture( gl.GL_TEXTURE_2D, self.texture_id )
gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
gl.GL_LINEAR )
gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
gl.GL_LINEAR )
gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S,
gl.GL_CLAMP_TO_EDGE )
gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T,
gl.GL_CLAMP_TO_EDGE )
gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_LUMINANCE,
image.shape[1], image.shape[0], 0,
gl.GL_LUMINANCE, gl.GL_FLOAT, image )
gl.glTexEnvf( gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE,
gl.GL_MODULATE )
gl.glClearColor( 1, 1, 1, 1 )
gl.glClear( gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT )
x, y, w, h = gl.glGetIntegerv( gl.GL_VIEWPORT )
gl.glEnable( gl.GL_BLEND )
gl.glBlendFunc ( gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA )
gl.glColor(0,0,0,1)
gl.glEnable( gl.GL_TEXTURE_2D )
gl.glActiveTexture( gl.GL_TEXTURE0 )
gl.glBindTexture( gl.GL_TEXTURE_2D, self.texture_id )
drawTexture(x,y,w,h)
self.window.dispatch_event()
self.window.flip()
if __name__ == '__main__':
DisIm = DisplayImage(1000,1000)
raw_input("Is it initialising?")
def func4( x, y ):
return numpy.ceil( x ) + numpy.ceil( y )
x = numpy.linspace( -3.0, 3.0, 800 )
y = numpy.linspace( -3.0, 3.0, 800 )
Z = func4( *numpy.meshgrid( x, y ) )
Z = ( Z - Z.min() ) / ( Z.max() - Z.min() )
DisIm.DrawTexture_image(Z)
raw_input("Does it display image?")
DisIm.window.close()
|