How can you move 2D objects around in OpenGL? Wha开发者_开发技巧t is the mechanism? E.g., do we need to control frames?
An example code snippet would be welcome!
The short answer is that you can move an object by modifying the modelview matrix. This can be done in several ways and it depends on your coding preferences and which version of opengl drivers you have available.
Here's some partial code to get you started in the right direction:
The following is deprecated in Opengl 3.0+ (but even then it is still available via the compatibility profile).
// you'll need a game loop to make some opengl calls each frame
while(!done)
{
// clear color buffers, depth buffers, etc
initOpenglFrame();
glPushMatrixf();
glTranslatef(obj->getX(), obj->getY(), obj->getZ());
obj->draw();
glPopMatrixf();
// move the object 0.01 units to the left each tick
obj->setX(obj->getX() + 0.01);
// flush, swap buffers, etc
finishOpenglFrame();
}
For OpenGL 3.0 and beyond, glPushMatrixf(), glPopMatrixf(), glTranslatef(), and similar fixed function pipeline interface is all deprecated. If you need to using OpenGL 3.0 or greater, you'll want to do the same type of solution but you'll need your own implementation of matricies and a matrix stack. I think that discussion is beyond the scope of your question.
For further reading, and to get started with OpenGL, I'd recommend you check out nehe.gamedev.net. If you would like to get started with OpenGL 3.X, then I'd recommend purchasing the Opengl Superbible 5th Edition. Great book!
精彩评论