[Bf-blender-cvs] [b6d35e1] master: Viewport smoke: add support to render the volume using a color ramp.

Kévin Dietrich noreply at git.blender.org
Sun Oct 30 12:29:16 CET 2016


Commit: b6d35e1fa74076e5072e53dad63dc712f85a7027
Author: Kévin Dietrich
Date:   Sun Oct 30 12:29:05 2016 +0100
Branches: master
https://developer.blender.org/rBb6d35e1fa74076e5072e53dad63dc712f85a7027

Viewport smoke: add support to render the volume using a color ramp.

This is yet another debug option that allows to render an arbitrary
simulation field by using a color ramp to inspect its voxel values.
Note that when using this, fire rendering is turned off.

Reviewers: plasmasolutions, gottfried

Differential Revision: https://developer.blender.org/D1733

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

M	release/scripts/startup/bl_ui/properties_physics_smoke.py
M	source/blender/blenkernel/intern/smoke.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/blenloader/intern/writefile.c
M	source/blender/editors/space_view3d/drawobject.c
M	source/blender/editors/space_view3d/drawvolume.c
M	source/blender/editors/space_view3d/view3d_intern.h
M	source/blender/gpu/GPU_shader.h
M	source/blender/gpu/intern/gpu_shader.c
M	source/blender/gpu/shaders/gpu_shader_smoke_frag.glsl
M	source/blender/makesdna/DNA_smoke_types.h
M	source/blender/makesrna/intern/rna_smoke.c

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

diff --git a/release/scripts/startup/bl_ui/properties_physics_smoke.py b/release/scripts/startup/bl_ui/properties_physics_smoke.py
index df03f23..0374d03 100644
--- a/release/scripts/startup/bl_ui/properties_physics_smoke.py
+++ b/release/scripts/startup/bl_ui/properties_physics_smoke.py
@@ -397,6 +397,14 @@ class PHYSICS_PT_smoke_display_settings(PhysicButtonsPanel, Panel):
         col.prop(domain, "vector_draw_type")
         col.prop(domain, "vector_scale")
 
+        layout.separator()
+        layout.label(text="Color Mapping:")
+        layout.prop(domain, "use_color_ramp")
+        col = layout.column();
+        col.enabled = domain.use_color_ramp
+        col.prop(domain, "coba_field")
+        col.template_color_ramp(domain, "color_ramp", expand=True)
+
 
 if __name__ == "__main__":  # only for live edit.
     bpy.utils.register_module(__name__)
diff --git a/source/blender/blenkernel/intern/smoke.c b/source/blender/blenkernel/intern/smoke.c
index 05540f5..e8970d4 100644
--- a/source/blender/blenkernel/intern/smoke.c
+++ b/source/blender/blenkernel/intern/smoke.c
@@ -360,6 +360,10 @@ static void smokeModifier_freeDomain(SmokeModifierData *smd)
 		BKE_ptcache_free_list(&(smd->domain->ptcaches[0]));
 		smd->domain->point_cache[0] = NULL;
 
+		if (smd->domain->coba) {
+			MEM_freeN(smd->domain->coba);
+		}
+
 		MEM_freeN(smd->domain);
 		smd->domain = NULL;
 	}
@@ -544,6 +548,9 @@ void smokeModifier_createType(struct SmokeModifierData *smd)
 			smd->domain->slice_depth = 0.5f;
 			smd->domain->slice_axis = 0;
 			smd->domain->vector_scale = 1.0f;
+
+			smd->domain->coba = NULL;
+			smd->domain->coba_field = FLUID_FIELD_DENSITY;
 		}
 		else if (smd->type & MOD_SMOKE_TYPE_FLOW)
 		{
@@ -646,6 +653,10 @@ void smokeModifier_copy(struct SmokeModifierData *smd, struct SmokeModifierData
 		tsmd->domain->draw_velocity = smd->domain->draw_velocity;
 		tsmd->domain->vector_draw_type = smd->domain->vector_draw_type;
 		tsmd->domain->vector_scale = smd->domain->vector_scale;
+
+		if (smd->domain->coba) {
+			tsmd->domain->coba = MEM_dupallocN(smd->domain->coba);
+		}
 	}
 	else if (tsmd->flow) {
 		tsmd->flow->psys = smd->flow->psys;
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 41b2757..98c8a26 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -5091,6 +5091,7 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
 				smd->domain->tex = NULL;
 				smd->domain->tex_shadow = NULL;
 				smd->domain->tex_wt = NULL;
+				smd->domain->coba = newdataadr(fd, smd->domain->coba);
 				
 				smd->domain->effector_weights = newdataadr(fd, smd->domain->effector_weights);
 				if (!smd->domain->effector_weights)
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index 6678189..d104fc8 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -1728,6 +1728,10 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
 					smd->domain->point_cache[1]->step = 1;
 
 					write_pointcaches(wd, &(smd->domain->ptcaches[1]));
+
+					if (smd->domain->coba) {
+						writestruct(wd, DATA, ColorBand, 1, smd->domain->coba);
+					}
 				}
 
 				writestruct(wd, DATA, SmokeDomainSettings, 1, smd->domain);
diff --git a/source/blender/editors/space_view3d/drawobject.c b/source/blender/editors/space_view3d/drawobject.c
index ea40d4e..dd282c4 100644
--- a/source/blender/editors/space_view3d/drawobject.c
+++ b/source/blender/editors/space_view3d/drawobject.c
@@ -7915,10 +7915,6 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, const short
 			if (!render_override && sds->draw_velocity) {
 				draw_smoke_velocity(sds, viewnormal);
 			}
-
-#ifdef SMOKE_DEBUG_HEAT
-			draw_smoke_heat(smd->domain, ob);
-#endif
 		}
 	}
 
