What is the best approach for creating a movable 2D camera and applying it to my game in openGL?
I have seen people use
1
| glTranslatef(-focusCam.x + (SCREENX / 2), -focusCam.y + (SCREENY / 2)); |
before sending there data off to the graphics card for processing.
But is that way to do it for best performance?
Assuming my game loop is like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| while(runGame) { frames++; gameClockNow = timeGetTime();
loops = 0; while( (timeGetTime() >= gameClock) && (loops < MAXUPDATES) ) {
pollForInput();
gameClock += updateTime; loops++; }
alpha = ((timeGetTime() - gameClock) + updateTime) / updateTime; calculateFPS(gameClockNow, startTime);
interpolateAllObjects(alpha);
drawGame(); |
And my drawGame Function looks like this and uses vertex arrays:
1 2 3 4 5 6 7 8 9 10 11 12 13
| void drawGame() {
batcher.begin(); for(int i = 0; i < COUNTSPRITE; i++) batcher.drawAtlasSprite((float)spriteList[i].x, (float)spriteList[i].y, 64, 64, spriteList[i].OriginX, spriteList[i].OriginY, 128.0f, 128.0f);
batcher.end(); batcher.swapBuffer(); } |
Also Im not sure if you need this but here is my init funciton for openGL :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| void initOpenGl() { glDisable(GL_DEPTH_TEST); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, SCREENX, SCREENY);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
glOrtho(0.0f, SCREENX, SCREENY, 0.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } |