Search code examples
cudatexturesoptix

how to draw texture on obj model through optix example


I'm very new to optix and cuda. I'm trying to modify optix SDK example to present a 3D model with ray tracing. I modified the "progressivePhotonMap" example. Because of lacking of optix/cuda knowledge, I don't know how to draw texture on the 3D model, can anyone who is familiar with SDK example could help me?

I read other draw texture examples like "swimmingShark" or "cook" and try to find out clue to use. However, those examples seem has different way to draw texture.

From now on, I know i have to load texture in cpp file

GeometryInstance instance = m_context->createGeometryInstance( mesh, &m_material, &m_material+1 );
instance["diffuse_map"]->setTextureSampler(loadTexture( m_context, ... );

and create TextureSampler in cuda file

rtTextureSampler<float4, 2>      diffuse_map; // Corresponds to OBJ mtl params

,and give them texcoord to draw, like this,

float3 Kd = make_float3( tex2D( diffuse_map, texcoord.x*diffuse_map_scale, texcoord.y*diffuse_map_scale ) );

However, I cannot found where the texcoord get the texture coordinate data in cuda file. It seems there should be some code like this in .cpp file

GI["texcoord"]->setBuffer(texcoord)

Could anyone teach me where texcoord get the texture coordinate data, and how to match coordinate data and texure to present 3D model with ray tracing? I can't find tutorial in google, I really need help or direction to reach my goal. Thank you.


Solution

  • You should read up on the OptiX documentation first. Specifically the paragraph regarding Attribute variables.

    IIRC the texcoord variable is an attribute of the form

    rtDeclareVariable( float3, texcoord, attribute texcoord );
    

    that is computed in the intersection program and passed along to the closest hit program (attributes are designed to pass data from the intersection point to the shading points).

    Short answer: it is set into another CUDA function which, conceptually, computes some data needed by that line.