TPortal3D


Croquet-Teapot

Comment:

TPortal3D are a kind of 3D portal. They render a miniature version of a space (or a subset of it). The content of this portal can be manipulated normally.



Hierarchy:

ProtoObject
Object
TObject
TFrame
TPortal3D

Summary:

instance variables:

angle boundSphere boundsChanged clipBox cube inPortal offset scale scaleInverse toSpace

methods:

instance class
accessing initialize render stepping testing instance creation

Detail:

instance variables:

angle
boundSphere
boundsChanged
clipBox
cube
inPortal
offset
scale
scaleInverse
toSpace

instance methods:

accessing
boundSphere


	boundsChanged ifTrue:[
		self initBounds. 
		boundsChanged _ false.
		].
	^ boundSphere.
boundingBox


	^ clipBox.
extent


	| ext |
	ext _ clipBox extent.
	^ ext x@ ext y.
extent: ext


	| clip clipN |
	clip _ ext x/2.0.
	clipN _ clip negated.
	self translation:( B3DVector3 x:0 y:0 z:0 ).
	clipBox _ TBox min: (B3DVector3 x:clipN y:clipN z:clipN) max: (B3DVector3 x:clip y: clip z: clip).
	cube extent: clipBox extent.
	
offset
 

	^ offset
offset: os


	offset _ os.
scale


	^ scale.
scale: scl


	scale _ scl.
	scaleInverse _ 1.0/scale.
space


	^ toSpace.
space: spc


	toSpace _ spc.

initialize
initBounds


	boundSphere _ TBoundSphere localPosition: clipBox center radius: (clipBox diagonal)/2.
	boundSphere frame: self.

initialize


	| txtr mat |
	super initialize.
	inPortal _ false.
	cube _ TCube new.
	self addChild: cube.
	txtr _ TTexture
				new initializeWithFileName: 'box.png'
				mipmap: true
				shrinkFit: false.

	mat _ TMaterial new.
	mat ambientColor: #(0.5 0.7 0.7 0.05) asFloatArray.
	mat diffuseColor: #(0.5 0.7 0.7 0.05) asFloatArray.
	mat cullFace: false.
	mat texture: txtr.
	cube material: mat.
	scale _ 0.05.
	angle _ 0.0.
	self objectName:'Portal3D'.
	boundsChanged _ true.
	offset _ B3DVector3 x: 0.0 y: 0.0 z: 0.0.
	self extent:(B3DVector3 x:4 y:4 z:4).
	^self
initializeWithSpace: spc


	self initialize.
	toSpace _ spc.

" set the initial offset value to the location of the activeCamera, but about two/thirds of the way down the window."
	offset _ Croquet world activeCamera globalPosition negated - (0@((self extent y/scale)/6.0)@0).
	^self

render
disableClipPlanes: ogl


	ogl glDisable: GLClipPlane1.
	ogl glDisable: GLClipPlane2.
	ogl glDisable: GLClipPlane3.
	ogl glDisable: GLClipPlane4.
	ogl glDisable: GLClipPlane5.
enableClipPlanes: ogl

	
"------ enableClipPlanes is used to clip the TSpace inside the 3DPortal to a sub-cube inside the portal. This ensures that objects don't get rendered outside of the portal, which would look bad and be confusing.------"
	| equation |

