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