~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/render/intern/source/texture.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id: texture.c 30538 2010-07-20 13:32:42Z blendix $
3
 
 *
4
 
 * ***** BEGIN GPL LICENSE BLOCK *****
5
 
 *
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.
10
 
 *
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.
15
 
 *
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.
19
 
 *
20
 
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21
 
 * All rights reserved.
22
 
 *
23
 
 * Contributor(s): 2004-2006, Blender Foundation, full recode
24
 
 *
25
 
 * ***** END GPL LICENSE BLOCK *****
26
 
 */
27
 
 
28
 
#include <stdio.h>
29
 
#include <stdlib.h>
30
 
#include <string.h>
31
 
#include <math.h>
32
 
 
33
 
 
34
 
 
35
 
#include "BLI_blenlib.h"
36
 
#include "BLI_math.h"
37
 
#include "BLI_rand.h"
38
 
 
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"
47
 
 
48
 
#include "IMB_imbuf_types.h"
49
 
#include "IMB_imbuf.h"
50
 
 
51
 
#include "BKE_colortools.h"
52
 
#include "BKE_image.h"
53
 
#include "BKE_node.h"
54
 
#include "BKE_plugin_types.h"
55
 
#include "BKE_utildefines.h"
56
 
 
57
 
#include "BKE_global.h"
58
 
#include "BKE_main.h"
59
 
#include "BKE_material.h"
60
 
 
61
 
#include "BKE_library.h"
62
 
#include "BKE_image.h"
63
 
#include "BKE_texture.h"
64
 
#include "BKE_key.h"
65
 
#include "BKE_ipo.h"
66
 
 
67
 
#include "envmap.h"
68
 
#include "pointdensity.h"
69
 
#include "voxeldata.h"
70
 
#include "renderpipeline.h"
71
 
#include "render_types.h"
72
 
#include "rendercore.h"
73
 
#include "shading.h"
74
 
#include "texture.h"
75
 
 
76
 
#include "renderdatabase.h" /* needed for UV */
77
 
 
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
 
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
83
 
 
84
 
 
85
 
 
86
 
 
87
 
void init_render_texture(Render *re, Tex *tex)
88
 
{
89
 
        int cfra= re->scene->r.cfra;
90
 
        
91
 
        if(re) cfra= re->r.cfra;
92
 
        
93
 
        /* imap test */
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);
96
 
        }
97
 
        
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 
102
 
                        }
103
 
                }
104
 
        }
105
 
        else if(tex->type==TEX_ENVMAP) {
106
 
                /* just in case */
107
 
                tex->imaflag |= TEX_INTERPOL | TEX_MIPMAP;
108
 
                tex->extend= TEX_CLIP;
109
 
                
110
 
                if(tex->env) {
111
 
                        if(tex->env->type==ENV_PLANE)
112
 
                                tex->extend= TEX_EXTEND;
113
 
                        
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);
119
 
                        }
120
 
                }
121
 
        }
122
 
        
123
 
        if(tex->nodetree && tex->use_nodes) {
124
 
                ntreeBeginExecTree(tex->nodetree); /* has internal flag to detect it only does it once */
125
 
        }
126
 
}
127
 
 
128
 
/* ------------------------------------------------------------------------- */
129
 
 
130
 
void init_render_textures(Render *re)
131
 
{
132
 
        Tex *tex;
133
 
        
134
 
        tex= G.main->tex.first;
135
 
        while(tex) {
136
 
                if(tex->id.us) init_render_texture(re, tex);
137
 
                tex= tex->id.next;
138
 
        }
139
 
}
140
 
 
141
 
void end_render_texture(Tex *tex)
142
 
{
143
 
        if(tex && tex->use_nodes && tex->nodetree)
144
 
                ntreeEndExecTree(tex->nodetree);
145
 
}
146
 
 
147
 
void end_render_textures(void)
148
 
{
149
 
        Tex *tex;
150
 
        for(tex= G.main->tex.first; tex; tex= tex->id.next)
151
 
                if(tex->id.us)
152
 
                        end_render_texture(tex);
153
 
}
154
 
 
155
 
/* ------------------------------------------------------------------------- */
156
 
 
157
 
 
158
 
/* this allows colorbanded textures to control normals as well */
159
 
static void tex_normal_derivate(Tex *tex, TexResult *texres)
160
 
{
161
 
        if (tex->flag & TEX_COLORBAND) {
162
 
                float col[4];
163
 
                if (do_colorband(tex->coba, texres->tin, col)) {
164
 
                        float fac0, fac1, fac2, fac3;
165
 
                        
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]);
173
 
                        
174
 
                        texres->nor[0]= 0.3333*(fac0 - fac1);
175
 
                        texres->nor[1]= 0.3333*(fac0 - fac2);
176
 
                        texres->nor[2]= 0.3333*(fac0 - fac3);
177
 
                        
178
 
                        return;
179
 
                }
180
 
        }
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];
184
 
}
185
 
 
186
 
 
187
 
 
188
 
static int blend(Tex *tex, float *texvec, TexResult *texres)
189
 
{
190
 
        float x, y, t;
191
 
 
192
 
        if(tex->flag & TEX_FLIPBLEND) {
193
 
                x= texvec[1];
194
 
                y= texvec[0];
195
 
        }
196
 
        else {
197
 
                x= texvec[0];
198
 
                y= texvec[1];
199
 
        }
200
 
 
201
 
        if(tex->stype==TEX_LIN) {       /* lin */
202
 
                texres->tin= (1.0+x)/2.0;
203
 
        }
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;
208
 
        }
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;
213
 
                else {
214
 
                        t= texres->tin*texres->tin;
215
 
                        texres->tin= (3.0*t-2.0*t*texres->tin);
216
 
                }
217
 
        }
218
 
        else if(tex->stype==TEX_DIAG) { /* diag */
219
 
                texres->tin= (2.0+x+y)/4.0;
220
 
        }
221
 
        else if(tex->stype==TEX_RAD) { /* radial */
222
 
                texres->tin= (atan2(y,x) / (2*M_PI) + 0.5);
223
 
        }
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 */
228
 
        }
229
 
 
230
 
        BRICONT;
231
 
 
232
 
        return TEX_INT;
233
 
}
234
 
 
235
 
/* ------------------------------------------------------------------------- */
236
 
/* ************************************************************************* */
237
 
 
238
 
/* newnoise: all noisebased types now have different noisebases to choose from */
239
 
 
240
 
static int clouds(Tex *tex, float *texvec, TexResult *texres)
241
 
{
242
 
        int rv = TEX_INT;
243
 
        
244
 
        texres->tin = BLI_gTurbulence(tex->noisesize, texvec[0], texvec[1], texvec[2], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
245
 
 
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);
251
 
                
252
 
                tex_normal_derivate(tex, texres);
253
 
                rv |= TEX_NOR;
254
 
        }
255
 
 
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);
262
 
                BRICONTRGB;
263
 
                texres->ta = 1.0;
264
 
                return (rv | TEX_RGB);
265
 
        }
266
 
 
267
 
        BRICONT;
268
 
 
269
 
        return rv;
270
 
 
271
 
}
272
 
 
273
 
/* creates a sine wave */
274
 
static float tex_sin(float a)
275
 
{
276
 
        a = 0.5 + 0.5*sin(a);
277
 
                
278
 
        return a;
279
 
}
280
 
 
281
 
/* creates a saw wave */
282
 
static float tex_saw(float a)
283
 
{
284
 
        const float b = 2*M_PI;
285
 
        
286
 
        int n = (int)(a / b);
287
 
        a -= n*b;
288
 
        if (a < 0) a += b;
289
 
        return a / b;
290
 
}
291
 
 
292
 
/* creates a triangle wave */
293
 
static float tex_tri(float a)
294
 
{
295
 
        const float b = 2*M_PI;
296
 
        const float rmax = 1.0;
297
 
        
298
 
        a = rmax - 2.0*fabs(floor((a*(1.0/b))+0.5) - (a*(1.0/b)));
299
 
        
300
 
        return a;
301
 
}
302
 
 
303
 
/* computes basic wood intensity value at x,y,z */
304
 
static float wood_int(Tex *tex, float x, float y, float z)
305
 
{
306
 
        float wi=0;                                             
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 */
309
 
 
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;
314
 
        
315
 
        if ((wf>TEX_TRI) || (wf<TEX_SIN)) wf=0; /* check to be sure noisebasis2 is initialized ahead of time */
316
 
                
317
 
        if (wt==TEX_BAND) {
318
 
                wi = waveform[wf]((x + y + z)*10.0);
319
 
        }
320
 
        else if (wt==TEX_RING) {
321
 
                wi = waveform[wf](sqrt(x*x + y*y + z*z)*20.0);
322
 
        }
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);
326
 
        }
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);
330
 
        }
331
 
        
332
 
        return wi;
333
 
}
334
 
 
335
 
static int wood(Tex *tex, float *texvec, TexResult *texres)
336
 
{
337
 
        int rv=TEX_INT;
338
 
 
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);
345
 
                
346
 
                tex_normal_derivate(tex, texres);
347
 
                rv |= TEX_NOR;
348
 
        }
349
 
 
350
 
        BRICONT;
351
 
 
352
 
        return rv;
353
 
}
354
 
 
355
 
/* computes basic marble intensity at x,y,z */
356
 
static float marble_int(Tex *tex, float x, float y, float z)
357
 
{
358
 
        float n, mi;
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                                        */
361
 
        
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;
366
 
        
367
 
        if ((wf>TEX_TRI) || (wf<TEX_SIN)) wf=0; /* check to be sure noisebasis2 isn't initialized ahead of time */
368
 
        
369
 
        n = 5.0 * (x + y + z);
370
 
        
371
 
        mi = n + tex->turbul * BLI_gTurbulence(tex->noisesize, x, y, z, tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT),  tex->noisebasis);
372
 
 
373
 
        if (mt>=TEX_SOFT) {  /* TEX_SOFT always true */
374
 
                mi = waveform[wf](mi);
375
 
                if (mt==TEX_SHARP) {
376
 
                        mi = sqrt(mi);
377
 
                } 
378
 
                else if (mt==TEX_SHARPER) {
379
 
                        mi = sqrt(sqrt(mi));
380
 
                }
381
 
        }
382
 
 
383
 
        return mi;
384
 
}
385
 
 
386
 
static int marble(Tex *tex, float *texvec, TexResult *texres)
387
 
{
388
 
        int rv=TEX_INT;
389
 
 
390
 
        texres->tin = marble_int(tex, texvec[0], texvec[1], texvec[2]);
391
 
 
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);
397
 
                
398
 
                tex_normal_derivate(tex, texres);
399
 
                
400
 
                rv |= TEX_NOR;
401
 
        }
402
 
 
403
 
        BRICONT;
404
 
 
405
 
        return rv;
406
 
}
407
 
 
408
 
/* ------------------------------------------------------------------------- */
409
 
 
410
 
static int magic(Tex *tex, float *texvec, TexResult *texres)
411
 
{
412
 
        float x, y, z, turb=1.0;
413
 
        int n;
414
 
 
415
 
        n= tex->noisedepth;
416
 
        turb= tex->turbul/5.0;
417
 
 
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 );
421
 
        if(n>0) {
422
 
                x*= turb;
423
 
                y*= turb;
424
 
                z*= turb;
425
 
                y= -cos(x-y+z);
426
 
                y*= turb;
427
 
                if(n>1) {
428
 
                        x= cos(x-y-z);
429
 
                        x*= turb;
430
 
                        if(n>2) {
431
 
                                z= sin(-x-y-z);
432
 
                                z*= turb;
433
 
                                if(n>3) {
434
 
                                        x= -cos(-x+y-z);
435
 
                                        x*= turb;
436
 
                                        if(n>4) {
437
 
                                                y= -sin(-x+y+z);
438
 
                                                y*= turb;
439
 
                                                if(n>5) {
440
 
                                                        y= -cos(-x+y+z);
441
 
                                                        y*= turb;
442
 
                                                        if(n>6) {
443
 
                                                                x= cos(x+y+z);
444
 
                                                                x*= turb;
445
 
                                                                if(n>7) {
446
 
                                                                        z= sin(x+y-z);
447
 
                                                                        z*= turb;
448
 
                                                                        if(n>8) {
449
 
                                                                                x= -cos(-x-y+z);
450
 
                                                                                x*= turb;
451
 
                                                                                if(n>9) {
452
 
                                                                                        y= -sin(x-y+z);
453
 
                                                                                        y*= turb;
454
 
                                                                                }
455
 
                                                                        }
456
 
                                                                }
457
 
                                                        }
458
 
                                                }
459
 
                                        }
460
 
                                }
461
 
                        }
462
 
                }
463
 
        }
