# This file merges szgpovinteract.py with a POV-Ray scene which renders # a localized portion of the Menger sponge. Paul Prue--7/5/2006 import szgpov from PySZG import * from OpenGL.GL import * from OpenGL.GLU import * import os import sys import math class POVEffector(arEffector): def __init__(self): arEffector.__init__(self,1,6,0,0,0) # set length to 5 ft. self.setTipOffset( arVector3(0,0,3) ) # set to interact with closest object within 1 ft. of tip # (see PySZG.py for alternative classes for selecting objects) self.setInteractionSelector( arDistanceInteractionSelector(1.5) ) # set to grab an object (that has already been selected for interaction # using rule specified on previous line) when button 0 or button 2 # is pressed and held. Button 0 will allow user to drag the object with orientation # change, button 2 will allow dragging but square will maintain fixed orientation. # The arGrabCondition specifies that a grab will occur whenever the value # of the specified button event # is > 0.5. self.setDrag( arGrabCondition( AR_EVENT_BUTTON, 0, 0.5 ), arWandRelativeDrag() ) def updateState( self, inputState ): arEffector.updateState( self, inputState ) tmp = arEffector.getBaseMatrix( self ) self.setMatrix( szgpov.ar_szgMatrixToPOV( tmp ) ) def getState(self): return self.getBaseMatrix().toTuple() def setState( self, stateTuple ): self.setMatrix( arMatrix4( stateTuple ) ) def __call__(self): return [str(self)] def __str__(self): return \ """#declare wand = box {<-.1,-.1,0>,<.1,.1,3> pigment {bozo color_map{ [0 color Red] [1 color Yellow]}} finish {ambient 1 diffuse 0} matrix <%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f>} object {wand} """ % \ szgpov.ar_povMatrixToTuple( self.getBaseMatrix() ) # ColoredSquare: a yellow square that changes green when 'touched' # (i.e. when the effector tip is within 1 ft.) and can be dragged around. # See szg/src/interaction/arInteractable.h. class POVColoredSphere(arPyInteractable): def __init__(self): # must call superclass init so that on...() methods get called # when appropriate. arPyInteractable.__init__(self) self.color = (1.,1.,0.) self.setMatrix( ar_translationMatrix(0.,5.,3.) ) def getState(self): return (self.color,self.getMatrix().toTuple()) def setState( self, stateTuple ): self.color = stateTuple[0] self.setMatrix( arMatrix4( stateTuple[1] ) ) def onTouch( self, effector ): self.color = (0.,1.,0.) def onUntouch( self, effector ): self.color = (1.,1.,0.) def __call__(self): return [str(self)] def __str__(self): theTuple = self.color + szgpov.ar_povMatrixToTuple( self.getMatrix() ) return \ """ #declare Ball = sphere { 0,0.2 pigment {color <%f,%f,%f>} matrix <%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f>} object {Ball}""" % theTuple class POVInteractScene(object): def __init__(self): object.__init__(self) # (period, amplitude, offset) tuples self.object = POVColoredSphere() self.effector = POVEffector() def update( self, app ): self.effector.updateState( app.getInputState() ) ar_processInteractionList( self.effector, [self.object] ) def getState(self): return (self.effector.getState(),self.object.getState()) def setState( self, stateTuple ): self.effector.setState( stateTuple[0] ) self.object.setState( stateTuple[1] ) def __call__(self): # See http:#www.povray.org/documentation/ . return [\ """ #include \"colors.inc\" #include \"shapes.inc\" #include \"textures.inc\" #include \"glass.inc\" #include \"consts.inc\" light_source { <0,18,-3> color rgb 1.3} //light_source { <0,6,-2> color White } //light_source { <-4,13,-3> color Red } background { color rgb 0.4 } fog { distance 50 color rgb <0.3,0.3,0.42> } """, \ str(self.object), str(self.effector), \ """ plane {y,-6 texture { pigment { gradient x pigment_map { [0.0 Black] [0.2 Black] [0.201 White] [0.999 White] [1.0 Black] } } scale 10 } texture { pigment { gradient z pigment_map { [0.0 Black] [0.2 Black] [0.201 color rgbt 1] [0.999 color rgbt 1] [1.0 Black] } } scale 10 } } #declare NumberOfPoints = 20; #declare RecursionPoints = array[NumberOfPoints] { <-2, 2,-2>, < 0, 2,-2>, < 2, 2,-2>, <-2, 2, 0>, < 2, 2, 0>, <-2, 2, 2>, < 0, 2, 2>, < 2, 2, 2>, <-2, 0,-2>, < 2, 0,-2>, <-2, 0, 2>, < 2, 0, 2>, <-2,-2,-2>, < 0,-2,-2>, < 2,-2,-2>, <-2,-2, 0>, < 2,-2, 0>, <-2,-2, 2>, < 0,-2, 2>, < 2,-2, 2> } #declare factor = 1; #declare focus = min_extent (Ball); // Constrain to inside of sponge frame. #declare focus = ; #declare r1=seed(1079); #declare RECURSION_MAX = 4; #declare RECURSION_MIN = 2; #macro CutOut (center, factor, RecLevel) #if (RecLevel < RECURSION_MAX) #local sampleCount = 0; #while (sampleCount < NumberOfPoints) #local samplePoint = center + factor * RecursionPoints[sampleCount]; #if (RecLevel < RECURSION_MIN) CutOut (samplePoint, factor * (1/3), RecLevel + 1) #else #local DistFromLocus = (1/1.6) * (VDist(samplePoint,focus)); #if ((RecLevel + DistFromLocus)} finish {ambient 0.35}// 0.45 + 0.25 *abs(center.x) specular 0 diffuse 0 } //scale 2 } #end // if #end // macro #declare Block = box { -1,1 scale 3 } //Scanned sponge: CutOut (<0,0,0>, 1, 1) #declare corners = array[8] { <-6,-6,-6>, <-6,-6, 6>, <-6, 6, 6>, <-6, 6,-6>, < 6, 6, 6>, < 6,-6, 6>, < 6,-6,-6>, < 6, 6,-6>} // Remainder, rendered at minimum recursion: //Frame: union { sphere {corners[0],0.3 texture {Gold_Texture} } sphere {corners[1],0.3 texture {Gold_Texture} } sphere {corners[2],0.3 texture {Gold_Texture} } sphere {corners[3],0.3 texture {Gold_Texture} } sphere {corners[4],0.3 texture {Gold_Texture} } sphere {corners[5],0.3 texture {Gold_Texture} } sphere {corners[6],0.3 texture {Gold_Texture} } sphere {corners[7],0.3 texture {Gold_Texture} } box { -1,1 scale 0.15 translate pigment { color rgb 1.1} } box { -1,1 scale 0.15 translate <-6, focus.y, -6> pigment { color rgb 1.1} } box { -1,1 scale 0.15 translate <-6, -6, focus.z> pigment { color rgb 1.1} } #declare MyGreyTex = texture {pigment {color rgb 0.6}} cylinder { corners[0], corners[1], 0.15 texture {MyGreyTex} } cylinder { corners[1], corners[2], 0.15 texture {MyGreyTex} } cylinder { corners[2], corners[3], 0.15 texture {MyGreyTex} } cylinder { corners[3], corners[0], 0.15 texture {MyGreyTex} } cylinder { corners[2], corners[4], 0.15 texture {MyGreyTex} } cylinder { corners[4], corners[5], 0.15 texture {MyGreyTex} } cylinder { corners[5], corners[1], 0.15 texture {MyGreyTex} } cylinder { corners[5], corners[6], 0.15 texture {MyGreyTex} } cylinder { corners[6], corners[7], 0.15 texture {MyGreyTex} } cylinder { corners[7], corners[3], 0.15 texture {MyGreyTex} } cylinder { corners[0], corners[6], 0.15 texture {MyGreyTex} } cylinder { corners[7], corners[4], 0.15 texture {MyGreyTex} } no_shadow scale 0.5 } """ ] if __name__=='__main__': import sys app = szgpov.POVApp( scene=POVInteractScene() ) if not app.init( sys.argv ): raise RuntimeError, 'app init failed.' if not app.start(): raise RuntimeError, 'app start failed.'