Triangular Vertex co-ordinates of .obj Geometry

Hello developers,
Thank you for all your support throughout my project, Now I have been working on irregular objects.
I have imported an .obj file as a model and used “mesh intersect program” as an intersection like this:

GeometryGroup geometrygroupobj = m_context->createGeometryGroup();

 InsertModel(prog_path, geometrygroupobj, box_matl, mesh_intersect, m0, true);
InsertModel("C:/ProgramData/NVIDIA Corporation/OptiX SDK 3.8.0/SDK/tutorial/Amago.obj", geometrygroupobj, box_matl, mesh_intersect, m0, false);

My insert Model program is:

void Tutorial::InsertModel(const std::string& name, GeometryGroup ggroup, Material mat, Program mint, const optix::Matrix4x4 matx, bool suppressErrors)
	try {
		OptiXMesh mesh(m_context, ggroup, mat, m_accel_desc);
	catch (...) {
		if (suppressErrors)

and my Mesh Intersect program is :

RT_PROGRAM void mesh_intersect( int primIdx )
  int3 v_idx = vindex_buffer[primIdx];


  float3 p0 = vertex_buffer[ v_idx.x];
  float3 p1 = vertex_buffer[ v_idx.y];
  float3 p2 = vertex_buffer[ v_idx.z];

  // Intersect ray with triangle
  float3 n;
  float  t, beta, gamma;
  if( intersect_triangle( ray, p0, p1, p2, n, t, beta, gamma ) ) {

    if(  rtPotentialIntersection( t ) ) {

      // Calculate normals and tex coords 
      float3 geo_n = normalize( n );
      int3 n_idx = nindex_buffer[ primIdx ];
      if ( normal_buffer.size() == 0 || n_idx.x < 0 || n_idx.y < 0 || n_idx.z < 0 ) {
        shading_normal = geo_n;
      } else {
        float3 n0 = normal_buffer[ n_idx.x ];
        float3 n1 = normal_buffer[ n_idx.y ];
        float3 n2 = normal_buffer[ n_idx.z ];
        shading_normal = normalize( n1*beta + n2*gamma + n0*(1.0f-beta-gamma) );
      geometric_normal = geo_n;

      int3 t_idx = tindex_buffer[ primIdx ];
      if ( texcoord_buffer.size() == 0 || t_idx.x < 0 || t_idx.y < 0 || t_idx.z < 0 ) {
        texcoord = make_float3( 0.0f, 0.0f, 0.0f );
      } else {

        float2 t0 = texcoord_buffer[ t_idx.x ];
        float2 t1 = texcoord_buffer[ t_idx.y ];
        float2 t2 = texcoord_buffer[ t_idx.z ];
        texcoord = make_float3( t1*beta + t2*gamma + t0*(1.0f-beta-gamma) );

      refine_and_offset_hitpoint( ray.origin + t*ray.direction, ray.direction,
                                  geo_n, p0,
                                  back_hit_point, front_hit_point );


Now I want to store all the vertex of triangular primitives which made up a geometry, in a buffer, I want to emit my rays from each of these vertex.
As I want this information before launch of context so I cannot use intersection programs for stroing primitve coordinate.

Can any one please help me with this issue.

// blank

It looks like you already have all of the vertices of your triangular primitives in a buffer called vertex_buffer. You can make vertex_buffer a context variable instead of a program variable in order to have it available to your ray generation program. Is that what you want?

Yes I want the same thing, Can you please elaborate how can I make “vertex_buffer” a context variable? Thank you for your reply

You create a context variable with rtContextDeclareVariable. You create a program variable with rtProgramDeclareVariable. That is the only difference.

Thank you for your reply, In my program I have initiated “vertex_buffer” as a BUFFER not a variable like this:

rtBuffer<float3> vertex_buffer;

Now how can I copy this buffer to my Ray generation program?

or should I import the coordinates from .obj file directly? But that would be inefficeint

A buffer IS a variable.

By adding the same line to your ray generation program.

Sorry I cannot understand your explanation, I have made a buffer in my ray generation program:

rtBuffer<float3,1>           vertex_buffer;

but how the data of from that buffer to this new buffer?

I think you misunderstand. It is not a new buffer, it is another reference to the same buffer that you have already made.

In order to use the buffer from both programs, you have to make sure that it is declared as a context variable, and not as a program variable. That is why, if it is currently declared in your CPU code as a program-specific variable with rtProgramDeclareVariable, you must change it to be declared as a context-wide variable with with rtContextDeclareVariable.

Sorry I am confused with buffers and variables:
First thing:
I haven’t initiated any “vertex_buffer” in my .cpp file (CPU program).
It is automatically created by OPTIX:MESH function . so how can I restore the data from that buffer.


vertex buffer is only initiated in .cu programs as rtBuffer not as rtProgramDeclareVariable. can you please give some code how can I do this task?

You will have to edit the OPTIX:MESH function (or write your own function to do the same thing).

It is declared with rtProgramDeclareVariable or rtContextDeclareVariable somewhere, probably in your OPTIX:MESH function, otherwise your mesh_intersect program would give you an error at runtime. Assuming it’s declared using rtProgramDeclareVariable (which you still need to check), then you must edit the OPTIX:MESH function (or wherever it is declared) to make the variable visible to the entire context.

Also, since you seem to be using c++, you should note that there are convenience methods for rtProgramDeclareVariable and rtContextDeclareVariable, namely program->declareVariable and context->declareVariable.

Thank you so much, it worked, I have edited Optix:Mesh function.
Thanks for your help.