464
 
 
465
 
        if(turb!=0.0) {
466
 
                turb*= 2.0;
467
 
                x/= turb; 
468
 
                y/= turb; 
469
 
                z/= turb;
470
 
        }
471
 
        texres->tr= 0.5-x;
472
 
        texres->tg= 0.5-y;
473
 
        texres->tb= 0.5-z;
474
 
 
475
 
        texres->tin= 0.3333*(texres->tr+texres->tg+texres->tb);
476
 
        
477
 
        BRICONTRGB;
478
 
        texres->ta= 1.0;
479
 
        
480
 
        return TEX_RGB;
481
 
}
482
 
 
483
 
/* ------------------------------------------------------------------------- */
484
 
 
485
 
/* newnoise: stucci also modified to use different noisebasis */
486
 
static int stucci(Tex *tex, float *texvec, TexResult *texres)
487
 
{
488
 
        float nor[3], b2, ofs;
489
 
        int retval= TEX_INT;
490
 
        
491
 
        b2= BLI_gNoise(tex->noisesize, texvec[0], texvec[1], texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
492
 
        
493
 
        ofs= tex->turbul/200.0;
494
 
 
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);
499
 
 
500
 
        texres->tin= nor[2];
501
 
        
502
 
        if(texres->nor) { 
503
 
                
504
 
                VECCOPY(texres->nor, nor);
505
 
                tex_normal_derivate(tex, texres);
506
 
                
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];
511
 
                }
512
 
                
513
 
                retval |= TEX_NOR;
514
 
        }
515
 
        
516
 
        if(tex->stype==TEX_WALLOUT) 
517
 
                texres->tin= 1.0f-texres->tin;
518
 
        
519
 
        if(texres->tin<0.0f)
520
 
                texres->tin= 0.0f;
521
 
        
522
 
        return retval;
523
 
}
524
 
 
525
 
/* ------------------------------------------------------------------------- */
526
 
/* newnoise: musgrave terrain noise types */
527
 
 
528
 
static float mg_mFractalOrfBmTex(Tex *tex, float *texvec, TexResult *texres)
529
 
{
530
 
        int rv = TEX_INT;
531
 
        float (*mgravefunc)(float, float, float, float, float, float, int);
532
 
 
533
 
        if (tex->stype==TEX_MFRACTAL)
534
 
                mgravefunc = mg_MultiFractal;
535
 
        else
536
 
                mgravefunc = mg_fBm;
537
 
 
538
 
        texres->tin = tex->ns_outscale*mgravefunc(texvec[0], texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->noisebasis);
539
 
 
540
 
        if (texres->nor!=NULL) {
541
 
                float offs= tex->nabla/tex->noisesize;  // also scaling of texvec
542
 
                
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);
547
 
                
548
 
                tex_normal_derivate(tex, texres);
549
 
                rv |= TEX_NOR;
550
 
        }
551
 
 
552
 
        BRICONT;
553
 
 
554
 
        return rv;
555
 
 
556
 
}
557
 
 
558
 
static float mg_ridgedOrHybridMFTex(Tex *tex, float *texvec, TexResult *texres)
559
 
{
560
 
        int rv = TEX_INT;
561
 
        float (*mgravefunc)(float, float, float, float, float, float, float, float, int);
562
 
 
563
 
        if (tex->stype==TEX_RIDGEDMF)
564
 
                mgravefunc = mg_RidgedMultiFractal;
565
 
        else
566
 
                mgravefunc = mg_HybridMultiFractal;
567
 
 
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);
569
 
 
570
 
        if (texres->nor!=NULL) {
571
 
                float offs= tex->nabla/tex->noisesize;  // also scaling of texvec
572
 
                
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);
577
 
                
578
 
                tex_normal_derivate(tex, texres);
579
 
                rv |= TEX_NOR;
580
 
        }
581
 
 
582
 
        BRICONT;
583
 
 
584
 
        return rv;
585
 
 
586
 
}
587
 
 
588
 
 
589
 
static float mg_HTerrainTex(Tex *tex, float *texvec, TexResult *texres)
590
 
{
591
 
        int rv = TEX_INT;
592
 
 
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);
594
 
 
595
 
        if (texres->nor!=NULL) {
596
 
                float offs= tex->nabla/tex->noisesize;  // also scaling of texvec
597
 
                
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);
602
 
                
603
 
                tex_normal_derivate(tex, texres);
604
 
                rv |= TEX_NOR;
605
 
        }
606
 
 
607
 
        BRICONT;
608
 
 
609
 
        return rv;
610
 
 
611
 
}
612
 
 
613
 
 
614
 
static float mg_distNoiseTex(Tex *tex, float *texvec, TexResult *texres)
615
 
{
616
 
        int rv = TEX_INT;
617
 
 
618
 
        texres->tin = mg_VLNoise(texvec[0], texvec[1], texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
619
 
 
620
 
        if (texres->nor!=NULL) {
621
 
                float offs= tex->nabla/tex->noisesize;  // also scaling of texvec
622
 
                
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);
627
 
 
628
 
                tex_normal_derivate(tex, texres);
629
 
                rv |= TEX_NOR;
630
 
        }
631
 
 
632
 
        BRICONT;
633
 
 
634
 
 
635
 
        return rv;
636
 
 
637
 
}
638
 
 
639
 
 
640
 
/* ------------------------------------------------------------------------- */
641
 
/* newnoise: Voronoi texture type, probably the slowest, especially with minkovsky, bumpmapping, could be done another way */
642
 
 
643
 
static float voronoiTex(Tex *tex, float *texvec, TexResult *texres)
644
 
{
645
 
        int rv = TEX_INT;
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;
653
 
 
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]);
656
 
 
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;
677
 
                        if (t1>1) t1=1;
678
 
                        if (tex->vn_coltype==3) t1*=texres->tin; else t1*=sc;
679
 
                        texres->tr *= t1;
680
 
                        texres->tg *= t1;
681
 
                        texres->tb *= t1;
682
 
                }
683
 
                else {
684
 
                        texres->tr *= sc;
685
 
                        texres->tg *= sc;
686
 
                        texres->tb *= sc;
687
 
                }
688
 
        }
689
 
 
690
 
        if (texres->nor!=NULL) {
691
 
                float offs= tex->nabla/tex->noisesize;  // also scaling of texvec
692
 
 
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]);
700
 
                
701
 
                tex_normal_derivate(tex, texres);
702
 
                rv |= TEX_NOR;
703
 
        }
704
 
 
705
 
        if (tex->vn_coltype) {
706
 
                BRICONTRGB;
707
 
                texres->ta = 1.0;
708
 
                return (rv | TEX_RGB);
709
 
        }
710
 
        
711
 
        BRICONT;
712
 
 
713
 
        return rv;
714
 
 
715
 
}
716
 
 
717
 
/* ------------------------------------------------------------------------- */
718
 
 
719
 
static int texnoise(Tex *tex, TexResult *texres)
720
 
{
721
 
        float div=3.0;
722
 
        int val, ran, loop;
723
 
        
724
 
        ran= BLI_rand();
725
 
        val= (ran & 3);
726
 
        
727
 
        loop= tex->noisedepth;
728
 
        while(loop--) {
729
 
                ran= (ran>>2);
730
 
                val*= (ran & 3);
731
 
                div*= 3.0;
732
 
        }
733
 
        
734
 
        texres->tin= ((float)val)/div;;
735
 
 
736
 
        BRICONT;
737
 
        return TEX_INT;
738
 
}
739
 
 
740
 
/* ------------------------------------------------------------------------- */
741
 
 
742
 
static int plugintex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres)
743
 
{
744
 
        PluginTex *pit;
745
 
        int rgbnor=0;
746
 
        float result[ 8 ];
747
 
 
748
 
        texres->tin= 0.0;
749
 
 
750
 
        pit= tex->plugin;
751
 
        if(pit && pit->doit) {
752
 
                if(texres->nor) {
753
 
                        if (pit->version < 6) {
754
 
                                VECCOPY(pit->result+5, texres->nor);
755
 
                        } else {
756
 
                                VECCOPY(result+5, texres->nor);
757
 
                        }
758
 
                }
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);
764
 
                } else {
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);
769
 
                }
770
 
 
771
 
                if (pit->version < 6) {
772
 
                        texres->tin = pit->result[0];
773
 
                } else {
774
 
                        texres->tin = result[0];
775
 
                }
776
 
 
777
 
                if(rgbnor & TEX_NOR) {
778
 
                        if(texres->nor) {
779
 
                                if (pit->version < 6) {
780
 
                                        VECCOPY(texres->nor, pit->result+5);
781
 
                                } else {
782
 
                                        VECCOPY(texres->nor, result+5);
783
 
                                }
784
 
                        }
785
 
                }
786
 
                
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];
793
 
                        } else {
794
 
                                texres->tr = result[1];
795
 
                                texres->tg = result[2];
796
 
                                texres->tb = result[3];
797
 
                                texres->ta = result[4];
798
 
                        }
799
 
 
800
 
                        BRICONTRGB;
801
 
                }
802
 
                
803
 
                BRICONT;
804
 
        }
805
 
 
806
 
        return rgbnor;
807
 
}
808
 
 
809
 
 
810
 
static int cubemap_glob(float *n, float x, float y, float z, float *adr1, float *adr2)
811
 
{
812
 
        float x1, y1, z1, nor[3];
813
 
        int ret;
814
 
        
815
 
        if(n==NULL) {
816
 
                nor[0]= x; nor[1]= y; nor[2]= z;        // use local render coord
817
 
        }
818
 
        else {
819
 
                VECCOPY(nor, n);
820
 
        }
821
 
        mul_mat3_m4_v3(R.viewinv, nor);
822
 
 
823
 
        x1= fabs(nor[0]);
824
 
        y1= fabs(nor[1]);
825
 
        z1= fabs(nor[2]);
826
 
        
827
 
        if(z1>=x1 && z1>=y1) {
828
 
                *adr1 = (x + 1.0) / 2.0;
829
 
                *adr2 = (y + 1.0) / 2.0;
830
 
                ret= 0;
831
 
        }
832
 
        else if(y1>=x1 && y1>=z1) {
833
 
                *adr1 = (x + 1.0) / 2.0;
834
 
                *adr2 = (z + 1.0) / 2.0;
835
 
                ret= 1;
836
 
        }
837
 
        else {
838
 
                *adr1 = (y + 1.0) / 2.0;
839
 
                *adr2 = (z + 1.0) / 2.0;
840
 
                ret= 2;         
841
 
        }
842
 
        return ret;
843
 
}
844
 
 
845
 
/* ------------------------------------------------------------------------- */
846
 
 
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)
849
 
{
850
 
        int proj[4]={0, ME_PROJXY, ME_PROJXZ, ME_PROJYZ}, ret= 0;
851
 
        
852
 
        if(vlr) {
853
 
                int index;
854
 
                
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) {
859
 
                                float nor[3];
860
 
                                normal_tri_v3( nor,vlr->v1->orco, vlr->v2->orco, vlr->v3->orco);
861
 
                                
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;
865
 
                        }
866
 
                        else return cubemap_glob(n, x, y, z, adr1, adr2);
867
 
                }
868
 
                
869
 
                if(mtex) {
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;
874
 
 
875
 
                        index = (int) mtex->projy;
876
 
                        proj[index]= ME_PROJXZ;
877
 
 
878
 
                        index = (int) mtex->projz;
879
 
                        proj[index]= ME_PROJYZ;
880
 
                }
881
 
                
882
 
                if(vlr->puno & proj[1]) {
883
 
                        *adr1 = (x + 1.0) / 2.0;
884
 
                        *adr2 = (y + 1.0) / 2.0;        
885
 
                }
886
 
                else if(vlr->puno & proj[2]) {
887
 
                        *adr1 = (x + 1.0) / 2.0;
888
 
                        *adr2 = (z + 1.0) / 2.0;
889
 
                        ret= 1;
890
 
                }
891
 
                else {
892
 
                        *adr1 = (y + 1.0) / 2.0;
893
 
                        *adr2 = (z + 1.0) / 2.0;
894
 
                        ret= 2;
895
 
                }               
896
 
        } 
897
 
        else {
898
 
                return cubemap_glob(n, x, y, z, adr1, adr2);
899
 
        }
900
 
        
901
 
        return ret;
902
 
}
903
 
 
904
 
