2013-02-12 05:09:35 +00:00
|
|
|
/*
|
2011-11-13 12:17:27 +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) Blender Foundation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): Matt Ebb
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2013-02-12 05:09:35 +00:00
|
|
|
/** \file blender/modifiers/intern/MOD_ocean.c
|
|
|
|
* \ingroup modifiers
|
|
|
|
*/
|
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
#include "DNA_customdata_types.h"
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
#include "DNA_modifier_types.h"
|
|
|
|
#include "DNA_scene_types.h"
|
|
|
|
|
|
|
|
#include "BLI_math.h"
|
|
|
|
#include "BLI_math_inline.h"
|
2015-12-06 18:59:20 +01:00
|
|
|
#include "BLI_task.h"
|
2011-11-13 12:17:27 +00:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
2012-10-30 19:20:17 +00:00
|
|
|
#include "BKE_cdderivedmesh.h"
|
2018-05-31 15:24:30 +02:00
|
|
|
#include "BKE_global.h"
|
|
|
|
#include "BKE_main.h"
|
2012-10-30 19:20:17 +00:00
|
|
|
#include "BKE_modifier.h"
|
|
|
|
#include "BKE_ocean.h"
|
|
|
|
|
2018-06-22 15:03:42 +02:00
|
|
|
#include "DEG_depsgraph_query.h"
|
|
|
|
|
2017-08-01 09:06:34 +10:00
|
|
|
#include "MOD_modifiertypes.h"
|
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
#ifdef WITH_OCEANSIM
|
2018-06-09 15:16:44 +02:00
|
|
|
static void init_cache_data(Object *ob, struct OceanModifierData *omd)
|
2011-11-13 12:17:27 +00:00
|
|
|
{
|
2018-06-09 15:16:44 +02:00
|
|
|
const char *relbase = modifier_path_relbase_from_global(ob);
|
2011-11-20 14:38:11 +00:00
|
|
|
|
2015-04-04 15:18:52 +11:00
|
|
|
omd->oceancache = BKE_ocean_init_cache(omd->cachepath, relbase,
|
2011-11-20 14:38:11 +00:00
|
|
|
omd->bakestart, omd->bakeend, omd->wave_scale,
|
|
|
|
omd->chop_amount, omd->foam_coverage, omd->foam_fade, omd->resolution);
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void clear_cache_data(struct OceanModifierData *omd)
|
|
|
|
{
|
2015-04-04 15:18:52 +11:00
|
|
|
BKE_ocean_free_cache(omd->oceancache);
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->oceancache = NULL;
|
2014-04-01 11:34:00 +11:00
|
|
|
omd->cached = false;
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* keep in sync with init_ocean_modifier_bake(), object_modifier.c */
|
|
|
|
static void init_ocean_modifier(struct OceanModifierData *omd)
|
|
|
|
{
|
|
|
|
int do_heightfield, do_chop, do_normals, do_jacobian;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
|
|
|
if (!omd || !omd->ocean) return;
|
|
|
|
|
2014-04-01 11:34:00 +11:00
|
|
|
do_heightfield = true;
|
2011-11-13 12:17:27 +00:00
|
|
|
do_chop = (omd->chop_amount > 0);
|
|
|
|
do_normals = (omd->flag & MOD_OCEAN_GENERATE_NORMALS);
|
|
|
|
do_jacobian = (omd->flag & MOD_OCEAN_GENERATE_FOAM);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-04-04 15:18:52 +11:00
|
|
|
BKE_ocean_free_data(omd->ocean);
|
|
|
|
BKE_ocean_init(omd->ocean, omd->resolution * omd->resolution, omd->resolution * omd->resolution,
|
2012-05-06 13:38:33 +00:00
|
|
|
omd->spatial_size, omd->spatial_size,
|
2011-11-22 18:03:33 +00:00
|
|
|
omd->wind_velocity, omd->smallest_wave, 1.0, omd->wave_direction, omd->damp, omd->wave_alignment,
|
|
|
|
omd->depth, omd->time,
|
|
|
|
do_heightfield, do_chop, do_normals, do_jacobian,
|
|
|
|
omd->seed);
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void simulate_ocean_modifier(struct OceanModifierData *omd)
|
|
|
|
{
|
|
|
|
if (!omd || !omd->ocean) return;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-04-04 15:18:52 +11:00
|
|
|
BKE_ocean_simulate(omd->ocean, omd->time, omd->wave_scale, omd->chop_amount);
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
2011-11-22 18:03:33 +00:00
|
|
|
#endif /* WITH_OCEANSIM */
|
2011-11-13 12:17:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Modifier Code */
|
|
|
|
|
|
|
|
static void initData(ModifierData *md)
|
|
|
|
{
|
|
|
|
#ifdef WITH_OCEANSIM
|
2012-05-06 13:38:33 +00:00
|
|
|
OceanModifierData *omd = (OceanModifierData *) md;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
|
|
|
omd->resolution = 7;
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->spatial_size = 50;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->wave_alignment = 0.0;
|
|
|
|
omd->wind_velocity = 30.0;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->damp = 0.5;
|
|
|
|
omd->smallest_wave = 0.01;
|
2012-05-06 13:38:33 +00:00
|
|
|
omd->wave_direction = 0.0;
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->depth = 200.0;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->wave_scale = 1.0;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->chop_amount = 1.0;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->foam_coverage = 0.0;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->seed = 0;
|
|
|
|
omd->time = 1.0;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->refresh = 0;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->size = 1.0;
|
|
|
|
omd->repeat_x = 1;
|
|
|
|
omd->repeat_y = 1;
|
2011-11-13 15:17:24 +00:00
|
|
|
|
2011-11-22 10:04:28 +00:00
|
|
|
modifier_path_init(omd->cachepath, sizeof(omd->cachepath), "cache_ocean");
|
2011-11-13 15:17:24 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->cached = 0;
|
|
|
|
omd->bakestart = 1;
|
|
|
|
omd->bakeend = 250;
|
|
|
|
omd->oceancache = NULL;
|
|
|
|
omd->foam_fade = 0.98;
|
2012-05-06 13:38:33 +00:00
|
|
|
omd->foamlayername[0] = '\0'; /* layer name empty by default */
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-04-04 15:18:52 +11:00
|
|
|
omd->ocean = BKE_ocean_add();
|
2011-11-13 12:17:27 +00:00
|
|
|
init_ocean_modifier(omd);
|
|
|
|
simulate_ocean_modifier(omd);
|
2013-01-02 16:03:58 +00:00
|
|
|
#else /* WITH_OCEANSIM */
|
2012-05-06 13:38:33 +00:00
|
|
|
/* unused */
|
2011-11-13 12:17:27 +00:00
|
|
|
(void)md;
|
2013-01-02 16:03:58 +00:00
|
|
|
#endif /* WITH_OCEANSIM */
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void freeData(ModifierData *md)
|
|
|
|
{
|
|
|
|
#ifdef WITH_OCEANSIM
|
2012-05-06 13:38:33 +00:00
|
|
|
OceanModifierData *omd = (OceanModifierData *) md;
|
2011-11-13 12:17:27 +00:00
|
|
|
|
2015-04-04 15:18:52 +11:00
|
|
|
BKE_ocean_free(omd->ocean);
|
2011-11-13 12:17:27 +00:00
|
|
|
if (omd->oceancache)
|
2015-04-04 15:18:52 +11:00
|
|
|
BKE_ocean_free_cache(omd->oceancache);
|
2013-01-02 16:03:58 +00:00
|
|
|
#else /* WITH_OCEANSIM */
|
2011-11-13 12:17:27 +00:00
|
|
|
/* unused */
|
|
|
|
(void)md;
|
2013-01-02 16:03:58 +00:00
|
|
|
#endif /* WITH_OCEANSIM */
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 15:15:08 +02:00
|
|
|
static void copyData(const ModifierData *md, ModifierData *target, const int flag)
|
2011-11-13 12:17:27 +00:00
|
|
|
{
|
|
|
|
#ifdef WITH_OCEANSIM
|
2017-06-19 14:40:08 +02:00
|
|
|
#if 0
|
2018-05-08 15:04:10 +02:00
|
|
|
const OceanModifierData *omd = (const OceanModifierData *) md;
|
2017-06-19 14:40:08 +02:00
|
|
|
#endif
|
2012-05-06 13:38:33 +00:00
|
|
|
OceanModifierData *tomd = (OceanModifierData *) target;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2018-07-04 15:15:08 +02:00
|
|
|
modifier_copyData_generic(md, target, flag);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
tomd->refresh = 0;
|
|
|
|
|
|
|
|
/* XXX todo: copy cache runtime too */
|
|
|
|
tomd->cached = 0;
|
|
|
|
tomd->oceancache = NULL;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-04-04 15:18:52 +11:00
|
|
|
tomd->ocean = BKE_ocean_add();
|
2011-11-13 12:17:27 +00:00
|
|
|
init_ocean_modifier(tomd);
|
|
|
|
simulate_ocean_modifier(tomd);
|
2013-01-02 16:03:58 +00:00
|
|
|
#else /* WITH_OCEANSIM */
|
2011-11-13 12:17:27 +00:00
|
|
|
/* unused */
|
|
|
|
(void)md;
|
|
|
|
(void)target;
|
2018-07-04 15:15:08 +02:00
|
|
|
(void)flag;
|
2013-01-02 16:03:58 +00:00
|
|
|
#endif /* WITH_OCEANSIM */
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_OCEANSIM
|
|
|
|
static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
|
|
|
|
{
|
|
|
|
OceanModifierData *omd = (OceanModifierData *)md;
|
|
|
|
CustomDataMask dataMask = 0;
|
|
|
|
|
|
|
|
if (omd->flag & MOD_OCEAN_GENERATE_FOAM)
|
|
|
|
dataMask |= CD_MASK_MCOL;
|
|
|
|
|
|
|
|
return dataMask;
|
|
|
|
}
|
2013-01-02 16:03:58 +00:00
|
|
|
#else /* WITH_OCEANSIM */
|
2011-11-13 12:17:27 +00:00
|
|
|
static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
|
|
|
|
{
|
|
|
|
/* unused */
|
|
|
|
(void)md;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-02 16:03:58 +00:00
|
|
|
#endif /* WITH_OCEANSIM */
|
2011-11-13 12:17:27 +00:00
|
|
|
|
2013-06-05 19:22:12 +00:00
|
|
|
static bool dependsOnNormals(ModifierData *md)
|
|
|
|
{
|
|
|
|
OceanModifierData *omd = (OceanModifierData *)md;
|
|
|
|
return (omd->geometry_mode != MOD_OCEAN_GEOM_GENERATE);
|
|
|
|
}
|
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
#if 0
|
2011-11-13 16:10:01 +00:00
|
|
|
static void dm_get_bounds(DerivedMesh *dm, float *sx, float *sy, float *ox, float *oy)
|
2011-11-13 12:17:27 +00:00
|
|
|
{
|
|
|
|
/* get bounding box of underlying dm */
|
2012-05-06 13:38:33 +00:00
|
|
|
int v, totvert = dm->getNumVerts(dm);
|
2011-11-13 12:17:27 +00:00
|
|
|
float min[3], max[3], delta[3];
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2012-05-06 13:38:33 +00:00
|
|
|
MVert *mvert = dm->getVertDataArray(dm, 0);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
copy_v3_v3(min, mvert->co);
|
|
|
|
copy_v3_v3(max, mvert->co);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2012-05-06 13:38:33 +00:00
|
|
|
for (v = 1; v < totvert; v++, mvert++) {
|
2012-10-23 13:28:22 +00:00
|
|
|
min[0] = min_ff(min[0], mvert->co[0]);
|
|
|
|
min[1] = min_ff(min[1], mvert->co[1]);
|
|
|
|
min[2] = min_ff(min[2], mvert->co[2]);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2012-10-23 13:28:22 +00:00
|
|
|
max[0] = max_ff(max[0], mvert->co[0]);
|
|
|
|
max[1] = max_ff(max[1], mvert->co[1]);
|
|
|
|
max[2] = max_ff(max[2], mvert->co[2]);
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
sub_v3_v3v3(delta, max, min);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
*sx = delta[0];
|
|
|
|
*sy = delta[1];
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
*ox = min[0];
|
|
|
|
*oy = min[1];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_OCEANSIM
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 18:59:20 +01:00
|
|
|
typedef struct GenerateOceanGeometryData {
|
2011-11-22 18:51:01 +00:00
|
|
|
MVert *mverts;
|
2011-11-22 19:43:09 +00:00
|
|
|
MPoly *mpolys;
|
|
|
|
MLoop *mloops;
|
2011-11-21 11:46:05 +00:00
|
|
|
int *origindex;
|
2015-12-06 18:59:20 +01:00
|
|
|
MLoopUV *mloopuvs;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 18:59:20 +01:00
|
|
|
int res_x, res_y;
|
|
|
|
int rx, ry;
|
|
|
|
float ox, oy;
|
|
|
|
float sx, sy;
|
|
|
|
float ix, iy;
|
|
|
|
} GenerateOceanGeometryData;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2018-01-10 12:49:51 +01:00
|
|
|
static void generate_ocean_geometry_vertices(
|
|
|
|
void *__restrict userdata,
|
|
|
|
const int y,
|
|
|
|
const ParallelRangeTLS *__restrict UNUSED(tls))
|
2015-12-06 18:59:20 +01:00
|
|
|
{
|
|
|
|
GenerateOceanGeometryData *gogd = userdata;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
for (x = 0; x <= gogd->res_x; x++) {
|
|
|
|
const int i = y * (gogd->res_x + 1) + x;
|
|
|
|
float *co = gogd->mverts[i].co;
|
|
|
|
co[0] = gogd->ox + (x * gogd->sx);
|
|
|
|
co[1] = gogd->oy + (y * gogd->sy);
|
|
|
|
co[2] = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2018-01-10 12:49:51 +01:00
|
|
|
static void generate_ocean_geometry_polygons(
|
|
|
|
void *__restrict userdata,
|
|
|
|
const int y,
|
|
|
|
const ParallelRangeTLS *__restrict UNUSED(tls))
|
2015-12-06 18:59:20 +01:00
|
|
|
{
|
|
|
|
GenerateOceanGeometryData *gogd = userdata;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
for (x = 0; x < gogd->res_x; x++) {
|
|
|
|
const int fi = y * gogd->res_x + x;
|
|
|
|
const int vi = y * (gogd->res_x + 1) + x;
|
|
|
|
MPoly *mp = &gogd->mpolys[fi];
|
|
|
|
MLoop *ml = &gogd->mloops[fi * 4];
|
|
|
|
|
|
|
|
ml->v = vi;
|
|
|
|
ml++;
|
|
|
|
ml->v = vi + 1;
|
|
|
|
ml++;
|
|
|
|
ml->v = vi + 1 + gogd->res_x + 1;
|
|
|
|
ml++;
|
|
|
|
ml->v = vi + gogd->res_x + 1;
|
|
|
|
ml++;
|
|
|
|
|
|
|
|
mp->loopstart = fi * 4;
|
|
|
|
mp->totloop = 4;
|
|
|
|
|
|
|
|
mp->flag |= ME_SMOOTH;
|
|
|
|
|
|
|
|
/* generated geometry does not map to original faces */
|
|
|
|
gogd->origindex[fi] = ORIGINDEX_NONE;
|
|
|
|
}
|
|
|
|
}
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2018-01-10 12:49:51 +01:00
|
|
|
static void generate_ocean_geometry_uvs(
|
|
|
|
void *__restrict userdata,
|
|
|
|
const int y,
|
|
|
|
const ParallelRangeTLS *__restrict UNUSED(tls))
|
2015-12-06 18:59:20 +01:00
|
|
|
{
|
|
|
|
GenerateOceanGeometryData *gogd = userdata;
|
|
|
|
int x;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 18:59:20 +01:00
|
|
|
for (x = 0; x < gogd->res_x; x++) {
|
|
|
|
const int i = y * gogd->res_x + x;
|
|
|
|
MLoopUV *luv = &gogd->mloopuvs[i * 4];
|
|
|
|
|
|
|
|
luv->uv[0] = x * gogd->ix;
|
|
|
|
luv->uv[1] = y * gogd->iy;
|
|
|
|
luv++;
|
|
|
|
|
|
|
|
luv->uv[0] = (x + 1) * gogd->ix;
|
|
|
|
luv->uv[1] = y * gogd->iy;
|
|
|
|
luv++;
|
|
|
|
|
|
|
|
luv->uv[0] = (x + 1) * gogd->ix;
|
|
|
|
luv->uv[1] = (y + 1) * gogd->iy;
|
|
|
|
luv++;
|
|
|
|
|
|
|
|
luv->uv[0] = x * gogd->ix;
|
|
|
|
luv->uv[1] = (y + 1) * gogd->iy;
|
|
|
|
luv++;
|
|
|
|
}
|
|
|
|
}
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 18:59:20 +01:00
|
|
|
static DerivedMesh *generate_ocean_geometry(OceanModifierData *omd)
|
|
|
|
{
|
|
|
|
DerivedMesh *result;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 18:59:20 +01:00
|
|
|
GenerateOceanGeometryData gogd;
|
|
|
|
|
|
|
|
int num_verts;
|
2015-12-06 21:03:57 +01:00
|
|
|
int num_polys;
|
2015-12-06 18:59:20 +01:00
|
|
|
|
2016-01-16 15:59:37 +01:00
|
|
|
const bool use_threading = omd->resolution > 4;
|
|
|
|
|
2015-12-06 18:59:20 +01:00
|
|
|
gogd.rx = omd->resolution * omd->resolution;
|
|
|
|
gogd.ry = omd->resolution * omd->resolution;
|
|
|
|
gogd.res_x = gogd.rx * omd->repeat_x;
|
|
|
|
gogd.res_y = gogd.ry * omd->repeat_y;
|
|
|
|
|
|
|
|
num_verts = (gogd.res_x + 1) * (gogd.res_y + 1);
|
2015-12-06 21:03:57 +01:00
|
|
|
num_polys = gogd.res_x * gogd.res_y;
|
2015-12-06 18:59:20 +01:00
|
|
|
|
|
|
|
gogd.sx = omd->size * omd->spatial_size;
|
|
|
|
gogd.sy = omd->size * omd->spatial_size;
|
|
|
|
gogd.ox = -gogd.sx / 2.0f;
|
|
|
|
gogd.oy = -gogd.sy / 2.0f;
|
|
|
|
|
|
|
|
gogd.sx /= gogd.rx;
|
|
|
|
gogd.sy /= gogd.ry;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 21:03:57 +01:00
|
|
|
result = CDDM_new(num_verts, 0, 0, num_polys * 4, num_polys);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 18:59:20 +01:00
|
|
|
gogd.mverts = CDDM_get_verts(result);
|
|
|
|
gogd.mpolys = CDDM_get_polys(result);
|
|
|
|
gogd.mloops = CDDM_get_loops(result);
|
2011-11-22 19:43:09 +00:00
|
|
|
|
2015-12-06 18:59:20 +01:00
|
|
|
gogd.origindex = CustomData_get_layer(&result->polyData, CD_ORIGINDEX);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2018-01-08 11:35:48 +01:00
|
|
|
ParallelRangeSettings settings;
|
|
|
|
BLI_parallel_range_settings_defaults(&settings);
|
|
|
|
settings.use_threading = use_threading;
|
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
/* create vertices */
|
2018-01-08 11:35:48 +01:00
|
|
|
BLI_task_parallel_range(0, gogd.res_y + 1, &gogd, generate_ocean_geometry_vertices, &settings);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
/* create faces */
|
2018-01-08 11:35:48 +01:00
|
|
|
BLI_task_parallel_range(0, gogd.res_y, &gogd, generate_ocean_geometry_polygons, &settings);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2012-01-06 02:59:28 +00:00
|
|
|
CDDM_calc_edges(result);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
/* add uvs */
|
2015-12-06 18:59:20 +01:00
|
|
|
if (CustomData_number_of_layers(&result->loopData, CD_MLOOPUV) < MAX_MTFACE) {
|
2015-12-06 21:03:57 +01:00
|
|
|
gogd.mloopuvs = CustomData_add_layer(&result->loopData, CD_MLOOPUV, CD_CALLOC, NULL, num_polys * 4);
|
2011-11-22 18:51:01 +00:00
|
|
|
|
2015-12-06 18:59:20 +01:00
|
|
|
if (gogd.mloopuvs) { /* unlikely to fail */
|
|
|
|
gogd.ix = 1.0 / gogd.rx;
|
|
|
|
gogd.iy = 1.0 / gogd.ry;
|
2011-11-22 19:43:09 +00:00
|
|
|
|
2018-01-08 11:35:48 +01:00
|
|
|
BLI_task_parallel_range(0, gogd.res_y, &gogd, generate_ocean_geometry_uvs, &settings);
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-30 17:36:43 +00:00
|
|
|
result->dirty |= DM_DIRTY_NORMALS;
|
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-12 08:04:56 +02:00
|
|
|
static DerivedMesh *doOcean(
|
2018-06-22 15:03:42 +02:00
|
|
|
ModifierData *md, Scene *scene, Object *ob,
|
2018-05-12 08:04:56 +02:00
|
|
|
DerivedMesh *derivedData,
|
|
|
|
int UNUSED(useRenderParams))
|
2011-11-13 12:17:27 +00:00
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
OceanModifierData *omd = (OceanModifierData *) md;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2012-05-06 13:38:33 +00:00
|
|
|
DerivedMesh *dm = NULL;
|
2011-11-13 12:17:27 +00:00
|
|
|
OceanResult ocr;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 21:03:57 +01:00
|
|
|
MVert *mverts;
|
2011-11-13 12:17:27 +00:00
|
|
|
|
|
|
|
int cfra;
|
2015-12-06 21:03:57 +01:00
|
|
|
int i, j;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-22 18:03:33 +00:00
|
|
|
/* use cached & inverted value for speed
|
|
|
|
* expanded this would read...
|
|
|
|
*
|
|
|
|
* (axis / (omd->size * omd->spatial_size)) + 0.5f) */
|
|
|
|
#define OCEAN_CO(_size_co_inv, _v) ((_v * _size_co_inv) + 0.5f)
|
|
|
|
|
2012-05-06 13:38:33 +00:00
|
|
|
const float size_co_inv = 1.0f / (omd->size * omd->spatial_size);
|
2011-11-22 18:03:33 +00:00
|
|
|
|
2013-07-22 21:02:45 +00:00
|
|
|
/* can happen in when size is small, avoid bad array lookups later and quit now */
|
2016-05-16 00:48:02 +02:00
|
|
|
if (!isfinite(size_co_inv)) {
|
2013-07-22 21:02:45 +00:00
|
|
|
return derivedData;
|
|
|
|
}
|
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
/* update modifier */
|
2015-12-06 21:03:57 +01:00
|
|
|
if (omd->refresh & MOD_OCEAN_REFRESH_RESET) {
|
2011-11-13 12:17:27 +00:00
|
|
|
init_ocean_modifier(omd);
|
2015-12-06 21:03:57 +01:00
|
|
|
}
|
|
|
|
if (omd->refresh & MOD_OCEAN_REFRESH_CLEAR_CACHE) {
|
2011-11-13 12:17:27 +00:00
|
|
|
clear_cache_data(omd);
|
2015-12-06 21:03:57 +01:00
|
|
|
}
|
2011-11-13 12:17:27 +00:00
|
|
|
omd->refresh = 0;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
/* do ocean simulation */
|
2014-04-01 11:34:00 +11:00
|
|
|
if (omd->cached == true) {
|
2015-12-06 21:03:57 +01:00
|
|
|
if (!omd->oceancache) {
|
2018-06-09 15:16:44 +02:00
|
|
|
init_cache_data(ob, omd);
|
2015-12-06 21:03:57 +01:00
|
|
|
}
|
2018-06-22 15:03:42 +02:00
|
|
|
BKE_ocean_simulate_cache(omd->oceancache, scene->r.cfra);
|
2011-11-22 15:10:08 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-11-13 12:17:27 +00:00
|
|
|
simulate_ocean_modifier(omd);
|
|
|
|
}
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2013-06-05 19:22:12 +00:00
|
|
|
if (omd->geometry_mode == MOD_OCEAN_GEOM_GENERATE) {
|
2011-11-13 12:17:27 +00:00
|
|
|
dm = generate_ocean_geometry(omd);
|
2013-06-05 19:22:12 +00:00
|
|
|
DM_ensure_normals(dm);
|
|
|
|
}
|
2011-11-13 12:17:27 +00:00
|
|
|
else if (omd->geometry_mode == MOD_OCEAN_GEOM_DISPLACE) {
|
2012-01-29 21:59:47 +00:00
|
|
|
dm = CDDM_copy(derivedData);
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2018-06-22 15:03:42 +02:00
|
|
|
cfra = scene->r.cfra;
|
2011-11-13 12:17:27 +00:00
|
|
|
CLAMP(cfra, omd->bakestart, omd->bakeend);
|
2013-01-02 16:03:58 +00:00
|
|
|
cfra -= omd->bakestart; /* shift to 0 based */
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-22 18:03:33 +00:00
|
|
|
mverts = dm->getVertArray(dm);
|
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
/* add vcols before displacement - allows lookup based on position */
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
if (omd->flag & MOD_OCEAN_GENERATE_FOAM) {
|
2015-12-06 21:03:57 +01:00
|
|
|
if (CustomData_number_of_layers(&dm->loopData, CD_MLOOPCOL) < MAX_MCOL) {
|
|
|
|
const int num_polys = dm->getNumPolys(dm);
|
2015-12-06 21:06:11 +01:00
|
|
|
const int num_loops = dm->getNumLoops(dm);
|
2015-12-06 21:03:57 +01:00
|
|
|
MLoop *mloops = dm->getLoopArray(dm);
|
|
|
|
MLoopCol *mloopcols = CustomData_add_layer_named(
|
2015-12-06 21:06:11 +01:00
|
|
|
&dm->loopData, CD_MLOOPCOL, CD_CALLOC, NULL, num_loops, omd->foamlayername);
|
2011-11-22 19:01:52 +00:00
|
|
|
|
2011-11-22 19:43:09 +00:00
|
|
|
if (mloopcols) { /* unlikely to fail */
|
2012-05-06 13:38:33 +00:00
|
|
|
MPoly *mpolys = dm->getPolyArray(dm);
|
2011-11-22 19:43:09 +00:00
|
|
|
MPoly *mp;
|
2011-11-22 19:01:52 +00:00
|
|
|
|
2015-12-06 21:03:57 +01:00
|
|
|
for (i = 0, mp = mpolys; i < num_polys; i++, mp++) {
|
|
|
|
MLoop *ml = &mloops[mp->loopstart];
|
|
|
|
MLoopCol *mlcol = &mloopcols[mp->loopstart];
|
2011-11-22 19:43:09 +00:00
|
|
|
|
2015-12-06 21:03:57 +01:00
|
|
|
for (j = mp->totloop; j--; ml++, mlcol++) {
|
|
|
|
const float *vco = mverts[ml->v].co;
|
|
|
|
const float u = OCEAN_CO(size_co_inv, vco[0]);
|
|
|
|
const float v = OCEAN_CO(size_co_inv, vco[1]);
|
|
|
|
float foam;
|
2011-11-22 19:01:52 +00:00
|
|
|
|
2014-04-01 11:34:00 +11:00
|
|
|
if (omd->oceancache && omd->cached == true) {
|
2011-11-22 19:01:52 +00:00
|
|
|
BKE_ocean_cache_eval_uv(omd->oceancache, &ocr, cfra, u, v);
|
|
|
|
foam = ocr.foam;
|
|
|
|
CLAMP(foam, 0.0f, 1.0f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BKE_ocean_eval_uv(omd->ocean, &ocr, u, v);
|
|
|
|
foam = BKE_ocean_jminus_to_foam(ocr.Jminus, omd->foam_coverage);
|
|
|
|
}
|
|
|
|
|
2011-11-22 19:43:09 +00:00
|
|
|
mlcol->r = mlcol->g = mlcol->b = (char)(foam * 255);
|
2013-02-12 01:52:55 +00:00
|
|
|
/* This needs to be set (render engine uses) */
|
|
|
|
mlcol->a = 255;
|
2015-12-06 18:59:20 +01:00
|
|
|
}
|
2011-11-22 19:01:52 +00:00
|
|
|
}
|
2011-11-22 18:03:33 +00:00
|
|
|
}
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-13 16:10:01 +00:00
|
|
|
|
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
/* displace the geometry */
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 21:03:57 +01:00
|
|
|
/* Note: tried to parallelized that one and previous foam loop, but gives 20% slower results... odd. */
|
|
|
|
{
|
|
|
|
const int num_verts = dm->getNumVerts(dm);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 21:03:57 +01:00
|
|
|
for (i = 0; i < num_verts; i++) {
|
|
|
|
float *vco = mverts[i].co;
|
|
|
|
const float u = OCEAN_CO(size_co_inv, vco[0]);
|
|
|
|
const float v = OCEAN_CO(size_co_inv, vco[1]);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 21:03:57 +01:00
|
|
|
if (omd->oceancache && omd->cached == true) {
|
|
|
|
BKE_ocean_cache_eval_uv(omd->oceancache, &ocr, cfra, u, v);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BKE_ocean_eval_uv(omd->ocean, &ocr, u, v);
|
|
|
|
}
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2015-12-06 21:03:57 +01:00
|
|
|
vco[2] += ocr.disp[1];
|
|
|
|
|
|
|
|
if (omd->chop_amount > 0.0f) {
|
|
|
|
vco[0] += ocr.disp[0];
|
|
|
|
vco[1] += ocr.disp[2];
|
|
|
|
}
|
2011-11-13 12:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-22 21:02:45 +00:00
|
|
|
#undef OCEAN_CO
|
2011-11-13 12:17:27 +00:00
|
|
|
|
|
|
|
return dm;
|
|
|
|
}
|
2013-01-02 16:03:58 +00:00
|
|
|
#else /* WITH_OCEANSIM */
|
2018-05-12 08:04:56 +02:00
|
|
|
static DerivedMesh *doOcean(
|
2018-06-22 15:21:12 +02:00
|
|
|
ModifierData *UNUSED(md), Scene *UNUSED(scene), Object *UNUSED(ob),
|
2018-05-12 08:04:56 +02:00
|
|
|
DerivedMesh *derivedData,
|
|
|
|
int UNUSED(useRenderParams))
|
2011-11-13 12:17:27 +00:00
|
|
|
{
|
|
|
|
/* unused */
|
|
|
|
return derivedData;
|
|
|
|
}
|
2013-01-02 16:03:58 +00:00
|
|
|
#endif /* WITH_OCEANSIM */
|
2011-11-13 12:17:27 +00:00
|
|
|
|
2018-05-12 08:04:56 +02:00
|
|
|
static DerivedMesh *applyModifier(
|
2018-05-12 08:21:07 +02:00
|
|
|
ModifierData *md, const ModifierEvalContext *ctx,
|
|
|
|
DerivedMesh *derivedData)
|
2011-11-13 12:17:27 +00:00
|
|
|
{
|
|
|
|
DerivedMesh *result;
|
2018-06-22 15:03:42 +02:00
|
|
|
Scene *scene = DEG_get_evaluated_scene(ctx->depsgraph);
|
|
|
|
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2018-06-22 15:03:42 +02:00
|
|
|
result = doOcean(md, scene, ctx->object, derivedData, 0);
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2012-03-24 06:24:53 +00:00
|
|
|
if (result != derivedData)
|
2013-05-30 17:36:43 +00:00
|
|
|
result->dirty |= DM_DIRTY_NORMALS;
|
2011-11-13 16:10:01 +00:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ModifierTypeInfo modifierType_Ocean = {
|
|
|
|
/* name */ "Ocean",
|
|
|
|
/* structName */ "OceanModifierData",
|
|
|
|
/* structSize */ sizeof(OceanModifierData),
|
|
|
|
/* type */ eModifierTypeType_Constructive,
|
2012-05-06 13:38:33 +00:00
|
|
|
/* flags */ eModifierTypeFlag_AcceptsMesh |
|
|
|
|
eModifierTypeFlag_SupportsEditmode |
|
|
|
|
eModifierTypeFlag_EnableInEditmode,
|
2011-11-13 12:17:27 +00:00
|
|
|
|
|
|
|
/* copyData */ copyData,
|
2018-04-18 15:45:54 +02:00
|
|
|
/* deformMatrices_DM */ NULL,
|
|
|
|
|
|
|
|
/* deformVerts_DM */ NULL,
|
|
|
|
/* deformVertsEM_DM */ NULL,
|
|
|
|
/* deformMatricesEM_DM*/NULL,
|
|
|
|
/* applyModifier_DM */ applyModifier,
|
|
|
|
/* applyModifierEM_DM */NULL,
|
|
|
|
|
2011-11-14 16:05:44 +00:00
|
|
|
/* deformVerts */ NULL,
|
2018-04-18 15:45:54 +02:00
|
|
|
/* deformMatrices */ NULL,
|
2011-11-14 16:05:44 +00:00
|
|
|
/* deformVertsEM */ NULL,
|
|
|
|
/* deformMatricesEM */ NULL,
|
2018-04-18 15:45:54 +02:00
|
|
|
/* applyModifier */ NULL,
|
2013-05-02 14:42:05 +00:00
|
|
|
/* applyModifierEM */ NULL,
|
2018-04-18 15:45:54 +02:00
|
|
|
|
2011-11-13 12:17:27 +00:00
|
|
|
/* initData */ initData,
|
|
|
|
/* requiredDataMask */ requiredDataMask,
|
|
|
|
/* freeData */ freeData,
|
2011-11-14 16:05:44 +00:00
|
|
|
/* isDisabled */ NULL,
|
Depsgraph: New dependency graph integration commit
This commit integrates the work done so far on the new dependency graph system,
where goal was to replace legacy depsgraph with the new one, supporting loads of
neat features like:
- More granular dependency relation nature, which solves issues with fake cycles
in the dependencies.
- Move towards all-animatable, by better integration of drivers into the system.
- Lay down some basis for upcoming copy-on-write, overrides and so on.
The new system is living side-by-side with the previous one and disabled by
default, so nothing will become suddenly broken. The way to enable new depsgraph
is to pass `--new-depsgraph` command line argument.
It's a bit early to consider the system production-ready, there are some TODOs
and issues were discovered during the merge period, they'll be addressed ASAP.
But it's important to merge, because it's the only way to attract artists to
really start testing this system.
There are number of assorted documents related on the design of the new system:
* http://wiki.blender.org/index.php/User:Aligorith/GSoC2013_Depsgraph#Design_Documents
* http://wiki.blender.org/index.php/User:Nazg-gul/DependencyGraph
There are also some user-related information online:
* http://code.blender.org/2015/02/blender-dependency-graph-branch-for-users/
* http://code.blender.org/2015/03/more-dependency-graph-tricks/
Kudos to everyone who was involved into the project:
- Joshua "Aligorith" Leung -- design specification, initial code
- Lukas "lukas_t" Toenne -- integrating code into blender, with further fixes
- Sergey "Sergey" "Sharybin" -- some mocking around, trying to wrap up the
project and so
- Bassam "slikdigit" Kurdali -- stressing the new system, reporting all the
issues and recording/writing documentation.
- Everyone else who i forgot to mention here :)
2015-05-12 15:05:57 +05:00
|
|
|
/* updateDepsgraph */ NULL,
|
2011-11-14 16:05:44 +00:00
|
|
|
/* dependsOnTime */ NULL,
|
2013-06-05 19:22:12 +00:00
|
|
|
/* dependsOnNormals */ dependsOnNormals,
|
2011-11-14 16:05:44 +00:00
|
|
|
/* foreachObjectLink */ NULL,
|
|
|
|
/* foreachIDLink */ NULL,
|
2015-10-05 15:57:10 +02:00
|
|
|
/* foreachTexLink */ NULL,
|
2011-11-13 12:17:27 +00:00
|
|
|
};
|