[libgdx] trouble understanding orthocam and viewport

Hi, I recently realized how I don’t quite understand just how cameras and viewports work together. I have been working on a project (which I’m approaching the point where I will put it up on WIP for public scrutiny) in my free time (that is limited), and used a camera / viewport setup that I saw in a tutorial and it worked for what I needed, so it was forgotten. Unfortunately, when it came time that the tutorial solution was no longer sufficient and tried to modify things that I realized how fragile my grasp of the subject actually is.

I know this seems a basic question, but the documentation on the subject is limited at best, and even with tutorials it seems people just go over their choice with little to no explanation. I’ve been looking over various pages trying to refresh this for myself before asking, and it seems I must be missing something.

Tell me if this is correct, I’ll be working through this over the next few days trying to get how this works, but, the way I understand is that the camera is looking at the scene being drawn (the level plus characters, not libgdx scene), where the viewport is the section of that scene that gets displayed in the window (or android app)? Is this more or less correct?

If this is accurate, Would it be possible / good to either;

  • set the camera to be the size of the level (say 100 tiles wide by 20 tiles tall), then set the viewport to see, say 20 tiles wide by 20 tiles tall)?
  • Keep the camera and viewport as the same values and just move the camera across the map?
  • set the camera to the screen resolution and ignore the viewport?
  • any other particular suggestions?

similarly, what is the best strategy to use a viewport that would be suitable for any resolution without the bars of glClear color?

For any box2d scaling issues, I understand what all needs to be scaled, that’s not so much of an issue.

When using viewports in windows, is it possible in the resize event to configure the viewport to compensate for any resolution? For android, is the better strategy to test for the device resolution and configure the display when the program starts?

as a second side question; which strategy would make things easier for things like zooming in an out on the screen?

I think I found one of the downsides to using a game engine, in that I dont really understand how things work, as opposed to with C++ where building everything from scratch meant that I had spent time on every aspect until I came to the solution.

Thanks for any help / answers / advice.

So the best way to understand this is by looking at the camera analogy:

So the camera is all of the viewing (putting the camera in place), modeling (putting the objects in place) and projection (choosing the lens). The viewport is developing the picture (putting the image somewhere on the paper, the paper be analogous to the window).

So glViewport() only controls where in the window the stuff is rendered. Do not use it to control what is rendered. For most use cases, you should call glViewport() when the window is resized to make sure that you are rendering to the entire window.

I’m not sure what you mean by “a viewport that would be suitable for any resolution without the bars of glClear color” What bars is glClear presenting? If you mean to control the aspect ratio of a particular resolution, that should be taken care of in the camera.

For zooming, think about the camera analogy. To zoom in with a camera, you can either change the lens or you can move the camera closer. Same for OpenGL, change the projection or the viewing. What you cannot do is zoom by developing the photo differently.

Hope I’ve answered some questions.

I know, late response to this one… got through this issue.

@quew8 - Thanks for the help, that image you put up and description really made me realize how I was over thinking this part.

So, my resolution to this was to just use the default view port, actually, I fail to see the purpose of using an alternative viewport, call that another question if anyone is willing?

What I wound up doing :

  • Get the screenwidth and screenheight.
  • Figured how many tiles the screen should display (tilesX, tilesY)
  • How many pixels per tile (pixPerT)
  • PPM

Then I just did height = tilesY * pixPerT * pixPerM
and for width = tilesX * pixPerT * pixPerM * aspectRatio

and that simplicity gave the display that I could live with… I will just have to consider having different scales of images for larger screens, but thats a problem for later. So far, it looks good at most sizes.

I figured I would leave the solution for future reference, for others encountering similar issues, and well, because I appreciate the help I’ve been getting while Im learning java.