OpenGL 2D Camera System
Moderator: Coders of Rage
-
- Chaos Rift Cool Newbie
- Posts: 70
- Joined: Mon Dec 13, 2010 10:55 pm
OpenGL 2D Camera System
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)
I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
Re: OpenGL 2D Camera System
Err...You're looking for something pre-built?
-
- Chaos Rift Cool Newbie
- Posts: 70
- Joined: Mon Dec 13, 2010 10:55 pm
Re: OpenGL 2D Camera System
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.
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)
I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
Re: OpenGL 2D Camera System
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?
Is it possible to see some code or maybe a screenshot of what's going wrong?
-
- Chaos Rift Cool Newbie
- Posts: 70
- Joined: Mon Dec 13, 2010 10:55 pm
Re: OpenGL 2D Camera System
Sure. Here's the drawing code for any 2D Texture:
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
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();
-- Jesse Guarascia
I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
- Falco Girgis
- 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
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.
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.
-
- Chaos Rift Cool Newbie
- Posts: 70
- Joined: Mon Dec 13, 2010 10:55 pm
Re: OpenGL 2D Camera System
Well yeah probably :/ Here's the code for that though, if it helps: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.
Code: Select all
Camera::CurrentCamera()->Pos().TranslateTo();
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)
I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)