3ds Max C++ API Reference
IOSLRaytracingInterface Class Referenceabstract

Interface for the 3ds Max OSL Raytracing features, allowing OSL to use a 3rd party renderer's raytracing engine. More...

#include <MaxOSLInterface.h>

Public Types

enum  ReturnFlags { HitSomething = 1 }
 Bit values for the return value of TraceRay. More...
 
enum  TraceFlags { Shade = 1 }
 Bit values for the flags parameter of TraceRay. More...
 

Public Member Functions

virtual int TraceRay (Ray &ray, float min_dist=-1, float max_dist=-1, int flags=0, const char *trace_set=nullptr, float *hit_dist=nullptr, Point3 *hit=nullptr, Color *hitCol=nullptr, Point3 *hitNormal=nullptr, Point3 *hitUVW=nullptr)=0
 The TraceRay call - to be implemented by the Renderer. More...
 

Detailed Description

Interface for the 3ds Max OSL Raytracing features, allowing OSL to use a 3rd party renderer's raytracing engine.

This interface should be exposed by the renderer on the ShadeContext it passes to the OSL Map's EvalColor/Mono/NormalPerturb() methods. The OSL Map rendering service callback for the OSL trace() call will get the interface with

#define MAXOSL_RAYTRACE_INTERFACE
Interface ID for the 3ds Max OSL Raytracing features, allowing OSL to use a 3rd party renderer's rayt...
Definition: MaxOSLInterface.h:50
virtual UtilExport BaseInterface * GetInterface(Interface_ID id)
Interface for the 3ds Max OSL Raytracing features, allowing OSL to use a 3rd party renderer's raytrac...
Definition: MaxOSLInterface.h:311

and call its IOSLRaytracingInterface::TraceRay method to perform raytracing.

Member Enumeration Documentation

◆ ReturnFlags

Bit values for the return value of TraceRay.

Enumerator
HitSomething 

Something was hit.

314  {
315  HitSomething = 1
316  };
@ HitSomething
Something was hit.
Definition: MaxOSLInterface.h:315

◆ TraceFlags

enum TraceFlags

Bit values for the flags parameter of TraceRay.

Enumerator
Shade 

Run the shader at the hit point.

318  {
319  Shade = 1
320  };
@ Shade
Run the shader at the hit point.
Definition: MaxOSLInterface.h:319

Member Function Documentation

◆ TraceRay()

virtual int TraceRay ( Ray ray,
float  min_dist = -1,
float  max_dist = -1,
int  flags = 0,
const char *  trace_set = nullptr,
float *  hit_dist = nullptr,
Point3 hit = nullptr,
Color hitCol = nullptr,
Point3 hitNormal = nullptr,
Point3 hitUVW = nullptr 
)
pure virtual

The TraceRay call - to be implemented by the Renderer.

The OSL language contains a trace() call, and the renderer being used to render the OSL Map can choose to implement the trace() call in its own interal raytracing functionality. If this is not done, a fall back functionality is used, which is not very optimized (effectively, RenderGlobalContext::IntersectWorld is used!!). The Renderer needs to accept a GetInterface(MAXOSL_RAYTRACE_INTERFACE) on the ShadeContext it provides, and implement the IOSLRaytracingInterface and this function.

The function returns ReturnFlags and the HitSomething bit defines if the ray hits something or not.

If the OSL trace call has the "shade" value set, the TraceFlags::Shade bit is set. The other bits in the TraceFlags are currently unused and are reserved for future expansion.

In the Scanline renderer implementation, if TraceFlags::Shade=0, only hit_dist is filled in. However, if TraceFlags::Shade=1, the hit, hitCol and hitNormal are also filled in. These can be retrieved in the OSL code by doing

getmessage("trace", "P", val); // Get the point
getmessage("trace", "N", val); // Get the normal
getmessage("trace", "color", val); // Get the resulting color

respectively.

Note
Note that getting "P" and "N" are part of the OSL specification, but the ability to get the "color" is an extension made by 3ds Max.
Returns
The function returns a TraceFlags bitmask of which currently only the HitSomething bit is defined, and means that the ray hit something.
Parameters
rayThe ray to be traced
min_distThe minimum distance (basically, ray start offset)
max_distThe maximum distance (do not find hits beyond this distance)
flagsFlags for the rendering. Currently only the Shade flag is supported. If that is set, the surface shader will be invoked at the hit point, and, additional values can be retrieved after the trace call
trace_setThis is the char * version of the ustring that contains the trace set. Since it originates in an ustring, one can rely on the same pointer also containing the same string, so after checking a string's actual value once, one can resort to a pure pointer compare moving forward. IOSLGlobalInterface::GetUString() can also be used to compute ustring pointers up-front.
hit_distIf non-NULL, filled in with the distance to the hit point.
hitIf non-NULL, and Shade bit was 1, filled in with the world space hit point.
hitColIf non-NULL, and Shade bit was 1, filled in with the color resulting by tracing the ray.
hitNormalIf non-NULL, and Shade bit was 1, filled in with the normal of the traced object.
hitUVWIf non-NULL, and Shade bit was 1, filled in with the UVW coordinates of the traced object.