2
* $Id: texture.c 30538 2010-07-20 13:32:42Z blendix $
4
* ***** BEGIN GPL LICENSE BLOCK *****
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21
* All rights reserved.
23
* Contributor(s): 2004-2006, Blender Foundation, full recode
25
* ***** END GPL LICENSE BLOCK *****
35
#include "BLI_blenlib.h"
39
#include "DNA_texture_types.h"
40
#include "DNA_object_types.h"
41
#include "DNA_lamp_types.h"
42
#include "DNA_mesh_types.h"
43
#include "DNA_meshdata_types.h"
44
#include "DNA_material_types.h"
45
#include "DNA_image_types.h"
46
#include "DNA_node_types.h"
48
#include "IMB_imbuf_types.h"
49
#include "IMB_imbuf.h"
51
#include "BKE_colortools.h"
52
#include "BKE_image.h"
54
#include "BKE_plugin_types.h"
55
#include "BKE_utildefines.h"
57
#include "BKE_global.h"
59
#include "BKE_material.h"
61
#include "BKE_library.h"
62
#include "BKE_image.h"
63
#include "BKE_texture.h"
68
#include "pointdensity.h"
69
#include "voxeldata.h"
70
#include "renderpipeline.h"
71
#include "render_types.h"
72
#include "rendercore.h"
76
#include "renderdatabase.h" /* needed for UV */
78
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
79
/* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
80
/* only to be used here in this file, it's for speed */
81
extern struct Render R;
82
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
87
void init_render_texture(Render *re, Tex *tex)
89
int cfra= re->scene->r.cfra;
91
if(re) cfra= re->r.cfra;
94
if(tex->ima && ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
95
BKE_image_user_calc_frame(&tex->iuser, cfra, re?re->flag & R_SEC_FIELD:0);
98
if(tex->type==TEX_PLUGIN) {
99
if(tex->plugin && tex->plugin->doit) {
100
if(tex->plugin->cfra) {
101
*(tex->plugin->cfra)= (float)cfra; //BKE_curframe(re->scene); // XXX old animsys - timing stuff to be fixed
105
else if(tex->type==TEX_ENVMAP) {
107
tex->imaflag |= TEX_INTERPOL | TEX_MIPMAP;
108
tex->extend= TEX_CLIP;
111
if(tex->env->type==ENV_PLANE)
112
tex->extend= TEX_EXTEND;
114
/* only free envmap when rendermode was set to render envmaps, for previewrender */
115
if(G.rendering && re) {
116
if (re->r.mode & R_ENVMAP)
117
if(tex->env->stype==ENV_ANIM)
118
BKE_free_envmapdata(tex->env);
123
if(tex->nodetree && tex->use_nodes) {
124
ntreeBeginExecTree(tex->nodetree); /* has internal flag to detect it only does it once */
128
/* ------------------------------------------------------------------------- */
130
void init_render_textures(Render *re)
134
tex= G.main->tex.first;
136
if(tex->id.us) init_render_texture(re, tex);
141
void end_render_texture(Tex *tex)
143
if(tex && tex->use_nodes && tex->nodetree)
144
ntreeEndExecTree(tex->nodetree);
147
void end_render_textures(void)
150
for(tex= G.main->tex.first; tex; tex= tex->id.next)
152
end_render_texture(tex);
155
/* ------------------------------------------------------------------------- */
158
/* this allows colorbanded textures to control normals as well */
159
static void tex_normal_derivate(Tex *tex, TexResult *texres)
161
if (tex->flag & TEX_COLORBAND) {
163
if (do_colorband(tex->coba, texres->tin, col)) {
164
float fac0, fac1, fac2, fac3;
166
fac0= (col[0]+col[1]+col[2]);
167
do_colorband(tex->coba, texres->nor[0], col);
168
fac1= (col[0]+col[1]+col[2]);
169
do_colorband(tex->coba, texres->nor[1], col);
170
fac2= (col[0]+col[1]+col[2]);
171
do_colorband(tex->coba, texres->nor[2], col);
172
fac3= (col[0]+col[1]+col[2]);
174
texres->nor[0]= 0.3333*(fac0 - fac1);
175
texres->nor[1]= 0.3333*(fac0 - fac2);
176
texres->nor[2]= 0.3333*(fac0 - fac3);
181
texres->nor[0]= texres->tin - texres->nor[0];
182
texres->nor[1]= texres->tin - texres->nor[1];
183
texres->nor[2]= texres->tin - texres->nor[2];
188
static int blend(Tex *tex, float *texvec, TexResult *texres)
192
if(tex->flag & TEX_FLIPBLEND) {
201
if(tex->stype==TEX_LIN) { /* lin */
202
texres->tin= (1.0+x)/2.0;
204
else if(tex->stype==TEX_QUAD) { /* quad */
205
texres->tin= (1.0+x)/2.0;
206
if(texres->tin<0.0) texres->tin= 0.0;
207
else texres->tin*= texres->tin;
209
else if(tex->stype==TEX_EASE) { /* ease */
210
texres->tin= (1.0+x)/2.0;
211
if(texres->tin<=.0) texres->tin= 0.0;
212
else if(texres->tin>=1.0) texres->tin= 1.0;
214
t= texres->tin*texres->tin;
215
texres->tin= (3.0*t-2.0*t*texres->tin);
218
else if(tex->stype==TEX_DIAG) { /* diag */
219
texres->tin= (2.0+x+y)/4.0;
221
else if(tex->stype==TEX_RAD) { /* radial */
222
texres->tin= (atan2(y,x) / (2*M_PI) + 0.5);
224
else { /* sphere TEX_SPHERE */
225
texres->tin= 1.0-sqrt(x*x+ y*y+texvec[2]*texvec[2]);
226
if(texres->tin<0.0) texres->tin= 0.0;
227
if(tex->stype==TEX_HALO) texres->tin*= texres->tin; /* halo */
235
/* ------------------------------------------------------------------------- */
236
/* ************************************************************************* */
238
/* newnoise: all noisebased types now have different noisebases to choose from */
240
static int clouds(Tex *tex, float *texvec, TexResult *texres)
244
texres->tin = BLI_gTurbulence(tex->noisesize, texvec[0], texvec[1], texvec[2], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
246
if (texres->nor!=NULL) {
247
// calculate bumpnormal
248
texres->nor[0] = BLI_gTurbulence(tex->noisesize, texvec[0] + tex->nabla, texvec[1], texvec[2], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
249
texres->nor[1] = BLI_gTurbulence(tex->noisesize, texvec[0], texvec[1] + tex->nabla, texvec[2], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
250
texres->nor[2] = BLI_gTurbulence(tex->noisesize, texvec[0], texvec[1], texvec[2] + tex->nabla, tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
252
tex_normal_derivate(tex, texres);
256
if (tex->stype==TEX_COLOR) {
257
// in this case, int. value should really be computed from color,
258
// and bumpnormal from that, would be too slow, looks ok as is
259
texres->tr = texres->tin;
260
texres->tg = BLI_gTurbulence(tex->noisesize, texvec[1], texvec[0], texvec[2], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
261
texres->tb = BLI_gTurbulence(tex->noisesize, texvec[1], texvec[2], texvec[0], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
264
return (rv | TEX_RGB);
273
/* creates a sine wave */
274
static float tex_sin(float a)
276
a = 0.5 + 0.5*sin(a);
281
/* creates a saw wave */
282
static float tex_saw(float a)
284
const float b = 2*M_PI;
286
int n = (int)(a / b);
292
/* creates a triangle wave */
293
static float tex_tri(float a)
295
const float b = 2*M_PI;
296
const float rmax = 1.0;
298
a = rmax - 2.0*fabs(floor((a*(1.0/b))+0.5) - (a*(1.0/b)));
303
/* computes basic wood intensity value at x,y,z */
304
static float wood_int(Tex *tex, float x, float y, float z)
307
short wf = tex->noisebasis2; /* wave form: TEX_SIN=0, TEX_SAW=1, TEX_TRI=2 */
308
short wt = tex->stype; /* wood type: TEX_BAND=0, TEX_RING=1, TEX_BANDNOISE=2, TEX_RINGNOISE=3 */
310
float (*waveform[3])(float); /* create array of pointers to waveform functions */
311
waveform[0] = tex_sin; /* assign address of tex_sin() function to pointer array */
312
waveform[1] = tex_saw;
313
waveform[2] = tex_tri;
315
if ((wf>TEX_TRI) || (wf<TEX_SIN)) wf=0; /* check to be sure noisebasis2 is initialized ahead of time */
318
wi = waveform[wf]((x + y + z)*10.0);
320
else if (wt==TEX_RING) {
321
wi = waveform[wf](sqrt(x*x + y*y + z*z)*20.0);
323
else if (wt==TEX_BANDNOISE) {
324
wi = tex->turbul*BLI_gNoise(tex->noisesize, x, y, z, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
325
wi = waveform[wf]((x + y + z)*10.0 + wi);
327
else if (wt==TEX_RINGNOISE) {
328
wi = tex->turbul*BLI_gNoise(tex->noisesize, x, y, z, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
329
wi = waveform[wf](sqrt(x*x + y*y + z*z)*20.0 + wi);
335
static int wood(Tex *tex, float *texvec, TexResult *texres)
339
texres->tin = wood_int(tex, texvec[0], texvec[1], texvec[2]);
340
if (texres->nor!=NULL) {
341
/* calculate bumpnormal */
342
texres->nor[0] = wood_int(tex, texvec[0] + tex->nabla, texvec[1], texvec[2]);
343
texres->nor[1] = wood_int(tex, texvec[0], texvec[1] + tex->nabla, texvec[2]);
344
texres->nor[2] = wood_int(tex, texvec[0], texvec[1], texvec[2] + tex->nabla);
346
tex_normal_derivate(tex, texres);
355
/* computes basic marble intensity at x,y,z */
356
static float marble_int(Tex *tex, float x, float y, float z)
359
short wf = tex->noisebasis2; /* wave form: TEX_SIN=0, TEX_SAW=1, TEX_TRI=2 */
360
short mt = tex->stype; /* marble type: TEX_SOFT=0, TEX_SHARP=1,TEX_SHAPER=2 */
362
float (*waveform[3])(float); /* create array of pointers to waveform functions */
363
waveform[0] = tex_sin; /* assign address of tex_sin() function to pointer array */
364
waveform[1] = tex_saw;
365
waveform[2] = tex_tri;
367
if ((wf>TEX_TRI) || (wf<TEX_SIN)) wf=0; /* check to be sure noisebasis2 isn't initialized ahead of time */
369
n = 5.0 * (x + y + z);
371
mi = n + tex->turbul * BLI_gTurbulence(tex->noisesize, x, y, z, tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
373
if (mt>=TEX_SOFT) { /* TEX_SOFT always true */
374
mi = waveform[wf](mi);
378
else if (mt==TEX_SHARPER) {
386
static int marble(Tex *tex, float *texvec, TexResult *texres)
390
texres->tin = marble_int(tex, texvec[0], texvec[1], texvec[2]);
392
if (texres->nor!=NULL) {
393
/* calculate bumpnormal */
394
texres->nor[0] = marble_int(tex, texvec[0] + tex->nabla, texvec[1], texvec[2]);
395
texres->nor[1] = marble_int(tex, texvec[0], texvec[1] + tex->nabla, texvec[2]);
396
texres->nor[2] = marble_int(tex, texvec[0], texvec[1], texvec[2] + tex->nabla);
398
tex_normal_derivate(tex, texres);
408
/* ------------------------------------------------------------------------- */
410
static int magic(Tex *tex, float *texvec, TexResult *texres)
412
float x, y, z, turb=1.0;
416
turb= tex->turbul/5.0;
418
x= sin( ( texvec[0]+texvec[1]+texvec[2])*5.0 );
419
y= cos( (-texvec[0]+texvec[1]-texvec[2])*5.0 );
420
z= -cos( (-texvec[0]-texvec[1]+texvec[2])*5.0 );
475
texres->tin= 0.3333*(texres->tr+texres->tg+texres->tb);
483
/* ------------------------------------------------------------------------- */
485
/* newnoise: stucci also modified to use different noisebasis */
486
static int stucci(Tex *tex, float *texvec, TexResult *texres)
488
float nor[3], b2, ofs;
491
b2= BLI_gNoise(tex->noisesize, texvec[0], texvec[1], texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
493
ofs= tex->turbul/200.0;
495
if(tex->stype) ofs*=(b2*b2);
496
nor[0] = BLI_gNoise(tex->noisesize, texvec[0]+ofs, texvec[1], texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
497
nor[1] = BLI_gNoise(tex->noisesize, texvec[0], texvec[1]+ofs, texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
498
nor[2] = BLI_gNoise(tex->noisesize, texvec[0], texvec[1], texvec[2]+ofs, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
504
VECCOPY(texres->nor, nor);
505
tex_normal_derivate(tex, texres);
507
if(tex->stype==TEX_WALLOUT) {
508
texres->nor[0]= -texres->nor[0];
509
texres->nor[1]= -texres->nor[1];
510
texres->nor[2]= -texres->nor[2];
516
if(tex->stype==TEX_WALLOUT)
517
texres->tin= 1.0f-texres->tin;
525
/* ------------------------------------------------------------------------- */
526
/* newnoise: musgrave terrain noise types */
528
static float mg_mFractalOrfBmTex(Tex *tex, float *texvec, TexResult *texres)
531
float (*mgravefunc)(float, float, float, float, float, float, int);
533
if (tex->stype==TEX_MFRACTAL)
534
mgravefunc = mg_MultiFractal;
538
texres->tin = tex->ns_outscale*mgravefunc(texvec[0], texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->noisebasis);
540
if (texres->nor!=NULL) {
541
float offs= tex->nabla/tex->noisesize; // also scaling of texvec
543
/* calculate bumpnormal */
544
texres->nor[0] = tex->ns_outscale*mgravefunc(texvec[0] + offs, texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->noisebasis);
545
texres->nor[1] = tex->ns_outscale*mgravefunc(texvec[0], texvec[1] + offs, texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->noisebasis);
546
texres->nor[2] = tex->ns_outscale*mgravefunc(texvec[0], texvec[1], texvec[2] + offs, tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->noisebasis);
548
tex_normal_derivate(tex, texres);
558
static float mg_ridgedOrHybridMFTex(Tex *tex, float *texvec, TexResult *texres)
561
float (*mgravefunc)(float, float, float, float, float, float, float, float, int);
563
if (tex->stype==TEX_RIDGEDMF)
564
mgravefunc = mg_RidgedMultiFractal;
566
mgravefunc = mg_HybridMultiFractal;
568
texres->tin = tex->ns_outscale*mgravefunc(texvec[0], texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->mg_gain, tex->noisebasis);
570
if (texres->nor!=NULL) {
571
float offs= tex->nabla/tex->noisesize; // also scaling of texvec
573
/* calculate bumpnormal */
574
texres->nor[0] = tex->ns_outscale*mgravefunc(texvec[0] + offs, texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->mg_gain, tex->noisebasis);
575
texres->nor[1] = tex->ns_outscale*mgravefunc(texvec[0], texvec[1] + offs, texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->mg_gain, tex->noisebasis);
576
texres->nor[2] = tex->ns_outscale*mgravefunc(texvec[0], texvec[1], texvec[2] + offs, tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->mg_gain, tex->noisebasis);
578
tex_normal_derivate(tex, texres);
589
static float mg_HTerrainTex(Tex *tex, float *texvec, TexResult *texres)
593
texres->tin = tex->ns_outscale*mg_HeteroTerrain(texvec[0], texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->noisebasis);
595
if (texres->nor!=NULL) {
596
float offs= tex->nabla/tex->noisesize; // also scaling of texvec
598
/* calculate bumpnormal */
599
texres->nor[0] = tex->ns_outscale*mg_HeteroTerrain(texvec[0] + offs, texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->noisebasis);
600
texres->nor[1] = tex->ns_outscale*mg_HeteroTerrain(texvec[0], texvec[1] + offs, texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->noisebasis);
601
texres->nor[2] = tex->ns_outscale*mg_HeteroTerrain(texvec[0], texvec[1], texvec[2] + offs, tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->noisebasis);
603
tex_normal_derivate(tex, texres);
614
static float mg_distNoiseTex(Tex *tex, float *texvec, TexResult *texres)
618
texres->tin = mg_VLNoise(texvec[0], texvec[1], texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
620
if (texres->nor!=NULL) {
621
float offs= tex->nabla/tex->noisesize; // also scaling of texvec
623
/* calculate bumpnormal */
624
texres->nor[0] = mg_VLNoise(texvec[0] + offs, texvec[1], texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
625
texres->nor[1] = mg_VLNoise(texvec[0], texvec[1] + offs, texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
626
texres->nor[2] = mg_VLNoise(texvec[0], texvec[1], texvec[2] + offs, tex->dist_amount, tex->noisebasis, tex->noisebasis2);
628
tex_normal_derivate(tex, texres);
640
/* ------------------------------------------------------------------------- */
641
/* newnoise: Voronoi texture type, probably the slowest, especially with minkovsky, bumpmapping, could be done another way */
643
static float voronoiTex(Tex *tex, float *texvec, TexResult *texres)
646
float da[4], pa[12]; /* distance and point coordinate arrays of 4 nearest neighbours */
647
float aw1 = fabs(tex->vn_w1);
648
float aw2 = fabs(tex->vn_w2);
649
float aw3 = fabs(tex->vn_w3);
650
float aw4 = fabs(tex->vn_w4);
651
float sc = (aw1 + aw2 + aw3 + aw4);
652
if (sc!=0.f) sc = tex->ns_outscale/sc;
654
voronoi(texvec[0], texvec[1], texvec[2], da, pa, tex->vn_mexp, tex->vn_distm);
655
texres->tin = sc * fabs(tex->vn_w1*da[0] + tex->vn_w2*da[1] + tex->vn_w3*da[2] + tex->vn_w4*da[3]);
657
if (tex->vn_coltype) {
658
float ca[3]; /* cell color */
659
cellNoiseV(pa[0], pa[1], pa[2], ca);
660
texres->tr = aw1*ca[0];
661
texres->tg = aw1*ca[1];
662
texres->tb = aw1*ca[2];
663
cellNoiseV(pa[3], pa[4], pa[5], ca);
664
texres->tr += aw2*ca[0];
665
texres->tg += aw2*ca[1];
666
texres->tb += aw2*ca[2];
667
cellNoiseV(pa[6], pa[7], pa[8], ca);
668
texres->tr += aw3*ca[0];
669
texres->tg += aw3*ca[1];
670
texres->tb += aw3*ca[2];
671
cellNoiseV(pa[9], pa[10], pa[11], ca);
672
texres->tr += aw4*ca[0];
673
texres->tg += aw4*ca[1];
674
texres->tb += aw4*ca[2];
675
if (tex->vn_coltype>=2) {
676
float t1 = (da[1]-da[0])*10;
678
if (tex->vn_coltype==3) t1*=texres->tin; else t1*=sc;
690
if (texres->nor!=NULL) {
691
float offs= tex->nabla/tex->noisesize; // also scaling of texvec
693
/* calculate bumpnormal */
694
voronoi(texvec[0] + offs, texvec[1], texvec[2], da, pa, tex->vn_mexp, tex->vn_distm);
695
texres->nor[0] = sc * fabs(tex->vn_w1*da[0] + tex->vn_w2*da[1] + tex->vn_w3*da[2] + tex->vn_w4*da[3]);
696
voronoi(texvec[0], texvec[1] + offs, texvec[2], da, pa, tex->vn_mexp, tex->vn_distm);
697
texres->nor[1] = sc * fabs(tex->vn_w1*da[0] + tex->vn_w2*da[1] + tex->vn_w3*da[2] + tex->vn_w4*da[3]);
698
voronoi(texvec[0], texvec[1], texvec[2] + offs, da, pa, tex->vn_mexp, tex->vn_distm);
699
texres->nor[2] = sc * fabs(tex->vn_w1*da[0] + tex->vn_w2*da[1] + tex->vn_w3*da[2] + tex->vn_w4*da[3]);
701
tex_normal_derivate(tex, texres);
705
if (tex->vn_coltype) {
708
return (rv | TEX_RGB);
717
/* ------------------------------------------------------------------------- */
719
static int texnoise(Tex *tex, TexResult *texres)
727
loop= tex->noisedepth;
734
texres->tin= ((float)val)/div;;
740
/* ------------------------------------------------------------------------- */
742
static int plugintex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres)
751
if(pit && pit->doit) {
753
if (pit->version < 6) {
754
VECCOPY(pit->result+5, texres->nor);
756
VECCOPY(result+5, texres->nor);
759
if (pit->version < 6) {
760
if(osatex) rgbnor= ((TexDoitold)pit->doit)(tex->stype,
761
pit->data, texvec, dxt, dyt);
762
else rgbnor= ((TexDoitold)pit->doit)(tex->stype,
763
pit->data, texvec, 0, 0);
765
if(osatex) rgbnor= ((TexDoit)pit->doit)(tex->stype,
766
pit->data, texvec, dxt, dyt, result);
767
else rgbnor= ((TexDoit)pit->doit)(tex->stype,
768
pit->data, texvec, 0, 0, result);
771
if (pit->version < 6) {
772
texres->tin = pit->result[0];
774
texres->tin = result[0];
777
if(rgbnor & TEX_NOR) {
779
if (pit->version < 6) {
780
VECCOPY(texres->nor, pit->result+5);
782
VECCOPY(texres->nor, result+5);
787
if(rgbnor & TEX_RGB) {
788
if (pit->version < 6) {
789
texres->tr = pit->result[1];
790
texres->tg = pit->result[2];
791
texres->tb = pit->result[3];
792
texres->ta = pit->result[4];
794
texres->tr = result[1];
795
texres->tg = result[2];
796
texres->tb = result[3];
797
texres->ta = result[4];
810
static int cubemap_glob(float *n, float x, float y, float z, float *adr1, float *adr2)
812
float x1, y1, z1, nor[3];
816
nor[0]= x; nor[1]= y; nor[2]= z; // use local render coord
821
mul_mat3_m4_v3(R.viewinv, nor);
827
if(z1>=x1 && z1>=y1) {
828
*adr1 = (x + 1.0) / 2.0;
829
*adr2 = (y + 1.0) / 2.0;
832
else if(y1>=x1 && y1>=z1) {
833
*adr1 = (x + 1.0) / 2.0;
834
*adr2 = (z + 1.0) / 2.0;
838
*adr1 = (y + 1.0) / 2.0;
839
*adr2 = (z + 1.0) / 2.0;
845
/* ------------------------------------------------------------------------- */
847
/* mtex argument only for projection switches */
848
static int cubemap(MTex *mtex, VlakRen *vlr, float *n, float x, float y, float z, float *adr1, float *adr2)
850
int proj[4]={0, ME_PROJXY, ME_PROJXZ, ME_PROJYZ}, ret= 0;
855
/* Mesh vertices have such flags, for others we calculate it once based on orco */
856
if((vlr->puno & (ME_PROJXY|ME_PROJXZ|ME_PROJYZ))==0) {
857
/* test for v1, vlr can be faked for baking */
858
if(vlr->v1 && vlr->v1->orco) {
860
normal_tri_v3( nor,vlr->v1->orco, vlr->v2->orco, vlr->v3->orco);
862
if( fabs(nor[0])<fabs(nor[2]) && fabs(nor[1])<fabs(nor[2]) ) vlr->puno |= ME_PROJXY;
863
else if( fabs(nor[0])<fabs(nor[1]) && fabs(nor[2])<fabs(nor[1]) ) vlr->puno |= ME_PROJXZ;
864
else vlr->puno |= ME_PROJYZ;
866
else return cubemap_glob(n, x, y, z, adr1, adr2);
870
/* the mtex->proj{xyz} have type char. maybe this should be wider? */
871
/* casting to int ensures that the index type is right. */
872
index = (int) mtex->projx;
873
proj[index]= ME_PROJXY;
875
index = (int) mtex->projy;
876
proj[index]= ME_PROJXZ;
878
index = (int) mtex->projz;
879
proj[index]= ME_PROJYZ;
882
if(vlr->puno & proj[1]) {
883
*adr1 = (x + 1.0) / 2.0;
884
*adr2 = (y + 1.0) / 2.0;
886
else if(vlr->puno & proj[2]) {
887
*adr1 = (x + 1.0) / 2.0;
888
*adr2 = (z + 1.0) / 2.0;
892
*adr1 = (y + 1.0) / 2.0;
893
*adr2 = (z + 1.0) / 2.0;
898
return cubemap_glob(n, x, y, z, adr1, adr2);
904
/* ------------------------------------------------------------------------- */
906
static int cubemap_ob(Object *ob, float *n, float x, float y, float z, float *adr1, float *adr2)
908
float x1, y1, z1, nor[3];
911
if(n==NULL) return 0;
914
if(ob) mul_mat3_m4_v3(ob->imat, nor);
920
if(z1>=x1 && z1>=y1) {
921
*adr1 = (x + 1.0) / 2.0;
922
*adr2 = (y + 1.0) / 2.0;
925
else if(y1>=x1 && y1>=z1) {
926
*adr1 = (x + 1.0) / 2.0;
927
*adr2 = (z + 1.0) / 2.0;
931
*adr1 = (y + 1.0) / 2.0;
932
*adr2 = (z + 1.0) / 2.0;
938
/* ------------------------------------------------------------------------- */
940
static void do_2d_mapping(MTex *mtex, float *t, VlakRen *vlr, float *n, float *dxt, float *dyt)
944
float fx, fy, fac1, area[8];
945
int ok, proj, areaflag= 0, wrap, texco;
947
/* mtex variables localized, only cubemap doesn't cooperate yet... */
955
if(wrap==MTEX_FLAT) {
956
fx = (t[0] + 1.0) / 2.0;
957
fy = (t[1] + 1.0) / 2.0;
959
else if(wrap==MTEX_TUBE) map_to_tube( &fx, &fy,t[0], t[1], t[2]);
960
else if(wrap==MTEX_SPHERE) map_to_sphere( &fx, &fy,t[0], t[1], t[2]);
962
if(texco==TEXCO_OBJECT) cubemap_ob(ob, n, t[0], t[1], t[2], &fx, &fy);
963
else if(texco==TEXCO_GLOB) cubemap_glob(n, t[0], t[1], t[2], &fx, &fy);
964
else cubemap(mtex, vlr, n, t[0], t[1], t[2], &fx, &fy);
968
if(tex->extend==TEX_REPEAT) {
970
float origf= fx *= tex->xrepeat;
972
if(fx>1.0) fx -= (int)(fx);
973
else if(fx<0.0) fx+= 1-(int)(fx);
975
if(tex->flag & TEX_REPEAT_XMIR) {
976
int orig= (int)floor(origf);
982
float origf= fy *= tex->yrepeat;
984
if(fy>1.0) fy -= (int)(fy);
985
else if(fy<0.0) fy+= 1-(int)(fy);
987
if(tex->flag & TEX_REPEAT_YMIR) {
988
int orig= (int)floor(origf);
995
if(tex->cropxmin!=0.0 || tex->cropxmax!=1.0) {
996
fac1= tex->cropxmax - tex->cropxmin;
997
fx= tex->cropxmin+ fx*fac1;
999
if(tex->cropymin!=0.0 || tex->cropymax!=1.0) {
1000
fac1= tex->cropymax - tex->cropymin;
1001
fy= tex->cropymin+ fy*fac1;
1009
if(wrap==MTEX_FLAT) {
1010
fx= (t[0] + 1.0) / 2.0;
1011
fy= (t[1] + 1.0) / 2.0;
1019
else if ELEM(wrap, MTEX_TUBE, MTEX_SPHERE) {
1020
/* exception: the seam behind (y<0.0) */
1025
if(fx>=0.0 && fy>=0.0 && t[0]>=0.0);
1026
else if(fx<=0.0 && fy<=0.0 && t[0]<=0.0);
1030
if(wrap==MTEX_TUBE) {
1031
map_to_tube( area, area+1,t[0], t[1], t[2]);
1032
map_to_tube( area+2, area+3,t[0]+dxt[0], t[1]+dxt[1], t[2]+dxt[2]);
1033
map_to_tube( area+4, area+5,t[0]+dyt[0], t[1]+dyt[1], t[2]+dyt[2]);
1036
map_to_sphere(area,area+1,t[0], t[1], t[2]);
1037
map_to_sphere( area+2, area+3,t[0]+dxt[0], t[1]+dxt[1], t[2]+dxt[2]);
1038
map_to_sphere( area+4, area+5,t[0]+dyt[0], t[1]+dyt[1], t[2]+dyt[2]);
1043
if(wrap==MTEX_TUBE) map_to_tube( &fx, &fy,t[0], t[1], t[2]);
1044
else map_to_sphere( &fx, &fy,t[0], t[1], t[2]);
1053
if(texco==TEXCO_OBJECT) proj = cubemap_ob(ob, n, t[0], t[1], t[2], &fx, &fy);
1054
else if (texco==TEXCO_GLOB) proj = cubemap_glob(n, t[0], t[1], t[2], &fx, &fy);
1055
else proj = cubemap(mtex, vlr, n, t[0], t[1], t[2], &fx, &fy);
1058
SWAP(float, dxt[1], dxt[2]);
1059
SWAP(float, dyt[1], dyt[2]);
1062
float f1= dxt[0], f2= dyt[0];
1081
/* if area, then reacalculate dxt[] and dyt[] */
1092
if(tex->extend==TEX_REPEAT) {
1094
if(tex->xrepeat>1) {
1095
float origf= fx *= tex->xrepeat;
1097
// TXF: omit mirror here, see comments in do_material_tex() after do_2d_mapping() call
1098
if (tex->texfilter == TXF_BOX) {
1099
if(fx>1.0f) fx -= (int)(fx);
1100
else if(fx<0.0f) fx+= 1-(int)(fx);
1102
if(tex->flag & TEX_REPEAT_XMIR) {
1103
int orig= (int)floor(origf);
1111
dxt[0]*= tex->xrepeat;
1112
dyt[0]*= tex->xrepeat;
1114
if(tex->yrepeat>1) {
1115
float origf= fy *= tex->yrepeat;
1117
// TXF: omit mirror here, see comments in do_material_tex() after do_2d_mapping() call
1118
if (tex->texfilter == TXF_BOX) {
1119
if(fy>1.0f) fy -= (int)(fy);
1120
else if(fy<0.0f) fy+= 1-(int)(fy);
1122
if(tex->flag & TEX_REPEAT_YMIR) {
1123
int orig= (int)floor(origf);
1129
if(max<tex->yrepeat)
1132
dxt[1]*= tex->yrepeat;
1133
dyt[1]*= tex->yrepeat;
1142
if(tex->cropxmin!=0.0 || tex->cropxmax!=1.0) {
1143
fac1= tex->cropxmax - tex->cropxmin;
1144
fx= tex->cropxmin+ fx*fac1;
1148
if(tex->cropymin!=0.0 || tex->cropymax!=1.0) {
1149
fac1= tex->cropymax - tex->cropymin;
1150
fy= tex->cropymin+ fy*fac1;
1161
/* ************************************** */
1163
static int multitex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres, short thread, short which_output)
1166
int retval=0; /* return value, int:0, col:1, nor:2, everything:3 */
1168
texres->talpha= 0; /* is set when image texture returns alpha (considered premul) */
1170
if(tex->use_nodes && tex->nodetree) {
1171
retval = ntreeTexExecTree(tex->nodetree, texres, texvec, dxt, dyt, osatex, thread,
1172
tex, which_output, R.r.cfra, (R.r.scemode & R_TEXNODE_PREVIEW) != 0, NULL, NULL);
1181
retval= clouds(tex, texvec, texres);
1184
retval= wood(tex, texvec, texres);
1187
retval= marble(tex, texvec, texres);
1190
retval= magic(tex, texvec, texres);
1193
retval= blend(tex, texvec, texres);
1196
retval= stucci(tex, texvec, texres);
1199
retval= texnoise(tex, texres);
1202
if(osatex) retval= imagewraposa(tex, tex->ima, NULL, texvec, dxt, dyt, texres);
1203
else retval= imagewrap(tex, tex->ima, NULL, texvec, texres);
1204
tag_image_time(tex->ima); /* tag image as having being used */
1207
retval= plugintex(tex, texvec, dxt, dyt, osatex, texres);
1210
retval= envmaptex(tex, texvec, dxt, dyt, osatex, texres);
1213
/* newnoise: musgrave types */
1215
/* ton: added this, for Blender convention reason.
1216
* artificer: added the use of tmpvec to avoid scaling texvec
1218
VECCOPY(tmpvec, texvec);
1219
mul_v3_fl(tmpvec, 1.0/tex->noisesize);
1221
switch(tex->stype) {
1224
retval= mg_mFractalOrfBmTex(tex, tmpvec, texres);
1228
retval= mg_ridgedOrHybridMFTex(tex, tmpvec, texres);
1231
retval= mg_HTerrainTex(tex, tmpvec, texres);
1235
/* newnoise: voronoi type */
1237
/* ton: added this, for Blender convention reason.
1238
* artificer: added the use of tmpvec to avoid scaling texvec
1240
VECCOPY(tmpvec, texvec);
1241
mul_v3_fl(tmpvec, 1.0/tex->noisesize);
1243
retval= voronoiTex(tex, tmpvec, texres);
1246
/* ton: added this, for Blender convention reason.
1247
* artificer: added the use of tmpvec to avoid scaling texvec
1249
VECCOPY(tmpvec, texvec);
1250
mul_v3_fl(tmpvec, 1.0/tex->noisesize);
1252
retval= mg_distNoiseTex(tex, tmpvec, texres);
1254
case TEX_POINTDENSITY:
1255
retval= pointdensitytex(tex, texvec, texres);
1258
retval= voxeldatatex(tex, texvec, texres);
1263
if (tex->flag & TEX_COLORBAND) {
1265
if (do_colorband(tex->coba, texres->tin, col)) {
1277
/* this is called from the shader and texture nodes */
1278
int multitex_nodes(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres, short thread, short which_output, ShadeInput *shi, MTex *mtex)
1281
memset(texres, 0, sizeof(TexResult));
1286
which_output= mtex->which_output;
1288
if(tex->type==TEX_IMAGE) {
1292
/* we have mtex, use it for 2d mapping images only */
1293
do_2d_mapping(mtex, texvec, shi->vlr, shi->facenor, dxt, dyt);
1294
rgbnor= multitex(tex, texvec, dxt, dyt, osatex, texres, thread, which_output);
1296
if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
1297
ImBuf *ibuf = BKE_image_get_ibuf(tex->ima, &tex->iuser);
1299
/* don't linearize float buffers, assumed to be linear */
1300
if(ibuf && !(ibuf->rect_float) && R.r.color_mgt_flag & R_COLOR_MANAGEMENT)
1301
srgb_to_linearrgb_v3_v3(&texres->tr, &texres->tr);
1305
/* we don't have mtex, do default flat 2d projection */
1307
float texvec_l[3], dxt_l[3], dyt_l[3];
1309
localmtex.mapping= MTEX_FLAT;
1311
localmtex.object= NULL;
1312
localmtex.texco= TEXCO_ORCO;
1314
copy_v3_v3(texvec_l, texvec);
1316
copy_v3_v3(dxt_l, dxt);
1317
copy_v3_v3(dyt_l, dyt);
1324
do_2d_mapping(&localmtex, texvec_l, NULL, NULL, dxt_l, dyt_l);
1325
rgbnor= multitex(tex, texvec_l, dxt_l, dyt_l, osatex, texres, thread, which_output);
1331
return multitex(tex, texvec, dxt, dyt, osatex, texres, thread, which_output);
1334
/* this is called for surface shading */
1335
int multitex_mtex(ShadeInput *shi, MTex *mtex, float *texvec, float *dxt, float *dyt, TexResult *texres)
1337
Tex *tex= mtex->tex;
1339
if(tex->use_nodes && tex->nodetree) {
1340
/* stupid exception here .. but we have to pass shi and mtex to
1341
textures nodes for 2d mapping and color management for images */
1342
return ntreeTexExecTree(tex->nodetree, texres, texvec, dxt, dyt, shi->osatex, shi->thread,
1343
tex, mtex->which_output, R.r.cfra, (R.r.scemode & R_TEXNODE_PREVIEW) != 0, shi, mtex);
1346
return multitex(mtex->tex, texvec, dxt, dyt, shi->osatex, texres, shi->thread, mtex->which_output);
1349
/* Warning, if the texres's values are not declared zero, check the return value to be sure
1350
* the color values are set before using the r/g/b values, otherwise you may use uninitialized values - Campbell */
1351
int multitex_ext(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres)
1353
return multitex_nodes(tex, texvec, dxt, dyt, osatex, texres, 0, 0, NULL, NULL);
1356
/* ------------------------------------------------------------------------- */
1358
/* in = destination, tex = texture, out = previous color */
1359
/* fact = texture strength, facg = button strength value */
1360
void texture_rgb_blend(float *in, float *tex, float *out, float fact, float facg, int blendtype)
1369
in[0]= (fact*tex[0] + facm*out[0]);
1370
in[1]= (fact*tex[1] + facm*out[1]);
1371
in[2]= (fact*tex[2] + facm*out[2]);
1377
in[0]= (facm+fact*tex[0])*out[0];
1378
in[1]= (facm+fact*tex[1])*out[1];
1379
in[2]= (facm+fact*tex[2])*out[2];
1385
in[0]= 1.0 - (facm+fact*(1.0-tex[0])) * (1.0-out[0]);
1386
in[1]= 1.0 - (facm+fact*(1.0-tex[1])) * (1.0-out[1]);
1387
in[2]= 1.0 - (facm+fact*(1.0-tex[2])) * (1.0-out[2]);
1395
in[0] = out[0] * (facm + 2.0f*fact*tex[0]);
1397
in[0] = 1.0f - (facm + 2.0f*fact*(1.0 - tex[0])) * (1.0 - out[0]);
1399
in[1] = out[1] * (facm + 2.0f*fact*tex[1]);
1401
in[1] = 1.0f - (facm + 2.0f*fact*(1.0 - tex[1])) * (1.0 - out[1]);
1403
in[2] = out[2] * (facm + 2.0f*fact*tex[2]);
1405
in[2] = 1.0f - (facm + 2.0f*fact*(1.0 - tex[2])) * (1.0 - out[2]);
1412
in[0]= (fact*tex[0] + out[0]);
1413
in[1]= (fact*tex[1] + out[1]);
1414
in[2]= (fact*tex[2] + out[2]);
1422
in[0]= facm*out[0] + fact*out[0]/tex[0];
1424
in[1]= facm*out[1] + fact*out[1]/tex[1];
1426
in[2]= facm*out[2] + fact*out[2]/tex[2];
1433
in[0]= facm*out[0] + fact*fabs(tex[0]-out[0]);
1434
in[1]= facm*out[1] + fact*fabs(tex[1]-out[1]);
1435
in[2]= facm*out[2] + fact*fabs(tex[2]-out[2]);
1442
col= tex[0]+((1-tex[0])*facm);
1443
if(col < out[0]) in[0]= col; else in[0]= out[0];
1444
col= tex[1]+((1-tex[1])*facm);
1445
if(col < out[1]) in[1]= col; else in[1]= out[1];
1446
col= tex[2]+((1-tex[2])*facm);
1447
if(col < out[2]) in[2]= col; else in[2]= out[2];
1455
if(col > out[0]) in[0]= col; else in[0]= out[0];
1457
if(col > out[1]) in[1]= col; else in[1]= out[1];
1459
if(col > out[2]) in[2]= col; else in[2]= out[2];
1462
case MTEX_BLEND_HUE:
1465
ramp_blend(MA_RAMP_HUE, in, in+1, in+2, fact, tex);
1467
case MTEX_BLEND_SAT:
1470
ramp_blend(MA_RAMP_SAT, in, in+1, in+2, fact, tex);
1472
case MTEX_BLEND_VAL:
1475
ramp_blend(MA_RAMP_VAL, in, in+1, in+2, fact, tex);
1477
case MTEX_BLEND_COLOR:
1480
ramp_blend(MA_RAMP_COLOR, in, in+1, in+2, fact, tex);
1482
case MTEX_SOFT_LIGHT:
1485
ramp_blend(MA_RAMP_SOFT, in, in+1, in+2, fact, tex);
1487
case MTEX_LIN_LIGHT:
1490
ramp_blend(MA_RAMP_LINEAR, in, in+1, in+2, fact, tex);
1495
float texture_value_blend(float tex, float out, float fact, float facg, int blendtype)
1497
float in=0.0, facm, col, scf;
1498
int flip= (facg < 0.0f);
1504
if(flip) SWAP(float, fact, facm);
1508
in= fact*tex + facm*out;
1513
in= (facm+fact*tex)*out;
1518
in= 1.0-(facm+fact*(1.0-tex))*(1.0-out);
1524
in = out * (facm + 2.0f*fact*tex);
1526
in = 1.0f - (facm + 2.0f*fact*(1.0 - tex)) * (1.0 - out);
1537
in= facm*out + fact*out/tex;
1541
in= facm*out + fact*fabs(tex-out);
1546
if(col < out) in= col; else in= out;
1551
if(col > out) in= col; else in= out;
1554
case MTEX_SOFT_LIGHT:
1556
scf=1.0 - (1.0 - tex) * (1.0 - out);
1557
in= facm*out + fact * ((1.0 - out) * tex * out) + (out * scf);
1560
case MTEX_LIN_LIGHT:
1562
in = out + fact*(2*(tex - 0.5));
1564
in = out + fact*(2*tex - 1);
1571
static void texco_mapping(ShadeInput* shi, Tex* tex, MTex* mtex, float* co, float* dx, float* dy, float* texvec, float* dxt, float* dyt)
1573
// new: first swap coords, then map, then trans/scale
1574
if (tex->type == TEX_IMAGE) {
1576
texvec[0] = mtex->projx ? co[mtex->projx - 1] : 0.f;
1577
texvec[1] = mtex->projy ? co[mtex->projy - 1] : 0.f;
1578
texvec[2] = mtex->projz ? co[mtex->projz - 1] : 0.f;
1582
dxt[0] = dx[mtex->projx - 1];
1583
dyt[0] = dy[mtex->projx - 1];
1585
else dxt[0] = dyt[0] = 0.f;
1587
dxt[1] = dx[mtex->projy - 1];
1588
dyt[1] = dy[mtex->projy - 1];
1590
else dxt[1] = dyt[1] = 0.f;
1592
dxt[2] = dx[mtex->projz - 1];
1593
dyt[2] = dy[mtex->projz - 1];
1595
else dxt[2] = dyt[2] = 0.f;
1597
do_2d_mapping(mtex, texvec, shi->vlr, shi->facenor, dxt, dyt);
1599
// translate and scale
1600
texvec[0] = mtex->size[0]*(texvec[0] - 0.5f) + mtex->ofs[0] + 0.5f;
1601
texvec[1] = mtex->size[1]*(texvec[1] - 0.5f) + mtex->ofs[1] + 0.5f;
1603
dxt[0] = mtex->size[0]*dxt[0];
1604
dxt[1] = mtex->size[1]*dxt[1];
1605
dyt[0] = mtex->size[0]*dyt[0];
1606
dyt[1] = mtex->size[1]*dyt[1];
1609
/* problem: repeat-mirror is not a 'repeat' but 'extend' in imagetexture.c */
1610
// TXF: bug was here, only modify texvec when repeat mode set, old code affected other modes too.
1611
// New texfilters solve mirroring differently so that it also works correctly when
1612
// textures are scaled (sizeXYZ) as well as repeated. See also modification in do_2d_mapping().
1613
// (since currently only done in osa mode, results will look incorrect without osa TODO)
1614
if (tex->extend == TEX_REPEAT && (tex->flag & TEX_REPEAT_XMIR)) {
1615
if (tex->texfilter == TXF_BOX)
1616
texvec[0] -= floorf(texvec[0]); // this line equivalent to old code, same below
1617
else if (texvec[0] < 0.f || texvec[0] > 1.f) {
1618
const float tx = 0.5f*texvec[0];
1619
texvec[0] = 2.f*(tx - floorf(tx));
1620
if (texvec[0] > 1.f) texvec[0] = 2.f - texvec[0];
1623
if (tex->extend == TEX_REPEAT && (tex->flag & TEX_REPEAT_YMIR)) {
1624
if (tex->texfilter == TXF_BOX)
1625
texvec[1] -= floorf(texvec[1]);
1626
else if (texvec[1] < 0.f || texvec[1] > 1.f) {
1627
const float ty = 0.5f*texvec[1];
1628
texvec[1] = 2.f*(ty - floorf(ty));
1629
if (texvec[1] > 1.f) texvec[1] = 2.f - texvec[1];
1634
else { // procedural
1636
texvec[0] = mtex->size[0]*(mtex->projx ? (co[mtex->projx - 1] + mtex->ofs[0]) : mtex->ofs[0]);
1637
texvec[1] = mtex->size[1]*(mtex->projy ? (co[mtex->projy - 1] + mtex->ofs[1]) : mtex->ofs[1]);
1638
texvec[2] = mtex->size[2]*(mtex->projz ? (co[mtex->projz - 1] + mtex->ofs[2]) : mtex->ofs[2]);
1642
dxt[0] = mtex->size[0]*dx[mtex->projx - 1];
1643
dyt[0] = mtex->size[0]*dy[mtex->projx - 1];
1645
else dxt[0] = dyt[0] = 0.f;
1647
dxt[1] = mtex->size[1]*dx[mtex->projy - 1];
1648
dyt[1] = mtex->size[1]*dy[mtex->projy - 1];
1650
else dxt[1] = dyt[1] = 0.f;
1652
dxt[2] = mtex->size[2]*dx[mtex->projz - 1];
1653
dyt[2] = mtex->size[2]*dy[mtex->projz - 1];
1655
else dxt[2]= dyt[2] = 0.f;
1660
void do_material_tex(ShadeInput *shi)
1664
TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
1665
float *co = NULL, *dx = NULL, *dy = NULL;
1666
float fact, facm, factt, facmm, stencilTin=1.0;
1667
float texvec[3], dxt[3], dyt[3], tempvec[3], norvec[3], warpvec[3]={0.0f, 0.0f, 0.0f}, Tnor=1.0;
1668
int tex_nr, rgbnor= 0, warpdone=0;
1669
float nu[3] = {0,0,0}, nv[3] = {0,0,0}, nn[3] = {0,0,0}, dudnu = 1.f, dudnv = 0.f, dvdnu = 0.f, dvdnv = 1.f; // bump mapping
1672
if (R.r.scemode & R_NO_TEX) return;
1673
/* here: test flag if there's a tex (todo) */
1675
for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
1677
/* separate tex switching */
1678
if(shi->mat->septex & (1<<tex_nr)) continue;
1680
if(shi->mat->mtex[tex_nr]) {
1681
mtex= shi->mat->mtex[tex_nr];
1684
if(tex==0) continue;
1687
if(mtex->texco==TEXCO_ORCO) {
1688
if(mtex->texflag & MTEX_DUPLI_MAPTO) {
1689
co= shi->duplilo; dx= dxt; dy= dyt;
1690
dxt[0]= dxt[1]= dxt[2]= 0.0f;
1691
dyt[0]= dyt[1]= dyt[2]= 0.0f;
1694
co= shi->lo; dx= shi->dxlo; dy= shi->dylo;
1697
else if(mtex->texco==TEXCO_STICKY) {
1698
co= shi->sticky; dx= shi->dxsticky; dy= shi->dysticky;
1700
else if(mtex->texco==TEXCO_OBJECT) {
1701
Object *ob= mtex->object;
1706
VECCOPY(tempvec, shi->co);
1707
if(mtex->texflag & MTEX_OB_DUPLI_ORIG)
1708
if(shi->obi && shi->obi->duplitexmat)
1709
mul_m4_v3(shi->obi->duplitexmat, tempvec);
1710
mul_m4_v3(ob->imat, tempvec);
1712
VECCOPY(dxt, shi->dxco);
1713
VECCOPY(dyt, shi->dyco);
1714
mul_mat3_m4_v3(ob->imat, dxt);
1715
mul_mat3_m4_v3(ob->imat, dyt);
1719
/* if object doesn't exist, do not use orcos (not initialized) */
1721
dx= shi->dxco; dy= shi->dyco;
1724
else if(mtex->texco==TEXCO_REFL) {
1725
co= shi->ref; dx= shi->dxref; dy= shi->dyref;
1727
else if(mtex->texco==TEXCO_NORM) {
1728
co= shi->orn; dx= shi->dxno; dy= shi->dyno;
1730
else if(mtex->texco==TEXCO_TANGENT) {
1731
co= shi->tang; dx= shi->dxno; dy= shi->dyno;
1733
else if(mtex->texco==TEXCO_GLOB) {
1734
co= shi->gl; dx= shi->dxco; dy= shi->dyco;
1736
else if(mtex->texco==TEXCO_UV) {
1737
if(mtex->texflag & MTEX_DUPLI_MAPTO) {
1738
co= shi->dupliuv; dx= dxt; dy= dyt;
1739
dxt[0]= dxt[1]= dxt[2]= 0.0f;
1740
dyt[0]= dyt[1]= dyt[2]= 0.0f;
1743
ShadeInputUV *suv= &shi->uv[shi->actuv];
1746
if(mtex->uvname[0] != 0) {
1747
for(i = 0; i < shi->totuv; i++) {
1748
if(strcmp(shi->uv[i].name, mtex->uvname)==0) {
1759
// uvmapping only, calculation of normal tangent u/v partial derivatives
1760
// (should not be here, dudnu, dudnv, dvdnu & dvdnv should probably be part of ShadeInputUV struct,
1761
// nu/nv in ShadeInput and this calculation should then move to shadeinput.c, shade_input_set_shade_texco() func.)
1762
// NOTE: test for shi->obr->ob here, since vlr/obr/obi can be 'fake' when called from fastshade(), another reason to move it..
1763
// NOTE: shi->v1 is NULL when called from displace_render_vert, assigning verts in this case is not trivial because the shi quad face side is not know.
1764
if ((mtex->texflag & MTEX_NEW_BUMP) && shi->obr && shi->obr->ob && shi->v1) {
1765
if(mtex->mapto & (MAP_NORM|MAP_WARP) && !((tex->type==TEX_IMAGE) && (tex->imaflag & TEX_NORMALMAP))) {
1766
MTFace* tf = RE_vlakren_get_tface(shi->obr, shi->vlr, i, NULL, 0);
1767
int j1 = shi->i1, j2 = shi->i2, j3 = shi->i3;
1769
vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
1771
// compute ortho basis around normal
1773
// render normal is negated
1774
nn[0] = -shi->vn[0];
1775
nn[1] = -shi->vn[1];
1776
nn[2] = -shi->vn[2];
1777
ortho_basis_v3v3_v3( nu, nv,nn);
1782
float *uv1 = tf->uv[j1], *uv2 = tf->uv[j2], *uv3 = tf->uv[j3];
1783
const float an[3] = {fabsf(nn[0]), fabsf(nn[1]), fabsf(nn[2])};
1784
const int a1 = (an[0] > an[1] && an[0] > an[2]) ? 1 : 0;
1785
const int a2 = (an[2] > an[0] && an[2] > an[1]) ? 1 : 2;
1786
const float dp1_a1 = shi->v1->co[a1] - shi->v3->co[a1];
1787
const float dp1_a2 = shi->v1->co[a2] - shi->v3->co[a2];
1788
const float dp2_a1 = shi->v2->co[a1] - shi->v3->co[a1];
1789
const float dp2_a2 = shi->v2->co[a2] - shi->v3->co[a2];
1790
const float du1 = uv1[0] - uv3[0], du2 = uv2[0] - uv3[0];
1791
const float dv1 = uv1[1] - uv3[1], dv2 = uv2[1] - uv3[1];
1792
const float dpdu_a1 = dv2*dp1_a1 - dv1*dp2_a1;
1793
const float dpdu_a2 = dv2*dp1_a2 - dv1*dp2_a2;
1794
const float dpdv_a1 = du1*dp2_a1 - du2*dp1_a1;
1795
const float dpdv_a2 = du1*dp2_a2 - du2*dp1_a2;
1796
float d = dpdu_a1*dpdv_a2 - dpdv_a1*dpdu_a2;
1797
float uvd = du1*dv2 - dv1*du2;
1799
if (uvd == 0.f) uvd = 1e-5f;
1800
if (d == 0.f) d = 1e-5f;
1803
dudnu = (dpdv_a2*nu[a1] - dpdv_a1*nu[a2])*d;
1804
dvdnu = (dpdu_a1*nu[a2] - dpdu_a2*nu[a1])*d;
1805
dudnv = (dpdv_a2*nv[a1] - dpdv_a1*nv[a2])*d;
1806
dvdnv = (dpdu_a1*nv[a2] - dpdu_a2*nv[a1])*d;
1812
else if(mtex->texco==TEXCO_WINDOW) {
1813
co= shi->winco; dx= shi->dxwin; dy= shi->dywin;
1815
else if(mtex->texco==TEXCO_STRAND) {
1816
co= tempvec; dx= dxt; dy= dyt;
1817
co[0]= shi->strandco;
1819
dx[0]= shi->dxstrand;
1821
dy[0]= shi->dystrand;
1824
else if(mtex->texco==TEXCO_STRESS) {
1825
co= tempvec; dx= dxt; dy= dyt;
1833
else continue; // can happen when texco defines disappear and it renders old files
1835
/* the pointer defines if bumping happens */
1836
if(mtex->mapto & (MAP_NORM|MAP_WARP)) {
1838
norvec[0]= norvec[1]= norvec[2]= 0.0;
1840
else texres.nor= NULL;
1843
VECADD(tempvec, co, warpvec);
1847
if(mtex->texflag & MTEX_NEW_BUMP) {
1848
// compute ortho basis around normal
1850
// render normal is negated
1851
nn[0] = -shi->vn[0];
1852
nn[1] = -shi->vn[1];
1853
nn[2] = -shi->vn[2];
1854
ortho_basis_v3v3_v3( nu, nv,nn);
1858
if(texres.nor && !((tex->type==TEX_IMAGE) && (tex->imaflag & TEX_NORMALMAP))) {
1859
TexResult ttexr = {0, 0, 0, 0, 0, texres.talpha, NULL}; // temp TexResult
1860
float tco[3], texv[3], cd, ud, vd, du, dv, idu, idv;
1861
const int fromrgb = ((tex->type == TEX_IMAGE) || ((tex->flag & TEX_COLORBAND)!=0));
1862
const float bf = 0.04f*Tnor*stencilTin*mtex->norfac;
1863
// disable internal bump eval
1864
float* nvec = texres.nor;
1866
// du & dv estimates, constant value defaults
1869
// two methods, either constant based on main image resolution,
1870
// (which also works without osa, though of course not always good (or even very bad) results),
1871
// or based on tex derivative max values (osa only). Not sure which is best...
1873
if (!shi->osatex && (tex->type == TEX_IMAGE) && tex->ima) {
1874
// in case we have no proper derivatives, fall back to
1875
// computing du/dv it based on image size
1876
ImBuf* ibuf = BKE_image_get_ibuf(tex->ima, &tex->iuser);
1878
du = 1.f/(float)ibuf->x;
1879
dv = 1.f/(float)ibuf->y;
1882
else if (shi->osatex) {
1883
// we have derivatives, can compute proper du/dv
1884
if (tex->type == TEX_IMAGE) { // 2d image, use u & v max. of dx/dy 2d vecs
1885
const float adx[2] = {fabsf(dx[0]), fabsf(dx[1])};
1886
const float ady[2] = {fabsf(dy[0]), fabsf(dy[1])};
1887
du = MAX2(adx[0], ady[0]);
1888
dv = MAX2(adx[1], ady[1]);
1890
else { // 3d procedural, estimate from all dx/dy elems
1891
const float adx[3] = {fabsf(dx[0]), fabsf(dx[1]), fabsf(dx[2])};
1892
const float ady[3] = {fabsf(dy[0]), fabsf(dy[1]), fabsf(dy[2])};
1893
du = MAX3(adx[0], adx[1], adx[2]);
1894
dv = MAX3(ady[1], ady[1], ady[2]);
1898
// center, main return value
1899
texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
1900
rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, &texres);
1901
cd = fromrgb ? (texres.tr + texres.tg + texres.tb)*0.33333333f : texres.tin;
1903
if (mtex->texco == TEXCO_UV) {
1904
// for the uv case, use the same value for both du/dv,
1905
// since individually scaling the normal derivatives makes them useless...
1907
idu = (du < 1e-5f) ? bf : (bf/du);
1910
tco[0] = co[0] + dudnu*du;
1911
tco[1] = co[1] + dvdnu*du;
1913
texco_mapping(shi, tex, mtex, tco, dx, dy, texv, dxt, dyt);
1914
multitex_mtex(shi, mtex, texv, dxt, dyt, &ttexr);
1915
ud = idu*(cd - (fromrgb ? (ttexr.tr + ttexr.tg + ttexr.tb)*0.33333333f : ttexr.tin));
1918
tco[0] = co[0] + dudnv*du;
1919
tco[1] = co[1] + dvdnv*du;
1921
texco_mapping(shi, tex, mtex, tco, dx, dy, texv, dxt, dyt);
1922
multitex_mtex(shi, mtex, texv, dxt, dyt, &ttexr);
1923
vd = idu*(cd - (fromrgb ? (ttexr.tr + ttexr.tg + ttexr.tb)*0.33333333f : ttexr.tin));
1926
float tu[3] = {nu[0], nu[1], nu[2]}, tv[3] = {nv[0], nv[1], nv[2]};
1928
idu = (du < 1e-5f) ? bf : (bf/du);
1929
idv = (dv < 1e-5f) ? bf : (bf/dv);
1931
if ((mtex->texco == TEXCO_ORCO) && shi->obr && shi->obr->ob) {
1932
mul_mat3_m4_v3(shi->obr->ob->imat, tu);
1933
mul_mat3_m4_v3(shi->obr->ob->imat, tv);
1937
else if (mtex->texco == TEXCO_GLOB) {
1938
mul_mat3_m4_v3(R.viewinv, tu);
1939
mul_mat3_m4_v3(R.viewinv, tv);
1941
else if (mtex->texco == TEXCO_OBJECT && mtex->object) {
1942
mul_mat3_m4_v3(mtex->object->imat, tu);
1943
mul_mat3_m4_v3(mtex->object->imat, tv);
1949
tco[0] = co[0] + tu[0]*du;
1950
tco[1] = co[1] + tu[1]*du;
1951
tco[2] = co[2] + tu[2]*du;
1952
texco_mapping(shi, tex, mtex, tco, dx, dy, texv, dxt, dyt);
1953
multitex_mtex(shi, mtex, texv, dxt, dyt, &ttexr);
1954
ud = idu*(cd - (fromrgb ? (ttexr.tr + ttexr.tg + ttexr.tb)*0.33333333f : ttexr.tin));
1957
tco[0] = co[0] + tv[0]*dv;
1958
tco[1] = co[1] + tv[1]*dv;
1959
tco[2] = co[2] + tv[2]*dv;
1960
texco_mapping(shi, tex, mtex, tco, dx, dy, texv, dxt, dyt);
1961
multitex_mtex(shi, mtex, texv, dxt, dyt, &ttexr);
1962
vd = idv*(cd - (fromrgb ? (ttexr.tr + ttexr.tg + ttexr.tb)*0.33333333f : ttexr.tin));
1972
cross_v3_v3v3(nvec, nu, nv);
1981
texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
1982
rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, &texres);
1986
texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
1987
rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, &texres);
1990
/* texture output */
1992
if( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
1993
texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
1996
if(mtex->texflag & MTEX_NEGATIVE) {
1997
if(rgbnor & TEX_RGB) {
1998
texres.tr= 1.0-texres.tr;
1999
texres.tg= 1.0-texres.tg;
2000
texres.tb= 1.0-texres.tb;
2002
texres.tin= 1.0-texres.tin;
2004
if(mtex->texflag & MTEX_STENCIL) {
2005
if(rgbnor & TEX_RGB) {
2007
texres.ta*= stencilTin;
2012
texres.tin*= stencilTin;
2021
if((rgbnor & TEX_NOR)==0) {
2022
/* make our own normal */
2023
if(rgbnor & TEX_RGB) {
2024
texres.nor[0]= texres.tr;
2025
texres.nor[1]= texres.tg;
2026
texres.nor[2]= texres.tb;
2029
float co_nor= 0.5*cos(texres.tin-0.5);
2030
float si= 0.5*sin(texres.tin-0.5);
2035
texres.nor[0]= f1*co_nor+f2*si;
2036
texres.nor[1]= f2*co_nor-f1*si;
2039
texres.nor[1]= f1*co_nor+f2*si;
2040
texres.nor[2]= f2*co_nor-f1*si;
2043
// warping, local space
2044
if(mtex->mapto & MAP_WARP) {
2045
warpvec[0]= mtex->warpfac*texres.nor[0];
2046
warpvec[1]= mtex->warpfac*texres.nor[1];
2047
warpvec[2]= mtex->warpfac*texres.nor[2];
2051
if(mtex->texflag & MTEX_VIEWSPACE) {
2052
// rotate to global coords
2053
if(mtex->texco==TEXCO_ORCO || mtex->texco==TEXCO_UV) {
2054
if(shi->vlr && shi->obr && shi->obr->ob) {
2055
float len= normalize_v3(texres.nor);
2056
// can be optimized... (ton)
2057
mul_mat3_m4_v3(shi->obr->ob->obmat, texres.nor);
2058
mul_mat3_m4_v3(R.viewmat, texres.nor);
2059
normalize_v3(texres.nor);
2060
mul_v3_fl(texres.nor, len);
2068
if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
2071
/* stencil maps on the texture control slider, not texture intensity value */
2073
tcol[0]=texres.tr; tcol[1]=texres.tg; tcol[2]=texres.tb;
2075
if((rgbnor & TEX_RGB)==0) {
2080
else if(mtex->mapto & MAP_ALPHA) {
2081
texres.tin= stencilTin;
2083
else texres.tin= texres.ta;
2085
/* inverse gamma correction */
2086
if (tex->type==TEX_IMAGE) {
2087
Image *ima = tex->ima;
2088
ImBuf *ibuf = BKE_image_get_ibuf(ima, &tex->iuser);
2090
/* don't linearize float buffers, assumed to be linear */
2091
if (ibuf && !(ibuf->rect_float) && R.r.color_mgt_flag & R_COLOR_MANAGEMENT)
2092
srgb_to_linearrgb_v3_v3(tcol, tcol);
2095
if(mtex->mapto & MAP_COL) {
2096
float colfac= mtex->colfac*stencilTin;
2097
texture_rgb_blend(&shi->r, tcol, &shi->r, texres.tin, colfac, mtex->blendtype);
2099
if(mtex->mapto & MAP_COLSPEC) {
2100
float colspecfac= mtex->colspecfac*stencilTin;
2101
texture_rgb_blend(&shi->specr, tcol, &shi->specr, texres.tin, colspecfac, mtex->blendtype);
2103
if(mtex->mapto & MAP_COLMIR) {
2104
float mirrfac= mtex->mirrfac*stencilTin;
2106
// exception for envmap only
2107
if(tex->type==TEX_ENVMAP && mtex->blendtype==MTEX_BLEND) {
2108
fact= texres.tin*mirrfac;
2110
shi->refcol[0]= fact + facm*shi->refcol[0];
2111
shi->refcol[1]= fact*tcol[0] + facm*shi->refcol[1];
2112
shi->refcol[2]= fact*tcol[1] + facm*shi->refcol[2];
2113
shi->refcol[3]= fact*tcol[2] + facm*shi->refcol[3];
2116
texture_rgb_blend(&shi->mirr, tcol, &shi->mirr, texres.tin, mirrfac, mtex->blendtype);
2120
if( (mtex->mapto & MAP_NORM) ) {
2122
float norfac= mtex->norfac;
2124
/* we need to code blending modes for normals too once.. now 1 exception hardcoded */
2126
if ((tex->type==TEX_IMAGE) && (tex->imaflag & TEX_NORMALMAP)) {
2127
/* qdn: for normalmaps, to invert the normalmap vector,
2128
it is better to negate x & y instead of subtracting the vector as was done before */
2129
if (norfac < 0.0f) {
2130
texres.nor[0] = -texres.nor[0];
2131
texres.nor[1] = -texres.nor[1];
2133
fact = Tnor*fabs(norfac);
2134
if (fact>1.f) fact = 1.f;
2136
if(mtex->normapspace == MTEX_NSPACE_TANGENT) {
2137
/* qdn: tangent space */
2139
cross_v3_v3v3(B, shi->vn, shi->nmaptang); /* bitangent */
2140
/* transform norvec from tangent space to object surface in camera space */
2141
tv[0] = texres.nor[0]*shi->nmaptang[0] + texres.nor[1]*B[0] + texres.nor[2]*shi->vn[0];
2142
tv[1] = texres.nor[0]*shi->nmaptang[1] + texres.nor[1]*B[1] + texres.nor[2]*shi->vn[1];
2143
tv[2] = texres.nor[0]*shi->nmaptang[2] + texres.nor[1]*B[2] + texres.nor[2]*shi->vn[2];
2144
shi->vn[0]= facm*shi->vn[0] + fact*tv[0];
2145
shi->vn[1]= facm*shi->vn[1] + fact*tv[1];
2146
shi->vn[2]= facm*shi->vn[2] + fact*tv[2];
2151
VECCOPY(nor, texres.nor);
2153
if(mtex->normapspace == MTEX_NSPACE_CAMERA);
2154
else if(mtex->normapspace == MTEX_NSPACE_WORLD) {
2155
mul_mat3_m4_v3(R.viewmat, nor);
2157
else if(mtex->normapspace == MTEX_NSPACE_OBJECT) {
2158
if(shi->obr && shi->obr->ob)
2159
mul_mat3_m4_v3(shi->obr->ob->obmat, nor);
2160
mul_mat3_m4_v3(R.viewmat, nor);
2165
/* qdn: worldspace */
2166
shi->vn[0]= facm*shi->vn[0] + fact*nor[0];
2167
shi->vn[1]= facm*shi->vn[1] + fact*nor[1];
2168
shi->vn[2]= facm*shi->vn[2] + fact*nor[2];
2172
if (mtex->texflag & MTEX_NEW_BUMP) {
2173
shi->vn[0] = texres.nor[0];
2174
shi->vn[1] = texres.nor[1];
2175
shi->vn[2] = texres.nor[2];
2180
if(shi->mat->mode & MA_TANGENT_V) {
2181
shi->tang[0]+= Tnor*norfac*texres.nor[0];
2182
shi->tang[1]+= Tnor*norfac*texres.nor[1];
2183
shi->tang[2]+= Tnor*norfac*texres.nor[2];
2186
/* prevent bump to become negative normal */
2187
nor[0]= Tnor*norfac*texres.nor[0];
2188
nor[1]= Tnor*norfac*texres.nor[1];
2189
nor[2]= Tnor*norfac*texres.nor[2];
2191
dot= 0.5f + 0.5f*INPR(nor, shi->vn);
2193
shi->vn[0]+= dot*nor[0];
2194
shi->vn[1]+= dot*nor[1];
2195
shi->vn[2]+= dot*nor[2];
2198
normalize_v3(shi->vn);
2200
/* this makes sure the bump is passed on to the next texture */
2201
shi->orn[0]= -shi->vn[0];
2202
shi->orn[1]= -shi->vn[1];
2203
shi->orn[2]= -shi->vn[2];
2205
/* reflection vector */
2210
if( mtex->mapto & MAP_DISPLACE ) {
2211
/* Now that most textures offer both Nor and Intensity, allow */
2212
/* both to work, and let user select with slider. */
2214
float norfac= mtex->norfac;
2216
shi->displace[0]+= 0.2f*Tnor*norfac*texres.nor[0];
2217
shi->displace[1]+= 0.2f*Tnor*norfac*texres.nor[1];
2218
shi->displace[2]+= 0.2f*Tnor*norfac*texres.nor[2];
2221
if(rgbnor & TEX_RGB) {
2222
texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
2225
factt= (0.5f-texres.tin)*mtex->dispfac*stencilTin; facmm= 1.0f-factt;
2227
if(mtex->blendtype==MTEX_BLEND) {
2228
shi->displace[0]= factt*shi->vn[0] + facmm*shi->displace[0];
2229
shi->displace[1]= factt*shi->vn[1] + facmm*shi->displace[1];
2230
shi->displace[2]= factt*shi->vn[2] + facmm*shi->displace[2];
2232
else if(mtex->blendtype==MTEX_MUL) {
2233
shi->displace[0]*= factt*shi->vn[0];
2234
shi->displace[1]*= factt*shi->vn[1];
2235
shi->displace[2]*= factt*shi->vn[2];
2237
else { /* add or sub */
2238
if(mtex->blendtype==MTEX_SUB) factt= -factt;
2240
shi->displace[0]+= factt*shi->vn[0];
2241
shi->displace[1]+= factt*shi->vn[1];
2242
shi->displace[2]+= factt*shi->vn[2];
2246
if(mtex->mapto & MAP_VARS) {
2247
/* stencil maps on the texture control slider, not texture intensity value */
2249
if(rgbnor & TEX_RGB) {
2250
if(texres.talpha) texres.tin= texres.ta;
2251
else texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2254
if(mtex->mapto & MAP_REF) {
2255
float difffac= mtex->difffac*stencilTin;
2257
shi->refl= texture_value_blend(mtex->def_var, shi->refl, texres.tin, difffac, mtex->blendtype);
2258
if(shi->refl<0.0) shi->refl= 0.0;
2260
if(mtex->mapto & MAP_SPEC) {
2261
float specfac= mtex->specfac*stencilTin;
2263
shi->spec= texture_value_blend(mtex->def_var, shi->spec, texres.tin, specfac, mtex->blendtype);
2264
if(shi->spec<0.0) shi->spec= 0.0;
2266
if(mtex->mapto & MAP_EMIT) {
2267
float emitfac= mtex->emitfac*stencilTin;
2269
shi->emit= texture_value_blend(mtex->def_var, shi->emit, texres.tin, emitfac, mtex->blendtype);
2270
if(shi->emit<0.0) shi->emit= 0.0;
2272
if(mtex->mapto & MAP_ALPHA) {
2273
float alphafac= mtex->alphafac*stencilTin;
2275
shi->alpha= texture_value_blend(mtex->def_var, shi->alpha, texres.tin, alphafac, mtex->blendtype);
2276
if(shi->alpha<0.0) shi->alpha= 0.0;
2277
else if(shi->alpha>1.0) shi->alpha= 1.0;
2279
if(mtex->mapto & MAP_HAR) {
2280
float har; // have to map to 0-1
2281
float hardfac= mtex->hardfac*stencilTin;
2283
har= ((float)shi->har)/128.0;
2284
har= 128.0*texture_value_blend(mtex->def_var, har, texres.tin, hardfac, mtex->blendtype);
2286
if(har<1.0) shi->har= 1;
2287
else if(har>511.0) shi->har= 511;
2288
else shi->har= (int)har;
2290
if(mtex->mapto & MAP_RAYMIRR) {
2291
float raymirrfac= mtex->raymirrfac*stencilTin;
2293
shi->ray_mirror= texture_value_blend(mtex->def_var, shi->ray_mirror, texres.tin, raymirrfac, mtex->blendtype);
2294
if(shi->ray_mirror<0.0) shi->ray_mirror= 0.0;
2295
else if(shi->ray_mirror>1.0) shi->ray_mirror= 1.0;
2297
if(mtex->mapto & MAP_TRANSLU) {
2298
float translfac= mtex->translfac*stencilTin;
2300
shi->translucency= texture_value_blend(mtex->def_var, shi->translucency, texres.tin, translfac, mtex->blendtype);
2301
if(shi->translucency<0.0) shi->translucency= 0.0;
2302
else if(shi->translucency>1.0) shi->translucency= 1.0;
2304
if(mtex->mapto & MAP_AMB) {
2305
float ambfac= mtex->ambfac*stencilTin;
2307
shi->amb= texture_value_blend(mtex->def_var, shi->amb, texres.tin, ambfac, mtex->blendtype);
2308
if(shi->amb<0.0) shi->amb= 0.0;
2309
else if(shi->amb>1.0) shi->amb= 1.0;
2311
shi->ambr= shi->amb*R.wrld.ambr;
2312
shi->ambg= shi->amb*R.wrld.ambg;
2313
shi->ambb= shi->amb*R.wrld.ambb;
2321
void do_volume_tex(ShadeInput *shi, float *xyz, int mapto_flag, float *col, float *val)
2325
TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
2326
int tex_nr, rgbnor= 0;
2327
float co[3], texvec[3];
2328
float fact, stencilTin=1.0;
2330
if (R.r.scemode & R_NO_TEX) return;
2331
/* here: test flag if there's a tex (todo) */
2333
for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
2334
/* separate tex switching */
2335
if(shi->mat->septex & (1<<tex_nr)) continue;
2337
if(shi->mat->mtex[tex_nr]) {
2338
mtex= shi->mat->mtex[tex_nr];
2340
if(tex==0) continue;
2342
/* only process if this texture is mapped
2343
* to one that we're interested in */
2344
if (!(mtex->mapto & mapto_flag)) continue;
2347
if(mtex->texco==TEXCO_OBJECT) {
2348
Object *ob= mtex->object;
2352
if(mtex->texflag & MTEX_OB_DUPLI_ORIG) {
2353
if(shi->obi && shi->obi->duplitexmat)
2354
mul_m4_v3(shi->obi->duplitexmat, co);
2356
mul_m4_v3(ob->imat, co);
2359
/* not really orco, but 'local' */
2360
else if(mtex->texco==TEXCO_ORCO) {
2362
if(mtex->texflag & MTEX_DUPLI_MAPTO) {
2363
VECCOPY(co, shi->duplilo);
2366
Object *ob= shi->obi->ob;
2368
mul_m4_v3(ob->imat, co);
2371
else if(mtex->texco==TEXCO_GLOB) {
2373
mul_m4_v3(R.viewinv, co);
2375
else continue; // can happen when texco defines disappear and it renders old files
2379
if(tex->type==TEX_IMAGE) {
2380
continue; /* not supported yet */
2381
//do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2385
if(mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
2386
else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
2388
if(mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
2389
else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
2391
if(mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
2392
else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
2395
rgbnor= multitex(tex, texvec, NULL, NULL, 0, &texres, 0, mtex->which_output); /* NULL = dxt/dyt, 0 = shi->osatex - not supported */
2397
/* texture output */
2399
if( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
2400
texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2403
if(mtex->texflag & MTEX_NEGATIVE) {
2404
if(rgbnor & TEX_RGB) {
2405
texres.tr= 1.0-texres.tr;
2406
texres.tg= 1.0-texres.tg;
2407
texres.tb= 1.0-texres.tb;
2409
texres.tin= 1.0-texres.tin;
2411
if(mtex->texflag & MTEX_STENCIL) {
2412
if(rgbnor & TEX_RGB) {
2414
texres.ta*= stencilTin;
2419
texres.tin*= stencilTin;
2425
if((mapto_flag & (MAP_EMISSION_COL+MAP_TRANSMISSION_COL+MAP_REFLECTION_COL)) && (mtex->mapto & (MAP_EMISSION_COL+MAP_TRANSMISSION_COL+MAP_REFLECTION_COL))) {
2428
/* stencil maps on the texture control slider, not texture intensity value */
2430
if((rgbnor & TEX_RGB)==0) {
2439
texres.tin= texres.ta;
2443
if((mapto_flag & MAP_EMISSION_COL) && (mtex->mapto & MAP_EMISSION_COL)) {
2444
float colemitfac= mtex->colemitfac*stencilTin;
2445
texture_rgb_blend(col, tcol, col, texres.tin, colemitfac, mtex->blendtype);
2448
if((mapto_flag & MAP_REFLECTION_COL) && (mtex->mapto & MAP_REFLECTION_COL)) {
2449
float colreflfac= mtex->colreflfac*stencilTin;
2450
texture_rgb_blend(col, tcol, col, texres.tin, colreflfac, mtex->blendtype);
2453
if((mapto_flag & MAP_TRANSMISSION_COL) && (mtex->mapto & MAP_TRANSMISSION_COL)) {
2454
float coltransfac= mtex->coltransfac*stencilTin;
2455
texture_rgb_blend(col, tcol, col, texres.tin, coltransfac, mtex->blendtype);
2459
if((mapto_flag & MAP_VARS) && (mtex->mapto & MAP_VARS)) {
2460
/* stencil maps on the texture control slider, not texture intensity value */
2462
/* convert RGB to intensity if intensity info isn't provided */
2463
if (!(rgbnor & TEX_INT)) {
2464
if (rgbnor & TEX_RGB) {
2465
if(texres.talpha) texres.tin= texres.ta;
2466
else texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2470
if((mapto_flag & MAP_EMISSION) && (mtex->mapto & MAP_EMISSION)) {
2471
float emitfac= mtex->emitfac*stencilTin;
2473
*val = texture_value_blend(mtex->def_var, *val, texres.tin, emitfac, mtex->blendtype);
2474
if(*val<0.0) *val= 0.0;
2476
if((mapto_flag & MAP_DENSITY) && (mtex->mapto & MAP_DENSITY)) {
2477
float densfac= mtex->densfac*stencilTin;
2479
*val = texture_value_blend(mtex->def_var, *val, texres.tin, densfac, mtex->blendtype);
2480
CLAMP(*val, 0.0, 1.0);
2482
if((mapto_flag & MAP_SCATTERING) && (mtex->mapto & MAP_SCATTERING)) {
2483
float scatterfac= mtex->scatterfac*stencilTin;
2485
*val = texture_value_blend(mtex->def_var, *val, texres.tin, scatterfac, mtex->blendtype);
2486
CLAMP(*val, 0.0, 1.0);
2488
if((mapto_flag & MAP_REFLECTION) && (mtex->mapto & MAP_REFLECTION)) {
2489
float reflfac= mtex->reflfac*stencilTin;
2491
*val = texture_value_blend(mtex->def_var, *val, texres.tin, reflfac, mtex->blendtype);
2492
CLAMP(*val, 0.0, 1.0);
2500
/* ------------------------------------------------------------------------- */
2502
void do_halo_tex(HaloRen *har, float xn, float yn, float *colf)
2505
TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
2506
float texvec[3], dxt[3], dyt[3], fact, facm, dx;
2509
if (R.r.scemode & R_NO_TEX) return;
2511
mtex= har->mat->mtex[0];
2512
if(mtex->tex==NULL) return;
2514
/* no normal mapping */
2517
texvec[0]= xn/har->rad;
2518
texvec[1]= yn/har->rad;
2521
osatex= (har->mat->texco & TEXCO_OSA);
2524
if(mtex->projx) texvec[0]= mtex->size[0]*(texvec[mtex->projx-1]+mtex->ofs[0]);
2525
else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
2527
if(mtex->projy) texvec[1]= mtex->size[1]*(texvec[mtex->projy-1]+mtex->ofs[1]);
2528
else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
2530
if(mtex->projz) texvec[2]= mtex->size[2]*(texvec[mtex->projz-1]+mtex->ofs[2]);
2531
else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
2538
dxt[0]= mtex->size[0]*dx;
2539
dyt[0]= mtex->size[0]*dx;
2541
else dxt[0]= dyt[0]= 0.0;
2544
dxt[1]= mtex->size[1]*dx;
2545
dyt[1]= mtex->size[1]*dx;
2547
else dxt[1]= dyt[1]= 0.0;
2553
else dxt[2]= dyt[2]= 0.0;
2557
if(mtex->tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2559
rgb= multitex(mtex->tex, texvec, dxt, dyt, osatex, &texres, 0, mtex->which_output);
2561
/* texture output */
2562
if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
2563
texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2566
if(mtex->texflag & MTEX_NEGATIVE) {
2568
texres.tr= 1.0-texres.tr;
2569
texres.tg= 1.0-texres.tg;
2570
texres.tb= 1.0-texres.tb;
2572
else texres.tin= 1.0-texres.tin;
2576
if(mtex->mapto & MAP_COL) {
2583
else if(mtex->mapto & MAP_ALPHA) {
2586
else texres.tin= texres.ta;
2588
/* inverse gamma correction */
2589
if (mtex->tex->type==TEX_IMAGE) {
2590
Image *ima = mtex->tex->ima;
2591
ImBuf *ibuf = BKE_image_get_ibuf(ima, &mtex->tex->iuser);
2593
/* don't linearize float buffers, assumed to be linear */
2594
if (ibuf && !(ibuf->rect_float) && R.r.color_mgt_flag & R_COLOR_MANAGEMENT)
2595
srgb_to_linearrgb_v3_v3(&texres.tr, &texres.tr);
2598
fact= texres.tin*mtex->colfac;
2601
if(mtex->blendtype==MTEX_MUL) {
2602
facm= 1.0-mtex->colfac;
2605
if(mtex->blendtype==MTEX_SUB) fact= -fact;
2607
if(mtex->blendtype==MTEX_BLEND) {
2608
colf[0]= (fact*texres.tr + facm*har->r);
2609
colf[1]= (fact*texres.tg + facm*har->g);
2610
colf[2]= (fact*texres.tb + facm*har->b);
2612
else if(mtex->blendtype==MTEX_MUL) {
2613
colf[0]= (facm+fact*texres.tr)*har->r;
2614
colf[1]= (facm+fact*texres.tg)*har->g;
2615
colf[2]= (facm+fact*texres.tb)*har->b;
2618
colf[0]= (fact*texres.tr + har->r);
2619
colf[1]= (fact*texres.tg + har->g);
2620
colf[2]= (fact*texres.tb + har->b);
2622
CLAMP(colf[0], 0.0, 1.0);
2623
CLAMP(colf[1], 0.0, 1.0);
2624
CLAMP(colf[2], 0.0, 1.0);
2627
if(mtex->mapto & MAP_ALPHA) {
2629
if(texres.talpha) texres.tin= texres.ta;
2630
else texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2633
colf[3]*= texres.tin;
2637
/* ------------------------------------------------------------------------- */
2639
/* hor and zen are RGB vectors, blend is 1 float, should all be initialized */
2640
void do_sky_tex(float *rco, float *lo, float *dxyview, float *hor, float *zen, float *blend, int skyflag, short thread)
2644
TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
2645
float *co, fact, stencilTin=1.0;
2646
float tempvec[3], texvec[3], dxt[3], dyt[3];
2647
int tex_nr, rgb= 0, ok;
2649
if (R.r.scemode & R_NO_TEX) return;
2650
/* todo: add flag to test if there's a tex */
2653
for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
2654
if(R.wrld.mtex[tex_nr]) {
2655
mtex= R.wrld.mtex[tex_nr];
2658
if(tex==0) continue;
2659
/* if(mtex->mapto==0) continue; */
2664
/* dxt dyt just from 1 value */
2666
dxt[0]= dxt[1]= dxt[2]= dxyview[0];
2667
dyt[0]= dyt[1]= dyt[2]= dxyview[1];
2670
dxt[0]= dxt[1]= dxt[2]= 0.0;
2671
dyt[0]= dyt[1]= dyt[2]= 0.0;
2674
/* Grab the mapping settings for this texture */
2675
switch(mtex->texco) {
2677
/* only works with texture being "real" */
2678
/* use saacos(), fixes bug [#22398], float precission caused lo[2] to be slightly less then -1.0 */
2679
fact= (1.0/M_PI)*saacos(lo[2])/(sqrt(lo[0]*lo[0] + lo[1]*lo[1]));
2680
tempvec[0]= lo[0]*fact;
2681
tempvec[1]= lo[1]*fact;
2686
case TEXCO_H_SPHEREMAP:
2687
case TEXCO_H_TUBEMAP:
2688
if(skyflag & WO_ZENUP) {
2689
if(mtex->texco==TEXCO_H_TUBEMAP) map_to_tube( tempvec, tempvec+1,lo[0], lo[2], lo[1]);
2690
else map_to_sphere( tempvec, tempvec+1,lo[0], lo[2], lo[1]);
2691
/* tube/spheremap maps for outside view, not inside */
2692
tempvec[0]= 1.0-tempvec[0];
2694
tempvec[1]= 2.0*tempvec[1]-1.0;
2696
/* and correction for do_2d_mapping */
2697
tempvec[0]= 2.0*tempvec[0]-1.0;
2698
tempvec[1]= 2.0*tempvec[1]-1.0;
2702
/* potentially dangerous... check with multitex! */
2708
VECCOPY(tempvec, lo);
2709
mul_m4_v3(mtex->object->imat, tempvec);
2716
VECCOPY(tempvec, rco);
2717
mul_m4_v3(R.viewinv, tempvec);
2723
// VECCOPY(shi->dxgl, shi->dxco);
2724
// mul_m3_v3(R.imat, shi->dxco);
2725
// VECCOPY(shi->dygl, shi->dyco);
2726
// mul_m3_v3(R.imat, shi->dyco);
2731
if(mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
2732
else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
2734
if(mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
2735
else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
2737
if(mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
2738
else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
2741
if(tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2743
rgb= multitex(mtex->tex, texvec, dxt, dyt, R.osa, &texres, thread, mtex->which_output);
2745
/* texture output */
2746
if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
2747
texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2750
if(mtex->texflag & MTEX_NEGATIVE) {
2752
texres.tr= 1.0-texres.tr;
2753
texres.tg= 1.0-texres.tg;
2754
texres.tb= 1.0-texres.tb;
2756
else texres.tin= 1.0-texres.tin;
2758
if(mtex->texflag & MTEX_STENCIL) {
2761
texres.ta*= stencilTin;
2766
texres.tin*= stencilTin;
2771
if(rgb) texres.ta *= stencilTin;
2772
else texres.tin*= stencilTin;
2776
if(mtex->mapto & (WOMAP_HORIZ+WOMAP_ZENUP+WOMAP_ZENDOWN)) {
2784
else texres.tin= texres.ta;
2786
tcol[0]= texres.tr; tcol[1]= texres.tg; tcol[2]= texres.tb;
2788
/* inverse gamma correction */
2789
if (tex->type==TEX_IMAGE) {
2790
Image *ima = tex->ima;
2791
ImBuf *ibuf = BKE_image_get_ibuf(ima, &tex->iuser);
2793
/* don't linearize float buffers, assumed to be linear */
2794
if (ibuf && !(ibuf->rect_float) && R.r.color_mgt_flag & R_COLOR_MANAGEMENT)
2795
srgb_to_linearrgb_v3_v3(tcol, tcol);
2798
if(mtex->mapto & WOMAP_HORIZ) {
2799
texture_rgb_blend(hor, tcol, hor, texres.tin, mtex->colfac, mtex->blendtype);
2801
if(mtex->mapto & (WOMAP_ZENUP+WOMAP_ZENDOWN)) {
2803
if(R.wrld.skytype & WO_SKYREAL) {
2804
if((skyflag & WO_ZENUP)) {
2805
if(mtex->mapto & WOMAP_ZENUP) ok= 1;
2807
else if(mtex->mapto & WOMAP_ZENDOWN) ok= 1;
2812
texture_rgb_blend(zen, tcol, zen, texres.tin, mtex->colfac, mtex->blendtype);
2816
if(mtex->mapto & WOMAP_BLEND) {
2817
if(rgb) texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2819
*blend= texture_value_blend(mtex->def_var, *blend, texres.tin, mtex->blendfac, mtex->blendtype);
2825
/* ------------------------------------------------------------------------- */
2826
/* colf supposed to be initialized with la->r,g,b */
2828
void do_lamp_tex(LampRen *la, float *lavec, ShadeInput *shi, float *colf, int effect)
2833
TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
2834
float *co = NULL, *dx = NULL, *dy = NULL, fact, stencilTin=1.0;
2835
float texvec[3], dxt[3], dyt[3], tempvec[3];
2836
int i, tex_nr, rgb= 0;
2838
if (R.r.scemode & R_NO_TEX) return;
2841
for(; tex_nr<MAX_MTEX; tex_nr++) {
2843
if(la->mtex[tex_nr]) {
2844
mtex= la->mtex[tex_nr];
2847
if(tex==NULL) continue;
2851
if(mtex->texco==TEXCO_OBJECT) {
2857
VECCOPY(tempvec, shi->co);
2858
mul_m4_v3(ob->imat, tempvec);
2860
VECCOPY(dxt, shi->dxco);
2861
VECCOPY(dyt, shi->dyco);
2862
mul_mat3_m4_v3(ob->imat, dxt);
2863
mul_mat3_m4_v3(ob->imat, dyt);
2868
dx= shi->dxco; dy= shi->dyco;
2871
else if(mtex->texco==TEXCO_GLOB) {
2872
co= shi->gl; dx= shi->dxco; dy= shi->dyco;
2873
VECCOPY(shi->gl, shi->co);
2874
mul_m4_v3(R.viewinv, shi->gl);
2876
else if(mtex->texco==TEXCO_VIEW) {
2878
VECCOPY(tempvec, lavec);
2879
mul_m3_v3(la->imat, tempvec);
2881
if(la->type==LA_SPOT) {
2882
tempvec[0]*= la->spottexfac;
2883
tempvec[1]*= la->spottexfac;
2889
VECCOPY(dxt, shi->dxlv);
2890
VECCOPY(dyt, shi->dylv);
2891
/* need some matrix conversion here? la->imat is a [3][3] matrix!!! **/
2892
mul_m3_v3(la->imat, dxt);
2893
mul_m3_v3(la->imat, dyt);
2895
mul_v3_fl(dxt, la->spottexfac);
2896
mul_v3_fl(dyt, la->spottexfac);
2902
if(mtex->projx && co) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
2903
else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
2905
if(mtex->projy && co) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
2906
else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
2908
if(mtex->projz && co) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
2909
else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
2914
dxt[i] = dyt[i] = 0.0;
2918
dxt[0]= mtex->size[0]*dx[mtex->projx-1];
2919
dyt[0]= mtex->size[0]*dy[mtex->projx-1];
2925
dxt[1]= mtex->size[1]*dx[mtex->projy-1];
2926
dyt[1]= mtex->size[1]*dy[mtex->projy-1];
2932
dxt[2]= mtex->size[2]*dx[mtex->projz-1];
2933
dyt[2]= mtex->size[2]*dy[mtex->projz-1];
2942
if(tex->type==TEX_IMAGE) {
2943
do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2946
rgb= multitex(tex, texvec, dxt, dyt, shi->osatex, &texres, shi->thread, mtex->which_output);
2948
/* texture output */
2949
if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
2950
texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2953
if(mtex->texflag & MTEX_NEGATIVE) {
2955
texres.tr= 1.0-texres.tr;
2956
texres.tg= 1.0-texres.tg;
2957
texres.tb= 1.0-texres.tb;
2959
else texres.tin= 1.0-texres.tin;
2961
if(mtex->texflag & MTEX_STENCIL) {
2964
texres.ta*= stencilTin;
2969
texres.tin*= stencilTin;
2974
if(rgb) texres.ta*= stencilTin;
2975
else texres.tin*= stencilTin;
2979
if(((mtex->mapto & LAMAP_COL) && (effect & LA_TEXTURE))||((mtex->mapto & LAMAP_SHAD) && (effect & LA_SHAD_TEX))) {
2987
else if(mtex->mapto & MAP_ALPHA) {
2988
texres.tin= stencilTin;
2990
else texres.tin= texres.ta;
2992
/* inverse gamma correction */
2993
if (tex->type==TEX_IMAGE) {
2994
Image *ima = tex->ima;
2995
ImBuf *ibuf = BKE_image_get_ibuf(ima, &tex->iuser);
2997
/* don't linearize float buffers, assumed to be linear */
2998
if (ibuf && !(ibuf->rect_float) && R.r.color_mgt_flag & R_COLOR_MANAGEMENT)
2999
srgb_to_linearrgb_v3_v3(&texres.tr, &texres.tr);
3002
/* lamp colors were premultiplied with this */
3003
col[0]= texres.tr*la->energy;
3004
col[1]= texres.tg*la->energy;
3005
col[2]= texres.tb*la->energy;
3007
texture_rgb_blend(colf, col, colf, texres.tin, mtex->colfac, mtex->blendtype);
3013
/* ------------------------------------------------------------------------- */
3015
int externtex(MTex *mtex, float *vec, float *tin, float *tr, float *tg, float *tb, float *ta)
3019
float dxt[3], dyt[3], texvec[3];
3023
if(tex==NULL) return 0;
3027
if(mtex->projx) texvec[0]= mtex->size[0]*(vec[mtex->projx-1]+mtex->ofs[0]);
3028
else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
3030
if(mtex->projy) texvec[1]= mtex->size[1]*(vec[mtex->projy-1]+mtex->ofs[1]);
3031
else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
3033
if(mtex->projz) texvec[2]= mtex->size[2]*(vec[mtex->projz-1]+mtex->ofs[2]);
3034
else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
3037
if(tex->type==TEX_IMAGE) {
3038
do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
3041
rgb= multitex(tex, texvec, dxt, dyt, 0, &texr, 0, mtex->which_output);
3044
texr.tin= (0.35*texr.tr+0.45*texr.tg+0.2*texr.tb);
3062
/* ------------------------------------------------------------------------- */
3064
void render_realtime_texture(ShadeInput *shi, Image *ima)
3067
static Tex imatex[BLENDER_MAX_THREADS]; // threadsafe
3068
static int firsttime= 1;
3070
float texvec[3], dx[2], dy[2];
3071
ShadeInputUV *suv= &shi->uv[shi->actuv];
3074
if(R.r.scemode & R_NO_TEX) return;
3077
BLI_lock_thread(LOCK_IMAGE);
3079
for(a=0; a<BLENDER_MAX_THREADS; a++) {
3080
memset(&imatex[a], 0, sizeof(Tex));
3081
default_tex(&imatex[a]);
3082
imatex[a].type= TEX_IMAGE;
3087
BLI_unlock_thread(LOCK_IMAGE);
3090
tex= &imatex[shi->thread];
3091
tex->iuser.ok= ima->ok;
3093
texvec[0]= 0.5+0.5*suv->uv[0];
3094
texvec[1]= 0.5+0.5*suv->uv[1];
3095
texvec[2] = 0; // initalize it because imagewrap looks at it.
3097
dx[0]= 0.5*suv->dxuv[0];
3098
dx[1]= 0.5*suv->dxuv[1];
3099
dy[0]= 0.5*suv->dyuv[0];
3100
dy[1]= 0.5*suv->dyuv[1];
3105
if(shi->osatex) imagewraposa(tex, ima, NULL, texvec, dx, dy, &texr);
3106
else imagewrap(tex, ima, NULL, texvec, &texr);
3108
shi->vcol[0]*= texr.tr;
3109
shi->vcol[1]*= texr.tg;
3110
shi->vcol[2]*= texr.tb;
3111
shi->vcol[3]*= texr.ta;