1
votes

OK, so I made a library that handles basic OpenGL rendering. It contains three buffers, one for vertices, uv's and normals. In the main render function is where I bind the buffers, enable vertex attributes, and then call glDrawArrays(). But I ran into an unexpected issue that is more engineering based then code based which is that I can only bind one texture at a time. So all the vertices will have the same textures when I draw them, How can I get around this? If you need any more source I will post it.

Main Render Function

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,this->_VBO);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER,this->_TBO);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER,this->_NBO);

glDrawArrays(GL_TRIANGLES,0,this->polyCount);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);

Buffers:

//Designated Vertex Buffer
    std::array<GLfloat,BUFFER_SIZE> BUFFER_1;
    UINT bufferIndex1;

    //Designated Normal Buffers
    std::array<GLfloat,BUFFER_SIZE> BUFFER_2;
    UINT bufferIndex2;

    //Designated UV Buffer
    std::array<GLfloat,BUFFER_SIZE> BUFFER_3;
    UINT bufferIndex3;
1
You could bind as many as GL_MAX_TEXTURE_UNITS textures at once. However, yes - state cannot be changed within one draw call (and that's the whole idea of what 'batch' is). Either take e.g. vertex color (or any other attribute) as texture index and take one texture out of many based on it (could be slow, depending on actual data) or split it into separate draw calls. - keltar

1 Answers

0
votes

What we would normally do here is split the mesh up into sub-meshes, by "material". An object with a different texture is technically a different material, so you would render it separately. You can render a single mesh using multiple textures by using something called an Atlas. That is to say, you pack your textures into a single bitmap and choose texture coordinates within that bitmap depending on the texture you want to use.