chiark / gitweb /
Improve rendering time by using numpy to generate normals, and vertex arrays. This...
authordaid <daid303@gmail.com>
Mon, 30 Jul 2012 09:02:49 +0000 (11:02 +0200)
committerdaid <daid303@gmail.com>
Mon, 30 Jul 2012 09:02:49 +0000 (11:02 +0200)
Cura/gui/opengl.py
Cura/util/mesh.py

index 7b2b676c22d1318951740d052c5692001904bea4..4cf97405a2c80a5cc8bc0d4ce4482c535edc7401 100644 (file)
@@ -1,4 +1,4 @@
-import math\r
+import math, time\r
 \r
 from util import util3d\r
 from util import profile\r
@@ -205,20 +205,20 @@ def DrawBox(vMin, vMax):
 \r
 def DrawSTL(mesh):\r
        glEnable(GL_CULL_FACE)\r
-       for i in xrange(0, mesh.vertexCount, 3):\r
-               glBegin(GL_TRIANGLES)\r
-               v1 = mesh.vertexes[i]\r
-               v2 = mesh.vertexes[i+1]\r
-               v3 = mesh.vertexes[i+2]\r
-               glNormal3f(mesh.normal[i/3][0], mesh.normal[i/3][1], mesh.normal[i/3][2])\r
-               glVertex3f(v1[0], v1[1], v1[2])\r
-               glVertex3f(v2[0], v2[1], v2[2])\r
-               glVertex3f(v3[0], v3[1], v3[2])\r
-               glNormal3f(-mesh.normal[i/3][0], -mesh.normal[i/3][1], -mesh.normal[i/3][2])\r
-               glVertex3f(v1[0], v1[1], v1[2])\r
-               glVertex3f(v3[0], v3[1], v3[2])\r
-               glVertex3f(v2[0], v2[1], v2[2])\r
-               glEnd()\r
+       glEnableClientState(GL_VERTEX_ARRAY);\r
+       glEnableClientState(GL_NORMAL_ARRAY);\r
+       glVertexPointer(3, GL_FLOAT, 0, mesh.vertexes)\r
+       glNormalPointer(GL_FLOAT, 0, mesh.normal)\r
+       \r
+       glCullFace(GL_BACK)\r
+       glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount)\r
+       \r
+       glCullFace(GL_FRONT)\r
+       glNormalPointer(GL_FLOAT, 0, mesh.invNormal)\r
+       glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount)\r
+       \r
+       glDisableClientState(GL_VERTEX_ARRAY)\r
+       glDisableClientState(GL_NORMAL_ARRAY);\r
 \r
 def DrawGCodeLayer(layer):\r
        filamentRadius = profile.getProfileSettingFloat('filament_diameter') / 2\r
index dc01198c3b59ad46180ef72c59f883a58cbaa984..799551f870f8b387ee8e2459d03ae3daf8d036b1 100644 (file)
@@ -20,7 +20,7 @@ class mesh(object):
        def _prepareVertexCount(self, vertexNumber):
                #Set the amount of faces before loading data in them. This way we can create the numpy arrays before we fill them.
                self.origonalVertexes = numpy.zeros((vertexNumber, 3), float)
-               self.normal = numpy.zeros((vertexNumber / 3, 3))
+               self.normal = numpy.zeros((vertexNumber, 3), float)
                self.vertexCount = 0
 
        def _postProcessAfterLoad(self):
@@ -56,11 +56,11 @@ class mesh(object):
                mat10 = math.sin(rotate) * scaleX
                mat11 = math.cos(rotate) * scaleY
                
-               mat = numpy.array([[mat00,mat10,0],[mat01,mat11,0],[0,0,scaleZ]])
+               mat = numpy.array([[mat00,mat10,0],[mat01,mat11,0],[0,0,scaleZ]], float)
                if swapXZ:
-                       mat = numpy.array([mat[2],mat[1],mat[0]])
+                       mat = numpy.array([mat[2],mat[1],mat[0]], float)
                if swapYZ:
-                       mat = numpy.array([mat[0],mat[2],mat[1]])
+                       mat = numpy.array([mat[0],mat[2],mat[1]], float)
                self.vertexes = (numpy.matrix(self.origonalVertexes, copy = False) * numpy.matrix(mat)).getA()
 
                tris = self.vertexes.reshape(self.vertexCount / 3, 3, 3)
@@ -69,7 +69,13 @@ class mesh(object):
                normals[:,0] /= lens
                normals[:,1] /= lens
                normals[:,2] /= lens
-               self.normal = normals
+               
+               n = numpy.zeros((self.vertexCount / 3, 9), float)
+               n[:,0:3] = normals
+               n[:,3:6] = normals
+               n[:,6:9] = normals
+               self.normal = n.reshape(self.vertexCount, 3)
+               self.invNormal = -self.normal
                
                self.getMinimumZ()