vpSetShadowLookupShader(3)

πŸ’– vpSetShadowLookupShader πŸ’–

okay bestie, gather round, because this is the <volpack.h> function that makes your volume render shadows and honestly? it's giving main character energy. this is vpSetShadowLookupShader(3) from the Library Functions Manual and we're gonna get into it. 🫢


✨ NAME ✨

vpSetShadowLookupShader β€” specify shading lookup tables for rendering shadows


🌸 SYNOPSIS 🌸

#include <volpack.h>

vpResult
vpSetShadowLookupShader(vpc, color_channels, num_materials, color_field,
        color_table, color_table_size, weight_field, weight_table,
        weight_table_size, shadow_table, shadow_table_size)
    vpContext *vpc;
    int color_channels, num_materials;
    int color_field;
    float *color_table;
    int color_table_size;
    int weight_field;
    float *weight_table;
    int weight_table_size;
    float *shadow_table;
    int shadow_table_size;

πŸŽ€ ARGUMENTS πŸŽ€

  • vpc β€” your VolPack context, the one you got from vpCreateContext. your whole vibe lives in here.
  • color_channels β€” how many color channels per pixel. it's 1 or 3, pick one, no in-between.
  • num_materials β€” how many material types you've got.
  • color_field β€” the field number for the voxel field holding the color lookup table index.
  • color_table β€” the color lookup table itself. queen.
  • color_table_size β€” size of the color lookup table, in bytes.
  • weight_field β€” the field number for the voxel field holding the material weight lookup table index.
  • weight_table β€” the material weight lookup table.
  • weight_table_size β€” size of the material weight lookup table, in bytes.
  • shadow_table β€” the shadow color lookup table. the extra one. the moment. πŸ‘‘
  • shadow_table_size β€” size of the shadow color lookup table, in bytes.

🌈 DESCRIPTION 🌈

so vpSetShadowLookupShader is the one you call to set up the lookup tables that define a shading function for rendering a volume with shadows. lowkey important: you use this instead of vpSetLookupShader when shadows are enabled. don't mix them up, bestie, i'm begging. πŸ™ˆ

VolPack supports a fast, one-pass shadow algorithm. the algorithm computes the fraction of light from a given light source that reaches each voxel. that fraction then gets used to attenuate the diffuse and specular shading terms associated with that light source, which produces a dark shadow in the areas that are hidden from the light source. and the way it does all this is with lookup tables, so most of the shading calculation can be precomputed. slay.

now here's the tea β˜• β€” to compute shadows in a single pass, the algorithm puts a restriction on the direction of the light source: the light casting the shadows must not be more than 45 degrees from the viewing direction. and if the angle creeps up toward 45 degrees, the quality of the shadows may degrade. also, the current implementation allows shadows to be cast from only one light source. you can enable additional lights, sure, but they will not cast shadows. no cap.

πŸ¦‹ how to actually make a rendering with shadows πŸ¦‹

