26

This post is related to an earlier post of wanting to learn how to properly render in between LIBGDX and Box2D. I had to understand viewport well before I could proceed.
After much code/post readings, I felt the meaning of "viewport" was "the rectangle opening of a lens of the camera that views LIBGDX's Game world, where I can move it about the world to view what I want". But, after more reading, I seemed to be nowhere near the actual meaning.
I've read the LIBGDX wiki, and read in the OpenGL documentation, which seem to explain viewport as being two different things.

LIBGDX Wiki:

"The viewport is a rectangular viewing region of the screen where the 3D scene is projected. It is nothing more than mapping the 3 dimensional objects to the 2 dimensional plane."

OpenGL:

"the viewport indicates the shape of the available screen area into which the scene is mapped."

Stackoverflow:

"...It has several definitions in different contexts..." :'(

I've tried reading tens of forum posts and tutorials. But, unfortunately almost everybody jumps right into it as if "viewport" is such a primitive concept that everyone understands and knows.
I know I will get lots of heat for this utterly basic question. Please don't flame, I am asking because I actually don't know and actually need help.

Anyway, into the actual question.

What is "viewport" in LIBGDX context?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
coffeenet
  • 553
  • 1
  • 7
  • 17
  • 4
    Excellent q. This is the type of thing taken as granted but when you trawl through examples you realise people are hacking around misusing the components because of fundamental flaws in their understanding of the base components. – RichieHH Nov 17 '14 at 06:46

1 Answers1

28

That depends on the version of LibGDX you are using. With LibGDX 1.0.0 the Viewport class was introduced and probably that's what the latest tutorials and articles mean, when they talk about "viewports".

This class manages two things:

  • The viewport of a camera
  • The OpenGL viewport

The viewport of a camera defines how much you want to see of your game world. If you have a huge TiledMap of the size 10000px X 10000px, then you probably only want to show a small area of that map at once. If you define your cameras viewport to be 800px X 480px, then you will see a part of your map which has exactly this dimension (at least in the case of an OrthographicCamera). It is like a window through which you look through your world and you define the size of it via the viewport.

The OpenGL viewport defines the area to which the camera's view should be mapped to. By default that viewport is 100% of the size of your window on desktop operating systems. But that could also be different. A FitViewport with LibGDX 1.0.0 can scale that viewport so that it keeps the aspect ratio of a "virtual viewport" which you define (which is actually the camera's viewport, which is being managed by the Viewport class). That means in case the window's aspect ratio doesn't match the Viewport (virtual) aspect ratio, the OpenGL viewport will be set to a smaller size and black bars will appear. So you can think of it like another layer between the actual window on your desktop, and the camera. This layer takes the output of your camera and scales that to the defined size within your desktop window.

noone
  • 19,520
  • 5
  • 61
  • 76
  • Thank you for that splendid explanation. Did the meaning/usage/definition of viewport in Libgdx change prior to 1.0.0 version? – coffeenet May 06 '14 at 10:50
  • Prior to version 1.0.0 it was only the camera's viewport and the OpenGL viewport, because the `Viewport` class which manages both didn'T exist yet. – noone May 06 '14 at 10:53
  • So in the most recent version of LIBGDX, camera.viewportWidth/Height & stage.setViewport() follow the same concept as you kindly explained, right? And, is setting camera.viewportWidth/Height is the same as setting stage.setViewport()? – coffeenet May 06 '14 at 11:52
  • No, since in the latest version `stage.setViewport()` expects a `Viewport` object. This defines a scaling strategy (please check the wiki article I linked) and manages the camera viewport and the opengl viewport. It is basically a 3rd kind of "viewport". As you said, this word is pretty overloaded. – noone May 06 '14 at 12:02
  • Thank you for clearing up my confusion. I will go back to a question I asked some while ago about the box2d & libgdx ratio (I understand that they unrelated; thus no predefined ratio : ) ). And, I would really like it if you could kindly comment on my other question as well. – coffeenet May 06 '14 at 12:15