/* ------------------------------------------------------------------------- */
905
 
 
906
 
static int cubemap_ob(Object *ob, float *n, float x, float y, float z, float *adr1, float *adr2)
907
 
{
908
 
        float x1, y1, z1, nor[3];
909
 
        int ret;
910
 
        
911
 
        if(n==NULL) return 0;
912
 
        
913
 
        VECCOPY(nor, n);
914
 
        if(ob) mul_mat3_m4_v3(ob->imat, nor);
915
 
        
916
 
        x1= fabs(nor[0]);
917
 
        y1= fabs(nor[1]);
918
 
        z1= fabs(nor[2]);
919
 
        
920
 
        if(z1>=x1 && z1>=y1) {
921
 
                *adr1 = (x + 1.0) / 2.0;
922
 
                *adr2 = (y + 1.0) / 2.0;
923
 
                ret= 0;
924
 
        }
925
 
        else if(y1>=x1 && y1>=z1) {
926
 
                *adr1 = (x + 1.0) / 2.0;
927
 
                *adr2 = (z + 1.0) / 2.0;
928
 
                ret= 1;
929
 
        }
930
 
        else {
931
 
                *adr1 = (y + 1.0) / 2.0;
932
 
                *adr2 = (z + 1.0) / 2.0;
933
 
                ret= 2;         
934
 
        }
935
 
        return ret;
936
 
}
937
 
 
938
 
/* ------------------------------------------------------------------------- */
939
 
 
940
 
static void do_2d_mapping(MTex *mtex, float *t, VlakRen *vlr, float *n, float *dxt, float *dyt)
941
 
{
942
 
        Tex *tex;
943
 
        Object *ob= NULL;
944
 
        float fx, fy, fac1, area[8];
945
 
        int ok, proj, areaflag= 0, wrap, texco;
946
 
        
947
 
        /* mtex variables localized, only cubemap doesn't cooperate yet... */
948
 
        wrap= mtex->mapping;
949
 
        tex= mtex->tex;
950
 
        ob= mtex->object;
951
 
        texco= mtex->texco;
952
 
 
953
 
        if(R.osa==0) {
954
 
                
955
 
                if(wrap==MTEX_FLAT) {
956
 
                        fx = (t[0] + 1.0) / 2.0;
957
 
                        fy = (t[1] + 1.0) / 2.0;
958
 
                }
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]);
961
 
                else {
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);
965
 
                }
966
 
                
967
 
                /* repeat */
968
 
                if(tex->extend==TEX_REPEAT) {
969
 
                        if(tex->xrepeat>1) {
970
 
                                float origf= fx *= tex->xrepeat;
971
 
                                
972
 
                                if(fx>1.0) fx -= (int)(fx);
973
 
                                else if(fx<0.0) fx+= 1-(int)(fx);
974
 
                                
975
 
                                if(tex->flag & TEX_REPEAT_XMIR) {
976
 
                                        int orig= (int)floor(origf);
977
 
                                        if(orig & 1)
978
 
                                                fx= 1.0-fx;
979
 
                                }
980
 
                        }
981
 
                        if(tex->yrepeat>1) {
982
 
                                float origf= fy *= tex->yrepeat;
983
 
                                
984
 
                                if(fy>1.0) fy -= (int)(fy);
985
 
                                else if(fy<0.0) fy+= 1-(int)(fy);
986
 
                                
987
 
                                if(tex->flag & TEX_REPEAT_YMIR) {
988
 
                                        int orig= (int)floor(origf);
989
 
                                        if(orig & 1) 
990
 
                                                fy= 1.0-fy;
991
 
                                }
992
 
                        }
993
 
                }
994
 
                /* crop */
995
 
                if(tex->cropxmin!=0.0 || tex->cropxmax!=1.0) {
996
 
                        fac1= tex->cropxmax - tex->cropxmin;
997
 
                        fx= tex->cropxmin+ fx*fac1;
998
 
                }
999
 
                if(tex->cropymin!=0.0 || tex->cropymax!=1.0) {
1000
 
                        fac1= tex->cropymax - tex->cropymin;
1001
 
                        fy= tex->cropymin+ fy*fac1;
1002
 
                }
1003
 
 
1004
 
                t[0]= fx;
1005
 
                t[1]= fy;
1006
 
        }
1007
 
        else {
1008
 
                
1009
 
                if(wrap==MTEX_FLAT) {
1010
 
                        fx= (t[0] + 1.0) / 2.0;
1011
 
                        fy= (t[1] + 1.0) / 2.0;
1012
 
                        dxt[0]/= 2.0; 
1013
 
                        dxt[1]/= 2.0;
1014
 
                        dxt[2]/= 2.0;
1015
 
                        dyt[0]/= 2.0; 
1016
 
                        dyt[1]/= 2.0;
1017
 
                        dyt[2]/= 2.0;
1018
 
                }
1019
 
                else if ELEM(wrap, MTEX_TUBE, MTEX_SPHERE) {
1020
 
                        /* exception: the seam behind (y<0.0) */
1021
 
                        ok= 1;
1022
 
                        if(t[1]<=0.0) {
1023
 
                                fx= t[0]+dxt[0];
1024
 
                                fy= t[0]+dyt[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);
1027
 
                                else ok= 0;
1028
 
                        }
1029
 
                        if(ok) {
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]);
1034
 
                                }
1035
 
                                else { 
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]);
1039
 
                                }
1040
 
                                areaflag= 1;
1041
 
                        }
1042
 
                        else {
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]);
1045
 
                                dxt[0]/= 2.0; 
1046
 
                                dxt[1]/= 2.0;
1047
 
                                dyt[0]/= 2.0; 
1048
 
                                dyt[1]/= 2.0;
1049
 
                        }
1050
 
                }
1051
 
                else {
1052
 
 
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);
1056
 
 
1057
 
                        if(proj==1) {
1058
 
                                SWAP(float, dxt[1], dxt[2]);
1059
 
                                SWAP(float, dyt[1], dyt[2]);
1060
 
                        }
1061
 
                        else if(proj==2) {
1062
 
                                float f1= dxt[0], f2= dyt[0];
1063
 
                                dxt[0]= dxt[1];
1064
 
                                dyt[0]= dyt[1];
1065
 
                                dxt[1]= dxt[2];
1066
 
                                dyt[1]= dyt[2];
1067
 
                                dxt[2]= f1;
1068
 
                                dyt[2]= f2;
1069
 
                        }
1070
 
 
1071
 
                        dxt[0] *= 0.5f;
1072
 
                        dxt[1] *= 0.5f;
1073
 
                        dxt[2] *= 0.5f;
1074
 
 
1075
 
                        dyt[0] *= 0.5f;
1076
 
                        dyt[1] *= 0.5f;
1077
 
                        dyt[2] *= 0.5f;
1078
 
 
1079
 
                }
1080
 
                
1081
 
                /* if area, then reacalculate dxt[] and dyt[] */
1082
 
                if(areaflag) {
1083
 
                        fx= area[0]; 
1084
 
                        fy= area[1];
1085
 
                        dxt[0]= area[2]-fx;
1086
 
                        dxt[1]= area[3]-fy;
1087
 
                        dyt[0]= area[4]-fx;
1088
 
                        dyt[1]= area[5]-fy;
1089
 
                }
1090
 
                
1091
 
                /* repeat */
1092
 
                if(tex->extend==TEX_REPEAT) {
1093
 
                        float max= 1.0f;
1094
 
                        if(tex->xrepeat>1) {
1095
 
                                float origf= fx *= tex->xrepeat;
1096
 
                                
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);
1101
 
                                
1102
 
                                        if(tex->flag & TEX_REPEAT_XMIR) {
1103
 
                                                int orig= (int)floor(origf);
1104
 
                                                if(orig & 1) 
1105
 
                                                        fx= 1.0f-fx;
1106
 
                                        }
1107
 
                                }
1108
 
                                
1109
 
                                max= tex->xrepeat;
1110
 
                                
1111
 
                                dxt[0]*= tex->xrepeat;
1112
 
                                dyt[0]*= tex->xrepeat;
1113
 
                        }
1114
 
                        if(tex->yrepeat>1) {
1115
 
                                float origf= fy *= tex->yrepeat;
1116
 
                                
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);
1121
 
                                
1122
 
                                        if(tex->flag & TEX_REPEAT_YMIR) {
1123
 
                                                int orig= (int)floor(origf);
1124
 
                                                if(orig & 1) 
1125
 
                                                        fy= 1.0f-fy;
1126
 
                                        }
1127
 
                                }
1128
 
                                
1129
 
                                if(max<tex->yrepeat)
1130
 
                                        max= tex->yrepeat;
1131
 
 
1132
 
                                dxt[1]*= tex->yrepeat;
1133
 
                                dyt[1]*= tex->yrepeat;
1134
 
                        }
1135
 
                        if(max!=1.0f) {
1136
 
                                dxt[2]*= max;
1137
 
                                dyt[2]*= max;
1138
 
                        }
1139
 
                        
1140
 
                }
1141
 
                /* crop */
1142
 
                if(tex->cropxmin!=0.0 || tex->cropxmax!=1.0) {
1143
 
                        fac1= tex->cropxmax - tex->cropxmin;
1144
 
                        fx= tex->cropxmin+ fx*fac1;
1145
 
                        dxt[0]*= fac1;
1146
 
                        dyt[0]*= fac1;
1147
 
                }
1148
 
                if(tex->cropymin!=0.0 || tex->cropymax!=1.0) {
1149
 
                        fac1= tex->cropymax - tex->cropymin;
1150
 
                        fy= tex->cropymin+ fy*fac1;
1151
 
                        dxt[1]*= fac1;
1152
 
                        dyt[1]*= fac1;
1153
 
                }
1154
 
                
1155
 
                t[0]= fx;
1156
 
                t[1]= fy;
1157
 
 
1158
 
        }
1159
 
}
1160
 
 
1161
 
/* ************************************** */
1162
 
 
1163
 
static int multitex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres, short thread, short which_output)
1164
 
{
1165
 
        float tmpvec[3];
1166
 
        int retval=0; /* return value, int:0, col:1, nor:2, everything:3 */
1167
 
 
1168
 
        texres->talpha= 0;      /* is set when image texture returns alpha (considered premul) */
1169
 
        
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);
1173
 
        }
1174
 
        else
1175
 
        switch(tex->type) {
1176
 
        
1177
 
        case 0:
1178
 
                texres->tin= 0.0f;
1179
 
                return 0;
1180
 
        case TEX_CLOUDS:
1181
 
                retval= clouds(tex, texvec, texres);
1182
 
                break;
1183
 
        case TEX_WOOD:
1184
 
                retval= wood(tex, texvec, texres); 
1185
 
                break;
1186
 
        case TEX_MARBLE:
1187
 
                retval= marble(tex, texvec, texres); 
1188
 
                break;
1189
 
        case TEX_MAGIC:
1190
 
                retval= magic(tex, texvec, texres); 
1191
 
                break;
1192
 
        case TEX_BLEND:
1193
 
                retval= blend(tex, texvec, texres);
1194
 
                break;
1195
 
        case TEX_STUCCI:
1196
 
                retval= stucci(tex, texvec, texres); 
1197
 
                break;
1198
 
        case TEX_NOISE:
1199
 
                retval= texnoise(tex, texres); 
1200
 
                break;
1201
 
        case TEX_IMAGE:
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 */
1205
 
                break;
1206
 
        case TEX_PLUGIN:
1207
 
                retval= plugintex(tex, texvec, dxt, dyt, osatex, texres);
1208
 
                break;
1209
 
        case TEX_ENVMAP:
1210
 
                retval= envmaptex(tex, texvec, dxt, dyt, osatex, texres);
1211
 
                break;
1212
 
        case TEX_MUSGRAVE:
1213
 
                /* newnoise: musgrave types */
1214
 
                
1215
 
                /* ton: added this, for Blender convention reason. 
1216
 
                 * artificer: added the use of tmpvec to avoid scaling texvec
1217
 
                 */
1218
 
                VECCOPY(tmpvec, texvec);
1219
 
                mul_v3_fl(tmpvec, 1.0/tex->noisesize);
1220
 
                
1221
 
                switch(tex->stype) {
1222
 
                case TEX_MFRACTAL:
1223
 
                case TEX_FBM:
1224
 
                        retval= mg_mFractalOrfBmTex(tex, tmpvec, texres);
1225
 
                        break;
1226
 
                case TEX_RIDGEDMF:
1227
 
                case TEX_HYBRIDMF:
1228
 
                        retval= mg_ridgedOrHybridMFTex(tex, tmpvec, texres);
1229
 
                        break;
1230
 
                case TEX_HTERRAIN:
1231
 
                        retval= mg_HTerrainTex(tex, tmpvec, texres);
1232
 
                        break;
1233
 
                }
1234
 
                break;
1235
 
        /* newnoise: voronoi type */
1236
 
        case TEX_VORONOI:
1237
 
                /* ton: added this, for Blender convention reason.
1238
 
                 * artificer: added the use of tmpvec to avoid scaling texvec
1239
 
                 */
1240
 
                VECCOPY(tmpvec, texvec);
1241
 
                mul_v3_fl(tmpvec, 1.0/tex->noisesize);
1242
 
                
1243
 
                retval= voronoiTex(tex, tmpvec, texres);
1244
 
                break;
1245
 
        case TEX_DISTNOISE:
1246
 
                /* ton: added this, for Blender convention reason.
1247
 
                 * artificer: added the use of tmpvec to avoid scaling texvec
1248
 
                 */
1249
 
                VECCOPY(tmpvec, texvec);
1250
 
                mul_v3_fl(tmpvec, 1.0/tex->noisesize);
1251
 
                
1252
 
                retval= mg_distNoiseTex(tex, tmpvec, texres);
1253
 
                break;
1254
 
        case TEX_POINTDENSITY:
1255
 
                retval= pointdensitytex(tex, texvec, texres);
1256
 
                break;
1257
 
        case TEX_VOXELDATA:
1258
 
                retval= voxeldatatex(tex, texvec, texres);  
1259
 
                break;
1260
 
 
1261
 
        }
