TCylinder


Croquet-Teapot

Comment:

TCylinder generates an OGL generalized cylinder (cone).

Hierarchy:

ProtoObject
Object
TObject
TFrame
TPrimitive
TCylinder

Summary:

instance variables:

baseRadius capped height slices stacks topRadius

methods:

instance class
accessing initialize render no messages

Detail:

instance variables:

baseRadius
capped
height
slices
stacks
topRadius

instance methods:

accessing
baseRadius


	^ baseRadius.
baseRadius: br


	baseRadius _ br.
	self boundsChanged.
capped


	^ capped.
capped: bool


	capped _ bool.
frameBox


	| r |
	r _ baseRadius> topRadius ifTrue:[baseRadius] ifFalse:[topRadius].
	^ TBox min:(r negated@0.0@r negated) max:(r@height@r).
height


	^ height.
height: ht


	height _ ht.
	self boundsChanged.
scale: scl


	self translation: (self translation * scl).
	baseRadius _ baseRadius * scl.
	topRadius _ topRadius * scl.
	height _ height * scl.
slices


	^ slices.
slices: sl


	slices _ sl.
stacks

	^ stacks.
stacks: st

	
	stacks _ st.
topRadius


	^ topRadius.
topRadius: tr


	topRadius _ tr.
	self boundsChanged.

initialize
initBounds


	| location radius h2 |
	h2 _ height/2.0.
	h2 _ h2*h2.
	baseRadius < topRadius ifTrue:[radius _ topRadius*topRadius.] 
							ifFalse:[radius _ baseRadius * baseRadius.].
	radius _ (radius+h2) sqrt.
	location _ B3DVector3 x:0.0 y: height/2.0 z:0.0.
	boundSphere _ TBoundSphere localPosition: location radius: radius.
	boundSphere frame: self.

initialize

	super initialize.
	baseRadius _ 0.5.
	topRadius _ 0.0. "default is a cone"
	height _ 1.0.
	slices _ 8.
	stacks _ 4.
	capped _ true.

	^self.
		

render
pick: pointer


	| r1 r2 |
	(pointer pickCylinderFrom: B3DVector3 new radius: baseRadius 
		to: (B3DVector3 x:0.0 y: height z: 0.0) radius: topRadius) ifTrue:[^ true] ifFalse:[
			capped ifTrue:[
				r1 _ pointer pickSphere: 
					(B3DVector3 x:0.0 y: 0.0 z: 0.0) radiusSquared: baseRadius*baseRadius.
				r2 _ pointer pickSphere: 
					(B3DVector3 x:0.0 y: 0.0 z: height) radiusSquared: topRadius*topRadius.
				^ r1 | r2.
				].
			^ false.].
renderPrimitive: ogl

	| ringSin ringCos pi2 ax norm vert hval vval dy m u v du dv rBase hBase rTop hTop |

	ringSin _ FloatArray ofSize: slices+1.
	ringCos _ FloatArray ofSize: slices+1.
	vert _ B3DVector3 new.
	norm _ B3DVector3 new.
	pi2 _ Float pi *2.0.

	1 to: slices do:[ :index | 
		ax _ ((index) * pi2)/ slices.
		ringSin at:index put: ax sin.
		ringCos at:index put: ax cos.].
	ringSin at: slices+1 put: (ringSin at: 1).
	ringCos at: slices+1 put: (ringCos at: 1).

	baseRadius = topRadius ifTrue:[dy _ 0.0. m _ 1.0]
		ifFalse:[
		dy _ height/(baseRadius-topRadius).
		m _ 1/((1.0 + (dy*dy)) sqrt).
		dy _ dy*m.].
	hval _ height/stacks.
	vval _ (baseRadius-topRadius)/stacks.
	rBase _ baseRadius.
	hBase _ 0.0.
	rTop _ baseRadius - vval.
	hTop _ hval.
	du _ 1.0/slices.
	dv _ 1.0/stacks.
	v _ 0.0.
true ifTrue:[
	capped ifTrue:
		[ baseRadius = 0.0 ifFalse:[ "make the bottom cap here" 
				norm x: 0.0 y: -1.0 z: 0.0.
			    	ogl glNormal3fv: norm.
				ogl glBegin: GLTriangleFan.
				1 to: slices do:[ :ih |
					vert x: baseRadius* (ringSin at: 1+slices-ih) y:0.0 z: baseRadius*(ringCos at: 1+slices-ih).
					ogl glVertex3fv: vert.].
				ogl glEnd.
				
].
		  topRadius = 0.0 ifFalse:[ "make top cap here" 
				norm x: 0.0 y: 1.0 z: 0.0.
			    	ogl glNormal3fv: norm.
				ogl glBegin: GLTriangleFan.
				1 to: slices do:[ :ih |
					vert x: topRadius* (ringSin at: ih) y:height z: topRadius*(ringCos at: ih).
					ogl glVertex3fv: vert.].
				ogl glEnd.
				].
		].].

	1 to: stacks do:[ :iv |
		ogl glBegin: GLTriangleStrip.
		u _ 0.0.
		1 to: slices+1 do: [ :ih |
			norm x:m * (ringSin at: ih) y: dy z: m*(ringCos at: ih).
			ogl glNormal3fv: norm.
			ogl glTexCoord2f: u with: 1-(v+dv).
			vert x: rTop * (ringSin at: ih) y: hTop z: rTop *(ringCos at: ih).
			ogl glVertex3fv: vert.

			ogl glNormal3fv: norm.
			ogl glTexCoord2f: u with: 1-v.
			vert x: rBase * (ringSin at: ih) y: hBase z: rBase *(ringCos at: ih).
			ogl glVertex3fv: vert.

			u _ u+du.].
			ogl glEnd.
			v _ v+dv.
			rBase _ rTop.
			rTop _ rBase - vval.
			hBase _ hTop.
			hTop _ hBase + hval.].


class methods:

^top


- made by Dandelion -