[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [59220] branches/soc-2013-dingto: Cycles / Sky Model:
Thomas Dinges
blender at dingto.org
Sat Aug 17 23:24:47 CEST 2013
Revision: 59220
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=59220
Author: dingto
Date: 2013-08-17 21:24:46 +0000 (Sat, 17 Aug 2013)
Log Message:
-----------
Cycles / Sky Model:
* Old and new model can now be selected from a drop down menu.
* Old blend files load fine and use the old model, when we create a new Sky Texture node it defaults to the new model.
* Fixed Strength issues with new model and some code cleanup there.
ToDo:
* OSL implementation for thee new model.
* Code cleanup. Maybe outsource the whole precalc into its own cpp file?
Modified Paths:
--------------
branches/soc-2013-dingto/intern/cycles/blender/blender_shader.cpp
branches/soc-2013-dingto/intern/cycles/kernel/svm/svm.h
branches/soc-2013-dingto/intern/cycles/kernel/svm/svm_sky.h
branches/soc-2013-dingto/intern/cycles/kernel/svm/svm_types.h
branches/soc-2013-dingto/intern/cycles/render/nodes.cpp
branches/soc-2013-dingto/intern/cycles/render/nodes.h
branches/soc-2013-dingto/source/blender/editors/space_node/drawnode.c
branches/soc-2013-dingto/source/blender/makesdna/DNA_node_types.h
branches/soc-2013-dingto/source/blender/makesrna/intern/rna_nodetree.c
branches/soc-2013-dingto/source/blender/nodes/shader/nodes/node_shader_tex_sky.c
Modified: branches/soc-2013-dingto/intern/cycles/blender/blender_shader.cpp
===================================================================
--- branches/soc-2013-dingto/intern/cycles/blender/blender_shader.cpp 2013-08-17 19:15:06 UTC (rev 59219)
+++ branches/soc-2013-dingto/intern/cycles/blender/blender_shader.cpp 2013-08-17 21:24:46 UTC (rev 59220)
@@ -618,6 +618,7 @@
else if (b_node.is_a(&RNA_ShaderNodeTexSky)) {
BL::ShaderNodeTexSky b_sky_node(b_node);
SkyTextureNode *sky = new SkyTextureNode();
+ sky->type = SkyTextureNode::type_enum[(int)b_sky_node.sky_type()];
sky->sun_direction = get_float3(b_sky_node.sun_direction());
sky->turbidity = b_sky_node.turbidity();
sky->albedo = b_sky_node.albedo();
Modified: branches/soc-2013-dingto/intern/cycles/kernel/svm/svm.h
===================================================================
--- branches/soc-2013-dingto/intern/cycles/kernel/svm/svm.h 2013-08-17 19:15:06 UTC (rev 59219)
+++ branches/soc-2013-dingto/intern/cycles/kernel/svm/svm.h 2013-08-17 21:24:46 UTC (rev 59220)
@@ -257,7 +257,7 @@
#endif
#ifdef __PROCEDURAL_TEXTURES__
case NODE_TEX_SKY:
- svm_node_tex_sky(kg, sd, stack, node.y, node.z);
+ svm_node_tex_sky(kg, sd, stack, node.y, node.z, node.w);
break;
case NODE_TEX_GRADIENT:
svm_node_tex_gradient(sd, stack, node);
Modified: branches/soc-2013-dingto/intern/cycles/kernel/svm/svm_sky.h
===================================================================
--- branches/soc-2013-dingto/intern/cycles/kernel/svm/svm_sky.h 2013-08-17 19:15:06 UTC (rev 59219)
+++ branches/soc-2013-dingto/intern/cycles/kernel/svm/svm_sky.h 2013-08-17 21:24:46 UTC (rev 59220)
@@ -18,10 +18,7 @@
CCL_NAMESPACE_BEGIN
-/*
- * "An Analytic Model for Full Spectral Sky-Dome Radiance"
- * Lukas Hosek, Alexander Wilkie
- */
+/* Sky texture */
__device float sky_angle_between(float thetav, float phiv, float theta, float phi)
{
@@ -29,19 +26,60 @@
return safe_acosf(cospsi);
}
-/* ArHosekSkyModel_GetRadianceInternal */
+/*
+ * "A Practical Analytic Model for Daylight"
+ * A. J. Preetham, Peter Shirley, Brian Smits
+ */
+__device float sky_perez_function(__constant float *lam, float theta, float gamma)
+{
+ float ctheta = cosf(theta);
+ float cgamma = cosf(gamma);
+
+ return (1.0f + lam[0]*expf(lam[1]/ctheta)) * (1.0f + lam[2]*expf(lam[3]*gamma) + lam[4]*cgamma*cgamma);
+}
+
+__device float3 sky_radiance_old(KernelGlobals *kg, float3 dir)
+{
+ /* convert vector to spherical coordinates */
+ float2 spherical = direction_to_spherical(dir);
+ float theta = spherical.x;
+ float phi = spherical.y;
+
+ /* angle between sun direction and dir */
+ float gamma = sky_angle_between(theta, phi, kernel_data.sunsky.theta, kernel_data.sunsky.phi);
+
+ /* clamp theta to horizon */
+ theta = min(theta, M_PI_2_F - 0.001f);
+
+ /* compute xyY color space values */
+ float x = kernel_data.sunsky.radiance_y * sky_perez_function(kernel_data.sunsky.config_y, theta, gamma);
+ float y = kernel_data.sunsky.radiance_z * sky_perez_function(kernel_data.sunsky.config_z, theta, gamma);
+ float Y = kernel_data.sunsky.radiance_x * sky_perez_function(kernel_data.sunsky.config_x, theta, gamma);
+
+ /* convert to RGB */
+ float3 xyz = xyY_to_xyz(x, y, Y);
+ return xyz_to_rgb(xyz.x, xyz.y, xyz.z);
+}
+
+/*
+ * "An Analytic Model for Full Spectral Sky-Dome Radiance"
+ * Lukas Hosek, Alexander Wilkie
+ */
__device float sky_radiance_internal(__constant float *configuration, float theta, float gamma)
{
- const float expM = expf(configuration[4] * gamma);
- const float rayM = cosf(gamma)*cosf(gamma);
- const float mieM = (1.0f + cosf(gamma)*cosf(gamma)) / powf((1.0f + configuration[8]*configuration[8] - 2.0f*configuration[8]*cosf(gamma)), 1.5f);
- const float zenith = sqrt(cosf(theta));
+ float ctheta = cosf(theta);
+ float cgamma = cosf(gamma);
- return (1.0f + configuration[0] * expf(configuration[1] / (cosf(theta) + 0.01f))) *
- (configuration[2] + configuration[3] * expM + configuration[5] * rayM + configuration[6] * mieM + configuration[7] * zenith);
+ float expM = expf(configuration[4] * gamma);
+ float rayM = cgamma * cgamma;
+ float mieM = (1.0f + rayM) / powf((1.0f + configuration[8]*configuration[8] - 2.0f*configuration[8]*cgamma), 1.5f);
+ float zenith = sqrt(ctheta);
+
+ return (1.0f + configuration[0] * expf(configuration[1] / (ctheta + 0.01f))) *
+ (configuration[2] + configuration[3] * expM + configuration[5] * rayM + configuration[6] * mieM + configuration[7] * zenith);
}
-__device float3 sky_radiance(KernelGlobals *kg, float3 dir)
+__device float3 sky_radiance_new(KernelGlobals *kg, float3 dir)
{
/* convert vector to spherical coordinates */
float2 spherical = direction_to_spherical(dir);
@@ -59,15 +97,20 @@
float y = sky_radiance_internal(kernel_data.sunsky.config_y, theta, gamma) * kernel_data.sunsky.radiance_y;
float z = sky_radiance_internal(kernel_data.sunsky.config_z, theta, gamma) * kernel_data.sunsky.radiance_z;
- /* convert to RGB */
- return xyz_to_rgb(x, y, z);
+ /* convert to RGB and adjust strength*/
+ return xyz_to_rgb(x, y, z) * (M_2PI_F/683);
}
-__device void svm_node_tex_sky(KernelGlobals *kg, ShaderData *sd, float *stack, uint dir_offset, uint out_offset)
+__device void svm_node_tex_sky(KernelGlobals *kg, ShaderData *sd, float *stack, uint dir_offset, uint out_offset, uint sky_model)
{
float3 dir = stack_load_float3(stack, dir_offset);
- float3 f = sky_radiance(kg, dir);
+ float3 f;
+ if(sky_model == 0)
+ f = sky_radiance_old(kg, dir);
+ else
+ f = sky_radiance_new(kg, dir);
+
stack_store_float3(stack, out_offset, f);
}
Modified: branches/soc-2013-dingto/intern/cycles/kernel/svm/svm_types.h
===================================================================
--- branches/soc-2013-dingto/intern/cycles/kernel/svm/svm_types.h 2013-08-17 19:15:06 UTC (rev 59219)
+++ branches/soc-2013-dingto/intern/cycles/kernel/svm/svm_types.h 2013-08-17 21:24:46 UTC (rev 59220)
@@ -287,6 +287,11 @@
NODE_WAVE_RINGS
} NodeWaveType;
+typedef enum NodeSkyType {
+ NODE_SKY_OLD,
+ NODE_SKY_NEW
+} NodeSkyType;
+
typedef enum NodeGradientType {
NODE_BLEND_LINEAR,
NODE_BLEND_QUADRATIC,
Modified: branches/soc-2013-dingto/intern/cycles/render/nodes.cpp
===================================================================
--- branches/soc-2013-dingto/intern/cycles/render/nodes.cpp 2013-08-17 19:15:06 UTC (rev 59219)
+++ branches/soc-2013-dingto/intern/cycles/render/nodes.cpp 2013-08-17 21:24:46 UTC (rev 59220)
@@ -387,8 +387,72 @@
return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
}
-static void sky_texture_precompute(KernelSunSky *ksunsky, float3 dir, float turbidity, float albedo)
+/* Preetham model */
+static float sky_perez_function(float lam[6], float theta, float gamma)
{
+ return (1.0f + lam[0]*expf(lam[1]/cosf(theta))) * (1.0f + lam[2]*expf(lam[3]*gamma) + lam[4]*cosf(gamma)*cosf(gamma));
+}
+
+static void sky_texture_precompute_old(KernelSunSky *ksunsky, float3 dir, float turbidity)
+{
+ /*
+ * We re-use the kernel_data of the new model, to avoid extra variables
+ * zenith_Y/x/y is now radiance_x/y/z
+ * perez_Y/x/y is now config_x/y/z
+ */
+
+ float2 spherical = sky_spherical_coordinates(dir);
+ float theta = spherical.x;
+ float phi = spherical.y;
+
+ ksunsky->theta = theta;
+ ksunsky->phi = phi;
+
+ float theta2 = theta*theta;
+ float theta3 = theta2*theta;
+ float T = turbidity;
+ float T2 = T * T;
+
+ float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
+ ksunsky->radiance_x = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
+ ksunsky->radiance_x *= 0.06f;
+
+ ksunsky->radiance_y =
+ (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
+ (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
+ (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
+
+ ksunsky->radiance_z =
+ (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
+ (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta + 0.00516f) * T +
+ (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta + 0.26688f);
+
+ ksunsky->config_x[0] = (0.1787f * T - 1.4630f);
+ ksunsky->config_x[1] = (-0.3554f * T + 0.4275f);
+ ksunsky->config_x[2] = (-0.0227f * T + 5.3251f);
+ ksunsky->config_x[3] = (0.1206f * T - 2.5771f);
+ ksunsky->config_x[4] = (-0.0670f * T + 0.3703f);
+
+ ksunsky->config_y[0] = (-0.0193f * T - 0.2592f);
+ ksunsky->config_y[1] = (-0.0665f * T + 0.0008f);
+ ksunsky->config_y[2] = (-0.0004f * T + 0.2125f);
+ ksunsky->config_y[3] = (-0.0641f * T - 0.8989f);
+ ksunsky->config_y[4] = (-0.0033f * T + 0.0452f);
+
+ ksunsky->config_z[0] = (-0.0167f * T - 0.2608f);
+ ksunsky->config_z[1] = (-0.0950f * T + 0.0092f);
+ ksunsky->config_z[2] = (-0.0079f * T + 0.2102f);
+ ksunsky->config_z[3] = (-0.0441f * T - 1.6537f);
+ ksunsky->config_z[4] = (-0.0109f * T + 0.0529f);
+
+ ksunsky->radiance_x /= sky_perez_function(ksunsky->config_x, 0, theta);
+ ksunsky->radiance_y /= sky_perez_function(ksunsky->config_y, 0, theta);
+ ksunsky->radiance_z /= sky_perez_function(ksunsky->config_z, 0, theta);
+}
+
+/* Hosek / Wilkie */
+static void sky_texture_precompute_new(KernelSunSky *ksunsky, float3 dir, float turbidity, float albedo)
+{
/* Calculate Sun Direction and save coordinates */
float2 spherical = sky_spherical_coordinates(dir);
float theta = spherical.x;
@@ -442,9 +506,23 @@
arhosekskymodelstate_free(sky_state);
}
+static ShaderEnum sky_type_init()
+{
+ ShaderEnum enm;
+
+ enm.insert("Preetham", NODE_SKY_OLD);
+ enm.insert("Hosek / Wilkie", NODE_SKY_NEW);
+
+ return enm;
+}
+
+ShaderEnum SkyTextureNode::type_enum = sky_type_init();
+
SkyTextureNode::SkyTextureNode()
: TextureNode("sky_texture")
{
+ type = ustring("Hosek / Wilkie");
+
sun_direction = make_float3(0.0f, 0.0f, 1.0f);
turbidity = 2.2f;
albedo = 0.3f;
@@ -459,7 +537,10 @@
ShaderOutput *color_out = output("Color");
if(compiler.sunsky) {
- sky_texture_precompute(compiler.sunsky, sun_direction, turbidity, albedo);
+ if(type_enum[type] == NODE_SKY_OLD)
+ sky_texture_precompute_old(compiler.sunsky, sun_direction, turbidity);
+ else if(type_enum[type] == NODE_SKY_NEW)
+ sky_texture_precompute_new(compiler.sunsky, sun_direction, turbidity, albedo);
compiler.sunsky = NULL;
}
@@ -467,6 +548,7 @@
compiler.stack_assign(vector_in);
int vector_offset = vector_in->stack_offset;
+ int sky_model = type_enum[type];
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list