[Bf-blender-cvs] [56173e512ca] blender2.8: Merge branch 'master' into blender2.8
Campbell Barton
noreply at git.blender.org
Sun Jun 17 16:25:45 CEST 2018
Commit: 56173e512cab020a5bc96e6d3069641a03d47fbe
Author: Campbell Barton
Date: Sun Jun 17 16:18:15 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB56173e512cab020a5bc96e6d3069641a03d47fbe
Merge branch 'master' into blender2.8
===================================================================
===================================================================
diff --cc source/blender/blenkernel/intern/studiolight.c
index 297993ae3e4,00000000000..b116e99e63b
mode 100644,000000..100644
--- a/source/blender/blenkernel/intern/studiolight.c
+++ b/source/blender/blenkernel/intern/studiolight.c
@@@ -1,925 -1,0 +1,925 @@@
+/*
+ * ***** 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) 2006-2007 Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ *
+ */
+
+/** \file blender/blenkernel/intern/studiolight.c
+ * \ingroup bke
+ */
+
+#include "BKE_studiolight.h"
+
+#include "BKE_appdir.h"
+#include "BKE_icons.h"
+
+#include "BLI_fileops.h"
+#include "BLI_fileops_types.h"
+#include "BLI_listbase.h"
+#include "BLI_math.h"
+#include "BLI_path_util.h"
+#include "BLI_rand.h"
+#include "BLI_string.h"
+#include "BLI_string_utils.h"
+
+#include "DNA_listBase.h"
+
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+
+#include "GPU_texture.h"
+
+#include "MEM_guardedalloc.h"
+
+
+/* Statics */
+static ListBase studiolights;
+#define STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE 16
+#define STUDIOLIGHT_IRRADIANCE_EQUIRECTANGULAR_HEIGHT 64
+#define STUDIOLIGHT_IRRADIANCE_EQUIRECTANGULAR_WIDTH (STUDIOLIGHT_IRRADIANCE_EQUIRECTANGULAR_HEIGHT * 2)
+
+static const char *STUDIOLIGHT_CAMERA_FOLDER = "studiolights/camera/";
+static const char *STUDIOLIGHT_WORLD_FOLDER = "studiolights/world/";
+static const char *STUDIOLIGHT_MATCAP_FOLDER = "studiolights/matcap/";
+
+/* FUNCTIONS */
+static void studiolight_free(struct StudioLight *sl)
+{
+ for (int index = 0 ; index < 6 ; index ++) {
+ if (sl->radiance_cubemap_buffers[index] != NULL) {
+ IMB_freeImBuf(sl->radiance_cubemap_buffers[index]);
+ sl->radiance_cubemap_buffers[index] = NULL;
+ }
+ }
+ if (sl->equirectangular_radiance_gputexture) {
+ GPU_texture_free(sl->equirectangular_radiance_gputexture);
+ sl->equirectangular_radiance_gputexture = NULL;
+ }
+
+ if (sl->equirectangular_irradiance_gputexture) {
+ GPU_texture_free(sl->equirectangular_irradiance_gputexture);
+ sl->equirectangular_irradiance_gputexture = NULL;
+ }
+
+ if (sl->equirectangular_radiance_buffer) {
+ IMB_freeImBuf(sl->equirectangular_radiance_buffer);
+ sl->equirectangular_radiance_buffer = NULL;
+ }
+
+ if (sl->equirectangular_irradiance_buffer) {
+ IMB_freeImBuf(sl->equirectangular_irradiance_buffer);
+ sl->equirectangular_irradiance_buffer = NULL;
+ }
+ if (sl->path_irr) {
+ MEM_freeN(sl->path_irr);
+ sl->path_irr = NULL;
+ }
+ if (sl->gpu_matcap_3components) {
+ MEM_freeN(sl->gpu_matcap_3components);
+ sl->gpu_matcap_3components = NULL;
+ }
+ MEM_freeN(sl);
+}
+
+static struct StudioLight *studiolight_create(int flag)
+{
+ struct StudioLight *sl = MEM_callocN(sizeof(*sl), __func__);
+ sl->path[0] = 0x00;
+ sl->name[0] = 0x00;
+ sl->path_irr = NULL;
+ sl->flag = flag;
+ sl->index = BLI_listbase_count(&studiolights);
+ if (flag & STUDIOLIGHT_ORIENTATION_VIEWNORMAL) {
+ sl->icon_id_matcap = BKE_icon_ensure_studio_light(sl, STUDIOLIGHT_ICON_ID_TYPE_MATCAP);
+ sl->icon_id_matcap_flipped = BKE_icon_ensure_studio_light(sl, STUDIOLIGHT_ICON_ID_TYPE_MATCAP_FLIPPED);
+ }
+ else {
+ sl->icon_id_radiance = BKE_icon_ensure_studio_light(sl, STUDIOLIGHT_ICON_ID_TYPE_RADIANCE);
+ sl->icon_id_irradiance = BKE_icon_ensure_studio_light(sl, STUDIOLIGHT_ICON_ID_TYPE_IRRADIANCE);
+ }
+
+ for (int index = 0 ; index < 6 ; index ++) {
+ sl->radiance_cubemap_buffers[index] = NULL;
+ }
+ return sl;
+}
+
+static void direction_to_equirectangular(float r[2], const float dir[3])
+{
+ r[0] = (atan2f(dir[1], dir[0]) - M_PI) / -(M_PI * 2);
+ r[1] = (acosf(dir[2] / 1.0) - M_PI) / -M_PI;
+}
+
+static void equirectangular_to_direction(float r[3], float u, float v)
+{
+ float phi = (-(M_PI * 2)) * u + M_PI;
+ float theta = -M_PI * v + M_PI;
+ float sin_theta = sinf(theta);
+ r[0] = sin_theta * cosf(phi);
+ r[1] = sin_theta * sinf(phi);
+ r[2] = cosf(theta);
+}
+
+static void studiolight_calculate_radiance(ImBuf *ibuf, float color[4], const float direction[3])
+{
+ float uv[2];
+ direction_to_equirectangular(uv, direction);
+ nearest_interpolation_color_wrap(ibuf, NULL, color, uv[0] * ibuf->x, uv[1] * ibuf->y);
+}
+
+static void studiolight_calculate_radiance_buffer(
+ ImBuf *ibuf, float *colbuf,
+ const float start_x, const float add_x,
+ const float start_y, const float add_y, const float z,
+ const int index_x, const int index_y, const int index_z)
+{
+ float direction[3];
+ float yf = start_y;
+ float xf;
+ float *color = colbuf;
+
+ for (int y = 0; y < STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE; y ++, yf += add_y) {
+ xf = start_x;
+ for (int x = 0; x < STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE; x ++, xf += add_x) {
+ direction[index_x] = xf;
+ direction[index_y] = yf;
+ direction[index_z] = z;
+ normalize_v3(direction);
+ studiolight_calculate_radiance(ibuf, color, direction);
+ color += 4;
+ }
+ }
+}
+
+static void studiolight_load_equirectangular_image(StudioLight *sl)
+{
+ if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
+ ImBuf *ibuf = NULL;
+ ibuf = IMB_loadiffname(sl->path, 0, NULL);
+ if (ibuf) {
+ IMB_float_from_rect(ibuf);
+ sl->equirectangular_radiance_buffer = ibuf;
+ }
+ }
+ sl->flag |= STUDIOLIGHT_EXTERNAL_IMAGE_LOADED;
+}
+
+static void studiolight_create_equirectangular_radiance_gputexture(StudioLight *sl)
+{
+ if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
+ char error[256];
+ BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_EXTERNAL_IMAGE_LOADED);
+ ImBuf *ibuf = sl->equirectangular_radiance_buffer;
+
+ if (sl->flag & STUDIOLIGHT_ORIENTATION_VIEWNORMAL) {
+ sl->gpu_matcap_3components = MEM_callocN(sizeof(float[3]) * ibuf->x * ibuf->y, __func__);
+
+ float *offset4 = ibuf->rect_float;
+ float *offset3 = sl->gpu_matcap_3components;
+ for (int i = 0 ; i < ibuf->x * ibuf->y; i++) {
+ copy_v3_v3(offset3, offset4);
+ offset3 += 3;
+ offset4 += 4;
+ }
+ sl->equirectangular_radiance_gputexture = GPU_texture_create_2D(
+ ibuf->x, ibuf->y, GPU_R11F_G11F_B10F, sl->gpu_matcap_3components, error);
+ }
+ else {
+ sl->equirectangular_radiance_gputexture = GPU_texture_create_2D(
+ ibuf->x, ibuf->y, GPU_RGBA16F, ibuf->rect_float, error);
+ GPUTexture *tex = sl->equirectangular_radiance_gputexture;
+ GPU_texture_bind(tex, 0);
+ GPU_texture_filter_mode(tex, true);
+ GPU_texture_wrap_mode(tex, true);
+ GPU_texture_unbind(tex);
+ }
+ }
+ sl->flag |= STUDIOLIGHT_EQUIRECTANGULAR_RADIANCE_GPUTEXTURE;
+}
+
+static void studiolight_create_equirectangular_irradiance_gputexture(StudioLight *sl)
+{
+ if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
+ char error[256];
+ BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_EQUIRECTANGULAR_IRRADIANCE_IMAGE_CALCULATED);
+ ImBuf *ibuf = sl->equirectangular_irradiance_buffer;
+ sl->equirectangular_irradiance_gputexture = GPU_texture_create_2D(
+ ibuf->x, ibuf->y, GPU_RGBA16F, ibuf->rect_float, error);
+ GPUTexture *tex = sl->equirectangular_irradiance_gputexture;
+ GPU_texture_bind(tex, 0);
+ GPU_texture_filter_mode(tex, true);
+ GPU_texture_wrap_mode(tex, true);
+ GPU_texture_unbind(tex);
+ }
+ sl->flag |= STUDIOLIGHT_EQUIRECTANGULAR_IRRADIANCE_GPUTEXTURE;
+}
+
+static void studiolight_calculate_radiance_cubemap_buffers(StudioLight *sl)
+{
+ if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
+ BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_EXTERNAL_IMAGE_LOADED);
+ ImBuf *ibuf = sl->equirectangular_radiance_buffer;
+ if (ibuf) {
+ float *colbuf = MEM_mallocN(SQUARE(STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE) * sizeof(float[4]), __func__);
+ const float add = 1.0f / (STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE + 1);
+ const float start = ((1.0f / STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE) * 0.5f) - 0.5f;
+
+ /* front */
+ studiolight_calculate_radiance_buffer(ibuf, colbuf, start, add, start, add, 0.5f, 0, 2, 1);
+ sl->radiance_cubemap_buffers[STUDIOLIGHT_Y_POS] = IMB_allocFromBuffer(
+ NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
+
+ /* back */
+ studiolight_calculate_radiance_buffer(ibuf, colbuf, -start, -add, start, add, -0.5f, 0, 2, 1);
+ sl->radiance_cubemap_buffers[STUDIOLIGHT_Y_NEG] = IMB_allocFromBuffer(
+ NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
+
+ /* left */
+ studiolight_calculate_radiance_buffer(ibuf, colbuf, -start, -add, start, add, 0.5f, 1, 2, 0);
+ sl->radiance_cubemap_buffers[STUDIOLIGHT_X_POS] = IMB_allocFromBuffer(
+ NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
+
+ /* right */
+ studiolight_calculate_radiance_buffer(ibuf, colbuf, start, add, start, add, -0.5f, 1, 2, 0);
+ sl->radiance_cubemap_buffers[STUDIOLIGHT_X_NEG] = IMB_allocFromBuffer(
+ NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
+
+ /* top */
+ studiolight_calculate_radiance_buffer(ibuf, colbuf, start, add, start, add, -0.5f, 0, 1, 2);
+ sl->radiance_cubemap_buffers[STUDIOLIGHT_Z_NEG] = IMB_allocFromBuffer(
+ NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE)
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list