chiark / gitweb /
Add OpenGL buttons for tool selection.
[cura.git] / Cura / gui / util / opengl.py
index 1a9a43ffbee0268ca94f1ea46a2bf395b0ccaab4..73f820b2b88ffa3c96c6e2da099ad717f039282a 100644 (file)
@@ -2,11 +2,13 @@
 from __future__ import absolute_import
 
 import math
+import numpy
+import wx
 
 from Cura.util import meshLoader
 from Cura.util import util3d
 from Cura.util import profile
-from Cura.util.resources import getPathForMesh
+from Cura.util.resources import getPathForMesh, getPathForImage
 
 try:
        import OpenGL
@@ -22,8 +24,6 @@ except:
        print "Failed to find PyOpenGL: http://pyopengl.sourceforge.net/"
        hasOpenGLlibs = False
 
-import numpy
-
 def InitGL(window, view3D, zoom):
        # set viewing projection
        glMatrixMode(GL_MODELVIEW)
@@ -260,6 +260,25 @@ def unproject(winx, winy, winz, modelMatrix, projMatrix, viewport):
 def convert3x3MatrixTo4x4(matrix):
        return list(matrix.getA()[0]) + [0] + list(matrix.getA()[1]) + [0] + list(matrix.getA()[2]) + [0, 0,0,0,1]
 
+def loadGLTexture(filename):
+       tex = glGenTextures(1)
+       glBindTexture(GL_TEXTURE_2D, tex)
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
+       img = wx.ImageFromBitmap(wx.Bitmap(getPathForImage(filename)))
+       rgbData = img.GetData()
+       alphaData = img.GetAlphaData()
+       if alphaData != None:
+               data = ''
+               for i in xrange(0, len(alphaData)):
+                       data += rgbData[i*3:i*3+3] + alphaData[i]
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.GetWidth(), img.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, data)
+       else:
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.GetWidth(), img.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, rgbData)
+       return tex
+
 def ResetMatrixRotationAndScale():
        matrix = glGetFloatv(GL_MODELVIEW_MATRIX)
        noZ = False