cant get a 2D Camera in LWJGL working (beginner)

Hi all,

I am working on a 2D Game and I try to set up a simple camera for it (just moving and zooming).
Unfortunatly, I don’t get it working at all.
Since I am new to LWJGL and OpenGL in general I have no idea, if I am missing out on something very basic
or if the problem is more complex.

With my googling I found out that there are 3 approaches:
1.) Use glScalef and glTranslatef (this seems to be very easy… in theoriy…)
2.) Use gluLookAt and gluPerspective (this seems to be more sophisticated, but maybe too much for a 2D Game?)
3.) Hardcode the “camera” position and zoom level into each draw call (now that seems to be a terrible way to go, even though I am sure I could do that…)

The most effort so far I put into the glScalef and glTranslatef approach.
My code is compiling, but neither the scaling nor the translating has any
effect on the screen.

Maybe somebody can take a look at my code and help me out

So here is my code:


// called once at start
protected void create() {
		// Enable blending
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	// enable textures since we're going to use these for our sprites
		glEnable(GL_TEXTURE_2D);
		 
		// disable the OpenGL depth test since we're rendering 2D graphics
		glDisable(GL_DEPTH_TEST);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		 
		glOrtho(0, WIDTH, HEIGHT, 0, -1, 1);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glViewport(0, 0, WIDTH, HEIGHT);
		// Set clear to transparent black
		glClearColor(0f, 0f, 0f, 0f);
}

// called every frame
protected void render(){
    // Clear the screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    float centerX = WIDTH * 0.5f;
    float centerY = HEIGHT * 0.5f;
    //Move to center of scene
    glTranslatef(centerX,centerY,0);   
    //Scale at center
    glScalef(2,2,1);          
    //Move back to origin
    glTranslatef(-centerX,-centerY,0); 
    glPushMatrix();
		// ... render our game here ...
		spriteBatch.begin();
                         // next line is Pseudo-Code, but this is the way i draw all my graphics. 
                         // I pass the spriteBatch to all my Entities, and they are drawn in the fashion shown below
		         spriteBatch.draw( textureRegion, x, y, width, height );
		spriteBatch.end();
     glPopMatrix();
}

The SpriteBatch I use is not by me.
I use this one here: https://github.com/mattdesl/lwjgl-basics/blob/master/src/mdesl/graphics/SpriteBatch.java
Might it be the source of my problem? I tried to wrap my head around what actually is happening in that SpriteBatch,
but it is way beyond me :frowning:

glScale/glTranslate are deprecated. GLU isn’t recommended much these days, and for all intents and purposes you can also consider it deprecated.

glPushMatrix, glMatrixMode, glLoadIdentity, and glOrtho are also all deprecated, and won’t work in conjunction with my SpriteBatch (or any modern GL frameworks).

Instead, you need to provide SpriteBatch its own matrices. Below is an example using lwjgl-basics, and the display initialization from the “test” package (which is pretty much the same from the Display tutorial)
http://www.java-gaming.org/?action=pastebin&id=650

More info on matrices and modern GL here:
http://tomdalling.com/blog/modern-opengl/03-matrices-depth-buffering-animation/
http://tomdalling.com/blog/modern-opengl/04-cameras-vectors-and-input/
http://open.gl/transformations
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/
http://www.arcsynthesis.org/gltut/

Modern OpenGL is a very challenging path for a beginner; you can’t really just copy + paste and hope things work out. This is why frameworks like LibGDX are usually recommended instead; it allows you to work with OpenGL just as easily as LWJGL, but also gives you a lot of high-level functionality so that you aren’t struggling with complex problems every step of the way.

Thank you very much davedes, that is some very useful information.
I will try to get my head around the matrix stuff and take LibGDX as an emergency exit, if it proves to be too hard :slight_smile:

The code example you gave me works great! Also the sites you linked me seem to be amazing. It will take quite some time, to go through these, but at least I know, that this is the “real deal” (not some deprecatet concepts).
Also, I was told over at the LWJGL Forum, that you made the lwjgl-basics library including the spriteBatcher. I went through quite some of your tutorials before and they are really excellent. They helped me out a lot to set foot on this ground. So thank you very much for those, too :smiley: