OpenGL 2D Camera System

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

OpenGL 2D Camera System

Post 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. :/
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL 2D Camera System

Post by N64vSNES »

Err...You're looking for something pre-built?
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: OpenGL 2D Camera System

Post 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.
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL 2D Camera System

Post 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?
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: OpenGL 2D Camera System

Post 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
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: OpenGL 2D Camera System

Post 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. ;)
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: OpenGL 2D Camera System

Post 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
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
Post Reply