[Bf-blender-cvs] [ff19b527e85] blender2.8: Workbench: Calculate irradiance using radiance buffers

Jeroen Bakker noreply at git.blender.org
Tue May 22 15:05:58 CEST 2018


Commit: ff19b527e85ec5144efbd663bf9b4c338e179358
Author: Jeroen Bakker
Date:   Thu May 17 15:23:21 2018 +0200
Branches: blender2.8
https://developer.blender.org/rBff19b527e85ec5144efbd663bf9b4c338e179358

Workbench: Calculate irradiance using radiance buffers

===================================================================

M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/blenkernel/BKE_studiolight.h
M	source/blender/blenkernel/intern/studiolight.c
M	source/blender/draw/engines/workbench/workbench_materials.c
M	source/blender/makesrna/intern/rna_space.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index f20e9021e28..6bf10231be5 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -3515,12 +3515,11 @@ class VIEW3D_PT_shading(Panel):
 
             col.separator()
 
-            if not(shading.light == 'STUDIO' and shading.studio_light_orientation == 'WORLD'):
-                row = col.row()
-                row.prop(shading, "show_shadows")
-                sub = row.row()
-                sub.active = shading.show_shadows
-                sub.prop(shading, "shadow_intensity", text="")
+            row = col.row()
+            row.prop(shading, "show_shadows")
+            sub = row.row()
+            sub.active = shading.show_shadows
+            sub.prop(shading, "shadow_intensity", text="")
 
             col.prop(shading, "show_object_outline")
 
diff --git a/source/blender/blenkernel/BKE_studiolight.h b/source/blender/blenkernel/BKE_studiolight.h
index 863aab37f39..2fcf6ab2f19 100644
--- a/source/blender/blenkernel/BKE_studiolight.h
+++ b/source/blender/blenkernel/BKE_studiolight.h
@@ -38,6 +38,8 @@
 
 #include "DNA_space_types.h"
 
+#include "IMB_imbuf_types.h"
+
 /*
  * These defines are the indexes in the StudioLight.diffuse_light
  * X_POS means the light that is traveling towards the positive X
@@ -50,17 +52,16 @@
 #define STUDIOLIGHT_Z_POS 4
 #define STUDIOLIGHT_Z_NEG 5
 
-enum StudioLightFlag
-{
-	STUDIOLIGHT_DIFFUSE_LIGHT_CALCULATED   = (1 << 0),
-	STUDIOLIGHT_LIGHT_DIRECTION_CALCULATED = (1 << 1),
-	STUDIOLIGHT_EXTERNAL_FILE              = (1 << 2),
-	STUDIOLIGHT_ORIENTATION_CAMERA         = (1 << 3),
-	STUDIOLIGHT_ORIENTATION_WORLD          = (1 << 4),
+enum StudioLightFlag {
+	STUDIOLIGHT_DIFFUSE_LIGHT_CALCULATED      = (1 << 0),
+	STUDIOLIGHT_LIGHT_DIRECTION_CALCULATED    = (1 << 1),
+	STUDIOLIGHT_EXTERNAL_FILE                 = (1 << 2),
+	STUDIOLIGHT_ORIENTATION_CAMERA            = (1 << 3),
+	STUDIOLIGHT_ORIENTATION_WORLD             = (1 << 4),
+	STUDIOLIGHT_RADIANCE_BUFFERS_CALCULATED   = (1 << 5),
 } StudioLightFlag;
 
-typedef struct StudioLight
-{
+typedef struct StudioLight {
 	struct StudioLight *next, *prev;
 	int flag;
 	char name[FILE_MAXFILE];
@@ -69,6 +70,7 @@ typedef struct StudioLight
 	int index;
 	float diffuse_light[6][3];
 	float light_direction[3];
+	ImBuf *radiance_buffers[6];
 } StudioLight;
 
 void BKE_studiolight_init(void);
diff --git a/source/blender/blenkernel/intern/studiolight.c b/source/blender/blenkernel/intern/studiolight.c
index 7942cbaab71..5b84a686fa9 100644
--- a/source/blender/blenkernel/intern/studiolight.c
+++ b/source/blender/blenkernel/intern/studiolight.c
@@ -53,14 +53,21 @@
 
 /* Statics */
 static ListBase studiolights;
-#define STUDIO_LIGHT_EXTENSIONS ".jpg", ".hdr"
-#define STUDIO_LIGHT_DIFFUSE_SAMPLE_STEP 64
-static const char *STUDIO_LIGHT_CAMERA_FOLDER = "studiolights/camera/";
-static const char *STUDIO_LIGHT_WORLD_FOLDER = "studiolights/world/";
+#define STUDIOLIGHT_EXTENSIONS ".jpg", ".hdr"
+#define STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE 32
+
+static const char *STUDIOLIGHT_CAMERA_FOLDER = "studiolights/camera/";
+static const char *STUDIOLIGHT_WORLD_FOLDER = "studiolights/world/";
 
 /* FUNCTIONS */
 static void studiolight_free(struct StudioLight *sl)
 {
+	for (int index = 0 ; index < 6 ; index ++) {
+		if (sl->radiance_buffers[index] != NULL) {
+			MEM_freeN(sl->radiance_buffers[index]);
+			sl->radiance_buffers[index] = NULL;
+		}
+	}
 	MEM_freeN(sl);
 }
 
@@ -72,6 +79,10 @@ static struct StudioLight *studiolight_create(void)
 	sl->flag = 0;
 	sl->index = BLI_listbase_count(&studiolights);
 	sl->icon_id = BKE_icon_ensure_studio_light(sl);
