import sys
sys.path.append('c:/Documents and Settings/Kirby/My Documents/Py Files/dst/')

from pandac.libpandaeggModules import EggTexture, EggComment, EggData, EggPolygon, EggVertex, EggVertexPool, loadEggData
from pandac.PandaModules import Filename, NodePath, Point3D, Point2D, deg2Rad, Vec4
import math
import rbf

class WriteEgg(object):

    def __init__(self, filename, filepath = 'd:/Panda3D-1.2.2/bin/lib/site-packages/'):
        self.filename = filename
        self.filepath = filepath
        self.data = EggData()
        self.vp = EggVertexPool('verts')
        self.data.addChild(self.vp)
        self.vertcolor = Vec4(0.1, 0.1, 0.1, 1)
        self.polycolor = Vec4(0.2, 0.2, 0.8, 1)        

    def header(self):
        self.data.setCoordinateSystem(1) # Z-Up


    def addvert(self, vertex):
        v = EggVertex()
        v.setPos(Point3D(*vertex.xyz))
        v.setColor(self.vertcolor)
        v.setUv(Point2D(0,0))
        self.vp.addVertex(v)

    def addpoly(self, face, vtable):        
        poly = EggPolygon()
        poly.setColor(self.polycolor)
        self.data.addChild(poly)
        for f in face:
            v = self.vp.getVertex(vtable[f])
            poly.addVertex(v)
        poly.recomputePolygonNormal()

    def writedata(self):
        # To write the egg file to disk, use this:

        print self.filepath + self.filename
        self.data.writeEgg(Filename(self.filepath + self.filename))

def main():
    we = WriteEgg('cubocta.egg')
    we.header()
    poly = rbf.Cubocta()

    vtable = {}
    intkey = 0
    
    for v in poly.vertices:
        vtable[v] = intkey
        we.addvert(poly.vertices[v])
        intkey += 1

    for face in poly.faces:
        we.addpoly(face, vtable)
        
    we.writedata()
    
main()

# code highlighted using py2html.py version 0.8