CameraViewport

Blitz3D Forums/Blitz3D Programming/CameraViewport

Warren(Posted 2004) [#1]
I don't think I understand this command fully.

What I would LIKE to do is set up a rectangle and have Blitz clip all drawing to within that rectangle.

Is CameraViewport the way to do this? Is there a better way? CameraViewport will probably work but it's sort of weird trying to get it to behave the way I want.


_PJ_(Posted 2004) [#2]
YEah CameraViewport sets the frame for the camera view rendering.

What is weird about it?

If you are using commands like ProjectedX, ProjectedY, then these are relative to the viewport, not graphics resolution (if I remember correctly!)


Rob Farley(Posted 2004) [#3]
Cameraviewport doesn't clip as such, if you had a cameraviewport camera,0,0,320,240 and a cameraviewport,0,0,640,480 both would show the same thing but the 1/4 screen one would be a low res effectivly.


Warren(Posted 2004) [#4]
Rob

So there's no way to say, "I want you to draw the scene as normal, but I only want to see this subsection of it"? Not scaled to a rectangle, but clipped to it. Am I explaining this right?

Am I going to have to draw the scene and copyrect the section that I want?


fredborg(Posted 2004) [#5]
You can do it, if the section you want to render is at the center of the viewport. Then you need to compensate for different viewport aspects and the camerazoom.

I'm not sure it's possible if you want to show something that isn't at the center of where the camera is pointing.

Did that make any sense?

I'll put together a small example!


Warren(Posted 2004) [#6]
Well, I'm locked at 640x480 for this game.

I want to draw the game board at 208,48 on the screen with a 384x384 size. I want things to fly in from the sides of the game board, but not overwrite anything outside of that square. I want them contained and clipped to that area.

I suppose I'll try using some buffers and a copyrect or two.


fredborg(Posted 2004) [#7]
Oh, well that's no problem at all! I thought you wanted to do something else :)

Just use: CameraViewport camera,208,48,384,384

What's the problem with that?

Remember that if you use camerapick or cameraproject you need to compensate for the top left corner of the viewport!

So if you do a CameraPick camera,mousex(),mousey() it will pick at the wrong place. You need to use:

CameraPick camera,mousex()-208,mousey()-48


_PJ_(Posted 2004) [#8]
(Thats waht I said originally :) )


Mustang(Posted 2004) [#9]
I thought once too that CameraViewport would just clip the world / camera view or something... I wanted to use it for "widescreen" effect and save some drawing time (top and bottom "border") as well. Then I discovered that it basically just squuezed the view so that the resulting image was... well, squeezed. Now I have black "sprite" borders, stupid way to do "widescreen"... showing less and drawing more. :)


fredborg(Posted 2004) [#10]
This shows how you can compensate for any aspect ratio.
Graphics3D 640,480,0,2
SetBuffer BackBuffer()

clipx = 208
clipy = 48
clipw = 384
cliph = 384
origzoom# = 1.0
origclipw# = clipw

camera = CreateCamera()

For i = 0 To 10
	m = CreateCube()
	PositionEntity m,Rnd(-10,10),Rnd(-10,10),Rnd(10,50)
Next

For i = 0 To 10
	m = CreateSphere()
	PositionEntity m,Rnd(-10,10),Rnd(-10,10),Rnd(10,50)
Next


Repeat
	If MouseDown(1)
		If MouseX()-clipx>10 Then clipw = MouseX()-clipx
		If MouseY()-clipy>10 Then cliph = MouseY()-clipy
	EndIf
	If MouseDown(2)
		clipw = clipw+(clipx-MouseX())
		cliph = cliph+(clipy-MouseY())
		clipx = MouseX()
		clipy = MouseY()
	EndIf
		
	Cls
	
	; Compensate for viewport aspect!
	zoom# = origzoom#*(origclipw/clipw)

	Rect clipx-1,clipy-1,clipw+2,cliph+2,False
	CameraViewport camera,clipx,clipy,clipw,cliph
	CameraZoom camera,zoom
	
	RenderWorld
	
	Text GraphicsWidth()/2,0,"Left MB - Set Top Left ("+clipx+","+clipy+") | Right MB - Set Bottom Right ("+(clipx+clipw)+","+(clipy+cliph)+")",True,False
	Flip
	
Until KeyHit(1)



Warren(Posted 2004) [#11]
Thanks Fred! I'll give that a go when I get home tonight...

What's the problem with that?

I'm using screen aligned quads to do psuedo 2D graphics, and when I change the camera viewport they tend to slide around and not be in right place and such. Your code seems interesting though. As I said, I'll check it out later tonight...


Warren(Posted 2004) [#12]
Mustang

My kingdom for a simple clipper. :)


fredborg(Posted 2004) [#13]
I changed it so it should fully compensate for different aspect ratios now :)


_PJ_(Posted 2004) [#14]
I'm using screen aligned quads to do psuedo 2D graphics, and when I change the camera viewport they tend to slide around and not be in right place and such.


What do you mean - Screen Aligned?
Depends whether aligned to GraphicsWidh()/Height() or ProjectedX etc...

-------------------

As far as your frame goes, why not:

CameraViewport camera,0,(GraphicsHeight()/ratio#),GraphicsWidth(),(GraphicsHeight()-(GraphicsHeight()/ratio#))

CameraZoom camera,(ratio#/(15*(GraphicsHeight()/GraphicsWidth())))


A value of ratio#=15 is 'standard'.


Warren(Posted 2004) [#15]
I should be able to piece together something that works from this info guys, thanks!

It also occured to me halfway to the office that I'm scaling my 2D vertices to a virtual 640x480 screen. That's screwing things up as well and explains why my drawing swims around the smaller viewport depending on the resolution and ratios of it.

Ah, fun!


_PJ_(Posted 2004) [#16]
I would rely on some nifty variables along the lines of:

gwidth=GraphicsWidth()
gheight=GraphicsHeight()

camX=(gwidth/4)
camY=(gheight/4)
camHeight=(gheiht/2)
camWidth=(gwidth/2)

cam_ratio# = (1 / ((Gwidth/camX) * (Gheight/camY)))

;camX,camY= Top Left cameraView
;CamX+camWidth,CamY+CamHeight= Bottom Right cameraView



So what would be at x,y on normal 2D screen would be:

Rect camX+(x*cam_ratio#),(CamY*cam_ratio#),XX * cam_ratio#, YY * cam_ratio#,1,1


Warren(Posted 2004) [#17]
Well, I decided to write my own clipping routine instead of playing with the viewport any longer and it seems to do what I want it to.

Thanks anyway, guys!