Blender V2.61 - r43446

texture_ocean.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * Contributors: Matt Ebb
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00026 #include <stddef.h>
00027 
00028 #include "BLI_math.h"
00029 #include "BLI_utildefines.h"
00030 
00031 #include "DNA_modifier_types.h"
00032 #include "DNA_object_types.h"
00033 #include "DNA_texture_types.h"
00034 
00035 #include "BKE_global.h" /* XXX */
00036 
00037 #include "BKE_modifier.h"
00038 #include "BKE_ocean.h"
00039 #include "BKE_utildefines.h"
00040 
00041 #include "render_types.h"
00042 #include "RE_shader_ext.h"
00043 
00044 #include "texture.h"
00045 
00046 
00047 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
00048 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
00049 /* only to be used here in this file, it's for speed */
00050 extern struct Render R;
00051 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
00052 
00053 
00054 
00055 
00056 /* ***** actual texture sampling ***** */
00057 int ocean_texture(Tex *tex, float *texvec, TexResult *texres)
00058 {
00059     OceanTex *ot= tex->ot;
00060     ModifierData *md;
00061     OceanModifierData *omd;
00062 
00063     texres->tin = 0.0f;
00064 
00065     if ( !(ot) ||
00066          !(ot->object) ||
00067          !(md = (ModifierData *)modifiers_findByType(ot->object, eModifierType_Ocean)) ||
00068          !(omd= (OceanModifierData *)md)->ocean)
00069     {
00070         return 0;
00071     }
00072     else {
00073         const int do_normals = (omd->flag & MOD_OCEAN_GENERATE_NORMALS);
00074         int cfra = R.r.cfra;
00075         int retval = TEX_INT;
00076 
00077         OceanResult ocr;
00078         const float u = 0.5f+0.5f*texvec[0];
00079         const float v = 0.5f+0.5f*texvec[1];
00080 
00081         if (omd->oceancache && omd->cached==TRUE) {
00082 
00083             CLAMP(cfra, omd->bakestart, omd->bakeend);
00084             cfra -= omd->bakestart; // shift to 0 based
00085 
00086             BKE_ocean_cache_eval_uv(omd->oceancache, &ocr, cfra, u, v);
00087 
00088         }
00089         else {  // non-cached
00090 
00091             if (G.rendering)
00092                 BKE_ocean_eval_uv_catrom(omd->ocean, &ocr, u, v);
00093             else
00094                 BKE_ocean_eval_uv(omd->ocean, &ocr, u, v);
00095 
00096             ocr.foam = BKE_ocean_jminus_to_foam(ocr.Jminus, omd->foam_coverage);
00097         }
00098 
00099         switch (ot->output) {
00100             case TEX_OCN_DISPLACEMENT:
00101                 /* XYZ displacement */
00102                 texres->tr = 0.5f + 0.5f * ocr.disp[0];
00103                 texres->tg = 0.5f + 0.5f * ocr.disp[2];
00104                 texres->tb = 0.5f + 0.5f * ocr.disp[1];
00105 
00106                 texres->tr = MAX2(0.0f, texres->tr);
00107                 texres->tg = MAX2(0.0f, texres->tg);
00108                 texres->tb = MAX2(0.0f, texres->tb);
00109 
00110                 BRICONTRGB;
00111 
00112                 retval = TEX_RGB;
00113                 break;
00114 
00115             case TEX_OCN_EMINUS:
00116                 /* -ve eigenvectors ? */
00117                 texres->tr = ocr.Eminus[0];
00118                 texres->tg = ocr.Eminus[2];
00119                 texres->tb = ocr.Eminus[1];
00120                 retval = TEX_RGB;
00121                 break;
00122 
00123             case TEX_OCN_EPLUS:
00124                 /* -ve eigenvectors ? */
00125                 texres->tr = ocr.Eplus[0];
00126                 texres->tg = ocr.Eplus[2];
00127                 texres->tb = ocr.Eplus[1];
00128                 retval = TEX_RGB;
00129                 break;
00130 
00131             case TEX_OCN_JPLUS:
00132                 texres->tin = ocr.Jplus;
00133                 retval = TEX_INT;
00134                 break;
00135 
00136             case TEX_OCN_FOAM:
00137 
00138                 texres->tin = ocr.foam;
00139 
00140                 BRICONT;
00141 
00142                 retval = TEX_INT;
00143                 break;
00144         }
00145 
00146         /* if normals needed */
00147 
00148         if (texres->nor && do_normals) {
00149             normalize_v3_v3(texres->nor, ocr.normal);
00150             retval |= TEX_NOR;
00151         }
00152 
00153         texres->ta = 1.0f;
00154 
00155         return retval;
00156     }
00157 }