More boring zooming questions...

Blitz3D Forums/Blitz3D Programming/More boring zooming questions...

hollifd(Posted 2008) [#1]
I have a couple of zooming related questions...

Question 1:
I need to know how I can zoom towards the current position of the mouseX() and MouseY() using the center mouse button.

I use ProEngineer CAD software at work and it has this zoom ability. I am trying to build that type of zoom using Blitz. No matter where I place the mouse on the screen, ProEngineer will zoom to that point when scrolling the middle mouse wheel.

Question 2:
In ProEngineer, when I zoom in, everything appears flat on the screen but in Blitz, objects change shape as I zoom in, out, up or down.

For example:
Suppose I have a cube 1 unit wide in X axis, 1 unit tall in Y axis, and 12 units deep in Z axis.

In ProEngineer, I can zoom in or out and the cube that is displayed on the screen always stays 1 unit wide by 1 unit tall. It just gets bigger or smaller depending upon if I am zooming in or out. I never see the depth of the cube.

In Blitz, when I zoom in or out, I can sometimes see the depth of the cube depending upon where the cube is on the screen.

Is it possible to have all objects appear flat on the screen while zooming in or out like they do in ProEngineer? Ideally, I would want all of my objects to always appear flat on the screen no matter how far away they may be from the center of my screen.

Question 3:
Not related to zooming...
How do I post code to this sight and have all of the indentation remain? When I post my code, all of the code is left justified and it looks very unreadable.


I am sure these are very boring questions for game programmers but I am building a machine simulator for use at work and I am not really a programmer so I am having to learn a bunch on my own.


Thanks for any advice you may have,

David

I mistakenly posted this in the regular forum. I meant to post in the beginners forum and do not know how to move it. Perhaps someone else can move it for me.


Ross C(Posted 2008) [#2]
Point one for me, would involve putting a pivot at the mouses x and y position. Since you seem to be working in 2d, this should be easy to do. When zooming in, you want to do the following:

Point the camera at the pivot.
Move the camera towards the pivot.
Restore the cameras default rotation so it faces the drawing board again. Doing that inbetween renders, you won't notice the steps that took place.

Point 2: You will need to use orthographic camera project mode. A little tricky to get the camera zoom value correct to show everything you want to show, but it will stop you seeing the depth of your objects.

Point 3:

[ code ]

code here!

[ /code ]

If it's a long piece of code, use:

[ codebox ]

code here!

[ /codebox ]

No spaces betwwen the brackets though :o)

I still think your better doing this in 2d :o)


Stevie G(Posted 2008) [#3]
I don't have time to explain 1. right now but ...

2. You could use orthagonal camera, cameraprojmode 2 if I remember. Z-ordering gets a bit screwed up with this though. Alternatively, set the camera far away from the scene with an appropriate near and far range and camerzoom right in to get a more of less ortho view. There are little or no z-ordering issues using this method.

3. Use

{code}
Your code goes here
{/code}

or

{codebox}
Your code goes here
{/codebox}

but

replace the {} with [].

Stevie

EDIT: FFS Ross!!! You always seem reply 2 secs before me ;)


Ross C(Posted 2008) [#4]
creepy ;o)


