chiark / gitweb /
Change how the engine is interfaced from the python code. Put the GCode viewer in...
[cura.git] / Cura / gui / util / opengl.py
index 36213b5448f0f44d8ff7c856b103f71de236d05a..7cf19b3d76fdab682e193566b586027d115c643c 100644 (file)
@@ -6,14 +6,11 @@ import numpy
 import wx
 import time
 
-from Cura.util import meshLoader
-from Cura.util import util3d
-from Cura.util import profile
-from Cura.util.resources import getPathForMesh, getPathForImage
+from Cura.util.resources import getPathForImage
 
 import OpenGL
 
-OpenGL.ERROR_CHECKING = False
+#OpenGL.ERROR_CHECKING = False
 from OpenGL.GLUT import *
 from OpenGL.GLU import *
 from OpenGL.GL import *
@@ -131,62 +128,70 @@ class GLFakeShader(GLReferenceCounter):
                return ''
 
 class GLVBO(GLReferenceCounter):
-       def __init__(self, vertexArray, normalArray = None):
+       def __init__(self, renderType, vertexArray, normalArray = None, indicesArray = None):
                super(GLVBO, self).__init__()
+               self._renderType = renderType
                if not bool(glGenBuffers):
                        self._vertexArray = vertexArray
                        self._normalArray = normalArray
+                       self._indicesArray = indicesArray
+                       self._size = len(vertexArray)
                        self._buffer = None
+                       self._hasNormals = self._normalArray is not None
+                       self._hasIndices = self._indicesArray is not None
                else:
                        self._buffer = glGenBuffers(1)
                        self._size = len(vertexArray)
                        self._hasNormals = normalArray is not None
+                       self._hasIndices = indicesArray is not None
                        glBindBuffer(GL_ARRAY_BUFFER, self._buffer)
                        if self._hasNormals:
                                glBufferData(GL_ARRAY_BUFFER, numpy.concatenate((vertexArray, normalArray), 1), GL_STATIC_DRAW)
                        else:
                                glBufferData(GL_ARRAY_BUFFER, vertexArray, GL_STATIC_DRAW)
                        glBindBuffer(GL_ARRAY_BUFFER, 0)
-
-       def render(self, render_type = GL_TRIANGLES):
+                       if self._hasIndices:
+                               self._size = len(indicesArray)
+                               self._bufferIndices = glGenBuffers(1)
+                               glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self._bufferIndices)
+                               glBufferData(GL_ELEMENT_ARRAY_BUFFER, numpy.array(indicesArray, numpy.uint32), GL_STATIC_DRAW)
+
+       def render(self):
+               glEnableClientState(GL_VERTEX_ARRAY)
                if self._buffer is None:
-                       glEnableClientState(GL_VERTEX_ARRAY)
                        glVertexPointer(3, GL_FLOAT, 0, self._vertexArray)
-                       if self._normalArray is not None:
+                       if self._hasNormals:
                                glEnableClientState(GL_NORMAL_ARRAY)
                                glNormalPointer(GL_FLOAT, 0, self._normalArray)
-                       #Odd, drawing in batchs is a LOT faster then drawing it all at once.
-                       batchSize = 999    #Warning, batchSize needs to be dividable by 3
-                       extraStartPos = int(len(self._vertexArray) / batchSize) * batchSize
-                       extraCount = len(self._vertexArray) - extraStartPos
-
-                       glCullFace(GL_BACK)
-                       for i in xrange(0, int(len(self._vertexArray) / batchSize)):
-                               glDrawArrays(GL_TRIANGLES, i * batchSize, batchSize)
-                       glDrawArrays(GL_TRIANGLES, extraStartPos, extraCount)
                else:
-                       glEnableClientState(GL_VERTEX_ARRAY)
                        glBindBuffer(GL_ARRAY_BUFFER, self._buffer)
-
                        if self._hasNormals:
                                glEnableClientState(GL_NORMAL_ARRAY)
                                glVertexPointer(3, GL_FLOAT, 2*3*4, c_void_p(0))
                                glNormalPointer(GL_FLOAT, 2*3*4, c_void_p(3 * 4))
                        else:
                                glVertexPointer(3, GL_FLOAT, 3*4, c_void_p(0))
