Jump to content
  • Advertisement
Sign in to follow this  
Xeon06

OpenGL OpenGL 2D Camera?

This topic is 3756 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

Hey all, I have been using an ortographic 2D camera in OpenGL. Here is my camera's code:
glOrtho(0, width, height, 0, -1, 1);
So basically I can draw everything I want as if it was a nice 2D game. However I want two things. I want to be able to translate the whole "camera view". I want it to be centered on a mentioned point. I have that:
	static void translate(int x, int y)
	{
		glMatrixMode(GL_PROJECTION);
		glPushMatrix();
		glTranslatef(x, y, 0.0f);
		glPopMatrix();
	}
However, this doesn't center the camera properly. My window is 500*500 and if I draw something at (1000, 1000) and I try to translate the camera at (1000, 1000) I just see blackness when I should see what I drew no? Also, I want the whole "camera" to be able to rotate arround my camera's center point. That I have no idea how to do. I'm not really good at transformations. Does anyone know how to solve my problem?

Share this post


Link to post
Share on other sites
Advertisement
First of all use glMatrixMode(GL_MODELVIEW) for the translation. With your setup you screw your projection matrix.

Second, you should remove the glPushMatrix() and glPopMatrix() calls. The combination saves and restores the current matrix and thus you'll lose the glTranslatef() information.

Third, keep in mind that moving the camera actually means moving the scene to the camera, so in order to move to (1000 / 1000) you have to move the scene by (-1000/-1000).

Fourth, use glRotatef() in order to rotate the scene around the camera. Check out some matrix math/transformation tutorials since the order of the glRotatef and glTranslatef calls matters.

Share this post


Link to post
Share on other sites
Thanks I got my translation to work, However I'm still having trouble with the rotation. I'm trying to make it rotate around my camera view but it just rotates around 0, 0

static private int xCam, yCam, rCam;

static void translate(int x, int y)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, 0.0f);
glTranslatef(-x + (Window.width / 2), -y + (Window.height / 2), 0.0f);
xCam = x;
yCam = y;
}

static void rotate(int r)
{
glMatrixMode(GL_MODELVIEW);
glRotatef(-rCam, 0.0f, 0.0f, 1.0f);
glRotatef(r, 0.0f, 0.0f, 1.0f);
rCam = r;
}

Does anybody know how to fix my rotation?

Share this post


Link to post
Share on other sites
If you want to rotate your camera you basically need to rotate your world arount the camera. So first rotate the world, then translate it.

BTW, why do you call glTranslatef and glRotatef twice? There's no need for it.

From what you posted here, it is obvious that you don't know matrix math and transformations that well (like you stated above). If you are really interested in making 2D or 3D games (at least if they involve any scaling or rotating), you should go and learn a bit more on that topic, since that's one of the most fundamental basics of graphics programming.

Share this post


Link to post
Share on other sites
I was doing it twice to "cancel" the transformation but you're probably right I'll read up more about it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Xeon06
Thanks I got my translation to work, However I'm still having trouble with the rotation. I'm trying to make it rotate around my camera view but it just rotates around 0, 0


glTranslatef(0.0f, 0.0f, 0.0f);
glTranslatef(-x + (Window.width / 2), -y + (Window.height / 2), 0.0f);



First of all, this is not canceling the translation.
You are thinking like glTranslate moves the view to a specific coordinate. It doesn't.

It moves relative to the camera's current position.

I.e.

glTranslatef(5.0f, 0.0f, 0.0f);
glTranslatef(5.0f, 0.0f, 0.0f);


Is equal to

glTranslatef(10.0f, 0.0f, 0.0f);


In order to reverse a single translation, you must glTranslate backwards.

glTranslatef(5.0f, 0.0f, 0.0f);
glTranslatef(-5.0f, 0.0f, 0.0f);
//Camera is at same position after these lines


Share this post


Link to post
Share on other sites
You should not cancel the transformations but use glPushMatrix() and glPopMatrix() to save and restore the matrices. (There are other ways like storing them yourself and passing them to the shaders, but those seem to be a bit too advanced for you right now).

Basically it's like this:

glPushMatrix();
//call your translations here
//render something
glPopMatrix();

You also can stack those calls in order to use hierarchies, i.e.

glPushMatrix(); //save initial matrix, generally not necessary but included for demonstration (you normally call glIdentity(); then do the camera transforms
//camera transformations
glPushMatrix(); //save camera matrix
//parent object transformations
//render parent object
glPushMatrix(); //save parent object matrix
//child object transformations
//render child object
glPopMatrix(); //restore parent object matrix
glPopMatrix(); //restore camera matrix
glPushMatrix(); //save camera matrix
//object transformations
//render object
glPopMatrix(); //restore camera matrix
glPopMatrix(); //restore initial matrix, also not needed generally, see above

Share this post


Link to post
Share on other sites
Quote:
Original post by Lord_Evil
First of all use glMatrixMode(GL_MODELVIEW) for the translation. With your setup you screw your projection matrix.

Second, you should remove the glPushMatrix() and glPopMatrix() calls. The combination saves and restores the current matrix and thus you'll lose the glTranslatef() information.

Third, keep in mind that moving the camera actually means moving the scene to the camera, so in order to move to (1000 / 1000) you have to move the scene by (-1000/-1000).

Fourth, use glRotatef() in order to rotate the scene around the camera. Check out some matrix math/transformation tutorials since the order of the glRotatef and glTranslatef calls matters.


Thanks for the help guys. About push and pop, I was doing that at the beginning... And now I understand that transformation calls arent absolute but relative.

Edit: Oh wait, you gotta render the thing in-between? Maybe thats what I was doing wrong.

Edit: Ok I got my things working back but I still have the problem of my rotation only centered on 0,0. Well I'll work on that, thanks for all the help.

[Edited by - Xeon06 on July 8, 2008 10:35:44 AM]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!