Orange branch: Revived hidden treasure, the Groups!
[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 /* external modules: */
45 #include "MEM_guardedalloc.h"
46 #include "BLI_arithb.h"
47 #include "BLI_blenlib.h"
48 #include "BKE_utildefines.h"
49
50 #include "IMB_imbuf_types.h"
51 #include "IMB_imbuf.h"        /* for rectcpy */
52
53 #include "DNA_group_types.h"
54 #include "DNA_image_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_scene_types.h"
57 #include "DNA_texture_types.h"
58
59 #include "BKE_library.h"
60 #include "BKE_main.h"
61 #include "BKE_global.h"
62 #include "BKE_world.h"   // init_render_world
63 #include "BKE_image.h"   // BKE_write_ibuf 
64
65 #include "MTC_matrixops.h"
66
67 #include "SDL_thread.h"
68 #undef main 
69 #define main main /* stupid SDL_main redefines main as SDL_main */
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         unsigned 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         extern void init_filt_mask(void);
193         static RE_Render envR;
194         static Object *camera;
195         int cuberes;
196         
197         if(env) {
198                 envR= R;
199                 camera= G.scene->camera;
200                 
201                 cuberes = (env->cuberes * R.r.size) / 100;
202                 cuberes &= 0xFFFC;
203                 env->lastsize= R.r.size;
204                 R.rectx= R.r.xsch= R.recty= R.r.ysch= cuberes;
205                 R.afmx= R.afmy= R.r.xsch/2;
206                 R.xstart= R.ystart= -R.afmx;
207                 R.xend= R.yend= R.xstart+R.rectx-1;
208
209                 R.r.mode &= ~(R_BORDER | R_PANORAMA | R_ORTHO | R_MBLUR);
210                 R.r.filtertype= 0;
211                 R.r.xparts= R.r.yparts= 1;
212                 R.r.bufflag= 0;
213                 R.r.size= 100;
214                 R.ycor= 1.0; 
215                 R.r.yasp= R.r.xasp= 1;
216                 
217                 R.near= env->clipsta;
218                 R.far= env->clipend;
219                 
220                 G.scene->camera= env->object;
221                 
222         }
223         else {
224                 /* this to make sure init_renderdisplay works */
225                 envR.winx= R.winx;
226                 envR.winy= R.winy;
227                 envR.winxof= R.winxof;
228                 envR.winyof= R.winyof;
229                 
230                 R= envR;
231                 G.scene->camera= camera;
232         }
233         
234         /* gauss, gamma, etc */
235         init_filt_mask();
236 }
237
238 /* ------------------------------------------------------------------------- */
239
240 static void envmap_transmatrix(float mat[][4], int part)
241 {
242         float tmat[4][4], eul[3], rotmat[4][4];
243         
244         eul[0]= eul[1]= eul[2]= 0.0;
245         
246         if(part==0) {                   /* neg z */
247                 ;
248         } else if(part==1) {    /* pos z */
249                 eul[0]= M_PI;
250         } else if(part==2) {    /* pos y */
251                 eul[0]= M_PI/2.0;
252         } else if(part==3) {    /* neg x */
253                 eul[0]= M_PI/2.0;
254                 eul[2]= M_PI/2.0;
255         } else if(part==4) {    /* neg y */
256                 eul[0]= M_PI/2.0;
257                 eul[2]= M_PI;
258         } else {                                /* pos x */
259                 eul[0]= M_PI/2.0;
260                 eul[2]= -M_PI/2.0;
261         }
262         
263         MTC_Mat4CpyMat4(tmat, mat);
264         EulToMat4(eul, rotmat);
265         MTC_Mat4MulSerie(mat, tmat, rotmat,
266                                          0,   0,    0,
267                                          0,   0,    0);
268 }
269
270 /* ------------------------------------------------------------------------- */
271
272 static void env_rotate_scene(float mat[][4], int mode)
273 {
274         GroupObject *go;
275         VlakRen *vlr = NULL;
276         VertRen *ver = NULL;
277         LampRen *lar = NULL;
278         HaloRen *har = NULL;
279         float xn, yn, zn, imat[3][3], pmat[4][4], smat[4][4], tmat[4][4], cmat[3][3];
280         int a;
281         
282         if(mode==0) {
283                 MTC_Mat4Invert(tmat, mat);
284                 MTC_Mat3CpyMat4(imat, tmat);
285         }
286         else {
287                 MTC_Mat4CpyMat4(tmat, mat);
288                 MTC_Mat3CpyMat4(imat, mat);
289         }
290         
291         for(a=0; a<R.totvert; a++) {
292                 if((a & 255)==0) ver= R.blove[a>>8];
293                 else ver++;
294                 
295                 MTC_Mat4MulVecfl(tmat, ver->co);
296                 
297                 xn= ver->n[0];
298                 yn= ver->n[1];
299                 zn= ver->n[2];
300                 /* no transpose ! */
301                 ver->n[0]= imat[0][0]*xn+imat[1][0]*yn+imat[2][0]*zn;
302                 ver->n[1]= imat[0][1]*xn+imat[1][1]*yn+imat[2][1]*zn;
303                 ver->n[2]= imat[0][2]*xn+imat[1][2]*yn+imat[2][2]*zn;
304                 Normalise(ver->n);
305         }
306         
307         for(a=0; a<R.tothalo; a++) {
308                 if((a & 255)==0) har= R.bloha[a>>8];
309                 else har++;
310         
311                 MTC_Mat4MulVecfl(tmat, har->co);
312         }
313                 
314         for(a=0; a<R.totvlak; a++) {
315                 if((a & 255)==0) vlr= R.blovl[a>>8];
316                 else vlr++;
317                 
318                 xn= vlr->n[0];
319                 yn= vlr->n[1];
320                 zn= vlr->n[2];
321                 /* no transpose ! */
322                 vlr->n[0]= imat[0][0]*xn+imat[1][0]*yn+imat[2][0]*zn;
323                 vlr->n[1]= imat[0][1]*xn+imat[1][1]*yn+imat[2][1]*zn;
324                 vlr->n[2]= imat[0][2]*xn+imat[1][2]*yn+imat[2][2]*zn;
325                 Normalise(vlr->n);
326         }
327         
328         set_normalflags();
329         
330         for(go=R.lights.first; go; go= go->next) {
331                 lar= go->lampren;
332                 
333                 /* removed here some horrible code of someone in NaN who tried to fix
334                    prototypes... just solved by introducing a correct cmat[3][3] instead
335                    of using smat. this works, check square spots in reflections  (ton) */
336                 Mat3CpyMat3(cmat, lar->imat); 
337                 Mat3MulMat3(lar->imat, cmat, imat); 
338
339                 MTC_Mat3MulVecfl(imat, lar->vec);
340                 MTC_Mat4MulVecfl(tmat, lar->co);
341
342                 lar->sh_invcampos[0]= -lar->co[0];
343                 lar->sh_invcampos[1]= -lar->co[1];
344                 lar->sh_invcampos[2]= -lar->co[2];
345                 MTC_Mat3MulVecfl(lar->imat, lar->sh_invcampos);
346                 lar->sh_invcampos[2]*= lar->sh_zfac;
347                 
348                 if(lar->shb) {
349                         if(mode==1) {
350                                 MTC_Mat4Invert(pmat, mat);
351                                 MTC_Mat4MulMat4(smat, pmat, lar->shb->viewmat);
352                                 MTC_Mat4MulMat4(lar->shb->persmat, smat, lar->shb->winmat);
353                         }
354                         else MTC_Mat4MulMat4(lar->shb->persmat, lar->shb->viewmat, lar->shb->winmat);
355                 }
356         }
357         
358 }
359
360 /* ------------------------------------------------------------------------- */
361
362 static void env_layerflags(unsigned int notlay)
363 {
364         VlakRen *vlr = NULL;
365         int a;
366         
367         for(a=0; a<R.totvlak; a++) {
368                 if((a & 255)==0) vlr= R.blovl[a>>8];
369                 else vlr++;
370                 if(vlr->lay & notlay) vlr->flag &= ~R_VISIBLE;
371         }
372 }
373
374 static void env_hideobject(Object *ob)
375 {
376         VlakRen *vlr = NULL;
377         int a;
378         
379         for(a=0; a<R.totvlak; a++) {
380                 if((a & 255)==0) vlr= R.blovl[a>>8];
381                 else vlr++;
382                 if(vlr->ob == ob) vlr->flag &= ~R_VISIBLE;
383         }
384 }
385
386 /* ------------------------------------------------------------------------- */
387
388 static void env_set_imats()
389 {
390         Base *base;
391         float mat[4][4];
392         
393         base= G.scene->base.first;
394         while(base) {
395                 MTC_Mat4MulMat4(mat, base->object->obmat, R.viewmat);
396                 MTC_Mat4Invert(base->object->imat, mat);
397                 
398                 base= base->next;
399         }
400
401 }       
402
403 /* ------------------------------------------------------------------------- */
404
405 static void render_envmap(EnvMap *env)
406 {
407         /* only the cubemap is implemented */
408         ImBuf *ibuf;
409         Image *ima;
410         float oldviewinv[4][4], mat[4][4], tmat[4][4];
411         short part;
412         
413         /* need a recalc: ortho-render has no correct viewinv */
414         MTC_Mat4Invert(oldviewinv, R.viewmat);
415
416         /* do first, envmap_renderdata copies entire R struct */
417         if(R.rectz) MEM_freeN(R.rectz); R.rectz= NULL;
418         if(R.rectot) MEM_freeN(R.rectot); R.rectot= NULL;
419         if(R.rectftot) MEM_freeN(R.rectftot); R.rectftot= NULL;
420         
421         /* setup necessary globals */
422         envmap_renderdata(env);
423         
424         RE_local_init_render_display();
425
426         R.rectot= MEM_mallocN(sizeof(int)*R.rectx*R.recty, "rectot");
427         R.rectz=  MEM_mallocN(sizeof(int)*R.rectx*R.recty, "rectz");
428
429         for(part=0; part<6; part++) {
430
431                 RE_local_clear_render_display(R.win);
432                 fillrect(R.rectot, R.rectx, R.recty, 0);
433                 
434                 RE_setwindowclip(1,-1); /*  no jit:(-1) */
435                 
436                 MTC_Mat4CpyMat4(tmat, G.scene->camera->obmat);
437                 MTC_Mat4Ortho(tmat);
438                 envmap_transmatrix(tmat, part);
439                 MTC_Mat4Invert(mat, tmat);
440                 /* mat now is the camera 'viewmat' */
441
442                 MTC_Mat4CpyMat4(R.viewmat, mat);
443                 MTC_Mat4CpyMat4(R.viewinv, tmat);
444                 
445                 /* we have to correct for the already rotated vertexcoords */
446                 MTC_Mat4MulMat4(tmat, oldviewinv, R.viewmat);
447                 MTC_Mat4Invert(env->imat, tmat);
448                 
449                 env_rotate_scene(tmat, 1);
450                 init_render_world();
451                 setzbufvlaggen(RE_projectverto);
452                 env_layerflags(env->notlay);
453                 env_hideobject(env->object);
454                 env_set_imats();
455                                 
456                 if(RE_local_test_break()==0) {
457
458                         RE_local_printrenderinfo(0.0, part);
459
460                         if(R.r.mode & R_OSA) zbufshadeDA();
461                         else zbufshade();
462
463                 }
464                 
465                 /* rotate back */
466                 env_rotate_scene(tmat, 0);
467
468                 if(RE_local_test_break()==0) {
469                         ibuf= IMB_allocImBuf(R.rectx, R.recty, 24, IB_rect, 0);
470                         ima= MEM_callocN(sizeof(Image), "image");
471                         memcpy(ibuf->rect, R.rectot, 4*ibuf->x*ibuf->y);
472                         ima->ibuf= ibuf;
473                         ima->ok= 1;
474                         env->cube[part]= ima;
475                 }
476                 
477                 if(RE_local_test_break()) break;
478
479         }
480         
481         if(R.rectz) MEM_freeN(R.rectz); R.rectz= NULL;
482         if(R.rectot) MEM_freeN(R.rectot); R.rectot= NULL;
483         if(R.rectftot) MEM_freeN(R.rectftot); R.rectftot= NULL;
484         
485         if(RE_local_test_break()) RE_free_envmapdata(env);
486         else {
487                 if(R.r.mode & R_OSA) env->ok= ENV_OSA;
488                 else env->ok= ENV_NORMAL;
489                 env->lastframe= G.scene->r.cfra;
490         }
491         
492         /* restore */
493         envmap_renderdata(0);
494         env_set_imats();
495         init_render_world();
496
497 }
498
499 /* ------------------------------------------------------------------------- */
500
501 void make_envmaps()
502 {
503         Tex *tex;
504         int do_init= 0, depth= 0, trace;
505         
506         if (!(R.r.mode & R_ENVMAP)) return;
507         
508         /* we dont raytrace, disabling the flag will cause ray_transp render solid */
509         trace= (R.r.mode & R_RAYTRACE);
510         R.r.mode &= ~R_RAYTRACE;
511
512         /* 5 = hardcoded max recursion level */
513         while(depth<5) {
514                 tex= G.main->tex.first;
515                 while(tex) {
516                         if(tex->id.us && tex->type==TEX_ENVMAP) {
517                                 if(tex->env && tex->env->object) {
518                                         if(tex->env->object->lay & G.scene->lay) {
519                                                 if(tex->env->stype!=ENV_LOAD) {
520                                                         
521                                                         /* decide if to render an envmap (again) */
522                                                         if(tex->env->depth >= depth) {
523                                                                 
524                                                                 /* set 'recalc' to make sure it does an entire loop of recalcs */
525                                                                 
526                                                                 if(tex->env->ok) {
527                                                                                 /* free when OSA, and old one isn't OSA */
528                                                                         if((R.r.mode & R_OSA) && tex->env->ok==ENV_NORMAL) 
529                                                                                 RE_free_envmapdata(tex->env);
530                                                                                 /* free when size larger */
531                                                                         else if(tex->env->lastsize < R.r.size) 
532                                                                                 RE_free_envmapdata(tex->env);
533                                                                                 /* free when env is in recalcmode */
534                                                                         else if(tex->env->recalc)
535                                                                                 RE_free_envmapdata(tex->env);
536                                                                 }
537                                                                 
538                                                                 if(tex->env->ok==0 && depth==0) tex->env->recalc= 1;
539                                                                 
540                                                                 if(tex->env->ok==0) {
541                                                                         do_init= 1;
542                                                                         render_envmap(tex->env);
543                                                                         
544                                                                         if(depth==tex->env->depth) tex->env->recalc= 0;
545                                                                 }
546                                                         }
547                                                 }
548                                         }
549                                 }
550                         }
551                         tex= tex->id.next;
552                 }
553                 depth++;
554         }
555
556         if(do_init) {
557                 RE_local_init_render_display();
558                 RE_local_clear_render_display(R.win);
559                 R.flag |= R_REDRAW_PRV;
560         }       
561         // restore
562         R.r.mode |= trace;
563
564 }
565
566 /* ------------------------------------------------------------------------- */
567
568 static int envcube_isect(float *vec, float *answ)
569 {
570         float labda;
571         int face;
572         
573         /* which face */
574         if( vec[2]<=-fabs(vec[0]) && vec[2]<=-fabs(vec[1]) ) {
575                 face= 0;
576                 labda= -1.0/vec[2];
577                 answ[0]= labda*vec[0];
578                 answ[1]= labda*vec[1];
579         }
580         else if( vec[2]>=fabs(vec[0]) && vec[2]>=fabs(vec[1]) ) {
581                 face= 1;
582                 labda= 1.0/vec[2];
583                 answ[0]= labda*vec[0];
584                 answ[1]= -labda*vec[1];
585         }
586         else if( vec[1]>=fabs(vec[0]) ) {
587                 face= 2;
588                 labda= 1.0/vec[1];
589                 answ[0]= labda*vec[0];
590                 answ[1]= labda*vec[2];
591         }
592         else if( vec[0]<=-fabs(vec[1]) ) {
593                 face= 3;
594                 labda= -1.0/vec[0];
595                 answ[0]= labda*vec[1];
596                 answ[1]= labda*vec[2];
597         }
598         else if( vec[1]<=-fabs(vec[0]) ) {
599                 face= 4;
600                 labda= -1.0/vec[1];
601                 answ[0]= -labda*vec[0];
602                 answ[1]= labda*vec[2];
603         }
604         else {
605                 face= 5;
606                 labda= 1.0/vec[0];
607                 answ[0]= -labda*vec[1];
608                 answ[1]= labda*vec[2];
609         }
610         answ[0]= 0.5+0.5*answ[0];
611         answ[1]= 0.5+0.5*answ[1];
612         return face;
613 }
614
615 /* ------------------------------------------------------------------------- */
616
617 static void set_dxtdyt(float *dxts, float *dyts, float *dxt, float *dyt, int face)
618 {
619         if(face==2 || face==4) {
620                 dxts[0]= dxt[0];
621                 dyts[0]= dyt[0];
622                 dxts[1]= dxt[2];
623                 dyts[1]= dyt[2];
624         }
625         else if(face==3 || face==5) {
626                 dxts[0]= dxt[1];
627                 dxts[1]= dxt[2];
628                 dyts[0]= dyt[1];
629                 dyts[1]= dyt[2];
630         }
631         else {
632                 dxts[0]= dxt[0];
633                 dyts[0]= dyt[0];
634                 dxts[1]= dxt[1];
635                 dyts[1]= dyt[1];
636         }
637 }
638
639 /* ------------------------------------------------------------------------- */
640
641 int envmaptex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres)
642 {
643         extern SDL_mutex *load_ibuf_lock; // initrender.c
644         /* texvec should be the already reflected normal */
645         EnvMap *env;
646         Image *ima;
647         float fac, vec[3], sco[3], dxts[3], dyts[3];
648         int face, face1;
649         
650         env= tex->env;
651         if(env==NULL || (env->stype!=ENV_LOAD && env->object==NULL)) {
652                 texres->tin= 0.0;
653                 return 0;
654         }
655         if(env->stype==ENV_LOAD) {
656                 env->ima= tex->ima;
657                 if(env->ima && env->ima->ok) {
658                         // Now thread safe
659                         if(load_ibuf_lock) SDL_mutexP(load_ibuf_lock);
660                         if(env->ima->ibuf==NULL) ima_ibuf_is_nul(tex, tex->ima);
661                         if(load_ibuf_lock) SDL_mutexV(load_ibuf_lock);
662                         if(env->ima->ok && env->ok==0) envmap_split_ima(env);
663                 }
664         }
665
666         if(env->ok==0) {
667                 
668                 texres->tin= 0.0;
669                 return 0;
670         }
671         
672         /* rotate to envmap space, if object is set */
673         VECCOPY(vec, texvec);
674         if(env->object) MTC_Mat4Mul3Vecfl(env->object->imat, vec);
675         else MTC_Mat4Mul3Vecfl(R.viewinv, vec);
676         
677         face= envcube_isect(vec, sco);
678         ima= env->cube[face];
679         
680         if(osatex) {
681                 if(env->object) {
682                         MTC_Mat4Mul3Vecfl(env->object->imat, dxt);
683                         MTC_Mat4Mul3Vecfl(env->object->imat, dyt);
684                 }
685                 else {
686                         MTC_Mat4Mul3Vecfl(R.viewinv, dxt);
687                         MTC_Mat4Mul3Vecfl(R.viewinv, dyt);
688                 }
689                 set_dxtdyt(dxts, dyts, dxt, dyt, face);
690                 imagewraposa(tex, ima, sco, dxts, dyts, texres);
691                 
692                 /* edges? */
693                 
694                 if(texres->ta<1.0) {
695                         TexResult texr1, texr2;
696         
697                         texr1.nor= texr2.nor= NULL;
698
699                         VecAddf(vec, vec, dxt);
700                         face1= envcube_isect(vec, sco);
701                         VecSubf(vec, vec, dxt);
702                         
703                         if(face!=face1) {
704                                 ima= env->cube[face1];
705                                 set_dxtdyt(dxts, dyts, dxt, dyt, face1);
706                                 imagewraposa(tex, ima, sco, dxts, dyts, &texr1);
707                         }
708                         else texr1.tr= texr1.tg= texr1.tb= texr1.ta= 0.0;
709                         
710                         /* here was the nasty bug! results were not zero-ed. FPE! */
711                         
712                         VecAddf(vec, vec, dyt);
713                         face1= envcube_isect(vec, sco);
714                         VecSubf(vec, vec, dyt);
715                         
716                         if(face!=face1) {
717                                 ima= env->cube[face1];
718                                 set_dxtdyt(dxts, dyts, dxt, dyt, face1);
719                                 imagewraposa(tex, ima, sco, dxts, dyts, &texr2);
720                         }
721                         else texr2.tr= texr2.tg= texr2.tb= texr2.ta= 0.0;
722                         
723                         fac= (texres->ta+texr1.ta+texr2.ta);
724                         if(fac!=0.0) {
725                                 fac= 1.0/fac;
726                                 
727                                 texres->tr= fac*(texres->ta*texres->tr + texr1.ta*texr1.tr + texr2.ta*texr2.tr );
728                                 texres->tg= fac*(texres->ta*texres->tg + texr1.ta*texr1.tg + texr2.ta*texr2.tg );
729                                 texres->tb= fac*(texres->ta*texres->tb + texr1.ta*texr1.tb + texr2.ta*texr2.tb );
730                         }
731                         texres->ta= 1.0;
732                 }
733         }
734         else {
735                 imagewrap(tex, ima, sco, texres);
736         }
737         
738         return 1;
739 }
740
741 /* ------------------------------------------------------------------------- */
742
743 /* eof */