Advertisement
Guest User

Untitled

a guest
Feb 17th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.72 KB | None | 0 0
  1. from OpenGL.GL import *
  2. from OpenGL.arrays import vbo
  3. from OpenGL.GL import shaders
  4.  
  5. import numpy
  6. import wx
  7. import wx.glcanvas
  8.  
  9.  
  10. class MyCanvas(wx.glcanvas.GLCanvas):
  11.     def __init__(self, parent):
  12.         wx.glcanvas.GLCanvas.__init__(self, parent)
  13.         wx.EVT_PAINT(self, self.OnPaint)
  14.         wx.EVT_SIZE(self, lambda event: self.Refresh(0))
  15.         self.haveInited = False
  16.         self.image = None
  17.         self.context = wx.glcanvas.GLContext(self)
  18.  
  19.     def OnPaint( self, mode):
  20.         if not self.haveInited:
  21.             self.SetCurrent(self.context)
  22.             glClearColor(1, 1, 1, 1)
  23.  
  24.             self.displayTextureID = self.makeDisplayTexture()
  25.             self.colorTextureID = self.makeColorTexture()
  26.  
  27.             vertex = shaders.compileShader("""
  28.            void main() {
  29.                gl_FrontColor = gl_Color;
  30.                gl_TexCoord[0].xy = gl_MultiTexCoord0.xy;
  31.                gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  32.            }""", GL_VERTEX_SHADER)
  33.             fragment = shaders.compileShader("""
  34.            uniform sampler2D texture;
  35.            uniform sampler1D color_lut;
  36.            void main() {
  37.                vec2 uv = gl_TexCoord[0].xy;
  38.                vec4 color = texture2D(texture, uv);
  39.                gl_FragColor = texture1D(color_lut, color.a);
  40.            }""", GL_FRAGMENT_SHADER)
  41.             self.shader = shaders.compileProgram(vertex, fragment)
  42.             self.haveInited = True
  43.  
  44.         try:
  45.             glClearColor(1, 1, 1, 1)
  46.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  47.             glEnable(GL_BLEND)
  48.             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  49.             glColor(1, 1, 1, 1)
  50.            
  51.             glPushMatrix()
  52.             glLoadIdentity()
  53.             glTranslatef(-1, -1, 0)
  54.             glScalef(1/256.0, 1/256.0, 1)
  55.  
  56.             glEnable(GL_TEXTURE_1D)
  57.             glActiveTexture(GL_TEXTURE1)
  58.             glBindTexture(GL_TEXTURE_1D, self.colorTextureID)
  59.            
  60.             glEnable(GL_TEXTURE_2D)
  61.             glActiveTexture(GL_TEXTURE0)
  62.             glBindTexture(GL_TEXTURE_2D, self.displayTextureID)
  63.            
  64.             shaders.glUseProgram(self.shader)
  65.             loc = glGetUniformLocation(self.shader, 'texture')
  66.             glUniform1i(loc, 0)
  67.             loc = glGetUniformLocation(self.shader, 'color_lut')
  68.             glUniform1i(loc, 1)
  69.            
  70.             glBegin(GL_QUADS)
  71.             for x, y in [(0, 0), (1, 0), (1, 1), (0, 1)]:
  72.                 glVertex2f(x * 512, y * 512)
  73.                 glTexCoord2f(x, y)
  74.             glEnd()
  75.  
  76.             shaders.glUseProgram(0)
  77.  
  78.             glPopMatrix()
  79.             glFlush()
  80.             self.SwapBuffers()
  81.         except Exception, e:
  82.             import traceback; traceback.print_exc()
  83.             import sys; sys.exit()
  84.  
  85.  
  86.     # Make the display texture, a uniform gradient.
  87.     def makeDisplayTexture(self):
  88.         data = numpy.zeros((512, 512), dtype = numpy.uint16)
  89.         data[:] = numpy.linspace(0, 2 ** 15 - 1, 512)
  90.         textureID = glGenTextures(1)
  91.         glEnable(GL_TEXTURE_2D)
  92.         glBindTexture(GL_TEXTURE_2D, textureID)
  93.         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA32F_ARB, 512, 512, 0,
  94.                         GL_ALPHA, GL_SHORT, data)
  95.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  96.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  97.         return textureID
  98.        
  99.  
  100.     def makeColorTexture(self):
  101.         # A band that goes from dark blue to cyan to yellow to red.
  102.         colors = numpy.zeros((512, 3)).astype(numpy.float32)
  103.         # Red channel
  104.         colors[180:341, 0] = numpy.linspace(0, 1, 161)
  105.         colors[341:441, 0] = 1
  106.         colors[441:, 0] = numpy.linspace(1, .5, 71)
  107.         # Green channel
  108.         colors[51:200, 1] = numpy.linspace(0, 1, 149)
  109.         colors[200:311, 1] = 1
  110.         colors[311:461, 1] = numpy.linspace(1, 0, 150)
  111.         # Blue channel
  112.         colors[:51, 2] = numpy.linspace(.5, 1, 51)
  113.         colors[51:160, 2] = 1
  114.         colors[160:331, 2] = numpy.linspace(1, 0, 171)
  115.         textureID = glGenTextures(1)
  116.         glEnable(GL_TEXTURE_1D)
  117.         glBindTexture(GL_TEXTURE_1D, textureID)
  118.         glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 512, 0, GL_RGB, GL_FLOAT,
  119.                      colors)
  120.         glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  121.         glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  122.         return textureID
  123.  
  124.  
  125. class MyApp(wx.App):
  126.     def OnInit(self):
  127.         frame = wx.Frame(None)
  128.         frame.SetSize((512, 512))
  129.         canvas = MyCanvas(frame)
  130.         frame.Show()
  131.         return True
  132. MyApp(redirect = False).MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement