Page 1 of 1

OpenGL 2D Camera System

Posted: Sun May 01, 2011 9:03 pm
by JesseGuarascia
Does anybody know of a decent OpenGL 2D Camera System? I've tried some before, and all of them mess up with rotation of objects if the camera moves in a certain way. :/

Re: OpenGL 2D Camera System

Posted: Mon May 02, 2011 12:10 pm
by N64vSNES
Err...You're looking for something pre-built?

Re: OpenGL 2D Camera System

Posted: Mon May 02, 2011 2:57 pm
by JesseGuarascia
No, just some sort of way to move the scene around without messing with the rotations. As of now, everything renders fine, but once I apply some sort of rotation with the camera active, it just messes it up.

I use a call to glTranslatef() giving the camera coordinates (which are on the negative x and y planes). For some reason though, the call to glRotate in all of my Render methods are rotating very strangely.

Re: OpenGL 2D Camera System

Posted: Mon May 02, 2011 3:04 pm
by N64vSNES
I'm not entirely sure what your problem is.

Is it possible to see some code or maybe a screenshot of what's going wrong?

Re: OpenGL 2D Camera System

Posted: Mon May 02, 2011 3:10 pm
by JesseGuarascia
Sure. Here's the drawing code for any 2D Texture:

Code: Select all

	// Begin to draw the quad using the given texture
	glPushMatrix();

	glTranslatef(rect.CenterY(), rect.CenterX(), 0.0f);

	texture.Bind();

	Rect tRect (0.0f, 0.0f, 1.0f, 1.0f);

	if (texture.isPadded()){
		tRect = texture.Pad().ToTexCoords((float)texture.Width(), (float)texture.Height());
	}

	glRotatef(rotation, 0.0f, 0.0f, 1.0f);

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(rect.DrawLeft(), rect.DrawTop(), 0.0f);
		glTexCoord2f(tRect.right, 0.0f); glVertex3f(rect.DrawRight(), rect.DrawTop(), 0.0f);
		glTexCoord2f(tRect.right, tRect.bottom); glVertex3f(rect.DrawRight(), rect.DrawBottom(), 0.0f);
		glTexCoord2f(0.0f, tRect.bottom); glVertex3f(rect.DrawLeft(), rect.DrawBottom(), 0.0f);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, 0);

	glPopMatrix();
Outside of that, right before any drawing code begins, I make a call to glTranslatef() for the camera's positions. When I rotate 90 degrees from a specific point (the center of the screen) the middle of the texture doesn't rotate on the point, resulting in it rotating all over the place. I looked at the code for camera's in Eternal Quest as well, it seems kind of nice, but it'd definitely be a big change :P

Re: OpenGL 2D Camera System

Posted: Mon May 02, 2011 4:23 pm
by Falco Girgis
This is the ever-so-common case of a fundamental misunderstanding of a rotation matrix.

The code you've provided is correct, as you haven't provided code for translating to the CAMERA's position or rotating about the CAMERA's orientation.

A rotation matrix is a matrix that rotates a vector about the ORIGIN, not about an arbitrary point (the camera's origin). You must first translate each sprite to the camera's origin (subtract the camera's position), THEN perform the rotation (at your new origin), then translate your sprite back.

Don't bitch out and use a prebuilt camera system just because you can't figure a single matrix operation out. ;)

Re: OpenGL 2D Camera System

Posted: Mon May 02, 2011 4:32 pm
by JesseGuarascia
GyroVorbis wrote: The code you've provided is correct, as you haven't provided code for translating to the CAMERA's position or rotating about the CAMERA's orientation.
Well yeah probably :/ Here's the code for that though, if it helps:

Code: Select all

	Camera::CurrentCamera()->Pos().TranslateTo();
I take the center of the Camera's rectangle, and use the glTranslatef functions (wrapped in the TranslateTo method). Pos() just returns a Rectangle with the ability to translate to it's center. That's literally all I do for the camera. It works for basic positions, but rotation it does not :/ When I set up the position of the camera, I do it in the negative x, and negative y planes.

I kind of understand your suggestion Falco, with the altering of the Texture rendering positions based on the Camera's positions as opposed to the center point of a Rectangle. I'd just have to subtract the Camera's positions from the Textures' positions in order to do so (or at least as far as I can see). Thanks. :D