Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D world camera best approach?  (Read 433 times)
0 Members and 1 Guest are viewing this topic.
Offline GiraffeTap222

Senior Newbie





« Posted 2013-01-06 23:41:27 »

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)
   {
                //Add onto frame count and get the current time
     frames++;
      gameClockNow = timeGetTime();

                //The game update loop
               //Reset the loop count and execute the loop
     loops = 0;
      while( (timeGetTime() >= gameClock) && (loops < MAXUPDATES) )
      {

         //Poll for event inputs
        pollForInput();

         //Update game objects
        //updateGame();

         //Update the game clock and loop count
        gameClock += updateTime;
         loops++;
      }

      //Calculate the interpolation
     alpha = ((timeGetTime() - gameClock) + updateTime) / updateTime;
   
      //Calculate the FPS
     calculateFPS(gameClockNow, startTime);      

      //Calculate interpolation for the debug player sprite
               interpolateAllObjects(alpha);

      //Draw the game
     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();
         
        //Draw the sprites based on COUNTSPRITE using the atlas draw method
       //Where it takes in the x and y position of the sprite, the sprites width and height, its x and y origin on the atals in pixels, and the width and height of the atlas (128.0 x 128.0 in or case)
  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()
{
   //Clear openGl items
  glDisable(GL_DEPTH_TEST);
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   //Set the view port
  glViewport(0, 0, SCREENX, SCREENY);

   //Set / load the projection matrix
  glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

   //Set the ortho
  glOrtho(0.0f, SCREENX, SCREENY, 0.0f, -1.0f, 1.0f);

   //Set / load the model view
  glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   //Enable use of textures
  glEnable(GL_TEXTURE_2D);

   //Enable blending of alpha layer
  glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
Offline davedes
« Reply #1 - Posted 2013-01-07 03:04:27 »

With fixed-function pipeline you can use glTranslate and it should be fairly performant.

The programmable pipeline is obviously more flexible and will let you optimize a bit further. For example you could use uniforms in your vertex shader which hold view translation and scale (i.e. only 3 floats). Or you could do transformation on the CPU.

Offline orogamo

Senior Member


Medals: 5
Projects: 1



« Reply #2 - Posted 2013-01-07 08:51:46 »

Check this out: https://dl.dropbox.com/u/39535549/SpaceSploder.zip
It's a little (unfinished) 2D OpenGL-Accelerated Game.
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (60 views)
2013-05-17 21:29:12

alaslipknot (69 views)
2013-05-16 21:24:48

gouessej (99 views)
2013-05-16 00:53:38

gouessej (98 views)
2013-05-16 00:17:58

theagentd (107 views)
2013-05-15 15:01:13

theagentd (98 views)
2013-05-15 15:00:54

StreetDoggy (144 views)
2013-05-14 15:56:26

kutucuk (167 views)
2013-05-12 17:10:36

kutucuk (166 views)
2013-05-12 15:36:09

UnluckyDevil (175 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.184 seconds with 20 queries.