且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LIBGDX:什么是“视口”?

更新时间:2023-01-26 11:20:35

这取决于您使用的LibGDX版本。 LibGDX 1.0.0引入了 Viewport 类,可能这是最新的教程和文章的意思,当他们谈论视口时。 p>

这个类管理两件事:


  • 相机的视口

  • OpenGL视口



相机的视口定义了您希望看到的游戏世界的多少。如果您有一个大小为10000px X 10000px的巨大 TiledMap ,那么您可能只想立即显示该地图的一个小区域。如果您将相机视口定义为800px X 480px,那么您将看到地图的一部分恰好具有此维度(至少在 OrthographicCamera 的情况下)。它就像一个窗口,通过它来查看你的世界,并通过视口定义它的大小。



OpenGL视口定义相机视图应该被映射到。默认情况下,该视口是桌面操作系统上窗口大小的100%。但是,这也可能不同。使用LibGDX 1.0.0的 FitViewport 可以缩放该视口,使其保持您定义的虚拟视口的纵横比(这实际上是相机的视口,即由 Viewport 类管理)。这意味着如果窗口的宽高比与 Viewport (虚拟)宽高比不匹配,则OpenGL视口将设置为较小的大小,并且会出现黑条。因此,您可以将它想像为桌面上的实际窗口和相机之间的另一层。此图层取得您相机的输出,并在您的桌面窗口中将其缩放至所定义的大小。


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."

***:

"...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?

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.