|
Blender
V2.59
|
00001 /* 00002 * $Id: math_geom_inline.c 36235 2011-04-20 06:47:16Z campbellbarton $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00021 * All rights reserved. 00022 * 00023 * The Original Code is: some of this file. 00024 * 00025 * ***** END GPL LICENSE BLOCK ***** 00026 * */ 00027 00033 #include "BLI_math.h" 00034 00035 #ifndef BLI_MATH_GEOM_INLINE_H 00036 #define BLI_MATH_GEOM_INLINE_H 00037 00038 /****************************** Spherical Harmonics **************************/ 00039 00040 MINLINE void zero_sh(float r[9]) 00041 { 00042 memset(r, 0, sizeof(float)*9); 00043 } 00044 00045 MINLINE void copy_sh_sh(float r[9], const float a[9]) 00046 { 00047 memcpy(r, a, sizeof(float)*9); 00048 } 00049 00050 MINLINE void mul_sh_fl(float r[9], const float f) 00051 { 00052 int i; 00053 00054 for(i=0; i<9; i++) 00055 r[i] *= f; 00056 } 00057 00058 MINLINE void add_sh_shsh(float r[9], const float a[9], const float b[9]) 00059 { 00060 int i; 00061 00062 for(i=0; i<9; i++) 00063 r[i]= a[i] + b[i]; 00064 } 00065 00066 MINLINE float dot_shsh(float a[9], float b[9]) 00067 { 00068 float r= 0.0f; 00069 int i; 00070 00071 for(i=0; i<9; i++) 00072 r += a[i]*b[i]; 00073 00074 return r; 00075 } 00076 00077 MINLINE float diffuse_shv3(float sh[9], const float v[3]) 00078 { 00079 /* See formula (13) in: 00080 "An Efficient Representation for Irradiance Environment Maps" */ 00081 static const float c1 = 0.429043f, c2 = 0.511664f, c3 = 0.743125f; 00082 static const float c4 = 0.886227f, c5 = 0.247708f; 00083 float x, y, z, sum; 00084 00085 x= v[0]; 00086 y= v[1]; 00087 z= v[2]; 00088 00089 sum= c1*sh[8]*(x*x - y*y); 00090 sum += c3*sh[6]*z*z; 00091 sum += c4*sh[0]; 00092 sum += -c5*sh[6]; 00093 sum += 2.0f*c1*(sh[4]*x*y + sh[7]*x*z + sh[5]*y*z); 00094 sum += 2.0f*c2*(sh[3]*x + sh[1]*y + sh[2]*z); 00095 00096 return sum; 00097 } 00098 00099 MINLINE void vec_fac_to_sh(float r[9], const float v[3], const float f) 00100 { 00101 /* See formula (3) in: 00102 "An Efficient Representation for Irradiance Environment Maps" */ 00103 float sh[9], x, y, z; 00104 00105 x= v[0]; 00106 y= v[1]; 00107 z= v[2]; 00108 00109 sh[0]= 0.282095f; 00110 00111 sh[1]= 0.488603f*y; 00112 sh[2]= 0.488603f*z; 00113 sh[3]= 0.488603f*x; 00114 00115 sh[4]= 1.092548f*x*y; 00116 sh[5]= 1.092548f*y*z; 00117 sh[6]= 0.315392f*(3.0f*z*z - 1.0f); 00118 sh[7]= 1.092548f*x*z; 00119 sh[8]= 0.546274f*(x*x - y*y); 00120 00121 mul_sh_fl(sh, f); 00122 copy_sh_sh(r, sh); 00123 } 00124 00125 MINLINE float eval_shv3(float sh[9], const float v[3]) 00126 { 00127 float tmp[9]; 00128 00129 vec_fac_to_sh(tmp, v, 1.0f); 00130 return dot_shsh(tmp, sh); 00131 } 00132 00133 MINLINE void madd_sh_shfl(float r[9], const float sh[3], const float f) 00134 { 00135 float tmp[9]; 00136 00137 copy_sh_sh(tmp, sh); 00138 mul_sh_fl(tmp, f); 00139 add_sh_shsh(r, r, tmp); 00140 } 00141 00142 #endif /* BLI_MATH_GEOM_INLINE_H */ 00143