2012-12-18 17:46:26 +00:00
|
|
|
/*
|
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) 2012 by Blender Foundation
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): Morten Mikkelsen,
|
|
|
|
|
* Sergey Sharybin
|
|
|
|
|
*
|
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** \file blender/render/intern/source/multires_bake.c
|
|
|
|
|
* \ingroup render
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_math.h"
|
|
|
|
|
#include "BLI_listbase.h"
|
2012-12-19 08:13:41 +00:00
|
|
|
#include "BLI_threads.h"
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
#include "BKE_ccg.h"
|
2014-09-19 15:36:49 +02:00
|
|
|
#include "BKE_depsgraph.h"
|
2012-12-18 17:46:26 +00:00
|
|
|
#include "BKE_global.h"
|
|
|
|
|
#include "BKE_image.h"
|
2015-07-30 14:43:58 +02:00
|
|
|
#include "BKE_mesh.h"
|
2012-12-18 17:46:26 +00:00
|
|
|
#include "BKE_multires.h"
|
|
|
|
|
#include "BKE_modifier.h"
|
|
|
|
|
#include "BKE_subsurf.h"
|
|
|
|
|
|
|
|
|
|
#include "RE_multires_bake.h"
|
|
|
|
|
#include "RE_pipeline.h"
|
|
|
|
|
#include "RE_shader_ext.h"
|
|
|
|
|
|
|
|
|
|
#include "IMB_imbuf_types.h"
|
|
|
|
|
#include "IMB_imbuf.h"
|
|
|
|
|
|
2012-12-18 17:46:42 +00:00
|
|
|
#include "rayintersection.h"
|
|
|
|
|
#include "rayobject.h"
|
|
|
|
|
#include "rendercore.h"
|
|
|
|
|
|
2013-01-21 18:34:27 +00:00
|
|
|
typedef void (*MPassKnownData)(DerivedMesh *lores_dm, DerivedMesh *hires_dm, void *thread_data,
|
|
|
|
|
void *bake_data, ImBuf *ibuf, const int face_index, const int lvl,
|
|
|
|
|
const float st[2], float tangmat[3][3], const int x, const int y);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
typedef void * (*MInitBakeData)(MultiresBakeRender *bkr, Image *ima);
|
|
|
|
|
typedef void (*MFreeBakeData)(void *bake_data);
|
|
|
|
|
|
2013-01-21 18:34:27 +00:00
|
|
|
typedef struct MultiresBakeResult {
|
|
|
|
|
float height_min, height_max;
|
|
|
|
|
} MultiresBakeResult;
|
|
|
|
|
|
2012-12-18 17:46:26 +00:00
|
|
|
typedef struct {
|
|
|
|
|
MVert *mvert;
|
2015-07-30 14:43:58 +02:00
|
|
|
MPoly *mpoly;
|
|
|
|
|
MLoop *mloop;
|
|
|
|
|
MLoopUV *mloopuv;
|
|
|
|
|
const MLoopTri *mlooptri;
|
|
|
|
|
MTexPoly *mtpoly;
|
2012-12-18 17:46:26 +00:00
|
|
|
float *pvtangent;
|
2014-04-27 00:22:07 +10:00
|
|
|
const float *precomputed_normals;
|
2012-12-18 17:46:26 +00:00
|
|
|
int w, h;
|
2015-07-30 14:43:58 +02:00
|
|
|
int tri_index;
|
2012-12-18 17:46:26 +00:00
|
|
|
DerivedMesh *lores_dm, *hires_dm;
|
|
|
|
|
int lvl;
|
2013-01-21 18:34:27 +00:00
|
|
|
void *thread_data;
|
2012-12-18 17:46:26 +00:00
|
|
|
void *bake_data;
|
|
|
|
|
ImBuf *ibuf;
|
|
|
|
|
MPassKnownData pass_data;
|
|
|
|
|
} MResolvePixelData;
|
|
|
|
|
|
|
|
|
|
typedef void (*MFlushPixel)(const MResolvePixelData *data, const int x, const int y);
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
int w, h;
|
|
|
|
|
char *texels;
|
|
|
|
|
const MResolvePixelData *data;
|
|
|
|
|
MFlushPixel flush_pixel;
|
2013-10-17 18:29:01 +00:00
|
|
|
short *do_update;
|
2012-12-18 17:46:26 +00:00
|
|
|
} MBakeRast;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
float *heights;
|
|
|
|
|
Image *ima;
|
|
|
|
|
DerivedMesh *ssdm;
|
|
|
|
|
const int *orig_index_mp_to_orig;
|
|
|
|
|
} MHeightBakeData;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
const int *orig_index_mp_to_orig;
|
|
|
|
|
} MNormalBakeData;
|
|
|
|
|
|
2012-12-18 17:46:42 +00:00
|
|
|
typedef struct {
|
|
|
|
|
int number_of_rays;
|
|
|
|
|
float bias;
|
|
|
|
|
|
|
|
|
|
unsigned short *permutation_table_1;
|
|
|
|
|
unsigned short *permutation_table_2;
|
|
|
|
|
|
|
|
|
|
RayObject *raytree;
|
|
|
|
|
RayFace *rayfaces;
|
|
|
|
|
|
|
|
|
|
const int *orig_index_mp_to_orig;
|
|
|
|
|
} MAOBakeData;
|
|
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
static void multiresbake_get_normal(const MResolvePixelData *data, float norm[],const int tri_num, const int vert_index)
|
2012-12-18 17:46:26 +00:00
|
|
|
{
|
2015-07-30 14:43:58 +02:00
|
|
|
const int poly_index = data->mlooptri[tri_num].poly;
|
|
|
|
|
const MPoly *mp = &data->mpoly[poly_index];
|
|
|
|
|
const bool smoothnormal = (mp->flag & ME_SMOOTH) != 0;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
if (!smoothnormal) { /* flat */
|
|
|
|
|
if (data->precomputed_normals) {
|
2015-07-30 14:43:58 +02:00
|
|
|
copy_v3_v3(norm, &data->precomputed_normals[poly_index]);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2015-07-30 14:43:58 +02:00
|
|
|
BKE_mesh_calc_poly_normal(mp, &data->mloop[mp->loopstart], data->mvert, norm);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2015-07-30 14:43:58 +02:00
|
|
|
const int vi = data->mloop[data->mlooptri[tri_num].tri[vert_index]].v;
|
|
|
|
|
const short *no = data->mvert[vi].no;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
normal_short_to_float_v3(norm, no);
|
|
|
|
|
normalize_v3(norm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-17 18:29:01 +00:00
|
|
|
static void init_bake_rast(MBakeRast *bake_rast, const ImBuf *ibuf, const MResolvePixelData *data,
|
|
|
|
|
MFlushPixel flush_pixel, short *do_update)
|
2012-12-18 17:46:26 +00:00
|
|
|
{
|
2013-01-21 18:34:27 +00:00
|
|
|
BakeImBufuserData *userdata = (BakeImBufuserData *) ibuf->userdata;
|
|
|
|
|
|
2012-12-18 17:46:26 +00:00
|
|
|
memset(bake_rast, 0, sizeof(MBakeRast));
|
|
|
|
|
|
2013-01-21 18:34:27 +00:00
|
|
|
bake_rast->texels = userdata->mask_buffer;
|
2012-12-18 17:46:26 +00:00
|
|
|
bake_rast->w = ibuf->x;
|
|
|
|
|
bake_rast->h = ibuf->y;
|
|
|
|
|
bake_rast->data = data;
|
|
|
|
|
bake_rast->flush_pixel = flush_pixel;
|
2013-10-17 18:29:01 +00:00
|
|
|
bake_rast->do_update = do_update;
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void flush_pixel(const MResolvePixelData *data, const int x, const int y)
|
|
|
|
|
{
|
|
|
|
|
float st[2] = {(x + 0.5f) / data->w, (y + 0.5f) / data->h};
|
2014-04-27 00:22:07 +10:00
|
|
|
const float *st0, *st1, *st2;
|
|
|
|
|
const float *tang0, *tang1, *tang2;
|
2012-12-18 17:46:26 +00:00
|
|
|
float no0[3], no1[3], no2[3];
|
|
|
|
|
float fUV[2], from_tang[3][3], to_tang[3][3];
|
|
|
|
|
float u, v, w, sign;
|
|
|
|
|
int r;
|
|
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
st0 = data->mloopuv[data->mlooptri[data->tri_index].tri[0]].uv;
|
|
|
|
|
st1 = data->mloopuv[data->mlooptri[data->tri_index].tri[1]].uv;
|
|
|
|
|
st2 = data->mloopuv[data->mlooptri[data->tri_index].tri[2]].uv;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
multiresbake_get_normal(data, no0, data->tri_index, 0); /* can optimize these 3 into one call */
|
|
|
|
|
multiresbake_get_normal(data, no1, data->tri_index, 1);
|
|
|
|
|
multiresbake_get_normal(data, no2, data->tri_index, 2);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2014-03-26 11:15:21 +11:00
|
|
|
resolve_tri_uv_v2(fUV, st, st0, st1, st2);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
u = fUV[0];
|
|
|
|
|
v = fUV[1];
|
|
|
|
|
w = 1 - u - v;
|
|
|
|
|
|
2012-12-18 17:46:42 +00:00
|
|
|
if (data->pvtangent) {
|
2015-07-30 14:43:58 +02:00
|
|
|
tang0 = data->pvtangent + data->mlooptri[data->tri_index].tri[0] * 4;
|
|
|
|
|
tang1 = data->pvtangent + data->mlooptri[data->tri_index].tri[1] * 4;
|
|
|
|
|
tang2 = data->pvtangent + data->mlooptri[data->tri_index].tri[2] * 4;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2012-12-18 17:46:42 +00:00
|
|
|
/* the sign is the same at all face vertices for any non degenerate face.
|
|
|
|
|
* Just in case we clamp the interpolated value though. */
|
|
|
|
|
sign = (tang0[3] * u + tang1[3] * v + tang2[3] * w) < 0 ? (-1.0f) : 1.0f;
|
|
|
|
|
|
|
|
|
|
/* this sequence of math is designed specifically as is with great care
|
|
|
|
|
* to be compatible with our shader. Please don't change without good reason. */
|
|
|
|
|
for (r = 0; r < 3; r++) {
|
|
|
|
|
from_tang[0][r] = tang0[r] * u + tang1[r] * v + tang2[r] * w;
|
|
|
|
|
from_tang[2][r] = no0[r] * u + no1[r] * v + no2[r] * w;
|
|
|
|
|
}
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2012-12-18 17:46:42 +00:00
|
|
|
cross_v3_v3v3(from_tang[1], from_tang[2], from_tang[0]); /* B = sign * cross(N, T) */
|
|
|
|
|
mul_v3_fl(from_tang[1], sign);
|
|
|
|
|
invert_m3_m3(to_tang, from_tang);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
zero_m3(to_tang);
|
|
|
|
|
}
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2013-01-21 18:34:27 +00:00
|
|
|
data->pass_data(data->lores_dm, data->hires_dm, data->thread_data, data->bake_data,
|
2015-07-30 14:43:58 +02:00
|
|
|
data->ibuf, data->tri_index, data->lvl, st, to_tang, x, y);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void set_rast_triangle(const MBakeRast *bake_rast, const int x, const int y)
|
|
|
|
|
{
|
|
|
|
|
const int w = bake_rast->w;
|
|
|
|
|
const int h = bake_rast->h;
|
|
|
|
|
|
|
|
|
|
if (x >= 0 && x < w && y >= 0 && y < h) {
|
|
|
|
|
if ((bake_rast->texels[y * w + x]) == 0) {
|
|
|
|
|
bake_rast->texels[y * w + x] = FILTER_MASK_USED;
|
2012-12-23 18:58:10 +00:00
|
|
|
flush_pixel(bake_rast->data, x, y);
|
2013-10-17 18:29:01 +00:00
|
|
|
if (bake_rast->do_update) {
|
|
|
|
|
*bake_rast->do_update = true;
|
|
|
|
|
}
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rasterize_half(const MBakeRast *bake_rast,
|
|
|
|
|
const float s0_s, const float t0_s, const float s1_s, const float t1_s,
|
|
|
|
|
const float s0_l, const float t0_l, const float s1_l, const float t1_l,
|
|
|
|
|
const int y0_in, const int y1_in, const int is_mid_right)
|
|
|
|
|
{
|
|
|
|
|
const int s_stable = fabsf(t1_s - t0_s) > FLT_EPSILON ? 1 : 0;
|
|
|
|
|
const int l_stable = fabsf(t1_l - t0_l) > FLT_EPSILON ? 1 : 0;
|
|
|
|
|
const int w = bake_rast->w;
|
|
|
|
|
const int h = bake_rast->h;
|
|
|
|
|
int y, y0, y1;
|
|
|
|
|
|
|
|
|
|
if (y1_in <= 0 || y0_in >= h)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
y0 = y0_in < 0 ? 0 : y0_in;
|
|
|
|
|
y1 = y1_in >= h ? h : y1_in;
|
|
|
|
|
|
|
|
|
|
for (y = y0; y < y1; y++) {
|
|
|
|
|
/*-b(x-x0) + a(y-y0) = 0 */
|
|
|
|
|
int iXl, iXr, x;
|
|
|
|
|
float x_l = s_stable != 0 ? (s0_s + (((s1_s - s0_s) * (y - t0_s)) / (t1_s - t0_s))) : s0_s;
|
|
|
|
|
float x_r = l_stable != 0 ? (s0_l + (((s1_l - s0_l) * (y - t0_l)) / (t1_l - t0_l))) : s0_l;
|
|
|
|
|
|
|
|
|
|
if (is_mid_right != 0)
|
|
|
|
|
SWAP(float, x_l, x_r);
|
|
|
|
|
|
|
|
|
|
iXl = (int)ceilf(x_l);
|
|
|
|
|
iXr = (int)ceilf(x_r);
|
|
|
|
|
|
|
|
|
|
if (iXr > 0 && iXl < w) {
|
|
|
|
|
iXl = iXl < 0 ? 0 : iXl;
|
|
|
|
|
iXr = iXr >= w ? w : iXr;
|
|
|
|
|
|
|
|
|
|
for (x = iXl; x < iXr; x++)
|
|
|
|
|
set_rast_triangle(bake_rast, x, y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bake_rasterize(const MBakeRast *bake_rast, const float st0_in[2], const float st1_in[2], const float st2_in[2])
|
|
|
|
|
{
|
|
|
|
|
const int w = bake_rast->w;
|
|
|
|
|
const int h = bake_rast->h;
|
|
|
|
|
float slo = st0_in[0] * w - 0.5f;
|
|
|
|
|
float tlo = st0_in[1] * h - 0.5f;
|
|
|
|
|
float smi = st1_in[0] * w - 0.5f;
|
|
|
|
|
float tmi = st1_in[1] * h - 0.5f;
|
|
|
|
|
float shi = st2_in[0] * w - 0.5f;
|
|
|
|
|
float thi = st2_in[1] * h - 0.5f;
|
|
|
|
|
int is_mid_right = 0, ylo, yhi, yhi_beg;
|
|
|
|
|
|
|
|
|
|
/* skip degenerates */
|
|
|
|
|
if ((slo == smi && tlo == tmi) || (slo == shi && tlo == thi) || (smi == shi && tmi == thi))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* sort by T */
|
|
|
|
|
if (tlo > tmi && tlo > thi) {
|
|
|
|
|
SWAP(float, shi, slo);
|
|
|
|
|
SWAP(float, thi, tlo);
|
|
|
|
|
}
|
|
|
|
|
else if (tmi > thi) {
|
|
|
|
|
SWAP(float, shi, smi);
|
|
|
|
|
SWAP(float, thi, tmi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tlo > tmi) {
|
|
|
|
|
SWAP(float, slo, smi);
|
|
|
|
|
SWAP(float, tlo, tmi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check if mid point is to the left or to the right of the lo-hi edge */
|
|
|
|
|
is_mid_right = (-(shi - slo) * (tmi - thi) + (thi - tlo) * (smi - shi)) > 0 ? 1 : 0;
|
|
|
|
|
ylo = (int) ceilf(tlo);
|
|
|
|
|
yhi_beg = (int) ceilf(tmi);
|
|
|
|
|
yhi = (int) ceilf(thi);
|
|
|
|
|
|
|
|
|
|
/*if (fTmi>ceilf(fTlo))*/
|
|
|
|
|
rasterize_half(bake_rast, slo, tlo, smi, tmi, slo, tlo, shi, thi, ylo, yhi_beg, is_mid_right);
|
|
|
|
|
rasterize_half(bake_rast, smi, tmi, shi, thi, slo, tlo, shi, thi, yhi_beg, yhi, is_mid_right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int multiresbake_test_break(MultiresBakeRender *bkr)
|
|
|
|
|
{
|
|
|
|
|
if (!bkr->stop) {
|
|
|
|
|
/* this means baker is executed outside from job system */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
return *bkr->stop || G.is_break;
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
/* **** Threading routines **** */
|
|
|
|
|
|
|
|
|
|
typedef struct MultiresBakeQueue {
|
2015-07-30 14:43:58 +02:00
|
|
|
int cur_tri;
|
|
|
|
|
int tot_tri;
|
2012-12-19 08:13:41 +00:00
|
|
|
SpinLock spin;
|
|
|
|
|
} MultiresBakeQueue;
|
|
|
|
|
|
|
|
|
|
typedef struct MultiresBakeThread {
|
|
|
|
|
/* this data is actually shared between all the threads */
|
|
|
|
|
MultiresBakeQueue *queue;
|
|
|
|
|
MultiresBakeRender *bkr;
|
|
|
|
|
Image *image;
|
|
|
|
|
void *bake_data;
|
|
|
|
|
|
|
|
|
|
/* thread-specific data */
|
|
|
|
|
MBakeRast bake_rast;
|
|
|
|
|
MResolvePixelData data;
|
2013-01-21 18:34:27 +00:00
|
|
|
|
|
|
|
|
/* displacement-specific data */
|
|
|
|
|
float height_min, height_max;
|
2012-12-19 08:13:41 +00:00
|
|
|
} MultiresBakeThread;
|
|
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
static int multires_bake_queue_next_tri(MultiresBakeQueue *queue)
|
2012-12-18 17:46:26 +00:00
|
|
|
{
|
2012-12-19 08:13:41 +00:00
|
|
|
int face = -1;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
/* TODO: it could worth making it so thread will handle neighbor faces
|
|
|
|
|
* for better memory cache utilization
|
|
|
|
|
*/
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
BLI_spin_lock(&queue->spin);
|
2015-07-30 14:43:58 +02:00
|
|
|
if (queue->cur_tri < queue->tot_tri) {
|
|
|
|
|
face = queue->cur_tri;
|
|
|
|
|
queue->cur_tri++;
|
2012-12-18 17:46:42 +00:00
|
|
|
}
|
2012-12-19 08:13:41 +00:00
|
|
|
BLI_spin_unlock(&queue->spin);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
return face;
|
|
|
|
|
}
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
static void *do_multires_bake_thread(void *data_v)
|
|
|
|
|
{
|
|
|
|
|
MultiresBakeThread *handle = (MultiresBakeThread *) data_v;
|
|
|
|
|
MResolvePixelData *data = &handle->data;
|
|
|
|
|
MBakeRast *bake_rast = &handle->bake_rast;
|
|
|
|
|
MultiresBakeRender *bkr = handle->bkr;
|
2015-07-30 14:43:58 +02:00
|
|
|
int tri_index;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
while ((tri_index = multires_bake_queue_next_tri(handle->queue)) >= 0) {
|
|
|
|
|
const MLoopTri *lt = &data->mlooptri[tri_index];
|
|
|
|
|
MTexPoly *mtpoly = &data->mtpoly[lt->poly];
|
|
|
|
|
MLoopUV *mloopuv = data->mloopuv;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
if (multiresbake_test_break(bkr))
|
|
|
|
|
break;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
if (mtpoly->tpage != handle->image)
|
2012-12-19 08:13:41 +00:00
|
|
|
continue;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
data->tri_index = tri_index;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
bake_rasterize(bake_rast, mloopuv[lt->tri[0]].uv, mloopuv[lt->tri[1]].uv, mloopuv[lt->tri[2]].uv);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
/* tag image buffer for refresh */
|
|
|
|
|
if (data->ibuf->rect_float)
|
|
|
|
|
data->ibuf->userflags |= IB_RECT_INVALID;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
data->ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
|
2012-12-18 17:46:48 +00:00
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
/* update progress */
|
|
|
|
|
BLI_spin_lock(&handle->queue->spin);
|
|
|
|
|
bkr->baked_faces++;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
if (bkr->do_update)
|
2014-04-01 11:34:00 +11:00
|
|
|
*bkr->do_update = true;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
if (bkr->progress)
|
2015-07-30 14:43:58 +02:00
|
|
|
*bkr->progress = ((float)bkr->baked_objects + (float)bkr->baked_faces / handle->queue->tot_tri) / bkr->tot_obj;
|
2012-12-19 08:13:41 +00:00
|
|
|
BLI_spin_unlock(&handle->queue->spin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2013-01-21 18:34:14 +00:00
|
|
|
/* some of arrays inside ccgdm are lazy-initialized, which will generally
|
|
|
|
|
* require lock around accessing such data
|
|
|
|
|
* this function will ensure all arrays are allocated before threading started
|
|
|
|
|
*/
|
|
|
|
|
static void init_ccgdm_arrays(DerivedMesh *dm)
|
|
|
|
|
{
|
|
|
|
|
CCGElem **grid_data;
|
|
|
|
|
CCGKey key;
|
|
|
|
|
int grid_size;
|
2014-04-27 00:22:07 +10:00
|
|
|
const int *grid_offset;
|
2013-01-21 18:34:14 +00:00
|
|
|
|
|
|
|
|
grid_size = dm->getGridSize(dm);
|
|
|
|
|
grid_data = dm->getGridData(dm);
|
|
|
|
|
grid_offset = dm->getGridOffset(dm);
|
|
|
|
|
dm->getGridKey(dm, &key);
|
|
|
|
|
|
2013-01-24 08:49:40 +00:00
|
|
|
(void) grid_size;
|
2013-01-21 18:34:14 +00:00
|
|
|
(void) grid_data;
|
|
|
|
|
(void) grid_offset;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 11:34:00 +11:00
|
|
|
static void do_multires_bake(MultiresBakeRender *bkr, Image *ima, bool require_tangent, MPassKnownData passKnownData,
|
2013-01-21 18:34:27 +00:00
|
|
|
MInitBakeData initBakeData, MFreeBakeData freeBakeData, MultiresBakeResult *result)
|
2012-12-19 08:13:41 +00:00
|
|
|
{
|
|
|
|
|
DerivedMesh *dm = bkr->lores_dm;
|
2015-07-30 14:43:58 +02:00
|
|
|
const MLoopTri *mlooptri = dm->getLoopTriArray(dm);
|
2012-12-19 08:13:41 +00:00
|
|
|
const int lvl = bkr->lvl;
|
2015-07-30 14:43:58 +02:00
|
|
|
int tot_tri = dm->getNumLoopTri(dm);
|
2012-12-19 08:13:41 +00:00
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
if (tot_tri > 0) {
|
2012-12-19 08:13:41 +00:00
|
|
|
MultiresBakeThread *handles;
|
|
|
|
|
MultiresBakeQueue queue;
|
|
|
|
|
|
|
|
|
|
ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
|
|
|
|
|
MVert *mvert = dm->getVertArray(dm);
|
2015-07-30 14:43:58 +02:00
|
|
|
MPoly *mpoly = dm->getPolyArray(dm);
|
|
|
|
|
MLoop *mloop = dm->getLoopArray(dm);
|
|
|
|
|
MLoopUV *mloopuv = dm->getLoopDataArray(dm, CD_MLOOPUV);
|
|
|
|
|
MTexPoly *mtpoly = dm->getPolyDataArray(dm, CD_MTEXPOLY);
|
|
|
|
|
const float *precomputed_normals = dm->getPolyDataArray(dm, CD_NORMAL);
|
2012-12-19 08:13:41 +00:00
|
|
|
float *pvtangent = NULL;
|
|
|
|
|
|
|
|
|
|
ListBase threads;
|
|
|
|
|
int i, tot_thread = bkr->threads > 0 ? bkr->threads : BLI_system_thread_count();
|
|
|
|
|
|
|
|
|
|
void *bake_data = NULL;
|
|
|
|
|
|
|
|
|
|
if (require_tangent) {
|
2015-07-30 14:43:58 +02:00
|
|
|
if (CustomData_get_layer_index(&dm->loopData, CD_TANGENT) == -1)
|
2016-04-26 18:43:02 +10:00
|
|
|
DM_calc_loop_tangents(dm, true, NULL, 0);
|
2012-12-19 08:13:41 +00:00
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
pvtangent = DM_get_loop_data_layer(dm, CD_TANGENT);
|
2012-12-19 08:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* all threads shares the same custom bake data */
|
|
|
|
|
if (initBakeData)
|
|
|
|
|
bake_data = initBakeData(bkr, ima);
|
|
|
|
|
|
|
|
|
|
if (tot_thread > 1)
|
2018-02-16 01:13:46 +11:00
|
|
|
BLI_threadpool_init(&threads, do_multires_bake_thread, tot_thread);
|
2012-12-19 08:13:41 +00:00
|
|
|
|
|
|
|
|
handles = MEM_callocN(tot_thread * sizeof(MultiresBakeThread), "do_multires_bake handles");
|
|
|
|
|
|
2013-01-21 18:34:14 +00:00
|
|
|
init_ccgdm_arrays(bkr->hires_dm);
|
|
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
/* faces queue */
|
2015-07-30 14:43:58 +02:00
|
|
|
queue.cur_tri = 0;
|
|
|
|
|
queue.tot_tri = tot_tri;
|
2012-12-19 08:13:41 +00:00
|
|
|
BLI_spin_init(&queue.spin);
|
|
|
|
|
|
|
|
|
|
/* fill in threads handles */
|
|
|
|
|
for (i = 0; i < tot_thread; i++) {
|
|
|
|
|
MultiresBakeThread *handle = &handles[i];
|
|
|
|
|
|
|
|
|
|
handle->bkr = bkr;
|
|
|
|
|
handle->image = ima;
|
|
|
|
|
handle->queue = &queue;
|
|
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
handle->data.mpoly = mpoly;
|
2012-12-19 08:13:41 +00:00
|
|
|
handle->data.mvert = mvert;
|
2015-07-30 14:43:58 +02:00
|
|
|
handle->data.mloopuv = mloopuv;
|
|
|
|
|
handle->data.mlooptri = mlooptri;
|
|
|
|
|
handle->data.mtpoly = mtpoly;
|
|
|
|
|
handle->data.mloop = mloop;
|
2012-12-19 08:13:41 +00:00
|
|
|
handle->data.pvtangent = pvtangent;
|
|
|
|
|
handle->data.precomputed_normals = precomputed_normals; /* don't strictly need this */
|
|
|
|
|
handle->data.w = ibuf->x;
|
|
|
|
|
handle->data.h = ibuf->y;
|
|
|
|
|
handle->data.lores_dm = dm;
|
|
|
|
|
handle->data.hires_dm = bkr->hires_dm;
|
|
|
|
|
handle->data.lvl = lvl;
|
|
|
|
|
handle->data.pass_data = passKnownData;
|
2013-01-21 18:34:27 +00:00
|
|
|
handle->data.thread_data = handle;
|
2012-12-19 08:13:41 +00:00
|
|
|
handle->data.bake_data = bake_data;
|
|
|
|
|
handle->data.ibuf = ibuf;
|
|
|
|
|
|
2013-01-21 18:34:27 +00:00
|
|
|
handle->height_min = FLT_MAX;
|
|
|
|
|
handle->height_max = -FLT_MAX;
|
|
|
|
|
|
2013-10-17 18:29:01 +00:00
|
|
|
init_bake_rast(&handle->bake_rast, ibuf, &handle->data, flush_pixel, bkr->do_update);
|
2012-12-19 08:13:41 +00:00
|
|
|
|
|
|
|
|
if (tot_thread > 1)
|
2018-02-16 01:13:46 +11:00
|
|
|
BLI_threadpool_insert(&threads, handle);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
/* run threads */
|
|
|
|
|
if (tot_thread > 1)
|
2018-02-16 01:13:46 +11:00
|
|
|
BLI_threadpool_end(&threads);
|
2012-12-19 08:13:41 +00:00
|
|
|
else
|
|
|
|
|
do_multires_bake_thread(&handles[0]);
|
|
|
|
|
|
2013-01-21 18:34:27 +00:00
|
|
|
/* construct bake result */
|
|
|
|
|
result->height_min = handles[0].height_min;
|
|
|
|
|
result->height_max = handles[0].height_max;
|
|
|
|
|
|
|
|
|
|
for (i = 1; i < tot_thread; i++) {
|
|
|
|
|
result->height_min = min_ff(result->height_min, handles[i].height_min);
|
|
|
|
|
result->height_max = max_ff(result->height_max, handles[i].height_max);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
BLI_spin_end(&queue.spin);
|
|
|
|
|
|
|
|
|
|
/* finalize baking */
|
2012-12-18 17:46:26 +00:00
|
|
|
if (freeBakeData)
|
2012-12-19 08:13:41 +00:00
|
|
|
freeBakeData(bake_data);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2013-01-21 18:34:14 +00:00
|
|
|
MEM_freeN(handles);
|
|
|
|
|
|
2012-12-19 08:13:41 +00:00
|
|
|
BKE_image_release_ibuf(ima, ibuf, NULL);
|
|
|
|
|
}
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* mode = 0: interpolate normals,
|
|
|
|
|
* mode = 1: interpolate coord */
|
|
|
|
|
static void interp_bilinear_grid(CCGKey *key, CCGElem *grid, float crn_x, float crn_y, int mode, float res[3])
|
|
|
|
|
{
|
|
|
|
|
int x0, x1, y0, y1;
|
|
|
|
|
float u, v;
|
|
|
|
|
float data[4][3];
|
|
|
|
|
|
|
|
|
|
x0 = (int) crn_x;
|
|
|
|
|
x1 = x0 >= (key->grid_size - 1) ? (key->grid_size - 1) : (x0 + 1);
|
|
|
|
|
|
|
|
|
|
y0 = (int) crn_y;
|
|
|
|
|
y1 = y0 >= (key->grid_size - 1) ? (key->grid_size - 1) : (y0 + 1);
|
|
|
|
|
|
|
|
|
|
u = crn_x - x0;
|
|
|
|
|
v = crn_y - y0;
|
|
|
|
|
|
|
|
|
|
if (mode == 0) {
|
|
|
|
|
copy_v3_v3(data[0], CCG_grid_elem_no(key, grid, x0, y0));
|
|
|
|
|
copy_v3_v3(data[1], CCG_grid_elem_no(key, grid, x1, y0));
|
|
|
|
|
copy_v3_v3(data[2], CCG_grid_elem_no(key, grid, x1, y1));
|
|
|
|
|
copy_v3_v3(data[3], CCG_grid_elem_no(key, grid, x0, y1));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
copy_v3_v3(data[0], CCG_grid_elem_co(key, grid, x0, y0));
|
|
|
|
|
copy_v3_v3(data[1], CCG_grid_elem_co(key, grid, x1, y0));
|
|
|
|
|
copy_v3_v3(data[2], CCG_grid_elem_co(key, grid, x1, y1));
|
|
|
|
|
copy_v3_v3(data[3], CCG_grid_elem_co(key, grid, x0, y1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interp_bilinear_quad_v3(data, u, v, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void get_ccgdm_data(DerivedMesh *lodm, DerivedMesh *hidm,
|
2015-07-30 14:43:58 +02:00
|
|
|
const int *index_mp_to_orig,
|
|
|
|
|
const int lvl, const MLoopTri *lt, const float u, const float v, float co[3], float n[3])
|
2012-12-18 17:46:26 +00:00
|
|
|
{
|
|
|
|
|
CCGElem **grid_data;
|
|
|
|
|
CCGKey key;
|
|
|
|
|
float crn_x, crn_y;
|
|
|
|
|
int grid_size, S, face_side;
|
|
|
|
|
int *grid_offset, g_index;
|
2015-07-30 14:43:58 +02:00
|
|
|
int poly_index = lt->poly;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
grid_size = hidm->getGridSize(hidm);
|
|
|
|
|
grid_data = hidm->getGridData(hidm);
|
|
|
|
|
grid_offset = hidm->getGridOffset(hidm);
|
|
|
|
|
hidm->getGridKey(hidm, &key);
|
|
|
|
|
|
2013-11-18 13:05:57 +06:00
|
|
|
if (lvl == 0) {
|
2015-07-30 14:43:58 +02:00
|
|
|
MPoly *mpoly;
|
|
|
|
|
face_side = (grid_size << 1) - 1;
|
|
|
|
|
|
|
|
|
|
mpoly = lodm->getPolyArray(lodm) + poly_index;
|
|
|
|
|
g_index = grid_offset[poly_index];
|
|
|
|
|
S = mdisp_rot_face_to_crn(lodm->getVertArray(lodm), mpoly, lodm->getLoopArray(lodm), lt, face_side, u * (face_side - 1), v * (face_side - 1), &crn_x, &crn_y);
|
2013-11-18 13:05:57 +06:00
|
|
|
}
|
|
|
|
|
else {
|
2015-07-30 14:43:58 +02:00
|
|
|
/* number of faces per grid side */
|
|
|
|
|
int polys_per_grid_side = (1 << (lvl - 1));
|
|
|
|
|
/* get the original cage face index */
|
|
|
|
|
int cage_face_index = index_mp_to_orig ? index_mp_to_orig[poly_index] : poly_index;
|
|
|
|
|
/* local offset in total cage face grids
|
|
|
|
|
* (1 << (2 * lvl)) is number of all polys for one cage face */
|
|
|
|
|
int loc_cage_poly_offs = poly_index % (1 << (2 * lvl));
|
|
|
|
|
/* local offset in the vertex grid itself */
|
|
|
|
|
int cell_index = loc_cage_poly_offs % (polys_per_grid_side * polys_per_grid_side);
|
|
|
|
|
int cell_side = (grid_size - 1) / polys_per_grid_side;
|
|
|
|
|
/* row and column based on grid side */
|
|
|
|
|
int row = cell_index / polys_per_grid_side;
|
|
|
|
|
int col = cell_index % polys_per_grid_side;
|
|
|
|
|
|
|
|
|
|
/* S is the vertex whose grid we are examining */
|
2015-09-24 20:14:51 +10:00
|
|
|
S = poly_index / (1 << (2 * (lvl - 1))) - grid_offset[cage_face_index];
|
2015-07-30 14:43:58 +02:00
|
|
|
/* get offset of grid data for original cage face */
|
|
|
|
|
g_index = grid_offset[cage_face_index];
|
2013-11-18 13:05:57 +06:00
|
|
|
|
|
|
|
|
crn_y = (row * cell_side) + u * cell_side;
|
|
|
|
|
crn_x = (col * cell_side) + v * cell_side;
|
|
|
|
|
}
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
CLAMP(crn_x, 0.0f, grid_size);
|
|
|
|
|
CLAMP(crn_y, 0.0f, grid_size);
|
|
|
|
|
|
|
|
|
|
if (n != NULL)
|
|
|
|
|
interp_bilinear_grid(&key, grid_data[g_index + S], crn_x, crn_y, 0, n);
|
|
|
|
|
|
|
|
|
|
if (co != NULL)
|
|
|
|
|
interp_bilinear_grid(&key, grid_data[g_index + S], crn_x, crn_y, 1, co);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* mode = 0: interpolate normals,
|
|
|
|
|
* mode = 1: interpolate coord */
|
2015-07-30 14:43:58 +02:00
|
|
|
|
|
|
|
|
static void interp_bilinear_mpoly(DerivedMesh *dm, MLoop *mloop, MPoly *mpoly, const float u, const float v, const int mode, float res[3])
|
2012-12-18 17:46:26 +00:00
|
|
|
{
|
|
|
|
|
float data[4][3];
|
|
|
|
|
|
|
|
|
|
if (mode == 0) {
|
2015-07-30 14:43:58 +02:00
|
|
|
dm->getVertNo(dm, mloop[mpoly->loopstart].v, data[0]);
|
|
|
|
|
dm->getVertNo(dm, mloop[mpoly->loopstart + 1].v, data[1]);
|
|
|
|
|
dm->getVertNo(dm, mloop[mpoly->loopstart + 2].v, data[2]);
|
|
|
|
|
dm->getVertNo(dm, mloop[mpoly->loopstart + 3].v, data[3]);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2015-07-30 14:43:58 +02:00
|
|
|
dm->getVertCo(dm, mloop[mpoly->loopstart].v, data[0]);
|
|
|
|
|
dm->getVertCo(dm, mloop[mpoly->loopstart + 1].v, data[1]);
|
|
|
|
|
dm->getVertCo(dm, mloop[mpoly->loopstart + 2].v, data[2]);
|
|
|
|
|
dm->getVertCo(dm, mloop[mpoly->loopstart + 3].v, data[3]);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interp_bilinear_quad_v3(data, u, v, res);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
static void interp_barycentric_mlooptri(DerivedMesh *dm, MLoop *mloop, const MLoopTri *lt, const float u, const float v, const int mode, float res[3])
|
2012-12-18 17:46:26 +00:00
|
|
|
{
|
|
|
|
|
float data[3][3];
|
|
|
|
|
|
|
|
|
|
if (mode == 0) {
|
2015-07-30 14:43:58 +02:00
|
|
|
dm->getVertNo(dm, mloop[lt->tri[0]].v, data[0]);
|
|
|
|
|
dm->getVertNo(dm, mloop[lt->tri[1]].v, data[1]);
|
|
|
|
|
dm->getVertNo(dm, mloop[lt->tri[2]].v, data[2]);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2015-07-30 14:43:58 +02:00
|
|
|
dm->getVertCo(dm, mloop[lt->tri[0]].v, data[0]);
|
|
|
|
|
dm->getVertCo(dm, mloop[lt->tri[1]].v, data[1]);
|
|
|
|
|
dm->getVertCo(dm, mloop[lt->tri[2]].v, data[2]);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interp_barycentric_tri_v3(data, u, v, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* **************** Displacement Baker **************** */
|
|
|
|
|
|
|
|
|
|
static void *init_heights_data(MultiresBakeRender *bkr, Image *ima)
|
|
|
|
|
{
|
|
|
|
|
MHeightBakeData *height_data;
|
|
|
|
|
ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
|
|
|
|
|
DerivedMesh *lodm = bkr->lores_dm;
|
2013-01-21 18:34:27 +00:00
|
|
|
BakeImBufuserData *userdata = ibuf->userdata;
|
|
|
|
|
|
|
|
|
|
if (userdata->displacement_buffer == NULL)
|
|
|
|
|
userdata->displacement_buffer = MEM_callocN(sizeof(float) * ibuf->x * ibuf->y, "MultiresBake heights");
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
height_data = MEM_callocN(sizeof(MHeightBakeData), "MultiresBake heightData");
|
|
|
|
|
|
|
|
|
|
height_data->ima = ima;
|
2013-01-21 18:34:27 +00:00
|
|
|
height_data->heights = userdata->displacement_buffer;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
if (!bkr->use_lores_mesh) {
|
|
|
|
|
SubsurfModifierData smd = {{NULL}};
|
|
|
|
|
int ss_lvl = bkr->tot_lvl - bkr->lvl;
|
|
|
|
|
|
|
|
|
|
CLAMP(ss_lvl, 0, 6);
|
|
|
|
|
|
|
|
|
|
if (ss_lvl > 0) {
|
|
|
|
|
smd.levels = smd.renderLevels = ss_lvl;
|
|
|
|
|
smd.flags |= eSubsurfModifierFlag_SubsurfUv;
|
|
|
|
|
|
|
|
|
|
if (bkr->simple)
|
|
|
|
|
smd.subdivType = ME_SIMPLE_SUBSURF;
|
|
|
|
|
|
|
|
|
|
height_data->ssdm = subsurf_make_derived_from_derived(bkr->lores_dm, &smd, NULL, 0);
|
2013-01-21 18:34:14 +00:00
|
|
|
init_ccgdm_arrays(height_data->ssdm);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
height_data->orig_index_mp_to_orig = lodm->getPolyDataArray(lodm, CD_ORIGINDEX);
|
|
|
|
|
|
|
|
|
|
BKE_image_release_ibuf(ima, ibuf, NULL);
|
|
|
|
|
|
|
|
|
|
return (void *)height_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void free_heights_data(void *bake_data)
|
|
|
|
|
{
|
|
|
|
|
MHeightBakeData *height_data = (MHeightBakeData *)bake_data;
|
|
|
|
|
|
|
|
|
|
if (height_data->ssdm)
|
|
|
|
|
height_data->ssdm->release(height_data->ssdm);
|
|
|
|
|
|
|
|
|
|
MEM_freeN(height_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* MultiresBake callback for heights baking
|
|
|
|
|
* general idea:
|
|
|
|
|
* - find coord of point with specified UV in hi-res mesh (let's call it p1)
|
|
|
|
|
* - find coord of point and normal with specified UV in lo-res mesh (or subdivided lo-res
|
|
|
|
|
* mesh to make texture smoother) let's call this point p0 and n.
|
|
|
|
|
* - height wound be dot(n, p1-p0) */
|
2013-01-21 18:34:27 +00:00
|
|
|
static void apply_heights_callback(DerivedMesh *lores_dm, DerivedMesh *hires_dm, void *thread_data_v, void *bake_data,
|
2015-07-30 14:43:58 +02:00
|
|
|
ImBuf *ibuf, const int tri_index, const int lvl, const float st[2],
|
2012-12-18 17:46:26 +00:00
|
|
|
float UNUSED(tangmat[3][3]), const int x, const int y)
|
|
|
|
|
{
|
2015-07-30 14:43:58 +02:00
|
|
|
const MLoopTri *lt = lores_dm->getLoopTriArray(lores_dm) + tri_index;
|
|
|
|
|
MLoop *mloop = lores_dm->getLoopArray(lores_dm);
|
|
|
|
|
MPoly *mpoly = lores_dm->getPolyArray(lores_dm) + lt->poly;
|
|
|
|
|
MLoopUV *mloopuv = lores_dm->getLoopDataArray(lores_dm, CD_MLOOPUV);
|
2012-12-18 17:46:26 +00:00
|
|
|
MHeightBakeData *height_data = (MHeightBakeData *)bake_data;
|
2013-01-21 18:34:27 +00:00
|
|
|
MultiresBakeThread *thread_data = (MultiresBakeThread *) thread_data_v;
|
2012-12-18 17:46:26 +00:00
|
|
|
float uv[2], *st0, *st1, *st2, *st3;
|
|
|
|
|
int pixel = ibuf->x * y + x;
|
|
|
|
|
float vec[3], p0[3], p1[3], n[3], len;
|
|
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
/* ideally we would work on triangles only, however, we rely on quads to get orthogonal
|
|
|
|
|
* coordinates for use in grid space (triangle barycentric is not orthogonal) */
|
|
|
|
|
if (mpoly->totloop == 4) {
|
|
|
|
|
st0 = mloopuv[mpoly->loopstart].uv;
|
|
|
|
|
st1 = mloopuv[mpoly->loopstart + 1].uv;
|
|
|
|
|
st2 = mloopuv[mpoly->loopstart + 2].uv;
|
|
|
|
|
st3 = mloopuv[mpoly->loopstart + 3].uv;
|
2014-03-26 11:15:21 +11:00
|
|
|
resolve_quad_uv_v2(uv, st, st0, st1, st2, st3);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
2015-07-30 14:43:58 +02:00
|
|
|
else {
|
|
|
|
|
st0 = mloopuv[lt->tri[0]].uv;
|
|
|
|
|
st1 = mloopuv[lt->tri[1]].uv;
|
|
|
|
|
st2 = mloopuv[lt->tri[2]].uv;
|
2014-03-26 11:15:21 +11:00
|
|
|
resolve_tri_uv_v2(uv, st, st0, st1, st2);
|
2015-07-30 14:43:58 +02:00
|
|
|
}
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
CLAMP(uv[0], 0.0f, 1.0f);
|
|
|
|
|
CLAMP(uv[1], 0.0f, 1.0f);
|
|
|
|
|
|
|
|
|
|
get_ccgdm_data(lores_dm, hires_dm,
|
2015-07-30 14:43:58 +02:00
|
|
|
height_data->orig_index_mp_to_orig,
|
|
|
|
|
lvl, lt, uv[0], uv[1], p1, NULL);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
if (height_data->ssdm) {
|
|
|
|
|
get_ccgdm_data(lores_dm, height_data->ssdm,
|
2015-07-30 14:43:58 +02:00
|
|
|
height_data->orig_index_mp_to_orig,
|
|
|
|
|
0, lt, uv[0], uv[1], p0, n);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2015-07-30 14:43:58 +02:00
|
|
|
if (mpoly->totloop == 4) {
|
|
|
|
|
interp_bilinear_mpoly(lores_dm, mloop, mpoly, uv[0], uv[1], 1, p0);
|
|
|
|
|
interp_bilinear_mpoly(lores_dm, mloop, mpoly, uv[0], uv[1], 0, n);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2015-07-30 14:43:58 +02:00
|
|
|
interp_barycentric_mlooptri(lores_dm, mloop, lt, uv[0], uv[1], 1, p0);
|
|
|
|
|
interp_barycentric_mlooptri(lores_dm, mloop, lt, uv[0], uv[1], 0, n);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub_v3_v3v3(vec, p1, p0);
|
|
|
|
|
len = dot_v3v3(n, vec);
|
|
|
|
|
|
|
|
|
|
height_data->heights[pixel] = len;
|
2013-01-21 18:34:27 +00:00
|
|
|
|
|
|
|
|
thread_data->height_min = min_ff(thread_data->height_min, len);
|
|
|
|
|
thread_data->height_max = max_ff(thread_data->height_max, len);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
if (ibuf->rect_float) {
|
|
|
|
|
float *rrgbf = ibuf->rect_float + pixel * 4;
|
2013-01-21 18:34:27 +00:00
|
|
|
rrgbf[0] = rrgbf[1] = rrgbf[2] = len;
|
2012-12-18 17:46:26 +00:00
|
|
|
rrgbf[3] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
char *rrgb = (char *)ibuf->rect + pixel * 4;
|
2013-01-21 18:34:27 +00:00
|
|
|
rrgb[0] = rrgb[1] = rrgb[2] = FTOCHAR(len);
|
2012-12-18 17:46:26 +00:00
|
|
|
rrgb[3] = 255;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* **************** Normal Maps Baker **************** */
|
|
|
|
|
|
|
|
|
|
static void *init_normal_data(MultiresBakeRender *bkr, Image *UNUSED(ima))
|
|
|
|
|
{
|
|
|
|
|
MNormalBakeData *normal_data;
|
|
|
|
|
DerivedMesh *lodm = bkr->lores_dm;
|
|
|
|
|
|
|
|
|
|
normal_data = MEM_callocN(sizeof(MNormalBakeData), "MultiresBake normalData");
|
|
|
|
|
|
|
|
|
|
normal_data->orig_index_mp_to_orig = lodm->getPolyDataArray(lodm, CD_ORIGINDEX);
|
|
|
|
|
|
|
|
|
|
return (void *)normal_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void free_normal_data(void *bake_data)
|
|
|
|
|
{
|
|
|
|
|
MNormalBakeData *normal_data = (MNormalBakeData *)bake_data;
|
|
|
|
|
|
|
|
|
|
MEM_freeN(normal_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* MultiresBake callback for normals' baking
|
|
|
|
|
* general idea:
|
|
|
|
|
* - find coord and normal of point with specified UV in hi-res mesh
|
|
|
|
|
* - multiply it by tangmat
|
|
|
|
|
* - vector in color space would be norm(vec) /2 + (0.5, 0.5, 0.5) */
|
2013-01-21 18:34:27 +00:00
|
|
|
static void apply_tangmat_callback(DerivedMesh *lores_dm, DerivedMesh *hires_dm, void *UNUSED(thread_data),
|
2015-07-30 14:43:58 +02:00
|
|
|
void *bake_data, ImBuf *ibuf, const int tri_index, const int lvl,
|
2013-01-21 18:34:27 +00:00
|
|
|
const float st[2], float tangmat[3][3], const int x, const int y)
|
2012-12-18 17:46:26 +00:00
|
|
|
{
|
2015-07-30 14:43:58 +02:00
|
|
|
const MLoopTri *lt = lores_dm->getLoopTriArray(lores_dm) + tri_index;
|
|
|
|
|
MPoly *mpoly = lores_dm->getPolyArray(lores_dm) + lt->poly;
|
|
|
|
|
MLoopUV *mloopuv = lores_dm->getLoopDataArray(lores_dm, CD_MLOOPUV);
|
2012-12-18 17:46:26 +00:00
|
|
|
MNormalBakeData *normal_data = (MNormalBakeData *)bake_data;
|
|
|
|
|
float uv[2], *st0, *st1, *st2, *st3;
|
|
|
|
|
int pixel = ibuf->x * y + x;
|
|
|
|
|
float n[3], vec[3], tmp[3] = {0.5, 0.5, 0.5};
|
|
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
/* ideally we would work on triangles only, however, we rely on quads to get orthogonal
|
|
|
|
|
* coordinates for use in grid space (triangle barycentric is not orthogonal) */
|
|
|
|
|
if (mpoly->totloop == 4) {
|
|
|
|
|
st0 = mloopuv[mpoly->loopstart].uv;
|
|
|
|
|
st1 = mloopuv[mpoly->loopstart + 1].uv;
|
|
|
|
|
st2 = mloopuv[mpoly->loopstart + 2].uv;
|
|
|
|
|
st3 = mloopuv[mpoly->loopstart + 3].uv;
|
2014-03-26 11:15:21 +11:00
|
|
|
resolve_quad_uv_v2(uv, st, st0, st1, st2, st3);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
2015-07-30 14:43:58 +02:00
|
|
|
else {
|
|
|
|
|
st0 = mloopuv[lt->tri[0]].uv;
|
|
|
|
|
st1 = mloopuv[lt->tri[1]].uv;
|
|
|
|
|
st2 = mloopuv[lt->tri[2]].uv;
|
2014-03-26 11:15:21 +11:00
|
|
|
resolve_tri_uv_v2(uv, st, st0, st1, st2);
|
2015-07-30 14:43:58 +02:00
|
|
|
}
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
CLAMP(uv[0], 0.0f, 1.0f);
|
|
|
|
|
CLAMP(uv[1], 0.0f, 1.0f);
|
|
|
|
|
|
|
|
|
|
get_ccgdm_data(lores_dm, hires_dm,
|
2015-07-30 14:43:58 +02:00
|
|
|
normal_data->orig_index_mp_to_orig,
|
|
|
|
|
lvl, lt, uv[0], uv[1], NULL, n);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
mul_v3_m3v3(vec, tangmat, n);
|
2016-07-08 10:14:49 +10:00
|
|
|
normalize_v3_length(vec, 0.5);
|
2012-12-18 17:46:26 +00:00
|
|
|
add_v3_v3(vec, tmp);
|
|
|
|
|
|
|
|
|
|
if (ibuf->rect_float) {
|
|
|
|
|
float *rrgbf = ibuf->rect_float + pixel * 4;
|
|
|
|
|
rrgbf[0] = vec[0];
|
|
|
|
|
rrgbf[1] = vec[1];
|
|
|
|
|
rrgbf[2] = vec[2];
|
|
|
|
|
rrgbf[3] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
unsigned char *rrgb = (unsigned char *)ibuf->rect + pixel * 4;
|
|
|
|
|
rgb_float_to_uchar(rrgb, vec);
|
|
|
|
|
rrgb[3] = 255;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 17:46:42 +00:00
|
|
|
/* **************** Ambient Occlusion Baker **************** */
|
|
|
|
|
|
2012-12-20 05:07:14 +00:00
|
|
|
// must be a power of two
|
2012-12-18 17:46:42 +00:00
|
|
|
#define MAX_NUMBER_OF_AO_RAYS 1024
|
|
|
|
|
|
|
|
|
|
static unsigned short ao_random_table_1[MAX_NUMBER_OF_AO_RAYS];
|
|
|
|
|
static unsigned short ao_random_table_2[MAX_NUMBER_OF_AO_RAYS];
|
|
|
|
|
|
|
|
|
|
static void init_ao_random(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_NUMBER_OF_AO_RAYS; i++) {
|
|
|
|
|
ao_random_table_1[i] = rand() & 0xffff;
|
|
|
|
|
ao_random_table_2[i] = rand() & 0xffff;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unsigned short get_ao_random1(const int i)
|
|
|
|
|
{
|
|
|
|
|
return ao_random_table_1[i & (MAX_NUMBER_OF_AO_RAYS - 1)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unsigned short get_ao_random2(const int i)
|
|
|
|
|
{
|
|
|
|
|
return ao_random_table_2[i & (MAX_NUMBER_OF_AO_RAYS - 1)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void build_permutation_table(unsigned short permutation[], unsigned short temp_permutation[],
|
|
|
|
|
const int number_of_rays, const int is_first_perm_table)
|
|
|
|
|
{
|
|
|
|
|
int i, k;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < number_of_rays; i++)
|
|
|
|
|
temp_permutation[i] = i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < number_of_rays; i++) {
|
|
|
|
|
const unsigned int nr_entries_left = number_of_rays - i;
|
2014-04-01 11:34:00 +11:00
|
|
|
unsigned short rnd = is_first_perm_table != false ? get_ao_random1(i) : get_ao_random2(i);
|
2012-12-18 17:46:42 +00:00
|
|
|
const unsigned short entry = rnd % nr_entries_left;
|
|
|
|
|
|
|
|
|
|
/* pull entry */
|
|
|
|
|
permutation[i] = temp_permutation[entry];
|
|
|
|
|
|
|
|
|
|
/* delete entry */
|
2013-02-11 00:49:00 +00:00
|
|
|
for (k = entry; k < nr_entries_left - 1; k++) {
|
2012-12-18 17:46:42 +00:00
|
|
|
temp_permutation[k] = temp_permutation[k + 1];
|
2013-02-11 00:49:00 +00:00
|
|
|
}
|
2012-12-18 17:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* verify permutation table
|
|
|
|
|
* every entry must appear exactly once
|
|
|
|
|
*/
|
|
|
|
|
#if 0
|
2013-02-11 00:49:00 +00:00
|
|
|
for (i = 0; i < number_of_rays; i++) temp_permutation[i] = 0;
|
|
|
|
|
for (i = 0; i < number_of_rays; i++) ++temp_permutation[permutation[i]];
|
|
|
|
|
for (i = 0; i < number_of_rays; i++) BLI_assert(temp_permutation[i] == 1);
|
2012-12-18 17:46:42 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void create_ao_raytree(MultiresBakeRender *bkr, MAOBakeData *ao_data)
|
|
|
|
|
{
|
|
|
|
|
DerivedMesh *hidm = bkr->hires_dm;
|
|
|
|
|
RayObject *raytree;
|
|
|
|
|
RayFace *face;
|
|
|
|
|
CCGElem **grid_data;
|
|
|
|
|
CCGKey key;
|
2013-01-07 05:26:12 +00:00
|
|
|
int num_grids, grid_size /*, face_side */, num_faces;
|
2012-12-18 17:46:42 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
num_grids = hidm->getNumGrids(hidm);
|
|
|
|
|
grid_size = hidm->getGridSize(hidm);
|
|
|
|
|
grid_data = hidm->getGridData(hidm);
|
|
|
|
|
hidm->getGridKey(hidm, &key);
|
|
|
|
|
|
2013-01-07 05:26:12 +00:00
|
|
|
/* face_side = (grid_size << 1) - 1; */ /* UNUSED */
|
2012-12-18 17:46:42 +00:00
|
|
|
num_faces = num_grids * (grid_size - 1) * (grid_size - 1);
|
|
|
|
|
|
2012-12-18 17:46:54 +00:00
|
|
|
raytree = ao_data->raytree = RE_rayobject_create(bkr->raytrace_structure, num_faces, bkr->octree_resolution);
|
2012-12-18 17:46:42 +00:00
|
|
|
face = ao_data->rayfaces = (RayFace *) MEM_callocN(num_faces * sizeof(RayFace), "ObjectRen faces");
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < num_grids; i++) {
|
|
|
|
|
int x, y;
|
|
|
|
|
for (x = 0; x < grid_size - 1; x++) {
|
|
|
|
|
for (y = 0; y < grid_size - 1; y++) {
|
|
|
|
|
float co[4][3];
|
|
|
|
|
|
|
|
|
|
copy_v3_v3(co[0], CCG_grid_elem_co(&key, grid_data[i], x, y));
|
|
|
|
|
copy_v3_v3(co[1], CCG_grid_elem_co(&key, grid_data[i], x, y + 1));
|
|
|
|
|
copy_v3_v3(co[2], CCG_grid_elem_co(&key, grid_data[i], x + 1, y + 1));
|
|
|
|
|
copy_v3_v3(co[3], CCG_grid_elem_co(&key, grid_data[i], x + 1, y));
|
|
|
|
|
|
|
|
|
|
RE_rayface_from_coords(face, ao_data, face, co[0], co[1], co[2], co[3]);
|
|
|
|
|
RE_rayobject_add(raytree, RE_rayobject_unalignRayFace(face));
|
|
|
|
|
|
|
|
|
|
face++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RE_rayobject_done(raytree);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void *init_ao_data(MultiresBakeRender *bkr, Image *UNUSED(ima))
|
|
|
|
|
{
|
|
|
|
|
MAOBakeData *ao_data;
|
|
|
|
|
DerivedMesh *lodm = bkr->lores_dm;
|
|
|
|
|
unsigned short *temp_permutation_table;
|
|
|
|
|
size_t permutation_size;
|
|
|
|
|
|
|
|
|
|
init_ao_random();
|
|
|
|
|
|
|
|
|
|
ao_data = MEM_callocN(sizeof(MAOBakeData), "MultiresBake aoData");
|
|
|
|
|
|
|
|
|
|
ao_data->number_of_rays = bkr->number_of_rays;
|
|
|
|
|
ao_data->bias = bkr->bias;
|
|
|
|
|
|
|
|
|
|
ao_data->orig_index_mp_to_orig = lodm->getPolyDataArray(lodm, CD_ORIGINDEX);
|
|
|
|
|
|
|
|
|
|
create_ao_raytree(bkr, ao_data);
|
|
|
|
|
|
|
|
|
|
/* initialize permutation tables */
|
|
|
|
|
permutation_size = sizeof(unsigned short) * bkr->number_of_rays;
|
|
|
|
|
ao_data->permutation_table_1 = MEM_callocN(permutation_size, "multires AO baker perm1");
|
|
|
|
|
ao_data->permutation_table_2 = MEM_callocN(permutation_size, "multires AO baker perm2");
|
|
|
|
|
temp_permutation_table = MEM_callocN(permutation_size, "multires AO baker temp perm");
|
|
|
|
|
|
|
|
|
|
build_permutation_table(ao_data->permutation_table_1, temp_permutation_table, bkr->number_of_rays, 1);
|
|
|
|
|
build_permutation_table(ao_data->permutation_table_2, temp_permutation_table, bkr->number_of_rays, 0);
|
|
|
|
|
|
|
|
|
|
MEM_freeN(temp_permutation_table);
|
|
|
|
|
|
|
|
|
|
return (void *)ao_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void free_ao_data(void *bake_data)
|
|
|
|
|
{
|
|
|
|
|
MAOBakeData *ao_data = (MAOBakeData *) bake_data;
|
|
|
|
|
|
|
|
|
|
RE_rayobject_free(ao_data->raytree);
|
|
|
|
|
MEM_freeN(ao_data->rayfaces);
|
|
|
|
|
|
|
|
|
|
MEM_freeN(ao_data->permutation_table_1);
|
|
|
|
|
MEM_freeN(ao_data->permutation_table_2);
|
|
|
|
|
|
|
|
|
|
MEM_freeN(ao_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* builds an X and a Y axis from the given Z axis */
|
|
|
|
|
static void build_coordinate_frame(float axisX[3], float axisY[3], const float axisZ[3])
|
|
|
|
|
{
|
|
|
|
|
const float faX = fabsf(axisZ[0]);
|
|
|
|
|
const float faY = fabsf(axisZ[1]);
|
|
|
|
|
const float faZ = fabsf(axisZ[2]);
|
|
|
|
|
|
|
|
|
|
if (faX <= faY && faX <= faZ) {
|
|
|
|
|
const float len = sqrtf(axisZ[1] * axisZ[1] + axisZ[2] * axisZ[2]);
|
|
|
|
|
axisY[0] = 0; axisY[1] = axisZ[2] / len; axisY[2] = -axisZ[1] / len;
|
|
|
|
|
cross_v3_v3v3(axisX, axisY, axisZ);
|
|
|
|
|
}
|
|
|
|
|
else if (faY <= faZ) {
|
|
|
|
|
const float len = sqrtf(axisZ[0] * axisZ[0] + axisZ[2] * axisZ[2]);
|
|
|
|
|
axisX[0] = axisZ[2] / len; axisX[1] = 0; axisX[2] = -axisZ[0] / len;
|
|
|
|
|
cross_v3_v3v3(axisY, axisZ, axisX);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const float len = sqrtf(axisZ[0] * axisZ[0] + axisZ[1] * axisZ[1]);
|
|
|
|
|
axisX[0] = axisZ[1] / len; axisX[1] = -axisZ[0] / len; axisX[2] = 0;
|
|
|
|
|
cross_v3_v3v3(axisY, axisZ, axisX);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 11:34:00 +11:00
|
|
|
/* return false if nothing was hit and true otherwise */
|
2012-12-18 17:46:42 +00:00
|
|
|
static int trace_ao_ray(MAOBakeData *ao_data, float ray_start[3], float ray_direction[3])
|
|
|
|
|
{
|
|
|
|
|
Isect isect = {{0}};
|
|
|
|
|
|
|
|
|
|
isect.dist = RE_RAYTRACE_MAXDIST;
|
|
|
|
|
copy_v3_v3(isect.start, ray_start);
|
|
|
|
|
copy_v3_v3(isect.dir, ray_direction);
|
|
|
|
|
isect.lay = -1;
|
|
|
|
|
|
|
|
|
|
normalize_v3(isect.dir);
|
|
|
|
|
|
|
|
|
|
return RE_rayobject_raycast(ao_data->raytree, &isect);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-21 18:34:27 +00:00
|
|
|
static void apply_ao_callback(DerivedMesh *lores_dm, DerivedMesh *hires_dm, void *UNUSED(thread_data),
|
2015-07-30 14:43:58 +02:00
|
|
|
void *bake_data, ImBuf *ibuf, const int tri_index, const int lvl,
|
2013-01-21 18:34:27 +00:00
|
|
|
const float st[2], float UNUSED(tangmat[3][3]), const int x, const int y)
|
2012-12-18 17:46:42 +00:00
|
|
|
{
|
2015-07-30 14:43:58 +02:00
|
|
|
const MLoopTri *lt = lores_dm->getLoopTriArray(lores_dm) + tri_index;
|
|
|
|
|
MPoly *mpoly = lores_dm->getPolyArray(lores_dm) + lt->poly;
|
|
|
|
|
MLoopUV *mloopuv = lores_dm->getLoopDataArray(lores_dm, CD_MLOOPUV);
|
2012-12-18 17:46:42 +00:00
|
|
|
MAOBakeData *ao_data = (MAOBakeData *) bake_data;
|
|
|
|
|
|
|
|
|
|
int i, k, perm_offs;
|
|
|
|
|
float pos[3], nrm[3];
|
|
|
|
|
float cen[3];
|
|
|
|
|
float axisX[3], axisY[3], axisZ[3];
|
|
|
|
|
float shadow = 0;
|
|
|
|
|
float value;
|
|
|
|
|
int pixel = ibuf->x * y + x;
|
|
|
|
|
float uv[2], *st0, *st1, *st2, *st3;
|
|
|
|
|
|
2015-07-30 14:43:58 +02:00
|
|
|
/* ideally we would work on triangles only, however, we rely on quads to get orthogonal
|
|
|
|
|
* coordinates for use in grid space (triangle barycentric is not orthogonal) */
|
|
|
|
|
if (mpoly->totloop == 4) {
|
|
|
|
|
st0 = mloopuv[mpoly->loopstart].uv;
|
|
|
|
|
st1 = mloopuv[mpoly->loopstart + 1].uv;
|
|
|
|
|
st2 = mloopuv[mpoly->loopstart + 2].uv;
|
|
|
|
|
st3 = mloopuv[mpoly->loopstart + 3].uv;
|
2014-03-26 11:15:21 +11:00
|
|
|
resolve_quad_uv_v2(uv, st, st0, st1, st2, st3);
|
2012-12-18 17:46:42 +00:00
|
|
|
}
|
2015-07-30 14:43:58 +02:00
|
|
|
else {
|
|
|
|
|
st0 = mloopuv[lt->tri[0]].uv;
|
|
|
|
|
st1 = mloopuv[lt->tri[1]].uv;
|
|
|
|
|
st2 = mloopuv[lt->tri[2]].uv;
|
2014-03-26 11:15:21 +11:00
|
|
|
resolve_tri_uv_v2(uv, st, st0, st1, st2);
|
2015-07-30 14:43:58 +02:00
|
|
|
}
|
2012-12-18 17:46:42 +00:00
|
|
|
|
|
|
|
|
CLAMP(uv[0], 0.0f, 1.0f);
|
|
|
|
|
CLAMP(uv[1], 0.0f, 1.0f);
|
|
|
|
|
|
|
|
|
|
get_ccgdm_data(lores_dm, hires_dm,
|
2015-07-30 14:43:58 +02:00
|
|
|
ao_data->orig_index_mp_to_orig,
|
|
|
|
|
lvl, lt, uv[0], uv[1], pos, nrm);
|
2012-12-18 17:46:42 +00:00
|
|
|
|
|
|
|
|
/* offset ray origin by user bias along normal */
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
|
cen[i] = pos[i] + ao_data->bias * nrm[i];
|
|
|
|
|
|
|
|
|
|
/* build tangent frame */
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
|
axisZ[i] = nrm[i];
|
|
|
|
|
|
|
|
|
|
build_coordinate_frame(axisX, axisY, axisZ);
|
|
|
|
|
|
|
|
|
|
/* static noise */
|
|
|
|
|
perm_offs = (get_ao_random2(get_ao_random1(x) + y)) & (MAX_NUMBER_OF_AO_RAYS - 1);
|
|
|
|
|
|
|
|
|
|
/* importance sample shadow rays (cosine weighted) */
|
|
|
|
|
for (i = 0; i < ao_data->number_of_rays; i++) {
|
|
|
|
|
int hit_something;
|
|
|
|
|
|
|
|
|
|
/* use N-Rooks to distribute our N ray samples across
|
|
|
|
|
* a multi-dimensional domain (2D)
|
|
|
|
|
*/
|
2012-12-20 05:03:00 +00:00
|
|
|
const unsigned short I = ao_data->permutation_table_1[(i + perm_offs) % ao_data->number_of_rays];
|
|
|
|
|
const unsigned short J = ao_data->permutation_table_2[i];
|
2012-12-18 17:46:42 +00:00
|
|
|
|
|
|
|
|
const float JitPh = (get_ao_random2(I + perm_offs) & (MAX_NUMBER_OF_AO_RAYS-1))/((float) MAX_NUMBER_OF_AO_RAYS);
|
|
|
|
|
const float JitTh = (get_ao_random1(J + perm_offs) & (MAX_NUMBER_OF_AO_RAYS-1))/((float) MAX_NUMBER_OF_AO_RAYS);
|
|
|
|
|
const float SiSqPhi = (I + JitPh) / ao_data->number_of_rays;
|
2012-12-19 01:48:54 +00:00
|
|
|
const float Theta = (float)(2 * M_PI) * ((J + JitTh) / ao_data->number_of_rays);
|
2012-12-18 17:46:42 +00:00
|
|
|
|
|
|
|
|
/* this gives results identical to the so-called cosine
|
|
|
|
|
* weighted distribution relative to the north pole.
|
|
|
|
|
*/
|
2014-09-17 14:11:37 +10:00
|
|
|
float SiPhi = sqrtf(SiSqPhi);
|
2012-12-20 05:03:00 +00:00
|
|
|
float CoPhi = SiSqPhi < 1.0f ? sqrtf(1.0f - SiSqPhi) : 0;
|
2014-09-17 14:11:37 +10:00
|
|
|
float CoThe = cosf(Theta);
|
|
|
|
|
float SiThe = sinf(Theta);
|
2012-12-18 17:46:42 +00:00
|
|
|
|
|
|
|
|
const float dx = CoThe * CoPhi;
|
|
|
|
|
const float dy = SiThe * CoPhi;
|
|
|
|
|
const float dz = SiPhi;
|
|
|
|
|
|
|
|
|
|
/* transform ray direction out of tangent frame */
|
|
|
|
|
float dv[3];
|
|
|
|
|
for (k = 0; k < 3; k++)
|
|
|
|
|
dv[k] = axisX[k] * dx + axisY[k] * dy + axisZ[k] * dz;
|
|
|
|
|
|
|
|
|
|
hit_something = trace_ao_ray(ao_data, cen, dv);
|
|
|
|
|
|
|
|
|
|
if (hit_something != 0)
|
|
|
|
|
shadow += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value = 1.0f - (shadow / ao_data->number_of_rays);
|
|
|
|
|
|
|
|
|
|
if (ibuf->rect_float) {
|
|
|
|
|
float *rrgbf = ibuf->rect_float + pixel * 4;
|
|
|
|
|
rrgbf[0] = rrgbf[1] = rrgbf[2] = value;
|
|
|
|
|
rrgbf[3] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
unsigned char *rrgb = (unsigned char *) ibuf->rect + pixel * 4;
|
|
|
|
|
rrgb[0] = rrgb[1] = rrgb[2] = FTOCHAR(value);
|
|
|
|
|
rrgb[3] = 255;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 17:46:26 +00:00
|
|
|
/* **************** Common functions public API relates on **************** */
|
|
|
|
|
|
|
|
|
|
static void count_images(MultiresBakeRender *bkr)
|
|
|
|
|
{
|
2015-09-14 15:55:43 +10:00
|
|
|
int a, totpoly;
|
2012-12-18 17:46:26 +00:00
|
|
|
DerivedMesh *dm = bkr->lores_dm;
|
2015-09-14 15:55:43 +10:00
|
|
|
MTexPoly *mtexpoly = CustomData_get_layer(&dm->polyData, CD_MTEXPOLY);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2014-02-08 06:07:10 +11:00
|
|
|
BLI_listbase_clear(&bkr->image);
|
2012-12-18 17:46:26 +00:00
|
|
|
bkr->tot_image = 0;
|
|
|
|
|
|
2015-09-14 15:55:43 +10:00
|
|
|
totpoly = dm->getNumPolys(dm);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2015-09-14 15:55:43 +10:00
|
|
|
for (a = 0; a < totpoly; a++)
|
Split id->flag in two, persistent flags and runtime tags.
This is purely internal sanitizing/cleanup, no change in behavior is expected at all.
This change was also needed because we were getting short on ID flags, and
future enhancement of 'user_one' ID behavior requires two new ones.
id->flag remains for persistent data (fakeuser only, so far!), this also allows us
100% backward & forward compatibility.
New id->tag is used for most flags. Though written in .blend files, its content
is cleared at read time.
Note that .blend file version was bumped, so that we can clear runtimeflags from
old .blends, important in case we add new persistent flags in future.
Also, behavior of tags (either status ones, or whether they need to be cleared before/after use)
has been added as comments to their declaration.
Reviewers: sergey, campbellbarton
Differential Revision: https://developer.blender.org/D1683
2015-12-27 11:53:50 +01:00
|
|
|
mtexpoly[a].tpage->id.tag &= ~LIB_TAG_DOIT;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2015-09-14 15:55:43 +10:00
|
|
|
for (a = 0; a < totpoly; a++) {
|
|
|
|
|
Image *ima = mtexpoly[a].tpage;
|
Split id->flag in two, persistent flags and runtime tags.
This is purely internal sanitizing/cleanup, no change in behavior is expected at all.
This change was also needed because we were getting short on ID flags, and
future enhancement of 'user_one' ID behavior requires two new ones.
id->flag remains for persistent data (fakeuser only, so far!), this also allows us
100% backward & forward compatibility.
New id->tag is used for most flags. Though written in .blend files, its content
is cleared at read time.
Note that .blend file version was bumped, so that we can clear runtimeflags from
old .blends, important in case we add new persistent flags in future.
Also, behavior of tags (either status ones, or whether they need to be cleared before/after use)
has been added as comments to their declaration.
Reviewers: sergey, campbellbarton
Differential Revision: https://developer.blender.org/D1683
2015-12-27 11:53:50 +01:00
|
|
|
if ((ima->id.tag & LIB_TAG_DOIT) == 0) {
|
2012-12-18 17:46:26 +00:00
|
|
|
LinkData *data = BLI_genericNodeN(ima);
|
|
|
|
|
BLI_addtail(&bkr->image, data);
|
|
|
|
|
bkr->tot_image++;
|
Split id->flag in two, persistent flags and runtime tags.
This is purely internal sanitizing/cleanup, no change in behavior is expected at all.
This change was also needed because we were getting short on ID flags, and
future enhancement of 'user_one' ID behavior requires two new ones.
id->flag remains for persistent data (fakeuser only, so far!), this also allows us
100% backward & forward compatibility.
New id->tag is used for most flags. Though written in .blend files, its content
is cleared at read time.
Note that .blend file version was bumped, so that we can clear runtimeflags from
old .blends, important in case we add new persistent flags in future.
Also, behavior of tags (either status ones, or whether they need to be cleared before/after use)
has been added as comments to their declaration.
Reviewers: sergey, campbellbarton
Differential Revision: https://developer.blender.org/D1683
2015-12-27 11:53:50 +01:00
|
|
|
ima->id.tag |= LIB_TAG_DOIT;
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-14 15:55:43 +10:00
|
|
|
for (a = 0; a < totpoly; a++)
|
Split id->flag in two, persistent flags and runtime tags.
This is purely internal sanitizing/cleanup, no change in behavior is expected at all.
This change was also needed because we were getting short on ID flags, and
future enhancement of 'user_one' ID behavior requires two new ones.
id->flag remains for persistent data (fakeuser only, so far!), this also allows us
100% backward & forward compatibility.
New id->tag is used for most flags. Though written in .blend files, its content
is cleared at read time.
Note that .blend file version was bumped, so that we can clear runtimeflags from
old .blends, important in case we add new persistent flags in future.
Also, behavior of tags (either status ones, or whether they need to be cleared before/after use)
has been added as comments to their declaration.
Reviewers: sergey, campbellbarton
Differential Revision: https://developer.blender.org/D1683
2015-12-27 11:53:50 +01:00
|
|
|
mtexpoly[a].tpage->id.tag &= ~LIB_TAG_DOIT;
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-21 18:34:27 +00:00
|
|
|
static void bake_images(MultiresBakeRender *bkr, MultiresBakeResult *result)
|
2012-12-18 17:46:26 +00:00
|
|
|
{
|
|
|
|
|
LinkData *link;
|
|
|
|
|
|
|
|
|
|
for (link = bkr->image.first; link; link = link->next) {
|
|
|
|
|
Image *ima = (Image *)link->data;
|
|
|
|
|
ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
|
|
|
|
|
|
|
|
|
|
if (ibuf->x > 0 && ibuf->y > 0) {
|
2013-01-21 18:34:27 +00:00
|
|
|
BakeImBufuserData *userdata = MEM_callocN(sizeof(BakeImBufuserData), "MultiresBake userdata");
|
|
|
|
|
userdata->mask_buffer = MEM_callocN(ibuf->y * ibuf->x, "MultiresBake imbuf mask");
|
|
|
|
|
ibuf->userdata = userdata;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
switch (bkr->mode) {
|
|
|
|
|
case RE_BAKE_NORMALS:
|
2014-04-01 11:34:00 +11:00
|
|
|
do_multires_bake(bkr, ima, true, apply_tangmat_callback, init_normal_data, free_normal_data, result);
|
2012-12-18 17:46:26 +00:00
|
|
|
break;
|
|
|
|
|
case RE_BAKE_DISPLACEMENT:
|
2013-10-09 15:51:14 +00:00
|
|
|
case RE_BAKE_DERIVATIVE:
|
2014-04-01 11:34:00 +11:00
|
|
|
do_multires_bake(bkr, ima, false, apply_heights_callback, init_heights_data, free_heights_data, result);
|
2012-12-18 17:46:26 +00:00
|
|
|
break;
|
2012-12-18 17:46:42 +00:00
|
|
|
case RE_BAKE_AO:
|
2014-04-01 11:34:00 +11:00
|
|
|
do_multires_bake(bkr, ima, false, apply_ao_callback, init_ao_data, free_ao_data, result);
|
2012-12-18 17:46:42 +00:00
|
|
|
break;
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BKE_image_release_ibuf(ima, ibuf, NULL);
|
|
|
|
|
|
Split id->flag in two, persistent flags and runtime tags.
This is purely internal sanitizing/cleanup, no change in behavior is expected at all.
This change was also needed because we were getting short on ID flags, and
future enhancement of 'user_one' ID behavior requires two new ones.
id->flag remains for persistent data (fakeuser only, so far!), this also allows us
100% backward & forward compatibility.
New id->tag is used for most flags. Though written in .blend files, its content
is cleared at read time.
Note that .blend file version was bumped, so that we can clear runtimeflags from
old .blends, important in case we add new persistent flags in future.
Also, behavior of tags (either status ones, or whether they need to be cleared before/after use)
has been added as comments to their declaration.
Reviewers: sergey, campbellbarton
Differential Revision: https://developer.blender.org/D1683
2015-12-27 11:53:50 +01:00
|
|
|
ima->id.tag |= LIB_TAG_DOIT;
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-21 18:34:27 +00:00
|
|
|
static void finish_images(MultiresBakeRender *bkr, MultiresBakeResult *result)
|
2012-12-18 17:46:26 +00:00
|
|
|
{
|
|
|
|
|
LinkData *link;
|
2013-10-09 15:51:14 +00:00
|
|
|
bool use_displacement_buffer = ELEM(bkr->mode, RE_BAKE_DISPLACEMENT, RE_BAKE_DERIVATIVE);
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
for (link = bkr->image.first; link; link = link->next) {
|
|
|
|
|
Image *ima = (Image *)link->data;
|
|
|
|
|
ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
|
2013-01-21 18:34:27 +00:00
|
|
|
BakeImBufuserData *userdata = (BakeImBufuserData *) ibuf->userdata;
|
2012-12-18 17:46:26 +00:00
|
|
|
|
|
|
|
|
if (ibuf->x <= 0 || ibuf->y <= 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-21 18:34:27 +00:00
|
|
|
if (use_displacement_buffer) {
|
2013-10-09 15:51:14 +00:00
|
|
|
if (bkr->mode == RE_BAKE_DERIVATIVE) {
|
|
|
|
|
RE_bake_make_derivative(ibuf, userdata->displacement_buffer, userdata->mask_buffer,
|
|
|
|
|
result->height_min, result->height_max, bkr->user_scale);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
RE_bake_ibuf_normalize_displacement(ibuf, userdata->displacement_buffer, userdata->mask_buffer,
|
|
|
|
|
result->height_min, result->height_max);
|
|
|
|
|
}
|
2013-01-21 18:34:27 +00:00
|
|
|
}
|
2012-12-18 17:46:26 +00:00
|
|
|
|
2013-02-22 09:57:10 +00:00
|
|
|
RE_bake_ibuf_filter(ibuf, userdata->mask_buffer, bkr->bake_filter);
|
|
|
|
|
|
2012-12-18 17:46:26 +00:00
|
|
|
ibuf->userflags |= IB_BITMAPDIRTY | IB_DISPLAY_BUFFER_INVALID;
|
|
|
|
|
|
|
|
|
|
if (ibuf->rect_float)
|
|
|
|
|
ibuf->userflags |= IB_RECT_INVALID;
|
|
|
|
|
|
|
|
|
|
if (ibuf->mipmap[0]) {
|
|
|
|
|
ibuf->userflags |= IB_MIPMAP_INVALID;
|
|
|
|
|
imb_freemipmapImBuf(ibuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ibuf->userdata) {
|
2013-01-21 18:34:27 +00:00
|
|
|
if (userdata->displacement_buffer)
|
|
|
|
|
MEM_freeN(userdata->displacement_buffer);
|
|
|
|
|
|
|
|
|
|
MEM_freeN(userdata->mask_buffer);
|
|
|
|
|
MEM_freeN(userdata);
|
2012-12-18 17:46:26 +00:00
|
|
|
ibuf->userdata = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BKE_image_release_ibuf(ima, ibuf, NULL);
|
2014-09-19 15:36:49 +02:00
|
|
|
DAG_id_tag_update(&ima->id, 0);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RE_multires_bake_images(MultiresBakeRender *bkr)
|
|
|
|
|
{
|
2013-01-21 18:34:27 +00:00
|
|
|
MultiresBakeResult result;
|
|
|
|
|
|
2012-12-18 17:46:26 +00:00
|
|
|
count_images(bkr);
|
2013-01-21 18:34:27 +00:00
|
|
|
bake_images(bkr, &result);
|
|
|
|
|
finish_images(bkr, &result);
|
2012-12-18 17:46:26 +00:00
|
|
|
}
|