ZoomRotate

Help Contents ZoomRotate
picbutton always_rotate

ZoomRotate

With this little library you can zoom and rotate any 3D drawings with your mouse.

Usage: place the line
	run "zoomRotate
before your 3D drawings, but after loading "graphix", and then zoom with the right mouse button pressed, rotate with the left mouse button pressed, while and after the drawings take place.

Example: see Polyhedra-fractals (3d fractal) !

This library is under construction. (AM)

run "io

;local "mouse "radius "eye "focus "upwards "running "theta "phi "phiIncrement

make "radius 70
make "eye     vector radius 0 0
make "focus   vector 0 0 0
make "upwards vector 0 1 0

to ButtonEnd
	make "zoomRotate.onmousemove
;	KeyboardOff
;	WindowDelete "mywindow
	make "running "false
end

to ButtonReset
	make "theta 17
	make "phi 10
	make "phiIncrement 1
	mouse.up
	make "rotateStartPos :mouse.pos
	mouse.rotate
end

to updateView

;	StaticUpdate "staticzoomRotate.theta Se "theta: form :theta 4 0
;	StaticUpdate "staticzoomRotate.phi   Se "phi:   form :phi    4 0
		
	make "rotateStartPos :mouse.pos

	(make "eye 
		(vector
			:radius*(cos :theta)*(sin :phi)
			:radius*(sin :theta)
			:radius*(cos :theta)*(cos :phi)
		)
	)
	lookat :eye :focus :upwards
		
;	setTurtle -3	; some light rotation effects to make things look more dynamic
;	make "r 10000
;	setXYZ  ~
;		:r*(cos :theta)*cos :phi*0.95	~
;		:r*sin :theta   ~
;		:r*(cos :theta)*sin :phi*0.95
end

make "mouse
[	[]
	local "pos
	make "pos vector 0 0 0

	to up
		make "button "none
	end

	to downLeft
		make "button "left
		make "rotateStartPos :pos
	end

	to downRight
		make "button "right
		make "zoomStartPos :pos
	end

	to move
		if :button="left 
		[
			rotate
		]
		[
			if :button="right 
			[zoom]	; print MousePos]
		]
	end

	to zoom

		local "absBegin "absEnd "zoomFactor

		make "absBegin norm :zoomStartPos
		make "absEnd   norm :pos

		if :absBegin=0 [output]

		make "zoomFactor :absEnd/:absBegin

		make "radius :radius*:zoomFactor

		make "eye.x :eye.x*:zoomFactor
		make "eye.y :eye.y*:zoomFactor
		make "eye.z :eye.z*:zoomFactor

		lookat :eye :focus :upwards
	end

	to rotate
		local "x "y "x0 "y0

		make "x0 :rotateStartPos.x
		make "y0 :rotateStartPos.y

		make "x :pos.x
		make "y :pos.y

		make "theta :theta-100*(:y-:y0)
		make   "phi   :phi-100*(:x-:x0)

		updateView
	end
]

make "mouse mouse

to checkKeys
	if readkey?
	[
		local "ch
		make "ch readkey

		if :ch=:escape [ButtonEnd]
		if :ch=:return [ButtonReset]
	]
end

;	WindowCreate "main "mywindow [Zoom-Rotate] 300 200 100 120 []

;	StaticCreate "mywindow "staticzoomRotate.theta [theta:] 18 3 40 10
;	StaticCreate "mywindow "staticzoomRotate.phi [phi:] 25 13 40 10
;	StaticCreate "mywindow "staticzoomRotate.phiIncrement [phiIncrement:] 25 28 70 10

;	ScrollbarCreate "mywindow "scrollzoomRotate.phiInc 12 40 70 20 [make "phiIncrement (ScrollbarGet "scrollzoomRotate.phiInc)/100]
;	ScrollbarSet "scrollzoomRotate.phiInc 0 1000 100

;	ButtonCreate  "mywindow "myend "End 20 65 18 10 [ButtonEnd]
;	ButtonCreate  "mywindow "myforward "Reset 50 65 25 10 [ButtonReset]

;	StaticCreate "mywindow "staticHelp [Left Mouse Button: rotate] 8 80 90 10
;	StaticCreate "mywindow "staticHelp2 [Right Mouse Button: zoom] 5 90 90 10

make "mouse.pos vector 0 0 0
ButtonReset

;	setFocus [MSWLogo Screen]
;	KeyboardOn [zoomRotate.key]

;	MouseOn
;		[mouseDownLeft ] [mouseUp]
;		[mouseDownRight] [mouseUp]
;		[mouseMove]

to onmousemove :x :y :xp :yp :s
	make "mouse.pos vector :x :y 0
	mouse.move
end

to onmousedown :x :y :xp :yp :s
	if (:s=8)  and (:button<>"left)  [mouse.downLeft]
	if (:s=16) and (:button<>"right) [mouse.downRight]
	onmousemove :x :y :xp :yp :s
end

to onmouseup :x :y :xp :yp :s
	mouse.up
	onmousemove :x :y :xp :yp :s
end

ZoomRotate

Examples:

References:

picbutton always_rotate