"------ We need to do it this way, because Squeak does not directly support doubles and gllClipPlane requires an array of same. ------"

	equation _ ExternalData fromHandle: (ExternalAddress allocate: 8*4) type:ExternalType double.
	
	equation getHandle doubleAt: 1 put:  1.0.
	equation getHandle doubleAt: 9 put: 0.0.
	equation getHandle doubleAt: 17 put: 0.0.
	equation getHandle doubleAt: 25 put: clipBox max x.
	ogl glClipPlane: GLClipPlane1 with: equation.
	ogl glEnable: GLClipPlane1.

	equation getHandle doubleAt: 1 put: -1.0.
	equation getHandle doubleAt: 9 put: 0.0.
	equation getHandle doubleAt: 17 put: 0.0.
	equation getHandle doubleAt: 25 put: clipBox min x negated.
	ogl glClipPlane: GLClipPlane2 with: equation.
	ogl glEnable: GLClipPlane2.

	equation getHandle doubleAt: 1 put: 0.0.
	equation getHandle doubleAt: 9 put: 0.0.
	equation getHandle doubleAt: 17 put: 1.0.
	equation getHandle doubleAt: 25 put: clipBox max z.
	ogl glClipPlane: GLClipPlane3 with: equation.
	ogl glEnable: GLClipPlane3.

	equation getHandle doubleAt: 1 put: 0.0.
	equation getHandle doubleAt: 9 put: 0.0.
	equation getHandle doubleAt: 17 put: -1.0.
	equation getHandle doubleAt: 25 put: clipBox min z negated.
	ogl glClipPlane: GLClipPlane4 with: equation.
	ogl glEnable: GLClipPlane4.

	equation getHandle doubleAt: 1 put: 0.0.
	equation getHandle doubleAt: 9 put: -1.0.
	equation getHandle doubleAt: 17 put: 0.0.
	equation getHandle doubleAt: 25 put: clipBox min y negated.
	ogl glClipPlane: GLClipPlane5 with: equation.
	ogl glEnable: GLClipPlane5.
	equation free.
render: ogl

	| rval currentCameraTrans currentPointerTrans cpt ip ac ct globalTrans |

	rval _ 0.
"Can't recurse into TPortal3D objects because of the limitation in number of clipping planes. Maybe if I figure out a different way to do it later..."
	ogl inPortal3D ifTrue:[^ rval].
	ogl inPortal3D: true.

	toSpace ifNil:[^ 0 ].
	inPortal ifTrue:[^ 0 ].
	inPortal _ true.

	ogl glPushMatrix. 
	toSpace pushAlphaObjects.

	self enableClipPlanes: ogl.
	ac _ ogl camera.
	ip _ ac inPortal.
	ac inPortal: true.
	cpt _ ac clipPlanesTransform.
	ct _ toSpace currentTransform.
	toSpace currentTransform: nil.
	currentCameraTrans _ ac globalTransform copy.
	ac  globalTransform: ((self globalTransform composeWith: (B3DMatrix4x4 identity translation: offset *scale))orthoNormInverse composeWith: ac globalTransform).
	ac globalPosition: ac globalPosition/scale.
	ac pointer ifNotNil:[
		currentPointerTrans _ ac pointer globalTransform copy.
		ac pointer globalTransform: (ac globalTransform composeWith: ac pointer localTransform).
		ac downPointer setAutomatic: false.
	].
	ac transformClipPlanes.
	Croquet world frameScale: scale.
	ogl glScalef: scale with: scale with: scale.
	ogl glTranslatef: offset x with: offset y with: offset z.
	rval _ toSpace renderFrame: ogl parent: toSpace root: toSpace.

	toSpace hasAlphaObjectsToRender ifTrue:[
		globalTrans _ B3DMatrix4x4 new.
		ogl glGetFloatv: GLModelviewMatrix with: globalTrans.
		globalTrans _ globalTrans transposed.
		ogl glLoadMatrixf: globalTrans  transposed.
		toSpace renderSpaceAlpha: ogl transform: (globalTrans inverseTransformation). 
		].
	Croquet world frameScale: 1.0.
	ac globalTransform: currentCameraTrans.
	ac pointer ifNotNil:[
		ac pointer globalTransform: currentPointerTrans.
		ac downPointer setAutomatic: true.
		].
	ac clipPlanesTransform: cpt.
	ac inPortal: ip.
	toSpace currentTransform: ct.
	self disableClipPlanes: ogl.
	toSpace popAlphaObjects.
	inPortal _ false.
	ogl inPortal3D: false.
	ogl glPopMatrix.
	^ rval.

stepping
step

wantsSteps


	^ false.

testing
isComponent


	^ true.

class methods:

^top


- made by Dandelion -