Search code examples
c#openglopentk

Why should I use glTranslate?


What's the purpose of pushing/popping a matrix to translate a shape when I can just sort of do the position math myself? I can understand using said the aforementioned matrices to do more difficult operations (rotation for example), but why translating specifically?

public void render()
        {
            positionX++;

            GL.PushMatrix();
            GL.Translate(positionX, positionY, 0);

            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();

            GL.Begin(PrimitiveType.Quads);

            GL.Vertex2(0, 0);
            GL.Vertex2(0 + widthX, 0);
            GL.Vertex2(0 + widthX, 0 + widthY);
            GL.Vertex2(0, 0 + widthY);

            GL.PopMatrix();

            GL.End();
        }

versus

public void render()
        {
            positionX++;
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();

            GL.Begin(PrimitiveType.Quads);

            GL.Vertex2(positionX, positionY);
            GL.Vertex2(positionX + widthX, positionY);
            GL.Vertex2(positionX + widthX, positionY + widthY);
            GL.Vertex2(positionX, positionY + widthY);

            GL.End();
        }

Solution

  • I don't think this is a dumb question, altough the answer might become clear as you work with OpenGL more.

    Translating using glTranslate (or similar methods) can definitely come in handy when you want a different order of transformations. Changing the vertex coordinates can only work if the translation is the first transformation you want to do. If you want to do, for example, rotation, then translation, then another rotation, you have to use a matrix.

    The main reason, however, is that usually you will not be sending the vertices to the GPU on every frame. That is extremely slow and, in fact, all of the GL.* method that you use are now deprecated. The way it is done nowadays is:

    1. Send all vertex data the the GPU once before you begin rendering anything (remember it can be thousands of polygons or more).
    2. The GPU will store the data in its internal memory (that's why you have however many MB/GB of RAM on your graphics card) and give you an id (e.g. 1).
    3. When rendering a frame, you just tell the GPU "draw stuff that you saved with id 1". That is a single OpenGL call - keep in mind that every OpenGL call has a cost (meaning time), so the fewer calls you make, the faster your graphics will be.

    The same is done for textures, shaders and other things.

    Therefore, you generally don't modify your vertex data once you send it to the GPU (technically it is possible to modify, but it's not advisable for performance reasons). So if you want to do any transformations at all, you have to use a matrix.

    As a final note, I'll say again that you're using deprecated functionality, which is not so bad for learning some basic concepts I suppose, but I would definitely recommend moving to current OpenGL (or at least OpenGL 3) as soon as possible and get to know shaders, vertex buffers, vertex arrays and all the new shiny stuff.

    TL;DR: Yes, you can do it yourself. But it's better to let your graphics card do it because it can do it a LOT faster. In fact, that is the main reason for having a dedicated graphics card.