Example #1
0
void GLGameModel::create()
{
    GLuint currentTex = 65535;
    TextureManager *tm = GAME->getTextureManager();

    glNewList(displayListID, GL_COMPILE);

    if(getIsLightingEnabled()) glEnable(GL_LIGHTING);

    for(int i = 0; i < glModelFaces.size(); ++i)
    {
        GLModelFace face = glModelFaces.at(i);

        // set or unset texture if neccessary
        if(!face.materialName.isEmpty() &&
            materials.contains(face.materialName))
        {
            QString textureFileName = materials.value(face.materialName);
            GLuint texId = tm->getTextureId(textureFileName);
            if(currentTex != texId)
            {
                glDisable(GL_COLOR_MATERIAL);
                glEnable(GL_TEXTURE_2D);
                glBindTexture(GL_TEXTURE_2D, texId);
                GAME->getGLWidget()->qglColor(Qt::white);
                currentTex = texId;
            }
        }
        else
        {
            if(currentTex != 0)
            {
                glEnable(GL_COLOR_MATERIAL);
                currentTex = 0;
                glBindTexture(GL_TEXTURE_2D, 0);
                glDisable(GL_TEXTURE_2D);
            }
        }

        // draw the model

        glBegin(GL_POLYGON);

        for(int n = 0; n < face.vertexIds.size(); ++n)
        {
            int x = face.vertexIds.at(n);

            if(x < 0 || x >= vertices.size()) continue;

            QVector3D v = vertices.at(x);

            if(n < face.textureCoordIds.size() && currentTex > 0)
            {
                x = face.textureCoordIds.at(n);
                QVector2D vt = textureCoords.at(x);
                glTexCoord2f(vt.x(), vt.y());
            } 

            if(n < face.vertexNormalIds.size())
            {
                x = face.vertexNormalIds.at(n);
                QVector3D vn = vertexNormals.at(x);
                glNormal3f(vn.x(), vn.y(), vn.z());
            }

            glVertex3f(v.x(), v.y(), v.z());
        }

        glEnd();
    }

    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);
    glEndList();

    createFrame();

    created = true;
}