Blender V2.61 - r43446
|
00001 /* 00002 * Copyright 2011, Blender Foundation. 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #ifndef __SVM_HSV_H__ 00020 #define __SVM_HSV_H__ 00021 00022 CCL_NAMESPACE_BEGIN 00023 00024 __device float3 rgb_to_hsv(float3 rgb) 00025 { 00026 float cmax, cmin, h, s, v, cdelta; 00027 float3 c; 00028 00029 cmax = fmaxf(rgb.x, fmaxf(rgb.y, rgb.z)); 00030 cmin = min(rgb.x, min(rgb.y, rgb.z)); 00031 cdelta = cmax - cmin; 00032 00033 v = cmax; 00034 00035 if(cmax != 0.0f) { 00036 s = cdelta/cmax; 00037 } 00038 else { 00039 s = 0.0f; 00040 h = 0.0f; 00041 } 00042 00043 if(s == 0.0f) { 00044 h = 0.0f; 00045 } 00046 else { 00047 float3 cmax3 = make_float3(cmax, cmax, cmax); 00048 c = (cmax3 - rgb)/cdelta; 00049 00050 if(rgb.x == cmax) h = c.z - c.y; 00051 else if(rgb.y == cmax) h = 2.0f + c.x - c.z; 00052 else h = 4.0f + c.y - c.x; 00053 00054 h /= 6.0f; 00055 00056 if(h < 0.0f) 00057 h += 1.0f; 00058 } 00059 00060 return make_float3(h, s, v); 00061 } 00062 00063 __device float3 hsv_to_rgb(float3 hsv) 00064 { 00065 float i, f, p, q, t, h, s, v; 00066 float3 rgb; 00067 00068 h = hsv.x; 00069 s = hsv.y; 00070 v = hsv.z; 00071 00072 if(s==0.0f) { 00073 rgb = make_float3(v, v, v); 00074 } 00075 else { 00076 if(h==1.0f) 00077 h = 0.0f; 00078 00079 h *= 6.0f; 00080 i = floor(h); 00081 f = h - i; 00082 rgb = make_float3(f, f, f); 00083 p = v*(1.0f-s); 00084 q = v*(1.0f-(s*f)); 00085 t = v*(1.0f-(s*(1.0f-f))); 00086 00087 if(i == 0.0f) rgb = make_float3(v, t, p); 00088 else if(i == 1.0f) rgb = make_float3(q, v, p); 00089 else if(i == 2.0f) rgb = make_float3(p, v, t); 00090 else if(i == 3.0f) rgb = make_float3(p, q, v); 00091 else if(i == 4.0f) rgb = make_float3(t, p, v); 00092 else rgb = make_float3(v, p, q); 00093 } 00094 00095 return rgb; 00096 } 00097 00098 __device void svm_node_hsv(KernelGlobals *kg, ShaderData *sd, float *stack, uint in_color_offset, uint fac_offset, uint out_color_offset, int *offset) 00099 { 00100 /* read extra data */ 00101 uint4 node1 = read_node(kg, offset); 00102 00103 float fac = stack_load_float(stack, fac_offset); 00104 float3 in_color = stack_load_float3(stack, in_color_offset); 00105 float3 color = in_color; 00106 00107 float hue = stack_load_float(stack, node1.y); 00108 float sat = stack_load_float(stack, node1.z); 00109 float val = stack_load_float(stack, node1.w); 00110 00111 color = rgb_to_hsv(color); 00112 00113 // remember: fmod doesn't work for negative numbers 00114 color.x += hue + 0.5f; 00115 color.x = fmod(color.x, 1.0f); 00116 color.y *= sat; 00117 color.z *= val; 00118 00119 color = hsv_to_rgb(color); 00120 00121 color.x = fac*color.x + (1.0f - fac)*in_color.x; 00122 color.y = fac*color.y + (1.0f - fac)*in_color.y; 00123 color.z = fac*color.z + (1.0f - fac)*in_color.z; 00124 00125 if (stack_valid(out_color_offset)) 00126 stack_store_float3(stack, out_color_offset, color); 00127 } 00128 00129 CCL_NAMESPACE_END 00130 00131 #endif /* __SVM_HSV_H__ */ 00132