SirYancy
SirYancy

Reputation: 167

LWJGL glVertexAttribPointer index switching

As I'm learning OpenGL through LWJGL, I've been running through the tutorials on the LWJGL wiki. Specifically, this one.

My problem is, that despite the fact that I have duplicated that code, call for call, function for function, the two vertex attribute pointers need to be switched (so the code looks like this:

GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, Vertex.sizeInBytes, 
    // Put the colors in attribute list 1
GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, Vertex.sizeInBytes,
    Vertex.elementBytes * 4);

Switching it so that the position index is 1 and the color index is 0, makes a nice gradient render, as expected. However, if I run it with the indices switched (the way I think it should work, I get this:

Confusing insanity

Clearly, after studying this render for a while, the position and color data have been switched, and thus, switching the index numbers completely solves the problem, but I'm pretty sure I've followed everything correctly. What is wrong with the code? Why is this being such a bastard?

Upvotes: 0

Views: 1778

Answers (1)

Tim
Tim

Reputation: 35933

You should not be hardcoding attribute id's like that, and just hoping that they end up linked to the right shader attributes.

You need to either use glBindAttribLocation (before glLinkProgram), or glGetAttribLocation, so that you can map shader variables to their id number.

Upvotes: 1

Related Questions