Advertisement
Guest User

meshgenerator.py

a guest
Oct 28th, 2011
1,299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. #
  2. #
  3. #   Generate a mesh
  4. #
  5. #
  6.  
  7. from panda3d.core import Texture, GeomNode
  8. from panda3d.core import GeomVertexFormat, GeomVertexData
  9. from panda3d.core import Geom, GeomTriangles, GeomVertexWriter
  10. from panda3d.core import Vec3, Vec4, Point3
  11. import time
  12.  
  13. def myNormalize(myVec):
  14.     myVec.normalize()
  15.     return myVec
  16.  
  17. class MeshGenerator():
  18.    
  19.     def __init__(self, name = 'Mesh'):
  20.        
  21.         self.name = name
  22.         self.finished = False
  23.  
  24.         self.format = GeomVertexFormat.getV3c4t2()
  25.         #print self.format
  26.         self.vertexData = GeomVertexData(name, self.format, Geom.UHStatic)
  27.         self.vertexData.setNumRows(26136)
  28.        
  29.         self.mesh = Geom(self.vertexData)
  30.         self.triangles = GeomTriangles(Geom.UHStatic)
  31.         self.triangleData = self.triangles.modifyVertices()
  32.         self.triangleData.setNumRows(26136)
  33.        
  34.         self.vertex = GeomVertexWriter(self.vertexData, 'vertex')
  35.         self.normal = GeomVertexWriter(self.vertexData, 'normal')
  36.         self.color = GeomVertexWriter(self.vertexData, 'color')
  37.         self.texcoord = GeomVertexWriter(self.vertexData, 'texcoord')
  38.        
  39.         self.faceCount = 0
  40.    
  41.     def makeFace(self, x1, y1, z1, x2, y2, z2, id, color):
  42.        
  43.         if x1 != x2:
  44.        
  45.             self.vertex.addData3f(x1, y1, z1)
  46.             self.vertex.addData3f(x2, y1, z1)
  47.             self.vertex.addData3f(x2, y2, z2)
  48.             self.vertex.addData3f(x1, y2, z2)
  49.            
  50.         else:
  51.        
  52.             self.vertex.addData3f(x1, y1, z1)
  53.             self.vertex.addData3f(x2, y2, z1)
  54.             self.vertex.addData3f(x2, y2, z2)
  55.             self.vertex.addData3f(x1, y1, z2)
  56.  
  57.  
  58.         self.color.addData4f(color, color, color, 1.0)
  59.         self.color.addData4f(color, color, color, 1.0)
  60.         self.color.addData4f(color, color, color, 1.0)
  61.         self.color.addData4f(color, color, color, 1.0)
  62.        
  63.         if id == 1:
  64.             self.texcoord.addData2f(0.0, 1.0)
  65.             self.texcoord.addData2f(0.0, 0.0)
  66.             self.texcoord.addData2f(0.5, 0.0)
  67.             self.texcoord.addData2f(0.5, 1.0)
  68.         elif id == 2:
  69.             self.texcoord.addData2f(0.5, 1.0)
  70.             self.texcoord.addData2f(0.5, 0.0)
  71.             self.texcoord.addData2f(1.0, 0.0)
  72.             self.texcoord.addData2f(1.0, 1.0)
  73.        
  74.         vertexId = self.faceCount * 4
  75.        
  76.         self.triangles.addVertices(vertexId, vertexId + 1, vertexId + 3)
  77.         self.triangles.addVertices(vertexId + 1, vertexId + 2, vertexId + 3)
  78.        
  79.         self.faceCount += 1
  80.    
  81.     def makeFrontFace(self, x, y, z, id):
  82.         self.makeFace(x + 1, y + 1, z - 1, x, y + 1, z, id, 0.6)
  83.    
  84.     def makeBackFace(self, x, y, z, id):
  85.         self.makeFace(x, y, z - 1, x + 1, y, z, id, 0.85)
  86.    
  87.     def makeRightFace(self, x, y, z, id):
  88.         self.makeFace(x + 1, y, z - 1, x + 1, y + 1, z, id, 1.0)
  89.    
  90.     def makeLeftFace(self, x, y, z, id):
  91.         self.makeFace(x, y + 1, z - 1, x, y, z, id, 0.9)
  92.    
  93.     def makeTopFace(self, x, y, z, id):
  94.         self.makeFace(x + 1, y + 1, z, x, y, z, id, 1.0)
  95.    
  96.     def makeBottomFace(self, x, y, z, id):
  97.         self.makeFace(x, y + 1, z - 1, x + 1, y, z - 1, id, 0.70)
  98.    
  99.     def getMesh(self):
  100.         return self.mesh
  101.    
  102.     def getGeomNode(self):
  103.         if self.finished == False:
  104.             self.triangles.closePrimitive()
  105.             self.mesh.addPrimitive(self.triangles)
  106.             self.finished = True
  107.         geomNode = GeomNode(self.name)
  108.         geomNode.addGeom(self.mesh)
  109.         return geomNode
  110.        
  111. '''ft = time.time()
  112.  
  113. mesh = MeshGenerator('Meshymesh')
  114.  
  115. for i in range(0, 100000):
  116.    mesh.makeRightFace(0,1,1, 1)
  117.    
  118. print time.time() - ft'''
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement