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);
}
}