Search code examples
javaopenglopengl-compat

Can I push my matrix for one time for static objects in OpenGL?


I need to understand can I call glBegin, glEnd, for my project just one time.

I tried to call myRender function one time, not for every tick, but it delets all polygons, on second frame.

My tick event

public void render() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
        glClearColor(0.925f, 0.98f, 0.988f, 1f);

        glPushMatrix();
        game.tickRender();
        glPopMatrix();
    }

I tried this

public void render() { 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
        glClearColor(0.925f, 0.98f, 0.988f, 1f);
    }
public void begin(){
        glPushMatrix();
        game.tickRender();
        glPopMatrix();
}

I need to calc myMatrix one time, for project optimization. Also if i can do it, i have second question. Can i draw static polygons one time, and non-static (moving) polygons in render function every tick?


Solution

  • Note, that drawing by glBegin/glEnd sequences and the fixed function matrix stack is deprecated since decades. See Fixed Function Pipeline and Legacy OpenGL.
    Read about Vertex Specification and Shader for a state of the art way of rendering.


    glBegin starts a sequence of Primitives and glEnd ends the sequence.
    Of course you can draw multiple sequences of primitives in your code.

    The vertex coordinates of the primitives are set by glVertex. Each vertec coordinate is transformed by the current model view and projection matrix (see glMatrixMode).

    But note it is not allowed to change the current matrix within a glBegin/glEnd sequence. The only allowed commands after glBegin (and before glEnd) are the commands which set the vertex coordinate and set thee corresponding attributes like glVertex, glColor, glNormal, glTexCoord...

    The instructions like glColor, glNormal, glTexCoord ... change the current attributes, which is associated with the vertex when glVertex is called. This instructions can be performed before a glBegin/glEnd sequence, too.