+
+	for (int index = 0 ; index < 6 ; index ++) {
+		sl->radiance_buffers[index] = NULL;
+	}
 	return sl;
 }
 
@@ -91,44 +102,203 @@ static void equirectangular_to_direction(float r[3], float u, float v)
 	r[2] = cosf(theta);
 }
 
-static void studiolight_calculate_directional_diffuse_light(ImBuf *ibuf, float color[4], const float start[3], const float v1[3], const float v2[3])
+static void studiolight_calculate_radiance(ImBuf *ibuf, float color[4], const float direction[3])
 {
-	const int steps = STUDIO_LIGHT_DIFFUSE_SAMPLE_STEP;
 	float uv[2];
-	float dir[3];
-	float col[4];
-	float v11[3];
-	float v12[3];
-	float totcol[4];
-
-	zero_v4(totcol);
-	for (int x = 0; x < steps ; x++) {
-		float xf = (float)x / (float)steps;
-		mul_v3_v3fl(v11, v1, xf);
-		for (int y = 0; y < steps; y++) {
-			float yf = (float)y / (float)steps;
-			/* start + x/steps*v1 + y/steps*v2 */
-			mul_v3_v3fl(v12, v2, yf);
-			add_v3_v3v3(dir, start, v11);
-			add_v3_v3(dir, v12);
-			/* normalize */
-			normalize_v3(dir);
-
-			/* sample */
-			direction_to_equirectangular(uv, dir);
-			nearest_interpolation_color_wrap(ibuf, NULL, col, uv[0] * ibuf->x, uv[1] * ibuf->y);
-			add_v3_v3(totcol, col);
+	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;
 		}
 	}
-	mul_v3_v3fl(color, totcol, 1.0 / (steps * steps));
 }
 
-static void studiolight_calculate_diffuse_light(StudioLight *sl)
+static void studiolight_calculate_radiance_buffers(StudioLight *sl)
 {
-	float start[3];
-	float v1[3];
-	float v2[3];
+	if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
+		ImBuf* ibuf = NULL;
+		ibuf = IMB_loadiffname(sl->path, 0, NULL);
+		if (ibuf) {
+			IMB_float_from_rect(ibuf);
+			float *colbuf = MEM_mallocN(4*STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE*STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE*sizeof(float), __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_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_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_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_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_buffers[STUDIOLIGHT_Z_NEG] = IMB_allocFromBuffer(NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
+
+			/* bottom */
+			studiolight_calculate_radiance_buffer(ibuf, colbuf, start, add, -start, -add, 0.5f, 0, 1, 2);
+			sl->radiance_buffers[STUDIOLIGHT_Z_POS] = IMB_allocFromBuffer(NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
+
+#if 0
+			IMB_saveiff(sl->radiance_buffers[STUDIOLIGHT_X_POS], "/tmp/studiolight_radiance_left.png", IB_rectfloat);
+			IMB_saveiff(sl->radiance_buffers[STUDIOLIGHT_X_NEG], "/tmp/studiolight_radiance_right.png", IB_rectfloat);
+			IMB_saveiff(sl->radiance_buffers[STUDIOLIGHT_Y_POS], "/tmp/studiolight_radiance_front.png", IB_rectfloat);
+			IMB_saveiff(sl->radiance_buffers[STUDIOLIGHT_Y_NEG], "/tmp/studiolight_radiance_back.png", IB_rectfloat);
+			IMB_saveiff(sl->radiance_buffers[STUDIOLIGHT_Z_POS], "/tmp/studiolight_radiance_bottom.png", IB_rectfloat);
+			IMB_saveiff(sl->radiance_buffers[STUDIOLIGHT_Z_NEG], "/tmp/studiolight_radiance_top.png", IB_rectfloat);
+#endif
+			MEM_freeN(colbuf);
+			IMB_freeImBuf(ibuf);
+		}
+	}
+	sl->flag |= STUDIOLIGHT_RADIANCE_BUFFERS_CALCULATED;
+}
 
+static void studiolight_calculate_irradiance(StudioLight *sl, float color[3], const float normal[3])
+{
+	float direction[3];
+	ImBuf *radiance_buffer;
+	float *radiance_color;
+	float angle;
+
+	/* back */
+	radiance_buffer = sl->radiance_buffers[STUDIOLIGHT_Y_POS];
+	radiance_color = radiance_buffer->rect_float;
+	for (int y = 0; y < STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE; y ++) {
+		for (int x = 0; x < STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE; x ++) {
+			// calculate light direction;
+			direction[1] = 0.5;
+			direction[0] = (x / (float)STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE) - 0.5f;
+			direction[2] = (y / (float)STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE) - 0.5f;
+			normalize_v3(direction);
+			angle = dot_v3v3(direction, normal);
+			if (angle > 0.0f) {
+				madd_v3_v3fl(color, radiance_color, angle);
+			}
+			radiance_color += 4;
+		}
+	}
+
+	/* front */
+	radiance_buffer = sl->radiance_buffers[STUDIOLIGHT_Y_NEG];
+	radiance_color = radiance_buffer->rect_float;
+	for (int y = 0; y < STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE; y ++) {
+		for (int x = 0; x < STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE; x ++) {
+			// calculate light direction;
+			direction[1] = -0.5;
+			direction[0] = (x / (float)STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE) - 0.5f;
+			direction[2] = (y / (float)STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE) - 0.5f;
+			normalize_v3(direction);
+			angle = dot_v3v3(direction, normal);
+			if (angle > 0.0f

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list