ritest.py :  » Game-2D-3D » CGKit » cgkit-2.0.0alpha9 » unittests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » CGKit 
CGKit » cgkit 2.0.0alpha9 » unittests » ritest.py
# Test the Ri interface

import sys
from cgkit import ri
from cgkit import cri
from cgkit.cgtypes import *

try:
    import numpy
    has_numpy = True
except ImportError:
    sys.stderr.write("Warning: numpy module not found. Skipping numpy test in ritest.py.\n")
    has_numpy = False

def normalizeRIB(inFile, outFile):
    """Helper function to normalize a RIB file.
    
    Comments are removed and all numbers are rewritten as floats.
    This is used so that RIBs generated from different renderers can be
    compared.
    
    inFile is an open file handle of the input RIB file. outFile is an
    open handle that is used to write the normalized RIB.
    """
    outFile.write("# Normalized RIB")
    for s in inFile:
        s = s.strip()
        s = s.replace("[", " [ ")
        s = s.replace("]", " ] ")
        s = s.replace("#", " # ")
        tokens = s.split()
        # Drop the version line
        if len(tokens)>0 and tokens[0]=="version":
            continue
        for tok in tokens:
            if tok=="#":
                break
            try:
                f = float(tok)
                f = round(f, 4)
                outFile.write(" %s"%f)
                continue
            except:
                pass
            if tok not in ["[", "]"] and '"' not in tok and (tok[0]==tok[0].upper() or tok=="version"):
                outFile.write("\n%s"%tok)
            else:
                outFile.write(" %s"%tok)
                
                
def customPixelFilter(x, y, w, h):
    return 1.0

def customErrorFunc(code, severity, msg):
    pass
#    print ">>> %s <<< -- Code:%d  Severity:%d"%(msg, code, severity)
    
def subdivFunc(data, detail):
#    print "Subdiv",data,detail
    ri.RiSphere(0.5,-0.5,0.5,360, "uniform color Cs", [0,0,1], "constant string data", data, "constant string info", "This sphere was generated by a custom procedural")
    
def freeFunc(data):
    pass
#    print "Free",data

