~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2005-11-06 12:40:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051106124003-3pgs7tcg5rox96xg
Tags: 2.37a-1.1
* Non-maintainer upload.
* Split out parts of 01_SConstruct_debian.dpatch again: root_build_dir
  really needs to get adjusted before the clean target runs - closes: #333958,
  see #288882 for reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* texture.c
2
2
 *
3
3
 *
4
 
 * $Id: texture.c,v 1.20 2004/05/07 18:03:40 ton Exp $
 
4
 * $Id: texture.c,v 1.46 2005/05/29 10:44:52 ton Exp $
5
5
 *
6
6
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7
7
 *
41
41
#include <config.h>
42
42
#endif
43
43
 
44
 
#ifdef WIN32
45
 
#include "BLI_winstuff.h"
46
 
#endif
47
 
 
48
44
#include "MTC_matrixops.h"
49
45
 
50
46
#include "BLI_blenlib.h"
55
51
#include "DNA_object_types.h"
56
52
#include "DNA_lamp_types.h"
57
53
#include "DNA_mesh_types.h"
 
54
#include "DNA_meshdata_types.h"
58
55
#include "DNA_material_types.h"
59
56
#include "DNA_image_types.h"
60
57
 
61
58
#include "IMB_imbuf_types.h"
62
59
#include "IMB_imbuf.h"
63
60
 
64
 
#include "BKE_osa_types.h"
65
61
#include "BKE_plugin_types.h"
66
62
#include "BKE_utildefines.h"
67
63
 
77
73
#include "render.h"
78
74
#include "rendercore.h"
79
75
#include "envmap.h"
80
 
 
81
 
/* These vars form the texture channel */
82
 
float Tin, Tr, Tg, Tb, Ta, Txtra;
83
 
extern int Talpha;
84
 
 
 
76
#include "texture.h"
 
77
 
 
78
/* prototypes */
 
79
static int calcimanr(int cfra, Tex *tex);
85
80
 
86
81
/* ------------------------------------------------------------------------- */
87
82
 
 
83
int calcimanr(int cfra, Tex *tex)
 
84
{
 
85
        int imanr, len, a, fra, dur;
 
86
        
 
87
        /* here (+fie_ima/2-1) makes sure that division happens correctly */
 
88
        
 
89
        if(tex->frames==0) return 1;
 
90
        
 
91
        cfra= cfra-tex->sfra+1;
 
92
        
 
93
        /* cyclic */
 
94
        if(tex->len==0) len= (tex->fie_ima*tex->frames)/2;
 
95
        else len= tex->len;
 
96
        
 
97
        if(tex->imaflag & TEX_ANIMCYCLIC) {
 
98
                cfra= ( (cfra) % len );
 
99
                if(cfra < 0) cfra+= len;
 
100
                if(cfra==0) cfra= len;
 
101
        }
 
102
        
 
103
        if(cfra<1) cfra= 1;
 
104
        else if(cfra>len) cfra= len;
 
105
        
 
106
        /* convert current frame to current field */
 
107
        cfra= 2*(cfra);
 
108
        if(R.flag & R_SEC_FIELD) cfra++;
 
109
        
 
110
        /* transform to images space */
 
111
        imanr= (cfra+tex->fie_ima-2)/tex->fie_ima;
 
112
        if(imanr>tex->frames) imanr= tex->frames;
 
113
        imanr+= tex->offset;
 
114
        
 
115
        if(tex->imaflag & TEX_ANIMCYCLIC) {
 
116
                imanr= ( (imanr) % len );
 
117
                while(imanr < 0) imanr+= len;
 
118
                if(imanr==0) imanr= len;
 
119
        }
 
120
        
 
121
        /* are there images that last longer? */
 
122
        for(a=0; a<4; a++) {
 
123
                if(tex->fradur[a][0]) {
 
124
                        
 
125
                        fra= tex->fradur[a][0];
 
126
                        dur= tex->fradur[a][1]-1;
 
127
                        
 
128
                        while(dur>0 && imanr>fra) {
 
129
                                imanr--;
 
130
                                dur--;
 
131
                        }
 
132
                }
 
133
        }
 
134
        
 
135
        return imanr;
 
136
}
 
137
 
