Blender  V3.3
Classes | Typedefs | Functions
bake.c File Reference

The API itself is simple. Blender sends a populated array of BakePixels to the renderer, and gets back an array of floats with the result. More...

#include <limits.h>
#include "MEM_guardedalloc.h"
#include "BLI_math.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "BKE_bvhutils.h"
#include "BKE_customdata.h"
#include "BKE_image.h"
#include "BKE_lib_id.h"
#include "BKE_mesh.h"
#include "BKE_mesh_runtime.h"
#include "BKE_mesh_tangent.h"
#include "BKE_node.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "RE_bake.h"
#include "RE_texture_margin.h"
#include "render_types.h"
#include "zbuf.h"

Go to the source code of this file.

Classes

struct  BakeDataZSpan
 
struct  TSpace
 
struct  TriTessFace
 

Typedefs

typedef struct BakeDataZSpan BakeDataZSpan
 
typedef struct TSpace TSpace
 
typedef struct TriTessFace TriTessFace
 

Functions

static void store_bake_pixel (void *handle, int x, int y, float u, float v)
 
void RE_bake_mask_fill (const BakePixel pixel_array[], const size_t pixels_num, char *mask)
 
void RE_bake_margin (ImBuf *ibuf, char *mask, const int margin, const char margin_type, Mesh const *me, char const *uv_layer, const float uv_offset[2])
 
static void calc_point_from_barycentric_cage (TriTessFace *triangles_low, TriTessFace *triangles_cage, const float mat_low[4][4], const float mat_cage[4][4], int primitive_id, float u, float v, float r_co[3], float r_dir[3])
 
static void calc_point_from_barycentric_extrusion (TriTessFace *triangles, const float mat[4][4], const float imat[4][4], int primitive_id, float u, float v, float cage_extrusion, float r_co[3], float r_dir[3], const bool is_cage)
 
static void barycentric_differentials_from_position (const float co[3], const float v1[3], const float v2[3], const float v3[3], const float dxco[3], const float dyco[3], const float facenor[3], const bool differentials, float *u, float *v, float *dx_u, float *dx_v, float *dy_u, float *dy_v)
 
static bool cast_ray_highpoly (BVHTreeFromMesh *treeData, TriTessFace *triangle_low, TriTessFace *triangles[], BakePixel *pixel_array_low, BakePixel *pixel_array, const float mat_low[4][4], BakeHighPolyData *highpoly, const float co[3], const float dir[3], const int pixel_id, const int tot_highpoly, const float max_ray_distance)
 
static TriTessFacemesh_calc_tri_tessface (Mesh *me, bool tangent, Mesh *me_eval)
 
bool RE_bake_pixels_populate_from_objects (struct Mesh *me_low, BakePixel pixel_array_from[], BakePixel pixel_array_to[], BakeHighPolyData highpoly[], const int tot_highpoly, const size_t pixels_num, const bool is_custom_cage, const float cage_extrusion, const float max_ray_distance, float mat_low[4][4], float mat_cage[4][4], struct Mesh *me_cage)
 
static void bake_differentials (BakeDataZSpan *bd, const float *uv1, const float *uv2, const float *uv3)
 
void RE_bake_pixels_populate (Mesh *me, BakePixel pixel_array[], const size_t pixels_num, const BakeTargets *targets, const char *uv_layer)
 
static void normal_compress (float out[3], const float in[3], const eBakeNormalSwizzle normal_swizzle[3])
 
void RE_bake_normal_world_to_tangent (const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], Mesh *me, const eBakeNormalSwizzle normal_swizzle[3], float mat[4][4])
 
void RE_bake_normal_world_to_object (const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], struct Object *ob, const eBakeNormalSwizzle normal_swizzle[3])
 
void RE_bake_normal_world_to_world (const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], const eBakeNormalSwizzle normal_swizzle[3])
 
void RE_bake_ibuf_clear (Image *image, const bool is_tangent)
 
int RE_pass_depth (const eScenePassType pass_type)
 

Detailed Description

The API itself is simple. Blender sends a populated array of BakePixels to the renderer, and gets back an array of floats with the result.

Development Notes for External Engines

The Bake API is fully implemented with Python rna functions. The operator expects/call a function:

def bake(scene, object, pass_type, object_id, pixel_array, pixels_num, depth, result)

Note
Normals are expected to be in World Space and in the +X, +Y, +Z orientation.

BakePixel data structure

pixel_array is a Python object storing BakePixel elements:

struct BakePixel {
float uv[2];
float du_dx, du_dy;
float dv_dx, dv_dy;
};
float dv_dx
Definition: RE_bake.h:57
float du_dx
Definition: RE_bake.h:56
float du_dy
Definition: RE_bake.h:56
float uv[2]
Definition: RE_bake.h:55
float dv_dy
Definition: RE_bake.h:57
int object_id
Definition: RE_bake.h:53
int primitive_id
Definition: RE_bake.h:53

In python you have access to:

Note
Pixels that should not be baked have primitive_id == -1.

For a complete implementation example look at the Cycles Bake commit.

Definition in file bake.c.

Typedef Documentation

◆ BakeDataZSpan

typedef struct BakeDataZSpan BakeDataZSpan

◆ TriTessFace

typedef struct TriTessFace TriTessFace

◆ TSpace

typedef struct TSpace TSpace

struct wrapping up tangent space data

Function Documentation

◆ bake_differentials()

static void bake_differentials ( BakeDataZSpan bd,
const float uv1,
const float uv2,
const float uv3 
)
static

◆ barycentric_differentials_from_position()

static void barycentric_differentials_from_position ( const float  co[3],
const float  v1[3],
const float  v2[3],
const float  v3[3],
const float  dxco[3],
const float  dyco[3],
const float  facenor[3],
const bool  differentials,
float u,
float v,
float dx_u,
float dx_v,
float dy_u,
float dy_v 
)
static

