Quote Originally Posted by Power2012 View Post
I did a test, which shows that you could just use one texture unit, such as GL_TEXTURE0.

and it seems quite a few things are not quite right in your source code.
What is wrong exactly? Here is the whole class with the init method to get a clearer picture of my code:

Code :
#include "global.h"
#include "CModel.h"
#include "CModelLoader.h"
#include "CShaderLoader.h"
 
CModel::CModel(){}
 
CModel::~CModel()
{
    // TODO free memory
}
 
/** @brief Load mbm file
  *
  * @fileName: mdm model file name we want to load and draw
  */
void CModel::init(char *fileName)
{
    CModelLoader   loader;
    CShaderLoader  shaders;
 
    loader.loadData(fileName);
 
    loader.createDataArray( m_dataArray,
                            m_dataArraySizes,
                            m_numObjects,
                            m_numElements,
                            m_textureIDs );
 
    glGenVertexArrays(1, &m_vertexArrayID);
    glBindVertexArray(m_vertexArrayID);
 
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
 
    m_programID      = shaders.loadShaders("vertexshader.vert", "fragmentshader.frag");
 
    //m_matrixUniform  = glGetUniformLocation(m_programID, "MVP");
    //m_textureUniform = glGetUniformLocation(m_programID, "myTextureSampler");
 
    m_bufferIDs      = new unsigned int[m_numObjects];
 
    glGenBuffers(m_numObjects, m_bufferIDs);
    for(unsigned int i=0; i<m_numObjects; i++)
    {
        glBindBuffer(GL_ARRAY_BUFFER, m_bufferIDs[i]);
        glBufferData(GL_ARRAY_BUFFER, sizeof(float)*m_dataArraySizes[i], m_dataArray[i], GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
 
}
 
void CModel::draw(glm::mat4 &MVP)
{
    glActiveTexture(GL_TEXTURE0);
 
    for(unsigned int i=0; i<m_numObjects; i++)
    {
        glUseProgram(m_programID);
 
        unsigned int matrixUniform  = glGetUniformLocation(m_programID, "MVP");
        unsigned int textureUniform = glGetUniformLocation(m_programID, "myTextureSampler");
 
        glUniformMatrix4fv(matrixUniform, 1, GL_FALSE, &MVP[0][0]);
 
        glBindTexture(GL_TEXTURE_2D, m_textureIDs[i]);
 
        glUniform1i(textureUniform, 0);
 
        glBindBuffer(GL_ARRAY_BUFFER, m_bufferIDs[i]);
        // vertices
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(
                0,                  // must match the layout in the shader.
                3,                  // size
                GL_FLOAT,           // type
                GL_FALSE,           // normalized?
                sizeof(float)*8,    // stride
                (void*)0            // array buffer offset
        );
 
        // UVs
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(
                1,
                2,
                GL_FLOAT,
                GL_FALSE,
                sizeof(float)*8,
                (void*)6
        );
 
        glDrawArrays(GL_TRIANGLES, 0, m_numElements[i]);
 
        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
 
        glUseProgram(0);
    }
 
    //glDisable(GL_TEXTURE_2D);
}
 
    glUseProgram(0);
    //glDisable(GL_TEXTURE_2D);
}