88
138
void init_render_texture(Tex *tex)
89
139
{
90
140
        Image *ima;
92
142
        unsigned short numlen;
93
143
        char name[FILE_MAXDIR+FILE_MAXFILE], head[FILE_MAXDIR+FILE_MAXFILE], tail[FILE_MAXDIR+FILE_MAXFILE];
94
144
 
95
 
        /* is also used as signal */
96
 
        tex->nor= NULL;
97
 
 
98
145
        /* imap test */
99
146
        if(tex->frames && tex->ima && tex->ima->name) { /* frames */
100
147
                strcpy(name, tex->ima->name);
193
240
 
194
241
/* ------------------------------------------------------------------------- */
195
242
 
196
 
static int blend(Tex *tex, float *texvec)
 
243
/* this allows colorbanded textures to control normals as well */
 
244
static void tex_normal_derivate(Tex *tex, TexResult *texres)
 
245
{
 
246
        if (tex->flag & TEX_COLORBAND) {
 
247
                float col[4];
 
248
                if (do_colorband(tex->coba, texres->tin, col)) {
 
249
                        float fac0, fac1, fac2, fac3;
 
250
                        
 
251
                        fac0= (col[0]+col[1]+col[2]);
 
252
                        do_colorband(tex->coba, texres->nor[0], col);
 
253
                        fac1= (col[0]+col[1]+col[2]);
 
254
                        do_colorband(tex->coba, texres->nor[1], col);
 
255
                        fac2= (col[0]+col[1]+col[2]);
 
256
                        do_colorband(tex->coba, texres->nor[2], col);
 
257
                        fac3= (col[0]+col[1]+col[2]);
 
258
                        
 
259
                        texres->nor[0]= 0.3333*(fac0 - fac1);
 
260
                        texres->nor[1]= 0.3333*(fac0 - fac2);
 
261
                        texres->nor[2]= 0.3333*(fac0 - fac3);
 
262
                        
 
263
                        return;
 
264
                }
 
265
        }
 
266
        texres->nor[0]= texres->tin - texres->nor[0];
 
267
        texres->nor[1]= texres->tin - texres->nor[1];
 
268
        texres->nor[2]= texres->tin - texres->nor[2];
 
269
}
 
270
 
 
271
 
 
272
 
 
273
static int blend(Tex *tex, float *texvec, TexResult *texres)
197
274
{
198
275
        float x, y, t;
199
276
 
207
284
        }
208
285
 
209
286
        if(tex->stype==0) {     /* lin */
210
 
                Tin= (1.0+x)/2.0;
 
287
                texres->tin= (1.0+x)/2.0;
211
288
        }
212
289
        else if(tex->stype==1) {        /* quad */
213
 
                Tin= (1.0+x)/2.0;
214
 
                if(Tin<0.0) Tin= 0.0;
215
 
                else Tin*= Tin;
 
290
                texres->tin= (1.0+x)/2.0;
 
291
                if(texres->tin<0.0) texres->tin= 0.0;
 
292
                else texres->tin*= texres->tin;
216
293
        }
217
294
        else if(tex->stype==2) {        /* ease */
218
 
                Tin= (1.0+x)/2.0;
219
 
                if(Tin<=.0) Tin= 0.0;
220
 
                else if(Tin>=1.0) Tin= 1.0;
 
295
                texres->tin= (1.0+x)/2.0;
 
296
                if(texres->tin<=.0) texres->tin= 0.0;
 
297
                else if(texres->tin>=1.0) texres->tin= 1.0;
221
298
                else {
222
 
                        t= Tin*Tin;
223
 
                        Tin= (3.0*t-2.0*t*Tin);
 
299
                        t= texres->tin*texres->tin;
 
300
                        texres->tin= (3.0*t-2.0*t*texres->tin);
224
301
                }
225
302
        }
226
303
        else if(tex->stype==3) { /* diag */
227
 
                Tin= (2.0+x+y)/4.0;
 
304
                texres->tin= (2.0+x+y)/4.0;
228
305
        }
229
306
        else {  /* sphere */
230
 
                Tin= 1.0-sqrt(x*x+      y*y+texvec[2]*texvec[2]);
231
 
                if(Tin<0.0) Tin= 0.0;
232
 
                if(tex->stype==5) Tin*= Tin;  /* halo */
 
307
                texres->tin= 1.0-sqrt(x*x+      y*y+texvec[2]*texvec[2]);
 
308
                if(texres->tin<0.0) texres->tin= 0.0;
 
309
                if(tex->stype==5) texres->tin*= texres->tin;  /* halo */
233
310
        }
234
311
 
235
 
        BRICON;
236
 
        if(tex->flag & TEX_COLORBAND)  return do_colorband(tex->coba);
 
312
        BRICONT;
237
313
 
238
314
        return 0;
239
315
}
240
316
 
241
317
/* ------------------------------------------------------------------------- */
242
318
/* ************************************************************************* */
243
 
/* clouds, wood & marble updated to do proper bumpmapping */
244
 
/* 0.025 seems reasonable value for offset */
245
 
#define B_OFFS 0.025
246
319
 
247
320
/* newnoise: all noisebased types now have different noisebases to choose from */
248
321
 
249
 
static int clouds(Tex *tex, float *texvec)
 
322
static int clouds(Tex *tex, float *texvec, TexResult *texres)
250
323
{
251
324
        int rv=0;       /* return value, int:0, col:1, nor:2, everything:3 */
252
 
        Tin = BLI_gTurbulence(tex->noisesize, texvec[0], texvec[1], texvec[2], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
325
        
 
326
        texres->tin = BLI_gTurbulence(tex->noisesize, texvec[0], texvec[1], texvec[2], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
253
327
 
254
 
        if (tex->nor!=NULL) {
 
328
        if (texres->nor!=NULL) {
255
329
                // calculate bumpnormal
256
 
                tex->nor[0] = Tin - BLI_gTurbulence(tex->noisesize, texvec[0] + B_OFFS, texvec[1], texvec[2], tex->noisedepth,  (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
257
 
                tex->nor[1] = Tin - BLI_gTurbulence(tex->noisesize, texvec[0], texvec[1] + B_OFFS, texvec[2], tex->noisedepth,  (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
258
 
                tex->nor[2] = Tin - BLI_gTurbulence(tex->noisesize, texvec[0], texvec[1], texvec[2] + B_OFFS, tex->noisedepth,  (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
330
                texres->nor[0] = BLI_gTurbulence(tex->noisesize, texvec[0] + tex->nabla, texvec[1], texvec[2], tex->noisedepth,  (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
331
                texres->nor[1] = BLI_gTurbulence(tex->noisesize, texvec[0], texvec[1] + tex->nabla, texvec[2], tex->noisedepth,  (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
332
                texres->nor[2] = BLI_gTurbulence(tex->noisesize, texvec[0], texvec[1], texvec[2] + tex->nabla, tex->noisedepth,  (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
333
                
 
334
                tex_normal_derivate(tex, texres);
259
335
                rv += 2;
260
336
        }
261
337
 
262
338
        if (tex->stype==1) {
263
339
                // in this case, int. value should really be computed from color,
264
340
                // and bumpnormal from that, would be too slow, looks ok as is
265
 
                Tr = Tin;
266
 
                Tg = BLI_gTurbulence(tex->noisesize, texvec[1], texvec[0], texvec[2], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
267
 
                Tb = BLI_gTurbulence(tex->noisesize, texvec[1], texvec[2], texvec[0], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
268
 
                BRICONRGB;
269
 
                Ta = 1.0;
 
341
                texres->tr = texres->tin;
 
342
                texres->tg = BLI_gTurbulence(tex->noisesize, texvec[1], texvec[0], texvec[2], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
343
                texres->tb = BLI_gTurbulence(tex->noisesize, texvec[1], texvec[2], texvec[0], tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
344
                BRICONTRGB;
 
345
                texres->ta = 1.0;
270
346
                return (rv+1);
271
347
        }
272
348
 
273
 
        BRICON;
274
 
 
275
 
        if (tex->flag & TEX_COLORBAND)  return (rv + do_colorband(tex->coba));
 
349
        BRICONT;
276
350
 
277
351
        return rv;
278
352
 
279
353
}
280
354
 
 
355
/* creates a sine wave */
 
356
static float tex_sin(float a)
 
357
{
 
358
        a = 0.5 + 0.5*sin(a);
 
359
                
 
360
        return a;
 
361
}
 
362
 
 
363
/* creates a saw wave */
 
364
static float tex_saw(float a)
 
365
{
 
366
        const float b = 2*M_PI;
 
367
        
 
368
        int n = (int)(a / b);
 
369
        a -= n*b;
 
370
        if (a < 0) a += b;
 
371
        return a / b;
 
372
}
 
373
 
 
374
/* creates a triangle wave */
 
375
static float tex_tri(float a)
 
376
{
 
377
        const float b = 2*M_PI;
 
378
        const float rmax = 1.0;
 
379
        
 
380
        a = rmax - 2.0*fabs(floor((a*(1.0/b))+0.5) - (a*(1.0/b)));
 
381
        
 
382
        return a;
 
383
}
 
384
 
281
385
/* computes basic wood intensity value at x,y,z */
282
386
static float wood_int(Tex *tex, float x, float y, float z)
283
387
{
284
 
        float wi=0;
285
 
 
286
 
        if (tex->stype==0)
287
 
                wi = 0.5 + 0.5*sin((x + y + z)*10.0);
288
 
        else if (tex->stype==1)
289
 
                wi = 0.5 + 0.5*sin(sqrt(x*x + y*y + z*z)*20.0);
290
 
        else if (tex->stype==2) {
291
 
                wi = BLI_gNoise(tex->noisesize, x, y, z, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
292
 
                wi = 0.5 + 0.5*sin(tex->turbul*wi + (x + y + z)*10.0);
293
 
        }
294
 
        else if (tex->stype==3) {
295
 
                wi = BLI_gNoise(tex->noisesize, x, y, z, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
296
 
                wi = 0.5 + 0.5*sin(tex->turbul*wi + (sqrt(x*x + y*y + z*z))*20.0);
297
 
        }
298
 
 
 
388
        float wi=0;                                             
 
389
        short wf = tex->noisebasis2;    /* wave form:   TEX_SIN=0,  TEX_SAW=1,  TEX_TRI=2                                                */
 
390
        short wt = tex->stype;                  /* wood type:   TEX_BAND=0, TEX_RING=1, TEX_BANDNOISE=2, TEX_RINGNOISE=3 */
 
391
 
 
392
        float (*waveform[3])(float);    /* create array of pointers to waveform functions */
 
393
        waveform[0] = tex_sin;                  /* assign address of tex_sin() function to pointer array */
 
394
        waveform[1] = tex_saw;
 
395
        waveform[2] = tex_tri;
 
396
        
 
397
        if ((wf>TEX_TRI) || (wf<TEX_SIN)) wf=0; /* check to be sure noisebasis2 is initialized ahead of time */
 
398
                
 
399
        if (wt==TEX_BAND) {
 
400
                wi = waveform[wf]((x + y + z)*10.0);
 
401
        }
 
402
        else if (wt==TEX_RING) {
 
403
                wi = waveform[wf](sqrt(x*x + y*y + z*z)*20.0);
 
404
        }
 
405
        else if (wt==TEX_BANDNOISE) {
 
406
                wi = tex->turbul*BLI_gNoise(tex->noisesize, x, y, z, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
407
                wi = waveform[wf]((x + y + z)*10.0 + wi);
 
408
        }
 
409
        else if (wt==TEX_RINGNOISE) {
 
410
                wi = tex->turbul*BLI_gNoise(tex->noisesize, x, y, z, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
411
                wi = waveform[wf](sqrt(x*x + y*y + z*z)*20.0 + wi);
 
412
        }
 
413
        
299
414
        return wi;
300
415
}
301
416
 
302
 
static int wood(Tex *tex, float *texvec)
 
417
static int wood(Tex *tex, float *texvec, TexResult *texres)
303
418
{
304
 
        int rv=0;       /* return value, int:0, col:1, nor:2, everything:3 */
 
419
        int rv=TEX_INT; /* return value, int:0, col:1, nor:2, everything:3 */
305
420
 
306
 
        Tin = wood_int(tex, texvec[0], texvec[1], texvec[2]);
307
 
        if (tex->nor!=NULL) {
 
421
        texres->tin = wood_int(tex, texvec[0], texvec[1], texvec[2]);
 
422
        if (texres->nor!=NULL) {
308
423
                /* calculate bumpnormal */
309
 
                tex->nor[0] = Tin - wood_int(tex, texvec[0] + B_OFFS, texvec[1], texvec[2]);
310
 
                tex->nor[1] = Tin - wood_int(tex, texvec[0], texvec[1] + B_OFFS, texvec[2]);
311
 
                tex->nor[2] = Tin - wood_int(tex, texvec[0], texvec[1], texvec[2] + B_OFFS);
312
 
                rv += 2;
 
424
                texres->nor[0] = wood_int(tex, texvec[0] + tex->nabla, texvec[1], texvec[2]);
 
425
                texres->nor[1] = wood_int(tex, texvec[0], texvec[1] + tex->nabla, texvec[2]);
 
426
                texres->nor[2] = wood_int(tex, texvec[0], texvec[1], texvec[2] + tex->nabla);
 
427
                
 
428
                tex_normal_derivate(tex, texres);
 
429
                rv = TEX_NOR;
313
430
        }
314
431
 
315
 
        BRICON;
316
 
        if (tex->flag & TEX_COLORBAND)  return (rv + do_colorband(tex->coba));
 
432
        BRICONT;
317
433
 
318
434
        return rv;
319
435
}
322
438
static float marble_int(Tex *tex, float x, float y, float z)
323
439
{
324
440
        float n, mi;
325
 
 
 
441
        short wf = tex->noisebasis2;    /* wave form:   TEX_SIN=0,  TEX_SAW=1,  TEX_TRI=2                                               */
 
442
        short mt = tex->stype;                  /* marble type: TEX_SOFT=0,     TEX_SHARP=1,TEX_SHAPER=2                                        */
 
443
        
 
444
        float (*waveform[3])(float);    /* create array of pointers to waveform functions */
 
445
        waveform[0] = tex_sin;                  /* assign address of tex_sin() function to pointer array */
 
446
        waveform[1] = tex_saw;
 
447
        waveform[2] = tex_tri;
 
448
        
 
449
        if ((wf>TEX_TRI) || (wf<TEX_SIN)) wf=0; /* check to be sure noisebasis2 isn't initialized ahead of time */
 
450
        
326
451
        n = 5.0 * (x + y + z);
 
452
        
 
453
        mi = n + tex->turbul * BLI_gTurbulence(tex->noisesize, x, y, z, tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT),  tex->noisebasis);
327
454
 
328
 
        mi = 0.5 + 0.5 * sin(n + tex->turbul * BLI_gTurbulence(tex->noisesize, x, y, z, tex->noisedepth, (tex->noisetype!=TEX_NOISESOFT),  tex->noisebasis));
329
 
        if (tex->stype>=1) {
330
 
                mi = sqrt(mi);
331
 
                if (tex->stype==2) mi = sqrt(mi);
 
455
        if (mt>=TEX_SOFT) {  /* TEX_SOFT always true */
 
456
                mi = waveform[wf](mi);
 
457
                if (mt==TEX_SHARP) {
 
458
                        mi = sqrt(mi);
 
459
                } 
 
460
                else if (mt==TEX_SHARPER) {
 
461
                        mi = sqrt(sqrt(mi));
 
462
                }
332
463
        }
333
464
 
334
465
        return mi;
335
466
}
336
467
 
337
 
static int marble(Tex *tex, float *texvec)
 
468
static int marble(Tex *tex, float *texvec, TexResult *texres)
338
469
{
339
 
        int rv=0;       /* return value, int:0, col:1, nor:2, everything:3 */
340
 
 
341
 
        Tin = marble_int(tex, texvec[0], texvec[1], texvec[2]);
342
 
 
343
 
        if (tex->nor!=NULL) {
 
470
        int rv=TEX_INT; /* return value, int:0, col:1, nor:2, everything:3 */
 
471
 
 
472
        texres->tin = marble_int(tex, texvec[0], texvec[1], texvec[2]);
 
473
 
 
474
        if (texres->nor!=NULL) {
344
475
                /* calculate bumpnormal */
345
 
                tex->nor[0] = Tin - marble_int(tex, texvec[0] + B_OFFS, texvec[1], texvec[2]);
346
 
                tex->nor[1] = Tin - marble_int(tex, texvec[0], texvec[1] + B_OFFS, texvec[2]);
347
 
                tex->nor[2] = Tin - marble_int(tex, texvec[0], texvec[1], texvec[2] + B_OFFS);
348
 
                rv += 2;
 
476
                texres->nor[0] = marble_int(tex, texvec[0] + tex->nabla, texvec[1], texvec[2]);
 
477
                texres->nor[1] = marble_int(tex, texvec[0], texvec[1] + tex->nabla, texvec[2]);
 
478
                texres->nor[2] = marble_int(tex, texvec[0], texvec[1], texvec[2] + tex->nabla);
 
479
                
 
480
                tex_normal_derivate(tex, texres);
 
481
                
 
482
                rv = TEX_NOR;
349
483
        }
350
484
 
351
 
        BRICON;
352
 
        if (tex->flag & TEX_COLORBAND)  return (rv + do_colorband(tex->coba));
 
485
        BRICONT;
353
486
 
354
487
        return rv;
355
488
}
356
489
 
357
490
/* ------------------------------------------------------------------------- */
358
491
 
359
 
static int magic(Tex *tex, float *texvec)
 
492
static int magic(Tex *tex, float *texvec, TexResult *texres)
360
493
{
361
494
        float x, y, z, turb=1.0;
362
495
        int n;
417
550
                y/= turb; 
418
551
                z/= turb;
419
552
        }
420
 
        Tr= 0.5-x;
421
 
        Tg= 0.5-y;
422
 
        Tb= 0.5-z;
 
553
        texres->tr= 0.5-x;
 
554
        texres->tg= 0.5-y;
 
555
        texres->tb= 0.5-z;
423
556
 
424
 
        BRICONRGB;
425
 
        Ta= 1.0;
 
557
        texres->tin= 0.3333*(texres->tr+texres->tg+texres->tb);
 
558
        
 
559
        BRICONTRGB;
 
560
        texres->ta= 1.0;
426
561
        
427
562
        return 1;
428
563
}
430
565
/* ------------------------------------------------------------------------- */
431
566
 
432
567
/* newnoise: stucci also modified to use different noisebasis */
433
 
static int stucci(Tex *tex, float *texvec)
 
568
static int stucci(Tex *tex, float *texvec, TexResult *texres)
434
569
{
435
 
        float b2, vec[3], ofs;
 
570
        float b2, ofs;
436
571
 
437
 
        if(tex->nor == NULL) return 0;
 
572
        if(texres->nor == NULL) return 0;
438
573
 
439
574
        ofs= tex->turbul/200.0;
440
575
 
441
 
        b2= BLI_gNoise(tex->noisesize, texvec[0], texvec[1], texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
576
        texres->tin=b2= BLI_gNoise(tex->noisesize, texvec[0], texvec[1], texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
442
577
        if(tex->stype) ofs*=(b2*b2);
443
 
        vec[0] = b2 - BLI_gNoise(tex->noisesize, texvec[0]+ofs, texvec[1], texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
444
 
        vec[1] = b2 - BLI_gNoise(tex->noisesize, texvec[0], texvec[1]+ofs, texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);        
445
 
        vec[2] = b2 - BLI_gNoise(tex->noisesize, texvec[0], texvec[1], texvec[2]+ofs, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
578
        texres->nor[0] = BLI_gNoise(tex->noisesize, texvec[0]+ofs, texvec[1], texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
579
        texres->nor[1] = BLI_gNoise(tex->noisesize, texvec[0], texvec[1]+ofs, texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);     
 
580
        texres->nor[2] = BLI_gNoise(tex->noisesize, texvec[0], texvec[1], texvec[2]+ofs, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
446
581
 
447
 
        if(tex->stype==1) {
448
 
                tex->nor[0]= vec[0];
449
 
                tex->nor[1]= vec[1];
450
 
                tex->nor[2]= vec[2];
451
 
        }
452
 
        else {
453
 
                tex->nor[0]= -vec[0];
454
 
                tex->nor[1]= -vec[1];
455
 
                tex->nor[2]= -vec[2];
 
582
        tex_normal_derivate(tex, texres);
 
583
        
 
584
        if(tex->stype==2) {
 
585
                texres->nor[0]= -texres->nor[0];
 
586
                texres->nor[1]= -texres->nor[1];
 
587
                texres->nor[2]= -texres->nor[2];
456
588
        }
457
589
 
458
590
        return 2;
461
593
/* ------------------------------------------------------------------------- */
462
594
/* newnoise: musgrave terrain noise types */
463
595
 
464
 
static float mg_mFractalOrfBmTex(Tex *tex, float *texvec)
 
596
static float mg_mFractalOrfBmTex(Tex *tex, float *texvec, TexResult *texres)
465
597
{
466
598
        int rv=0;       /* return value, int:0, col:1, nor:2, everything:3 */
467
599
        float (*mgravefunc)(float, float, float, float, float, float, int);
471
603
        else
472
604
                mgravefunc = mg_fBm;
473
605
 
474
 
        Tin = mgravefunc(texvec[0], texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->noisebasis);
 
606
        texres->tin = tex->ns_outscale*mgravefunc(texvec[0], texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->noisebasis);
475
607
 
476
 
        if (tex->nor!=NULL) {
 
608
        if (texres->nor!=NULL) {
 
609
                float offs= tex->nabla/tex->noisesize;  // also scaling of texvec
 
610
                
477
611
                /* calculate bumpnormal */
478
 
                tex->nor[0] = Tin - mgravefunc(texvec[0] + B_OFFS, texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->noisebasis);
479
 
                tex->nor[1] = Tin - mgravefunc(texvec[0], texvec[1] + B_OFFS, texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->noisebasis);
480
 
                tex->nor[2] = Tin - mgravefunc(texvec[0], texvec[1], texvec[2] + B_OFFS, tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->noisebasis);
 
612
                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);
 
613
                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);
 
614
                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);
 
615
                
 
616
                tex_normal_derivate(tex, texres);
481
617
                rv += 2;
482
618
        }
483
619
 
484
 
        Tin *= tex->ns_outscale;
485
 
 
486
 
        BRICON;
487
 
 
488
 
        if (tex->flag & TEX_COLORBAND)  return (rv + do_colorband(tex->coba));
 
620
        BRICONT;
489
621
 
490
622
        return rv;
491
623
 
492
624
}
493
625
 
494
 
static float mg_ridgedOrHybridMFTex(Tex *tex, float *texvec)
 
626
static float mg_ridgedOrHybridMFTex(Tex *tex, float *texvec, TexResult *texres)
495
627
{
496
628
        int rv=0;       /* return value, int:0, col:1, nor:2, everything:3 */
497
629
        float (*mgravefunc)(float, float, float, float, float, float, float, float, int);
501
633
        else
502
634
                mgravefunc = mg_HybridMultiFractal;
503
635
 
504
 
        Tin = mgravefunc(texvec[0], texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->mg_gain, tex->noisebasis);
505
 
 
506
 
        if (tex->nor!=NULL) {
507
 
                /* calculate bumpnormal */
508
 
                tex->nor[0] = Tin - mgravefunc(texvec[0] + B_OFFS, texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->mg_gain, tex->noisebasis);
509
 
                tex->nor[1] = Tin - mgravefunc(texvec[0], texvec[1] + B_OFFS, texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->mg_gain, tex->noisebasis);
510
 
                tex->nor[2] = Tin - mgravefunc(texvec[0], texvec[1], texvec[2] + B_OFFS, tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->mg_gain, tex->noisebasis);
511
 
                rv += 2;
512
 
        }
513
 
 
514
 
        Tin *= tex->ns_outscale;
515
 
 
516
 
        BRICON;
517
 
 
518
 
        if (tex->flag & TEX_COLORBAND)  return (rv + do_colorband(tex->coba));
519
 
 
520
 
        return rv;
521
 
 
522
 
}
523
 
 
524
 
 
525
 
static float mg_HTerrainTex(Tex *tex, float *texvec)
526
 
{
527
 
        int rv=0;       /* return value, int:0, col:1, nor:2, everything:3 */
528
 
 
529
 
        Tin = mg_HeteroTerrain(texvec[0], texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->noisebasis);
530
 
 
531
 
        if (tex->nor!=NULL) {
532
 
                /* calculate bumpnormal */
533
 
                tex->nor[0] = Tin - mg_HeteroTerrain(texvec[0] + B_OFFS, texvec[1], texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->noisebasis);
534
 
                tex->nor[1] = Tin - mg_HeteroTerrain(texvec[0], texvec[1] + B_OFFS, texvec[2], tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->noisebasis);
535
 
                tex->nor[2] = Tin - mg_HeteroTerrain(texvec[0], texvec[1], texvec[2] + B_OFFS, tex->mg_H, tex->mg_lacunarity, tex->mg_octaves, tex->mg_offset, tex->noisebasis);
536
 
                rv += 2;
537
 
        }
538
 
 
539
 
        Tin *= tex->ns_outscale;
540
 
 
541
 
        BRICON;
542
 
 
543
 
        if (tex->flag & TEX_COLORBAND)  return (rv + do_colorband(tex->coba));
544
 
 
545
 
        return rv;
546
 
 
547
 
}
548
 
 
549
 
 
550
 
static float mg_distNoiseTex(Tex *tex, float *texvec)
551
 
{
552
 
        int rv=0;       /* return value, int:0, col:1, nor:2, everything:3 */
553
 
 
554
 
        Tin = mg_VLNoise(texvec[0], texvec[1], texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
555
 
 
556
 
        if (tex->nor!=NULL) {
557
 
                /* calculate bumpnormal */
558
 
                tex->nor[0] = Tin - mg_VLNoise(texvec[0] + B_OFFS, texvec[1], texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
559
 
                tex->nor[1] = Tin - mg_VLNoise(texvec[0], texvec[1] + B_OFFS, texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
560
 
                tex->nor[2] = Tin - mg_VLNoise(texvec[0], texvec[1], texvec[2] + B_OFFS, tex->dist_amount, tex->noisebasis, tex->noisebasis2);
561
 
                rv += 2;
562
 
        }
563
 
 
564
 
        BRICON;
565
 
 
566
 
        if (tex->flag & TEX_COLORBAND)  return (rv + do_colorband(tex->coba));
 
636
        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);
 
637
 
 
638
        if (texres->nor!=NULL) {
 
639
                float offs= tex->nabla/tex->noisesize;  // also scaling of texvec
 
640
                
 
641
                /* calculate bumpnormal */
 
642
                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);
 
643
                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);
 
644
                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);
 
645
                
 
646
                tex_normal_derivate(tex, texres);
 
647
                rv += 2;
 
648
        }
 
649
 
 
650
        BRICONT;
 
651
 
 
652
        return rv;
 
653
 
 
654
}
 
655
 
 
656
 
 
657
static float mg_HTerrainTex(Tex *tex, float *texvec, TexResult *texres)
 
658
{
 
659
        int rv=0;       /* return value, int:0, col:1, nor:2, everything:3 */
 
660
 
 
661
        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);
 
662
 
 
663
        if (texres->nor!=NULL) {
 
664
                float offs= tex->nabla/tex->noisesize;  // also scaling of texvec
 
665
                
 
666
                /* calculate bumpnormal */
 
667
                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);
 
668
                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);
 
669
                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);
 
670
                
 
671
                tex_normal_derivate(tex, texres);
 
672
                rv += 2;
 
673
        }
 
674
 
 
675
        BRICONT;
 
676
 
 
677
        return rv;
 
678
 
 
679
}
 
680
 
 
681
 
 
682
static float mg_distNoiseTex(Tex *tex, float *texvec, TexResult *texres)
 
683
{
 
684
        int rv=0;       /* return value, int:0, col:1, nor:2, everything:3 */
 
685
 
 
686
        texres->tin = mg_VLNoise(texvec[0], texvec[1], texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
 
687
 
 
688
        if (texres->nor!=NULL) {
 
689
                float offs= tex->nabla/tex->noisesize;  // also scaling of texvec
 
690
                
 
691
                /* calculate bumpnormal */
 
692
                texres->nor[0] = mg_VLNoise(texvec[0] + offs, texvec[1], texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
 
693
                texres->nor[1] = mg_VLNoise(texvec[0], texvec[1] + offs, texvec[2], tex->dist_amount, tex->noisebasis, tex->noisebasis2);
 
694
                texres->nor[2] = mg_VLNoise(texvec[0], texvec[1], texvec[2] + offs, tex->dist_amount, tex->noisebasis, tex->noisebasis2);
 
695
 
 
696
                tex_normal_derivate(tex, texres);
 
697
                rv += 2;
 
698
        }
 
699
 
 
700
        BRICONT;
 
701
 
567
702
 
568
703
        return rv;
569
704
 
573
708
/* ------------------------------------------------------------------------- */
574
709
/* newnoise: Voronoi texture type, probably the slowest, especially with minkovsky, bumpmapping, could be done another way */
575
710
 
576
 
static float voronoiTex(Tex *tex, float *texvec)
 
711
static float voronoiTex(Tex *tex, float *texvec, TexResult *texres)
577
712
{
578
713
        int rv=0;       /* return value, int:0, col:1, nor:2, everything:3 */
579
714
        float da[4], pa[12];    /* distance and point coordinate arrays of 4 nearest neighbours */
585
720
        if (sc!=0.f) sc =  tex->ns_outscale/sc;
586
721
 
587
722
        voronoi(texvec[0], texvec[1], texvec[2], da, pa, tex->vn_mexp, tex->vn_distm);
588
 
        Tin = sc * fabs(tex->vn_w1*da[0] + tex->vn_w2*da[1] + tex->vn_w3*da[2] + tex->vn_w4*da[3]);
 
723
        texres->tin = sc * fabs(tex->vn_w1*da[0] + tex->vn_w2*da[1] + tex->vn_w3*da[2] + tex->vn_w4*da[3]);
589
724
 
590
725
        if (tex->vn_coltype) {
591
726
                float ca[3];    /* cell color */
592
727
                cellNoiseV(pa[0], pa[1], pa[2], ca);
593
 
                Tr = aw1*ca[0];
594
 
                Tg = aw1*ca[1];
595
 
                Tb = aw1*ca[2];
 
728
                texres->tr = aw1*ca[0];
 
729
                texres->tg = aw1*ca[1];
 
730
                texres->tb = aw1*ca[2];
596
731
                cellNoiseV(pa[3], pa[4], pa[5], ca);
597
 
                Tr += aw2*ca[0];
598
 
                Tg += aw2*ca[1];
599
 
                Tb += aw2*ca[2];
 
732
                texres->tr += aw2*ca[0];
 
733
                texres->tg += aw2*ca[1];
 
734
                texres->tb += aw2*ca[2];
600
735
                cellNoiseV(pa[6], pa[7], pa[8], ca);
601
 
                Tr += aw3*ca[0];
602
 
                Tg += aw3*ca[1];
603
 
                Tb += aw3*ca[2];
 
736
                texres->tr += aw3*ca[0];
 
737
                texres->tg += aw3*ca[1];
 
738
                texres->tb += aw3*ca[2];
604
739
                cellNoiseV(pa[9], pa[10], pa[11], ca);
605
 
                Tr += aw4*ca[0];
606
 
                Tg += aw4*ca[1];
607
 
                Tb += aw4*ca[2];
 
740
                texres->tr += aw4*ca[0];
 
741
                texres->tg += aw4*ca[1];
 
742
                texres->tb += aw4*ca[2];
608
743
                if (tex->vn_coltype>=2) {
609
744
                        float t1 = (da[1]-da[0])*10;
610
745
                        if (t1>1) t1=1;
611
 
                        if (tex->vn_coltype==3) t1*=Tin; else t1*=sc;
612
 
                        Tr *= t1;
613
 
                        Tg *= t1;
614
 
                        Tb *= t1;
 
746
                        if (tex->vn_coltype==3) t1*=texres->tin; else t1*=sc;
 
747
                        texres->tr *= t1;
 
748
                        texres->tg *= t1;
 
749
                        texres->tb *= t1;
615
750
                }
616
751
                else {
617
 
                        Tr *= sc;
618
 
                        Tg *= sc;
619
 
                        Tb *= sc;
 
752
                        texres->tr *= sc;
 
753
                        texres->tg *= sc;
 
754
                        texres->tb *= sc;
620
755
                }
621
756
        }
622
757
 
623
 
        if (tex->nor!=NULL) {
 
758
        if (texres->nor!=NULL) {
 
759
                float offs= tex->nabla/tex->noisesize;  // also scaling of texvec
 
760
 
624
761
                /* calculate bumpnormal */
625
 
                voronoi(texvec[0] + B_OFFS, texvec[1], texvec[2], da, pa, tex->vn_mexp,  tex->vn_distm);
626
 
                tex->nor[0] = Tin - sc * fabs(tex->vn_w1*da[0] + tex->vn_w2*da[1] + tex->vn_w3*da[2] + tex->vn_w4*da[3]);
627
 
                voronoi(texvec[0], texvec[1] + B_OFFS, texvec[2], da, pa, tex->vn_mexp,  tex->vn_distm);
628
 
                tex->nor[1] = Tin - sc * fabs(tex->vn_w1*da[0] + tex->vn_w2*da[1] + tex->vn_w3*da[2] + tex->vn_w4*da[3]);
629
 
                voronoi(texvec[0], texvec[1], texvec[2] + B_OFFS, da, pa, tex->vn_mexp,  tex->vn_distm);
630
 
                tex->nor[2] = Tin - sc * fabs(tex->vn_w1*da[0] + tex->vn_w2*da[1] + tex->vn_w3*da[2] + tex->vn_w4*da[3]);
 
762
                voronoi(texvec[0] + offs, texvec[1], texvec[2], da, pa, tex->vn_mexp,  tex->vn_distm);
 
763
                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]);
 
764
                voronoi(texvec[0], texvec[1] + offs, texvec[2], da, pa, tex->vn_mexp,  tex->vn_distm);
 
765
                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]);
 
766
                voronoi(texvec[0], texvec[1], texvec[2] + offs, da, pa, tex->vn_mexp,  tex->vn_distm);
 
767
                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]);
 
768
                
 
769
                tex_normal_derivate(tex, texres);
631
770
                rv += 2;
632
771
        }
633
772
 
634
773
        if (tex->vn_coltype) {
635
 
                BRICONRGB;
636
 
                Ta = 1.0;
 
774
                BRICONTRGB;
 
775
                texres->ta = 1.0;
637
776
                return (rv+1);
638
777
        }
639
778
        
640
 
        BRICON;
641
 
 
642
 
        if (tex->flag & TEX_COLORBAND)  return (rv + do_colorband(tex->coba));
 
779
        BRICONT;
643
780
 
644
781
        return rv;
645
782
 
648
785
 
649
786
/* ------------------------------------------------------------------------- */
650
787
 
651
 
static int texnoise(Tex *tex)
 
788
static int texnoise(Tex *tex, TexResult *texres)
652
789
{
653
790
        float div=3.0;
654
791
        int val, ran, loop;
663
800
                div*= 3.0;
664
801
        }
665
802
        
666
 
        Tin= ((float)val)/div;;
 
803
        texres->tin= ((float)val)/div;;
667
804
 
668
 
        BRICON;
669
 
        if(tex->flag & TEX_COLORBAND)  return do_colorband(tex->coba);
670
 
        
 
805
        BRICONT;
671
806
        return 0;
672
807
}
673
808
 
674
809
/* ------------------------------------------------------------------------- */
675
810
 
676
 
static int plugintex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex)
 
811
static int plugintex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres)
677
812
{
678
813
        PluginTex *pit;
679
814
        int rgbnor=0;
680
815
 
681
 
        Tin= 0.0;
 
816
        texres->tin= 0.0;
682
817
 
683
818
        pit= tex->plugin;
684
819
        if(pit && pit->doit) {
685
 
                if(tex->nor) {
686
 
                        VECCOPY(pit->result+5, tex->nor);
 
820
                if(texres->nor) {
 
821
                        VECCOPY(pit->result+5, texres->nor);
687
822
                }
688
823
                if(osatex) rgbnor= ((TexDoit)pit->doit)(tex->stype, pit->data, texvec, dxt, dyt);
689
824
                else rgbnor= ((TexDoit)pit->doit)(tex->stype, pit->data, texvec, 0, 0);
690
825
 
691
 
                Tin= pit->result[0];
 
826
                texres->tin= pit->result[0];
692
827
 
693
828
                if(rgbnor & TEX_NOR) {
694
 
                        if(tex->nor) {
695
 
                                VECCOPY(tex->nor, pit->result+5);
 
829
                        if(texres->nor) {
 
830
                                VECCOPY(texres->nor, pit->result+5);
696
831
                        }
697
832
                }
698
833
                
699
834
                if(rgbnor & TEX_RGB) {
700
 
                        Tr= pit->result[1];
701
 
                        Tg= pit->result[2];
702
 
                        Tb= pit->result[3];
703
 
                        Ta= pit->result[4];
 
835
                        texres->tr= pit->result[1];
 
836
                        texres->tg= pit->result[2];
 
837
                        texres->tb= pit->result[3];
 
838
                        texres->ta= pit->result[4];
704
839
 
705
 
                        BRICONRGB;
 
840
                        BRICONTRGB;
706
841
                }
707
842
                
708
 
                BRICON;
709
 
                if(tex->flag & TEX_COLORBAND)  rgbnor |= do_colorband(tex->coba);
 
843
                BRICONT;
710
844
        }
711
845
 
712
846
        return rgbnor;
713
847
}
714
848
 
715
 
/* *************** PROJECTIONS ******************* */
716
 
 
717
 
void tubemap(float x, float y, float z, float *adr1, float *adr2)
718
 
{
719
 
        float len;
720
 
 
721
 
        *adr2 = (z + 1.0) / 2.0;
722
 
 
723
 
        len= sqrt(x*x+y*y);
724
 
        if(len>0) {
725
 
                *adr1 = (1.0 - (atan2(x/len,y/len) / M_PI)) / 2.0;
726
 
        }
727
 
}
728
 
 
729
 
/* ------------------------------------------------------------------------- */
730
 
 
731
 
void spheremap(float x, float y, float z, float *adr1, float *adr2)
732
 
{
733
 
        float len;
734
 
 
735
 
        len= sqrt(x*x+y*y+z*z);
736
 
        if(len>0.0) {
737
 
                
738
 
                if(x==0.0 && y==0.0) *adr1= 0.0;        /* othwise domain error */
739
 
                else *adr1 = (1.0 - atan2(x,y)/M_PI )/2.0;
740
 
 
741
 
                z/=len;
742
 
                *adr2 = 1.0- saacos(z)/M_PI;
743
 
        }
744
 
}
745
 
 
746
 
/* ------------------------------------------------------------------------- */
747
849
 
748
850
static int cubemap_glob(MTex *mtex, VlakRen *vlr, float x, float y, float z, float *adr1, float *adr2)
749
851
{
1034
1136
 
1035
1137
/* ************************************** */
1036
1138
 
1037
 
int multitex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex)
 
1139
static int multitex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres)
1038
1140
{
 
1141
        int retval=0; /* return value, int:0, col:1, nor:2, everything:3 */
1039
1142
 
 
1143
        texres->talpha= 0;      /* is set when image texture returns alpha (considered premul) */
 
1144
        
1040
1145
        switch(tex->type) {
1041
1146
        
1042
1147
        case 0:
1043
 
                Tin= 0.0;
 
1148
                texres->tin= 0.0;
1044
1149
                return 0;
1045
1150
        case TEX_CLOUDS:
1046
 
                return clouds(tex, texvec);
 
1151
                retval= clouds(tex, texvec, texres);
 
1152
                break;
1047
1153
        case TEX_WOOD:
1048
 
                return wood(tex, texvec); 
 
1154
                retval= wood(tex, texvec, texres); 
 
1155
                break;
1049
1156
        case TEX_MARBLE:
1050
 
                return marble(tex, texvec); 
 
1157
                retval= marble(tex, texvec, texres); 
 
1158
                break;
1051
1159
        case TEX_MAGIC:
1052
 
                return magic(tex, texvec); 
 
1160
                retval= magic(tex, texvec, texres); 
 
1161
                break;
1053
1162
        case TEX_BLEND:
1054
 
                return blend(tex, texvec);
 
1163
                retval= blend(tex, texvec, texres);
 
1164
                break;
1055
1165
        case TEX_STUCCI:
1056
 
                Tin= 0.0;
1057
 
                return stucci(tex, texvec); 
 
1166
                retval= stucci(tex, texvec, texres); 
 
1167
                if (tex->flag & TEX_COLORBAND);
 
1168
                else texres->tin= 0.0;  // stucci doesnt return Tin, for backwards compat...
 
1169
                break;
1058
1170
        case TEX_NOISE:
1059
 
                return texnoise(tex); 
 
1171
                retval= texnoise(tex, texres); 
 
1172
                break;
1060
1173
        case TEX_IMAGE:
1061
 
                if(osatex) return imagewraposa(tex, texvec, dxt, dyt); 
1062
 
                else return imagewrap(tex, texvec); 
 
1174
                if(osatex) retval= imagewraposa(tex, tex->ima, texvec, dxt, dyt, texres); 
 
1175
                else retval= imagewrap(tex, tex->ima, texvec, texres); 
 
1176
                break;
1063
1177
        case TEX_PLUGIN:
1064
 
                return plugintex(tex, texvec, dxt, dyt, osatex);
 
1178
                retval= plugintex(tex, texvec, dxt, dyt, osatex, texres);
 
1179
                break;
1065
1180
        case TEX_ENVMAP:
1066
 
                return envmaptex(tex, texvec, dxt, dyt, osatex);
 
1181
                retval= envmaptex(tex, texvec, dxt, dyt, osatex, texres);
 
1182
                break;
1067
1183
        case TEX_MUSGRAVE:
1068
1184
                /* newnoise: musgrave types */
1069
1185
                
1070
 
                /* ton: added this, for Blender convention reason. scaling texvec here is so-so... */
 
1186
                /* ton: added this, for Blender convention reason. scaling texvec here is so-so ... */
1071
1187
                VecMulf(texvec, 1.0/tex->noisesize);
1072
1188
                
1073
1189
                switch(tex->stype) {
1074
1190
                case TEX_MFRACTAL:
1075
1191
                case TEX_FBM:
1076
 
                        return mg_mFractalOrfBmTex(tex, texvec);
 
1192
                        retval= mg_mFractalOrfBmTex(tex, texvec, texres);
 
1193
                        break;
1077
1194
                case TEX_RIDGEDMF:
1078
1195
                case TEX_HYBRIDMF:
1079
 
                        return mg_ridgedOrHybridMFTex(tex, texvec);
 
1196
                        retval= mg_ridgedOrHybridMFTex(tex, texvec, texres);
 
1197
                        break;
1080
1198
                case TEX_HTERRAIN:
1081
 
                        return mg_HTerrainTex(tex, texvec);
 
1199
                        retval= mg_HTerrainTex(tex, texvec, texres);
 
1200
                        break;
1082
1201
                }
1083
1202
                break;
1084
1203
        /* newnoise: voronoi type */
1085
1204
        case TEX_VORONOI:
1086
 
                /* ton: added this, for Blender convention reason. scaling texvec here is so-so... */
 
1205
                /* ton: added this, for Blender convention reason. scaling texvec here is so-so ... */
1087
1206
                VecMulf(texvec, 1.0/tex->noisesize);
1088
1207
                
1089
 
                return voronoiTex(tex, texvec);
 
1208
                retval= voronoiTex(tex, texvec, texres);
 
1209
                break;
1090
1210
        case TEX_DISTNOISE:
1091
 
                /* ton: added this, for Blender convention reason. scaling texvec here is so-so... */
 
1211
                /* ton: added this, for Blender convention reason. scaling texvec here is so-so ... */
1092
1212
                VecMulf(texvec, 1.0/tex->noisesize);
1093
1213
                
1094
 
                return mg_distNoiseTex(tex, texvec);
1095
 
        }
1096
 
        return 0;
 
1214
                retval= mg_distNoiseTex(tex, texvec, texres);
 
1215
                break;
 
1216
        }
 
1217
 
 
1218
        if (tex->flag & TEX_COLORBAND) {
 
1219
                float col[4];
 
1220
                if (do_colorband(tex->coba, texres->tin, col)) {
 
1221
                        texres->talpha= 1;
 
1222
                        texres->tr= col[0];
 
1223
                        texres->tg= col[1];
 
1224
                        texres->tb= col[2];
 
1225
                        texres->ta= col[3];
 
1226
                        retval |= 1;
 
1227
                }
 
1228
        }
 
1229
        return retval;
 
1230
}
 
1231
 
 
1232
int multitex_ext(Tex *tex, float *texvec, float *tin, float *tr, float *tg, float *tb, float *ta)
 
1233
{
 
1234
        TexResult texr;
 
1235
        float dummy[3];
 
1236
        int retval;
 
1237
        
 
1238
        /* does not return Tin, hackish... */
 
1239
        if(tex->type==TEX_STUCCI) {
 
1240
                texr.nor= dummy;
 
1241
                dummy[0]= 1.0;
 
1242
                dummy[1]= dummy[2]= 0.0;
 
1243
        }
 
1244
        else texr.nor= NULL;
 
1245
        
 
1246
        retval= multitex(tex, texvec, NULL, NULL, 0, &texr);
 
1247
        if(tex->type==TEX_STUCCI) {
 
1248
                *tin= 0.5 + 0.7*texr.nor[0];
 
1249
                CLAMP(*tin, 0.0, 1.0);
 
1250
        }
 
1251
        else *tin= texr.tin;
 
1252
        *tr= texr.tr;
 
1253
        *tg= texr.tg;
 
1254
        *tb= texr.tb;
 
1255
        *ta= texr.ta;
 
1256
        return retval;
1097
1257
}
1098
1258
 
1099
1259
/* ------------------------------------------------------------------------- */
1100
1260
 
 
1261
/* in = destination, tex = texture, out = previous color */
 
1262
/* fact = texture strength, facg = button strength value */
 
1263
static void texture_rgb_blend(float *in, float *tex, float *out, float fact, float facg, int blendtype)
 
1264
{
 
1265
        float facm, col;
 
1266
        
 
1267
        switch(blendtype) {
 
1268
        case MTEX_BLEND:
 
1269
                fact*= facg;
 
1270
                facm= 1.0-fact;
 
1271
 
 
1272
                in[0]= (fact*tex[0] + facm*out[0]);
 
1273
                in[1]= (fact*tex[1] + facm*out[1]);
 
1274
                in[2]= (fact*tex[2] + facm*out[2]);
 
1275
                break;
 
1276
 
 
1277
        case MTEX_MUL:
 
1278
                fact*= facg;
 
1279
                facm= 1.0-facg;
 
1280
                in[0]= (facm+fact*tex[0])*out[0];
 
1281
                in[1]= (facm+fact*tex[1])*out[1];
 
1282
                in[2]= (facm+fact*tex[2])*out[2];
 
1283
                break;
 
1284
 
 
1285
        case MTEX_SCREEN:
 
1286
                fact*= facg;
 
1287
                facm= 1.0-facg;
 
1288
                in[0]= 1.0-(facm+fact*(1.0-tex[0]))*(1.0-out[0]);
 
1289
                in[1]= 1.0-(facm+fact*(1.0-tex[1]))*(1.0-out[1]);
 
1290
                in[2]= 1.0-(facm+fact*(1.0-tex[2]))*(1.0-out[2]);
 
1291
                break;
 
1292
 
 
1293
        case MTEX_SUB:
 
1294
                fact= -fact;
 
1295
        case MTEX_ADD:
 
1296
                fact*= facg;
 
1297
                in[0]= (fact*tex[0] + out[0]);
 
1298
                in[1]= (fact*tex[1] + out[1]);
 
1299
                in[2]= (fact*tex[2] + out[2]);
 
1300
                break;
 
1301
 
 
1302
        case MTEX_DIV:
 
1303
                fact*= facg;
 
1304
                facm= 1.0-fact;
 
1305
                
 
1306
                if(tex[0]!=0.0)
 
1307
                        in[0]= facm*out[0] + fact*out[0]/tex[0];
 
1308
                if(tex[1]!=0.0)
 
1309
                        in[1]= facm*out[1] + fact*out[1]/tex[1];
 
1310
                if(tex[2]!=0.0)
 
1311
                        in[2]= facm*out[2] + fact*out[2]/tex[2];
 
1312
 
 
1313
                break;
 
1314
 
 
1315
        case MTEX_DIFF:
 
1316
                fact*= facg;
 
1317
                facm= 1.0-fact;
 
1318
                in[0]= facm*out[0] + fact*fabs(tex[0]-out[0]);
 
1319
                in[1]= facm*out[1] + fact*fabs(tex[1]-out[1]);
 
1320
                in[2]= facm*out[2] + fact*fabs(tex[2]-out[2]);
 
1321
                break;
 
1322
 
 
1323
        case MTEX_DARK:
 
1324
                fact*= facg;
 
1325
                facm= 1.0-fact;
 
1326
                
 
1327
                col= fact*tex[0];
 
1328
                if(col < out[0]) in[0]= col; else in[0]= out[0];
 
1329
                col= fact*tex[1];
 
1330
                if(col < out[1]) in[1]= col; else in[1]= out[1];
 
1331
                col= fact*tex[2];
 
1332
                if(col < out[2]) in[2]= col; else in[2]= out[2];
 
1333
                break;
 
1334
 
 
1335
        case MTEX_LIGHT:
 
1336
                fact*= facg;
 
1337
                facm= 1.0-fact;
 
1338
                
 
1339
                col= fact*tex[0];
 
1340
                if(col > out[0]) in[0]= col; else in[0]= out[0];
 
1341
                col= fact*tex[1];
 
1342
                if(col > out[1]) in[1]= col; else in[1]= out[1];
 
1343
                col= fact*tex[2];
 
1344
                if(col > out[2]) in[2]= col; else in[2]= out[2];
 
1345
                break;
 
1346
        }
 
1347
 
 
1348
 
 
1349
}
 
1350
 
 
1351
static float texture_value_blend(float tex, float out, float fact, float facg, int blendtype, int flip)
 
1352
{
 
1353
        float in=0.0, facm, col;
 
1354
        
 
1355
        fact*= facg;
 
1356
        facm= 1.0-fact;
 
1357
        if(flip) SWAP(float, fact, facm);
 
1358
 
 
1359
        switch(blendtype) {
 
1360
        case MTEX_BLEND:
 
1361
                in= fact*tex + facm*out;
 
1362
                break;
 
1363
 
 
1364
        case MTEX_MUL:
 
1365
                facm= 1.0-facg;
 
1366
                in= (facm+fact*tex)*out;
 
1367
                break;
 
1368
 
 
1369
        case MTEX_SCREEN:
 
1370
                facm= 1.0-facg;
 
1371
                in= 1.0-(facm+fact*(1.0-tex))*(1.0-out);
 
1372
                break;
 
1373
 
 
1374
        case MTEX_SUB:
 
1375
                fact= -fact;
 
1376
        case MTEX_ADD:
 
1377
                in= fact*tex + out;
 
1378
                break;
 
1379
 
 
1380
        case MTEX_DIV:
 
1381
                if(tex!=0.0)
 
1382
                        in= facm*out + fact*out/tex;
 
1383
                break;
 
1384
 
 
1385
        case MTEX_DIFF:
 
1386
                in= facm*out + fact*fabs(tex-out);
 
1387
                break;
 
1388
 
 
1389
        case MTEX_DARK:
 
1390
                col= fact*tex;
 
1391
                if(col < out) in= col; else in= out;
 
1392
                break;
 
1393
 
 
1394
        case MTEX_LIGHT:
 
1395
                col= fact*tex;
 
1396
                if(col > out) in= col; else in= out;
 
1397
                break;
 
1398
        }
 
1399
        
 
1400
        return in;
 
1401
}
 
1402
 
 
1403
 
1101
1404
void do_material_tex(ShadeInput *shi)
1102
1405
{
1103
 
        Object *ob;
1104
 
        Material *mat_col, *mat_colspec, *mat_colmir, *mat_ref, *mat_amb;
1105
 
        Material *mat_spec, *mat_har, *mat_emit, *mat_alpha, *mat_ray_mirr, *mat_translu;
1106
1406
        MTex *mtex;
1107
1407
        Tex *tex;
1108
 
        float *co = NULL, *dx = NULL, *dy = NULL, fact, 
1109
 
                facm, factt, facmm, facmul = 0.0, stencilTin=1.0;
1110
 
        float texvec[3], dxt[3], dyt[3], tempvec[3], norvec[3], Tnor=1.0;
1111
 
        int tex_nr, rgbnor= 0;
 
1408
        TexResult texres;
 
1409
        float *co = NULL, *dx = NULL, *dy = NULL;
 
1410
        float fact, facm, factt, facmm, stencilTin=1.0;
 
1411
        float texvec[3], dxt[3], dyt[3], tempvec[3], norvec[3], warpvec[3], Tnor=1.0;
 
1412
        int tex_nr, rgbnor= 0, warpdone=0;
1112
1413
 
1113
1414
        /* here: test flag if there's a tex (todo) */
1114
1415
        
1115
 
        mat_col=mat_colspec=mat_colmir=mat_ref=mat_spec=mat_har=mat_emit=mat_alpha=mat_ray_mirr=mat_translu=mat_amb= shi->mat;
1116
 
        
1117
 
        for(tex_nr=0; tex_nr<8; tex_nr++) {
 
1416
        for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
1118
1417
                
1119
1418
                /* separate tex switching */
1120
1419
                if(shi->mat->septex & (1<<tex_nr)) continue;
1127
1426
                        
1128
1427
                        /* which coords */
1129
1428
                        if(mtex->texco==TEXCO_ORCO) {
1130
 
                                co= shi->lo; dx= O.dxlo; dy= O.dylo;
 
1429
                                co= shi->lo; dx= shi->dxlo; dy= shi->dylo;
1131
1430
                        }
1132
1431
                        else if(mtex->texco==TEXCO_STICKY) {
1133
 
                                co= shi->sticky; dx= O.dxsticky; dy= O.dysticky;
 
1432
                                co= shi->sticky; dx= shi->dxsticky; dy= shi->dysticky;
1134
1433
                        }
1135
1434
                        else if(mtex->texco==TEXCO_OBJECT) {
1136
 
                                ob= mtex->object;
 
1435
                                Object *ob= mtex->object;
1137
1436
                                if(ob) {
1138
1437
                                        co= tempvec;
1139
1438
                                        dx= dxt;
1141
1440
                                        VECCOPY(tempvec, shi->co);
1142
1441
                                        MTC_Mat4MulVecfl(ob->imat, tempvec);
1143
1442
                                        if(shi->osatex) {
1144
 
                                                VECCOPY(dxt, O.dxco);
1145
 
                                                VECCOPY(dyt, O.dyco);
 
1443
                                                VECCOPY(dxt, shi->dxco);
 
1444
                                                VECCOPY(dyt, shi->dyco);
1146
1445
                                                MTC_Mat4Mul3Vecfl(ob->imat, dxt);
1147
1446
                                                MTC_Mat4Mul3Vecfl(ob->imat, dyt);
1148
1447
                                        }
1150
1449
                                else {
1151
1450
                                        /* if object doesn't exist, do not use orcos (not initialized) */
1152
1451
                                        co= shi->co;
1153
 
                                        dx= O.dxco; dy= O.dyco;
 
1452
                                        dx= shi->dxco; dy= shi->dyco;
1154
1453
                                }
1155
1454
                        }
1156
1455
                        else if(mtex->texco==TEXCO_REFL) {
1157
 
                                co= shi->ref; dx= O.dxref; dy= O.dyref;
 
1456
                                co= shi->ref; dx= shi->dxref; dy= shi->dyref;
1158
1457
                        }
1159
1458
                        else if(mtex->texco==TEXCO_NORM) {
1160
 
                                co= shi->orn; dx= O.dxno; dy= O.dyno;
 
1459
                                co= shi->orn; dx= shi->dxno; dy= shi->dyno;
1161
1460
                        }
1162
1461
                        else if(mtex->texco==TEXCO_GLOB) {
1163
 
                                co= shi->gl; dx= O.dxco; dy= O.dyco;
 
1462
                                co= shi->gl; dx= shi->dxco; dy= shi->dyco;
1164
1463
                        }
1165
1464
                        else if(mtex->texco==TEXCO_UV) {
1166
 
                                co= shi->uv; dx= O.dxuv; dy= O.dyuv; 
 
1465
                                co= shi->uv; dx= shi->dxuv; dy= shi->dyuv; 
1167
1466
                        }
1168
1467
                        else if(mtex->texco==TEXCO_WINDOW) {
1169
 
                                co= shi->winco; dx= O.dxwin; dy= O.dywin;
 
1468
                                co= shi->winco; dx= shi->dxwin; dy= shi->dywin;
1170
1469
                        }
 
1470
                        else continue;  // can happen when texco defines disappear and it renders old files
1171
1471
                        
1172
1472
                        /* de pointer defines if bumping happens */
1173
 
                        if(mtex->mapto & (MAP_NORM|MAP_DISPLACE)) {
1174
 
                                tex->nor= norvec;
 
1473
                        if(mtex->mapto & (MAP_NORM|MAP_DISPLACE|MAP_WARP)) {
 
1474
                                texres.nor= norvec;
1175
1475
                                norvec[0]= norvec[1]= norvec[2]= 0.0;
1176
1476
                        }
1177
 
                        else tex->nor= NULL;
1178
 
 
 
1477
                        else texres.nor= NULL;
 
1478
                        
 
1479
                        if(warpdone) {
 
1480
                                VECADD(tempvec, co, warpvec);
 
1481
                                co= tempvec;
 
1482
                        }
 
1483
                        
1179
1484
                        if(tex->type==TEX_IMAGE) {
1180
1485
 
1181
1486
                                /* new: first swap coords, then map, then trans/scale */
1250
1555
                                }
1251
1556
                        }
1252
1557
 
1253
 
                        rgbnor= multitex(tex, texvec, dxt, dyt, shi->osatex);
 
1558
                        rgbnor= multitex(tex, texvec, dxt, dyt, shi->osatex, &texres);
1254
1559
 
1255
1560
                        /* texture output */
1256
1561
 
1257
1562
                        if( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
1258
 
                                Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
1259
 
                                rgbnor-= 1;
 
1563
                                texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
 
1564
                                rgbnor-= TEX_RGB;
1260
1565
                        }
1261
1566
                        if(mtex->texflag & MTEX_NEGATIVE) {
1262
1567
                                if(rgbnor & TEX_RGB) {
1263
 
                                        Tr= 1.0-Tr;
1264
 
                                        Tg= 1.0-Tg;
1265
 
                                        Tb= 1.0-Tb;
 
1568
                                        texres.tr= 1.0-texres.tr;
 
1569
                                        texres.tg= 1.0-texres.tg;
 
1570
                                        texres.tb= 1.0-texres.tb;
1266
1571
                                }
1267
 
                                Tin= 1.0-Tin;
 
1572
                                texres.tin= 1.0-texres.tin;
1268
1573
                        }
1269
1574
                        if(mtex->texflag & MTEX_STENCIL) {
1270
1575
                                if(rgbnor & TEX_RGB) {
1271
 
                                        fact= Ta;
1272
 
                                        Ta*= stencilTin;
 
1576
                                        fact= texres.ta;
 
1577
                                        texres.ta*= stencilTin;
1273
1578
                                        stencilTin*= fact;
1274
1579
                                }
1275
1580
                                else {
1276
 
                                        fact= Tin;
1277
 
                                        Tin*= stencilTin;
 
1581
                                        fact= texres.tin;
 
1582
                                        texres.tin*= stencilTin;
1278
1583
                                        stencilTin*= fact;
1279
1584
                                }
1280
1585
                        }
1281
1586
                        else {
1282
 
                                Ta*= stencilTin;
 
1587
                                texres.ta*= stencilTin;
1283
1588
                                Tnor*= stencilTin;
1284
 
                                Tin*= stencilTin;
1285
 
                        }
1286
 
 
1287
 
                        if(tex->nor && (rgbnor & TEX_NOR)==0) {
1288
 
                                /* make our own normal */
1289
 
                                if(rgbnor & TEX_RGB) {
1290
 
                                        tex->nor[0]= Tr;
1291
 
                                        tex->nor[1]= Tg;
1292
 
                                        tex->nor[2]= Tb;
1293
 
                                }
1294
 
                                else {
1295
 
                                        float co= 0.5*cos(Tin-0.5);
1296
 
                                        float si= 0.5*sin(Tin-0.5);
1297
 
                                        float f1, f2;
1298
 
 
1299
 
                                        f1= shi->vn[0];
1300
 
                                        f2= shi->vn[1];
1301
 
                                        tex->nor[0]= f1*co+f2*si;
1302
 
                                        tex->nor[1]= f2*co-f1*si;
1303
 
                                        f1= shi->vn[1];
1304
 
                                        f2= shi->vn[2];
1305
 
                                        tex->nor[1]= f1*co+f2*si;
1306
 
                                        tex->nor[2]= f2*co-f1*si;
1307
 
                                }
1308
 
                        }
1309
 
 
 
1589
                                texres.tin*= stencilTin;
 
1590
                        }
 
1591
                        
 
1592
                        
 
1593
                        if(texres.nor) {
 
1594
                                if((rgbnor & TEX_NOR)==0) {
 
1595
                                        /* make our own normal */
 
1596
                                        if(rgbnor & TEX_RGB) {
 
1597
                                                texres.nor[0]= texres.tr;
 
1598
                                                texres.nor[1]= texres.tg;
 
1599
                                                texres.nor[2]= texres.tb;
 
1600
                                        }
 
1601
                                        else {
 
1602
                                                float co= 0.5*cos(texres.tin-0.5);
 
1603
                                                float si= 0.5*sin(texres.tin-0.5);
 
1604
                                                float f1, f2;
 
1605
 
 
1606
                                                f1= shi->vn[0];
 
1607
                                                f2= shi->vn[1];
 
1608
                                                texres.nor[0]= f1*co+f2*si;
 
1609
                                                texres.nor[1]= f2*co-f1*si;
 
1610
                                                f1= shi->vn[1];
 
1611
                                                f2= shi->vn[2];
 
1612
                                                texres.nor[1]= f1*co+f2*si;
 
1613
                                                texres.nor[2]= f2*co-f1*si;
 
1614
                                        }
 
1615
                                }
 
1616
                                // warping, local space
 
1617
                                if(mtex->mapto & MAP_WARP) {
 
1618
                                        warpvec[0]= mtex->warpfac*texres.nor[0];
 
1619
                                        warpvec[1]= mtex->warpfac*texres.nor[1];
 
1620
                                        warpvec[2]= mtex->warpfac*texres.nor[2];
 
1621
                                        warpdone= 1;
 
1622
                                }
 
1623
                                
 
1624
                                if(mtex->texflag & MTEX_VIEWSPACE) {
 
1625
                                        // rotate to global coords
 
1626
                                        if(mtex->texco==TEXCO_ORCO || mtex->texco==TEXCO_UV) {
 
1627
                                                if(shi->vlr && shi->vlr->ob) {
 
1628
                                                        float len= Normalise(texres.nor);
 
1629
                                                        // can be optimized... (ton)
 
1630
                                                        Mat4Mul3Vecfl(shi->vlr->ob->obmat, texres.nor);
 
1631
                                                        Mat4Mul3Vecfl(R.viewmat, texres.nor);
 
1632
                                                        Normalise(texres.nor);
 
1633
                                                        VecMulf(texres.nor, len);
 
1634
                                                }
 
1635
                                        }
 
1636
                                }
 
1637
                        }
1310
1638
 
1311
1639
                        /* mapping */
1312
1640
                        if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
1313
 
 
 
1641
                                float tcol[3];
 
1642
                                
 
1643
                                tcol[0]=texres.tr; tcol[1]=texres.tg; tcol[2]=texres.tb;
 
1644
                                
1314
1645
                                if((rgbnor & TEX_RGB)==0) {
1315
 
                                        Tr= mtex->r;
1316
 
                                        Tg= mtex->g;
1317
 
                                        Tb= mtex->b;
 
1646
                                        tcol[0]= mtex->r;
 
1647
                                        tcol[1]= mtex->g;
 
1648
                                        tcol[2]= mtex->b;
1318
1649
                                }
1319
1650
                                else if(mtex->mapto & MAP_ALPHA) {
1320
 
                                        if(mtex->texflag & MTEX_ALPHAMIX) Tin= Ta;
1321
 
                                        else Tin= stencilTin;
 
1651
                                        texres.tin= stencilTin;
1322
1652
                                }
1323
 
                                else Tin= Ta;
1324
 
 
1325
 
                                fact= Tin*mtex->colfac;
1326
 
                                facm= 1.0-fact;
1327
 
                                if(mtex->blendtype==MTEX_MUL) facm= 1.0-mtex->colfac;
1328
 
                                if(mtex->blendtype==MTEX_SUB) fact= -fact;
1329
 
 
 
1653
                                else texres.tin= texres.ta;
 
1654
                                
1330
1655
                                if(mtex->mapto & MAP_COL) {
1331
 
                                        if(mtex->blendtype==MTEX_BLEND) {
1332
 
                                                shi->matren->r= (fact*Tr + facm*mat_col->r);
1333
 
                                                shi->matren->g= (fact*Tg + facm*mat_col->g);
1334
 
                                                shi->matren->b= (fact*Tb + facm*mat_col->b);
1335
 
                                        }
1336
 
                                        else if(mtex->blendtype==MTEX_MUL) {
1337
 
                                                shi->matren->r= (facm+fact*Tr)*mat_col->r;
1338
 
                                                shi->matren->g= (facm+fact*Tg)*mat_col->g;
1339
 
                                                shi->matren->b= (facm+fact*Tb)*mat_col->b;
1340
 
                                        }
1341
 
                                        else {
1342
 
                                                shi->matren->r= (fact*Tr + mat_col->r);
1343
 
                                                shi->matren->g= (fact*Tg + mat_col->g);
1344
 
                                                shi->matren->b= (fact*Tb + mat_col->b);
1345
 
                                        }
1346
 
                                        mat_col= shi->matren;
 
1656
                                        texture_rgb_blend(&shi->r, tcol, &shi->r, texres.tin, mtex->colfac, mtex->blendtype);
1347
1657
                                }
1348
1658
                                if(mtex->mapto & MAP_COLSPEC) {
1349
 
                                        if(mtex->blendtype==MTEX_BLEND) {
1350
 
                                                shi->matren->specr= (fact*Tr + facm*mat_colspec->specr);
1351
 
                                                shi->matren->specg= (fact*Tg + facm*mat_colspec->specg);
1352
 
                                                shi->matren->specb= (fact*Tb + facm*mat_colspec->specb);
1353
 
                                        }
1354
 
                                        else if(mtex->blendtype==MTEX_MUL) {
1355
 
                                                shi->matren->specr= (facm+fact*Tr)*mat_colspec->specr;
1356
 
                                                shi->matren->specg= (facm+fact*Tg)*mat_colspec->specg;
1357
 
                                                shi->matren->specb= (facm+fact*Tb)*mat_colspec->specb;
1358
 
                                        }
1359
 
                                        else {
1360
 
                                                shi->matren->specr= (fact*Tr + mat_colspec->specr);
1361
 
                                                shi->matren->specg= (fact*Tg + mat_colspec->specg);
1362
 
                                                shi->matren->specb= (fact*Tb + mat_colspec->specb);
1363
 
                                        }
1364
 
                                        mat_colspec= shi->matren;
 
1659
                                        texture_rgb_blend(&shi->specr, tcol, &shi->specr, texres.tin, mtex->colfac, mtex->blendtype);
1365
1660
                                }
1366
1661
                                if(mtex->mapto & MAP_COLMIR) {
1367
 
                                        if(mtex->blendtype==MTEX_BLEND) {
1368
 
                                                // exception for envmap only
1369
 
                                                if(tex->type==TEX_ENVMAP) {
1370
 
                                                        shi->refcol[0]= fact + facm*shi->refcol[0];
1371
 
                                                        shi->refcol[1]= fact*Tr + facm*shi->refcol[1];
1372
 
                                                        shi->refcol[2]= fact*Tg + facm*shi->refcol[2];
1373
 
                                                        shi->refcol[3]= fact*Tb + facm*shi->refcol[3];
1374
 
                                                } else {
1375
 
                                                        shi->matren->mirr= fact*Tr + facm*mat_colmir->mirr;
1376
 
                                                        shi->matren->mirg= fact*Tg + facm*mat_colmir->mirg;
1377
 
                                                        shi->matren->mirb= fact*Tb + facm*mat_colmir->mirb;
1378
 
                                                }
1379
 
                                        }
1380
 
                                        else if(mtex->blendtype==MTEX_MUL) {
1381
 
                                                shi->matren->mirr= (facm+fact*Tr)*mat_colmir->mirr;
1382
 
                                                shi->matren->mirg= (facm+fact*Tg)*mat_colmir->mirg;
1383
 
                                                shi->matren->mirb= (facm+fact*Tb)*mat_colmir->mirb;
 
1662
                                        // exception for envmap only
 
1663
                                        if(tex->type==TEX_ENVMAP && mtex->blendtype==MTEX_BLEND) {
 
1664
                                                fact= texres.tin*mtex->colfac;
 
1665
                                                facm= 1.0- fact;
 
1666
                                                shi->refcol[0]= fact + facm*shi->refcol[0];
 
1667
                                                shi->refcol[1]= fact*tcol[0] + facm*shi->refcol[1];
 
1668
                                                shi->refcol[2]= fact*tcol[1] + facm*shi->refcol[2];
 
1669
                                                shi->refcol[3]= fact*tcol[2] + facm*shi->refcol[3];
1384
1670
                                        }
1385
1671
                                        else {
1386
 
                                                shi->matren->mirr= (fact*Tr + mat_colmir->mirr);
1387
 
                                                shi->matren->mirg= (fact*Tg + mat_colmir->mirg);
1388
 
                                                shi->matren->mirb= (fact*Tb + mat_colmir->mirb);
 
1672
                                                texture_rgb_blend(&shi->mirr, tcol, &shi->mirr, texres.tin, mtex->colfac, mtex->blendtype);
1389
1673
                                        }
1390
 
                                        mat_colmir= shi->matren;
1391
1674
                                }
1392
1675
                        }
1393
1676
                        if( (mtex->mapto & MAP_NORM) ) {
1394
 
                                if(tex->nor) {
 
1677
                                if(texres.nor) {
1395
1678
                                        
1396
1679
                                        if(mtex->maptoneg & MAP_NORM) tex->norfac= -mtex->norfac;
1397
1680
                                        else tex->norfac= mtex->norfac;
1398
 
 
1399
 
                                        shi->vn[0]+= Tnor*tex->norfac*tex->nor[0];
1400
 
                                        shi->vn[1]+= Tnor*tex->norfac*tex->nor[1];
1401
 
                                        shi->vn[2]+= Tnor*tex->norfac*tex->nor[2];
1402
 
                                        
 
1681
                                        
 
1682
                                        /* we need to code blending modes for normals too once.. now 1 exception hardcoded */
 
1683
                                        
 
1684
                                        if(tex->type==TEX_IMAGE && (tex->imaflag & TEX_NORMALMAP)) {
 
1685
                                                fact= Tnor*tex->norfac;
 
1686
                                                if(fact>1.0) fact= 1.0; else if(fact<-1.0) fact= -1.0;
 
1687
                                                facm= 1.0- fact;
 
1688
                                                shi->vn[0]= facm*shi->vn[0] + fact*texres.nor[0];
 
1689
                                                shi->vn[1]= facm*shi->vn[1] + fact*texres.nor[1];
 
1690
                                                shi->vn[2]= facm*shi->vn[2] + fact*texres.nor[2];
 
1691
                                        }
 
1692
                                        else {
 
1693
                                                shi->vn[0]+= Tnor*tex->norfac*texres.nor[0];
 
1694
                                                shi->vn[1]+= Tnor*tex->norfac*texres.nor[1];
 
1695
                                                shi->vn[2]+= Tnor*tex->norfac*texres.nor[2];
 
1696
                                        }                                       
1403
1697
                                        Normalise(shi->vn);
1404
1698
                                        
1405
1699
                                        /* this makes sure the bump is passed on to the next texture */
1406
 
                                        shi->orn[0]= shi->vn[0];
1407
 
                                        shi->orn[1]= shi->vn[1];
1408
 
                                        shi->orn[2]= shi->vn[2];
 
1700
                                        shi->orn[0]= -shi->vn[0];
 
1701
                                        shi->orn[1]= -shi->vn[1];
 
1702
                                        shi->orn[2]= -shi->vn[2];
1409
1703
                                        
1410
1704
                                        /* reflection vector */
1411
1705
                                        calc_R_ref(shi);
1415
1709
                        if( mtex->mapto & MAP_DISPLACE ) {
1416
1710
                                /* Now that most textures offer both Nor and Intensity, allow  */
1417
1711
                                /* both to work, and let user select with slider.   */
1418
 
                                if(tex->nor) {
 
1712
                                if(texres.nor) {
1419
1713
                                        if(mtex->maptoneg & MAP_DISPLACE) tex->norfac= -mtex->norfac;
1420
1714
                                        else tex->norfac= mtex->norfac;
1421
1715
 
1422
 
                                        shi->displace[0]+= 0.2f*Tnor*tex->norfac*tex->nor[0];
1423
 
                                        shi->displace[1]+= 0.2f*Tnor*tex->norfac*tex->nor[1];
1424
 
                                        shi->displace[2]+= 0.2f*Tnor*tex->norfac*tex->nor[2];
 
1716
                                        shi->displace[0]+= 0.2f*Tnor*tex->norfac*texres.nor[0];
 
1717
                                        shi->displace[1]+= 0.2f*Tnor*tex->norfac*texres.nor[1];
 
1718
                                        shi->displace[2]+= 0.2f*Tnor*tex->norfac*texres.nor[2];
1425
1719
                                }
1426
1720
                                
1427
1721
                                if(rgbnor & TEX_RGB) {
1428
 
                                        if(Talpha) Tin= Ta;
1429
 
                                        else Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
 
1722
                                        if(texres.talpha) texres.tin= texres.ta;
 
1723
                                        else texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
1430
1724
                                }
1431
1725
 
1432
1726
                                if(mtex->maptoneg & MAP_DISPLACE) {
1433
 
                                        factt= (0.5-Tin)*mtex->dispfac; facmm= 1.0-factt;
 
1727
                                        factt= (0.5-texres.tin)*mtex->dispfac; facmm= 1.0-factt;
1434
1728
                                }
1435
1729
                                else {
1436
 
                                        factt= (Tin-0.5)*mtex->dispfac; facmm= 1.0-factt;
 
1730
                                        factt= (texres.tin-0.5)*mtex->dispfac; facmm= 1.0-factt;
1437
1731
                                }
1438
1732
 
1439
1733
                                if(mtex->blendtype==MTEX_BLEND) {
1457
1751
 
1458
1752
                        if(mtex->mapto & MAP_VARS) {
1459
1753
                                if(rgbnor & TEX_RGB) {
1460
 
                                        if(Talpha) Tin= Ta;
1461
 
                                        else Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
 
1754
                                        if(texres.talpha) texres.tin= texres.ta;
 
1755
                                        else texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
1462
1756
                                }
1463
1757
 
1464
 
                                fact= Tin*mtex->varfac;
1465
 
                                facm= 1.0-fact;
1466
 
                                if(mtex->blendtype==MTEX_MUL) facmul= 1.0-mtex->varfac;
1467
 
                                if(mtex->blendtype==MTEX_SUB) fact= -fact;
1468
 
 
1469
1758
                                if(mtex->mapto & MAP_REF) {
1470
 
                                        if(mtex->maptoneg & MAP_REF) {factt= facm; facmm= fact;}
1471
 
                                        else {factt= fact; facmm= facm;}
 
1759
                                        int flip= mtex->maptoneg & MAP_REF;
1472
1760
 
1473
 
                                        if(mtex->blendtype==MTEX_BLEND)
1474
 
                                                shi->matren->ref= factt*mtex->def_var+ facmm*mat_ref->ref;
1475
 
                                        else if(mtex->blendtype==MTEX_MUL)
1476
 
                                                shi->matren->ref= (facmul+factt)*mat_ref->ref;
1477
 
                                        else {
1478
 
                                                shi->matren->ref= factt+mat_ref->ref;
1479
 
                                                if(shi->matren->ref<0.0) shi->matren->ref= 0.0;
1480
 
                                        }
1481
 
                                        mat_ref= shi->matren;
 
1761
                                        shi->refl= texture_value_blend(mtex->def_var, shi->refl, texres.tin, mtex->varfac, mtex->blendtype, flip);
 
1762
                                        if(shi->refl<0.0) shi->refl= 0.0;
1482
1763
                                }
1483
1764
                                if(mtex->mapto & MAP_SPEC) {
1484
 
                                        if(mtex->maptoneg & MAP_SPEC) {factt= facm; facmm= fact;}
1485
 
                                        else {factt= fact; facmm= facm;}
1486
 
 
1487
 
                                        if(mtex->blendtype==MTEX_BLEND)
1488
 
                                                shi->matren->spec= factt*mtex->def_var+ facmm*mat_spec->spec;
1489
 
                                        else if(mtex->blendtype==MTEX_MUL)
1490
 
                                                shi->matren->spec= (facmul+factt)*mat_spec->spec;
1491
 
                                        else {
1492
 
                                                shi->matren->spec= factt+mat_spec->spec;
1493
 
                                                if(shi->matren->spec<0.0) shi->matren->spec= 0.0;
1494
 
                                        }
1495
 
                                        mat_spec= shi->matren;
 
1765
                                        int flip= mtex->maptoneg & MAP_SPEC;
 
1766
                                        
 
1767
                                        shi->spec= texture_value_blend(mtex->def_var, shi->spec, texres.tin, mtex->varfac, mtex->blendtype, flip);
 
1768
                                        if(shi->spec<0.0) shi->spec= 0.0;
1496
1769
                                }
1497
1770
                                if(mtex->mapto & MAP_EMIT) {
1498
 
                                        if(mtex->maptoneg & MAP_EMIT) {factt= facm; facmm= fact;}
1499
 
                                        else {factt= fact; facmm= facm;}
 
1771
                                        int flip= mtex->maptoneg & MAP_EMIT;
1500
1772
 
1501
 
                                        if(mtex->blendtype==MTEX_BLEND)
1502
 
                                                shi->matren->emit= factt*mtex->def_var+ facmm*mat_emit->emit;
1503
 
                                        else if(mtex->blendtype==MTEX_MUL)
1504
 
                                                shi->matren->emit= (facmul+factt)*mat_emit->emit;
1505
 
                                        else {
1506
 
                                                shi->matren->emit= factt+mat_emit->emit;
1507
 
                                                if(shi->matren->emit<0.0) shi->matren->emit= 0.0;
1508
 
                                        }
1509
 
                                        mat_emit= shi->matren;
 
1773
                                        shi->emit= texture_value_blend(mtex->def_var, shi->emit, texres.tin, mtex->varfac, mtex->blendtype, flip);
 
1774
                                        if(shi->emit<0.0) shi->emit= 0.0;
1510
1775
                                }
1511
1776
                                if(mtex->mapto & MAP_ALPHA) {
1512
 
                                        if(mtex->maptoneg & MAP_ALPHA) {factt= facm; facmm= fact;}
1513
 
                                        else {factt= fact; facmm= facm;}
 
1777
                                        int flip= mtex->maptoneg & MAP_ALPHA;
1514
1778
 
1515
 
                                        if(mtex->blendtype==MTEX_BLEND)
1516
 
                                                shi->matren->alpha= factt*mtex->def_var+ facmm*mat_alpha->alpha;
1517
 
                                        else if(mtex->blendtype==MTEX_MUL)
1518
 
                                                shi->matren->alpha= (facmul+factt)*mat_alpha->alpha;
1519
 
                                        else {
1520
 
                                                shi->matren->alpha= factt+mat_alpha->alpha;
1521
 
                                                if(shi->matren->alpha<0.0) shi->matren->alpha= 0.0;
1522
 
                                                else if(shi->matren->alpha>1.0) shi->matren->alpha= 1.0;
1523
 
                                        }
1524
 
                                        mat_alpha= shi->matren;
 
1779
                                        shi->alpha= texture_value_blend(mtex->def_var, shi->alpha, texres.tin, mtex->varfac, mtex->blendtype, flip);
 
1780
                                        if(shi->alpha<0.0) shi->alpha= 0.0;
 
1781
                                        else if(shi->alpha>1.0) shi->alpha= 1.0;
1525
1782
                                }
1526
1783
                                if(mtex->mapto & MAP_HAR) {
1527
 
                                        if(mtex->maptoneg & MAP_HAR) {factt= facm; facmm= fact;}
1528
 
                                        else {factt= fact; facmm= facm;}
1529
 
 
1530
 
                                        if(mtex->blendtype==MTEX_BLEND) {
1531
 
                                                shi->matren->har= 128.0*factt*mtex->def_var+ facmm*mat_har->har;
1532
 
                                        } else if(mtex->blendtype==MTEX_MUL) {
1533
 
                                                shi->matren->har= (facmul+factt)*mat_har->har;
1534
 
                                        } else {
1535
 
                                                shi->matren->har= 128.0*factt+mat_har->har;
1536
 
                                                if(shi->matren->har<1) shi->matren->har= 1;
1537
 
                                        }
1538
 
                                        mat_har= shi->matren;
 
1784
                                        int flip= mtex->maptoneg & MAP_HAR;
 
1785
                                        float har;  // have to map to 0-1
 
1786
                                        
 
1787
                                        har= ((float)shi->har)/128.0;
 
1788
                                        har= 128.0*texture_value_blend(mtex->def_var, har, texres.tin, mtex->varfac, mtex->blendtype, flip);
 
1789
                                        
 
1790
                                        if(har<1.0) shi->har= 1; 
 
1791
                                        else if(har>511.0) shi->har= 511;
 
1792
                                        else shi->har= (int)har;
1539
1793
                                }
1540
1794
                                if(mtex->mapto & MAP_RAYMIRR) {
1541
 
                                        if(mtex->maptoneg & MAP_RAYMIRR) {factt= facm; facmm= fact;}
1542
 
                                        else {factt= fact; facmm= facm;}
 
1795
                                        int flip= mtex->maptoneg & MAP_RAYMIRR;
1543
1796
 
1544
 
                                        if(mtex->blendtype==MTEX_BLEND)
1545
 
                                                shi->matren->ray_mirror= factt*mtex->def_var+ facmm*mat_ray_mirr->ray_mirror;
1546
 
                                        else if(mtex->blendtype==MTEX_MUL)
1547
 
                                                shi->matren->ray_mirror= (facmul+factt)*mat_ray_mirr->ray_mirror;
1548
 
                                        else {
1549
 
                                                shi->matren->ray_mirror= factt+mat_ray_mirr->ray_mirror;
1550
 
                                                if(shi->matren->ray_mirror<0.0) shi->matren->ray_mirror= 0.0;
1551
 
                                                else if(shi->matren->ray_mirror>1.0) shi->matren->ray_mirror= 1.0;
1552
 
                                        }
1553
 
                                        mat_ray_mirr= shi->matren;
 
1797
                                        shi->ray_mirror= texture_value_blend(mtex->def_var, shi->ray_mirror, texres.tin, mtex->varfac, mtex->blendtype, flip);
 
1798
                                        if(shi->ray_mirror<0.0) shi->ray_mirror= 0.0;
 
1799
                                        else if(shi->ray_mirror>1.0) shi->ray_mirror= 1.0;
1554
1800
                                }
1555
1801
                                if(mtex->mapto & MAP_TRANSLU) {
1556
 
                                        if(mtex->maptoneg & MAP_TRANSLU) {factt= facm; facmm= fact;}
1557
 
                                        else {factt= fact; facmm= facm;}
 
1802
                                        int flip= mtex->maptoneg & MAP_TRANSLU;
1558
1803
 
1559
 
                                        if(mtex->blendtype==MTEX_BLEND)
1560
 
                                                shi->matren->translucency= factt*mtex->def_var+ facmm*mat_translu->translucency;
1561
 
                                        else if(mtex->blendtype==MTEX_MUL)
1562
 
                                                shi->matren->translucency= (facmul+factt)*mat_translu->translucency;
1563
 
                                        else {
1564
 
                                                shi->matren->translucency= factt+mat_translu->translucency;
1565
 
                                                if(shi->matren->translucency<0.0) shi->matren->translucency= 0.0;
1566
 
                                                else if(shi->matren->translucency>1.0) shi->matren->translucency= 1.0;
1567
 
                                        }
1568
 
                                        mat_translu= shi->matren;
 
1804
                                        shi->translucency= texture_value_blend(mtex->def_var, shi->translucency, texres.tin, mtex->varfac, mtex->blendtype, flip);
 
1805
                                        if(shi->translucency<0.0) shi->translucency= 0.0;
 
1806
                                        else if(shi->translucency>1.0) shi->translucency= 1.0;
1569
1807
                                }
1570
1808
                                if(mtex->mapto & MAP_AMB) {
1571
 
                                        if(mtex->maptoneg & MAP_AMB) {factt= facm; facmm= fact;}
1572
 
                                        else {factt= fact; facmm= facm;}
 
1809
                                        int flip= mtex->maptoneg & MAP_AMB;
1573
1810
 
1574
 
                                        if(mtex->blendtype==MTEX_BLEND)
1575
 
                                                shi->matren->amb= factt*mtex->def_var+ facmm*mat_translu->amb;
1576
 
                                        else if(mtex->blendtype==MTEX_MUL)
1577
 
                                                shi->matren->amb= (facmul+factt)*mat_translu->amb;
1578
 
                                        else {
1579
 
                                                shi->matren->amb= factt+mat_translu->amb;
1580
 
                                                if(shi->matren->amb<0.0) shi->matren->amb= 0.0;
1581
 
                                                else if(shi->matren->amb>1.0) shi->matren->amb= 1.0;
1582
 
                                        }
1583
 
                                        mat_amb= shi->matren;
 
1811
                                        shi->amb= texture_value_blend(mtex->def_var, shi->amb, texres.tin, mtex->varfac, mtex->blendtype, flip);
 
1812
                                        if(shi->amb<0.0) shi->amb= 0.0;
 
1813
                                        else if(shi->amb>1.0) shi->amb= 1.0;
1584
1814
                                }
1585
1815
                        }
1586
1816
                }
1592
1822
void do_halo_tex(HaloRen *har, float xn, float yn, float *colf)
1593
1823
{
1594
1824
        MTex *mtex;
 
1825
        TexResult texres;
1595
1826
        float texvec[3], dxt[3], dyt[3], fact, facm, dx;
1596
1827
        int rgb, osatex;
1597
1828
        
1598
1829
        mtex= har->mat->mtex[0];
1599
 
        if(mtex->tex==0) return;
 
1830
        if(mtex->tex==NULL) return;
 
1831
        
1600
1832
        /* no normal mapping */
1601
 
        mtex->tex->nor= NULL;
 
1833
        texres.nor= NULL;
1602
1834
                
1603
1835
        texvec[0]= xn/har->rad;
1604
1836
        texvec[1]= yn/har->rad;
1624
1856
                        dxt[0]= mtex->size[0]*dx;
1625
1857
                        dyt[0]= mtex->size[0]*dx;
1626
1858
                }
1627
 
                else dxt[0]= 0.0;
 
1859
                else dxt[0]= dyt[0]= 0.0;
 
1860
                
1628
1861
                if(mtex->projy) {
1629
1862
                        dxt[1]= mtex->size[1]*dx;
1630
1863
                        dyt[1]= mtex->size[1]*dx;
1631
1864
                }
1632
 
                else dxt[1]= 0.0;
 
1865
                else dxt[1]= dyt[1]= 0.0;
 
1866
                
1633
1867
                if(mtex->projz) {
1634
1868
                        dxt[2]= 0.0;
1635
1869
                        dyt[2]= 0.0;
1636
1870
                }
1637
 
                else dxt[2]= 0.0;
 
1871
                else dxt[2]= dyt[2]= 0.0;
1638
1872
 
1639
1873
        }
1640
1874
 
1641
1875
        if(mtex->tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, dxt, dyt);
1642
1876
        
1643
 
        rgb= multitex(mtex->tex, texvec, dxt, dyt, osatex);
 
1877
        rgb= multitex(mtex->tex, texvec, dxt, dyt, osatex, &texres);
1644
1878
 
1645
1879
        /* texture output */
1646
1880
        if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
1647
 
                Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
 
1881
                texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
1648
1882
                rgb= 0;
1649
1883
        }
1650
1884
        if(mtex->texflag & MTEX_NEGATIVE) {
1651
1885
                if(rgb) {
1652
 
                        Tr= 1.0-Tr;
1653
 
                        Tg= 1.0-Tg;
1654
 
                        Tb= 1.0-Tb;
 
1886
                        texres.tr= 1.0-texres.tr;
 
1887
                        texres.tg= 1.0-texres.tg;
 
1888
                        texres.tb= 1.0-texres.tb;
1655
1889
                }
1656
 
                else Tin= 1.0-Tin;
 
1890
                else texres.tin= 1.0-texres.tin;
1657
1891
        }
1658
1892
 
1659
1893
        /* mapping */
1660
1894
        if(mtex->mapto & MAP_COL) {
1661
1895
                
1662
1896
                if(rgb==0) {
1663
 
                        Tr= mtex->r;
1664
 
                        Tg= mtex->g;
1665
 
                        Tb= mtex->b;
 
1897
                        texres.tr= mtex->r;
 
1898
                        texres.tg= mtex->g;
 
1899
                        texres.tb= mtex->b;
1666
1900
                }
1667
1901
                else if(mtex->mapto & MAP_ALPHA) {
1668
 
                        if(mtex->texflag & MTEX_ALPHAMIX) Tin= Ta; 
1669
 
                        else Tin= 1.0;
 
1902
                        texres.tin= 1.0;
1670
1903
                }
1671
 
                else Tin= Ta;
 
1904
                else texres.tin= texres.ta;
1672
1905
 
1673
 
                fact= Tin*mtex->colfac;
 
1906
                fact= texres.tin*mtex->colfac;
1674
1907
                facm= 1.0-fact;
1675
1908
                
1676
1909
                if(mtex->blendtype==MTEX_MUL) {
1677
1910
                        facm= 1.0-mtex->colfac;
1678
1911
                }
1679
 
                else fact*= 256;
1680
 
 
 
1912
                
1681
1913
                if(mtex->blendtype==MTEX_SUB) fact= -fact;
1682
1914
 
1683
1915
                if(mtex->blendtype==MTEX_BLEND) {
1684
 
                        colf[0]= (fact*Tr + facm*har->r);
1685
 
                        colf[1]= (fact*Tg + facm*har->g);
1686
 
                        colf[2]= (fact*Tb + facm*har->b);
 
1916
                        colf[0]= (fact*texres.tr + facm*har->r);
 
1917
                        colf[1]= (fact*texres.tg + facm*har->g);
 
1918
                        colf[2]= (fact*texres.tb + facm*har->b);
1687
1919
                }
1688
1920
                else if(mtex->blendtype==MTEX_MUL) {
1689
 
                        colf[0]= (facm+fact*Tr)*har->r;
1690
 
                        colf[1]= (facm+fact*Tg)*har->g;
1691
 
                        colf[2]= (facm+fact*Tb)*har->b;
 
1921
                        colf[0]= (facm+fact*texres.tr)*har->r;
 
1922
                        colf[1]= (facm+fact*texres.tg)*har->g;
 
1923
                        colf[2]= (facm+fact*texres.tb)*har->b;
1692
1924
                }
1693
1925
                else {
1694
 
                        colf[0]= (fact*Tr + har->r);
1695
 
                        colf[1]= (fact*Tg + har->g);
1696
 
                        colf[2]= (fact*Tb + har->b);
 
1926
                        colf[0]= (fact*texres.tr + har->r);
 
1927
                        colf[1]= (fact*texres.tg + har->g);
 
1928
                        colf[2]= (fact*texres.tb + har->b);
1697
1929
                        
1698
1930
                        CLAMP(colf[0], 0.0, 1.0);
1699
1931
                        CLAMP(colf[1], 0.0, 1.0);
1702
1934
        }
1703
1935
        if(mtex->mapto & MAP_ALPHA) {
1704
1936
                if(rgb) {
1705
 
                        if(Talpha) Tin= Ta;
1706
 
                        else Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
 
1937
                        if(texres.talpha) texres.tin= texres.ta;
 
1938
                        else texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
1707
1939
                }
1708
1940
                                
1709
 
                colf[3]*= Tin;
 
1941
                colf[3]*= texres.tin;
1710
1942
        }
1711
1943
}
1712
1944
 
1713
1945
/* ------------------------------------------------------------------------- */
1714
1946
 
1715
 
void do_sky_tex(float *lo)
 
1947
/* hor and zen are RGB vectors, blend is 1 float, should all be initialized */
 
1948
void do_sky_tex(float *lo, float *dxyview, float *hor, float *zen, float *blend)
1716
1949
{
1717
 
        World *wrld_hor, *wrld_zen;
1718
1950
        MTex *mtex;
1719
 
        float *co, fact, facm, factt, facmm, facmul = 0.0, stencilTin=1.0;
 
1951
        TexResult texres;
 
1952
        float *co, fact, stencilTin=1.0;
1720
1953
        float tempvec[3], texvec[3], dxt[3], dyt[3];
1721
1954
        int tex_nr, rgb= 0, ok;
1722
1955
        
1723
 
 
1724
1956
        /* todo: add flag to test if there's a tex */
 
1957
        texres.nor= NULL;
1725
1958
        
1726
 
        wrld_hor= wrld_zen= G.scene->world;
1727
 
 
1728
 
        /* The 6 here is the max amount of channels for a world */
1729
 
        for(tex_nr=0; tex_nr<6; tex_nr++) {
 
1959
        for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
1730
1960
                if(R.wrld.mtex[tex_nr]) {
1731
1961
                        mtex= R.wrld.mtex[tex_nr];
1732
1962
                        
1737
1967
                        co= lo;
1738
1968
                        
1739
1969
                        /* dxt dyt just from 1 value */
1740
 
                        dxt[0]= dxt[1]= dxt[2]= O.dxview;
1741
 
                        dyt[0]= dyt[1]= dyt[2]= O.dyview;
 
1970
                        if(dxyview) {
 
1971
                                dxt[0]= dxt[1]= dxt[2]= dxyview[0];
 
1972
                                dyt[0]= dyt[1]= dyt[2]= dxyview[1];
 
1973
                        }
 
1974
                        else {
 
1975
                                dxt[0]= dxt[1]= dxt[2]= 0.0;
 
1976
                                dyt[0]= dyt[1]= dyt[2]= 0.0;
 
1977
                        }
1742
1978
                        
1743
1979
                        /* Grab the mapping settings for this texture */
1744
1980
                        switch(mtex->texco) {
1768
2004
                                }
1769
2005
                                else {
1770
2006
                                        /* potentially dangerous... check with multitex! */
1771
 
                                        R.wrld.horr= (wrld_hor->horr);
1772
 
                                        R.wrld.horg= (wrld_hor->horg);
1773
 
                                        R.wrld.horb= (wrld_hor->horb);
1774
 
                                        R.wrld.zenr= (wrld_hor->zenr);
1775
 
                                        R.wrld.zeng= (wrld_hor->zeng);
1776
 
                                        R.wrld.zenb= (wrld_hor->zenb);
1777
2007
                                        continue;
1778
2008
                                }
1779
2009
                                break;
1798
2028
                        /* texture */
1799
2029
                        if(mtex->tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, dxt, dyt);
1800
2030
                
1801
 
                        rgb= multitex(mtex->tex, texvec, dxt, dyt, R.osa);
 
2031
                        rgb= multitex(mtex->tex, texvec, dxt, dyt, R.osa, &texres);
1802
2032
                        
1803
2033
                        /* texture output */
1804
2034
                        if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
1805
 
                                Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
 
2035
                                texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
1806
2036
                                rgb= 0;
1807
2037
                        }
1808
2038
                        if(mtex->texflag & MTEX_NEGATIVE) {
1809
2039
                                if(rgb) {
1810
 
                                        Tr= 1.0-Tr;
1811
 
                                        Tg= 1.0-Tg;
1812
 
                                        Tb= 1.0-Tb;
 
2040
                                        texres.tr= 1.0-texres.tr;
 
2041
                                        texres.tg= 1.0-texres.tg;
 
2042
                                        texres.tb= 1.0-texres.tb;
1813
2043
                                }
1814
 
                                else Tin= 1.0-Tin;
 
2044
                                else texres.tin= 1.0-texres.tin;
1815
2045
                        }
1816
2046
                        if(mtex->texflag & MTEX_STENCIL) {
1817
2047
                                if(rgb) {
1818
 
                                        
 
2048
                                        fact= texres.ta;
 
2049
                                        texres.ta*= stencilTin;
 
2050
                                        stencilTin*= fact;
1819
2051
                                }
1820
2052
                                else {
1821
 
                                        fact= Tin;
1822
 
                                        Tin*= stencilTin;
 
2053
                                        fact= texres.tin;
 
2054
                                        texres.tin*= stencilTin;
1823
2055
                                        stencilTin*= fact;
1824
2056
                                }
1825
2057
                        }
1826
2058
                        else {
1827
 
                                if(rgb) ;
1828
 
                                else Tin*= stencilTin;
 
2059
                                if(rgb) texres.ta *= stencilTin;
 
2060
                                else texres.tin*= stencilTin;
1829
2061
                        }
1830
2062
                        
1831
2063
                        /* colour mapping */
1832
2064
                        if(mtex->mapto & (WOMAP_HORIZ+WOMAP_ZENUP+WOMAP_ZENDOWN)) {
 
2065
                                float tcol[3];
1833
2066
                                
1834
2067
                                if(rgb==0) {
1835
 
                                        Tr= mtex->r;
1836
 
                                        Tg= mtex->g;
1837
 
                                        Tb= mtex->b;
 
2068
                                        texres.tr= mtex->r;
 
2069
                                        texres.tg= mtex->g;
 
2070
                                        texres.tb= mtex->b;
1838
2071
                                }
1839
 
                                else Tin= 1.0;
1840
 
 
1841
 
                                fact= Tin*mtex->colfac;
1842
 
                                facm= 1.0-fact;
1843
 
                                if(mtex->blendtype==MTEX_MUL) facm= 1.0-mtex->colfac;
1844
 
                                if(mtex->blendtype==MTEX_SUB) fact= -fact;
 
2072
                                else texres.tin= texres.ta;
 
2073
                                
 
2074
                                tcol[0]= texres.tr; tcol[1]= texres.tg; tcol[2]= texres.tb;
1845
2075
 
1846
2076
                                if(mtex->mapto & WOMAP_HORIZ) {
1847
 
                                        if(mtex->blendtype==MTEX_BLEND) {
1848
 
                                                R.wrld.horr= (fact*Tr + facm*wrld_hor->horr);
1849
 
                                                R.wrld.horg= (fact*Tg + facm*wrld_hor->horg);
1850
 
                                                R.wrld.horb= (fact*Tb + facm*wrld_hor->horb);
1851
 
                                        }
1852
 
                                        else if(mtex->blendtype==MTEX_MUL) {
1853
 
                                                R.wrld.horr= (facm+fact*Tr)*wrld_hor->horr;
1854
 
                                                R.wrld.horg= (facm+fact*Tg)*wrld_hor->horg;
1855
 
                                                R.wrld.horb= (facm+fact*Tb)*wrld_hor->horb;
1856
 
                                        }
1857
 
                                        else {
1858
 
                                                R.wrld.horr= (fact*Tr + wrld_hor->horr);
1859
 
                                                R.wrld.horg= (fact*Tg + wrld_hor->horg);
1860
 
                                                R.wrld.horb= (fact*Tb + wrld_hor->horb);
1861
 
                                        }
1862
 
                                        wrld_hor= &R.wrld;
 
2077
                                        texture_rgb_blend(hor, tcol, hor, texres.tin, mtex->colfac, mtex->blendtype);
1863
2078
                                }
1864
2079
                                if(mtex->mapto & (WOMAP_ZENUP+WOMAP_ZENDOWN)) {
1865
2080
                                        ok= 0;
1872
2087
                                        else ok= 1;
1873
2088
                                        
1874
2089
                                        if(ok) {
1875
 
                                        
1876
 
                                                if(mtex->blendtype==MTEX_BLEND) {
1877
 
                                                        R.wrld.zenr= (fact*Tr + facm*wrld_zen->zenr);
1878
 
                                                        R.wrld.zeng= (fact*Tg + facm*wrld_zen->zeng);
1879
 
                                                        R.wrld.zenb= (fact*Tb + facm*wrld_zen->zenb);
1880
 
                                                }
1881
 
                                                else if(mtex->blendtype==MTEX_MUL) {
1882
 
                                                        R.wrld.zenr= (facm+fact*Tr)*wrld_zen->zenr;
1883
 
                                                        R.wrld.zeng= (facm+fact*Tg)*wrld_zen->zeng;
1884
 
                                                        R.wrld.zenb= (facm+fact*Tb)*wrld_zen->zenb;
1885
 
                                                }
1886
 
                                                else {
1887
 
                                                        R.wrld.zenr= (fact*Tr + wrld_zen->zenr);
1888
 
                                                        R.wrld.zeng= (fact*Tg + wrld_zen->zeng);
1889
 
                                                        R.wrld.zenb= (fact*Tb + wrld_zen->zenb);
1890
 
                                                }
1891
 
                                                wrld_zen= &R.wrld;
1892
 
                                        }
1893
 
                                        else {
1894
 
                                                /* otherwise zenRGB undefined */
1895
 
                                                R.wrld.zenr= wrld_zen->zenr;
1896
 
                                                R.wrld.zeng= wrld_zen->zeng;
1897
 
                                                R.wrld.zenb= wrld_zen->zenb;
 
2090
                                                texture_rgb_blend(zen, tcol, zen, texres.tin, mtex->colfac, mtex->blendtype);
1898
2091
                                        }
1899
2092
                                }
1900
2093
                        }
1901
2094
                        if(mtex->mapto & WOMAP_BLEND) {
1902
 
                                if(rgb) Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
1903
 
                                
1904
 
                                fact= Tin*mtex->varfac;
1905
 
                                facm= 1.0-fact;
1906
 
                                if(mtex->blendtype==MTEX_MUL) facmul= 1.0-mtex->varfac;
1907
 
                                if(mtex->blendtype==MTEX_SUB) fact= -fact;
1908
 
 
1909
 
                                factt= fact; facmm= facm;
1910
 
                                
1911
 
                                if(mtex->blendtype==MTEX_BLEND)
1912
 
                                        R.inprz= factt*mtex->def_var+ facmm*R.inprz;
1913
 
                                else if(mtex->blendtype==MTEX_MUL)
1914
 
                                        R.inprz= (facmul+factt)*R.inprz;
1915
 
                                else {
1916
 
                                        R.inprz= factt+R.inprz;
1917
 
                                }
 
2095
                                if(rgb) texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
 
2096
                                
 
2097
                                *blend= texture_value_blend(mtex->def_var, *blend, texres.tin, mtex->varfac, mtex->blendtype, 0);
1918
2098
                        }
1919
2099
                }
1920
2100
        }
1921
2101
}
1922
2102
 
1923
2103
/* ------------------------------------------------------------------------- */
1924
 
/* explicit lampren stuff should be factored out! or rather, the
1925
 
   texturing stuff might need to go...*/
1926
 
void do_lamp_tex(LampRen *la, float *lavec, ShadeInput *shi)
 
2104
/* colf supposed to be initialized with la->r,g,b */
 
2105
 
 
2106
void do_lamp_tex(LampRen *la, float *lavec, ShadeInput *shi, float *colf)
1927
2107
{
1928
2108
        Object *ob;
1929
 
        LampRen *la_col;
1930
2109
        MTex *mtex;
1931
2110
        Tex *tex;
1932
 
        float *co = NULL, *dx = NULL, *dy = NULL, fact, facm, stencilTin=1.0;
 
2111
        TexResult texres;
 
2112
        float *co = NULL, *dx = NULL, *dy = NULL, fact, stencilTin=1.0;
1933
2113
        float texvec[3], dxt[3], dyt[3], tempvec[3];
1934
2114
        int tex_nr, rgb= 0;
1935
2115
        
1936
 
        la_col= la->org;
1937
 
        
1938
2116
        tex_nr= 0;
1939
2117
        
1940
 
        for(; tex_nr<6; tex_nr++) {
 
2118
        for(; tex_nr<MAX_MTEX; tex_nr++) {
1941
2119
                
1942
2120
                if(la->mtex[tex_nr]) {
1943
2121
                        mtex= la->mtex[tex_nr];
1944
2122
                        
1945
2123
                        tex= mtex->tex;
1946
 
                        if(tex==0) continue;
1947
 
                        tex->nor= NULL;
 
2124
                        if(tex==NULL) continue;
 
2125
                        texres.nor= NULL;
1948
2126
                        
1949
2127
                        /* which coords */
1950
2128
                        if(mtex->texco==TEXCO_OBJECT) {
1956
2134
                                        VECCOPY(tempvec, shi->co);
1957
2135
                                        MTC_Mat4MulVecfl(ob->imat, tempvec);
1958
2136
                                        if(shi->osatex) {
1959
 
                                                VECCOPY(dxt, O.dxco);
1960
 
                                                VECCOPY(dyt, O.dyco);
 
2137
                                                VECCOPY(dxt, shi->dxco);
 
2138
                                                VECCOPY(dyt, shi->dyco);
1961
2139
                                                MTC_Mat4Mul3Vecfl(ob->imat, dxt);
1962
2140
                                                MTC_Mat4Mul3Vecfl(ob->imat, dyt);
1963
2141
                                        }
1964
2142
                                }
1965
2143
                                else {
1966
2144
                                        co= shi->co;
1967
 
                                        dx= O.dxco; dy= O.dyco;
 
2145
                                        dx= shi->dxco; dy= shi->dyco;
1968
2146
                                }
1969
2147
                        }
1970
2148
                        else if(mtex->texco==TEXCO_GLOB) {
1971
 
                                co= shi->gl; dx= O.dxco; dy= O.dyco;
 
2149
                                co= shi->gl; dx= shi->dxco; dy= shi->dyco;
1972
2150
                                VECCOPY(shi->gl, shi->co);
1973
2151
                                MTC_Mat4MulVecfl(R.viewinv, shi->gl);
1974
2152
                        }
1977
2155
                                VECCOPY(tempvec, lavec);
1978
2156
                                MTC_Mat3MulVecfl(la->imat, tempvec);
1979
2157
                                
1980
 
                                tempvec[0]*= la->spottexfac;
1981
 
                                tempvec[1]*= la->spottexfac;
 
2158
                                if(la->type==LA_SPOT) {
 
2159
                                        tempvec[0]*= la->spottexfac;
 
2160
                                        tempvec[1]*= la->spottexfac;
 
2161
                                }
1982
2162
                                co= tempvec; 
1983
2163
                                
1984
2164
                                dx= dxt; dy= dyt;       
1985
2165
                                if(shi->osatex) {
1986
 
                                        VECCOPY(dxt, O.dxlv);
1987
 
                                        VECCOPY(dyt, O.dylv);
 
2166
                                        VECCOPY(dxt, shi->dxlv);
 
2167
                                        VECCOPY(dyt, shi->dylv);
1988
2168
                                        /* need some matrix conversion here? la->imat is a [3][3]  matrix!!! **/
1989
2169
                                        MTC_Mat3MulVecfl(la->imat, dxt);
1990
2170
                                        MTC_Mat3MulVecfl(la->imat, dyt);
2028
2208
                                do_2d_mapping(mtex, texvec, NULL, dxt, dyt);
2029
2209
                        }
2030
2210
                        
2031
 
                        rgb= multitex(tex, texvec, dxt, dyt, shi->osatex);
 
2211
                        rgb= multitex(tex, texvec, dxt, dyt, shi->osatex, &texres);
2032
2212
 
2033
2213
                        /* texture output */
2034
2214
                        if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
2035
 
                                Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
 
2215
                                texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2036
2216
                                rgb= 0;
2037
2217
                        }
2038
2218
                        if(mtex->texflag & MTEX_NEGATIVE) {
2039
2219
                                if(rgb) {
2040
 
                                        Tr= 1.0-Tr;
2041
 
                                        Tg= 1.0-Tg;
2042
 
                                        Tb= 1.0-Tb;
 
2220
                                        texres.tr= 1.0-texres.tr;
 
2221
                                        texres.tg= 1.0-texres.tg;
 
2222
                                        texres.tb= 1.0-texres.tb;
2043
2223
                                }
2044
 
                                else Tin= 1.0-Tin;
 
2224
                                else texres.tin= 1.0-texres.tin;
2045
2225
                        }
2046
2226
                        if(mtex->texflag & MTEX_STENCIL) {
2047
2227
                                if(rgb) {
2048
 
                                        fact= Ta;
2049
 
                                        Ta*= stencilTin;
 
2228
                                        fact= texres.ta;
 
2229
                                        texres.ta*= stencilTin;
2050
2230
                                        stencilTin*= fact;
2051
2231
                                }
2052
2232
                                else {
2053
 
                                        fact= Tin;
2054
 
                                        Tin*= stencilTin;
 
2233
                                        fact= texres.tin;
 
2234
                                        texres.tin*= stencilTin;
2055
2235
                                        stencilTin*= fact;
2056
2236
                                }
2057
2237
                        }
2058
2238
                        else {
2059
 
                                if(rgb) Ta*= stencilTin;
2060
 
                                else Tin*= stencilTin;
 
2239
                                if(rgb) texres.ta*= stencilTin;
 
2240
                                else texres.tin*= stencilTin;
2061
2241
                        }
2062
2242
                        
2063
2243
                        /* mapping */
2064
2244
                        if(mtex->mapto & LAMAP_COL) {
 
2245
                                float col[3];
2065
2246
                                
2066
2247
                                if(rgb==0) {
2067
 
                                        Tr= mtex->r;
2068
 
                                        Tg= mtex->g;
2069
 
                                        Tb= mtex->b;
 
2248
                                        texres.tr= mtex->r;
 
2249
                                        texres.tg= mtex->g;
 
2250
                                        texres.tb= mtex->b;
2070
2251
                                }
2071
2252
                                else if(mtex->mapto & MAP_ALPHA) {
2072
 
                                        if(mtex->texflag & MTEX_ALPHAMIX) Tin= Ta;
2073
 
                                        else Tin= stencilTin;
2074
 
                                }
2075
 
                                else Tin= Ta;
2076
 
 
2077
 
                                Tr*= la->energy;
2078
 
                                Tg*= la->energy;
2079
 
                                Tb*= la->energy;
2080
 
 
2081
 
                                fact= Tin*mtex->colfac;
2082
 
                                facm= 1.0-fact;
2083
 
                                if(mtex->blendtype==MTEX_MUL) facm= 1.0-mtex->colfac;
2084
 
                                if(mtex->blendtype==MTEX_SUB) fact= -fact;
2085
 
 
2086
 
                                if(mtex->blendtype==MTEX_BLEND) {
2087
 
                                        la->r= (fact*Tr + facm*la_col->r);
2088
 
                                        la->g= (fact*Tg + facm*la_col->g);
2089
 
                                        la->b= (fact*Tb + facm*la_col->b);
2090
 
                                }
2091
 
                                else if(mtex->blendtype==MTEX_MUL) {
2092
 
                                        la->r= (facm+fact*Tr)*la_col->r;
2093
 
                                        la->g= (facm+fact*Tg)*la_col->g;
2094
 
                                        la->b= (facm+fact*Tb)*la_col->b;
2095
 
                                }
2096
 
                                else {
2097
 
                                        la->r= (fact*Tr + la_col->r);
2098
 
                                        la->g= (fact*Tg + la_col->g);
2099
 
                                        la->b= (fact*Tb + la_col->b);
2100
 
                                }
2101
 
                                la_col= la; /* makes sure first run uses la->org, then la */
 
2253
                                        texres.tin= stencilTin;
 
2254
                                }
 
2255
                                else texres.tin= texres.ta;
 
2256
 
 
2257
                                /* lamp colors were premultiplied with this */
 
2258
                                col[0]= texres.tr*la->energy;
 
2259
                                col[1]= texres.tg*la->energy;
 
2260
                                col[2]= texres.tb*la->energy;
 
2261
                                
 
2262
                                texture_rgb_blend(colf, col, colf, texres.tin, mtex->colfac, mtex->blendtype);
2102
2263
                        }
2103
 
                        
2104
2264
                }
2105
2265
        }
2106
2266
}
2107
2267
 
2108
2268
/* ------------------------------------------------------------------------- */
2109
2269
 
2110
 
void externtex(MTex *mtex, float *vec)
 
2270
void externtex(MTex *mtex, float *vec, float *tin, float *tr, float *tg, float *tb, float *ta)
2111
2271
{
2112
2272
        Tex *tex;
2113
 
        float dxt[3], dyt[3], texvec[3], dummy[3];
 
2273
        TexResult texr;
 
2274
        float dxt[3], dyt[3], texvec[3];
2114
2275
        int rgb;
2115
2276
        
2116
2277
        tex= mtex->tex;
2117
 
        if(tex==0) return;
 
2278
        if(tex==NULL) return;
 
2279
        texr.nor= NULL;
2118
2280
        
2119
2281
        /* placement */
2120
2282
        if(mtex->projx) texvec[0]= mtex->size[0]*(vec[mtex->projx-1]+mtex->ofs[0]);
2129
2291
        /* texture */
2130
2292
        if(tex->type==TEX_IMAGE) {
2131
2293
                do_2d_mapping(mtex, texvec, NULL, dxt, dyt);
2132
 
                
2133
 
                if(mtex->mapto & MAP_NORM) {
2134
 
                        /* the pointer defines if there's bump */
2135
 
                        tex->nor= dummy;
2136
 
                        if(mtex->maptoneg & MAP_NORM) tex->norfac= -mtex->norfac;
2137
 
                        else tex->norfac= mtex->norfac;
2138
 
                }
2139
 
                else tex->nor= NULL;
2140
2294
        }
2141
2295
        
2142
 
        rgb= multitex(tex, texvec, dxt, dyt, 0);
 
2296
        rgb= multitex(tex, texvec, dxt, dyt, 0, &texr);
2143
2297
        
2144
2298
        if(rgb) {
2145
 
                Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
 
2299
                texr.tin= (0.35*texr.tr+0.45*texr.tg+0.2*texr.tb);
2146
2300
        }
2147
2301
        else {
2148
 
                Tr= mtex->r;
2149
 
                Tg= mtex->g;
2150
 
                Tb= mtex->b;
 
2302
                texr.tr= mtex->r;
 
2303
                texr.tg= mtex->g;
 
2304
                texr.tb= mtex->b;
2151
2305
        }
2152
 
}
2153
 
 
2154
 
/* ------------------------------------------------------------------------- */
2155
 
 
2156
 
void externtexcol(MTex *mtex, float *orco, char *col)
2157
 
{
2158
 
        int temp;
2159
 
        float b1;
2160
 
 
2161
 
        if(mtex->tex==0) return;
2162
 
        
2163
 
        externtex(mtex, orco);
2164
 
 
2165
 
        b1= 1.0-Tin;
2166
 
 
2167
 
        temp= 255*(Tin*Tr)+b1*col[0];
2168
 
        if(temp>255) col[0]= 255; else col[0]= temp;
2169
 
        temp= 255*(Tin*Tg)+b1*col[1];
2170
 
        if(temp>255) col[1]= 255; else col[1]= temp;
2171
 
        temp= 255*(Tin*Tb)+b1*col[2];
2172
 
        if(temp>255) col[2]= 255; else col[2]= temp;
2173
 
        
2174
 
}
 
2306
        
 
2307
        *tin= texr.tin;
 
2308
        *tr= texr.tr;
 
2309
        *tg= texr.tg;
 
2310
        *tb= texr.tb;
 
2311
        *ta= texr.ta;
 
2312
}
 
2313
 
2175
2314
 
2176
2315
/* ------------------------------------------------------------------------- */
2177
2316
 
2178
2317
void render_realtime_texture(ShadeInput *shi)
2179
2318
{
2180
 
        static Tex tex;
 
2319
        TexResult texr;
 
2320
        Image *ima;
 
2321
        static Tex tex1, tex2;  // threadsafe
2181
2322
        static int firsttime= 1;
 
2323
        Tex *tex;
2182
2324
        float texvec[2], dx[2], dy[2];
2183
2325
        
2184
2326
        if(firsttime) {
2185
 
                default_tex(&tex);
2186
 
                tex.type= TEX_IMAGE;
2187
2327
                firsttime= 0;
 
2328
                default_tex(&tex1);
 
2329
                default_tex(&tex2);
 
2330
                tex1.type= TEX_IMAGE;
 
2331
                tex2.type= TEX_IMAGE;
2188
2332
        }
2189
2333
        
2190
 
        tex.ima = shi->vlr->tface->tpage;
2191
 
        if(tex.ima) {
 
2334
        if(((int)(shi->ys+0.5)) & 1) tex= &tex1; else tex= &tex2;       // threadsafe
 
2335
        
 
2336
        ima = shi->vlr->tface->tpage;
 
2337
        if(ima) {
2192
2338
                
2193
2339
                texvec[0]= 0.5+0.5*shi->uv[0];
2194
2340
                texvec[1]= 0.5+0.5*shi->uv[1];
2195
2341
                if(shi->osatex) {
2196
 
                        dx[0]= 0.5*O.dxuv[0];
2197
 
                        dx[1]= 0.5*O.dxuv[1];
2198
 
                        dy[0]= 0.5*O.dyuv[0];
2199
 
                        dy[1]= 0.5*O.dyuv[1];
 
2342
                        dx[0]= 0.5*shi->dxuv[0];
 
2343
                        dx[1]= 0.5*shi->dxuv[1];
 
2344
                        dy[0]= 0.5*shi->dyuv[0];
 
2345
                        dy[1]= 0.5*shi->dyuv[1];
2200
2346
                }
2201
2347
                
2202
 
                if(shi->osatex) imagewraposa(&tex, texvec, dx, dy); 
2203
 
                else imagewrap(&tex, texvec); 
2204
 
                
2205
 
                shi->vcol[0]*= Tr;
2206
 
                shi->vcol[1]*= Tg;
2207
 
                shi->vcol[2]*= Tb;
 
2348
                texr.nor= NULL;
 
2349
                
 
2350
                if(shi->osatex) imagewraposa(tex, ima, texvec, dx, dy, &texr); 
 
2351
                else imagewrap(tex, ima, texvec, &texr); 
 
2352
                
 
2353
                shi->vcol[0]*= texr.tr;
 
2354
                shi->vcol[1]*= texr.tg;
 
2355
                shi->vcol[2]*= texr.tb;
2208
2356
        }
2209
 
        
2210
 
        
2211
2357
}
2212
2358
 
2213
2359
/* eof */