Biiig commit! Thanks to 2-3 weeks of cvs freeze...
[blender.git] / source / blender / render / intern / source / envmap.c
1
2 /*  envmap.c        RENDER
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * 
32  *  may 1999
33  * 
34  * $Id$
35  */
36
37 #include <math.h>
38 #include <string.h>
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #ifdef WIN32
45 #include "BLI_winstuff.h"
46 #endif
47
48 /* external modules: */
49 #include "MEM_guardedalloc.h"
50 #include "BLI_arithb.h"
51 #include "BLI_blenlib.h"
52 #include "BKE_utildefines.h"
53
54 #include "IMB_imbuf_types.h"
55 #include "IMB_imbuf.h"        /* for rectcpy */
56
57 #include "DNA_texture_types.h"
58 #include "DNA_image_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_scene_types.h"
61 #include "DNA_texture_types.h"
62
63 #include "BKE_library.h"
64 #include "BKE_main.h"
65 #include "BKE_global.h"
66 #include "BKE_world.h"   // init_render_world
67 #include "BKE_image.h"   // BKE_write_ibuf 
68
69 #include "MTC_matrixops.h"
70
71 /* this module */
72 #include "RE_callbacks.h"
73 #include "render.h"
74 #include "envmap.h"
75 #include "mydevice.h"
76 #include "rendercore.h" 
77 #include "renderHelp.h"
78 #include "texture.h"
79 #include "zbuf.h"
80
81
82 /* ------------------------------------------------------------------------- */
83
84 EnvMap *RE_add_envmap(void)
85 {
86         EnvMap *env;
87         
88         env= MEM_callocN(sizeof(EnvMap), "envmap");
89         env->type= ENV_CUBE;
90         env->stype= ENV_STATIC;
91         env->clipsta= 0.1;
92         env->clipend= 100.0;
93         env->cuberes= 100;
94         
95         return env;
96 } /* end of EnvMap *RE_add_envmap() */
97
98 /* ------------------------------------------------------------------------- */
99
100 EnvMap *RE_copy_envmap(EnvMap *env)
101 {
102         EnvMap *envn;
103         int a;
104         
105         envn= MEM_dupallocN(env);
106         envn->ok= 0;
107         for(a=0; a<6; a++) envn->cube[a]= 0;
108         if(envn->ima) id_us_plus((ID *)envn->ima);
109         
110         return envn;
111 }
112
113 /* ------------------------------------------------------------------------- */
114
115 void RE_free_envmapdata(EnvMap *env)
116 {
117         Image *ima;
118         int a, part;
119         
120         for(part=0; part<6; part++) {
121                 ima= env->cube[part];
122                 if(ima) {
123                         if(ima->ibuf) IMB_freeImBuf(ima->ibuf);
124
125                         for(a=0; a<BLI_ARRAY_NELEMS(ima->mipmap); a++) {
126                                 if(ima->mipmap[a]) IMB_freeImBuf(ima->mipmap[a]);
127                         }
128                         MEM_freeN(ima);
129                         env->cube[part]= 0;
130                 }
131         }
132         env->ok= 0;
133 }
134
135 /* ------------------------------------------------------------------------- */
136
137 void RE_free_envmap(EnvMap *env)
138 {
139         
140         RE_free_envmapdata(env);
141         MEM_freeN(env);
142         
143 }
144
145 /* ------------------------------------------------------------------------- */
146
147 static void envmap_split_ima(EnvMap *env)
148 {
149         ImBuf *ibuf;
150         Image *ima;
151 /*      extern rectcpy(); */
152         int dx, part;
153         
154         RE_free_envmapdata(env);        
155         
156         dx= env->ima->ibuf->y;
157         dx/= 2;
158         if(3*dx != env->ima->ibuf->x) {
159                 printf("Incorrect envmap size\n");
160                 env->ok= 0;
161                 env->ima->ok= 0;
162         }
163         else {
164                 for(part=0; part<6; part++) {
165                         ibuf= IMB_allocImBuf(dx, dx, 24, IB_rect, 0);
166                         ima= MEM_callocN(sizeof(Image), "image");
167                         ima->ibuf= ibuf;
168                         ima->ok= 1;
169                         env->cube[part]= ima;
170                 }
171                 IMB_rectop(env->cube[0]->ibuf, env->ima->ibuf, 
172                         0, 0, 0, 0, dx, dx, IMB_rectcpy, 0);
173                 IMB_rectop(env->cube[1]->ibuf, env->ima->ibuf, 
174                         0, 0, dx, 0, dx, dx, IMB_rectcpy, 0);
175                 IMB_rectop(env->cube[2]->ibuf, env->ima->ibuf, 
176                         0, 0, 2*dx, 0, dx, dx, IMB_rectcpy, 0);
177                 IMB_rectop(env->cube[3]->ibuf, env->ima->ibuf, 
178                         0, 0, 0, dx, dx, dx, IMB_rectcpy, 0);
179                 IMB_rectop(env->cube[4]->ibuf, env->ima->ibuf, 
180                         0, 0, dx, dx, dx, dx, IMB_rectcpy, 0);
181                 IMB_rectop(env->cube[5]->ibuf, env->ima->ibuf, 
182                         0, 0, 2*dx, dx, dx, dx, IMB_rectcpy, 0);
183                 env->ok= 2;
184         }
185 }
186
187 /* ------------------------------------------------------------------------- */
188 /* ****************** RENDER ********************** */
189
190 static void envmap_renderdata(EnvMap *env)
191 {
192         static RE_Render envR;
193         static Object *camera;
194         int cuberes;
195         
196         if(env) {
197                 envR= R;
198                 camera= G.scene->camera;
199                 
200                 cuberes = (env->cuberes * R.r.size) / 100;
201                 cuberes &= 0xFFFC;
202                 env->lastsize= R.r.size;
203                 R.rectx= R.r.xsch= R.recty= R.r.ysch= cuberes;
204                 R.afmx= R.afmy= R.r.xsch/2;
205                 R.xstart= R.ystart= -R.afmx;
206                 R.xend= R.yend= R.xstart+R.rectx-1;
207
208                 R.r.mode &= ~(R_BORDER | R_PANORAMA | R_ORTHO | R_MBLUR);
209                 R.r.xparts= R.r.yparts= 1;
210                 R.r.bufflag= 0;
211                 R.r.size= 100;
212                 R.ycor= 1.0; 
213                 R.r.yasp= R.r.xasp= 1;
214                 
215                 R.near= env->clipsta;
216                 R.far= env->clipend;
217                 
218                 G.scene->camera= env->object;
219                 
220         }
221         else {
222                 /* this to make sure init_renderdisplay works */
223                 envR.winx= R.winx;
224                 envR.winy= R.winy;
225                 envR.winxof= R.winxof;
226                 envR.winyof= R.winyof;
227                 
228                 R= envR;
229                 G.scene->camera= camera;
230         }
231         
232 }
233
234 /* ------------------------------------------------------------------------- */
235
236 static void envmap_transmatrix(float mat[][4], int part)
237 {
238         float tmat[4][4], eul[3], rotmat[4][4];
239         
240         eul[0]= eul[1]= eul[2]= 0.0;
241         
242         if(part==0) {                   /* neg z */
243                 ;
244         } else if(part==1) {    /* pos z */
245                 eul[0]= M_PI;
246         } else if(part==2) {    /* pos y */
247                 eul[0]= M_PI/2.0;
248         } else if(part==3) {    /* neg x */
249                 eul[0]= M_PI/2.0;
250                 eul[2]= M_PI/2.0;
251         } else if(part==4) {    /* neg y */
252                 eul[0]= M_PI/2.0;
253                 eul[2]= M_PI;
254         } else {                                /* pos x */
255                 eul[0]= M_PI/2.0;
256                 eul[2]= -M_PI/2.0;
257         }
258         
259         MTC_Mat4CpyMat4(tmat, mat);
260         EulToMat4(eul, rotmat);
261         MTC_Mat4MulSerie(mat, tmat, rotmat,
262                                          0,   0,    0,
263                                          0,   0,    0);
264 }
265
266 /* ------------------------------------------------------------------------- */
267
268 static void env_rotate_scene(float mat[][4], int mode)
269 {
270         VlakRen *vlr = NULL;
271         VertRen *ver = NULL;
272         LampRen *lar = NULL;
273         HaloRen *har = NULL;
274         float xn, yn, zn, imat[3][3], pmat[4][4], smat[4][4], tmat[4][4], cmat[3][3];
275         int a;
276         
277         if(mode==0) {
278                 MTC_Mat4Invert(tmat, mat);
279                 MTC_Mat3CpyMat4(imat, tmat);
280         }
281         else {
282                 MTC_Mat4CpyMat4(tmat, mat);
283                 MTC_Mat3CpyMat4(imat, mat);
284         }
285         
286         for(a=0; a<R.totvert; a++) {
287                 if((a & 255)==0) ver= R.blove[a>>8];
288                 else ver++;
289                 
290                 MTC_Mat4MulVecfl(tmat, ver->co);
291                 
292                 xn= ver->n[0];
293                 yn= ver->n[1];
294                 zn= ver->n[2];
295                 /* no transpose ! */
296                 ver->n[0]= imat[0][0]*xn+imat[1][0]*yn+imat[2][0]*zn;
297                 ver->n[1]= imat[0][1]*xn+imat[1][1]*yn+imat[2][1]*zn;
298                 ver->n[2]= imat[0][2]*xn+imat[1][2]*yn+imat[2][2]*zn;
299                 Normalise(ver->n);
300         }
301         
302         for(a=0; a<R.tothalo; a++) {
303                 if((a & 255)==0) har= R.bloha[a>>8];
304                 else har++;
305         
306                 MTC_Mat4MulVecfl(tmat, har->co);
307         }
308                 
309         for(a=0; a<R.totvlak; a++) {
310                 if((a & 255)==0) vlr= R.blovl[a>>8];
311                 else vlr++;
312                 
313                 xn= vlr->n[0];
314                 yn= vlr->n[1];
315                 zn= vlr->n[2];
316                 /* no transpose ! */
317                 vlr->n[0]= imat[0][0]*xn+imat[1][0]*yn+imat[2][0]*zn;
318                 vlr->n[1]= imat[0][1]*xn+imat[1][1]*yn+imat[2][1]*zn;
319                 vlr->n[2]= imat[0][2]*xn+imat[1][2]*yn+imat[2][2]*zn;
320                 Normalise(vlr->n);
321         }
322         
323         set_normalflags();
324         
325         for(a=0; a<R.totlamp; a++) {
326                 lar= R.la[a];
327                 
328                 /* removed here some horrible code of someone in NaN who tried to fix
329                    prototypes... just solved by introducing a correct cmat[3][3] instead
330                    of using smat. this works, check square spots in reflections  (ton) */
331                 Mat3CpyMat3(cmat, lar->imat); 
332                 Mat3MulMat3(lar->imat, cmat, imat); 
333
334                 MTC_Mat3MulVecfl(imat, lar->vec);
335                 MTC_Mat4MulVecfl(tmat, lar->co);
336
337                 lar->sh_invcampos[0]= -lar->co[0];
338                 lar->sh_invcampos[1]= -lar->co[1];
339                 lar->sh_invcampos[2]= -lar->co[2];
340                 MTC_Mat3MulVecfl(lar->imat, lar->sh_invcampos);
341                 lar->sh_invcampos[2]*= lar->sh_zfac;
342                 
343                 if(lar->shb) {
344                         if(mode==1) {
345                                 MTC_Mat4Invert(pmat, mat);
346                                 MTC_Mat4MulMat4(smat, pmat, lar->shb->viewmat);
347                                 MTC_Mat4MulMat4(lar->shb->persmat, smat, lar->shb->winmat);
348                         }
349                         else MTC_Mat4MulMat4(lar->shb->persmat, lar->shb->viewmat, lar->shb->winmat);
350                 }
351         }
352         
353 }
354
355 /* ------------------------------------------------------------------------- */
356
357 static void env_layerflags(unsigned int notlay)
358 {
359         VlakRen *vlr = NULL;
360         int a;
361         
362         for(a=0; a<R.totvlak; a++) {
363                 if((a & 255)==0) vlr= R.blovl[a>>8];
364                 else vlr++;
365                 if(vlr->lay & notlay) vlr->flag &= ~R_VISIBLE;
366         }
367 }
368
369 void env_hideobject(Object *ob)
370 {
371         VlakRen *vlr = NULL;
372         int a;
373         
374         for(a=0; a<R.totvlak; a++) {
375                 if((a & 255)==0) vlr= R.blovl[a>>8];
376                 else vlr++;
377                 if(vlr->ob == ob) vlr->flag &= ~R_VISIBLE;
378         }
379 }
380
381 /* ------------------------------------------------------------------------- */
382
383 static void env_set_imats()
384 {
385         Base *base;
386         float mat[4][4];
387         
388         base= G.scene->base.first;
389         while(base) {
390                 MTC_Mat4MulMat4(mat, base->object->obmat, R.viewmat);
391                 MTC_Mat4Invert(base->object->imat, mat);
392                 
393                 base= base->next;
394         }
395
396 }       
397
398 /* ------------------------------------------------------------------------- */
399
400 static void render_envmap(EnvMap *env)
401 {
402         /* only the cubemap is implemented */
403         ImBuf *ibuf;
404         Image *ima;
405         float oldviewinv[4][4], mat[4][4], tmat[4][4];
406         short part;
407         
408         /* need a recalc: ortho-render has no correct viewinv */
409         MTC_Mat4Invert(oldviewinv, R.viewmat);
410
411         /* do first, envmap_renderdata copies entire R struct */
412         if(R.rectz) MEM_freeN(R.rectz); R.rectz= NULL;
413         if(R.rectot) MEM_freeN(R.rectot); R.rectot= NULL;
414         if(R.rectftot) MEM_freeN(R.rectftot); R.rectftot= NULL;
415         
416         /* setup necessary globals */
417         envmap_renderdata(env);
418         
419         RE_local_init_render_display();
420
421         R.rectot= MEM_mallocN(sizeof(int)*R.rectx*R.recty, "rectot");
422         R.rectz=  MEM_mallocN(sizeof(int)*R.rectx*R.recty, "rectz");
423
424         for(part=0; part<6; part++) {
425
426                 RE_local_clear_render_display(R.win);
427                 fillrect(R.rectot, R.rectx, R.recty, 0);
428                 
429                 RE_setwindowclip(1,-1); /*  no jit:(-1) */
430                 
431                 MTC_Mat4CpyMat4(tmat, G.scene->camera->obmat);
432                 MTC_Mat4Ortho(tmat);
433                 envmap_transmatrix(tmat, part);
434                 MTC_Mat4Invert(mat, tmat);
435                 /* mat now is the camera 'viewmat' */
436
437                 MTC_Mat4CpyMat4(R.viewmat, mat);
438                 MTC_Mat4CpyMat4(R.viewinv, tmat);
439                 
440                 /* we have to correct for the already rotated vertexcoords */
441                 MTC_Mat4MulMat4(tmat, oldviewinv, R.viewmat);
442                 MTC_Mat4Invert(env->imat, tmat);
443                 
444                 env_rotate_scene(tmat, 1);
445                 init_render_world();
446                 setzbufvlaggen(RE_projectverto);
447                 env_layerflags(env->notlay);
448                 env_hideobject(env->object);
449                 env_set_imats();
450                                 
451                 if(RE_local_test_break()==0) {
452
453                         RE_local_printrenderinfo(0.0, part);
454
455                         if(R.r.mode & R_OSA) zbufshadeDA();
456                         else zbufshade();
457
458                 }
459                 
460                 /* rotate back */
461                 env_rotate_scene(tmat, 0);
462
463                 if(RE_local_test_break()==0) {
464                         ibuf= IMB_allocImBuf(R.rectx, R.recty, 24, IB_rect, 0);
465                         ima= MEM_callocN(sizeof(Image), "image");
466                         memcpy(ibuf->rect, R.rectot, 4*ibuf->x*ibuf->y);
467                         ima->ibuf= ibuf;
468                         ima->ok= 1;
469                         env->cube[part]= ima;
470                 }
471                 
472                 if(RE_local_test_break()) break;
473
474         }
475         
476         if(R.rectz) MEM_freeN(R.rectz); R.rectz= NULL;
477         if(R.rectot) MEM_freeN(R.rectot); R.rectot= NULL;
478         if(R.rectftot) MEM_freeN(R.rectftot); R.rectftot= NULL;
479         
480         if(RE_local_test_break()) RE_free_envmapdata(env);
481         else {
482                 if(R.r.mode & R_OSA) env->ok= ENV_OSA;
483                 else env->ok= ENV_NORMAL;
484                 env->lastframe= G.scene->r.cfra;
485         }
486         
487         /* restore */
488         envmap_renderdata(0);
489         env_set_imats();
490         init_render_world();
491
492 }
493
494 /* ------------------------------------------------------------------------- */
495
496 void make_envmaps()
497 {
498         Tex *tex;
499         int do_init= 0, depth= 0, trace;
500         
501         if (!(R.r.mode & R_ENVMAP)) return;
502         
503         /* we dont raytrace, disabling the flag will cause ray_transp render solid */
504         trace= (R.r.mode & R_RAYTRACE);
505         R.r.mode &= ~R_RAYTRACE;
506
507         /* 5 = hardcoded max recursion level */
508         while(depth<5) {
509                 tex= G.main->tex.first;
510                 while(tex) {
511                         if(tex->id.us && tex->type==TEX_ENVMAP) {
512                                 if(tex->env && tex->env->object) {
513                                         if(tex->env->object->lay & G.scene->lay) {
514                                                 if(tex->env->stype!=ENV_LOAD) {
515                                                         
516                                                         /* decide if to render an envmap (again) */
517                                                         if(tex->env->depth >= depth) {
518                                                                 
519                                                                 /* set 'recalc' to make sure it does an entire loop of recalcs */
520                                                                 
521                                                                 if(tex->env->ok) {
522                                                                                 /* free when OSA, and old one isn't OSA */
523                                                                         if((R.r.mode & R_OSA) && tex->env->ok==ENV_NORMAL) 
524                                                                                 RE_free_envmapdata(tex->env);
525                                                                                 /* free when size larger */
526                                                                         else if(tex->env->lastsize < R.r.size) 
527                                                                                 RE_free_envmapdata(tex->env);
528                                                                                 /* free when env is in recalcmode */
529                                                                         else if(tex->env->recalc)
530                                                                                 RE_free_envmapdata(tex->env);
531                                                                 }
532                                                                 
533                                                                 if(tex->env->ok==0 && depth==0) tex->env->recalc= 1;
534                                                                 
535                                                                 if(tex->env->ok==0) {
536                                                                         do_init= 1;
537                                                                         render_envmap(tex->env);
538                                                                         
539                                                                         if(depth==tex->env->depth) tex->env->recalc= 0;
540                                                                 }
541                                                         }
542                                                 }
543                                         }
544                                 }
545                         }
546                         tex= tex->id.next;
547                 }
548                 depth++;
549         }
550
551         if(do_init) {
552                 RE_local_init_render_display();
553                 RE_local_clear_render_display(R.win);
554                 R.flag |= R_REDRAW_PRV;
555         }       
556         // restore
557         R.r.mode |= trace;
558
559 }
560
561 /* ------------------------------------------------------------------------- */
562
563 static int envcube_isect(float *vec, float *answ)
564 {
565         float labda;
566         int face;
567         
568         /* which face */
569         if( vec[2]<=-fabs(vec[0]) && vec[2]<=-fabs(vec[1]) ) {
570                 face= 0;
571                 labda= -1.0/vec[2];
572                 answ[0]= labda*vec[0];
573                 answ[1]= labda*vec[1];
574         }
575         else if( vec[2]>=fabs(vec[0]) && vec[2]>=fabs(vec[1]) ) {
576                 face= 1;
577                 labda= 1.0/vec[2];
578                 answ[0]= labda*vec[0];
579                 answ[1]= -labda*vec[1];
580         }
581         else if( vec[1]>=fabs(vec[0]) ) {
582                 face= 2;
583                 labda= 1.0/vec[1];
584                 answ[0]= labda*vec[0];
585                 answ[1]= labda*vec[2];
586         }
587         else if( vec[0]<=-fabs(vec[1]) ) {
588                 face= 3;
589                 labda= -1.0/vec[0];
590                 answ[0]= labda*vec[1];
591                 answ[1]= labda*vec[2];
592         }
593         else if( vec[1]<=-fabs(vec[0]) ) {
594                 face= 4;
595                 labda= -1.0/vec[1];
596                 answ[0]= -labda*vec[0];
597                 answ[1]= labda*vec[2];
598         }
599         else {
600                 face= 5;
601                 labda= 1.0/vec[0];
602                 answ[0]= -labda*vec[1];
603                 answ[1]= labda*vec[2];
604         }
605         answ[0]= 0.5+0.5*answ[0];
606         answ[1]= 0.5+0.5*answ[1];
607         return face;
608 }
609
610 /* ------------------------------------------------------------------------- */
611
612 static void set_dxtdyt(float *dxts, float *dyts, float *dxt, float *dyt, int face)
613 {
614         if(face==2 || face==4) {
615                 dxts[0]= dxt[0];
616                 dyts[0]= dyt[0];
617                 dxts[1]= dxt[2];
618                 dyts[1]= dyt[2];
619         }
620         else if(face==3 || face==5) {
621                 dxts[0]= dxt[1];
622                 dxts[1]= dxt[2];
623                 dyts[0]= dyt[1];
624                 dyts[1]= dyt[2];
625         }
626         else {
627                 dxts[0]= dxt[0];
628                 dyts[0]= dyt[0];
629                 dxts[1]= dxt[1];
630                 dyts[1]= dyt[1];
631         }
632 }
633
634 /* ------------------------------------------------------------------------- */
635
636 int envmaptex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres)
637 {
638         /* texvec should be the already reflected normal */
639         EnvMap *env;
640         float fac, vec[3], sco[3], dxts[3], dyts[3];
641         int face, face1;
642         
643         env= tex->env;
644         if(env==0 || env->object==0) {
645                 texres->tin= 0.0;
646                 return 0;
647         }
648         if(env->stype==ENV_LOAD) {
649                 env->ima= tex->ima;
650                 if(env->ima && env->ima->ok) {
651                         if(env->ima->ibuf==0) ima_ibuf_is_nul(tex);
652                         if(env->ima->ok && env->ok==0) envmap_split_ima(env);
653                 }
654         }
655
656         if(env->ok==0) {
657                 
658                 texres->tin= 0.0;
659                 return 0;
660         }
661         
662         /* rotate to envmap space */
663         VECCOPY(vec, texvec);
664         MTC_Mat4Mul3Vecfl(env->object->imat, vec);
665         
666         face= envcube_isect(vec, sco);
667         tex->ima= env->cube[face];
668         
669         if(osatex) {
670
671                 MTC_Mat4Mul3Vecfl(env->object->imat, dxt);
672                 MTC_Mat4Mul3Vecfl(env->object->imat, dyt);
673                 
674                 set_dxtdyt(dxts, dyts, dxt, dyt, face);
675                 imagewraposa(tex, sco, dxts, dyts, texres);
676                 
677                 /* edges? */
678                 
679                 if(texres->ta<1.0) {
680                         TexResult texr1, texr2;
681         
682                         VecAddf(vec, vec, dxt);
683                         face1= envcube_isect(vec, sco);
684                         VecSubf(vec, vec, dxt);
685                         
686                         if(face!=face1) {
687                                 tex->ima= env->cube[face1];
688                                 set_dxtdyt(dxts, dyts, dxt, dyt, face1);
689                                 imagewraposa(tex, sco, dxts, dyts, &texr1);
690                         }
691                         else texr1.tr= texr1.tg= texr1.tb= texr1.ta= 0.0;
692                         
693                         /* here was the nasty bug! results were not zero-ed. FPE! */
694                         
695                         VecAddf(vec, vec, dyt);
696                         face1= envcube_isect(vec, sco);
697                         VecSubf(vec, vec, dyt);
698                         
699                         if(face!=face1) {
700                                 tex->ima= env->cube[face1];
701                                 set_dxtdyt(dxts, dyts, dxt, dyt, face1);
702                                 imagewraposa(tex, sco, dxts, dyts, &texr2);
703                         }
704                         else texr2.tr= texr2.tg= texr2.tb= texr2.ta= 0.0;
705                         
706                         fac= (texres->ta+texr1.ta+texr2.ta);
707                         if(fac!=0.0) {
708                                 fac= 1.0/fac;
709                                 
710                                 texres->tr= fac*(texres->ta*texres->tr + texr1.ta*texr1.tr + texr2.ta*texr2.tr );
711                                 texres->tg= fac*(texres->ta*texres->tg + texr1.ta*texr1.tg + texr2.ta*texr2.tg );
712                                 texres->tb= fac*(texres->ta*texres->tb + texr1.ta*texr1.tb + texr2.ta*texr2.tb );
713                         }
714                         texres->ta= 1.0;
715                 }
716         }
717         else {
718                 imagewrap(tex, sco, texres);
719         }
720         
721         tex->ima= env->ima;
722         
723         return 1;
724 }
725
726 /* ------------------------------------------------------------------------- */
727
728 /* eof */