 # OptiX 6 - Vertex position

Hello,

When creating geometry triangles objects, we have two ways to define their vertices positions, as explain in the programming guide https://raytracing-docs.nvidia.com/optix_6_0/guide_6_0/index.html#host#3519

The simplest is to only put coordinates into the vertex_buffer, and each triplets of vertices correspond to a triangle.

The second one is to put all the coordinates into the vertex_buffer in an arbitrary order, and to add an index buffer who will give the correspondence between three vertices and a triangle.

If I understood them correctly, all OptiX examples use the second method, and in the “mesh_attributes” program, get the triangle coordinates through the index_buffer.

As I don’t need the textcoord or the normal attributes for the vertices, I use the first method, without the index_buffer.
In the “mesh_attributes” program, I get the vertices position like this:

``````int primIdx = rtGetPrimitiveIndex();

const float3 v0 = vertex_buffer[primIdx];
const float3 v1 = vertex_buffer[primIdx+1];
const float3 v2 = vertex_buffer[primIdx+2];
``````

It seems to be correct. Can someone confirm it?

Thanks,
Arnaud

That vertex_buffer indexing is incorrect.

If you’re not using an index buffer, your triangle vertex data are just independent triangles and each triplet of vertices is one triangle. So far correct.
But the number of geometric primitives is the number of triangles, not the number of vertices, set with rtGeometryTrianglesSetPrimitiveCount:
https://raytracing-docs.nvidia.com/optix_6_0/api_6_0/html/optix__host_8h.html#a39c1cd436b2d2efde89bf3f2947396b7
which means the primitive index needs to be multiplied by three to get the proper index into the vertex buffer, also it’s an unsigned int:

``````    // Each triangle primitive is stored in three consecutive vertices.
const unsigned int vertexIndex = rtGetPrimitiveIndex() * 3;

const float3 v0 = vertex_buffer[vertexIndex    ];
const float3 v1 = vertex_buffer[vertexIndex + 1];
const float3 v2 = vertex_buffer[vertexIndex + 2];``````
1 Like