Archived

This topic is now archived and is closed to further replies.

Vertex Arrays & Binding Textures

This topic is 5504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically, I need to know how to bind textures when using a Vertex Array. The only way I can think of doing it is to group the Vertex Array by texture number, then use glDrawArrays to draw all of the vertices that use each number. Eg. Something like (this more of an algorithm than real code):
  
// Define A Structure To Contain Texture Groups

struct TextureIndex
{
    GLint startIndex;
    GLsizei count;
}

// Number Of Textures In Use

unsigned int numTextures = MAXTEXTURES;

// Create An Array Of Texture Indices

TextureIndex *texIndices=new TextureIndex[numTextures];

// ****************

// Enable / Create / Populate Arrays Somewhere Here

// ****************


// Draw Vertices

for(int tex=-1; ++tex < numTextures; )
{
    // Bind The Texture

    glBindTexture(GL_TEXTURE_2D, tex);

    // Draw All Vertices That Use The Current Texture

    glDrawArrays(GL_TRIANGLES,
                   texIndices[tex].startIndex, 
                   texIndices[tex].count);
}

  
What I want to know, is this the way it is done or is there a way that I can get OpenGL to manage it for me?

Share this post


Link to post
Share on other sites
Specify all your arrays with position and tex coord data as per normal. Then call glDrawRangeElements which will draw only specific sets of vertices that u specify. Make a call for each different texture you have, so u would bind a texture, then draw the range of vertices for that texture, then bind the next texture and draw the next range and so on and so forth until all your vertices are drawn and textured correctly.

I''m sure there''s a million articles on tetxuring vertex arrays on the net and in books. Go and have a look for them. They can often answer your questions in a much more clear and concise manner than anyone here will take the time to do (not meant as a derogatory statement to the people on this board) and it will save us all time.

Share this post


Link to post
Share on other sites
Thanks MelvinElvin, but thats what I was getting at with the code I posted.

I already checked google, but all the sites I found used Vertex Arrays with either non-textured scenes or scenes that use only one texture.

I just figured there could''ve been a way that OpenGL vertex arrays could manage the texture to be bound as well.

Share this post


Link to post
Share on other sites
Here is how I did it in my Quake rendering engines.

First we start off with a list of faces that need to be drawn. I am assuming they are in no particular order and each face holds an index into an array of textures.

First you need to sort the array by texture index. I would suggest using QuickSort. There is a qsort routine in the standard C library but I suggest you find an algorithms book and write your own. I wrote my own and saw a speed improvement over the qsort in the standard C library.

Now you have a list of faces that are sorted by texture.

First set your texture to the first texture in the face list.
glBindTexture(blah, blah);

Next you start iterating through the faces, adding vertices to you vertex array and texture coord array. This happens until either you fill one of your buffers or the texture index changes. In this case you draw the array using glDrawArrays and reset the buffer length to 0. Then if the texture index changed you bind the new texture and start again until you have no more faces to draw.

Hope this helps.

-dizzy

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
When you compile a vertex array, it will be compiled with the current bound texture?

Share this post


Link to post
Share on other sites