This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/blenkernel/intern/world.c
Bastien Montagne 865796375b Cleanup: avoid incrementing/decrementing id->us outside of BKE_library.
We have callbacks for that, they also do some checks and help ensure things are done
correctly. Only place where this is assumed not true is blenloader (since here we
may affect refcount of library IDs as well...).
2015-11-09 21:00:53 +01:00

232 lines
5.0 KiB
C

/*
* ***** 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) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/blenkernel/intern/world.c
* \ingroup bke
*/
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "MEM_guardedalloc.h"
#include "DNA_world_types.h"
#include "DNA_scene_types.h"
#include "DNA_texture_types.h"
#include "BLI_utildefines.h"
#include "BLI_listbase.h"
#include "BKE_animsys.h"
#include "BKE_global.h"
#include "BKE_icons.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_node.h"
#include "BKE_world.h"
#include "GPU_material.h"
void BKE_world_free_ex(World *wrld, bool do_id_user)
{
MTex *mtex;
int a;
for (a = 0; a < MAX_MTEX; a++) {
mtex = wrld->mtex[a];
if (do_id_user && mtex && mtex->tex)
id_us_min(&mtex->tex->id);
if (mtex)
MEM_freeN(mtex);
}
BKE_previewimg_free(&wrld->preview);
BKE_animdata_free((ID *)wrld);
/* is no lib link block, but world extension */
if (wrld->nodetree) {
ntreeFreeTree_ex(wrld->nodetree, do_id_user);
MEM_freeN(wrld->nodetree);
}
if (wrld->gpumaterial.first)
GPU_material_free(&wrld->gpumaterial);
BKE_icon_id_delete((struct ID *)wrld);
wrld->id.icon_id = 0;
}
void BKE_world_free(World *wrld)
{
BKE_world_free_ex(wrld, true);
}
void BKE_world_init(World *wrld)
{
BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(wrld, id));
wrld->horr = 0.05f;
wrld->horg = 0.05f;
wrld->horb = 0.05f;
wrld->zenr = 0.01f;
wrld->zeng = 0.01f;
wrld->zenb = 0.01f;
wrld->skytype = 0;
wrld->exp = 0.0f;
wrld->exposure = wrld->range = 1.0f;
wrld->aodist = 10.0f;
wrld->aosamp = 5;
wrld->aoenergy = 1.0f;
wrld->ao_env_energy = 1.0f;
wrld->ao_indirect_energy = 1.0f;
wrld->ao_indirect_bounces = 1;
wrld->aobias = 0.05f;
wrld->ao_samp_method = WO_AOSAMP_HAMMERSLEY;
wrld->ao_approx_error = 0.25f;
wrld->preview = NULL;
wrld->miststa = 5.0f;
wrld->mistdist = 25.0f;
}
World *add_world(Main *bmain, const char *name)
{
World *wrld;
wrld = BKE_libblock_alloc(bmain, ID_WO, name);
BKE_world_init(wrld);
return wrld;
}
World *BKE_world_copy(World *wrld)
{
World *wrldn;
int a;
wrldn = BKE_libblock_copy(&wrld->id);
for (a = 0; a < MAX_MTEX; a++) {
if (wrld->mtex[a]) {
wrldn->mtex[a] = MEM_mallocN(sizeof(MTex), "BKE_world_copy");
memcpy(wrldn->mtex[a], wrld->mtex[a], sizeof(MTex));
id_us_plus((ID *)wrldn->mtex[a]->tex);
}
}
if (wrld->nodetree) {
wrldn->nodetree = ntreeCopyTree(wrld->nodetree);
}
if (wrld->preview)
wrldn->preview = BKE_previewimg_copy(wrld->preview);
BLI_listbase_clear(&wrldn->gpumaterial);
if (wrld->id.lib) {
BKE_id_lib_local_paths(G.main, wrld->id.lib, &wrldn->id);
}
return wrldn;
}
World *localize_world(World *wrld)
{
World *wrldn;
int a;
wrldn = BKE_libblock_copy_nolib(&wrld->id, false);
for (a = 0; a < MAX_MTEX; a++) {
if (wrld->mtex[a]) {
wrldn->mtex[a] = MEM_mallocN(sizeof(MTex), "localize_world");
memcpy(wrldn->mtex[a], wrld->mtex[a], sizeof(MTex));
/* free world decrements */
id_us_plus((ID *)wrldn->mtex[a]->tex);
}
}
if (wrld->nodetree)
wrldn->nodetree = ntreeLocalize(wrld->nodetree);
wrldn->preview = NULL;
BLI_listbase_clear(&wrldn->gpumaterial);
return wrldn;
}
void BKE_world_make_local(World *wrld)
{
Main *bmain = G.main;
Scene *sce;
bool is_local = false, is_lib = false;
/* - only lib users: do nothing
* - only local users: set flag
* - mixed: make copy
*/
if (wrld->id.lib == NULL) return;
if (wrld->id.us == 1) {
id_clear_lib_data(bmain, &wrld->id);
return;
}
for (sce = bmain->scene.first; sce && ELEM(false, is_lib, is_local); sce = sce->id.next) {
if (sce->world == wrld) {
if (sce->id.lib) is_lib = true;
else is_local = true;
}
}
if (is_local && is_lib == false) {
id_clear_lib_data(bmain, &wrld->id);
}
else if (is_local && is_lib) {
World *wrld_new = BKE_world_copy(wrld);
wrld_new->id.us = 0;
/* Remap paths of new ID using old library as base. */
BKE_id_lib_local_paths(bmain, wrld->id.lib, &wrld_new->id);
for (sce = bmain->scene.first; sce; sce = sce->id.next) {
if (sce->world == wrld) {
if (sce->id.lib == NULL) {
sce->world = wrld_new;
id_us_plus(&wrld_new->id);
id_us_min(&wrld->id);
}
}
}
}
}