+                       if self._hasIndices:
+                               glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self._bufferIndices)
 
+               if self._hasIndices:
+                       glDrawElements(self._renderType, self._size, GL_UNSIGNED_INT, c_void_p(0))
+               else:
                        batchSize = 996    #Warning, batchSize needs to be dividable by 4, 3 and 2
                        extraStartPos = int(self._size / batchSize) * batchSize
                        extraCount = self._size - extraStartPos
-
                        for i in xrange(0, int(self._size / batchSize)):
-                               glDrawArrays(render_type, i * batchSize, batchSize)
-                       glDrawArrays(render_type, extraStartPos, extraCount)
+                               glDrawArrays(self._renderType, i * batchSize, batchSize)
+                       glDrawArrays(self._renderType, extraStartPos, extraCount)
+
+               if self._buffer is not None:
                        glBindBuffer(GL_ARRAY_BUFFER, 0)
+               if self._hasIndices:
+                       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
 
-                       glDisableClientState(GL_VERTEX_ARRAY)
-                       if self._hasNormals:
-                               glDisableClientState(GL_NORMAL_ARRAY)
+               glDisableClientState(GL_VERTEX_ARRAY)
+               if self._hasNormals:
+                       glDisableClientState(GL_NORMAL_ARRAY)
 
        def release(self):
                if self._buffer is not None:
@@ -202,142 +207,6 @@ class GLVBO(GLReferenceCounter):
                if self._buffer is not None and bool(glDeleteBuffers):
                        print "VBO was not properly released!"
 
