views:

48

answers:

2

how to move 2d objects around in opengl? what is the mechanism, e.g, do we need to control frames or what?

some code segment is welcomed :)

thanks in advance!

A: 

OpenGL just passes vertex data though some transform matrices and rasterizes the result. Anything higher level than that you have to provide yourself.

genpfault
A: 

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!

Alex Wood