# Copyright 2009 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
# This script is to demonstrate the ssimple geometry creation and gemoetry instancing feature.
#
# Topic: FBMesh, FBVector3d, FBModelTransformationMatrix, FBModelShadingMode
#
from pyfbsdk import FBMesh, FBModelCube, FBVector3d, FBModelTransformationMatrix, FBModelShadingMode

# Create the custom mesh
lMesh = FBMesh("myGeom")

# Alway call GeometryBegin() / GeometryEnd() in pair when editting geometry.
lMesh.GeometryBegin()

# Call VertexInit() to resize or reserve vertex/normal array. 
# pResize = true, work with known vertex count and VertexSet() function.
# pResize = false, work with dynamical vertex count and VertexAdd() function.
lMesh.VertexInit(4, False, False)

lMesh.VertexAdd(0, 100, 0)  # vertex 0
lMesh.VertexAdd(100,100, 0) # vertex 1
lMesh.VertexAdd(100, 0, 0)  # vertex 2
lMesh.VertexAdd(0,  0, 0)   # vertex 3

# Add a Polygon
lMesh.PolygonBegin()
lMesh.PolygonVertexAdd(0)   # add polygon vertex 0
lMesh.PolygonVertexAdd(1)   # add polygon vertex 1
lMesh.PolygonVertexAdd(2)   # add polygon vertex 2
lMesh.PolygonVertexAdd(3)   # add polygon vertex 3
# Polygon add End
lMesh.PolygonEnd()

# Compute mesh vertex normal with Counter Clock-Wise order
lMesh.ComputeVertexNormals(True)

# Alway call GeometryBegin() / GeometryEnd() in pair when editting geometry.
lMesh.GeometryEnd()

# And we use Geometry Instancing feature to share this simple plane among multiple models.
for lIndex in range(1, 10):
    #create a cube but we will replace its geometry
    lModel = FBModelCube("myModel")

    # Replace the geometry instance
    lModel.Geometry = lMesh

    lModel.SetVector( FBVector3d(120*lIndex - 600, 50, 50 ) )

    # Let's use a different shading mode.
    if (lIndex > 5):
        lModel.ShadingMode = FBModelShadingMode.kFBModelShadingWire

    # The object must be set visible to be present in the system.
    lModel.Visible = True
    lModel.Show = True


# Cleanup.
del( lModel, lMesh, FBMesh, FBModelCube, FBVector3d, FBModelTransformationMatrix, FBModelShadingMode )