the following steps must be performed, in this order, no skipping:

  1. Call vpSetShadowLookupShader to define the lookup tables (there's a whole discussion below, keep reading).
  2. Call vpSeti with the VP_SHADOW_LIGHT option to specify which light will cast shadows. the current implementation only allows one light to be specified.
  3. Call vpSeti with the VP_SHADOW_BIAS option to set the shadow bias value (also discussed below, stay with me).
  4. Call vpEnable with the VP_SHADOW option to enable shadows.
  5. Call vpShadeTable as usual to initialize the lookup tables.
  6. Call one of the rendering routines. go off. πŸ’…

πŸ“ the tables themselves πŸ“

vpSetShadowLookupShader defines the lookup tables required for the shading and shadowing algorithm. the first nine arguments are identical to the arguments for vpSetLookupShader (peep the corresponding man page). the remaining two arguments specify an additional color lookup table, shadow_table, with the same dimensions as color_table. the contents of the table will be initialized by vpShadeTable.

πŸ’€ shadow bias, aka the self-shadowing situation πŸ’€

the call to vpSeti with the VP_SHADOW_BIAS option specifies an offset to eliminate self-shadowing. and honestly? self-shadowing is an intrinsic problem when implementing shadow algorithms for volume rendering. not me being dramatic, it's just facts.

consider a single voxelized object consisting of an opaque core surrounded by a "halo" of lower-opacity voxels (which is necessary for the scene to be band-limited). as a light ray pierces the halo its strength is attenuated. by the time the light reaches the high-opacity region, a significant fraction of the light may be obscured β€” resulting in a general darkening of the image even if no shadows should be present. 😭 the problem can be corrected by moving the shadow a small distance along the direction of the light rays. VP_SHADOW_BIAS specifies the distance of the bias in units of voxels.

the optimal value depends on the data set. increase the bias until it has no more effect on overall image brightness, but do not increase it too far or small features in the data will no longer produce correct shadows. periodt.

🧚 what vpShadeTable does when shadows are on 🧚

vpShadeTable initializes the shading lookup tables, and it operates differently when shadows are enabled. instead of computing one color for each surface normal vector and storing the results in color_table, the routine computes two color terms:

  • the first term is the portion of the voxel color due to the diffuse and specular components of the shadow light. this value is stored in shadow_table.
  • the second term contains the contribution of all other light sources and the ambient light term, and is stored in color_table.

during rendering the color of a voxel is computed by extracting a surface normal from the voxel, using the surface normal to index both color_table and shadow_table, attenuating the value from shadow_table by the local strength of the shadow light, and then adding the two terms together. the local strength of the shadow light is found by extracting a value from the shadow buffer, an internal data structure that is updated during rendering. iconic behavior, truly.

the values in the shading lookup tables may be initialized before or after calling vpSetShadowLookupShader. typically vpSetShadowLookupShader is called once at the beginning of a rendering session, and then vpShadeTable is called whenever the user changes the lighting and shading parameters or the viewing direction.

🌟 the shadow buffer 🌟

the shadow buffer is an internal 2D array used to maintain state during rendering. there are several state variables that can be used to query its current size in pixels (VP_SHADOW_WIDTH and VP_SHADOW_HEIGHT) and to suggest a size (VP_SHADOW_WIDTH_HINT and VP_SHADOW_HEIGHT_HINT). the required size depends on the volume size and the shadow light's direction. normally the buffer is automatically resized when necessary β€” so like, it handles itself. 🫢


⭐ STATE VARIABLES ⭐

info about the current shading table parameters can be retrieved with the following state variable codes (see vpGeti(3)):

VP_COLOR_CHANNELS, VP_SHADE_COLOR_TABLE, VP_SHADE_COLOR_SIZE, VP_SHADE_WEIGHT_TABLE, VP_SHADE_WEIGHT_SIZE, VP_SHADE_COLOR_FIELD, VP_SHADE_WEIGHT_FIELD, VP_MATERIAL_COUNT, VP_SHADOW, VP_SHADOW_LIGHT, VP_SHADOW_WIDTH_HINT, VP_SHADOW_HEIGHT_HINT, VP_SHADOW_WIDTH, VP_SHADOW_HEIGHT, VP_SHADOW_COLOR_TABLE, VP_SHADOW_COLOR_SIZE, VP_SHADOW_BIAS


πŸ”₯ ERRORS πŸ”₯

the normal return value is VP_OK. the following error return values are possible:

  • VPERROR_BAD_VALUE β€” one or more of the arguments has an invalid value or is out of range. check your inputs bestie.
  • VPERROR_LIMIT_EXCEEDED β€” the num_materials argument has exceeded an internal limit. change the value of VP_MAX_MATERIAL in volpack.h and recompile the VolPack library.

πŸŽ€ SEE ALSO πŸŽ€

VolPack(3), vpCreateContext(3), vpShadeTable(3), vpSetLookupShader(3)


and that's the whole page, bestie! πŸ’– this was brought to you by VolPack, and this iconic function lives in section vpSetShadowLookupShader(3). go render something gorgeous with correct shadows. mwah. πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from