Question

So I have successfully drawn a series of lines using a two VBOs one for the vertices and one for the indexes. The Python lists are:

vertices = [
            [0,1],[0,2],[0,3], # Axes 1
            [1,4],[1,5],[1,6], # Axes 2
            [2,10],[2,11],[2,12] # Axes 3
           ]

indexes = [
           0,4,4,7, # Line 1
           1,4,4,8, # Line 2
           3,6,6,9 # Line 3
          ]

I then convert these to a numpy array and aso to VBOs:

 vertexarray = np.array(vertices, dtype=np.float32)
 indexarray = np.array(indexes, dtype=np.int32)

 vertexvbo = glvbo.VBO(vertexarray)
 indexvbo = glvbo.VBO(indexarray, target=GL_ELEMENT_ARRAY_BUFFER)

And then I bind and draw these lines using:

glLineWidth(1)
glColor4f(1,1,1,1)   

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_BLEND)

glEnableClientState(GL_VERTEX_ARRAY)
vertexvbo.bind()
indexvbo.bind()
glVertexPointer(2, GL_FLOAT, 0, None)

glDrawElements(GL_LINES, len(indexarray), GL_UNSIGNED_INT, None)

glDisableClientState(GL_VERTEX_ARRAY)
glDisable(GL_BLEND)

As this picture shows (the bold white lines are just axes that are drawn using standard OpenGL commands):

Uncolored Lines

Now the problem comes when I try and colour these. As each of the 3 lines is made up of 2 smaller lines, I create a color array of size 3:

colors = []

for i in range(0,3):
        r = random.uniform(0.1,1)
        g = random.uniform(0.1,1)
        b = random.uniform(0.1,1)
        color = [r,g,b,0.7]
        colors.append(color)

And then a color index array with the following values:

colorindexes = [
                0,0,0,0, # Line 1
                1,1,1,1, # Line 2
                2,2,2,2 # Line 3
               ]

I then convert these to numpy arrays and create VBOs:

colorarray = np.array(colors, dtype=np.float32)
colorindexarray = np.array(colorindexes, dtype=np.int32)

colorvbo = glvbo.VBO(colorarray)
colorindexvbo = glvbo.VBO(colorindexarray, target=GL_ELEMENT_ARRAY_BUFFER)

In order to then bind and use this VBO I add the following code before the glEnableClientState(GL_VERTEX_ARRAY) line from the previous code:

glEnableClientState(GL_COLOR_ARRAY)
colorvbo.bind()
colorindexvbo.bind()
glColorPointer(4, GL_FLOAT, 0, None)

However the lines don't color properly, as the following picture shows, (the data for the lines is random so these lines aren't supposed to intersect at the same points as in the previous picture):

Colored Lines

Was it helpful?

Solution

Just a heads up, "indexed colour" actually has a completely different meaning from what you are implying here. At first, I thought you were actually using indexed colors instead of RGB[A]. What you are doing here is drawing vertices (with color) using an element array buffer, which you refer to as indices. It may sound pedantic, especially since almost nobody uses index color modes these days, but it definitely caught me off guard initially.

As for the actual question, I have spent some time looking over your code and I think you are under the impression that OpenGL uses a separate element buffer to index each type of vertex attribute.

This is not the case, while you can use multiple VBOs to store each vertex attribute, they function as if they were one single array of structured data. That is, you cannot do something like select vertex position #0, vertex color #4 and vertex normal #2 by using separate element arrays. You have a single element array and it always pulls each vertex attribute from the same index.

Unfortunately, the behavior discussed above means that when you have vertices that share some, but not all, attributes you wind up having to make two discrete vertices. More importantly, it means that your color array should have the same length as your position array and you only use a single index array to draw using both.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top