def quadrics(ri):
    ri.RiTransformBegin()
    ri.RiTranslate(-3,0,0)
    ri.RiSphere(0.5,-0.5,0.5,360, "varying color Cs", [(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTranslate(1,0,0)
    ri.RiCone(1,0.5,360, "varying color Cs", [(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTranslate(1,0,0)
    ri.RiCylinder(0.5,-0.5,0.5,360, "varying color Cs", [(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTranslate(1,0,0)
    ri.RiHyperboloid((0,0.3,0), (0.5,0,0), 360, "varying color Cs", [(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTranslate(1,0,0)
    ri.RiParaboloid(0.5,0.2,0.7, 360, "varying color Cs", [(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTranslate(1,0,0)
    if has_numpy:
        Cs = numpy.array([(1,0,0),(0,1,0),(0,0,1),(1,1,0)], dtype=numpy.float32)
    else:
        Cs = [(1,0,0),(0,1,0),(0,0,1),(1,1,0)]
    ri.RiDisk(0,0.5,360, "varying color Cs", Cs)
    ri.RiTranslate(1,0,0)
    ri.RiTorus(0.45,0.05,0,360,360, "varying color Cs", [(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTransformEnd()
    
def polys(ri):
    A = 0.5*vec3(-1,1,0)
    B = 0.5*vec3(1,1,0)
    C = 0.5*vec3(1,-1,0)
    D = 0.5*vec3(-1,-1,0)
    ri.RiTranslate(-2,0,0)
    ri.RiPolygon(P=[A,B,C,D], Cs=[(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTranslate(1.33,0,0)
    ri.RiGeneralPolygon([4], P=[A,B,C,D], Cs=[(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTranslate(1.33,0,0)
    if has_numpy:
        ids = numpy.array([0,1,2,3], dtype=numpy.int32)
    else:
        ids = [0,1,2,3]
    ri.RiPointsPolygons([4], ids, P=[A,B,C,D], Cs=[(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTranslate(1.33,0,0)
    ri.RiPointsGeneralPolygons([1], [4], [0,1,2,3], P=[A,B,C,D], Cs=[(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    
def patches(ri):
    ri.RiBasis(ri.RiBSplineBasis, ri.RI_BSPLINESTEP, ri.RiCatmullRomBasis, ri.RI_CATMULLROMSTEP)
    ri.RiBasis(ri.RiHermiteBasis, ri.RI_HERMITESTEP, ri.RiPowerBasis, ri.RI_POWERSTEP)
    ri.RiBasis(mat4(2), 3, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16], 3)
    ri.RiBasis(ri.RiBezierBasis, ri.RI_BEZIERSTEP, ri.RiBezierBasis, ri.RI_BEZIERSTEP)
    A = 0.5*vec3(-1,1,0)
    B = 0.5*vec3(1,1,0)
    C = 0.5*vec3(1,-1,0)
    D = 0.5*vec3(-1,-1,0)
    ri.RiTranslate(-2,0,0)
    ri.RiPatch(ri.RI_BILINEAR, P=[A, B, D, C], Cs=[(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTranslate(1.33,0,0)
    ri.RiPatchMesh(ri.RI_BILINEAR, 2, ri.RI_NONPERIODIC, 2, ri.RI_NONPERIODIC, P=[A,B,D,C], Cs=[(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTranslate(1.33,0,0)
    ri.RiNuPatch(2,2,[0,0,1,1],0,1, 2,2,[0,0,1,1],0,1, P=[A,B,D,C], Cs=[(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiTranslate(1.33,0,0)
    ri.RiAttributeBegin()
    ri.RiTrimCurve([1], [2], [0,0,1,2,3,4,4], [0],[4], [5], [0.3,0.7,0.7,0.3,0.3], [0.7,0.7,0.3,0.3,0.7], [1,1,1,1,1])
    ri.RiNuPatch(2,2,[0,0,1,1],0,1, 2,2,[0,0,1,1],0,1, P=[A,B,D,C], Cs=[(1,0,0),(0,1,0),(0,0,1),(1,1,0)])
    ri.RiAttributeEnd()
    ri.RiTranslate(1.33,0,0)
    ri.RiSubdivisionMesh("catmull-clark", [3,3,3,3], [0,1,3, 1,2,3, 2,0,3, 0,2,1], [], [0], [], [], P=[(-1,-1,0), (1,-1,0), (0,1,0), (0,0,1)], Cs=[(1,0,0),(0,1,0),(0,0,1),(1,1,0)])

def miscGeoms(ri):
    ri.RiTranslate(-2,0,0)
    ri.RiPoints(P=[0,0,0], Cs=(1,1,0), constantwidth=0.5)
    ri.RiTranslate(1.33,0,0)
    ri.RiCurves(ri.RI_LINEAR, [2], ri.RI_NONPERIODIC, P=[-0.5,-0.5,0, 0.5,0.5,0], constantwidth=0.1, Cs=[(1,0,0), (0,0,1)])
    ri.RiTranslate(1.33,0,0)
    ri.RiTransformBegin()
    ri.RiScale(0.5,0.5,0.5)
    ri.RiBlobby(2, [1001, 0, 1001, 16, 0,2,0,1], [mat4(1).translation((0.7,0.7,0)), mat4(1)], [], Cs=[(1,0,0),(0,0,1)])
    ri.RiTransformEnd()
    ri.RiTranslate(1.33,0,0)
    ri.RiTransformBegin()
    ri.RiTranslate(0,-0.2,0)
    ri.RiRotate(90, -1,0,0)
    ri.RiScale(vec3(0.2))
    ri.RiGeometry("teapot", "uniform color Cs", [(0,0.5,1)])
    ri.RiTransformEnd()
    ri.RiTranslate(1.33,0,0)
    ri.RiProcedural("spam", [-0.5,0.5,-0.5,0.5,-0.5,0.5], subdivFunc, freeFunc)

def testContextSwitch(ri, archiveName):
    prevCtx = ri.RiGetContext()
    ri.RiBegin(archiveName)
    ri.RiAttributeBegin()
    ri.RiTranslate(4,-3,0)
    ri.RiColor((1,0.5,0))
    ri.RiSphere(1,-1,1,360, "constant string info", "This sphere must be stored in include.rib")
    ri.RiAttributeEnd()
    ri.RiEnd()
    ri.RiContext(prevCtx)
    ri.RiReadArchive("include.rib")
    
def testTransformPoints(ri):
    res = ri.RiTransformPoints("world", "camera", [(1,0,0), vec3(0)])
    if res is not None:
        pnts = []
        for p in res:
            pnts.append(vec3(p.x, 0.2*p.z, 0.01))
        ri.RiColor((0.5,1,0.5))
        ri.RiPoints("P", pnts, "constantwidth", 1.0, "constant string info", "RiTransformPoints test")
#    else:
#        print "RiTransformPoints() failed"

def main(ri, name, archiveName="include.rib"):
    """Main function.
    
    ri: RenderMan API
    name: Argument for RiBegin()
    archiveName: Name of the test archive file
    """
    ri.RiBegin(name)

    ri.RiArchiveRecord(ri.RI_COMMENT, "Test RIB file %d", 1)
    ri.RiArchiveRecord(ri.RI_STRUCTURE, "Creator $CREATOR")
    ri.RiErrorHandler(customErrorFunc)
    ri.RiErrorHandler(ri.RiErrorAbort)
    ri.RiErrorHandler(ri.RiErrorPrint)
    ri.RiErrorHandler(ri.RiErrorIgnore)
    err = ri.RiLastError
    ri.RiOption("searchpath", "shader", "&:shaders")
    ri.RiOption("render", "string bucketorder", "horizontal")
    ri.RiFormat(800, 600, 1.0)
    ri.RiDisplay("out.tif", ri.RI_FRAMEBUFFER, ri.RI_RGBA, "integer[2] origin", [10,10])
    ri.RiCropWindow(0.25, 0.75, 0.1, 0.9)
    ri.RiCropWindow(0, 1, 0.0, 1.0)
    ri.RiPixelSamples(2,2.5)
    ri.RiShadingRate(2.0)
    ri.RiExposure(1.0, 1.0)
    ri.RiFrameAspectRatio(4.0/3.0)
    ri.RiScreenWindow(-4.0/3, 4.0/3, -1, 1)
    ri.RiQuantize(ri.RI_RGBA, 255, 0, 255, 0.5)
    ri.RiPixelFilter(customPixelFilter, 1, 1)
    ri.RiPixelFilter(ri.RiBoxFilter, 1, 1)
    ri.RiPixelFilter(ri.RiTriangleFilter, 1, 1)
    ri.RiPixelFilter(ri.RiCatmullRomFilter, 1, 1)
    ri.RiPixelFilter(ri.RiSincFilter, 1, 1)
    ri.RiPixelFilter(ri.RiGaussianFilter, 2.0, 2.5)
    ri.RiClipping(1, 20)
    ri.RiSides(2)
    ri.RiDeclare("foo", "uniform float")    
    ri.RiHider("hidden")
    ri.RiShadingInterpolation("constant")
    ri.RiPixelVariance(0.1)
    ri.RiGeometricApproximation("flatness", 1.0)
    ri.RiDepthOfField(22, 45, 1000)
    ri.RiDepthOfField(ri.RI_INFINITY, 45, 1000)
    ri.RiRelativeDetail(0.6)
    ri.RiColorSamples(mat3(1), mat3(1))
        
    ri.RiImager("foo", Ka=1.0)

    ri.RiIdentity()
    ri.RiProjection(ri.RI_PERSPECTIVE, fov=40)
    ri.RiTranslate(0,0,8)
    ri.RiTranslate((0,0,2))
    
    ri.RiShutter(0,1)
 
    ri.RiClippingPlane(5,0,0, 1,0,0)
 
    ri.RiFrameBegin(1)
    ri.RiWorldBegin()
    ri.RiCoordinateSystem("myworld")
    ri.RiAttribute("identifier", "name", "object")

    ri.RiColor((1,1,1))
    ri.RiOpacity((1,1,1))
    
    lgt = ri.RiLightSource("distantlight", "from", (0,0,-1), "to", (0,0,0))
    ri.RiIlluminate(lgt, ri.RI_TRUE)
    
    ri.RiSurface("plastic", Kd=1.0)
    
    ri.RiAttributeBegin()
    ri.RiTranslate(0,3,0)
    quadrics(ri)
    ri.RiAttributeEnd()
    
    ri.RiAttributeBegin()
    ri.RiTranslate(0,1.5,0)
    polys(ri)
    ri.RiAttributeEnd()
    
    ri.RiAttributeBegin()
    patches(ri)
    ri.RiAttributeEnd()

    ri.RiAttributeBegin()
    ri.RiTranslate(0,-1.5,0)
    miscGeoms(ri)
    ri.RiAttributeEnd()
    
    testContextSwitch(ri, archiveName)

    ri.RiTransformBegin()
    
    ri.RiTranslate(0,-3,0)
    # Motion blur
    ri.RiAttributeBegin()
    ri.RiMotionBegin(0.0, 1.0)
    ri.RiTranslate(-0.1,0,0)
    ri.RiTranslate( 0.1,0,0)
    ri.RiMotionEnd()
    ri.RiMotionBegin([0.0, 1.0])
    ri.RiRotate(0, vec3(1,0,0))
    ri.RiRotate(90, vec3(1,0,0))
    ri.RiMotionEnd()
    ri.RiSphere(0.5,-0.5,0.5,360)
    ri.RiAttributeEnd()
    
    # Object instancing
    obj = ri.RiObjectBegin()
    ri.RiSphere(0.5,-0.5,0.5,360)
    ri.RiObjectEnd()
    ri.RiTranslate(1.33, 0, 0)
    ri.RiColor((0,0.5,0.5))
    ri.RiObjectInstance(obj)

    ri.RiTransformEnd()
        
    testTransformPoints(ri)
    
    # Misc
    ri.RiMatte(ri.RI_TRUE)
    ri.RiOrientation("lh")
    ri.RiReverseOrientation()
    ri.RiPerspective(45.0)
    ri.RiSkew(45, 0,1,0, 1,0,0)
    ri.RiSkew(45, vec3(0,1,0), vec3(1,0,0))
    ri.RiTransform(mat4(1))
    ri.RiConcatTransform(mat4(1))
    ri.RiTextureCoordinates(0,0, 1,0, 1,1, 0,1)
    ri.RiDisplacement("foo", {"Ka":1.0, "constant string mapname":"foo.map"})
    ri.RiAtmosphere("foo", Ka=1.0, Ks=0.5)
    ri.RiInterior("foo", "Ka", 1.0, "Ks", 0.6, "constant string mapname", "foo.map")
    ri.RiExterior("foo", Ka=1.0)
    algt = ri.RiAreaLightSource("foo", Ka=1.0)
    # Test string handles
    lgt2 = ri.RiLightSource("distantlight", ri.RI_HANDLEID, "mylight", "Ka", 0.7)
    ri.RiIlluminate(lgt2, ri.RI_FALSE)
    
    ri.RiBound([0,1,0,1,0,1])
    ri.RiCoordSysTransform("myworld")
    ri.RiDetail([0,1,0,1,0,1])
    ri.RiDetailRange(0, 0, 10, 20)
    ri.RiSolidBegin("primitive")
    ri.RiSolidEnd()
    
    ri.RiProcedural("mymodel.rib", [-1,1,-1,1,-1,1], ri.RiProcDelayedReadArchive, ri.RiProcFree)
    ri.RiProcedural(["python teapot.py", "foo"], [-1,1,-1,1,-1,1], ri.RiProcRunProgram, ri.RiProcFree)
    ri.RiProcedural(["teaspot.so", "bar"], [-1,1,-1,1,-1,1], ri.RiProcDynamicLoad, ri.RiProcFree)
    
    ri.RiWorldEnd()
    ri.RiMakeTexture("input.tif", "output.tex", ri.RI_PERIODIC, ri.RI_CLAMP, ri.RiGaussianFilter, 2.0, 2.0)
    ri.RiMakeTexture("input.tif", "output.tex", ri.RI_PERIODIC, ri.RI_CLAMP, customPixelFilter, 2.0, 2.0)
    ri.RiMakeShadow("input.tif", "output.tex")
    ri.RiMakeLatLongEnvironment("input.tif", "output.tex", ri.RiGaussianFilter, 2.0, 2.0)
    ri.RiMakeLatLongEnvironment("input.tif", "output.tex", customPixelFilter, 2.0, 2.0)
    ri.RiMakeCubeFaceEnvironment("px.tif", "nx.tif", "py.tif", "ny.tif", "pz.tif", "nz.tif", "output.tex", 95, ri.RiGaussianFilter, 2.0, 2.0)
    ri.RiMakeCubeFaceEnvironment("px.tif", "nx.tif", "py.tif", "ny.tif", "pz.tif", "nz.tif", "output.tex", 95, customPixelFilter, 2.0, 2.0)
    ri.RiFrameEnd()
    ri.RiEnd()


if __name__=="__main__":
    import sys
    
#    renderer = None
    renderer = "3delight"
#    renderer = "ri"
#    renderer = "aqsislib"
#    renderer = "ri2rib"
    ri = cri.loadRI(renderer)
    main(ri, None)
#    main(ri, "test.rib")
        
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.