wxGLCanvas
コンテナ内の単純な2Dテクスチャ長方形として単一の画像を表示するコードを作成しようとしています。
これが私がこれまでに得たものの実行可能な例です:
import wx
from wxPython.glcanvas import wxGLCanvas
from OpenGL.GLU import *
from OpenGL.GL import *
import numpy as np
from scipy.misc import ascent
def run():
imarray = np.float32(ascent())
imarray = np.repeat(imarray[..., np.newaxis], 3, axis=2)
app = wx.PySimpleApp()
frame = wx.Frame(None, title='Simple 2D texture')
canvas = myGLCanvas(frame, imarray)
frame.Show(True)
app.MainLoop()
class myGLCanvas(wxGLCanvas):
def __init__(self, parent, image):
attribList = [wx.glcanvas.WX_GL_DOUBLEBUFFER]
wxGLCanvas.__init__(self, parent, -1, attribList=attribList)
wx.EVT_PAINT(self, self.OnPaint)
self.image = np.float32(image)
self.InitProjection()
self.InitTexture()
pass
def OnPaint(self, event):
"""Called whenever the window gets resized, uncovered etc."""
self.SetCurrent()
dc = wx.PaintDC(self)
self.OnDraw()
self.SwapBuffers()
pass
def InitProjection(self):
"""Enable the depth buffer and initialize the viewport and
projection matrix"""
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
width = self.image.shape[1]
height = self.image.shape[0]
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, width, height, 0, -1, 1)
pass
def InitTexture(self):
"""Initializes the texture from a numpy array"""
self.texture = glGenTextures(1)
glEnable(GL_TEXTURE_2D)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glBindTexture(GL_TEXTURE_2D, self.texture)
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_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
glTexImage2D(GL_TEXTURE_2D, 0,
GL_RGB,
self.image.shape[1], self.image.shape[0], 0,
GL_RGB,
GL_FLOAT,
self.image)
glDisable(GL_TEXTURE_2D)
pass
def OnDraw(self):
"""Draw a textured rectangle slightly smaller than the viewport"""
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glClearColor(0., 0., 0., 0.)
glClearDepth(1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glBindTexture(GL_TEXTURE_2D, self.texture)
glEnable(GL_TEXTURE_2D)
# draw a textured quad, shrink it a bit so the edge is clear
glBegin(GL_QUADS)
glTexCoord2f(0., 0.)
glVertex3f(-0.9, -0.9, 0.)
glTexCoord2f(1., 0.)
glVertex3f(0.9, -0.9, 0.)
glTexCoord2f(1., 1.)
glVertex3f(0.9, 0.9, 0.)
glTexCoord2f(0., 1.)
glVertex3f(-0.9, 0.9, 0.)
glEnd()
glDisable(GL_TEXTURE_2D)
pass
if __name__ == '__main__':
run()
これにより、長方形は正常に描画されますが、テクスチャは失敗します。表示されるのは白い長方形だけです。私が間違っていることについて何か考えはありますか?