1262
 
 
1263
 
        if (tex->flag & TEX_COLORBAND) {
1264
 
                float col[4];
1265
 
                if (do_colorband(tex->coba, texres->tin, col)) {
1266
 
                        texres->talpha= 1;
1267
 
                        texres->tr= col[0];
1268
 
                        texres->tg= col[1];
1269
 
                        texres->tb= col[2];
1270
 
                        texres->ta= col[3];
1271
 
                        retval |= TEX_RGB;
1272
 
                }
1273
 
        }
1274
 
        return retval;
1275
 
}
1276
 
 
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)
1279
 
{
1280
 
        if(tex==NULL) {
1281
 
                memset(texres, 0, sizeof(TexResult));
1282
 
                return 0;
1283
 
        }
1284
 
 
1285
 
        if(mtex)
1286
 
                which_output= mtex->which_output;
1287
 
        
1288
 
        if(tex->type==TEX_IMAGE) {
1289
 
                int rgbnor;
1290
 
 
1291
 
                if(mtex) {
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);
1295
 
 
1296
 
                        if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
1297
 
                                ImBuf *ibuf = BKE_image_get_ibuf(tex->ima, &tex->iuser);
1298
 
                                
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);
1302
 
                        }
1303
 
                }
1304
 
                else {
1305
 
                        /* we don't have mtex, do default flat 2d projection */
1306
 
                        MTex localmtex;
1307
 
                        float texvec_l[3], dxt_l[3], dyt_l[3];
1308
 
                        
1309
 
                        localmtex.mapping= MTEX_FLAT;
1310
 
                        localmtex.tex= tex;
1311
 
                        localmtex.object= NULL;
1312
 
                        localmtex.texco= TEXCO_ORCO;
1313
 
                        
1314
 
                        copy_v3_v3(texvec_l, texvec);
1315
 
                        if(dxt && dyt) {
1316
 
                                copy_v3_v3(dxt_l, dxt);
1317
 
                                copy_v3_v3(dyt_l, dyt);
1318
 
                        }
1319
 
                        else {
1320
 
                                zero_v3(dxt_l);
1321
 
                                zero_v3(dyt_l);
1322
 
                        }
1323
 
                        
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);
1326
 
                }
1327
 
 
1328
 
                return rgbnor;
1329
 
        }
1330
 
        else
1331
 
                return multitex(tex, texvec, dxt, dyt, osatex, texres, thread, which_output);
1332
 
}
1333
 
 
1334
 
/* this is called for surface shading */
1335
 
int multitex_mtex(ShadeInput *shi, MTex *mtex, float *texvec, float *dxt, float *dyt, TexResult *texres)
1336
 
{
1337
 
        Tex *tex= mtex->tex;
1338
 
 
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);
1344
 
        }
1345
 
        else
1346
 
                return multitex(mtex->tex, texvec, dxt, dyt, shi->osatex, texres, shi->thread, mtex->which_output);
1347
 
}
1348
 
 
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)
1352
 
{
1353
 
        return multitex_nodes(tex, texvec, dxt, dyt, osatex, texres, 0, 0, NULL, NULL);
1354
 
}
1355
 
 
1356
 
/* ------------------------------------------------------------------------- */
1357
 
 
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)
1361
 
{
1362
 
        float facm, col;
1363
 
        
1364
 
        switch(blendtype) {
1365
 
        case MTEX_BLEND:
1366
 
                fact*= facg;
1367
 
                facm= 1.0-fact;
1368
 
 
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]);
1372
 
                break;
1373
 
                
1374
 
        case MTEX_MUL:
1375
 
                fact*= facg;
1376
 
                facm= 1.0-facg;
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];
1380
 
                break;
1381
 
 
1382
 
        case MTEX_SCREEN:
1383
 
                fact*= facg;
1384
 
                facm= 1.0-facg;
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]);
1388
 
                break;
1389
 
 
1390
 
        case MTEX_OVERLAY:
1391
 
                fact*= facg;
1392
 
                facm= 1.0-facg;
1393
 
                
1394
 
                if(out[0] < 0.5f)
1395
 
                        in[0] = out[0] * (facm + 2.0f*fact*tex[0]);
1396
 
                else
1397
 
                        in[0] = 1.0f - (facm + 2.0f*fact*(1.0 - tex[0])) * (1.0 - out[0]);
1398
 
                if(out[1] < 0.5f)
1399
 
                        in[1] = out[1] * (facm + 2.0f*fact*tex[1]);
1400
 
                else
1401
 
                        in[1] = 1.0f - (facm + 2.0f*fact*(1.0 - tex[1])) * (1.0 - out[1]);
1402
 
                if(out[2] < 0.5f)
1403
 
                        in[2] = out[2] * (facm + 2.0f*fact*tex[2]);
1404
 
                else
1405
 
                        in[2] = 1.0f - (facm + 2.0f*fact*(1.0 - tex[2])) * (1.0 - out[2]);
1406
 
                break;
1407
 
                
1408
 
        case MTEX_SUB:
1409
 
                fact= -fact;
1410
 
        case MTEX_ADD:
1411
 
                fact*= facg;
1412
 
                in[0]= (fact*tex[0] + out[0]);
1413
 
                in[1]= (fact*tex[1] + out[1]);
1414
 
                in[2]= (fact*tex[2] + out[2]);
1415
 
                break;
1416
 
 
1417
 
        case MTEX_DIV:
1418
 
                fact*= facg;
1419
 
                facm= 1.0-fact;
1420
 
                
1421
 
                if(tex[0]!=0.0)
1422
 
                        in[0]= facm*out[0] + fact*out[0]/tex[0];
1423
 
                if(tex[1]!=0.0)
1424
 
                        in[1]= facm*out[1] + fact*out[1]/tex[1];
1425
 
                if(tex[2]!=0.0)
1426
 
                        in[2]= facm*out[2] + fact*out[2]/tex[2];
1427
 
 
1428
 
                break;
1429
 
 
1430
 
        case MTEX_DIFF:
1431
 
                fact*= facg;
1432
 
                facm= 1.0-fact;
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]);
1436
 
                break;
1437
 
 
1438
 
        case MTEX_DARK:
1439
 
                fact*= facg;
1440
 
                facm= 1.0-fact;
1441
 
                
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];
1448
 
                break;
1449
 
 
1450
 
        case MTEX_LIGHT:
1451
 
                fact*= facg;
1452
 
                facm= 1.0-fact;
1453
 
                
1454
 
                col= fact*tex[0];
1455
 
                if(col > out[0]) in[0]= col; else in[0]= out[0];
1456
 
                col= fact*tex[1];
1457
 
                if(col > out[1]) in[1]= col; else in[1]= out[1];
1458
 
                col= fact*tex[2];
1459
 
                if(col > out[2]) in[2]= col; else in[2]= out[2];
1460
 
                break;
1461
 
                
1462
 
        case MTEX_BLEND_HUE:
1463
 
                fact*= facg;
1464
 
                VECCOPY(in, out);
1465
 
                ramp_blend(MA_RAMP_HUE, in, in+1, in+2, fact, tex);
1466
 
                break;
1467
 
        case MTEX_BLEND_SAT:
1468
 
                fact*= facg;
1469
 
                VECCOPY(in, out);
1470
 
                ramp_blend(MA_RAMP_SAT, in, in+1, in+2, fact, tex);
1471
 
                break;
1472
 
        case MTEX_BLEND_VAL:
1473
 
                fact*= facg;
1474
 
                VECCOPY(in, out);
1475
 
                ramp_blend(MA_RAMP_VAL, in, in+1, in+2, fact, tex);
1476
 
                break;
1477
 
        case MTEX_BLEND_COLOR:
1478
 
                fact*= facg;
1479
 
                VECCOPY(in, out);
1480
 
                ramp_blend(MA_RAMP_COLOR, in, in+1, in+2, fact, tex);
1481
 
                break;
1482
 
        case MTEX_SOFT_LIGHT: 
1483
 
                fact*= facg; 
1484
 
                VECCOPY(in, out); 
1485
 
                ramp_blend(MA_RAMP_SOFT, in, in+1, in+2, fact, tex); 
1486
 
                break; 
1487
 
        case MTEX_LIN_LIGHT: 
1488
 
                fact*= facg; 
1489
 
                VECCOPY(in, out); 
1490
 
                ramp_blend(MA_RAMP_LINEAR, in, in+1, in+2, fact, tex); 
1491
 
                break; 
1492
 
        }
1493
 
}
1494
 
 
1495
 
float texture_value_blend(float tex, float out, float fact, float facg, int blendtype)
1496
 
{
1497
 
        float in=0.0, facm, col, scf;
1498
 
        int flip= (facg < 0.0f);
1499
 
 
1500
 
        facg= fabsf(facg);
1501
 
        
1502
 
        fact*= facg;
1503
 
        facm= 1.0-fact;
1504
 
        if(flip) SWAP(float, fact, facm);
1505
 
 
1506
 
        switch(blendtype) {
1507
 
        case MTEX_BLEND:
1508
 
                in= fact*tex + facm*out;
1509
 
                break;
1510
 
 
1511
 
        case MTEX_MUL:
1512
 
                facm= 1.0-facg;
1513
 
                in= (facm+fact*tex)*out;
1514
 
                break;
1515
 
 
1516
 
        case MTEX_SCREEN:
1517
 
                facm= 1.0-facg;
1518
 
                in= 1.0-(facm+fact*(1.0-tex))*(1.0-out);
1519
 
                break;
1520
 
 
1521
 
        case MTEX_OVERLAY:
1522
 
                facm= 1.0-facg;
1523
 
                if(out < 0.5f)
1524
 
                        in = out * (facm + 2.0f*fact*tex);
1525
 
                else
1526
 
                        in = 1.0f - (facm + 2.0f*fact*(1.0 - tex)) * (1.0 - out);
1527
 
                break;
1528
 
 
1529
 
        case MTEX_SUB:
1530
 
                fact= -fact;
1531
 
        case MTEX_ADD:
1532
 
                in= fact*tex + out;
1533
 
                break;
1534
 
 
1535
 
        case MTEX_DIV:
1536
 
                if(tex!=0.0)
1537
 
                        in= facm*out + fact*out/tex;
1538
 
                break;
1539
 
 
1540
 
        case MTEX_DIFF:
1541
 
                in= facm*out + fact*fabs(tex-out);
1542
 
                break;
1543
 
 
1544
 
        case MTEX_DARK:
1545
 
                col= fact*tex;
1546
 
                if(col < out) in= col; else in= out;
1547
 
                break;
1548
 
 
1549
 
        case MTEX_LIGHT:
1550
 
                col= fact*tex;
1551
 
                if(col > out) in= col; else in= out;
1552
 
                break;
1553
 
 
1554
 
        case MTEX_SOFT_LIGHT: 
1555
 
                col= fact*tex; 
1556
 
                scf=1.0 - (1.0 - tex) * (1.0 - out); 
1557
 
                in= facm*out + fact * ((1.0 - out) * tex * out) + (out * scf); 
1558
 
                break;       
1559
 
 
1560
 
        case MTEX_LIN_LIGHT: 
1561
 
                if (tex > 0.5) 
1562
 
                        in = out + fact*(2*(tex - 0.5)); 
1563
 
                else 
1564
 
                        in = out + fact*(2*tex - 1); 
1565
 
                break;
1566
 
        }
1567
 
        
1568
 
        return in;
1569
 
}
1570
 
 
1571
 
