Jump to content
  • Advertisement
Sign in to follow this  

Camera transforms

This topic is 3499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)
[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);

      glVertex2f(0.0f, 0.0f);
      glVertex2f(0.0f, sprite->height);
      glVertex2f(sprite->width, sprite->height);
      glVertex2f(sprite->width, 0.0f);
   /* 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 */

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!