4

I'm reading through and trying to learn shaders, but I suddenly got confused with passing information into the shader.

What is the difference between glVertexAttrib and glVertexAttribPointer? Are there different times to use them? Are they just two different ways to do the same thing? Does one provide better or worse performance?

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
David Torrey
  • 1,335
  • 3
  • 20
  • 43
  • Related: http://stackoverflow.com/q/7718976/743214 (though not sure if it can be considered a duplicate). – Christian Rau Nov 28 '12 at 15:12
  • thanks for pointing me to that. It's pretty similar. It's so hard to find straight answers in OpenGL. Even the red book is confusing (at least 7th edition) because it mixes depreciated methods in with current methods, and doesn't do that good of a job of highlighting what is and isn't depreciated, which functions are the new ones that replaced the depreciated ones, etc.... – David Torrey Nov 28 '12 at 16:29
  • 1
    Yeah, indeed (and sadly enough) the *Red Book* isn't **the** resource for learning OpenGL anymore, for exactly the reasons you describe. Either learn only the modern stuff without ever looking at the deprecated things (which is what I would suggest a complete newcomer), or learn the deprecated things first and then what replaces them in modern OpenGL. But the *Red Book*'s approach of mixing those things is not the best idea. If you want a modern book concentrating on future-ready features (GL 3+ core) only, I suggest the **newest** edition of the *Superbible*. – Christian Rau Nov 28 '12 at 18:15
  • There is new red book concerning GL 4+ – Bartek Banachewicz Nov 28 '12 at 20:26
  • that one's not out until march, isn't it? I pre-ordered 8th edition and it said it was delayed – David Torrey Nov 28 '12 at 22:32
  • 1
    @BartekBanachewicz Well, the current edition (7th) also explains modern things (GL 3+), but in the same way describes all the deprecated stuff. This results in the sentence *"many of the techniques and functions described in this chapter were removed through deprecation"* at the top of nearly every chapter, which is just rubbish. So we'll see how the 8th edition does, but I doubt that it will be any better, since for the sake of completeness they will not drop the old stuff, even if for the sake of didactics they should. But maybe this time they'll structure it a bit more clearly. – Christian Rau Nov 29 '12 at 10:55
  • ^ this. it would be much more helpful if they just had at the beginning a giant marked off section called 'depreciated', then have a new section in giant bold letters that says 'modern techniques'. As it is, you have to try to figure out what the current method is. Even if they just said at the beginning of an explanation of a function 'this is depreciated' it would be more helpful – David Torrey Nov 29 '12 at 11:15

1 Answers1

4

glVertexAttrib sets the value of an attribute for a given set of vertices.

glVertexAttribPointer sets the location of the attribute for every vertex.

So essentially these two are different functions, and you can't compare them in terms of speed. Anyway, setting the attribute pointer isn't the bottleneck in most rendering appliances.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • so does that mean that VertexAttribPointer is used with VBO's essentially, and VertexAtrrib would be used to send specific application variables? – David Torrey Nov 28 '12 at 13:49
  • More or less, yeah. You would use `Pointer` to create a triangle with mixed RGB colors, and `Attrib` to create a blue triangle :) – Bartek Banachewicz Nov 28 '12 at 14:14
  • so how much of a hit does vertexattrib have since it has to go to the application pull a variable then send it to GLSL? with Pointer it's no problem because it's in a VBO. Is it possible to put custom information (for instance a variable telling GLSL to render the triangle blue) into the same VBO, then pass it over to GLSL via Pointer or some other function? – David Torrey Nov 28 '12 at 14:24
  • What I mean by the above, is say I have 5 objects that all use the same mesh. I would store the vertex positions, normals, etc... into the VBO and pass it over. What if I wanted to use a separate texture for each object? is it possible to send the state of the object via VBO to GLSL so it knows which texture to pull? – David Torrey Nov 28 '12 at 14:26
  • Textures are sent by `uniform sampler*`, not by VBO. They are, essentially, per-pixel. You usually want to send per-vertex attributes via VBO. – Bartek Banachewicz Nov 28 '12 at 14:30
  • 1
    @BartekBanachewicz Well, strictly spoken the texture/sampler itself is not per-pixel but per-object (or rather per-drawcall), what you do with it is per-pixel, but so is everything done in a fragment shader. – Christian Rau Nov 28 '12 at 15:17
  • So say I'm combining several objects with different textures in a single draw call? In that case id think creating a mosaic and sending it,then passing the texture to pull in the mosaic via another vertexattribpointer should work, correct? – David Torrey Nov 29 '12 at 04:00