Search code examples
openglassimp

Drawing animated model with background - setting opengl matrix mode and perspective


I am trying to draw a animated model with a image background. To draw my model properly, I need to use glMatrixMode, glLoadIdentity, gluPerspective etc.. However, to draw rect on background, i need to change all those mode and matrix which I set to see my model properly.

I posted my code below. Can anyone tell me what I should do to see my model and background properly at the same time?

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity();               // Reset MV Matrix

glBindTexture(GL_TEXTURE_2D, m_texture1);
glBegin(GL_QUADS);
glColor4f(1.0f, 1.0f, 1.0f, 1);
glTexCoord2f(0, 1);
glVertex3f(-1.0, -1.0, 0);
glTexCoord2f(0, 0);
glVertex3f(-1.0, 1.0, 0);
glTexCoord2f(1, 0);
glVertex3f(1.0, 1.0, 0);
glTexCoord2f(1, 1);
glVertex3f(1.0, -1.0, 0);
glEnd();

const double aspectRatio = (float) getOpenGLViewWidth()
        / getOpenGLViewHeight(), fieldOfView = 45.0;

glMatrixMode (GL_PROJECTION);   
glLoadIdentity();   
gluPerspective(fieldOfView,
        aspectRatio,
        1.0,
        1000.0);
glMatrixMode (GL_MODELVIEW);                  // Select The Modelview Matrix
glLoadIdentity();
glTranslatef(0.0f, -25.0f, -70.0f); // Move 40 Units And Into The Screen

glViewport(0, 0, getOpenGLViewWidth(), getOpenGLViewHeight());

// glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
recursive_render(_scenes[0], _scenes[0]->mRootNode, 25);

Solution

  • If your image background is a static backdrop that should fill the whole viewport and not rotate with your scene you should render it separate using an orthogonal matrix.

    My legacy GL is a bit rusty, but it should look like this:

    1. Set an orthogonal projection matrix (e.g. with glOrtho)
    2. Set your model view matrix to identity
    3. Render your static background filling the whole viewport
    4. Set a perspective projection matrix
    5. Translate and rotate your model(view) matrix for your animated model
    6. Render your model

    Note that you may want to flip the order and render the background last if you want to save some fill rate.