PyOpenGL, wxPython weird behaviour

Adeola Bannis theCodeMaiden at gmail.com
Thu Jan 3 18:50:08 CET 2008


Hi everyone,

I'm doing a project using wxPython and pyopengl, and I seem to have a
problem rendering textures. This is code that worked before my hard
drive had a meltdown, but not since I re-installed everything.

I've determined the problem is in the OpenGL part of my program. I do
some calculations to generate a 2D numpy array that holds the image
data, and pylab.imshow() shows me the image as it is meant to be. I
used the same algorithm in Octave and MATLAB, and all are giving me
the right picture.

However, using pyOpenGL and the numpyhandler functions (http://cours-
info.iut-bm.univ-fcomte.fr/docs/python/OpenGL/
OpenGL.arrays.numpymodule.NumpyHandler-class.html) doesn't seem to
work. I get a garbled screen pocked with black pixels. I am including
my openGL code below. What am I doing wrong?

And yes, I did make the dtype of my array 'float32'.

-------code snippets------

import wx
from wx.glcanvas import GLCanvas

from OpenGL.GLU import *
from OpenGL.GL import *
from OpenGL.arrays.numpymodule import NumpyHandler


PC = 1
RI = 0

class myGLCanvas(GLCanvas):
	def __init__(self, parent):
		GLCanvas.__init__(self, parent,-1)
		wx.EVT_PAINT(self, self.OnPaint)
		self.init = 0
		self.mode = -1
		# making a texture for the range image
		self.texture = glGenTextures(1)
		# making a spot for the point cloud points
		self.cloud = None
		return

	def OnPaint(self,event):
		dc = wx.PaintDC(self)
		self.SetCurrent()
		if not self.init:
			self.InitGL()
			self.init = 1
		self.OnDraw()
		return

	def OnDraw(self):
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
		if self.mode == RI:
			self.drawRange()
		elif self.mode == PC:
			self.drawCloud()


	def InitGL(self):
		glClearColor(0.0, 0.0, 0.0, 0.0);
		glClearDepth(1.0)
		glEnable(GL_DEPTH_TEST)
		glDepthFunc(GL_LEQUAL)
		glClear(GL_COLOR_BUFFER_BIT)

		glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
		glPixelStorei(GL_PACK_ALIGNMENT, 1)

		#NTSC colour scales...
                glPixelTransferf(GL_RED_SCALE, 0.299);
		glPixelTransferf(GL_GREEN_SCALE, 0.587);
		glPixelTransferf(GL_BLUE_SCALE, 0.114);

		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()
		glOrtho(0.0,1.0,0,1.0,-1.0,1.0)
		glMatrixMode(GL_MODELVIEW)
		glLoadIdentity()

		return

	def rangeImage(self, image):

		glBindTexture(GL_TEXTURE_2D, self.texture)
		glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE )


		glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
						GL_LINEAR)
		glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
		glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
		glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

		# flatten it into a list so the OpenGL calls work
		n = NumpyHandler()
		fI = image.flatten()
		flatImage = n.dataPointer(n.contiguous(fI))

		print n.contiguous(fI)

		gluBuild2DMipmaps(GL_TEXTURE_2D, 1, image.shape[0]+1,
image.shape[1]+1,
							GL_LUMINANCE, GL_FLOAT, flatImage)
		self.mode = RI
		self.OnDraw()


	def drawRange(self):
		''' Controls the actual drawing of the range image'''

		glMatrixMode(GL_MODELVIEW)
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

		glColor3f(1.0,1.0,1.0)
		glEnable(GL_TEXTURE_2D)
		glBindTexture(GL_TEXTURE_2D, self.texture)
		glBegin(GL_TRIANGLE_FAN)
		glTexCoord2d(1,1); glVertex3f(0.0, 0.0, 0.0)
		glTexCoord2d(1,0); glVertex3f(0.0, 1.0, 0.0)
		glTexCoord2d(0,0); glVertex3f(1.0, 1.0, 0.0)
		glTexCoord2d(0,1); glVertex3f(1.0, 0.0, 0.0)
		glEnd()
		self.SwapBuffers()

--------end snippet-----------



More information about the Python-list mailing list