Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
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 780 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  
 
 
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.

xsi3rr4x (35 views)
2014-04-15 18:08:23

BurntPizza (31 views)
2014-04-15 03:46:01

UprightPath (46 views)
2014-04-14 17:39:50

UprightPath (29 views)
2014-04-14 17:35:47

Porlus (46 views)
2014-04-14 15:48:38

tom_mai78101 (67 views)
2014-04-10 04:04:31

BurntPizza (127 views)
2014-04-08 23:06:04

tom_mai78101 (227 views)
2014-04-05 13:34:39

trollwarrior1 (192 views)
2014-04-04 12:06:45

CJLetsGame (199 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!