diff options
author | Ton Roosendaal <ton@blender.org> | 2004-06-30 22:54:09 +0400 |
---|---|---|
committer | Ton Roosendaal <ton@blender.org> | 2004-06-30 22:54:09 +0400 |
commit | a8ef804146751f8b5ec0f83b411f4ab7d041ed84 (patch) | |
tree | e85a6e52b41f7774cf472171ee11783324eec991 /source/blender/blenkernel | |
parent | c9b4585618ea72f61c4671d4734c9e48b6ce6745 (diff) |
NEW: Ramp shades for diffuse and specular
http://www.blender3d.org/cms/Ramp_Shaders.348.0.html
Material color and specular now can be defined by a Colorband. The actual
color then is defined during shading based on:
- shade value (like dotproduct)
- energy value (dot product plus light)
- normal
- result of all shading (useful for adding stuff in the end)
Special request from [A]ndy! :)
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_texture.h | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/displist.c | 114 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/image.c | 4 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/material.c | 11 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/texture.c | 93 |
5 files changed, 126 insertions, 98 deletions
diff --git a/source/blender/blenkernel/BKE_texture.h b/source/blender/blenkernel/BKE_texture.h index 547ac593d15..dc1856a4803 100644 --- a/source/blender/blenkernel/BKE_texture.h +++ b/source/blender/blenkernel/BKE_texture.h @@ -51,7 +51,7 @@ void open_plugin_tex(struct PluginTex *pit); struct PluginTex *add_plugin_tex(char *str); void free_plugin_tex(struct PluginTex *pit); struct ColorBand *add_colorband(void); -int do_colorband(struct ColorBand *coba); +int do_colorband(struct ColorBand *coba, float in, float out[4]); void default_tex(struct Tex *tex); struct Tex *add_texture(char *name); void default_mtex(struct MTex *mtex); diff --git a/source/blender/blenkernel/intern/displist.c b/source/blender/blenkernel/intern/displist.c index 84167694741..fcb8749fe3b 100644 --- a/source/blender/blenkernel/intern/displist.c +++ b/source/blender/blenkernel/intern/displist.c @@ -475,8 +475,9 @@ static void fastshade(float *co, float *nor, float *orco, Material *ma, char *co { ShadeInput shi; FastLamp *fl; - float i, t, inp, soft, inpr, inpg, inpb, isr=0, isg=0, isb=0, lv[3], view[3], lampdist, ld; - float inpr1, inpg1, inpb1, isr1=0, isg1=0, isb1=0; + float i, t, inp, is, soft, lv[3], lampdist, ld; + float diff1[3], diff2[3]; + float isr1=0, isg1=0, isb1=0, isr=0, isg=0, isb=0; int a, back; if(ma==0) return; @@ -485,6 +486,7 @@ static void fastshade(float *co, float *nor, float *orco, Material *ma, char *co shi.vlr= NULL; // have to do this! ma= shi.matren; shi.osatex= 0; // also prevents reading vlr + VECCOPY(shi.vn, nor); if(ma->mode & MA_VERTEXCOLP) { if(vertcol) { @@ -496,7 +498,6 @@ static void fastshade(float *co, float *nor, float *orco, Material *ma, char *co if(ma->texco) { VECCOPY(shi.lo, orco); - VECCOPY(shi.vn, nor); if(ma->texco & TEXCO_GLOB) { VECCOPY(shi.gl, shi.lo); @@ -552,19 +553,21 @@ static void fastshade(float *co, float *nor, float *orco, Material *ma, char *co } if( vertcol && (ma->mode & (MA_VERTEXCOL+MA_VERTEXCOLP))== MA_VERTEXCOL ) { - inpr= inpr1= ma->emit+vertcol[3]/255.0; - inpg= inpg1= ma->emit+vertcol[2]/255.0; - inpb= inpb1= ma->emit+vertcol[1]/255.0; + diff1[0]= diff2[0]= ma->r*(ma->emit+vertcol[3]/255.0); + diff1[1]= diff2[1]= ma->g*(ma->emit+vertcol[2]/255.0); + diff1[2]= diff2[2]= ma->b*(ma->emit+vertcol[1]/255.0); } else { - inpr= inpg= inpb= inpr1= inpg1= inpb1= ma->emit; + diff1[0]= diff2[0]= ma->r*ma->emit; + diff1[1]= diff2[1]= ma->g*ma->emit; + diff1[2]= diff2[2]= ma->b*ma->emit; } - view[0]= 0.0; - view[1]= 0.0; - view[2]= 1.0; + shi.view[0]= 0.0; + shi.view[1]= 0.0; + shi.view[2]= 1.0; - Normalise(view); + Normalise(shi.view); for (fl= fastlamplist; fl; fl= fl->next) { /* if(fl->mode & LA_LAYER) if((fl->lay & ma->lay)==0) continue; */ @@ -623,76 +626,103 @@ static void fastshade(float *co, float *nor, float *orco, Material *ma, char *co } } - if(fl->mode & LA_NO_DIFF) inp= 0.0; + if(fl->mode & LA_NO_DIFF) is= 0.0; else { - inp= nor[0]*lv[0]+ nor[1]*lv[1]+ nor[2]*lv[2]; + is= nor[0]*lv[0]+ nor[1]*lv[1]+ nor[2]*lv[2]; - if(ma->diff_shader==MA_DIFF_ORENNAYAR) inp= OrenNayar_Diff(nor, lv, view, ma->roughness); - else if(ma->diff_shader==MA_DIFF_TOON) inp= Toon_Diff(nor, lv, view, ma->param[0], ma->param[1]); + if(ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(nor, lv, shi.view, ma->roughness); + else if(ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(nor, lv, shi.view, ma->param[0], ma->param[1]); } back= 0; - if(inp<0.0) { + if(is<0.0) { back= 1; - inp= -inp; + is= -is; } - inp*= lampdist*ma->ref; + inp= is*lampdist*ma->ref; if(back==0) { - inpr+= inp*fl->r; - inpg+= inp*fl->g; - inpb+= inp*fl->b; + add_to_diffuse(diff1, &shi, is, inp*fl->r, inp*fl->g, inp*fl->b); + //diff1[0]+= inp*fl->r; + //diff1[1]+= inp*fl->g; + //diff1[2]+= inp*fl->b; } else if(col2) { - inpr1+= inp*fl->r; - inpg1+= inp*fl->g; - inpb1+= inp*fl->b; + add_to_diffuse(diff2, &shi, is, inp*fl->r, inp*fl->g, inp*fl->b); + //diff2[0]+= inp*fl->r; + //diff2[1]+= inp*fl->g; + //diff2[2]+= inp*fl->b; } if(ma->spec && (fl->mode & LA_NO_SPEC)==0) { + float specfac; if(ma->spec_shader==MA_SPEC_PHONG) - t= Phong_Spec(nor, lv, view, ma->har); + specfac= Phong_Spec(nor, lv, shi.view, ma->har); else if(ma->spec_shader==MA_SPEC_COOKTORR) - t= CookTorr_Spec(nor, lv, view, ma->har); + specfac= CookTorr_Spec(nor, lv, shi.view, ma->har); else if(ma->spec_shader==MA_SPEC_BLINN) - t= Blinn_Spec(nor, lv, view, ma->refrac, (float)ma->har); + specfac= Blinn_Spec(nor, lv, shi.view, ma->refrac, (float)ma->har); else - t= Toon_Spec(nor, lv, view, ma->param[2], ma->param[3]); + specfac= Toon_Spec(nor, lv, shi.view, ma->param[2], ma->param[3]); - if(t>0) { - t*= ma->spec*lampdist; + if(specfac>0) { + t= specfac*ma->spec*lampdist; if(back==0) { - isr+= t*(fl->r * ma->specr); - isg+= t*(fl->g * ma->specg); - isb+= t*(fl->b * ma->specb); + if(ma->mode & MA_RAMP_SPEC) { + float spec[3]; + do_specular_ramp(&shi, specfac, t, spec); + isr+= t*(fl->r * spec[0]); + isg+= t*(fl->g * spec[1]); + isb+= t*(fl->b * spec[2]); + } + else { + isr+= t*(fl->r * ma->specr); + isg+= t*(fl->g * ma->specg); + isb+= t*(fl->b * ma->specb); + } } else if(col2) { - isr1+= t*(fl->r * ma->specr); - isg1+= t*(fl->g * ma->specg); - isb1+= t*(fl->b * ma->specb); + if(ma->mode & MA_RAMP_SPEC) { + float spec[3]; + do_specular_ramp(&shi, specfac, t, spec); + isr1+= t*(fl->r * spec[0]); + isg1+= t*(fl->g * spec[1]); + isb1+= t*(fl->b * spec[2]); + } + else { + isr1+= t*(fl->r * ma->specr); + isg1+= t*(fl->g * ma->specg); + isb1+= t*(fl->b * ma->specb); + } } } } } - a= 256*(inpr*ma->r + ma->ambr +isr); + if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(diff1, &shi); + if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(&isr, &isg, &isb, &shi); + + a= 256*(diff1[0] + ma->ambr +isr); if(a>255) col1[3]= 255; else col1[3]= a; - a= 256*(inpg*ma->g + ma->ambg +isg); + a= 256*(diff1[1] + ma->ambg +isg); if(a>255) col1[2]= 255; else col1[2]= a; - a= 256*(inpb*ma->b + ma->ambb +isb); + a= 256*(diff1[2] + ma->ambb +isb); if(a>255) col1[1]= 255; else col1[1]= a; if(col2) { - a= 256*(inpr1*ma->r + ma->ambr +isr1); + if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(diff2, &shi); + if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(&isr1, &isg1, &isb1, &shi); + + a= 256*(diff2[0] + ma->ambr +isr1); if(a>255) col2[3]= 255; else col2[3]= a; - a= 256*(inpr1*ma->g + ma->ambg +isg1); + a= 256*(diff2[1] + ma->ambg +isg1); if(a>255) col2[2]= 255; else col2[2]= a; - a= 256*(inpr1*ma->b + ma->ambb +isb1); + a= 256*(diff2[2] + ma->ambb +isb1); if(a>255) col2[1]= 255; else col2[1]= a; } diff --git a/source/blender/blenkernel/intern/image.c b/source/blender/blenkernel/intern/image.c index d3f0d3b72af..f903641f347 100644 --- a/source/blender/blenkernel/intern/image.c +++ b/source/blender/blenkernel/intern/image.c @@ -729,8 +729,6 @@ int imagewrap(Tex *tex, float *texvec) } } - if(tex->flag & TEX_COLORBAND) do_colorband(tex->coba); - if(tex->nor) return 3; else return 1; } @@ -1557,8 +1555,6 @@ int imagewraposa(Tex *tex, float *texvec, float *dxt, float *dyt) return 0; } - if(tex->flag & TEX_COLORBAND) do_colorband(tex->coba); - if(tex->nor) return 3; else return 1; } diff --git a/source/blender/blenkernel/intern/material.c b/source/blender/blenkernel/intern/material.c index 5d52e4750a1..a8219d560cf 100644 --- a/source/blender/blenkernel/intern/material.c +++ b/source/blender/blenkernel/intern/material.c @@ -71,13 +71,16 @@ void free_material(Material *ma) BPY_free_scriptlink(&ma->scriptlink); if(ma->ren) MEM_freeN(ma->ren); - ma->ren= 0; + ma->ren= NULL; for(a=0; a<8; a++) { mtex= ma->mtex[a]; if(mtex && mtex->tex) mtex->tex->id.us--; if(mtex) MEM_freeN(mtex); } + + if(ma->ramp_col) MEM_freeN(ma->ramp_col); + if(ma->ramp_spec) MEM_freeN(ma->ramp_spec); } void init_material(Material *ma) @@ -113,6 +116,10 @@ void init_material(Material *ma) ma->fresnel_tra_i= 1.25; ma->fresnel_mir_i= 1.25; + ma->rampfac_col= 1.0; + ma->rampfac_spec= 1.0; + ma->pr_lamp= 3; // two lamps, is bits + ma->mode= MA_TRACEBLE+MA_SHADOW+MA_RADIO; } @@ -145,6 +152,8 @@ Material *copy_material(Material *ma) } BPY_copy_scriptlink(&ma->scriptlink); + if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col); + if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec); return man; } diff --git a/source/blender/blenkernel/intern/texture.c b/source/blender/blenkernel/intern/texture.c index 44e16684f15..2507ca5c2b8 100644 --- a/source/blender/blenkernel/intern/texture.c +++ b/source/blender/blenkernel/intern/texture.c @@ -238,10 +238,9 @@ ColorBand *add_colorband() /* ------------------------------------------------------------------------- */ -int do_colorband(ColorBand *coba) +int do_colorband(ColorBand *coba, float in, float out[4]) { /* These vars form the texture channel, in render/intern/texture.c */ - extern float Tin, Tr, Tg, Tb, Ta; extern int Talpha; CBData *cbd1, *cbd2, *cbd0, *cbd3; float fac, mfac, t[4]; @@ -251,61 +250,55 @@ int do_colorband(ColorBand *coba) Talpha= 1; cbd1= coba->data; - - if(Tin <= cbd1->pos) { /* ultimate left */ - Tr= cbd1->r; - Tg= cbd1->g; - Tb= cbd1->b; - Ta= cbd1->a; + if(coba->tot==1) { + out[0]= cbd1->r; + out[1]= cbd1->g; + out[2]= cbd1->b; + out[3]= cbd1->a; } else { - /* we're looking for first pos > Tin */ - - for(a=0; a<coba->tot; a++, cbd1++) if(cbd1->pos >= Tin) break; + if(in < cbd1->pos) in= cbd1->pos; + + /* we're looking for first pos > in */ + for(a=0; a<coba->tot; a++, cbd1++) if(cbd1->pos > in) break; + + if(a==coba->tot) cbd1--; + if(in > cbd1->pos) in= cbd1->pos; + + cbd2= cbd1-1; + fac= (in-cbd1->pos)/(cbd2->pos-cbd1->pos); + + if(coba->ipotype==2) { + /* ipo from right to left: 3 2 1 0 */ - if(a==coba->tot) { /* ultimate right */ - cbd1--; - Tr= cbd1->r; - Tg= cbd1->g; - Tb= cbd1->b; - Ta= cbd1->a; + if(a>=coba->tot-1) cbd0= cbd1; + else cbd0= cbd1+1; + if(a<2) cbd3= cbd2; + else cbd3= cbd2-1; + + set_four_ipo(fac, t, KEY_BSPLINE); + + out[0]= t[3]*cbd3->r +t[2]*cbd2->r +t[1]*cbd1->r +t[0]*cbd0->r; + out[1]= t[3]*cbd3->g +t[2]*cbd2->g +t[1]*cbd1->g +t[0]*cbd0->g; + out[2]= t[3]*cbd3->b +t[2]*cbd2->b +t[1]*cbd1->b +t[0]*cbd0->b; + out[3]= t[3]*cbd3->a +t[2]*cbd2->a +t[1]*cbd1->a +t[0]*cbd0->a; + CLAMP(out[0], 0.0, 1.0); + CLAMP(out[1], 0.0, 1.0); + CLAMP(out[2], 0.0, 1.0); + CLAMP(out[3], 0.0, 1.0); } else { - cbd2= cbd1-1; - fac= (Tin-cbd1->pos)/(cbd2->pos-cbd1->pos); - - if(coba->ipotype==2) { - /* ipo from right to left: 3 2 1 0 */ - - if(a>=coba->tot-1) cbd0= cbd1; - else cbd0= cbd1+1; - if(a<2) cbd3= cbd2; - else cbd3= cbd2-1; - - set_four_ipo(fac, t, KEY_BSPLINE); - - Tr= t[3]*cbd3->r +t[2]*cbd2->r +t[1]*cbd1->r +t[0]*cbd0->r; - Tg= t[3]*cbd3->g +t[2]*cbd2->g +t[1]*cbd1->g +t[0]*cbd0->g; - Tb= t[3]*cbd3->b +t[2]*cbd2->b +t[1]*cbd1->b +t[0]*cbd0->b; - Ta= t[3]*cbd3->a +t[2]*cbd2->a +t[1]*cbd1->a +t[0]*cbd0->a; - CLAMP(Tr, 0.0, 1.0); - CLAMP(Tg, 0.0, 1.0); - CLAMP(Tb, 0.0, 1.0); - CLAMP(Ta, 0.0, 1.0); + + if(coba->ipotype==1) { /* EASE */ + mfac= fac*fac; + fac= 3.0f*mfac-2.0f*mfac*fac; } - else { + mfac= 1.0f-fac; - if(coba->ipotype==1) { /* EASE */ - mfac= fac*fac; - fac= 3.0f*mfac-2.0f*mfac*fac; - } - mfac= 1.0f-fac; - - Tr= mfac*cbd1->r + fac*cbd2->r; - Tg= mfac*cbd1->g + fac*cbd2->g; - Tb= mfac*cbd1->b + fac*cbd2->b; - Ta= mfac*cbd1->a + fac*cbd2->a; - } + out[0]= mfac*cbd1->r + fac*cbd2->r; + out[1]= mfac*cbd1->g + fac*cbd2->g; + out[2]= mfac*cbd1->b + fac*cbd2->b; + out[3]= mfac*cbd1->a + fac*cbd2->a; } } return 1; /* OK */ |