Blender  V2.59
shadeinput.c
Go to the documentation of this file.
00001 /*
00002 * $Id: shadeinput.c 38139 2011-07-06 09:15:18Z blendix $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2006 Blender Foundation
00021  * All rights reserved.
00022  *
00023  * Contributors: Hos, Robert Wenzlaff.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include <stdio.h>
00034 #include <math.h>
00035 #include <string.h>
00036 
00037 
00038 #include "BLI_math.h"
00039 #include "BLI_blenlib.h"
00040 #include "BLI_utildefines.h"
00041 
00042 #include "DNA_curve_types.h"
00043 #include "DNA_group_types.h"
00044 #include "DNA_lamp_types.h"
00045 #include "DNA_meshdata_types.h"
00046 #include "DNA_material_types.h"
00047 
00048 #include "BKE_colortools.h"
00049 
00050 #include "BKE_node.h"
00051 
00052 /* local include */
00053 #include "raycounter.h"
00054 #include "renderpipeline.h"
00055 #include "render_types.h"
00056 #include "renderdatabase.h"
00057 #include "rendercore.h"
00058 #include "shadbuf.h"
00059 #include "shading.h"
00060 #include "strand.h"
00061 #include "texture.h"
00062 #include "volumetric.h"
00063 #include "zbuf.h"
00064 
00065 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
00066 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
00067 /* only to be used here in this file, it's for speed */
00068 extern struct Render R;
00069 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
00070 
00071 
00072 #define VECADDISFAC(v1,v3,fac) {*(v1)+= *(v3)*(fac); *(v1+1)+= *(v3+1)*(fac); *(v1+2)+= *(v3+2)*(fac);}
00073 
00074 
00075 
00076 /* Shade Sample order:
00077 
00078 - shade_samples_fill_with_ps()
00079         - for each sample
00080                 - shade_input_set_triangle()  <- if prev sample-face is same, use shade_input_copy_triangle()
00081                 - if vlr
00082                         - shade_input_set_viewco()    <- not for ray or bake
00083                         - shade_input_set_uv()        <- not for ray or bake
00084                         - shade_input_set_normals()
00085 - shade_samples()
00086         - if AO
00087                 - shade_samples_do_AO()
00088         - if shading happens
00089                 - for each sample
00090                         - shade_input_set_shade_texco()
00091                         - shade_samples_do_shade()
00092 - OSA: distribute sample result with filter masking
00093 
00094         */
00095 
00096 /* initialise material variables in shadeinput, 
00097  * doing inverse gamma correction where applicable */
00098 void shade_input_init_material(ShadeInput *shi)
00099 {
00100         /* note, keep this synced with render_types.h */
00101         memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
00102         shi->har= shi->mat->har;
00103 }
00104 
00105 /* also used as callback for nodes */
00106 /* delivers a fully filled in ShadeResult, for all passes */
00107 void shade_material_loop(ShadeInput *shi, ShadeResult *shr)
00108 {
00109 
00110         shade_lamp_loop(shi, shr);      /* clears shr */
00111         
00112         if(shi->translucency!=0.0f) {
00113                 ShadeResult shr_t;
00114                 float fac= shi->translucency;
00115                 
00116                 shade_input_init_material(shi);
00117 
00118                 VECCOPY(shi->vn, shi->vno);
00119                 VECMUL(shi->vn, -1.0f);
00120                 VECMUL(shi->facenor, -1.0f);
00121                 shi->depth++;   /* hack to get real shadow now */
00122                 shade_lamp_loop(shi, &shr_t);
00123                 shi->depth--;
00124 
00125                 /* a couple of passes */
00126                 VECADDISFAC(shr->combined, shr_t.combined, fac);
00127                 if(shi->passflag & SCE_PASS_SPEC)
00128                         VECADDISFAC(shr->spec, shr_t.spec, fac);
00129                 if(shi->passflag & SCE_PASS_DIFFUSE)
00130                         VECADDISFAC(shr->diff, shr_t.diff, fac);
00131                 if(shi->passflag & SCE_PASS_SHADOW)
00132                         VECADDISFAC(shr->shad, shr_t.shad, fac);
00133 
00134                 VECMUL(shi->vn, -1.0f);
00135                 VECMUL(shi->facenor, -1.0f);
00136         }
00137         
00138         /* depth >= 1 when ray-shading */
00139         if(shi->depth==0 || shi->volume_depth > 0) {
00140                 if(R.r.mode & R_RAYTRACE) {
00141                         if(shi->ray_mirror!=0.0f || ((shi->mode & MA_TRANSP) && (shi->mode & MA_RAYTRANSP) && shr->alpha!=1.0f)) {
00142                                 /* ray trace works on combined, but gives pass info */
00143                                 ray_trace(shi, shr);
00144                         }
00145                 }
00146                 /* disable adding of sky for raytransp */
00147                 if((shi->mode & MA_TRANSP) && (shi->mode & MA_RAYTRANSP))
00148                         if((shi->layflag & SCE_LAY_SKY) && (R.r.alphamode==R_ADDSKY))
00149                                 shr->alpha= 1.0f;
00150         }
00151         
00152         if(R.r.mode & R_RAYTRACE) {
00153                 if (R.render_volumes_inside.first)
00154                         shade_volume_inside(shi, shr);
00155         }
00156 }
00157 
00158 
00159 /* do a shade, finish up some passes, apply mist */
00160 void shade_input_do_shade(ShadeInput *shi, ShadeResult *shr)
00161 {
00162         float alpha;
00163         
00164         /* ------  main shading loop -------- */
00165 #ifdef RE_RAYCOUNTER
00166         memset(&shi->raycounter, 0, sizeof(shi->raycounter));
00167 #endif
00168         
00169         if(shi->mat->nodetree && shi->mat->use_nodes) {
00170                 ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
00171         }
00172         else {
00173                 /* copy all relevant material vars, note, keep this synced with render_types.h */
00174                 shade_input_init_material(shi);
00175                 
00176                 if (shi->mat->material_type == MA_TYPE_VOLUME) {
00177                         if(R.r.mode & R_RAYTRACE) {
00178                                 shade_volume_outside(shi, shr);
00179                         }
00180                 } else { /* MA_TYPE_SURFACE, MA_TYPE_WIRE */
00181                         shade_material_loop(shi, shr);
00182                 }
00183         }
00184         
00185         /* copy additional passes */
00186         if(shi->passflag & (SCE_PASS_VECTOR|SCE_PASS_NORMAL)) {
00187                 QUATCOPY(shr->winspeed, shi->winspeed);
00188                 VECCOPY(shr->nor, shi->vn);
00189         }
00190         
00191         /* MIST */
00192         if((shi->passflag & SCE_PASS_MIST) || ((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST)==0))  {
00193                 if(R.r.mode & R_ORTHO)
00194                         shr->mist= mistfactor(-shi->co[2], shi->co);
00195                 else
00196                         shr->mist= mistfactor(len_v3(shi->co), shi->co);
00197         }
00198         else shr->mist= 0.0f;
00199         
00200         if((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST)==0 ) {
00201                 alpha= shr->mist;
00202         }
00203         else alpha= 1.0f;
00204         
00205         /* add mist and premul color */
00206         if(shr->alpha!=1.0f || alpha!=1.0f) {
00207                 float fac= alpha*(shr->alpha);
00208                 shr->combined[3]= fac;
00209                 
00210                 if (shi->mat->material_type!= MA_TYPE_VOLUME)
00211                         mul_v3_fl(shr->combined, fac);
00212         }
00213         else
00214                 shr->combined[3]= 1.0f;
00215         
00216         /* add z */
00217         shr->z= -shi->co[2];
00218         
00219         /* RAYHITS */
00220 /*
00221         if(1 || shi->passflag & SCE_PASS_RAYHITS)
00222         {
00223                 shr->rayhits[0] = (float)shi->raycounter.faces.test;
00224                 shr->rayhits[1] = (float)shi->raycounter.bb.hit;
00225                 shr->rayhits[2] = 0.0;
00226                 shr->rayhits[3] = 1.0;
00227         }
00228  */
00229         RE_RC_MERGE(&re_rc_counter[shi->thread], &shi->raycounter);
00230 }
00231 
00232 /* **************************************************************************** */
00233 /*                    ShadeInput                                                */
00234 /* **************************************************************************** */
00235 
00236 
00237 void vlr_set_uv_indices(VlakRen *vlr, int *i1, int *i2, int *i3)
00238 {
00239         /* to prevent storing new tfaces or vcols, we check a split runtime */
00240         /*              4---3           4---3 */
00241         /*              |\ 1|   or  |1 /| */
00242         /*              |0\ |           |/ 0| */
00243         /*              1---2           1---2   0 = orig face, 1 = new face */
00244         
00245         /* Update vert nums to point to correct verts of original face */
00246         if(vlr->flag & R_DIVIDE_24) {  
00247                 if(vlr->flag & R_FACE_SPLIT) {
00248                         (*i1)++; (*i2)++; (*i3)++;
00249                 }
00250                 else {
00251                         (*i3)++;
00252                 }
00253         }
00254         else if(vlr->flag & R_FACE_SPLIT) {
00255                 (*i2)++; (*i3)++; 
00256         }
00257 }
00258 
00259 /* copy data from face to ShadeInput, general case */
00260 /* indices 0 1 2 3 only */
00261 void shade_input_set_triangle_i(ShadeInput *shi, ObjectInstanceRen *obi, VlakRen *vlr, short i1, short i2, short i3)
00262 {
00263         VertRen **vpp= &vlr->v1;
00264         
00265         shi->vlr= vlr;
00266         shi->obi= obi;
00267         shi->obr= obi->obr;
00268 
00269         shi->v1= vpp[i1];
00270         shi->v2= vpp[i2];
00271         shi->v3= vpp[i3];
00272         
00273         shi->i1= i1;
00274         shi->i2= i2;
00275         shi->i3= i3;
00276         
00277         /* note, shi->mat is set in node shaders */
00278         shi->mat= shi->mat_override?shi->mat_override:vlr->mat;
00279         
00280         shi->osatex= (shi->mat->texco & TEXCO_OSA);
00281         shi->mode= shi->mat->mode_l;            /* or-ed result for all nodes */
00282 
00283         /* facenormal copy, can get flipped */
00284         shi->flippednor= 0;
00285         RE_vlakren_get_normal(&R, obi, vlr, shi->facenor);
00286         
00287         /* calculate vertexnormals */
00288         if(vlr->flag & R_SMOOTH) {
00289                 VECCOPY(shi->n1, shi->v1->n);
00290                 VECCOPY(shi->n2, shi->v2->n);
00291                 VECCOPY(shi->n3, shi->v3->n);
00292 
00293                 if(obi->flag & R_TRANSFORMED) {
00294                         mul_m3_v3(obi->nmat, shi->n1); normalize_v3(shi->n1);
00295                         mul_m3_v3(obi->nmat, shi->n2); normalize_v3(shi->n2);
00296                         mul_m3_v3(obi->nmat, shi->n3); normalize_v3(shi->n3);
00297                 }
00298         }
00299 }
00300 
00301 /* note, facenr declared volatile due to over-eager -O2 optimizations
00302  * on cygwin (particularly -frerun-cse-after-loop)
00303  */
00304 
00305 /* copy data from face to ShadeInput, scanline case */
00306 void shade_input_set_triangle(ShadeInput *shi, volatile int obi, volatile int facenr, int UNUSED(normal_flip))
00307 {
00308         if(facenr>0) {
00309                 shi->obi= &R.objectinstance[obi];
00310                 shi->obr= shi->obi->obr;
00311                 shi->facenr= (facenr-1) & RE_QUAD_MASK;
00312                 if( shi->facenr < shi->obr->totvlak ) {
00313                         VlakRen *vlr= RE_findOrAddVlak(shi->obr, shi->facenr);
00314                         
00315                         if(facenr & RE_QUAD_OFFS)
00316                                 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 2, 3);
00317                         else
00318                                 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 1, 2);
00319                 }
00320                 else
00321                         shi->vlr= NULL; /* general signal we got sky */
00322         }
00323         else
00324                 shi->vlr= NULL; /* general signal we got sky */
00325 }
00326 
00327 /* full osa case: copy static info */
00328 void shade_input_copy_triangle(ShadeInput *shi, ShadeInput *from)
00329 {
00330         /* not so nice, but works... warning is in RE_shader_ext.h */
00331         memcpy(shi, from, sizeof(struct ShadeInputCopy));
00332 }
00333 
00334 /* copy data from strand to shadeinput */
00335 void shade_input_set_strand(ShadeInput *shi, StrandRen *strand, StrandPoint *spoint)
00336 {
00337         /* note, shi->mat is set in node shaders */
00338         shi->mat= shi->mat_override? shi->mat_override: strand->buffer->ma;
00339         
00340         shi->osatex= (shi->mat->texco & TEXCO_OSA);
00341         shi->mode= shi->mat->mode_l;            /* or-ed result for all nodes */
00342 
00343         /* shade_input_set_viewco equivalent */
00344         VECCOPY(shi->co, spoint->co);
00345         VECCOPY(shi->view, shi->co);
00346         normalize_v3(shi->view);
00347 
00348         shi->xs= (int)spoint->x;
00349         shi->ys= (int)spoint->y;
00350 
00351         if(shi->osatex || (R.r.mode & R_SHADOW)) {
00352                 VECCOPY(shi->dxco, spoint->dtco);
00353                 VECCOPY(shi->dyco, spoint->dsco);
00354         }
00355 
00356         /* dxview, dyview, not supported */
00357 
00358         /* facenormal, simply viewco flipped */
00359         VECCOPY(shi->facenor, spoint->nor);
00360 
00361         /* shade_input_set_normals equivalent */
00362         if(shi->mat->mode & MA_TANGENT_STR) {
00363                 VECCOPY(shi->vn, spoint->tan)
00364         }
00365         else {
00366                 float cross[3];
00367 
00368                 cross_v3_v3v3(cross, spoint->co, spoint->tan);
00369                 cross_v3_v3v3(shi->vn, cross, spoint->tan);
00370                 normalize_v3(shi->vn);
00371 
00372                 if(INPR(shi->vn, shi->view) < 0.0f)
00373                         negate_v3(shi->vn);
00374         }
00375 
00376         VECCOPY(shi->vno, shi->vn);
00377 }
00378 
00379 void shade_input_set_strand_texco(ShadeInput *shi, StrandRen *strand, StrandVert *svert, StrandPoint *spoint)
00380 {
00381         StrandBuffer *strandbuf= strand->buffer;
00382         ObjectRen *obr= strandbuf->obr;
00383         StrandVert *sv;
00384         int mode= shi->mode;            /* or-ed result for all nodes */
00385         short texco= shi->mat->texco;
00386 
00387         if((shi->mat->texco & TEXCO_REFL)) {
00388                 /* shi->dxview, shi->dyview, not supported */
00389         }
00390 
00391         if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL))) {
00392                 /* not supported */
00393         }
00394 
00395         if(mode & (MA_TANGENT_V|MA_NORMAP_TANG)) {
00396                 VECCOPY(shi->tang, spoint->tan);
00397                 VECCOPY(shi->nmaptang, spoint->tan);
00398         }
00399 
00400         if(mode & MA_STR_SURFDIFF) {
00401                 float *surfnor= RE_strandren_get_surfnor(obr, strand, 0);
00402 
00403                 if(surfnor)
00404                         VECCOPY(shi->surfnor, surfnor)
00405                 else
00406                         VECCOPY(shi->surfnor, shi->vn)
00407 
00408                 if(shi->mat->strand_surfnor > 0.0f) {
00409                         shi->surfdist= 0.0f;
00410                         for(sv=strand->vert; sv!=svert; sv++)
00411                                 shi->surfdist+=len_v3v3(sv->co, (sv+1)->co);
00412                         shi->surfdist += spoint->t*len_v3v3(sv->co, (sv+1)->co);
00413                 }
00414         }
00415 
00416         if(R.r.mode & R_SPEED) {
00417                 float *speed;
00418                 
00419                 speed= RE_strandren_get_winspeed(shi->obi, strand, 0);
00420                 if(speed)
00421                         QUATCOPY(shi->winspeed, speed)
00422                 else
00423                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
00424         }
00425 
00426         /* shade_input_set_shade_texco equivalent */
00427         if(texco & NEED_UV) {
00428                 if(texco & TEXCO_ORCO) {
00429                         VECCOPY(shi->lo, strand->orco);
00430                         /* no shi->osatex, orco derivatives are zero */
00431                 }
00432 
00433                 if(texco & TEXCO_GLOB) {
00434                         VECCOPY(shi->gl, shi->co);
00435                         mul_m4_v3(R.viewinv, shi->gl);
00436                         
00437                         if(shi->osatex) {
00438                                 VECCOPY(shi->dxgl, shi->dxco); 
00439                                 mul_mat3_m4_v3(R.viewinv, shi->dxgl); 
00440                                 VECCOPY(shi->dygl, shi->dyco); 
00441                                 mul_mat3_m4_v3(R.viewinv, shi->dygl);
00442                         }
00443                 }
00444 
00445                 if(texco & TEXCO_STRAND) {
00446                         shi->strandco= spoint->strandco;
00447 
00448                         if(shi->osatex) {
00449                                 shi->dxstrand= spoint->dtstrandco;
00450                                 shi->dystrand= 0.0f;
00451                         }
00452                 }
00453 
00454                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
00455                         MCol *mcol;
00456                         float *uv;
00457                         char *name;
00458                         int i;
00459 
00460                         shi->totuv= 0;
00461                         shi->totcol= 0;
00462                         shi->actuv= obr->actmtface;
00463                         shi->actcol= obr->actmcol;
00464 
00465                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
00466                                 for (i=0; (mcol=RE_strandren_get_mcol(obr, strand, i, &name, 0)); i++) {
00467                                         ShadeInputCol *scol= &shi->col[i];
00468                                         char *cp= (char*)mcol;
00469                                         
00470                                         shi->totcol++;
00471                                         scol->name= name;
00472 
00473                                         scol->col[0]= cp[3]/255.0f;
00474                                         scol->col[1]= cp[2]/255.0f;
00475                                         scol->col[2]= cp[1]/255.0f;
00476                                 }
00477 
00478                                 if(shi->totcol) {
00479                                         shi->vcol[0]= shi->col[shi->actcol].col[0];
00480                                         shi->vcol[1]= shi->col[shi->actcol].col[1];
00481                                         shi->vcol[2]= shi->col[shi->actcol].col[2];
00482                                 }
00483                                 else {
00484                                         shi->vcol[0]= 0.0f;
00485                                         shi->vcol[1]= 0.0f;
00486                                         shi->vcol[2]= 0.0f;
00487                                 }
00488                         }
00489 
00490                         for (i=0; (uv=RE_strandren_get_uv(obr, strand, i, &name, 0)); i++) {
00491                                 ShadeInputUV *suv= &shi->uv[i];
00492 
00493                                 shi->totuv++;
00494                                 suv->name= name;
00495 
00496                                 if(strandbuf->overrideuv == i) {
00497                                         suv->uv[0]= -1.0f;
00498                                         suv->uv[1]= spoint->strandco;
00499                                         suv->uv[2]= 0.0f;
00500                                 }
00501                                 else {
00502                                         suv->uv[0]= -1.0f + 2.0f*uv[0];
00503                                         suv->uv[1]= -1.0f + 2.0f*uv[1];
00504                                         suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
00505                                 }
00506 
00507                                 if(shi->osatex) {
00508                                         suv->dxuv[0]= 0.0f;
00509                                         suv->dxuv[1]= 0.0f;
00510                                         suv->dyuv[0]= 0.0f;
00511                                         suv->dyuv[1]= 0.0f;
00512                                 }
00513 
00514                                 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
00515                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
00516                                                 shi->vcol[0]= 1.0f;
00517                                                 shi->vcol[1]= 1.0f;
00518                                                 shi->vcol[2]= 1.0f;
00519                                         }
00520                                 }
00521                         }
00522 
00523                         if(shi->totuv == 0) {
00524                                 ShadeInputUV *suv= &shi->uv[0];
00525 
00526                                 suv->uv[0]= 0.0f;
00527                                 suv->uv[1]= spoint->strandco;
00528                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
00529                                 
00530                                 if(mode & MA_FACETEXTURE) {
00531                                         /* no tface? set at 1.0f */
00532                                         shi->vcol[0]= 1.0f;
00533                                         shi->vcol[1]= 1.0f;
00534                                         shi->vcol[2]= 1.0f;
00535                                 }
00536                         }
00537 
00538                 }
00539 
00540                 if(texco & TEXCO_NORM) {
00541                         shi->orn[0]= -shi->vn[0];
00542                         shi->orn[1]= -shi->vn[1];
00543                         shi->orn[2]= -shi->vn[2];
00544                 }
00545 
00546                 if(texco & TEXCO_STRESS) {
00547                         /* not supported */
00548                 }
00549 
00550                 if(texco & TEXCO_TANGENT) {
00551                         if((mode & MA_TANGENT_V)==0) {
00552                                 /* just prevent surprises */
00553                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
00554                                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
00555                         }
00556                 }
00557         }
00558 
00559         /* this only avalailable for scanline renders */
00560         if(shi->depth==0) {
00561                 if(texco & TEXCO_WINDOW) {
00562                         shi->winco[0]= -1.0f + 2.0f*spoint->x/(float)R.winx;
00563                         shi->winco[1]= -1.0f + 2.0f*spoint->y/(float)R.winy;
00564                         shi->winco[2]= 0.0f;
00565 
00566                         /* not supported */
00567                         if(shi->osatex) {
00568                                 shi->dxwin[0]= 0.0f;
00569                                 shi->dywin[1]= 0.0f;
00570                                 shi->dxwin[0]= 0.0f;
00571                                 shi->dywin[1]= 0.0f;
00572                         }
00573                 }
00574 
00575                 if(texco & TEXCO_STICKY) {
00576                         /* not supported */
00577                 }
00578         }
00579         
00580         if (shi->do_manage) {
00581                 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
00582                         srgb_to_linearrgb_v3_v3(shi->vcol, shi->vcol);
00583                 }
00584         }
00585         
00586 }
00587 
00588 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
00589 void shade_input_calc_viewco(ShadeInput *shi, float x, float y, float z, float *view, float *dxyview, float *co, float *dxco, float *dyco)
00590 {
00591         /* returns not normalized, so is in viewplane coords */
00592         calc_view_vector(view, x, y);
00593         
00594         if(shi->mat->material_type == MA_TYPE_WIRE) {
00595                 /* wire cannot use normal for calculating shi->co, so
00596                  * we reconstruct the coordinate less accurate */
00597                 if(R.r.mode & R_ORTHO)
00598                         calc_renderco_ortho(co, x, y, z);
00599                 else
00600                         calc_renderco_zbuf(co, view, z);
00601         }
00602         else {
00603                 /* for non-wire, intersect with the triangle to get the exact coord */
00604                 float fac, dface, v1[3];
00605                 
00606                 VECCOPY(v1, shi->v1->co);
00607                 if(shi->obi->flag & R_TRANSFORMED)
00608                         mul_m4_v3(shi->obi->mat, v1);
00609                 
00610                 dface= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
00611                 
00612                 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
00613                 if(R.r.mode & R_ORTHO) {
00614                         /* x and y 3d coordinate can be derived from pixel coord and winmat */
00615                         float fx= 2.0f/(R.winx*R.winmat[0][0]);
00616                         float fy= 2.0f/(R.winy*R.winmat[1][1]);
00617                         
00618                         co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
00619                         co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
00620                         
00621                         /* using a*x + b*y + c*z = d equation, (a b c) is normal */
00622                         if(shi->facenor[2]!=0.0f)
00623                                 co[2]= (dface - shi->facenor[0]*co[0] - shi->facenor[1]*co[1])/shi->facenor[2];
00624                         else
00625                                 co[2]= 0.0f;
00626                         
00627                         if(dxco && dyco) {
00628                                 dxco[0]= fx;
00629                                 dxco[1]= 0.0f;
00630                                 if(shi->facenor[2]!=0.0f)
00631                                         dxco[2]= -(shi->facenor[0]*fx)/shi->facenor[2];
00632                                 else 
00633                                         dxco[2]= 0.0f;
00634                                 
00635                                 dyco[0]= 0.0f;
00636                                 dyco[1]= fy;
00637                                 if(shi->facenor[2]!=0.0f)
00638                                         dyco[2]= -(shi->facenor[1]*fy)/shi->facenor[2];
00639                                 else 
00640                                         dyco[2]= 0.0f;
00641                                 
00642                                 if(dxyview) {
00643                                         if(co[2]!=0.0f) fac= 1.0f/co[2]; else fac= 0.0f;
00644                                         dxyview[0]= -R.viewdx*fac;
00645                                         dxyview[1]= -R.viewdy*fac;
00646                                 }
00647                         }
00648                 }
00649                 else {
00650                         float div;
00651                         
00652                         div= shi->facenor[0]*view[0] + shi->facenor[1]*view[1] + shi->facenor[2]*view[2];
00653                         if (div!=0.0f) fac= dface/div;
00654                         else fac= 0.0f;
00655                         
00656                         co[0]= fac*view[0];
00657                         co[1]= fac*view[1];
00658                         co[2]= fac*view[2];
00659                         
00660                         /* pixel dx/dy for render coord */
00661                         if(dxco && dyco) {
00662                                 float u= dface/(div - R.viewdx*shi->facenor[0]);
00663                                 float v= dface/(div - R.viewdy*shi->facenor[1]);
00664                                 
00665                                 dxco[0]= co[0]- (view[0]-R.viewdx)*u;
00666                                 dxco[1]= co[1]- (view[1])*u;
00667                                 dxco[2]= co[2]- (view[2])*u;
00668                                 
00669                                 dyco[0]= co[0]- (view[0])*v;
00670                                 dyco[1]= co[1]- (view[1]-R.viewdy)*v;
00671                                 dyco[2]= co[2]- (view[2])*v;
00672                                 
00673                                 if(dxyview) {
00674                                         if(fac!=0.0f) fac= 1.0f/fac;
00675                                         dxyview[0]= -R.viewdx*fac;
00676                                         dxyview[1]= -R.viewdy*fac;
00677                                 }
00678                         }
00679                 }
00680         }
00681         
00682         /* set camera coords - for scanline, it's always 0.0,0.0,0.0 (render is in camera space)
00683          * however for raytrace it can be different - the position of the last intersection */
00684         shi->camera_co[0] = shi->camera_co[1] = shi->camera_co[2] = 0.0f;
00685         
00686         /* cannot normalize earlier, code above needs it at viewplane level */
00687         normalize_v3(view);
00688 }
00689 
00690 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
00691 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float xs, float ys, float z)
00692 {
00693         float *dxyview= NULL, *dxco= NULL, *dyco= NULL;
00694         
00695         /* currently in use for dithering (soft shadow), node preview, irregular shad */
00696         shi->xs= (int)xs;
00697         shi->ys= (int)ys;
00698 
00699         /* original scanline coordinate without jitter */
00700         shi->scanco[0]= x;
00701         shi->scanco[1]= y;
00702         shi->scanco[2]= z;
00703 
00704         /* check if we need derivatives */
00705         if(shi->osatex || (R.r.mode & R_SHADOW)) {
00706                 dxco= shi->dxco;
00707                 dyco= shi->dyco;
00708 
00709                 if((shi->mat->texco & TEXCO_REFL))
00710                         dxyview= &shi->dxview;
00711         }
00712 
00713         shade_input_calc_viewco(shi, xs, ys, z, shi->view, dxyview, shi->co, dxco, dyco);
00714 }
00715 
00716 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
00717 void shade_input_set_uv(ShadeInput *shi)
00718 {
00719         VlakRen *vlr= shi->vlr;
00720         
00721         if((vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
00722                 float v1[3], v2[3], v3[3];
00723 
00724                 VECCOPY(v1, shi->v1->co);
00725                 VECCOPY(v2, shi->v2->co);
00726                 VECCOPY(v3, shi->v3->co);
00727 
00728                 if(shi->obi->flag & R_TRANSFORMED) {
00729                         mul_m4_v3(shi->obi->mat, v1);
00730                         mul_m4_v3(shi->obi->mat, v2);
00731                         mul_m4_v3(shi->obi->mat, v3);
00732                 }
00733 
00734                 /* exception case for wire render of edge */
00735                 if(vlr->v2==vlr->v3) {
00736                         float lend, lenc;
00737                         
00738                         lend= len_v3v3(v2, v1);
00739                         lenc= len_v3v3(shi->co, v1);
00740                         
00741                         if(lend==0.0f) {
00742                                 shi->u=shi->v= 0.0f;
00743                         }
00744                         else {
00745                                 shi->u= - (1.0f - lenc/lend);
00746                                 shi->v= 0.0f;
00747                         }
00748                         
00749                         if(shi->osatex) {
00750                                 shi->dx_u=  0.0f;
00751                                 shi->dx_v=  0.0f;
00752                                 shi->dy_u=  0.0f;
00753                                 shi->dy_v=  0.0f;
00754                         }
00755                 }
00756                 else {
00757                         /* most of this could become re-used for faces */
00758                         float detsh, t00, t10, t01, t11, xn, yn, zn;
00759                         int axis1, axis2;
00760 
00761                         /* find most stable axis to project */
00762                         xn= fabs(shi->facenor[0]);
00763                         yn= fabs(shi->facenor[1]);
00764                         zn= fabs(shi->facenor[2]);
00765 
00766                         if(zn>=xn && zn>=yn) { axis1= 0; axis2= 1; }
00767                         else if(yn>=xn && yn>=zn) { axis1= 0; axis2= 2; }
00768                         else { axis1= 1; axis2= 2; }
00769 
00770                         /* compute u,v and derivatives */
00771                         t00= v3[axis1]-v1[axis1]; t01= v3[axis2]-v1[axis2];
00772                         t10= v3[axis1]-v2[axis1]; t11= v3[axis2]-v2[axis2];
00773 
00774                         detsh= (t00*t11-t10*t01);
00775                         detsh= (detsh != 0.0f)? 1.0f/detsh: 0.0f;
00776                         t00*= detsh; t01*=detsh; 
00777                         t10*=detsh; t11*=detsh;
00778 
00779                         shi->u= (shi->co[axis1]-v3[axis1])*t11-(shi->co[axis2]-v3[axis2])*t10;
00780                         shi->v= (shi->co[axis2]-v3[axis2])*t00-(shi->co[axis1]-v3[axis1])*t01;
00781                         if(shi->osatex) {
00782                                 shi->dx_u=  shi->dxco[axis1]*t11- shi->dxco[axis2]*t10;
00783                                 shi->dx_v=  shi->dxco[axis2]*t00- shi->dxco[axis1]*t01;
00784                                 shi->dy_u=  shi->dyco[axis1]*t11- shi->dyco[axis2]*t10;
00785                                 shi->dy_v=  shi->dyco[axis2]*t00- shi->dyco[axis1]*t01;
00786                         }
00787 
00788                         /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
00789                         CLAMP(shi->u, -2.0f, 1.0f);
00790                         CLAMP(shi->v, -2.0f, 1.0f);
00791                 }
00792         }       
00793 }
00794 
00795 void shade_input_set_normals(ShadeInput *shi)
00796 {
00797         float u= shi->u, v= shi->v;
00798         float l= 1.0f+u+v;
00799 
00800         shi->flippednor = 0;
00801 
00802         /* test flip normals to viewing direction */
00803         if(!(shi->vlr->flag & R_TANGENT)) {
00804                 if(dot_v3v3(shi->facenor, shi->view) < 0.0f) {
00805                         negate_v3(shi->facenor);
00806                         shi->flippednor= 1;
00807                 }
00808         }
00809         
00810         /* calculate vertexnormals */
00811         if(shi->vlr->flag & R_SMOOTH) {
00812                 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
00813 
00814                 if(shi->flippednor) {
00815                         negate_v3(n1);
00816                         negate_v3(n2);
00817                         negate_v3(n3);
00818                 }
00819                 
00820                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
00821                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
00822                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
00823 
00824                 // use unnormalized normal (closer to games)
00825                 VECCOPY(shi->nmapnorm, shi->vn);
00826                 
00827                 normalize_v3(shi->vn);
00828         }
00829         else
00830         {
00831                 VECCOPY(shi->vn, shi->facenor);
00832                 VECCOPY(shi->nmapnorm, shi->vn);
00833         }
00834         
00835         /* used in nodes */
00836         VECCOPY(shi->vno, shi->vn);
00837 
00838         /* flip normals to viewing direction */
00839         if(!(shi->vlr->flag & R_TANGENT))
00840                 if(dot_v3v3(shi->facenor, shi->view) < 0.0f)
00841                         shade_input_flip_normals(shi);
00842 }
00843 
00844 /* XXX shi->flippednor messes up otherwise */
00845 void shade_input_set_vertex_normals(ShadeInput *shi)
00846 {
00847         float u= shi->u, v= shi->v;
00848         float l= 1.0f+u+v;
00849         
00850         /* calculate vertexnormals */
00851         if(shi->vlr->flag & R_SMOOTH) {
00852                 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
00853                 
00854                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
00855                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
00856                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
00857                 
00858                 // use unnormalized normal (closer to games)
00859                 VECCOPY(shi->nmapnorm, shi->vn);
00860                 
00861                 normalize_v3(shi->vn);
00862         }
00863         else
00864         {
00865                 VECCOPY(shi->vn, shi->facenor);
00866                 VECCOPY(shi->nmapnorm, shi->vn);
00867         }
00868         
00869         /* used in nodes */
00870         VECCOPY(shi->vno, shi->vn);
00871 }
00872 
00873 
00874 /* use by raytrace, sss, bake to flip into the right direction */
00875 void shade_input_flip_normals(ShadeInput *shi)
00876 {
00877         shi->facenor[0]= -shi->facenor[0];
00878         shi->facenor[1]= -shi->facenor[1];
00879         shi->facenor[2]= -shi->facenor[2];
00880 
00881         shi->vn[0]= -shi->vn[0];
00882         shi->vn[1]= -shi->vn[1];
00883         shi->vn[2]= -shi->vn[2];
00884 
00885         shi->vno[0]= -shi->vno[0];
00886         shi->vno[1]= -shi->vno[1];
00887         shi->vno[2]= -shi->vno[2];
00888 
00889         shi->nmapnorm[0] = -shi->nmapnorm[0];
00890         shi->nmapnorm[1] = -shi->nmapnorm[1];
00891         shi->nmapnorm[2] = -shi->nmapnorm[2];
00892 
00893         shi->flippednor= !shi->flippednor;
00894 }
00895 
00896 void shade_input_set_shade_texco(ShadeInput *shi)
00897 {
00898         ObjectInstanceRen *obi= shi->obi;
00899         ObjectRen *obr= shi->obr;
00900         VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
00901         float u= shi->u, v= shi->v;
00902         float l= 1.0f+u+v, dl;
00903         int mode= shi->mode;            /* or-ed result for all nodes */
00904         short texco= shi->mat->texco;
00905 
00906         /* calculate dxno */
00907         if(shi->vlr->flag & R_SMOOTH) {
00908                 
00909                 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
00910                         float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
00911                         
00912                         dl= shi->dx_u+shi->dx_v;
00913                         shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
00914                         shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
00915                         shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
00916                         dl= shi->dy_u+shi->dy_v;
00917                         shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
00918                         shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
00919                         shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
00920                         
00921                 }
00922         }
00923 
00924         /* calc tangents */
00925         if (mode & (MA_TANGENT_V|MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
00926                 float *tangent, *s1, *s2, *s3;
00927                 float tl, tu, tv;
00928 
00929                 if(shi->vlr->flag & R_SMOOTH) {
00930                         tl= l;
00931                         tu= u;
00932                         tv= v;
00933                 }
00934                 else {
00935                         /* qdn: flat faces have tangents too,
00936                            could pick either one, using average here */
00937                         tl= 1.0f/3.0f;
00938                         tu= -1.0f/3.0f;
00939                         tv= -1.0f/3.0f;
00940                 }
00941 
00942                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
00943                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
00944 
00945                 if(mode & MA_TANGENT_V) {
00946                         s1 = RE_vertren_get_tangent(obr, v1, 0);
00947                         s2 = RE_vertren_get_tangent(obr, v2, 0);
00948                         s3 = RE_vertren_get_tangent(obr, v3, 0);
00949 
00950                         if(s1 && s2 && s3) {
00951                                 shi->tang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
00952                                 shi->tang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
00953                                 shi->tang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
00954 
00955                                 if(obi->flag & R_TRANSFORMED)
00956                                         mul_m3_v3(obi->nmat, shi->tang);
00957 
00958                                 normalize_v3(shi->tang);
00959                                 VECCOPY(shi->nmaptang, shi->tang);
00960                         }
00961                 }
00962 
00963                 if(mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
00964                         tangent= RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
00965 
00966                         if(tangent) {
00967                                 int j1= shi->i1, j2= shi->i2, j3= shi->i3;
00968                                 float c0[3], c1[3], c2[3];
00969 
00970                                 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
00971 
00972                                 VECCOPY(c0, &tangent[j1*4]);
00973                                 VECCOPY(c1, &tangent[j2*4]);
00974                                 VECCOPY(c2, &tangent[j3*4]);
00975 
00976                                 // keeping tangents normalized at vertex level
00977                                 // corresponds better to how it's done in game engines
00978                                 if(obi->flag & R_TRANSFORMED)
00979                                 {
00980                                         mul_mat3_m4_v3(obi->mat, c0); normalize_v3(c0);
00981                                         mul_mat3_m4_v3(obi->mat, c1); normalize_v3(c1);
00982                                         mul_mat3_m4_v3(obi->mat, c2); normalize_v3(c2);
00983                                 }
00984                                 
00985                                 // we don't normalize the interpolated TBN tangent
00986                                 // corresponds better to how it's done in game engines
00987                                 shi->nmaptang[0]= (tl*c2[0] - tu*c0[0] - tv*c1[0]);
00988                                 shi->nmaptang[1]= (tl*c2[1] - tu*c0[1] - tv*c1[1]);
00989                                 shi->nmaptang[2]= (tl*c2[2] - tu*c0[2] - tv*c1[2]);
00990 
00991                                 // the sign is the same for all 3 vertices of any
00992                                 // non degenerate triangle.
00993                                 shi->nmaptang[3]= tangent[j1*4+3];
00994                         }
00995                 }
00996         }
00997 
00998         if(mode & MA_STR_SURFDIFF) {
00999                 float *surfnor= RE_vlakren_get_surfnor(obr, shi->vlr, 0);
01000 
01001                 if(surfnor) {
01002                         VECCOPY(shi->surfnor, surfnor)
01003                         if(obi->flag & R_TRANSFORMED)
01004                                 mul_m3_v3(obi->nmat, shi->surfnor);
01005                 }
01006                 else
01007                         VECCOPY(shi->surfnor, shi->vn)
01008 
01009                 shi->surfdist= 0.0f;
01010         }
01011         
01012         if(R.r.mode & R_SPEED) {
01013                 float *s1, *s2, *s3;
01014                 
01015                 s1= RE_vertren_get_winspeed(obi, v1, 0);
01016                 s2= RE_vertren_get_winspeed(obi, v2, 0);
01017                 s3= RE_vertren_get_winspeed(obi, v3, 0);
01018                 if(s1 && s2 && s3) {
01019                         shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
01020                         shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
01021                         shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
01022                         shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
01023                 }
01024                 else {
01025                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
01026                 }
01027         }
01028 
01029         /* pass option forces UV calc */
01030         if(shi->passflag & SCE_PASS_UV)
01031                 texco |= (NEED_UV|TEXCO_UV);
01032         
01033         /* texture coordinates. shi->dxuv shi->dyuv have been set */
01034         if(texco & NEED_UV) {
01035                 
01036                 if(texco & TEXCO_ORCO) {
01037                         if(v1->orco) {
01038                                 float *o1, *o2, *o3;
01039                                 
01040                                 o1= v1->orco;
01041                                 o2= v2->orco;
01042                                 o3= v3->orco;
01043                                 
01044                                 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
01045                                 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
01046                                 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
01047                                 
01048                                 if(shi->osatex) {
01049                                         dl= shi->dx_u+shi->dx_v;
01050                                         shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
01051                                         shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
01052                                         shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
01053                                         dl= shi->dy_u+shi->dy_v;
01054                                         shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
01055                                         shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
01056                                         shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
01057                                 }
01058                         }
01059 
01060                         VECCOPY(shi->duplilo, obi->dupliorco);
01061                 }
01062                 
01063                 if(texco & TEXCO_GLOB) {
01064                         VECCOPY(shi->gl, shi->co);
01065                         mul_m4_v3(R.viewinv, shi->gl);
01066                         if(shi->osatex) {
01067                                 VECCOPY(shi->dxgl, shi->dxco); 
01068                                 mul_mat3_m4_v3(R.viewinv, shi->dxgl); 
01069                                 VECCOPY(shi->dygl, shi->dyco); 
01070                                 mul_mat3_m4_v3(R.viewinv, shi->dygl);
01071                         }
01072                 }
01073                 
01074                 if(texco & TEXCO_STRAND) {
01075                         shi->strandco= (l*v3->accum - u*v1->accum - v*v2->accum);
01076                         if(shi->osatex) {
01077                                 dl= shi->dx_u+shi->dx_v;
01078                                 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
01079                                 dl= shi->dy_u+shi->dy_v;
01080                                 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
01081                         }
01082                 }
01083                                 
01084                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
01085                         VlakRen *vlr= shi->vlr;
01086                         MTFace *tface;
01087                         MCol *mcol;
01088                         char *name;
01089                         int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
01090 
01091                         /* uv and vcols are not copied on split, so set them according vlr divide flag */
01092                         vlr_set_uv_indices(vlr, &j1, &j2, &j3);
01093 
01094                         shi->totuv= 0;
01095                         shi->totcol= 0;
01096                         shi->actuv= obr->actmtface;
01097                         shi->actcol= obr->actmcol;
01098 
01099                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
01100                                 for (i=0; (mcol=RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
01101                                         ShadeInputCol *scol= &shi->col[i];
01102                                         char *cp1, *cp2, *cp3;
01103                                         
01104                                         shi->totcol++;
01105                                         scol->name= name;
01106 
01107                                         cp1= (char *)(mcol+j1);
01108                                         cp2= (char *)(mcol+j2);
01109                                         cp3= (char *)(mcol+j3);
01110                                         
01111                                         scol->col[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0f;
01112                                         scol->col[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0f;
01113                                         scol->col[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0f;
01114                                 }
01115 
01116                                 if(shi->totcol) {
01117                                         shi->vcol[0]= shi->col[shi->actcol].col[0];
01118                                         shi->vcol[1]= shi->col[shi->actcol].col[1];
01119                                         shi->vcol[2]= shi->col[shi->actcol].col[2];
01120                                         shi->vcol[3]= 1.0f;
01121                                 }
01122                                 else {
01123                                         shi->vcol[0]= 0.0f;
01124                                         shi->vcol[1]= 0.0f;
01125                                         shi->vcol[2]= 0.0f;
01126                                         shi->vcol[3]= 1.0f;
01127                                 }
01128                         }
01129 
01130                         for (i=0; (tface=RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
01131                                 ShadeInputUV *suv= &shi->uv[i];
01132                                 float *uv1, *uv2, *uv3;
01133 
01134                                 shi->totuv++;
01135                                 suv->name= name;
01136                                 
01137                                 uv1= tface->uv[j1];
01138                                 uv2= tface->uv[j2];
01139                                 uv3= tface->uv[j3];
01140                                 
01141                                 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
01142                                 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
01143                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
01144 
01145                                 if(shi->osatex) {
01146                                         float duv[2];
01147                                         
01148                                         dl= shi->dx_u+shi->dx_v;
01149                                         duv[0]= shi->dx_u; 
01150                                         duv[1]= shi->dx_v;
01151                                         
01152                                         suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
01153                                         suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
01154                                         
01155                                         dl= shi->dy_u+shi->dy_v;
01156                                         duv[0]= shi->dy_u; 
01157                                         duv[1]= shi->dy_v;
01158                                         
01159                                         suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
01160                                         suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
01161                                 }
01162 
01163                                 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
01164                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
01165                                                 shi->vcol[0]= 1.0f;
01166                                                 shi->vcol[1]= 1.0f;
01167                                                 shi->vcol[2]= 1.0f;
01168                                                 shi->vcol[3]= 1.0f;
01169                                         }
01170                                         if(tface && tface->tpage)
01171                                                 render_realtime_texture(shi, tface->tpage);
01172                                 }
01173 
01174 
01175                         }
01176 
01177                         shi->dupliuv[0]= -1.0f + 2.0f*obi->dupliuv[0];
01178                         shi->dupliuv[1]= -1.0f + 2.0f*obi->dupliuv[1];
01179                         shi->dupliuv[2]= 0.0f;
01180 
01181                         if(shi->totuv == 0) {
01182                                 ShadeInputUV *suv= &shi->uv[0];
01183 
01184                                 suv->uv[0]= 2.0f*(u+.5f);
01185                                 suv->uv[1]= 2.0f*(v+.5f);
01186                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
01187                                 
01188                                 if(mode & MA_FACETEXTURE) {
01189                                         /* no tface? set at 1.0f */
01190                                         shi->vcol[0]= 1.0f;
01191                                         shi->vcol[1]= 1.0f;
01192                                         shi->vcol[2]= 1.0f;
01193                                         shi->vcol[3]= 1.0f;
01194                                 }
01195                         }
01196                 }
01197                 
01198                 if(texco & TEXCO_NORM) {
01199                         shi->orn[0]= -shi->vn[0];
01200                         shi->orn[1]= -shi->vn[1];
01201                         shi->orn[2]= -shi->vn[2];
01202                 }
01203                 
01204                 if(texco & TEXCO_STRESS) {
01205                         float *s1, *s2, *s3;
01206                         
01207                         s1= RE_vertren_get_stress(obr, v1, 0);
01208                         s2= RE_vertren_get_stress(obr, v2, 0);
01209                         s3= RE_vertren_get_stress(obr, v3, 0);
01210                         if(s1 && s2 && s3) {
01211                                 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
01212                                 if(shi->stress<1.0f) shi->stress-= 1.0f;
01213                                 else shi->stress= (shi->stress-1.0f)/shi->stress;
01214                         }
01215                         else shi->stress= 0.0f;
01216                 }
01217                 
01218                 if(texco & TEXCO_TANGENT) {
01219                         if((mode & MA_TANGENT_V)==0) {
01220                                 /* just prevent surprises */
01221                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
01222                                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
01223                         }
01224                 }
01225         }
01226         
01227         /* this only avalailable for scanline renders */
01228         if(shi->depth==0) {
01229                 float x= shi->xs;
01230                 float y= shi->ys;
01231                 
01232                 if(texco & TEXCO_WINDOW) {
01233                         shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
01234                         shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
01235                         shi->winco[2]= 0.0f;
01236                         if(shi->osatex) {
01237                                 shi->dxwin[0]= 2.0f/(float)R.winx;
01238                                 shi->dywin[1]= 2.0f/(float)R.winy;
01239                                 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
01240                                 shi->dywin[0]= shi->dywin[2]= 0.0f;
01241                         }
01242                 }
01243 
01244                 if(texco & TEXCO_STICKY) {
01245                         float *s1, *s2, *s3;
01246                         
01247                         s1= RE_vertren_get_sticky(obr, v1, 0);
01248                         s2= RE_vertren_get_sticky(obr, v2, 0);
01249                         s3= RE_vertren_get_sticky(obr, v3, 0);
01250                         
01251                         if(s1 && s2 && s3) {
01252                                 float obwinmat[4][4], winmat[4][4], ho1[4], ho2[4], ho3[4];
01253                                 float Zmulx, Zmuly;
01254                                 float hox, hoy, l, dl, u, v;
01255                                 float s00, s01, s10, s11, detsh;
01256                                 
01257                                 /* old globals, localized now */
01258                                 Zmulx=  ((float)R.winx)/2.0f; Zmuly=  ((float)R.winy)/2.0f;
01259 
01260                                 zbuf_make_winmat(&R, winmat);
01261                                 if(shi->obi->flag & R_TRANSFORMED)
01262                                         mul_m4_m4m4(obwinmat, obi->mat, winmat);
01263                                 else
01264                                         copy_m4_m4(obwinmat, winmat);
01265 
01266                                 zbuf_render_project(obwinmat, v1->co, ho1);
01267                                 zbuf_render_project(obwinmat, v2->co, ho2);
01268                                 zbuf_render_project(obwinmat, v3->co, ho3);
01269                                 
01270                                 s00= ho3[0]/ho3[3] - ho1[0]/ho1[3];
01271                                 s01= ho3[1]/ho3[3] - ho1[1]/ho1[3];
01272                                 s10= ho3[0]/ho3[3] - ho2[0]/ho2[3];
01273                                 s11= ho3[1]/ho3[3] - ho2[1]/ho2[3];
01274                                 
01275                                 detsh= s00*s11-s10*s01;
01276                                 detsh= (detsh != 0.0f)? 1.0f/detsh: 0.0f;
01277                                 s00*= detsh; s01*=detsh; 
01278                                 s10*=detsh; s11*=detsh;
01279                                 
01280                                 /* recalc u and v again */
01281                                 hox= x/Zmulx -1.0f;
01282                                 hoy= y/Zmuly -1.0f;
01283                                 u= (hox - ho3[0]/ho3[3])*s11 - (hoy - ho3[1]/ho3[3])*s10;
01284                                 v= (hoy - ho3[1]/ho3[3])*s00 - (hox - ho3[0]/ho3[3])*s01;
01285                                 l= 1.0f+u+v;
01286                                 
01287                                 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
01288                                 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
01289                                 shi->sticky[2]= 0.0f;
01290                                 
01291                                 if(shi->osatex) {
01292                                         float dxuv[2], dyuv[2];
01293                                         dxuv[0]=  s11/Zmulx;
01294                                         dxuv[1]=  - s01/Zmulx;
01295                                         dyuv[0]=  - s10/Zmuly;
01296                                         dyuv[1]=  s00/Zmuly;
01297                                         
01298                                         dl= dxuv[0] + dxuv[1];
01299                                         shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
01300                                         shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
01301                                         dl= dyuv[0] + dyuv[1];
01302                                         shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
01303                                         shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
01304                                 }
01305                         }
01306                 }
01307         } /* else {
01308          Note! For raytracing winco is not set, important because thus means all shader input's need to have their variables set to zero else in-initialized values are used
01309         */
01310         if (shi->do_manage) {
01311                 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
01312                         srgb_to_linearrgb_v3_v3(shi->vcol, shi->vcol);
01313                 }
01314         }
01315         
01316 }
01317 
01318 /* ****************** ShadeSample ************************************** */
01319 
01320 /* initialize per part, not per pixel! */
01321 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
01322 {
01323         
01324         memset(shi, 0, sizeof(ShadeInput));
01325         
01326         shi->sample= sample;
01327         shi->thread= pa->thread;
01328         shi->do_preview= (R.r.scemode & R_MATNODE_PREVIEW) != 0;
01329         shi->do_manage= (R.r.color_mgt_flag & R_COLOR_MANAGEMENT);
01330         shi->lay= rl->lay;
01331         shi->layflag= rl->layflag;
01332         shi->passflag= rl->passflag;
01333         shi->combinedflag= ~rl->pass_xor;
01334         shi->mat_override= rl->mat_override;
01335         shi->light_override= rl->light_override;
01336 //      shi->rl= rl;
01337         /* note shi.depth==0  means first hit, not raytracing */
01338         
01339 }
01340 
01341 /* initialize per part, not per pixel! */
01342 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
01343 {
01344         int a, tot;
01345         
01346         tot= R.osa==0?1:R.osa;
01347         
01348         for(a=0; a<tot; a++) {
01349                 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
01350                 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
01351         }
01352         
01353         get_sample_layers(pa, rl, ssamp->rlpp);
01354 }
01355 
01356 /* Do AO or (future) GI */
01357 void shade_samples_do_AO(ShadeSample *ssamp)
01358 {
01359         ShadeInput *shi;
01360         int sample;
01361         
01362         if(!(R.r.mode & R_SHADOW))
01363                 return;
01364         if(!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
01365                 return;
01366         
01367         if(R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) {
01368                 shi= &ssamp->shi[0];
01369 
01370                 if(((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT)))
01371                         || (shi->passflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT)))
01372                         for(sample=0, shi= ssamp->shi; sample<ssamp->tot; shi++, sample++)
01373                                 if(!(shi->mode & MA_SHLESS))
01374                                         ambient_occlusion(shi);         /* stores in shi->ao[] */
01375         }
01376 }
01377 
01378 
01379 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
01380 {
01381         ShadeInput *shi;
01382         float xs, ys;
01383         
01384         ssamp->tot= 0;
01385         
01386         for(shi= ssamp->shi; ps; ps= ps->next) {
01387                 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
01388                 
01389                 if(shi->vlr) {  /* NULL happens for env material or for 'all z' */
01390                         unsigned short curmask= ps->mask;
01391                         
01392                         /* full osa is only set for OSA renders */
01393                         if(shi->vlr->flag & R_FULL_OSA) {
01394                                 short shi_cp= 0, samp;
01395                                 
01396                                 for(samp=0; samp<R.osa; samp++) {
01397                                         if(curmask & (1<<samp)) {
01398                                                 /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
01399                                                 xs= (float)x + R.jit[samp][0] + 0.5f;
01400                                                 ys= (float)y + R.jit[samp][1] + 0.5f;
01401                                                 
01402                                                 if(shi_cp)
01403                                                         shade_input_copy_triangle(shi, shi-1);
01404                                                 
01405                                                 shi->mask= (1<<samp);
01406 //                                              shi->rl= ssamp->rlpp[samp];
01407                                                 shi->samplenr= R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
01408                                                 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
01409                                                 shade_input_set_uv(shi);
01410                                                 if(shi_cp==0)
01411                                                         shade_input_set_normals(shi);
01412                                                 else  /* XXX shi->flippednor messes up otherwise */
01413                                                         shade_input_set_vertex_normals(shi);
01414                                                 
01415                                                 shi_cp= 1;
01416                                                 shi++;
01417                                         }
01418                                 }
01419                         }
01420                         else {
01421                                 if(R.osa) {
01422                                         short b= R.samples->centmask[curmask];
01423                                         xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
01424                                         ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
01425                                 }
01426                                 else {
01427                                         xs= (float)x + 0.5f;
01428                                         ys= (float)y + 0.5f;
01429                                 }
01430 
01431                                 shi->mask= curmask;
01432                                 shi->samplenr= R.shadowsamplenr[shi->thread]++;
01433                                 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
01434                                 shade_input_set_uv(shi);
01435                                 shade_input_set_normals(shi);
01436                                 shi++;
01437                         }
01438                         
01439                         /* total sample amount, shi->sample is static set in initialize */
01440                         if(shi!=ssamp->shi)
01441                                 ssamp->tot= (shi-1)->sample + 1;
01442                 }
01443         }
01444 }
01445 
01446 /* shades samples, returns true if anything happened */
01447 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
01448 {
01449         shade_samples_fill_with_ps(ssamp, ps, x, y);
01450         
01451         if(ssamp->tot) {
01452                 ShadeInput *shi= ssamp->shi;
01453                 ShadeResult *shr= ssamp->shr;
01454                 int samp;
01455                 
01456                 /* if shadow or AO? */
01457                 shade_samples_do_AO(ssamp);
01458                 
01459                 /* if shade (all shadepinputs have same passflag) */
01460                 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB|SCE_PASS_INDEXMA)) {
01461 
01462                         for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
01463                                 shade_input_set_shade_texco(shi);
01464                                 shade_input_do_shade(shi, shr);
01465                         }
01466                 }
01467                 else if(shi->passflag & SCE_PASS_Z) {
01468                         for(samp=0; samp<ssamp->tot; samp++, shi++, shr++)
01469                                 shr->z= -shi->co[2];
01470                 }
01471                 
01472                 return 1;
01473         }
01474         return 0;
01475 }
01476