Definition at line 273 of file bake.c.

References axis_dominant_v3(), v, v1, and v2.

Referenced by cast_ray_highpoly().

◆ calc_point_from_barycentric_cage()

static void calc_point_from_barycentric_cage ( TriTessFace triangles_low,
TriTessFace triangles_cage,
const float  mat_low[4][4],
const float  mat_cage[4][4],
int  primitive_id,
float  u,
float  v,
float  r_co[3],
float  r_dir[3] 
)
static

This function returns the coordinate and normal of a barycentric u,v for a face defined by the primitive_id index. The returned normal is actually the direction from the same barycentric coordinate in the cage to the base mesh The returned coordinate is the point in the cage mesh

Definition at line 177 of file bake.c.

References copy_v3_v3(), data, interp_barycentric_tri_v3(), mul_m4_v3(), normalize_v3(), sub_v3_v3v3(), and v.

Referenced by RE_bake_pixels_populate_from_objects().

◆ calc_point_from_barycentric_extrusion()

static void calc_point_from_barycentric_extrusion ( TriTessFace triangles,
const float  mat[4][4],
const float  imat[4][4],
int  primitive_id,
float  u,
float  v,
float  cage_extrusion,
float  r_co[3],
float  r_dir[3],
const bool  is_cage 
)
static

This function returns the coordinate and normal of a barycentric u,v for a face defined by the primitive_id index. The returned coordinate is extruded along the normal by cage_extrusion

Definition at line 220 of file bake.c.

References add_v3_v3(), MVert::co, copy_v3_v3(), data, interp_barycentric_tri_v3(), TriTessFace::is_smooth, mul_m4_v3(), mul_transposed_mat3_m4_v3(), mul_v3_v3fl(), TriTessFace::mverts, negate_v3(), TriTessFace::normal, normalize_v3(), v, and TriTessFace::vert_normals.

Referenced by RE_bake_pixels_populate_from_objects().

◆ cast_ray_highpoly()

static bool cast_ray_highpoly ( BVHTreeFromMesh treeData,
TriTessFace triangle_low,
TriTessFace triangles[],
BakePixel pixel_array_low,
BakePixel pixel_array,
const float  mat_low[4][4],
BakeHighPolyData highpoly,
const float  co[3],
const float  dir[3],
const int  pixel_id,
const int  tot_highpoly,
const float  max_ray_distance 
)
static

◆ mesh_calc_tri_tessface()

static TriTessFace* mesh_calc_tri_tessface ( Mesh me,
bool  tangent,
Mesh me_eval 
)
static

◆ normal_compress()

static void normal_compress ( float  out[3],
const float  in[3],
const eBakeNormalSwizzle  normal_swizzle[3] 
)
static

◆ RE_bake_ibuf_clear()

void RE_bake_ibuf_clear ( Image image,
const bool  is_tangent 
)

◆ RE_bake_margin()

void RE_bake_margin ( ImBuf ibuf,
char *  mask,
const int  margin,
const char  margin_type,
Mesh const *  me,
char const *  uv_layer,
const float  uv_offset[2] 
)

◆ RE_bake_mask_fill()

void RE_bake_mask_fill ( const BakePixel  pixel_array[],
const size_t  pixels_num,
char *  mask 
)

Definition at line 129 of file bake.c.

References FILTER_MASK_USED, and mask().

Referenced by write_external_bake_pixels(), and write_internal_bake_pixels().

◆ RE_bake_normal_world_to_object()

void RE_bake_normal_world_to_object ( const BakePixel  pixel_array[],
const size_t  pixels_num,
const int  depth,
float  result[],
struct Object ob,
const eBakeNormalSwizzle  normal_swizzle[3] 
)

Definition at line 956 of file bake.c.

References copy_v3_v3(), invert_m4_m4(), mul_mat3_m4_v3(), nor, normal_compress(), normalize_v3(), Object::obmat, offset, and result.

Referenced by bake().

◆ RE_bake_normal_world_to_tangent()

void RE_bake_normal_world_to_tangent ( const BakePixel  pixel_array[],
size_t  pixels_num,
int  depth,
float  result[],
struct Mesh me,
const eBakeNormalSwizzle  normal_swizzle[3],
float  mat[4][4] 
)

◆ RE_bake_normal_world_to_world()

void RE_bake_normal_world_to_world ( const BakePixel  pixel_array[],
const size_t  pixels_num,
const int  depth,
float  result[],
const eBakeNormalSwizzle  normal_swizzle[3] 
)

Definition at line 988 of file bake.c.

References copy_v3_v3(), nor, normal_compress(), offset, and result.

Referenced by bake().

◆ RE_bake_pixels_populate()

void RE_bake_pixels_populate ( Mesh me,
BakePixel  pixel_array[],
const size_t  pixels_num,
const BakeTargets targets,
const char *  uv_layer 
)

◆ RE_bake_pixels_populate_from_objects()

bool RE_bake_pixels_populate_from_objects ( struct Mesh me_low,
BakePixel  pixel_array_from[],
BakePixel  pixel_array_to[],
BakeHighPolyData  highpoly[],
const int  tot_highpoly,
const size_t  pixels_num,
const bool  is_custom_cage,
const float  cage_extrusion,
const float  max_ray_distance,
float  mat_low[4][4],
float  mat_cage[4][4],
struct Mesh me_cage 
)

◆ RE_pass_depth()

int RE_pass_depth ( const eScenePassType  pass_type)

◆ store_bake_pixel()

static void store_bake_pixel ( void handle,
int  x,
int  y,
float  u,
float  v 
)
static