monkey2 icon indicating copy to clipboard operation
monkey2 copied to clipboard

Camera viewport always 640 x 480

Open DruggedBunny opened this issue 7 years ago • 7 comments

Camera viewport seems to be always 640 x 480 here:


#Import "<std>"
#Import "<mojo3d>"

Using std..
Using mojo..
Using mojo3d..

' ----------------------------------------
' Application name...
' ----------------------------------------

Global AppName:String = "My 3D Game"

Class Game Extends Window
	
	Const SHIFT_BOOST:Float = 5.0
	
 	Field camera_boost:Float = 1.0
 	
	' Basic 3D scene requirements...
	
	Field scene:Scene
	Field camera:Camera
	Field light:Light
 
	' Test cube...
	
	Field cube:Model
	
	Method New (title:String, width:Int, height:Int, flags:WindowFlags)
		
		Super.New (title, width, height, flags)
		
		scene = Scene.GetCurrent () ' Important!
		
		camera = New Camera
 
			' Camera settings...
			
			camera.Near = 0.1
		
			' Camera position...
			
			camera.Move (0, 0, -2)
		
			'camera.Viewport = Window.Rect
			
		light = New Light
		
			light.Move (-10, 10, -10)
		
		cube = New Model (Mesh.CreateBox (New Boxf (-0.5, -0.5, -0.5, 0.5, 0.5, 0.5)), New PbrMaterial (Color.Aluminum))
		
			cube.Rotate (0, 45, 0)
		
	End
	
	Method UpdateGame:Void ()
	End
	
	Method OnRender (canvas:Canvas) Override
 
	 	ProcessInput ()
 		UpdateGame ()
 
		cube.Rotate	(0.0, 1.0, 0.0)
 		
		' Tell app to draw frame when ready...
		
		RequestRender ()
		
		' Render scene to canvas (passed to OnRender by mojo), from camera...
		
		scene.Render (canvas)

		canvas.DrawText ("VP:" + camera.Viewport, 20, 20)
		
	End

	Method ProcessInput:Void ()

		If Keyboard.KeyHit (Key.Space) Then light.CastsShadow = Not light.CastsShadow

		If Keyboard.KeyDown (Key.LeftShift)
			camera_boost = SHIFT_BOOST
		Else
			camera_boost = 1.0
		Endif
		
		If Keyboard.KeyHit (Key.Escape) Then App.Terminate ()
		
		If Keyboard.KeyDown (Key.A)
			camera.Move (0.0, 0.0, 0.1 * camera_boost)
		Endif
 
		If Keyboard.KeyDown (Key.Z)
			camera.Move (0.0, 0.0, -0.1 * camera_boost)
		Endif
 
		If Keyboard.KeyDown (Key.Left)
			camera.Rotate (0.0, 1.0, 0.0)
		Endif
 
		If Keyboard.KeyDown (Key.Right)
			camera.Rotate (0.0, -1.0, 0.0)
		Endif
 
		If Keyboard.KeyDown (Key.Up)
			camera.Rotate (1.0, 0.0, 0.0, True)
		Endif
 
		If Keyboard.KeyDown (Key.Down)
			camera.Rotate (-1.0, 0.0, 0.0, true)
		Endif

	End

End
 
Function Main ()
 
	' Windowed mode...
	
'	Local width:Int			= 640
'	Local height:Int		= 480
'	Local flags:WindowFlags	= WindowFlags.Center
	
	' Full-screen mode (comment out above)...
	
	Local width:Int			= 1024
	Local height:Int		= 768
	Local flags:WindowFlags	= WindowFlags.Center'Fullscreen
	
	New AppInstance
	
	New Game (AppName, width, height, flags)
	
	App.Run ()
		
End

Calling camera.Viewport = Window.Rect (commented out above) fixes it.

DruggedBunny avatar May 05 '18 15:05 DruggedBunny

Calling camera.Viewport = Window.Rect (commented out above) fixes it.

That was helpful. :D

Although, resizing Camera.Viewport in real time, say 60 FPS, seems cpu intensive and slow. Monkey-v2018.05

I should add that is in conjunction with Camera.Render(Canvas) instead of Scene.Render(Canvas) as used above.

RichardBetson avatar Jun 04 '18 11:06 RichardBetson

You shouldn't have to do it every frame! I've done it in the New [window] setup here, just the once... think it should just 'stick' from there. At worst, you would maybe check for a window resizing event* and call again, but I don't think even that would be necessary...

* I'm guessing that's "OnMeasure", but not too sure on that point...

DruggedBunny avatar Jun 04 '18 18:06 DruggedBunny

You shouldn't have to do it every frame!

Well... Here is a use case for why you might do it every frame. https://www.phoenixusc.com/alienphoenix/2d-3d-example.html

In the past the camera render was bounded by using Canvas.Viewport, but, is not any more . It worked well before the change.

RichardBetson avatar Jun 05 '18 00:06 RichardBetson

By default, camera viewport stays at the size you set.

You can override this by attaching camera to a View, either when it's constructed, (eg: camera = new Camera( Self ) as I think most samples do) or via Camera.View property.

There are several approaches here and I'm not sure what's best TBH. The camera could just 'autosize' to whatever it's being rendered to but makes CameraPick a little less predicatble and means you wouldn't be able to automate muticamera views with multiple cameras etc.

So for now at least, camera viewport stays at what you set it (and defaults to 640x480) unless you attach camera to a view.

blitz-research avatar Jun 09 '18 22:06 blitz-research

How come it doesn't default to the initial display mode, though, rather than 640 x 480? Eg. in the above example, I'd have expected the camera to fit the initial 1024 x 768 display...

DruggedBunny avatar Jun 12 '18 18:06 DruggedBunny

You mean auto size camera to the size of the active window when it's created? Although the window wont strictly be active yet in its ctor, I can probably fudge this.

People are still going to have to 'connect' it to the main view if they want the camera to autoresize when the view resizes though. I do want to keep multiple windows/cameras a possibility, plus the ability to be able to render directly to a viewport within a user canvas etc.

On Wed, Jun 13, 2018 at 6:00 AM DruggedBunny [email protected] wrote:

How come it doesn't default to the initial display mode, though, rather than 640 x 480? Eg. in the above example, I'd have expected the camera to fit the initial 1024 x 768 display...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/blitz-research/monkey2/issues/378#issuecomment-396680114, or mute the thread https://github.com/notifications/unsubscribe-auth/ADU3Qq-dHIZYCNmaQMZGlr8pWH_40vGVks5t8AGogaJpZM4TzsCE .

blitz-research avatar Jun 12 '18 20:06 blitz-research

Hmm, I don't really have any awareness of 'views', as I don't touch them directly (eg. see source above), just sort of vaguely aware they exist!

Yes, we do of course want the option of multiple windows/cameras, but it just seemed natural to me that a camera would fill the display it's created in.

Not really a big deal, just didn't expect a default 640 x 480, and I don't think it used to do this (?), so was surprised and assumed bug.

DruggedBunny avatar Jun 13 '18 00:06 DruggedBunny