0

Let's say I have the following arrays (each of them with arbitrary lengths) and use them as spare-parts bins; put each of them in the buffer and then have openGL read the indices in the elements array and construct vertices by cherry-picking from each of the array bins.

We could construct one vertex by using
{coordinate v1, normal vn3, color c1, texCoord vt4}

Or we could construct another vertex using
{coordinate v1 again, normal vn3 again, color c3, texCoord vt2}

Note that "coordinates array" is not yet a vertex, it merely supplies a position to construct a vertex.

Is such thing going to be extremely difficult (for a beginner) in modern openGl using buffers and shaders?

CoordinatesArray:
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000

NormalsArray:
vn 0.000000 0.000000 -1.000000
vn -1.000000 -0.000000 -0.000000
vn -0.000000 -0.000000 1.000000
vn -0.000001 0.000000 1.000000
vn 1.000000 -0.000000 0.000000
vn 1.000000 0.000000 0.000001

ColorsPallete:
c 1.0,1.0,1.0
c 0.0,1.0,0.0
c 0.2,0.3,0.2

TextureCoordArray:
vt 0.748573 0.750412
vt 0.749279 0.501284
vt 0.999110 0.501077
vt 0.999455 0.750380
vt 0.250471 0.500702
vt 0.249682 0.749677
vt 0.001085 0.750380
vt 0.001517 0.499994
vt 0.499422 0.500239
vt 0.500149 0.750166
vt 0.748355 0.998230
vt 0.500193 0.998728
vt 0.498993 0.250415
vt 0.748953 0.250920

IndexRecipiesArray:
f {1,2,1,4}, {2,2,1,4}, {3,2,1,4}
f {2,2,1,4}, {3,2,1,4}, {4,2,1,4}
f {4,5,1,4}, {5,2,1,4}, {6,2,1,4}
f {6,2,1,4}, {7,2,1,4}, {8,2,1,4}
f {1,2,1,4}, {1,2,1,4}, {1,2,1,4}

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Thomas An
  • 503
  • 2
  • 7
  • 17

1 Answers1

5

This has come up in a similar form a number of times. But I couldn't find an exact duplicate, so I will answer it anyway.

OpenGL does not directly support this. It can only have a single index array, which means that you need a vertex for each unique combination of attributes.

There is an extension that comes fairly close: AMD_interleaved_elements. It does introduce the concept of per-attribute indices. But there are a couple of problems with using it:

  • As the name suggests, it's vendor specific. So you could not use it as the only rendering path you support, unless you can live with your code only working on a limited set of hardware.
  • It has serious constraints. It only supports two 16-bit indices or four 8-bit indices. Since two indices are not enough for the case you describe, you would be limited to 256 vertices per draw call. Which is rarely sufficient for anything interesting.

Therefore, the typical approach is to generate the unique attribute combinations and corresponding indices in your own code, before feeding the vertex data to OpenGL. I posted some pseudo-code for this task in an answer to this question: OpenGL - Index buffers difficulties.

Community
  • 1
  • 1
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • Thank you Sir ! It's a direct confirmation of what I was afraid of. Based on this, I am going to have to rethink my design strategy from the ground up. – Thomas An Jul 10 '15 at 02:28