Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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 1005 times)
0 Members and 1 Guest are viewing this topic.
Offline GiraffeTap222

Senior Newbie





« Posted 2013-01-06 22: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 02: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
« Reply #2 - Posted 2013-01-07 07: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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (35 views)
2014-12-09 22:41:13

BurntPizza (70 views)
2014-12-08 04:46:31

JscottyBieshaar (32 views)
2014-12-05 12:39:02

SHC (44 views)
2014-12-03 16:27:13

CopyableCougar4 (40 views)
2014-11-29 21:32:03

toopeicgaming1999 (108 views)
2014-11-26 15:22:04

toopeicgaming1999 (94 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!