-def DrawMachine(machineSize):
-       glDisable(GL_LIGHTING)
-       glDisable(GL_CULL_FACE)
-       glEnable(GL_BLEND)
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
-
-       sx = machineSize.x
-       sy = machineSize.y
-       for x in xrange(-int(sx/20)-1, int(sx / 20) + 1):
-               for y in xrange(-int(sx/20)-1, int(sy / 20) + 1):
-                       x1 = sx/2+x * 10
-                       x2 = x1 + 10
-                       y1 = sx/2+y * 10
-                       y2 = y1 + 10
-                       x1 = max(min(x1, sx), 0)
-                       y1 = max(min(y1, sy), 0)
-                       x2 = max(min(x2, sx), 0)
-                       y2 = max(min(y2, sy), 0)
-                       if (x & 1) == (y & 1):
-                               glColor4ub(5, 171, 231, 127)
-                       else:
-                               glColor4ub(5 * 8 / 10, 171 * 8 / 10, 231 * 8 / 10, 128)
-                       glBegin(GL_QUADS)
-                       glVertex3f(x1, y1, -0.02)
-                       glVertex3f(x2, y1, -0.02)
-                       glVertex3f(x2, y2, -0.02)
-                       glVertex3f(x1, y2, -0.02)
-                       glEnd()
-
-       glEnable(GL_CULL_FACE)
-
-       if profile.getPreference('machine_type') == 'ultimaker':
-               glPushMatrix()
-               glEnable(GL_LIGHTING)
-               glTranslate(100, 200, -1)
-               glLightfv(GL_LIGHT0, GL_DIFFUSE, [0.8, 0.8, 0.8])
-               glLightfv(GL_LIGHT0, GL_AMBIENT, [0.5, 0.5, 0.5])
-               glEnable(GL_BLEND)
-               glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR)
-
-               global platformMesh
-               if platformMesh is None:
-                       try:
-                               platformMesh = meshLoader.loadMesh(getPathForMesh('ultimaker_platform.stl'))
-                       except:
-                               platformMesh = False
-
-               if platformMesh:
-                       DrawMesh(platformMesh)
-               glPopMatrix()
-               glDisable(GL_LIGHTING)
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
-
-       glColor4ub(5, 171, 231, 64)
-       glBegin(GL_QUADS)
-       glVertex3f(0, 0, machineSize.z)
-       glVertex3f(0, machineSize.y, machineSize.z)
-       glVertex3f(machineSize.x, machineSize.y, machineSize.z)
-       glVertex3f(machineSize.x, 0, machineSize.z)
-       glEnd()
-
-       glColor4ub(5, 171, 231, 96)
-       glBegin(GL_QUADS)
-       glVertex3f(0, 0, 0)
-       glVertex3f(0, 0, machineSize.z)
-       glVertex3f(machineSize.x, 0, machineSize.z)
-       glVertex3f(machineSize.x, 0, 0)
-
-       glVertex3f(0, machineSize.y, machineSize.z)
-       glVertex3f(0, machineSize.y, 0)
-       glVertex3f(machineSize.x, machineSize.y, 0)
-       glVertex3f(machineSize.x, machineSize.y, machineSize.z)
-       glEnd()
-
-       glColor4ub(5, 171, 231, 128)
-       glBegin(GL_QUADS)
-       glVertex3f(0, 0, machineSize.z)
-       glVertex3f(0, 0, 0)
-       glVertex3f(0, machineSize.y, 0)
-       glVertex3f(0, machineSize.y, machineSize.z)
-
-       glVertex3f(machineSize.x, 0, 0)
-       glVertex3f(machineSize.x, 0, machineSize.z)
-       glVertex3f(machineSize.x, machineSize.y, machineSize.z)
-       glVertex3f(machineSize.x, machineSize.y, 0)
-       glEnd()
-
-       glDisable(GL_BLEND)
-
-       #Draw the X/Y/Z indicator
-       glPushMatrix()
-       glTranslate(5, 5, 2)
-       glLineWidth(2)
-       glColor3f(0.5, 0, 0)
-       glBegin(GL_LINES)
-       glVertex3f(0, 0, 0)
-       glVertex3f(20, 0, 0)
-       glEnd()
-       glColor3f(0, 0.5, 0)
-       glBegin(GL_LINES)
-       glVertex3f(0, 0, 0)
-       glVertex3f(0, 20, 0)
-       glEnd()
-       glColor3f(0, 0, 0.5)
-       glBegin(GL_LINES)
-       glVertex3f(0, 0, 0)
-       glVertex3f(0, 0, 20)
-       glEnd()
-
-       glDisable(GL_DEPTH_TEST)
-       #X
-       glColor3f(1, 0, 0)
-       glPushMatrix()
-       glTranslate(20, 0, 0)
-       noZ = ResetMatrixRotationAndScale()
-       glDrawStringCenter("X")
-       glPopMatrix()
-
-       #Y
-       glColor3f(0, 1, 0)
-       glPushMatrix()
-       glTranslate(0, 20, 0)
-       glDrawStringCenter("Y")
-       glPopMatrix()
-
-       #Z
-       if not noZ:
-               glColor3f(0, 0, 1)
-               glPushMatrix()
-               glTranslate(0, 0, 20)
-               glDrawStringCenter("Z")
-               glPopMatrix()
-
-       glPopMatrix()
-       glEnable(GL_DEPTH_TEST)
-
 def glDrawStringCenter(s):
        glRasterPos2f(0, 0)
        glBitmap(0,0,0,0, -glGetStringSize(s)[0]/2, 0, None)
@@ -689,106 +558,3 @@ def DrawMeshSteep(mesh, matrix, angle):
                        glVertex3f(mesh.vertexes[i + 1][0], mesh.vertexes[i + 1][1], mesh.vertexes[i + 1][2])
                        glEnd()
        glDepthFunc(GL_LESS)