static void texco_mapping(ShadeInput* shi, Tex* tex, MTex* mtex, float* co, float* dx, float* dy, float* texvec, float* dxt, float* dyt)
1572
 
{
1573
 
        // new: first swap coords, then map, then trans/scale
1574
 
        if (tex->type == TEX_IMAGE) {
1575
 
                // placement
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;
1579
 
 
1580
 
                if (shi->osatex) {
1581
 
                        if (mtex->projx) {
1582
 
                                dxt[0] = dx[mtex->projx - 1];
1583
 
                                dyt[0] = dy[mtex->projx - 1];
1584
 
                        }
1585
 
                        else dxt[0] = dyt[0] = 0.f;
1586
 
                        if (mtex->projy) {
1587
 
                                dxt[1] = dx[mtex->projy - 1];
1588
 
                                dyt[1] = dy[mtex->projy - 1];
1589
 
                        }
1590
 
                        else dxt[1] = dyt[1] = 0.f;
1591
 
                        if (mtex->projz) {
1592
 
                                dxt[2] = dx[mtex->projz - 1];
1593
 
                                dyt[2] = dy[mtex->projz - 1];
1594
 
                        }
1595
 
                        else dxt[2] = dyt[2] = 0.f;
1596
 
                }
1597
 
                do_2d_mapping(mtex, texvec, shi->vlr, shi->facenor, dxt, dyt);
1598
 
 
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;
1602
 
                if (shi->osatex) {
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];
1607
 
                }
1608
 
                
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];
1621
 
                        }
1622
 
                }
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];
1630
 
                        }
1631
 
                }
1632
 
                
1633
 
        }
1634
 
        else {  // procedural
1635
 
                // placement
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]);
1639
 
 
1640
 
                if (shi->osatex) {
1641
 
                        if (mtex->projx) {
1642
 
                                dxt[0] = mtex->size[0]*dx[mtex->projx - 1];
1643
 
                                dyt[0] = mtex->size[0]*dy[mtex->projx - 1];
1644
 
                        }
1645
 
                        else dxt[0] = dyt[0] = 0.f;
1646
 
                        if (mtex->projy) {
1647
 
                                dxt[1] = mtex->size[1]*dx[mtex->projy - 1];
1648
 
                                dyt[1] = mtex->size[1]*dy[mtex->projy - 1];
1649
 
                        }
1650
 
                        else dxt[1] = dyt[1] = 0.f;
1651
 
                        if (mtex->projz) {
1652
 
                                dxt[2] = mtex->size[2]*dx[mtex->projz - 1];
1653
 
                                dyt[2] = mtex->size[2]*dy[mtex->projz - 1];
1654
 
                        }
1655
 
                        else dxt[2]= dyt[2] = 0.f;
1656
 
                }
1657
 
        }
1658
 
}
1659
 
 
1660
 
void do_material_tex(ShadeInput *shi)
1661
 
{
1662
 
        MTex *mtex;
1663
 
        Tex *tex;
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
1670
 
        int nunvdone= 0;
1671
 
 
1672
 
        if (R.r.scemode & R_NO_TEX) return;
1673
 
        /* here: test flag if there's a tex (todo) */
1674
 
 
1675
 
        for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
1676
 
                
1677
 
                /* separate tex switching */
1678
 
                if(shi->mat->septex & (1<<tex_nr)) continue;
1679
 
                
1680
 
                if(shi->mat->mtex[tex_nr]) {
1681
 
                        mtex= shi->mat->mtex[tex_nr];
1682
 
                        
1683
 
                        tex= mtex->tex;
1684
 
                        if(tex==0) continue;
1685
 
 
1686
 
                        /* which coords */
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;
1692
 
                                }
1693
 
                                else {
1694
 
                                        co= shi->lo; dx= shi->dxlo; dy= shi->dylo;
1695
 
                                }
1696
 
                        }
1697
 
                        else if(mtex->texco==TEXCO_STICKY) {
1698
 
                                co= shi->sticky; dx= shi->dxsticky; dy= shi->dysticky;
1699
 
                        }
1700
 
                        else if(mtex->texco==TEXCO_OBJECT) {
1701
 
                                Object *ob= mtex->object;
1702
 
                                if(ob) {
1703
 
                                        co= tempvec;
1704
 
                                        dx= dxt;
1705
 
                                        dy= dyt;
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);
1711
 
                                        if(shi->osatex) {
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);
1716
 
                                        }
1717
 
                                }
1718
 
                                else {
1719
 
                                        /* if object doesn't exist, do not use orcos (not initialized) */
1720
 
                                        co= shi->co;
1721
 
                                        dx= shi->dxco; dy= shi->dyco;
1722
 
                                }
1723
 
                        }
1724
 
                        else if(mtex->texco==TEXCO_REFL) {
1725
 
                                co= shi->ref; dx= shi->dxref; dy= shi->dyref;
1726
 
                        }
1727
 
                        else if(mtex->texco==TEXCO_NORM) {
1728
 
                                co= shi->orn; dx= shi->dxno; dy= shi->dyno;
1729
 
                        }
1730
 
                        else if(mtex->texco==TEXCO_TANGENT) {
1731
 
                                co= shi->tang; dx= shi->dxno; dy= shi->dyno;
1732
 
                        }
1733
 
                        else if(mtex->texco==TEXCO_GLOB) {
1734
 
                                co= shi->gl; dx= shi->dxco; dy= shi->dyco;
1735
 
                        }
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;
1741
 
                                }
1742
 
                                else {
1743
 
                                        ShadeInputUV *suv= &shi->uv[shi->actuv];
1744
 
                                        int i = shi->actuv;
1745
 
 
1746
 
                                        if(mtex->uvname[0] != 0) {
1747
 
                                                for(i = 0; i < shi->totuv; i++) {
1748
 
                                                        if(strcmp(shi->uv[i].name, mtex->uvname)==0) {
1749
 
                                                                suv= &shi->uv[i];
1750
 
                                                                break;
1751
 
                                                        }
1752
 
                                                }
1753
 
                                        }
1754
 
 
1755
 
                                        co= suv->uv;
1756
 
                                        dx= suv->dxuv;
1757
 
                                        dy= suv->dyuv; 
1758
 
 
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;
1768
 
 
1769
 
                                                        vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
1770
 
 
1771
 
                                                        // compute ortho basis around normal
1772
 
                                                        if(!nunvdone) {
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);
1778
 
                                                                nunvdone= 1;
1779
 
                                                        }
1780
 
 
1781
 
                                                        if (tf) {
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;
1798
 
 
1799
 
                                                                if (uvd == 0.f) uvd = 1e-5f;
1800
 
                                                                if (d == 0.f) d = 1e-5f;
1801
 
                                                                d = uvd / d;
1802
 
 
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;
1807
 
                                                        }
1808
 
                                                }
1809
 
                                        }
1810
 
                                }
1811
 
                        }
1812
 
                        else if(mtex->texco==TEXCO_WINDOW) {
1813
 
                                co= shi->winco; dx= shi->dxwin; dy= shi->dywin;
1814
 
                        }
1815
 
                        else if(mtex->texco==TEXCO_STRAND) {
1816
 
                                co= tempvec; dx= dxt; dy= dyt;
1817
 
                                co[0]= shi->strandco;
1818
 
                                co[1]= co[2]= 0.0f;
1819
 
                                dx[0]= shi->dxstrand;
1820
 
                                dx[1]= dx[2]= 0.0f;
1821
 
                                dy[0]= shi->dystrand;
1822
 
                                dy[1]= dy[2]= 0.0f;
1823
 
                        }
1824
 
                        else if(mtex->texco==TEXCO_STRESS) {
1825
 
                                co= tempvec; dx= dxt; dy= dyt;
1826
 
                                co[0]= shi->stress;
1827
 
                                co[1]= co[2]= 0.0f;
1828
 
                                dx[0]= 0.0f;
1829
 
                                dx[1]= dx[2]= 0.0f;
1830
 
                                dy[0]= 0.0f;
1831
 
                                dy[1]= dy[2]= 0.0f;
1832
 
                        }
1833
 
                        else continue;  // can happen when texco defines disappear and it renders old files
1834
 
 
1835
 
                        /* the pointer defines if bumping happens */
1836
 
                        if(mtex->mapto & (MAP_NORM|MAP_WARP)) {
1837
 
                                texres.nor= norvec;
1838
 
                                norvec[0]= norvec[1]= norvec[2]= 0.0;
1839
 
                        }
1840
 
                        else texres.nor= NULL;
1841
 
                        
1842
 
                        if(warpdone) {
1843
 
                                VECADD(tempvec, co, warpvec);
1844
 
                                co= tempvec;
1845
 
                        }
1846
 
 
1847
 
                        if(mtex->texflag & MTEX_NEW_BUMP) {
1848
 
                                // compute ortho basis around normal
1849
 
                                if(!nunvdone) {
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);
1855
 
                                        nunvdone= 1;
1856
 
                                }
1857
 
 
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;
1865
 
                                        texres.nor = NULL;
1866
 
                                        // du & dv estimates, constant value defaults
1867
 
                                        du = dv = 0.01f;
1868
 
 
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...
1872
 
 
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);
1877
 
                                                if (ibuf) {
1878
 
                                                        du = 1.f/(float)ibuf->x;
1879
 
                                                        dv = 1.f/(float)ibuf->y;
1880
 
                                                }
1881
 
                                        }
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]);
1889
 
                                                }
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]);
1895
 
                                                }
1896
 
                                        }
1897
 
 
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;
1902
 
 
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...
1906
 
                                                du = MIN2(du, dv);
1907
 
                                                idu = (du < 1e-5f) ? bf : (bf/du);
1908
 
 
1909
 
                                                // +u val
1910
 
                                                tco[0] = co[0] + dudnu*du;
1911
 
                                                tco[1] = co[1] + dvdnu*du;
1912
 
                                                tco[2] = 0.f;
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));
1916
 
 
1917
 
                                                // +v val
1918
 
                                                tco[0] = co[0] + dudnv*du;
1919
 
                                                tco[1] = co[1] + dvdnv*du;
1920
 
                                                tco[2] = 0.f;
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));
1924
 
                                        }
1925
 
                                        else {
1926
 
                                                float tu[3] = {nu[0], nu[1], nu[2]}, tv[3] = {nv[0], nv[1], nv[2]};
1927
 
 
1928
 
                                                idu = (du < 1e-5f) ? bf : (bf/du);
1929
 
                                                idv = (dv < 1e-5f) ? bf : (bf/dv);
1930
 
 
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);
1934
 
                                                        normalize_v3(tu);
1935
 
                                                        normalize_v3(tv);
1936
 
                                                }
1937
 
                                                else if (mtex->texco == TEXCO_GLOB) {
1938
 
                                                        mul_mat3_m4_v3(R.viewinv, tu);
1939
 
                                                        mul_mat3_m4_v3(R.viewinv, tv);
1940
 
                                                }
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);
1944
 
                                                        normalize_v3(tu);
1945
 
                                                        normalize_v3(tv);
1946
 
                                                }
1947
 
 
1948
 
                                                // +u val
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));
1955
 
 
1956
 
                                                // +v val
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));
1963
 
                                        }
1964
 
 
1965
 
                                        // bumped normal
1966
 
                                        nu[0] += ud*nn[0];
1967
 
                                        nu[1] += ud*nn[1];
1968
 
                                        nu[2] += ud*nn[2];
1969
 
                                        nv[0] += vd*nn[0];
1970
 
                                        nv[1] += vd*nn[1];
1971
 
                                        nv[2] += vd*nn[2];
1972
 
                                        cross_v3_v3v3(nvec, nu, nv);
1973
 
 
1974
 
                                        nvec[0] = -nvec[0];
1975
 
                                        nvec[1] = -nvec[1];
1976
 
                                        nvec[2] = -nvec[2];
1977
 
                                        texres.nor = nvec;
1978
 
                                        rgbnor |= TEX_NOR;
1979
 
                                }
1980
 
                                else {
1981
 
                                        texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
1982
 
                                        rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, &texres);
1983
 
                                }
1984
 
                        }
