# Difference and interaction between Viewport and Camera

I'm very very confused about some very basic concepts in game/grafical developing. The interaction between the individual components (camera, viewport, window-sizes, gameworld-sizes, ...) when rendering graphical things are still confusing me.

As far as I understand:

• A camera "filming"/viewing something as long it is between the near- and the far-clipping plane
• Camera has a Width and Height which defines the area of what will get "viewed"... ?
• The viewport can connect to an camera
• the viewport is the area which will get shown withing the window
• the cameras view will get projected onto the viewport
• so if the camera has a size of 1024x768 and the viewport ist only 300x400, the 1024 pixels will get upset on 300 "real" pixels

As you may have have recognized Im very unsure about this. Maybe you can help me with that.

I want to run the test-app down bellow in a way, that the picture will not get stretched. Also Im wondering what "game coordinates" are in that context. For Demo-Cases and learning I've just created a piece of src:

package com.mygdx.game;

import ...

public class MyGdxGame extends ApplicationAdapter {
public PerspectiveCamera cam;
public Model model;
public ModelBatch modelBatch;
public ModelInstance instance;
private FitViewport viewport;

@Override
public void create() {
modelBatch = new ModelBatch();

// creating a camera; gets width and height from the window
// so it will "fit" best in the current window
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(10f, 10f, 10f);
cam.lookAt(0, 0, 0);
cam.near = 1f;
cam.far = 300f;
cam.update();

// creating viewport; width n height r not really necessary since it gets new
// values within
// the rezise() method; correct?
viewport = new FitViewport(1920, 1080, cam);

ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5f, 5f, 5f, new Material(ColorAttribute.createDiffuse(Color.GREEN)),
Usage.Position | Usage.Normal);
instance = new ModelInstance(model);
}

@Override
public void render() {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

// what is happening here?
cam.update();

modelBatch.begin(cam);
modelBatch.render(instance);
modelBatch.end();
}

@Override
public void dispose() {
model.dispose();
}

@Override
public void resize(int w, int h) {

// putting new values into the viewport
// and updating it will "resize" the camera ... ?
float aspectRatio = (float) w / (float) h;
// viewport.update((int) (h * aspectRatio), h);
viewport.update(1920, 1080);
viewport.update(w, h);
}
}


## bumped to the homepage by Community♦9 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

• Important to understand that the "camera" doesn't actually exist; it's just an analogy commonly used in tutorial material, but no API actually has a "camera". – Maximus Minimus May 30 at 20:13

You've got a lot of really good questions here! I'll be honest, after years of doing this stuff, I still find there are things I misunderstand everyday and I have to adjust my way of thinking about it.

• A camera "filming"/viewing something as long it is between the near- and the far-clipping plane

No, not exactly. Your scene exists in a (virtual) world. By setting up some matrices (more on this in a minute), we can tell the computer to project a portion of that world onto a portion of our window (or offscreen drawing surface if you're not drawing to a window).

This does something similar to what a real-world camera does. In the real world, photons are being emitted from light sources (light bulbs, the sun, etc.) and bouncing off objects in the world. They then pass through the lens of the camera and hit the film or sensor. This ends up projecting the objects from the scene onto the film or sensor. So it's a similar process to real-world camera in many ways, but is not exactly the same. (Generally, you'll only project the endpoints of the line segments that make up the polygons of your scene onto your window, and interpolate the values in between.)

• Camera has a Width and Height which defines the area of what will get "viewed"... ?

Again, not exactly. You have a window or other area of memory that you wish to draw into. It's usually a 2D array of pixels. You need to tell the computer which part of the world you want to project into some part of that 2D array of pixels. To do this, you need to give it a few different pieces of information:

1. Where in the world you want to project the scene to. You can think of this as the location of your virtual camera in your scene. It would generally be the translation component of a view matrix. (Or as Sidar pointed out in the comments: "the view matrix is the inverse of the camera objects transformation matrix in world space. This transforms the world ( vertices are transformed in the vertex shader ) as if the camera is always at its origin. This simplifies calculating to normalized device coordinates.")
2. The orientation of your virtual camera. That is, what direction is it facing. This is generally the rotation component of your view matrix.

With those 2 things you have a plane floating in your virtual world that you can project onto. Now you need to cut out a portion of that plane, squeeze it into your window and show it to the user. That's where the projection matrix comes in.

1. The projection matrix defines that space in front of the plane defined by the view matrix that you want to show to the user. Note that it doesn't have to be the same size or shape as the window you're drawing to. You can squeeze or stretch in x or y. You can also set the near and far planes as you mention to include objects farther or closer to the plane.

One way to think about this is that the view matrix defines the location and direction of your virtual camera. The projection matrix is sort of like the lens of that camera, either viewing a wider or narrower area and projecting it onto the same space in your window or offscreen drawing surface. It can simulate a wide angle lens that captures sweeping vistas, or a telephoto that zooms in close to the action.

• The viewport can connect to an camera

Generally in OpenGL (and I'd assume D3D, though I've not used that) the viewport is the 2D area of your window (or offscreen space) that you're going to draw into. It is unrelated to the camera in that sense. (It's also not to be confused with your view matrix.)

• the viewport is the area which will get shown withing the window

More precisely, it's the area of the window which will display something.

• the cameras view will get projected onto the viewport

Yes. (Well, the view of the scene through your virtual camera will get projected, not the camera itself.)

• so if the camera has a size of 1024x768 and the viewport ist only 300x400, the 1024 pixels will get upset on 300 "real" pixels

Don't think of the camera as having a size. That's not generally helpful. The projection matrix will cut out a portion of the 3D world and display it into the area of the window. The viewport is a mask on the window which only shows you a portion of the window.

• Where in the world you want to project the scene to.  Not sure if we are on the same page, but just to be clear, the view matrix is the inverse of the camera objects transformation matrix in world space. This transforms the world ( vertices are transformed in the vertex shader ) as if the camera is always at its origin. This simplifies calculating to normalized device coordinates. – Sidar Oct 11 at 10:07
• I've added your comment to the answer to hopefully clarify it a little. – user1118321 Oct 12 at 1:21