-
-def DrawGCodeLayer(layer, drawQuick = True):
-       filamentRadius = profile.getProfileSettingFloat('filament_diameter') / 2
-       filamentArea = math.pi * filamentRadius * filamentRadius
-       lineWidth = profile.getProfileSettingFloat('nozzle_size') / 2 / 10
-
-       fillCycle = 0
-       fillColorCycle = [[0.5, 0.5, 0.0, 1], [0.0, 0.5, 0.5, 1], [0.5, 0.0, 0.5, 1]]
-       moveColor = [0, 0, 1, 0.5]
-       retractColor = [1, 0, 0.5, 0.5]
-       supportColor = [0, 1, 1, 1]
-       extrudeColor = [[1, 0, 0, 1], [0, 1, 1, 1], [1, 1, 0, 1], [1, 0, 1, 1]]
-       innerWallColor = [0, 1, 0, 1]
-       skirtColor = [0, 0.5, 0.5, 1]
-       prevPathWasRetract = False
-
-       glDisable(GL_CULL_FACE)
-       for path in layer:
-               if path.type == 'move':
-                       if prevPathWasRetract:
-                               c = retractColor
-                       else:
-                               c = moveColor
-                       if drawQuick:
-                               continue
-               zOffset = 0.01
-               if path.type == 'extrude':
-                       if path.pathType == 'FILL':
-                               c = fillColorCycle[fillCycle]
-                               fillCycle = (fillCycle + 1) % len(fillColorCycle)
-                       elif path.pathType == 'WALL-INNER':
-                               c = innerWallColor
-                               zOffset = 0.02
-                       elif path.pathType == 'SUPPORT':
-                               c = supportColor
-                       elif path.pathType == 'SKIRT':
-                               c = skirtColor
-                       else:
-                               c = extrudeColor[path.extruder]
-               if path.type == 'retract':
-                       c = retractColor
-               if path.type == 'extrude' and not drawQuick:
-                       drawLength = 0.0
-                       prevNormal = None
-                       for i in xrange(0, len(path.points) - 1):
-                               v0 = path.points[i]
-                               v1 = path.points[i + 1]
-
-                               # Calculate line width from ePerDistance (needs layer thickness and filament diameter)
-                               dist = (v0 - v1).vsize()
-                               if dist > 0 and path.layerThickness > 0:
-                                       extrusionMMperDist = (v1.e - v0.e) / dist
-                                       lineWidth = extrusionMMperDist * filamentArea / path.layerThickness / 2 * v1.extrudeAmountMultiply
-
-                               drawLength += (v0 - v1).vsize()
-                               normal = (v0 - v1).cross(util3d.Vector3(0, 0, 1))
-                               normal.normalize()
-
-                               vv2 = v0 + normal * lineWidth
-                               vv3 = v1 + normal * lineWidth
-                               vv0 = v0 - normal * lineWidth
-                               vv1 = v1 - normal * lineWidth
-
-                               glBegin(GL_QUADS)
-                               glColor4fv(c)
-                               glVertex3f(vv0.x, vv0.y, vv0.z - zOffset)
-                               glVertex3f(vv1.x, vv1.y, vv1.z - zOffset)
-                               glVertex3f(vv3.x, vv3.y, vv3.z - zOffset)
-                               glVertex3f(vv2.x, vv2.y, vv2.z - zOffset)
-                               glEnd()
-                               if prevNormal is not None:
-                                       n = (normal + prevNormal)
-                                       n.normalize()
-                                       vv4 = v0 + n * lineWidth
-                                       vv5 = v0 - n * lineWidth
-                                       glBegin(GL_QUADS)
-                                       glColor4fv(c)
-                                       glVertex3f(vv2.x, vv2.y, vv2.z - zOffset)
-                                       glVertex3f(vv4.x, vv4.y, vv4.z - zOffset)
-                                       glVertex3f(prevVv3.x, prevVv3.y, prevVv3.z - zOffset)
-                                       glVertex3f(v0.x, v0.y, v0.z - zOffset)
-
-                                       glVertex3f(vv0.x, vv0.y, vv0.z - zOffset)
-                                       glVertex3f(vv5.x, vv5.y, vv5.z - zOffset)
-                                       glVertex3f(prevVv1.x, prevVv1.y, prevVv1.z - zOffset)
-                                       glVertex3f(v0.x, v0.y, v0.z - zOffset)
-                                       glEnd()
-
-                               prevNormal = normal
-                               prevVv1 = vv1
-                               prevVv3 = vv3
-               else:
-                       glColor4fv(c)
-                       glBegin(GL_TRIANGLES)
-                       for v in path.points:
-                               glVertex3f(v[0], v[1], v[2])
-                       glEnd()
-
-               if not path.type == 'move':
-                       prevPathWasRetract = False
-               #if path.type == 'retract' and path.points[0].almostEqual(path.points[-1]):
-               #       prevPathWasRetract = True
-       glEnable(GL_CULL_FACE)