Stevie G(Posted 2008) [#5]
Great Scots think alike and all that!


hollifd(Posted 2008) [#6]
Thanks guys!

I will give those ideas a try.

David


hollifd(Posted 2008) [#7]
Thanks guys. The orthagonal camera worked out perfectly for my needs.

I could not figure out the "zooming to my mouse position". However, it is not critical for what I am trying to do. Maybe I will try again later after I get a little further with my learning.

Thanks again,

David


Ross C(Posted 2008) [#8]
I'll knock together something for you.


Ross C(Posted 2008) [#9]
This isn't perfect, as it zooms into the corner of the zoom point. The football is on, so i'll maybe see if i can get it working properly.

ARROW keys to move the zoom point. key "1" to zoom in. Key "2" to zoom out.

Graphics3D 800,600
SetBuffer BackBuffer()

Global camera = CreateCamera()
PositionEntity camera,0,0,-10

Global light = CreateLight()


Global zoom_piv = CreateSphere()
ScaleEntity zoom_piv,0.3,0.3,0.3

Global plane = CreatePlane()
TurnEntity plane,-90,0,0

Global plane_texture = CreateTexture(64,64)
SetBuffer TextureBuffer(plane_texture)
Color 128,128,255
Rect 0,0,64,64
Color 64,64,255
Rect 1,1,62,62

SetBuffer BackBuffer()

EntityTexture plane,plane_texture


While Not KeyHit(1)

	If KeyDown(203) Then MoveEntity zoom_piv,-0.1,0,0
	If KeyDown(205) Then MoveEntity zoom_piv,0.1,0,0
	If KeyDown(200) Then MoveEntity zoom_piv,0,0.1,0
	If KeyDown(208) Then MoveEntity zoom_piv,0,-0.1,0
	
	If KeyDown(2) Then
		zoom_in()
	ElseIf KeyDown(3) Then
		zoom_out()
	End If
	
	UpdateWorld
	RenderWorld
	Flip
	
Wend

Function zoom_in()

	PointEntity camera,zoom_piv
	If EntityDistance#(zoom_piv,camera)>1.9 Then
		MoveEntity camera,0,0,EntityDistance#(zoom_piv,camera)/30.0
	End If
	RotateEntity camera,0,0,0
	
End Function

Function zoom_out()

;	PointEntity camera,zoom_piv
	If EntityDistance(zoom_piv,camera)<10 Then
		MoveEntity camera,0,0,-EntityDistance#(zoom_piv,camera)/30.0
	End If
	RotateEntity camera,0,0,0
	
End Function



hollifd(Posted 2008) [#10]
Thanks Ross C!

It works good enough for me. I changed the zoom speed to be faster and I hooked it up to my middle mouse wheel so I could use the wheel for zooming in and out.
Here is the code that I used for detecting the mousewheel. It may not be as efficient as it could be and it may not be the right way to do at all. If you know of a better way, I would love to know it...


CurrentMouseZ# = MouseZ()
IF CurrentMouseZ# < SaveMouseZ# then
     SaveMouseZ# = CurrentMouseZ
     zoom_in()
END IF

IF CurrentMouseZ# > SaveMouseZ# then
     SaveMouseZ# = CurrentMouseZ
     zoom_in()
END IF



I really want it to zoom in to my mouse position. How do I make the zoom_pivot always follow my mouse pointer rather than having to use the arrow keys to move the zoom_pivot? Does that complicate things too much?

Thanks,

David


Ross C(Posted 2008) [#11]
No, it should be easy enough :o) I get home in an hour.


Ross C(Posted 2008) [#12]
Ok, done it. This uses camerapick() to get the 3d location of the mouse, as if it was projected onto the plane working surface. It then positions the pivot (visualised as a white sphere), at the mouse co-ords.

Now, because when you zoom in, the mouse co-ords remain the same, this causes a problem. The zoom will constantly try and centre the screen. So you have to adjust the mouse pointer too. So, i'm using cameraproject directly after my zoom code, in the "zoom_in" function.

What this does it take an entity in the 3d world, and return it's 2d co-ords on screen. So after the zoom, the pivot moves closer to the centre of the camera. I project it's co-ords into 2d, and move the mouse to that location.

Function zoom_in()

	PositionEntity zoom_piv,EntityX(zoom_piv,True),EntityY(zoom_piv,True),EntityZ(camera)
	If EntityDistance(camera,zoom_piv) > 0.1 Then
		PointEntity camera,zoom_piv
		MoveEntity camera,0,0,EntityDistance#(zoom_piv,camera)/15
	End If
	PositionEntity zoom_piv,EntityX(zoom_piv,True),EntityY(zoom_piv,True),0

	PointEntity camera,zoom_piv
	If EntityZ(camera)<-1.9 Then
		MoveEntity camera,0,0,EntityDistance#(zoom_piv,camera)/20
		If EntityZ(camera)>-1.9 Then
			PositionEntity camera,EntityX(camera,True),EntityY(camera,True),-1.9
		End If
	End If
	
	RotateEntity camera,0,0,0
	
	CameraProject(camera,EntityX(zoom_piv,True),EntityY(zoom_piv,True),EntityZ(zoom_piv))
	MoveMouse ProjectedX#(),ProjectedY#()
	
End Function


The first part of that function moves the camera so the pivot is closer to the centre of the camera. It cuts off if it is within 0.1 blitz units, to stop it moving for so long.

The second part of the function is the same, except it moves the camera in more, actually zooming.

The third part of the function is the camera project stuff i explained above.

Full code:

Graphics3D 800,600
SetBuffer BackBuffer()

Global camera = CreateCamera()
PositionEntity camera,0,0,-10

Global light = CreateLight()


Global zoom_piv = CreateSphere()
ScaleEntity zoom_piv,0.3,0.3,0.3

Global plane = CreatePlane()
TurnEntity plane,-90,0,0
EntityPickMode plane,2

Global plane_texture = CreateTexture(64,64)
SetBuffer TextureBuffer(plane_texture)
Color 128,128,255
Rect 0,0,64,64
Color 64,64,255
Rect 1,1,62,62

SetBuffer BackBuffer()

EntityTexture plane,plane_texture


While Not KeyHit(1)

	If KeyDown(203) Then MoveEntity zoom_piv,-0.1,0,0
	If KeyDown(205) Then MoveEntity zoom_piv,0.1,0,0
	If KeyDown(200) Then MoveEntity zoom_piv,0,0.1,0
	If KeyDown(208) Then MoveEntity zoom_piv,0,-0.1,0
	
	If KeyDown(2) Then
		zoom_in()
	ElseIf KeyDown(3) Then
		zoom_out()
	End If
	
	CameraPick camera,MouseX(),MouseY()
	PositionEntity zoom_piv,PickedX(),PickedY(),0
	
	UpdateWorld
	RenderWorld
	Flip
	Delay 20
	
Wend

Function zoom_in()

	PositionEntity zoom_piv,EntityX(zoom_piv,True),EntityY(zoom_piv,True),EntityZ(camera)
	If EntityDistance(camera,zoom_piv) > 0.1 Then
		PointEntity camera,zoom_piv
		MoveEntity camera,0,0,EntityDistance#(zoom_piv,camera)/15
	End If
	PositionEntity zoom_piv,EntityX(zoom_piv,True),EntityY(zoom_piv,True),0

	PointEntity camera,zoom_piv
	If EntityZ(camera)<-1.9 Then
		MoveEntity camera,0,0,EntityDistance#(zoom_piv,camera)/20
		If EntityZ(camera)>-1.9 Then
			PositionEntity camera,EntityX(camera,True),EntityY(camera,True),-1.9
		End If
	End If
	
	RotateEntity camera,0,0,0
	
	CameraProject(camera,EntityX(zoom_piv,True),EntityY(zoom_piv,True),EntityZ(zoom_piv))
	MoveMouse ProjectedX#(),ProjectedY#()
	
End Function

Function zoom_out()

	If EntityZ(camera)>-10 Then
		MoveEntity camera,0,0,-EntityDistance#(zoom_piv,camera)/20.0
		If EntityZ(camera)<-10 Then
			PositionEntity camera,EntityX(camera,True),EntityY(camera,True),-10
		End If
	End If
	
	RotateEntity camera,0,0,0
	
End Function



hollifd(Posted 2008) [#13]
Ross C,

That works perfectly for me. Thanks for the explanation too.

Also, Thanks for the explanation of the "Type, End Type" in one of your posts to me in the Beginner's Forum. I am redoing some of my code to use that and it is making my life much simpler and code a lot shorter.

I had seen that before but never took the time to learn more about it. Too bad for me.

Have a good evening? I do have more questions on different topics but I'll try not to bug you again for a while.

Thanks again for all of your time and help,



David


Ross C(Posted 2008) [#14]
Yep types are great :o) When i first came to blitz, i had previous programmed on the commodore, and being the stubborn person i am, refused to use types for a while. Even refused to use functions for a long time, rather using lots of global variables and subroutines via gosub.

Nice to see you picking them up :o)

Good evening to you too sir!