TCamera


Croquet-Teapot

Comment:

The z value of the camera matrix actually points away from the direction of view. This seems to be a standard approach for OpenGL. 

instance variables:

viewPort
Typically the underlying morph in this implementation. Any valid OpenGL buffer should work.
	
bounds
Usually nil, which means that its actual value will be derived from the bounds of the current viewPort. bounds is the area that is rendered into. You can set this (#bounds:) to have an explicit hard value.

viewAngle
This is the view angle of the camera measured top to bottom. The default is 45 degrees, which means that the angle of the top and bottom edges of the rendered image are 45 degrees apart.

zNear, zFar
The near and far clipping planes in the graphics engine.

zScreen
This is the z distance from the camera that the virtual screen would be. That is, when you have an x,y location on the real screen, you can add the zScreen value (x,y,zScreen) and normalize which gives you an accurate pointing vector in 3D space.

length
Computed by initClipPlanes and used to render the test camera.

clipPlanes, clipPlanesTransform
clipPlanes are the four frustum clip planes defined in the cameras local orientation.
clipPlanesTransform are the four clipPlanes in the cameras global orientation frame. This is what is used to determine if objects are visible inside of the view frustum.

viewClip 
This is used to test the visibility culling of the camera. It makes the clipping planes visible. This is obsolete, but it may be useful again if we ever start playing with the object clipping code again. 

portalClip
This is an additional clip plane defined by a portal. Objects on the near side of the portal need to be clipped away as well. This is a 4x4 transform.

portalPlane
This is a 3D vector calculated from the portalClip.

inPortal
A flag indicating whether we are rendering a portals contents from this camera or not. This is used to supress certain tests (such as finding floors), and to determine that the avatar should or should not be rendered.

texture
Simply a texture that gets added to the default rendered camera. Not used anymore unless you really need to see the default camera.

killFrame
A flag to indicate that the current rendered image is junk and should not be rendered. This suppresses a swapBuffers call.

goToPortal, goToPortalTransform
When a camera is moved through a portal into a new space, the new portal is set in goToPortal and the new transform inside the new space. Actually, goToPortal can be any valid frame, we are only looking for the root frame.
goToTransform is the global transform that the camera is moved to in the new world. See #skidoo for actual use.

lookAt, lookUp
Vectors that are computed from the global transform. They are the z-vector emerging from the camera in the center of the screen, and the y vector pointing straight up from the cameras view. These are useful for creating new objects directly in front of the camera.

renderInterval
Defines the time between renderings in milliseconds.









 

Hierarchy:

ProtoObject
Object
TObject
TFrame
TGroup
TCamera

Summary:

instance variables:

bounds clipPlanes clipPlanesTransform currentSpace inPortal killFrame length portalClip portalPlane texture viewAngle viewClip viewPort zFar zNear zScreen

methods:

instance class
*Jana-Script CIM accessing fileIn/Out initialize render testing toys transform instance creation

Detail:

instance variables:

bounds
clipPlanes
clipPlanesTransform
currentSpace
inPortal
killFrame
length
portalClip
portalPlane
texture
viewAngle
viewClip
viewPort
zFar
zNear
zScreen

instance methods:

*Jana-Script
isPointerDown

	"Answer whether the pointer is currently pressed on aFrame"
	^self pointer isPointerDown
isPointerDownOn: aFrame

	"Answer whether the pointer is currently pressed on aFrame"
	^self pointer isPointerDownOn: aFrame

CIM
requestMeetingFrom: sender

	(self confirm: sender nickname, ' wants to join you.
Do you want to tell him where you are?') ifFalse:[^self].
	sender joinMeetingAt: self localTransform clone in: currentSpace.

accessing
aspect


^ (self bounds width/ self bounds height) asFloat.

avatar


	^ nil.
bounds


	bounds ifNotNil:[ ^ bounds.].
	^ 0@0 corner: (viewPort bounds bottomRight-viewPort bounds topLeft).
bounds:bnds


	bounds _ bnds.
	
clipPlanes


	^ clipPlanes.
clipPlanes: cp

	
	cp ifNotNil:[
		clipPlanes _ cp.].
currentSpace


	^currentSpace.
currentSpace: cs


	currentSpace _ cs.
debugOverlay


	^ nil.
frameLookAt: frame


	
	frame ifNotNil:[
		^ frame inverseGlobalOrientation localPointToGlobal: self lookAt negated.].
	^ nil.
hasAlpha


	^ viewClip.
inPortal


	^ inPortal.
inPortal: bool


"bool ifTrue:[Transcript show:'+'.]
ifFalse:[Transcript show:'-';cr.]."
	inPortal _ bool.
killFrame


	^ killFrame.
killFrame: bool

"Flag to let the system know not to reveal this frame, either because it is dirty or incomplete. Supresses the swapBuffer call."
	killFrame _ bool.
outVector


	^ self globalTransform column3.
	
pitch


	^self pitchYawRoll x
pointer


	^ nil.
pointer: pntr


	^ nil.
portalClip


	^ portalClip.
portalClip: pc


	portalClip _ pc.
	portalClip ifNotNil:[
		portalPlane _ (B3DVector3 x: portalClip a13  y:portalClip a23  z: portalClip a33) negated.].
roll

	^self pitchYawRoll z
roll: r

	| angles |
	angles := self pitchYawRoll.
	angles z: r.
	self pitchYawRoll: angles.
sideVector


	^ self globalTransform column1
	
texture


	^ texture.
texture: txtr


	texture _ txtr.
upVector


	^ self globalTransform column2.

	
viewAngle


	^ viewAngle.
viewAngle: va


	viewAngle _ va.
viewClip


	^ viewClip.
viewClip: bool


	viewClip _ bool.
viewPort
 

	^ viewPort.
yaw

	^self pitchYawRoll y
yaw: y

	| angles |
	angles := self pitchYawRoll.
	angles y: y.
	self pitchYawRoll: angles.
zFar


	^ zFar.
zFar: zf


	zFar _ zf.
zNear


	^ zNear.
zNear: zn


	zNear _ zn.
zScreen


	^ zScreen.
zScreen: zs


	zScreen _ zs.

fileIn/Out
exportOn: exporter

	self error:'Cannot export cameras'

initialize
activate

	Croquet world activeCamera: self
deactivate

	Croquet world activeCamera: nil.
frustumChanged

initClipPlanes


	| aspect |
	aspect _ self aspect. 
	length _ 1.0/(((viewAngle/2.0)*(Float pi/180.0))tan).

	clipPlanes _ B3DVector3Array ofSize: 4.
	
	clipPlanes at: 1 put: (B3DVector3 x: 0.0 y: length  z: 1)normalize negated.
	clipPlanes at: 2 put: (B3DVector3 x: 0.0 y: length negated z: 1)normalize negated.
	clipPlanes at: 3 put: (B3DVector3 x: length y:0.0 z: aspect)normalize negated.
	clipPlanes at: 4 put: (B3DVector3 x: length negated y:0.0 z: aspect)normalize negated.
initFrustum: ogl

	| radians bnds |

	bnds _ self bounds.
	radians _ (viewAngle/2.0)*(Float pi/180.0).
	ogl initFrustum: viewAngle bounds: ((bnds left@(viewPort bounds height - bnds bottom)) corner: (bnds right @ (viewPort bounds height - bnds top))) zNear: zNear zFar: zFar.
	zScreen _ (bnds height/2.0) / (radians tan).
	self frustumChanged.
initializeWithViewPort: vp


	super initialize.
	viewPort _ vp.
	viewAngle _ 45.0.
	zNear _ 0.5.
	zScreen _ 0.5. "give it any value - initFrustum gives it a real value"
	zFar _ 1500.0.
	localTransform _ B3DMatrix4x4 identity.
	self singleParent: true.
	viewClip _ true.
	self initClipPlanes.
	inPortal _ false.
	killFrame _ false.
	self visible: false.
	^self

render
postRender

preRender

render: ogl

	| vert origin scale sLength sAspect |

"This is deprecated code for sure, as there is no longer any way for a camera to render itself, but you never know if you will need to be able to visualize the viewport of a camera and this does just that."

	ogl glDisable: GLCullFace.
	origin _ B3DVector3 new.
	vert _ B3DVector3 new.

	scale _ 1.

	sLength _ scale * length negated.
	sAspect _ scale * self aspect.

	ogl glBegin: GLTriangles.

	ogl glNormal3fv: ((clipPlanes at:1) negated).
	ogl glVertex3fv: origin.
	vert x: sAspect y: scale z: sLength.
	ogl glVertex3fv: vert.
	vert x: sAspect negated y:  scale z: sLength.
	ogl glVertex3fv: vert.

	ogl glNormal3fv: ((clipPlanes at:2) negated).
	ogl glVertex3fv: origin.
	vert x: sAspect negated y:  scale negated z: sLength.
	ogl glVertex3fv: vert.
	vert x: sAspect y: scale negated z: sLength.
	ogl glVertex3fv: vert.

	ogl glNormal3fv: ((clipPlanes at:3) negated).
	ogl glVertex3fv: origin.
	vert x: sAspect y: scale negated z: sLength.
	ogl glVertex3fv: vert.
	vert x: sAspect y:  scale z: sLength.
	ogl glVertex3fv: vert.

	ogl glNormal3fv: ((clipPlanes at:4) negated).
	ogl glVertex3fv: origin.
	vert x: sAspect negated y:  scale z: sLength.
	ogl glVertex3fv: vert.
	vert x: sAspect negated y: scale negated z: sLength.
	ogl glVertex3fv: vert.
	ogl glEnd.

	ogl setCull.
	texture ifNotNil:[
		texture enable: ogl.
		ogl glBegin: GLQuads;
				glNormal3f: 0.0 with: 0.0 with: -1.0;
				glTexCoord2f:0.0 with: 0.0;	
				glVertex3f: sAspect negated with: scale with: sLength;
				glTexCoord2f:1.0 with:0.0;	
				glVertex3f: sAspect with: scale with: sLength;
				glTexCoord2f:1.0 with:1.0;	
				glVertex3f: sAspect with: scale negated with: sLength;
				glTexCoord2f:0.0 with:1.0;	
				glVertex3f: sAspect negated with: scale negated with:sLength;
			 glEnd.
		texture disable: ogl.].



renderAlpha: ogl

	| vert origin scale sLength sAspect |

	ogl glDisable: GLCullFace.
	origin _ B3DVector3 new.
	vert _ B3DVector3 new.
	
	scale _ 20.

	sLength _ scale * length negated.
	sAspect _ scale * self aspect.

	ogl glBegin: GLTriangles.

	ogl glNormal3fv: (clipPlanes at:1) negated.
	ogl glVertex3fv: origin.
	vert x: sAspect y: scale z: sLength.
	ogl glVertex3fv: vert.
	vert x: sAspect negated y:  scale z: sLength.
	ogl glVertex3fv: vert.

	ogl glNormal3fv: (clipPlanes at:2) negated.
	ogl glVertex3fv: origin.
	vert x: sAspect negated y:  scale negated z: sLength.
	ogl glVertex3fv: vert.
	vert x: sAspect y: scale negated z: sLength.
	ogl glVertex3fv: vert.

	ogl glNormal3fv: (clipPlanes at:3) negated.
	ogl glVertex3fv: origin.
	vert x: sAspect y: scale negated z: sLength.
	ogl glVertex3fv: vert.
	vert x: sAspect y:  scale z: sLength.
	ogl glVertex3fv: vert.

	ogl glNormal3fv: (clipPlanes at:4) negated.
	ogl glVertex3fv: origin.
	vert x: sAspect negated y:  scale z: sLength.
	ogl glVertex3fv: vert.
	vert x: sAspect negated y: scale negated z: sLength.
	ogl glVertex3fv: vert.
	ogl glEnd.

	ogl setCull.


renderView: ogl


	self preRender.
	ogl camera: self.
	ogl reset.
	ogl glEnable: GLDepthTest.
	ogl viewportOffset: Croquet world morph bounds origin.
	ogl clipRect: Croquet world morph bounds.
	ogl beginFrame: Croquet teaTime asFloat.
	ogl glEnable: GLLighting.
Croquet renderProtect: currentSpace in: 
[ :sp |
"------ set up the view frustum ------"
	self initFrustum: ogl. 
	sp renderClear: ogl.

	sp renderSpace: ogl.
].

	self renderOverlay: ogl.

	ogl endFrame.
	self postRender.
testBounds: bnds

	| position pposition d cp fsi |

	bnds ifNil: [^ true].
	position _ bnds globalPosition - self globalPosition.
"------- test to the near and far clip planes -------"
	d _ (self lookAt dot: position) negated.
	fsi _ Croquet world frameScaleInverse.
	d + bnds radius < (zNear*fsi) ifTrue:[^ false].
	d - bnds radius > (zFar*fsi) ifTrue:[^false].
	1 to: 4 do:[:i|
		cp := clipPlanesTransform at: i.
		(0 > ((cp dot: position)+bnds radius)) ifTrue:[ ^ false.]].

	" portalClip is the clipping plane of the portal if we happen to be rendering inside of one. "
	portalClip ifNotNil:[
		pposition _ bnds globalPosition - portalClip translation.
		0 > ((portalPlane dot: pposition)+ bnds radius) ifTrue:[ ^ false].
		].
^ true
testSphere: pos radius: radius

	| pposition position plane d  fsi |

	position _ pos - self globalPosition.
"------- test to the near and far clip planes -------"
	plane _ self lookAt.
	d _ ( plane dot: position ) negated.
	fsi _ Croquet world frameScaleInverse.
	d + radius < (zNear*fsi) ifTrue:[^ false].
	d - radius > (zFar*fsi) ifTrue:[^false].
"------- test to the four clip planes -------"
	1 to: 4
		do: [:index | 0 > (((clipPlanesTransform at:index)
								dot: position) + radius)
				ifTrue: [^ false]].
	" portalClip is the clipping plane of the portal if we happen to be rendering inside of one. "
	portalClip ifNotNil:[
		pposition _ position - portalClip translation.
		plane _ B3DVector3 x: portalClip a13 negated y:portalClip a23 negated z: portalClip a33 negated.
		0 > ((plane dot: pposition)+ radius) ifTrue:[^ false].
	].
	^ true

testing
isCamera


	^ true.

toys
jump


	self translation: self translation + (B3DVector3 x: 0 y: 20 z: 0).

transform
clipPlanesTransform


	^ clipPlanesTransform.
clipPlanesTransform: cpt


	clipPlanesTransform _ cpt.
doScale: scale


	frameChildren ifNotNil:[
		frameChildren do:[ :fc | fc scale: scale.]].
scale: scale


	self doScale: scale.
transformClipPlanes

	|  trans |
	clipPlanesTransform _ Array new: 4.
	trans _ self globalOrientation.
	1 to: 4 do: [:index | 
		clipPlanesTransform at: index put: (trans localPointToGlobal: (clipPlanes at: index))].

	
updateTransform


class methods:

^top


- made by Dandelion -