0

I'm starting with OpenGL and at the moment, my Program draws a Cube with GL_TRIANGLES (therefore, no code example; it's trivial and would only waste your time). My Question is:

Is it possible to reuse vertices so I need only 8 instead of 12*3 (or 14 with GL_TRIANGLE_STRIP) to draw a Cube? And if it's possible, how can I set multiple colors for one vertex (I want to only reuse the position data, but each triangle should have different colors.)

I tried to find information online, but didn't find anything about reusing vertices for different triangles (only to reuse them by changing the position matrix).

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
Bad.Tux
  • 3
  • 1
  • 1
    The vertices can include position, colour, material (texture) and normal information so if these are different for for each triangle then you need unique vertices. – Richard Critten Apr 22 '19 at 11:35
  • @RichardCritten But I could change the colorbuffer everytime, couldn't I? But that helps me only if I can reuse vertices at all. – Bad.Tux Apr 22 '19 at 11:36
  • 2
    You can use indices to share vertex data between multiple primitives, take a look at http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-9-vbo-indexing/ – Jack Apr 22 '19 at 11:37
  • @Jack So I can use an index buffer and change the color attributes of the vertices between the draw calls? – Bad.Tux Apr 22 '19 at 11:45
  • 1
    @Bad.Tux No, don't change the colors. Duplicate the vertices. It is much more performant to duplicate vertex coordinates than to swap the content of vertex buffers. – Rabbid76 Apr 22 '19 at 11:48
  • 1
    @Bad.Tux Trying to minimise the number of draw calls is the way to improve performance, so you have a trade-off: space (more vertices) vs performance (more draw calls). If you share vertex data and change it between draw calls, you also need to change it back; you are just creating long term problems for your code-base. – Richard Critten Apr 22 '19 at 11:48
  • Means: The solution of my problem is possible, but not practical, because there is no real possibility to save more than one color vector in one vertex (and render it, of course). Does this description fit my issue? – Bad.Tux Apr 22 '19 at 12:07
  • With current GPUs, you can easily do this in the vertex shader. But I don't recommend it, as it will be slow. GPUs have transformed vertex cache, which would not be utilized, if you do the indexing in the vertex shader (and maybe there are other drawbacks as well). – geza Apr 22 '19 at 12:13
  • @geza Do you know a good documentation for that? – Bad.Tux Apr 22 '19 at 12:32
  • @Bad.Tux: nope, unfortunately. Here's the general idea: you can use `gl_VertexID` as an index in vertex shader. And put vertex data into Shader Storage Buffer Objects. This way you can replicate indexing functionality in vertex shader. And you can have several index buffers (one for the position, and one for the color/other stuff), not just one. But again, I don't recommend this method, as it will be slower than the straightforward way (duplicate same-position vertices, if any attributes differ). – geza Apr 22 '19 at 12:54
  • @geza What is the cause for the slowness? The frequently sending of data to the GPU? – Bad.Tux Apr 22 '19 at 12:57
  • @Bad.Tux: no, this technique doesn't need frequent sending of data (if data doesn't change). Various cache performance will be slower than the straightforward way. – geza Apr 22 '19 at 13:26

0 Answers0