Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LIBGDX: What is a "viewport"?

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?

like image 893
coffeenet Avatar asked May 06 '14 09:05

coffeenet


1 Answers

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.

like image 91
noone Avatar answered Nov 06 '22 19:11

noone