73
BKE_free_envmapdata(env);
77
if (3*dx == ibuf->x) {
79
} else if (ibuf->x == ibuf->y) {
80
env->type = ENV_PLANE;
82
printf("Incorrect envmap size\n");
88
if (env->type == ENV_CUBE) {
89
for(part=0; part<6; part++) {
90
env->cube[part]= IMB_allocImBuf(dx, dx, 24, IB_rect|IB_rectfloat, 0);
92
IMB_float_from_rect(ibuf);
94
IMB_rectcpy(env->cube[0], ibuf,
96
IMB_rectcpy(env->cube[1], ibuf,
98
IMB_rectcpy(env->cube[2], ibuf,
99
0, 0, 2*dx, 0, dx, dx);
100
IMB_rectcpy(env->cube[3], ibuf,
101
0, 0, 0, dx, dx, dx);
102
IMB_rectcpy(env->cube[4], ibuf,
103
0, 0, dx, dx, dx, dx);
104
IMB_rectcpy(env->cube[5], ibuf,
105
0, 0, 2*dx, dx, dx, dx);
108
else { /* ENV_PLANE */
109
env->cube[1]= IMB_dupImBuf(ibuf);
110
IMB_float_from_rect(env->cube[1]);
77
/* after lock we test cube[1], if set the other thread has done it fine */
78
BLI_lock_thread(LOCK_IMAGE);
79
if (env->cube[1]==NULL) {
81
BKE_free_envmapdata(env);
85
if (3*dx == ibuf->x) {
89
else if (ibuf->x == ibuf->y) {
90
env->type = ENV_PLANE;
94
printf("Incorrect envmap size\n");
100
if (env->type == ENV_CUBE) {
101
for (part=0; part<6; part++) {
102
env->cube[part]= IMB_allocImBuf(dx, dx, 24, IB_rect|IB_rectfloat);
104
IMB_float_from_rect(ibuf);
106
IMB_rectcpy(env->cube[0], ibuf,
108
IMB_rectcpy(env->cube[1], ibuf,
109
0, 0, dx, 0, dx, dx);
110
IMB_rectcpy(env->cube[2], ibuf,
111
0, 0, 2*dx, 0, dx, dx);
112
IMB_rectcpy(env->cube[3], ibuf,
113
0, 0, 0, dx, dx, dx);
114
IMB_rectcpy(env->cube[4], ibuf,
115
0, 0, dx, dx, dx, dx);
116
IMB_rectcpy(env->cube[5], ibuf,
117
0, 0, 2*dx, dx, dx, dx);
120
else { /* ENV_PLANE */
121
env->cube[1]= IMB_dupImBuf(ibuf);
122
IMB_float_from_rect(env->cube[1]);
126
BLI_unlock_thread(LOCK_IMAGE);
116
129
/* ------------------------------------------------------------------------- */
146
159
envre->lay= re->lay;
148
161
/* view stuff in env render */
150
if(env->type==ENV_PLANE)
151
envre->lens*= env->viewscale;
153
envre->clipsta= env->clipsta; /* render_scene_set_window() respects this for now */
154
envre->clipend= env->clipend;
156
RE_SetCamera(envre, env->object);
162
viewscale= (env->type == ENV_PLANE)? env->viewscale: 1.0f;
163
RE_SetEnvmapCamera(envre, env->object, viewscale, env->clipsta, env->clipend);
159
166
envre->display_draw= re->display_draw;
210
217
eul[0]= eul[1]= eul[2]= 0.0;
212
if(part==0) { /* neg z */
219
if (part==0) { /* neg z */
214
} else if(part==1) { /* pos z */
222
else if (part==1) { /* pos z */
216
} else if(part==2) { /* pos y */
225
else if (part==2) { /* pos y */
217
226
eul[0]= M_PI/2.0;
218
} else if(part==3) { /* neg x */
228
else if (part==3) { /* neg x */
219
229
eul[0]= M_PI/2.0;
220
230
eul[2]= M_PI/2.0;
221
} else if(part==4) { /* neg y */
232
else if (part==4) { /* neg y */
222
233
eul[0]= M_PI/2.0;
225
237
eul[0]= M_PI/2.0;
226
238
eul[2]= -M_PI/2.0;
254
266
copy_m3_m4(imat, mat);
257
for(obi=re->instancetable.first; obi; obi=obi->next) {
269
for (obi=re->instancetable.first; obi; obi=obi->next) {
258
270
/* append or set matrix depending on dupli */
259
if(obi->flag & R_DUPLI_TRANSFORMED) {
271
if (obi->flag & R_DUPLI_TRANSFORMED) {
260
272
copy_m4_m4(tmpmat, obi->mat);
261
mul_m4_m4m4(obi->mat, tmpmat, tmat);
273
mult_m4_m4m4(obi->mat, tmat, tmpmat);
264
276
copy_m4_m4(obi->mat, tmat);
266
278
unit_m4(obi->mat);
270
282
transpose_m3(obi->nmat);
272
284
/* indicate the renderer has to use transform matrices */
274
286
obi->flag &= ~R_ENV_TRANSFORMED;
276
288
obi->flag |= R_ENV_TRANSFORMED;
280
for(obr=re->objecttable.first; obr; obr=obr->next) {
281
for(a=0; a<obr->tothalo; a++) {
282
if((a & 255)==0) har= obr->bloha[a>>8];
292
for (obr=re->objecttable.first; obr; obr=obr->next) {
293
for (a=0; a<obr->tothalo; a++) {
294
if ((a & 255)==0) har= obr->bloha[a>>8];
285
297
mul_m4_v3(tmat, har->co);
289
for(go=re->lights.first; go; go= go->next) {
301
for (go=re->lights.first; go; go= go->next) {
290
302
lar= go->lampren;
292
304
/* removed here some horrible code of someone in NaN who tried to fix
293
prototypes... just solved by introducing a correct cmat[3][3] instead
294
of using smat. this works, check square spots in reflections (ton) */
305
* prototypes... just solved by introducing a correct cmat[3][3] instead
306
* of using smat. this works, check square spots in reflections (ton) */
295
307
copy_m3_m3(cmat, lar->imat);
296
308
mul_m3_m3m3(lar->imat, cmat, imat);
304
316
mul_m3_v3(lar->imat, lar->sh_invcampos);
305
317
lar->sh_invcampos[2]*= lar->sh_zfac;
309
321
invert_m4_m4(pmat, mat);
310
mul_m4_m4m4(smat, pmat, lar->shb->viewmat);
311
mul_m4_m4m4(lar->shb->persmat, smat, lar->shb->winmat);
322
mult_m4_m4m4(smat, lar->shb->viewmat, pmat);
323
mult_m4_m4m4(lar->shb->persmat, lar->shb->winmat, smat);
313
else mul_m4_m4m4(lar->shb->persmat, lar->shb->viewmat, lar->shb->winmat);
325
else mult_m4_m4m4(lar->shb->persmat, lar->shb->winmat, lar->shb->viewmat);
327
339
/* invert notlay, so if face is in multiple layers it will still be visible,
328
unless all 'notlay' bits match the face bits.
332
now (face & ~not) is true
340
* unless all 'notlay' bits match the face bits.
344
* now (face & ~not) is true
337
for(obr=re->objecttable.first; obr; obr=obr->next) {
338
if((obr->lay & notlay)==0) {
339
for(a=0; a<obr->totvlak; a++) {
340
if((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
349
for (obr=re->objecttable.first; obr; obr=obr->next) {
350
if ((obr->lay & notlay)==0) {
351
for (a=0; a<obr->totvlak; a++) {
352
if ((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
343
355
vlr->flag |= R_HIDDEN;
417
429
normalize_m4(orthmat);
419
431
/* need imat later for texture imat */
420
mul_m4_m4m4(mat, orthmat, re->viewmat);
432
mult_m4_m4m4(mat, re->viewmat, orthmat);
421
433
invert_m4_m4(tmat, mat);
422
434
copy_m3_m4(env->obimat, tmat);
424
for(part=0; part<6; part++) {
425
if(env->type==ENV_PLANE && part!=1)
436
for (part=0; part<6; part++) {
437
if (env->type==ENV_PLANE && part!=1)
428
440
re->display_clear(re->dch, envre->result);
436
448
copy_m4_m4(envre->viewinv, tmat);
438
450
/* we have to correct for the already rotated vertexcoords */
439
mul_m4_m4m4(tmat, oldviewinv, envre->viewmat);
451
mult_m4_m4m4(tmat, envre->viewmat, oldviewinv);
440
452
invert_m4_m4(env->imat, tmat);
442
454
env_rotate_scene(envre, tmat, 1);
454
466
env_showobjects(envre);
455
467
env_rotate_scene(envre, tmat, 0);
457
if(re->test_break(re->tbh)==0) {
469
if (re->test_break(re->tbh)==0) {
458
470
RenderLayer *rl= envre->result->layers.first;
462
ibuf= IMB_allocImBuf(envre->rectx, envre->recty, 24, IB_rect|IB_rectfloat, 0);
474
ibuf= IMB_allocImBuf(envre->rectx, envre->recty, 24, IB_rect|IB_rectfloat);
463
475
memcpy(ibuf->rect_float, rl->rectf, ibuf->channels * ibuf->x * ibuf->y * sizeof(float));
465
477
if (re->scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
468
480
/* envmap renders without alpha */
469
481
alpha= ((float *)ibuf->rect_float)+3;
470
for(y= ibuf->x*ibuf->y - 1; y>=0; y--, alpha+=4)
482
for (y= ibuf->x*ibuf->y - 1; y>=0; y--, alpha+=4)
473
485
env->cube[part]= ibuf;
476
if(re->test_break(re->tbh)) break;
488
if (re->test_break(re->tbh)) break;
480
if(re->test_break(re->tbh)) BKE_free_envmapdata(env);
492
if (re->test_break(re->tbh)) BKE_free_envmapdata(env);
482
if(envre->r.mode & R_OSA) env->ok= ENV_OSA;
494
if (envre->r.mode & R_OSA) env->ok= ENV_OSA;
483
495
else env->ok= ENV_NORMAL;
484
496
env->lastframe= re->scene->r.cfra;
507
519
re->stats_draw(re->sdh, &re->i);
509
521
/* 5 = hardcoded max recursion level */
511
tex= G.main->tex.first;
513
if(tex->id.us && tex->type==TEX_ENVMAP) {
514
if(tex->env && tex->env->object) {
523
tex= re->main->tex.first;
525
if (tex->id.us && tex->type==TEX_ENVMAP) {
526
if (tex->env && tex->env->object) {
515
527
EnvMap *env= tex->env;
517
if(env->object->lay & re->lay) {
518
if(env->stype==ENV_LOAD) {
529
if (env->object->lay & re->lay) {
530
if (env->stype==ENV_LOAD) {
519
531
float orthmat[4][4], mat[4][4], tmat[4][4];
521
533
/* precalc orthmat for object */
523
535
normalize_m4(orthmat);
525
537
/* need imat later for texture imat */
526
mul_m4_m4m4(mat, orthmat, re->viewmat);
538
mult_m4_m4m4(mat, re->viewmat, orthmat);
527
539
invert_m4_m4(tmat, mat);
528
540
copy_m3_m4(env->obimat, tmat);
532
544
/* decide if to render an envmap (again) */
533
if(env->depth >= depth) {
545
if (env->depth >= depth) {
535
547
/* set 'recalc' to make sure it does an entire loop of recalcs */
538
550
/* free when OSA, and old one isn't OSA */
539
if((re->r.mode & R_OSA) && env->ok==ENV_NORMAL)
551
if ((re->r.mode & R_OSA) && env->ok==ENV_NORMAL)
540
552
BKE_free_envmapdata(env);
541
553
/* free when size larger */
542
else if(env->lastsize < re->r.size)
554
else if (env->lastsize < re->r.size)
543
555
BKE_free_envmapdata(env);
544
556
/* free when env is in recalcmode */
557
else if (env->recalc)
546
558
BKE_free_envmapdata(env);
549
if(env->ok==0 && depth==0) env->recalc= 1;
561
if (env->ok==0 && depth==0) env->recalc= 1;
553
565
render_envmap(re, env);
555
if(depth==env->depth) env->recalc= 0;
567
if (depth==env->depth) env->recalc= 0;
584
if(env->type==ENV_PLANE) {
596
if (env->type==ENV_PLANE) {
588
600
answ[0]= env->viewscale*labda*vec[0];
589
601
answ[1]= -env->viewscale*labda*vec[1];
593
if( vec[2]<=-fabs(vec[0]) && vec[2]<=-fabs(vec[1]) ) {
605
if ( vec[2] <= -fabsf(vec[0]) && vec[2] <= -fabsf(vec[1]) ) {
596
608
answ[0]= labda*vec[0];
597
609
answ[1]= labda*vec[1];
599
else if( vec[2]>=fabs(vec[0]) && vec[2]>=fabs(vec[1]) ) {
611
else if (vec[2] >= fabsf(vec[0]) && vec[2] >= fabsf(vec[1])) {
602
614
answ[0]= labda*vec[0];
603
615
answ[1]= -labda*vec[1];
605
else if( vec[1]>=fabs(vec[0]) ) {
617
else if (vec[1] >= fabsf(vec[0])) {
608
620
answ[0]= labda*vec[0];
609
621
answ[1]= labda*vec[2];
611
else if( vec[0]<=-fabs(vec[1]) ) {
623
else if (vec[0] <= -fabsf(vec[1])) {
614
626
answ[0]= labda*vec[1];
615
627
answ[1]= labda*vec[2];
617
else if( vec[1]<=-fabs(vec[0]) ) {
629
else if (vec[1] <= -fabsf(vec[0])) {
620
632
answ[0]= -labda*vec[0];
621
633
answ[1]= labda*vec[2];
626
638
answ[0]= -labda*vec[1];
627
639
answ[1]= labda*vec[2];
631
answ[0]= 0.5+0.5*answ[0];
632
answ[1]= 0.5+0.5*answ[1];
643
answ[0]= 0.5f+0.5f*answ[0];
644
answ[1]= 0.5f+0.5f*answ[1];
672
if(env==NULL || (env->stype!=ENV_LOAD && env->object==NULL)) {
684
if (env==NULL || (env->stype!=ENV_LOAD && env->object==NULL)) {
673
685
texres->tin= 0.0;
677
if(env->stype==ENV_LOAD) {
689
if (env->stype==ENV_LOAD) {
678
690
env->ima= tex->ima;
679
if(env->ima && env->ima->ok) {
680
if(env->cube[1]==NULL) {
681
ImBuf *ibuf= BKE_image_get_ibuf(env->ima, NULL);
683
envmap_split_ima(env, ibuf);
691
if (env->ima && env->ima->ok) {
692
if (env->cube[1]==NULL) {
693
ImBuf *ibuf_ima= BKE_image_get_ibuf(env->ima, NULL);
695
envmap_split_ima(env, ibuf_ima);
691
703
texres->tin= 0.0;
695
707
/* rotate to envmap space, if object is set */
696
VECCOPY(vec, texvec);
697
if(env->object) mul_m3_v3(env->obimat, vec);
708
copy_v3_v3(vec, texvec);
709
if (env->object) mul_m3_v3(env->obimat, vec);
698
710
else mul_mat3_m4_v3(R.viewinv, vec);
700
712
face= envcube_isect(env, vec, sco);
701
713
ibuf= env->cube[face];
705
717
mul_m3_v3(env->obimat, dxt);
706
718
mul_m3_v3(env->obimat, dyt);
729
if (texres->ta<1.0f) {
718
730
TexResult texr1, texr2;
720
732
texr1.nor= texr2.nor= NULL;
733
texr1.talpha= texr2.talpha= texres->talpha; /* boxclip expects this initialized */
722
735
add_v3_v3(vec, dxt);
723
736
face1= envcube_isect(env, vec, sco);
724
737
sub_v3_v3(vec, dxt);
727
740
ibuf= env->cube[face1];
728
741
set_dxtdyt(dxts, dyts, dxt, dyt, face1);
729
742
imagewraposa(tex, NULL, ibuf, sco, dxts, dyts, &texr1);
736
749
face1= envcube_isect(env, vec, sco);
737
750
sub_v3_v3(vec, dyt);
740
753
ibuf= env->cube[face1];
741
754
set_dxtdyt(dxts, dyts, dxt, dyt, face1);
742
755
imagewraposa(tex, NULL, ibuf, sco, dxts, dyts, &texr2);