1985
 
                        else {
1986
 
                                texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
1987
 
                                rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, &texres);
1988
 
                        }
1989
 
 
1990
 
                        /* texture output */
1991
 
 
1992
 
                        if( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
1993
 
                                texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
1994
 
                                rgbnor-= TEX_RGB;
1995
 
                        }
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;
2001
 
                                }
2002
 
                                texres.tin= 1.0-texres.tin;
2003
 
                        }
2004
 
                        if(mtex->texflag & MTEX_STENCIL) {
2005
 
                                if(rgbnor & TEX_RGB) {
2006
 
                                        fact= texres.ta;
2007
 
                                        texres.ta*= stencilTin;
2008
 
                                        stencilTin*= fact;
2009
 
                                }
2010
 
                                else {
2011
 
                                        fact= texres.tin;
2012
 
                                        texres.tin*= stencilTin;
2013
 
                                        stencilTin*= fact;
2014
 
                                }
2015
 
                        }
2016
 
                        else {
2017
 
                                Tnor*= stencilTin;
2018
 
                        }
2019
 
                        
2020
 
                        if(texres.nor) {
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;
2027
 
                                        }
2028
 
                                        else {
2029
 
                                                float co_nor= 0.5*cos(texres.tin-0.5);
2030
 
                                                float si= 0.5*sin(texres.tin-0.5);
2031
 
                                                float f1, f2;
2032
 
 
2033
 
                                                f1= shi->vn[0];
2034
 
                                                f2= shi->vn[1];
2035
 
                                                texres.nor[0]= f1*co_nor+f2*si;
2036
 
                                                texres.nor[1]= f2*co_nor-f1*si;
2037
 
                                                f1= shi->vn[1];
2038
 
                                                f2= shi->vn[2];
2039
 
                                                texres.nor[1]= f1*co_nor+f2*si;
2040
 
                                                texres.nor[2]= f2*co_nor-f1*si;
2041
 
                                        }
2042
 
                                }
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];
2048
 
                                        warpdone= 1;
2049
 
                                }
2050
 
#if 0                           
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);
2061
 
                                                }
2062
 
                                        }
2063
 
                                }
2064
 
#endif                          
2065
 
                        }
2066
 
 
2067
 
                        /* mapping */
2068
 
                        if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
2069
 
                                float tcol[3];
2070
 
                                
2071
 
                                /* stencil maps on the texture control slider, not texture intensity value */
2072
 
                                
2073
 
                                tcol[0]=texres.tr; tcol[1]=texres.tg; tcol[2]=texres.tb;
2074
 
                                
2075
 
                                if((rgbnor & TEX_RGB)==0) {
2076
 
                                        tcol[0]= mtex->r;
2077
 
                                        tcol[1]= mtex->g;
2078
 
                                        tcol[2]= mtex->b;
2079
 
                                }
2080
 
                                else if(mtex->mapto & MAP_ALPHA) {
2081
 
                                        texres.tin= stencilTin;
2082
 
                                }
2083
 
                                else texres.tin= texres.ta;
2084
 
                                
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);
2089
 
                                        
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);
2093
 
                                }
2094
 
                                
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);
2098
 
                                }
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);
2102
 
                                }
2103
 
                                if(mtex->mapto & MAP_COLMIR) {
2104
 
                                        float mirrfac= mtex->mirrfac*stencilTin;
2105
 
 
2106
 
                                        // exception for envmap only
2107
 
                                        if(tex->type==TEX_ENVMAP && mtex->blendtype==MTEX_BLEND) {
2108
 
                                                fact= texres.tin*mirrfac;
2109
 
                                                facm= 1.0- fact;
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];
2114
 
                                        }
2115
 
                                        else {
2116
 
                                                texture_rgb_blend(&shi->mirr, tcol, &shi->mirr, texres.tin, mirrfac, mtex->blendtype);
2117
 
                                        }
2118
 
                                }
2119
 
                        }
2120
 
                        if( (mtex->mapto & MAP_NORM) ) {
2121
 
                                if(texres.nor) {
2122
 
                                        float norfac= mtex->norfac;
2123
 
                                        
2124
 
                                        /* we need to code blending modes for normals too once.. now 1 exception hardcoded */
2125
 
                                        
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];
2132
 
                                                }
2133
 
                                                fact = Tnor*fabs(norfac);
2134
 
                                                if (fact>1.f) fact = 1.f;
2135
 
                                                facm = 1.f-fact;
2136
 
                                                if(mtex->normapspace == MTEX_NSPACE_TANGENT) {
2137
 
                                                        /* qdn: tangent space */
2138
 
                                                        float B[3], tv[3];
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];
2147
 
                                                }
2148
 
                                                else {
2149
 
                                                        float nor[3];
2150
 
 
2151
 
                                                        VECCOPY(nor, texres.nor);
2152
 
 
2153
 
                                                        if(mtex->normapspace == MTEX_NSPACE_CAMERA);
2154
 
                                                        else if(mtex->normapspace == MTEX_NSPACE_WORLD) {
2155
 
                                                                mul_mat3_m4_v3(R.viewmat, nor);
2156
 
                                                        }
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);
2161
 
                                                        }
2162
 
 
2163
 
                                                        normalize_v3(nor);
2164
 
 
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];
2169
 
                                                }
2170
 
                                        }
2171
 
                                        else {
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];
2176
 
                                                }
2177
 
                                                else {
2178
 
                                                        float nor[3], dot;
2179
 
        
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];
2184
 
                                                        }
2185
 
        
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];
2190
 
                                                        
2191
 
                                                        dot= 0.5f + 0.5f*INPR(nor, shi->vn);
2192
 
                                                        
2193
 
                                                        shi->vn[0]+= dot*nor[0];
2194
 
                                                        shi->vn[1]+= dot*nor[1];
2195
 
                                                        shi->vn[2]+= dot*nor[2];
2196
 
                                                }
2197
 
                                        }
2198
 
                                        normalize_v3(shi->vn);
2199
 
                                        
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];
2204
 
                                        
2205
 
                                        /* reflection vector */
2206
 
                                        calc_R_ref(shi);
2207
 
                                }
2208
 
                        }
2209
 
 
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.   */
2213
 
                                if(texres.nor) {
2214
 
                                        float norfac= mtex->norfac;
2215
 
 
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];
2219
 
                                }
2220
 
                                
2221
 
                                if(rgbnor & TEX_RGB) {
2222
 
                                        texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
2223
 
                                }
2224
 
 
2225
 
                                factt= (0.5f-texres.tin)*mtex->dispfac*stencilTin; facmm= 1.0f-factt;
2226
 
 
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];
2231
 
                                }
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];
2236
 
                                }
2237
 
                                else { /* add or sub */
2238
 
                                        if(mtex->blendtype==MTEX_SUB) factt= -factt;
2239
 
                                        else 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];
2243
 
                                }
2244
 
                        }
2245
 
 
2246
 
                        if(mtex->mapto & MAP_VARS) {
2247
 
                                /* stencil maps on the texture control slider, not texture intensity value */
2248
 
                                
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);
2252
 
                                }
2253
 
 
2254
 
                                if(mtex->mapto & MAP_REF) {
2255
 
                                        float difffac= mtex->difffac*stencilTin;
2256
 
 
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;
2259
 
                                }
2260
 
                                if(mtex->mapto & MAP_SPEC) {
2261
 
                                        float specfac= mtex->specfac*stencilTin;
2262
 
                                        
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;
2265
 
                                }
2266
 
                                if(mtex->mapto & MAP_EMIT) {
2267
 
                                        float emitfac= mtex->emitfac*stencilTin;
2268
 
 
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;
2271
 
                                }
2272
 
                                if(mtex->mapto & MAP_ALPHA) {
2273
 
                                        float alphafac= mtex->alphafac*stencilTin;
2274
 
 
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;
2278
 
                                }
2279
 
                                if(mtex->mapto & MAP_HAR) {
2280
 
                                        float har;  // have to map to 0-1
2281
 
                                        float hardfac= mtex->hardfac*stencilTin;
2282
 
                                        
2283
 
                                        har= ((float)shi->har)/128.0;
2284
 
                                        har= 128.0*texture_value_blend(mtex->def_var, har, texres.tin, hardfac, mtex->blendtype);
2285
 
                                        
2286
 
                                        if(har<1.0) shi->har= 1; 
2287
 
                                        else if(har>511.0) shi->har= 511;
2288
 
                                        else shi->har= (int)har;
2289
 
                                }
2290
 
                                if(mtex->mapto & MAP_RAYMIRR) {
2291
 
                                        float raymirrfac= mtex->raymirrfac*stencilTin;
2292
 
 
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;
2296
 
                                }
2297
 
                                if(mtex->mapto & MAP_TRANSLU) {
2298
 
                                        float translfac= mtex->translfac*stencilTin;
2299
 
 
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;
2303
 
                                }
2304
 
                                if(mtex->mapto & MAP_AMB) {
2305
 
                                        float ambfac= mtex->ambfac*stencilTin;
2306
 
 
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;
2310
 
                                        
2311
 
                                        shi->ambr= shi->amb*R.wrld.ambr;
2312
 
                                        shi->ambg= shi->amb*R.wrld.ambg;
2313
 
                                        shi->ambb= shi->amb*R.wrld.ambb;
2314
 
                                }
2315
 
                        }
2316
 
                }
2317
 
        }
2318
 
}
2319
 
 
2320
 
 
2321
 
void do_volume_tex(ShadeInput *shi, float *xyz, int mapto_flag, float *col, float *val)
2322
 
{
2323
 
        MTex *mtex;
2324
 
        Tex *tex;
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;
2329
 
        
2330
 
        if (R.r.scemode & R_NO_TEX) return;
2331
 
        /* here: test flag if there's a tex (todo) */
2332
 
        
2333
 
        for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
2334
 
                /* separate tex switching */
2335
 
                if(shi->mat->septex & (1<<tex_nr)) continue;
2336
 
                
2337
 
                if(shi->mat->mtex[tex_nr]) {
2338
 
                        mtex= shi->mat->mtex[tex_nr];
2339
 
                        tex= mtex->tex;
2340
 
                        if(tex==0) continue;
2341
 
                        
2342
 
                        /* only process if this texture is mapped 
2343
 
                         * to one that we're interested in */
2344
 
                        if (!(mtex->mapto & mapto_flag)) continue;
2345
 
                        
2346
 
                        /* which coords */
2347
 
                        if(mtex->texco==TEXCO_OBJECT) { 
2348
 
                                Object *ob= mtex->object;
2349
 
                                ob= mtex->object;
2350
 
                                if(ob) {                                                
2351
 
                                        VECCOPY(co, xyz);       
2352
 
                                        if(mtex->texflag & MTEX_OB_DUPLI_ORIG) {
2353
 
                                                if(shi->obi && shi->obi->duplitexmat)
2354
 
                                                        mul_m4_v3(shi->obi->duplitexmat, co);                                   
2355
 
                                        } 
2356
 
                                        mul_m4_v3(ob->imat, co);
2357
 
                                }
2358
 
                        }
2359
 
                        /* not really orco, but 'local' */
2360
 
                        else if(mtex->texco==TEXCO_ORCO) {
2361
 
                                
2362
 
                                if(mtex->texflag & MTEX_DUPLI_MAPTO) {
2363
 
                                        VECCOPY(co, shi->duplilo);
2364
 
                                }
2365
 
                                else {
2366
 
                                        Object *ob= shi->obi->ob;
2367
 
                                        VECCOPY(co, xyz);
2368
 
                                        mul_m4_v3(ob->imat, co);
2369
 
                                }
2370
 
                        }
2371
 
                        else if(mtex->texco==TEXCO_GLOB) {                                                      
2372
 
                           VECCOPY(co, xyz);
2373
 
                           mul_m4_v3(R.viewinv, co);
2374
 
                        }
2375
 
                        else continue;  // can happen when texco defines disappear and it renders old files
2376
 
 
2377
 
                        texres.nor= NULL;
2378
 
                        
2379
 
                        if(tex->type==TEX_IMAGE) {
2380
 
                                continue;       /* not supported yet */                         
2381
 
                                //do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2382
 
                        }
2383
 
                        else {
2384
 
                                /* placement */
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]);
2387
 
 
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]);
2390
 
 
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]);
2393
 
                        }
2394
 
                        
2395
 
                        rgbnor= multitex(tex, texvec, NULL, NULL, 0, &texres, 0, mtex->which_output);   /* NULL = dxt/dyt, 0 = shi->osatex - not supported */
2396
 
                        
2397
 
                        /* texture output */
