Sign in to follow this  
zacs7

Camera transforms

Recommended Posts

zacs7    136
Alloha, Just beginning programming with graphics, specifically OpenGL. Doing a bit of 2D before I dive into the deep end. Basically, I've wrapped the GL functions to provide basic sprite rendering, and a very simple camera for my application. So my question is, I move my entire scene for my camera to "move". However, I apply some rotations / etc to sprites. What's the best way to sort this out? Instead of storing and loading the matrix after each sprite, I simply reverse any rotations / transformations I did ... is this the "norm"? Basically my app, sets up * Orthographic projection, origin at (0,0) to (100, 100) * Render loop... transform the entire scene (ie, identity matrix and camera transforms on it). See [1] ** Render sprites with camera transforms. See [2] ** Load the identity matrix (ie no camera transforms) so I can draw a "overlay hud" sort of thing ** Draw the hud I'd appreciate any input! [1] Camera
/* set the camera position */
void render_camera_position(float x, float y)
{
   glTranslatef(x * -1, y, 0.0f);
}

/* render over the top, with 0->100% */
void render_overlay(void)
{
   glLoadIdentity();
}
[2] sprite_render() -- ignore the division! I'll precalculate this.
void sprite_render(sprite_t * sprite)
{
 /*  glPushMatrix(); */

   glColor3ub(0, 0, 255);

   /* translate */
   glTranslatef(sprite->x + (sprite->width / 2), sprite->y + (sprite->height / 2), 0.0f);
   glRotatef(sprite->rotation, 0.0f, 0.0f, 1.0f);
   glTranslatef(-(sprite->width / 2), -(sprite->height / 2), 0.0f);

   glBegin(GL_QUADS);
      glVertex2f(0.0f, 0.0f);
      glVertex2f(0.0f, sprite->height);
      glVertex2f(sprite->width, sprite->height);
      glVertex2f(sprite->width, 0.0f);
   glEnd();
  
   /* HACK: undo the matrix transforms, this is so
    * our camera still applies to the obj */ 
   glTranslatef(-sprite->x, -sprite->y, 0.0f);
   glRotatef(-sprite->rotation, 0.0f, 0.0f, 1.0f);  

 /*  glPopMatrix(); */
}
And a segment of the main loop
void client_render(void)
{
   /* move everything after here */
   render_camera_position(camX, camY);

   /* draw sprites */
   sprite_render(&test.sprite);

   render_overlay();
   if(console)
      console_render(false);
}
Sorry for the long post! I posted here because I'm still very new at this graphics stuff :-) Zac

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this