diff --git a/source/blender/editors/space_view3d/drawvolume.c b/source/blender/editors/space_view3d/drawvolume.c
index b0e2160..27ecbf8 100644
--- a/source/blender/editors/space_view3d/drawvolume.c
+++ b/source/blender/editors/space_view3d/drawvolume.c
@@ -1,4 +1,4 @@
-/*
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -41,6 +41,7 @@
 #include "BLI_math.h"
 
 #include "BKE_DerivedMesh.h"
+#include "BKE_texture.h"
 #include "BKE_particle.h"
 
 #include "smoke_API.h"
@@ -62,28 +63,33 @@ struct GPUTexture;
 #  include "PIL_time_utildefines.h"
 #endif
 
-static GPUTexture *create_flame_spectrum_texture(void)
+/* *************************** Transfer functions *************************** */
+
+enum {
+	TFUNC_FLAME_SPECTRUM = 0,
+	TFUNC_COLOR_RAMP     = 1,
+};
+
+#define TFUNC_WIDTH 256
+
+static void create_flame_spectrum_texture(float *data)
 {
-#define SPEC_WIDTH 256
 #define FIRE_THRESH 7
 #define MAX_FIRE_ALPHA 0.06f
 #define FULL_ON_FIRE 100
 
-	GPUTexture *tex;
-	int i, j, k;
-	float *spec_data = MEM_mallocN(SPEC_WIDTH * 4 * sizeof(float), "spec_data");
-	float *spec_pixels = MEM_mallocN(SPEC_WIDTH * 4 * 16 * 16 * sizeof(float), "spec_pixels");
+	float *spec_pixels = MEM_mallocN(TFUNC_WIDTH * 4 * 16 * 16 * sizeof(float), "spec_pixels");
 
-	blackbody_temperature_to_rgb_table(spec_data, SPEC_WIDTH, 1500, 3000);
+	blackbody_temperature_to_rgb_table(data, TFUNC_WIDTH, 1500, 3000);
 
-	for (i = 0; i < 16; i++) {
-		for (j = 0; j < 16; j++) {
-			for (k = 0; k < SPEC_WIDTH; k++) {
-				int index = (j * SPEC_WIDTH * 16 + i * SPEC_WIDTH + k) * 4;
+	for (int i = 0; i < 16; i++) {
+		for (int j = 0; j < 16; j++) {
+			for (int k = 0; k < TFUNC_WIDTH; k++) {
+				int index = (j * TFUNC_WIDTH * 16 + i * TFUNC_WIDTH + k) * 4;
 				if (k >= FIRE_THRESH) {
-					spec_pixels[index] = (spec_data[k * 4]);
-					spec_pixels[index + 1] = (spec_data[k * 4 + 1]);
-					spec_pixels[index + 2] = (spec_data[k * 4 + 2]);
+					spec_pixels[index] = (data[k * 4]);
+					spec_pixels[index + 1] = (data[k * 4 + 1]);
+					spec_pixels[index + 2] = (data[k * 4 + 2]);
 					spec_pixels[index + 3] = MAX_FIRE_ALPHA * (
 					        (k > FULL_ON_FIRE) ? 1.0f : (k - FIRE_THRESH) / ((float)FULL_ON_FIRE - FIRE_THRESH));
 				}
@@ -94,19 +100,69 @@ static GPUTexture *create_flame_spectrum_texture(void)
 		}
 	}
 
-	tex = GPU_texture_create_1D(SPEC_WIDTH, spec_pixels, NULL);
+	memcpy(data, spec_pixels, sizeof(float) * 4 * TFUNC_WIDTH);
 
-	MEM_freeN(spec_data);
 	MEM_freeN(spec_pixels);
 
-#undef SPEC_WIDTH
 #undef FIRE_THRESH
 #undef MAX_FIRE_ALPHA
 #undef FULL_ON_FIRE
+}
+
+static void create_color_ramp(const ColorBand *coba, float *data)
+{
+	for (int i = 0; i < TFUNC_WIDTH; i++) {
+		do_colorband(coba, (float)i / TFUNC_WIDTH, &data[i * 4]);
+	}
+}
+
+static GPUTexture *create_transfer_function(int type, const ColorBand *coba)
+{
+	float *data = MEM_mallocN(sizeof(float) * 4 * TFUNC_WIDTH, __func__);
+
+	switch (type) {
+		case TFUNC_FLAME_SPECTRUM:
+			create_flame_spectrum_texture(data);
+			break;
+		case TFUNC_COLOR_RAMP:
+			create_color_ramp(coba, data);
+			break;
+	}
+
+	GPUTexture *tex = GPU_texture_create_1D(TFUNC_WIDTH, data, NULL);
+
+	MEM_freeN(data);
 
 	return tex;
 }
 
+static GPUTexture *create_field_texture(SmokeDomainSettings *sds)
+{
+	float *field = NULL;
+
+	switch (sds->coba_field) {
+#ifdef WITH_SMOKE
+		case FLUID_FIELD_DENSITY:    field = smoke_get_density(sds->fluid); break;
+		case FLUID_FIELD_HEAT:       field = smoke_get_heat(sds->fluid); break;
+		case FLUID_FIELD_FUEL:       field = smoke_get_fuel(sds->fluid); break;
+		case FLUID_FIELD_REACT:      field = smoke_get_react(sds->fluid); break;
+		case FLUID_FIELD_FLAME:      field = smoke_get_flame(sds->fluid); break;
+		case FLUID_FIELD_VELOCITY_X: field = smoke_get_velocity_x(sds->fluid); break;
+		case FLUID_FIELD_VELOCITY_Y: field = smoke_get_velocity_y(sds->fluid); break;
+		case FLUID_FIELD_VELOCITY_Z: field = smoke_get_velocity_z(sds->fluid); break;
+		case FLUID_FIELD_COLOR_R:    field = smoke_get_color_r(sds->fluid); break;
+		case FLUID_FIELD_COLOR_G:    field = smoke_get_color_g(sds->fluid); break;
+		case FLUID_FIELD_COLOR_B:    field = smoke_get_color_b(sds->fluid); break;
+		case FLUID_FIELD_FORCE_X:    field = smoke_get_force_x(sds->fluid); break;
+		case FLUID_FIELD_FORCE_Y:    field = smoke_get_force_y(sds->fluid); break;
+		case FLUID_FIELD_FORCE_Z:    field = smoke_get_force_z(sds->fluid); break;
+#endif
+		default: return NULL;
+	}
+
+	return GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], 1, field);
+}
+
 typedef struct VolumeSlicer {
 	float size[3];
 	float min[3];
@@ -347,6 +403,7 @@ static int create_view_aligned_slices(VolumeSlicer *slicer,
 }
 
 static void bind_shader(SmokeDomainSettings *sds, GPUShader *shader, GPUTexture *tex_spec,
+                        GPUTexture *tex_tfunc, GPUTexture *tex_coba,
                         bool use_fire, const float min[3],
                         const float ob_sizei[3], const float invsize[3])
 {
@@ -359,6 +416,8 @@ static void bind_shader(SmokeDomainSettings *sds, GPUShader *shader, GPUTexture
 	int densityscale_location;
 	int spec_location, flame_location;
 	int shadow_location, actcol_location;
+	int tfunc_location = 0;
+	int coba_location = 0;
 
 	if (use_fire) {
 		spec_location = GPU_shader_get_uniform(shader, "spectrum_texture");
@@ -370,6 +429,11 @@ static void bind_shader(SmokeDomainSettings *sds, GPUShader *shader, GPUTexture
 		soot_location = GPU_shader_get_uniform(shader, "soot_texture");
 		stepsize_location = GPU_shader_get_uniform(shader, "step_size");
 		densityscale_location = GPU_shader_get_uniform(shader, "density_scale");
+
+		if (sds->use_coba) {
+			tfunc_location = GPU_shader_get_uniform(shader, "transfer_texture");
+			coba_location = GPU_shader_get_uniform(shader, "color_band_texture");
+		}
 	}
 
 	GPU_shader_bind(shader);
@@ -397,6 +461,14 @@ static void bind_shader(SmokeDomainSettings *sds, GPUShader *shader, GPUTexture
 		if ((sds->active_fields & SM_ACTIVE_COLORS) == 0)
 			mul_v3_v3(active_color, sds->active_color);
 		GPU_shader_uniform_vector(shader, actcol_location, 3, 1, active_color);
+
+		if (sds->use_coba) {
+			GPU_texture_bind(tex_tfunc, 4);
+			GPU_shader_uniform_

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list