2398
 
 
2399
 
                        if( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
2400
 
                                texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2401
 
                                rgbnor-= TEX_RGB;
2402
 
                        }
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;
2408
 
                                }
2409
 
                                texres.tin= 1.0-texres.tin;
2410
 
                        }
2411
 
                        if(mtex->texflag & MTEX_STENCIL) {
2412
 
                                if(rgbnor & TEX_RGB) {
2413
 
                                        fact= texres.ta;
2414
 
                                        texres.ta*= stencilTin;
2415
 
                                        stencilTin*= fact;
2416
 
                                }
2417
 
                                else {
2418
 
                                        fact= texres.tin;
2419
 
                                        texres.tin*= stencilTin;
2420
 
                                        stencilTin*= fact;
2421
 
                                }
2422
 
                        }
2423
 
                        
2424
 
                        
2425
 
                        if((mapto_flag & (MAP_EMISSION_COL+MAP_TRANSMISSION_COL+MAP_REFLECTION_COL)) && (mtex->mapto & (MAP_EMISSION_COL+MAP_TRANSMISSION_COL+MAP_REFLECTION_COL))) {
2426
 
                                float tcol[3];
2427
 
                                
2428
 
                                /* stencil maps on the texture control slider, not texture intensity value */
2429
 
                                
2430
 
                                if((rgbnor & TEX_RGB)==0) {
2431
 
                                        tcol[0]= mtex->r;
2432
 
                                        tcol[1]= mtex->g;
2433
 
                                        tcol[2]= mtex->b;
2434
 
                                } else {
2435
 
                                        tcol[0]=texres.tr;
2436
 
                                        tcol[1]=texres.tg;
2437
 
                                        tcol[2]=texres.tb;
2438
 
                                        if(texres.talpha)
2439
 
                                                texres.tin= texres.ta;
2440
 
                                }
2441
 
                                
2442
 
                                /* used for emit */
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);
2446
 
                                }
2447
 
                                
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);
2451
 
                                }
2452
 
                                
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);
2456
 
                                }
2457
 
                        }
2458
 
                        
2459
 
                        if((mapto_flag & MAP_VARS) && (mtex->mapto & MAP_VARS)) {
2460
 
                                /* stencil maps on the texture control slider, not texture intensity value */
2461
 
                                
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);
2467
 
                                        }
2468
 
                                }
2469
 
                                
2470
 
                                if((mapto_flag & MAP_EMISSION) && (mtex->mapto & MAP_EMISSION)) {
2471
 
                                        float emitfac= mtex->emitfac*stencilTin;
2472
 
 
2473
 
                                        *val = texture_value_blend(mtex->def_var, *val, texres.tin, emitfac, mtex->blendtype);
2474
 
                                        if(*val<0.0) *val= 0.0;
2475
 
                                }
2476
 
                                if((mapto_flag & MAP_DENSITY) && (mtex->mapto & MAP_DENSITY)) {
2477
 
                                        float densfac= mtex->densfac*stencilTin;
2478
 
 
2479
 
                                        *val = texture_value_blend(mtex->def_var, *val, texres.tin, densfac, mtex->blendtype);
2480
 
                                        CLAMP(*val, 0.0, 1.0);
2481
 
                                }
2482
 
                                if((mapto_flag & MAP_SCATTERING) && (mtex->mapto & MAP_SCATTERING)) {
2483
 
                                        float scatterfac= mtex->scatterfac*stencilTin;
2484
 
                                        
2485
 
                                        *val = texture_value_blend(mtex->def_var, *val, texres.tin, scatterfac, mtex->blendtype);
2486
 
                                        CLAMP(*val, 0.0, 1.0);
2487
 
                                }
2488
 
                                if((mapto_flag & MAP_REFLECTION) && (mtex->mapto & MAP_REFLECTION)) {
2489
 
                                        float reflfac= mtex->reflfac*stencilTin;
2490
 
                                        
2491
 
                                        *val = texture_value_blend(mtex->def_var, *val, texres.tin, reflfac, mtex->blendtype);
2492
 
                                        CLAMP(*val, 0.0, 1.0);
2493
 
                                }
2494
 
                        }
2495
 
                }
2496
 
        }
2497
 
}
2498
 
 
2499
 
 
2500
 
/* ------------------------------------------------------------------------- */
2501
 
 
2502
 
void do_halo_tex(HaloRen *har, float xn, float yn, float *colf)
2503
 
{
2504
 
        MTex *mtex;
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;
2507
 
        int rgb, osatex;
2508
 
 
2509
 
        if (R.r.scemode & R_NO_TEX) return;
2510
 
        
2511
 
        mtex= har->mat->mtex[0];
2512
 
        if(mtex->tex==NULL) return;
2513
 
        
2514
 
        /* no normal mapping */
2515
 
        texres.nor= NULL;
2516
 
                
2517
 
        texvec[0]= xn/har->rad;
2518
 
        texvec[1]= yn/har->rad;
2519
 
        texvec[2]= 0.0;
2520
 
        
2521
 
        osatex= (har->mat->texco & TEXCO_OSA);
2522
 
 
2523
 
        /* placement */
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]);
2526
 
        
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]);
2529
 
        
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]);
2532
 
        
2533
 
        if(osatex) {
2534
 
        
2535
 
                dx= 1.0/har->rad;
2536
 
        
2537
 
                if(mtex->projx) {
2538
 
                        dxt[0]= mtex->size[0]*dx;
2539
 
                        dyt[0]= mtex->size[0]*dx;
2540
 
                }
2541
 
                else dxt[0]= dyt[0]= 0.0;
2542
 
                
2543
 
                if(mtex->projy) {
2544
 
                        dxt[1]= mtex->size[1]*dx;
2545
 
                        dyt[1]= mtex->size[1]*dx;
2546
 
                }
2547
 
                else dxt[1]= dyt[1]= 0.0;
2548
 
                
2549
 
                if(mtex->projz) {
2550
 
                        dxt[2]= 0.0;
2551
 
                        dyt[2]= 0.0;
2552
 
                }
2553
 
                else dxt[2]= dyt[2]= 0.0;
2554
 
 
2555
 
        }
2556
 
 
2557
 
        if(mtex->tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2558
 
        
2559
 
        rgb= multitex(mtex->tex, texvec, dxt, dyt, osatex, &texres, 0, mtex->which_output);
2560
 
 
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);
2564
 
                rgb= 0;
2565
 
        }
2566
 
        if(mtex->texflag & MTEX_NEGATIVE) {
2567
 
                if(rgb) {
2568
 
                        texres.tr= 1.0-texres.tr;
2569
 
                        texres.tg= 1.0-texres.tg;
2570
 
                        texres.tb= 1.0-texres.tb;
2571
 
                }
2572
 
                else texres.tin= 1.0-texres.tin;
2573
 
        }
2574
 
 
2575
 
        /* mapping */
2576
 
        if(mtex->mapto & MAP_COL) {
2577
 
                
2578
 
                if(rgb==0) {
2579
 
                        texres.tr= mtex->r;
2580
 
                        texres.tg= mtex->g;
2581
 
                        texres.tb= mtex->b;
2582
 
                }
2583
 
                else if(mtex->mapto & MAP_ALPHA) {
2584
 
                        texres.tin= 1.0;
2585
 
                }
2586
 
                else texres.tin= texres.ta;
2587
 
 
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);
2592
 
                        
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);
2596
 
                }
2597
 
 
2598
 
                fact= texres.tin*mtex->colfac;
2599
 
                facm= 1.0-fact;
2600
 
                
2601
 
                if(mtex->blendtype==MTEX_MUL) {
2602
 
                        facm= 1.0-mtex->colfac;
2603
 
                }
2604
 
                
2605
 
                if(mtex->blendtype==MTEX_SUB) fact= -fact;
2606
 
 
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);
2611
 
                }
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;
2616
 
                }
2617
 
                else {
2618
 
                        colf[0]= (fact*texres.tr + har->r);
2619
 
                        colf[1]= (fact*texres.tg + har->g);
2620
 
                        colf[2]= (fact*texres.tb + har->b);
2621
 
                        
2622
 
                        CLAMP(colf[0], 0.0, 1.0);
2623
 
                        CLAMP(colf[1], 0.0, 1.0);
2624
 
                        CLAMP(colf[2], 0.0, 1.0);
2625
 
                }
2626
 
        }
2627
 
        if(mtex->mapto & MAP_ALPHA) {
2628
 
                if(rgb) {
2629
 
                        if(texres.talpha) texres.tin= texres.ta;
2630
 
                        else texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2631
 
                }
2632
 
                                
2633
 
                colf[3]*= texres.tin;
2634
 
        }
2635
 
}
2636
 
 
2637
 
/* ------------------------------------------------------------------------- */
2638
 
 
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)
2641
 
{
2642
 
        MTex *mtex;
2643
 
        Tex *tex;
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;
2648
 
        
2649
 
        if (R.r.scemode & R_NO_TEX) return;
2650
 
        /* todo: add flag to test if there's a tex */
2651
 
        texres.nor= NULL;
2652
 
        
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];
2656
 
                        
2657
 
                        tex= mtex->tex;
2658
 
                        if(tex==0) continue;
2659
 
                        /* if(mtex->mapto==0) continue; */
2660
 
                        
2661
 
                        /* which coords */
2662
 
                        co= lo;
2663
 
                        
2664
 
                        /* dxt dyt just from 1 value */
2665
 
                        if(dxyview) {
2666
 
                                dxt[0]= dxt[1]= dxt[2]= dxyview[0];
2667
 
                                dyt[0]= dyt[1]= dyt[2]= dxyview[1];
2668
 
                        }
2669
 
                        else {
2670
 
                                dxt[0]= dxt[1]= dxt[2]= 0.0;
2671
 
                                dyt[0]= dyt[1]= dyt[2]= 0.0;
2672
 
                        }
2673
 
                        
2674
 
                        /* Grab the mapping settings for this texture */
2675
 
                        switch(mtex->texco) {
2676
 
                        case TEXCO_ANGMAP:
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;
2682
 
                                tempvec[2]= 0.0;
2683
 
                                co= tempvec;
2684
 
                                break;
2685
 
                                
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];
2693
 
                                        /* only top half */
2694
 
                                        tempvec[1]= 2.0*tempvec[1]-1.0;
2695
 
                                        tempvec[2]= 0.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;
2699
 
                                        co= tempvec;
2700
 
                                }
2701
 
                                else {
2702
 
                                        /* potentially dangerous... check with multitex! */
2703
 
                                        continue;
2704
 
                                }
2705
 
                                break;
2706
 
                        case TEXCO_OBJECT:
2707
 
                                if(mtex->object) {
2708
 
                                        VECCOPY(tempvec, lo);
2709
 
                                        mul_m4_v3(mtex->object->imat, tempvec);
2710
 
                                        co= tempvec;
2711
 
                                }
2712
 
                                break;
2713
 
                                
2714
 
                        case TEXCO_GLOB:
2715
 
                                if(rco) {
2716
 
                                        VECCOPY(tempvec, rco);
2717
 
                                        mul_m4_v3(R.viewinv, tempvec);
2718
 
                                        co= tempvec;
2719
 
                                }
2720
 
                                else
2721
 
                                        co= lo;
2722
 
                                
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);
2727
 
                                break;
2728
 
                        }
2729
 
                        
2730
 
                        /* placement */                 
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]);
2733
 
                        
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]);
2736
 
                        
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]);
2739
 
                        
2740
 
                        /* texture */
2741
 
                        if(tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2742
 
                
2743
 
                        rgb= multitex(mtex->tex, texvec, dxt, dyt, R.osa, &texres, thread, mtex->which_output);
2744
 
                        
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);
2748
 
                                rgb= 0;
2749
 
                        }
2750
 
                        if(mtex->texflag & MTEX_NEGATIVE) {
2751
 
                                if(rgb) {
2752
 
                                        texres.tr= 1.0-texres.tr;
2753
 
                                        texres.tg= 1.0-texres.tg;
2754
 
                                        texres.tb= 1.0-texres.tb;
2755
 
                                }
2756
 
                                else texres.tin= 1.0-texres.tin;
2757
 
                        }
2758
 
                        if(mtex->texflag & MTEX_STENCIL) {
2759
 
                                if(rgb) {
2760
 
                                        fact= texres.ta;
2761
 
                                        texres.ta*= stencilTin;
2762
 
                                        stencilTin*= fact;
2763
 
                                }
2764
 
                                else {
2765
 
                                        fact= texres.tin;
2766
 
                                        texres.tin*= stencilTin;
2767
 
                                        stencilTin*= fact;
2768
 
                                }
2769
 
                        }
2770
 
                        else {
2771
 
                                if(rgb) texres.ta *= stencilTin;
2772
 
                                else texres.tin*= stencilTin;
2773
 
                        }
2774
 
                        
2775
 
                        /* color mapping */
2776
 
                        if(mtex->mapto & (WOMAP_HORIZ+WOMAP_ZENUP+WOMAP_ZENDOWN)) {
2777
 
                                float tcol[3];
2778
 
                                
2779
 
                                if(rgb==0) {
2780
 
                                        texres.tr= mtex->r;
2781
 
                                        texres.tg= mtex->g;
2782
 
                                        texres.tb= mtex->b;
2783
 
                                }
2784
 
                                else texres.tin= texres.ta;
2785
 
                                
2786
 
                                tcol[0]= texres.tr; tcol[1]= texres.tg; tcol[2]= texres.tb;
2787
 
 
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);
2792
 
                                        
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);
2796
 
                                }
2797
 
 
2798
 
                                if(mtex->mapto & WOMAP_HORIZ) {
2799
 
                                        texture_rgb_blend(hor, tcol, hor, texres.tin, mtex->colfac, mtex->blendtype);
2800
 
                                }
2801
 
                                if(mtex->mapto & (WOMAP_ZENUP+WOMAP_ZENDOWN)) {
2802
 
                                        ok= 0;
2803
 
                                        if(R.wrld.skytype & WO_SKYREAL) {
2804
 
                                                if((skyflag & WO_ZENUP)) {
2805
 
                                                        if(mtex->mapto & WOMAP_ZENUP) ok= 1;
2806
 
                                                }
2807
 
                                                else if(mtex->mapto & WOMAP_ZENDOWN) ok= 1;
2808
 
                                        }
2809
 
                                        else ok= 1;
2810
 
                                        
2811
 
                                        if(ok) {
2812
 
                                                texture_rgb_blend(zen, tcol, zen, texres.tin, mtex->colfac, mtex->blendtype);
2813
 
                                        }
2814
 
                                }
2815
 
                        }
2816
 
                        if(mtex->mapto & WOMAP_BLEND) {
2817
 
                                if(rgb) texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2818
 
                                
2819
 
                                *blend= texture_value_blend(mtex->def_var, *blend, texres.tin, mtex->blendfac, mtex->blendtype);
2820
 
                        }
2821
 
                }
2822
 
        }
2823
 
}
2824
 
 
2825
 
/* ------------------------------------------------------------------------- */
2826
 
/* colf supposed to be initialized with la->r,g,b */
2827
 
 
2828
 
void do_lamp_tex(LampRen *la, float *lavec, ShadeInput *shi, float *colf, int effect)
2829
 
{
2830
 
        Object *ob;
2831
 
        MTex *mtex;
2832
 
        Tex *tex;
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;
2837
 
        
2838
 
        if (R.r.scemode & R_NO_TEX) return;
2839
 
        tex_nr= 0;
2840
 
        
2841
 
        for(; tex_nr<MAX_MTEX; tex_nr++) {
2842
 
                
2843
 
                if(la->mtex[tex_nr]) {
2844
 
                        mtex= la->mtex[tex_nr];
2845
 
                        
2846
 
                        tex= mtex->tex;
2847
 
                        if(tex==NULL) continue;
2848
 
                        texres.nor= NULL;
2849
 
                        
2850
 
                        /* which coords */
2851
 
                        if(mtex->texco==TEXCO_OBJECT) {
2852
 
                                ob= mtex->object;
2853
 
                                if(ob) {
2854
 
                                        co= tempvec;
2855
 
                                        dx= dxt;
2856
 
                                        dy= dyt;
2857
 
                                        VECCOPY(tempvec, shi->co);
2858
 
                                        mul_m4_v3(ob->imat, tempvec);
2859
 
                                        if(shi->osatex) {
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);
2864
 
                                        }
2865
 
                                }
2866
 
                                else {
2867
 
                                        co= shi->co;
2868
 
                                        dx= shi->dxco; dy= shi->dyco;
2869
 
                                }
2870
 
                        }
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);
2875
 
                        }
2876
 
                        else if(mtex->texco==TEXCO_VIEW) {
2877
 
                                
2878
 
                                VECCOPY(tempvec, lavec);
2879
 
                                mul_m3_v3(la->imat, tempvec);
2880
 
                                
2881
 
                                if(la->type==LA_SPOT) {
2882
 
                                        tempvec[0]*= la->spottexfac;
2883
 
                                        tempvec[1]*= la->spottexfac;
2884
 
                                }
2885
 
                                co= tempvec; 
2886
 
                                
2887
 
                                dx= dxt; dy= dyt;       
2888
 
                                if(shi->osatex) {
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);
2894
 
                                        
2895
 
                                        mul_v3_fl(dxt, la->spottexfac);
2896
 
                                        mul_v3_fl(dyt, la->spottexfac);
2897
 
                                }
2898
 
                        }
2899
 
                        
2900
 
                        
2901
 
                        /* placement */
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]);
2904
 
                        
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]);
2907
 
                        
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]);
2910
 
                        
2911
 
                        if(shi->osatex) {
2912
 
                                if (!dx) {
2913
 
                                        for(i=0;i<2;i++) { 
2914
 
                                                dxt[i] = dyt[i] = 0.0;
2915
 
                                        }
2916
 
                                } else {
2917
 
                                        if(mtex->projx) {
2918
 
                                                dxt[0]= mtex->size[0]*dx[mtex->projx-1];
2919
 
                                                dyt[0]= mtex->size[0]*dy[mtex->projx-1];
2920
 
                                        } else {
2921
 
                                                dxt[0]= 0.0;
2922
 
                                                dyt[0]= 0.0;
2923
 
                                        }
2924
 
                                        if(mtex->projy) {
2925
 
                                                dxt[1]= mtex->size[1]*dx[mtex->projy-1];
2926
 
                                                dyt[1]= mtex->size[1]*dy[mtex->projy-1];
2927
 
                                        } else {
2928
 
                                                dxt[1]= 0.0;
2929
 
                                                dyt[1]= 0.0;
2930
 
                                        }
2931
 
                                        if(mtex->projz) {
2932
 
                                                dxt[2]= mtex->size[2]*dx[mtex->projz-1];
2933
 
                                                dyt[2]= mtex->size[2]*dy[mtex->projz-1];
2934
 
                                        } else {
2935
 
                                                dxt[2]= 0.0;
2936
 
                                                dyt[2]= 0.0;
2937
 
                                        }
2938
 
                                }
2939
 
                        }
2940
 
                        
2941
 
                        /* texture */
2942
 
                        if(tex->type==TEX_IMAGE) {
2943
 
                                do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2944
 
                        }
2945
 
                        
2946
 
                        rgb= multitex(tex, texvec, dxt, dyt, shi->osatex, &texres, shi->thread, mtex->which_output);
2947
 
 
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);
2951
 
                                rgb= 0;
2952
 
                        }
2953
 
                        if(mtex->texflag & MTEX_NEGATIVE) {
2954
 
                                if(rgb) {
2955
 
                                        texres.tr= 1.0-texres.tr;
2956
 
                                        texres.tg= 1.0-texres.tg;
2957
 
                                        texres.tb= 1.0-texres.tb;
2958
 
                                }
2959
 
                                else texres.tin= 1.0-texres.tin;
2960
 
                        }
2961
 
                        if(mtex->texflag & MTEX_STENCIL) {
2962
 
                                if(rgb) {
2963
 
                                        fact= texres.ta;
2964
 
                                        texres.ta*= stencilTin;
2965
 
                                        stencilTin*= fact;
2966
 
                                }
2967
 
                                else {
2968
 
                                        fact= texres.tin;
2969
 
                                        texres.tin*= stencilTin;
2970
 
                                        stencilTin*= fact;
2971
 
                                }
2972
 
                        }
2973
 
                        else {
2974
 
                                if(rgb) texres.ta*= stencilTin;
2975
 
                                else texres.tin*= stencilTin;
2976
 
                        }
2977
 
                        
2978
 
                        /* mapping */
2979
 
                        if(((mtex->mapto & LAMAP_COL) && (effect & LA_TEXTURE))||((mtex->mapto & LAMAP_SHAD) && (effect & LA_SHAD_TEX))) {
2980
 
                                float col[3];
2981
 
                                
2982
 
                                if(rgb==0) {
2983
 
                                        texres.tr= mtex->r;
2984
 
                                        texres.tg= mtex->g;
2985
 
                                        texres.tb= mtex->b;
2986
 
                                }
2987
 
                                else if(mtex->mapto & MAP_ALPHA) {
2988
 
                                        texres.tin= stencilTin;
2989
 
                                }
2990
 
                                else texres.tin= texres.ta;
2991
 
 
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);
2996
 
                                        
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);
3000
 
                                }
3001
 
 
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;
3006
 
                                
3007
 
                                texture_rgb_blend(colf, col, colf, texres.tin, mtex->colfac, mtex->blendtype);
3008
 
                        }
3009
 
                }
3010
 
        }
3011
 
}
3012
 
 
3013
 
/* ------------------------------------------------------------------------- */
3014
 
 
3015
 
int externtex(MTex *mtex, float *vec, float *tin, float *tr, float *tg, float *tb, float *ta)
3016
 
{
3017
 
        Tex *tex;
3018
 
        TexResult texr;
3019
 
        float dxt[3], dyt[3], texvec[3];
3020
 
        int rgb;
3021
 
        
3022
 
        tex= mtex->tex;
3023
 
        if(tex==NULL) return 0;
3024
 
        texr.nor= NULL;
3025
 
        
3026
 
        /* placement */
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]);
3029
 
        
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]);
3032
 
        
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]);
3035
 
        
3036
 
        /* texture */
3037
 
        if(tex->type==TEX_IMAGE) {
3038
 
                do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
3039
 
        }
3040
 
        
3041
 
        rgb= multitex(tex, texvec, dxt, dyt, 0, &texr, 0, mtex->which_output);
3042
 
        
3043
 
        if(rgb) {
3044
 
                texr.tin= (0.35*texr.tr+0.45*texr.tg+0.2*texr.tb);
3045
 
        }
3046
 
        else {
3047
 
                texr.tr= mtex->r;
3048
 
                texr.tg= mtex->g;
3049
 
                texr.tb= mtex->b;
3050
 
        }
3051
 
        
3052
 
        *tin= texr.tin;
3053
 
        *tr= texr.tr;
3054
 
        *tg= texr.tg;
3055
 
        *tb= texr.tb;
3056
 
        *ta= texr.ta;
3057
 
 
3058
 
        return (rgb != 0);
3059
 
}
3060
 
 
3061
 
 
3062
 
/* ------------------------------------------------------------------------- */
3063
 
 
3064
 
void render_realtime_texture(ShadeInput *shi, Image *ima)
3065
 
{
3066
 
        TexResult texr;
3067
 
        static Tex imatex[BLENDER_MAX_THREADS]; // threadsafe
3068
 
        static int firsttime= 1;
3069
 
        Tex *tex;
3070
 
        float texvec[3], dx[2], dy[2];
3071
 
        ShadeInputUV *suv= &shi->uv[shi->actuv];
3072
 
        int a;
3073
 
 
3074
 
        if(R.r.scemode & R_NO_TEX) return;
3075
 
 
3076
 
        if(firsttime) {
3077
 
                BLI_lock_thread(LOCK_IMAGE);
3078
 
                if(firsttime) {
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;
3083
 
                        }
3084
 
 
3085
 
                        firsttime= 0;
3086
 
                }
3087
 
                BLI_unlock_thread(LOCK_IMAGE);
3088
 
        }
3089
 
        
3090
 
        tex= &imatex[shi->thread];
3091
 
        tex->iuser.ok= ima->ok;
3092
 
        
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.
3096
 
        if(shi->osatex) {
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];
3101
 
        }
3102
 
        
3103
 
        texr.nor= NULL;
3104
 
        
3105
 
        if(shi->osatex) imagewraposa(tex, ima, NULL, texvec, dx, dy, &texr);
3106
 
        else imagewrap(tex, ima, NULL, texvec, &texr); 
3107
 
 
3108
 
        shi->vcol[0]*= texr.tr;
3109
 
        shi->vcol[1]*= texr.tg;
3110
 
        shi->vcol[2]*= texr.tb;
3111
 
        shi->vcol[3]*= texr.ta;
3112
 
}
3113
 
 
3114
 
/* eof */