fix for objectspace bumpmapping
This commit is contained in:
M.G. Kishalmi 2011-01-30 16:24:23 +00:00
parent 658f86b016
commit d5da54e1e6
4 changed files with 816 additions and 849 deletions

@ -630,8 +630,7 @@ void default_mtex(MTex *mtex)
mtex->size[1]= 1.0; mtex->size[1]= 1.0;
mtex->size[2]= 1.0; mtex->size[2]= 1.0;
mtex->tex= 0; mtex->tex= 0;
mtex->texflag= MTEX_3TAP_BUMP; mtex->texflag= MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE;
mtex->texflag= MTEX_BUMP_OBJECTSPACE;
mtex->colormodel= 0; mtex->colormodel= 0;
mtex->r= 1.0; mtex->r= 1.0;
mtex->g= 0.0; mtex->g= 0.0;

@ -40,8 +40,6 @@
#include "DNA_scene_types.h" #include "DNA_scene_types.h"
#include "DNA_world_types.h" #include "DNA_world_types.h"
#include "IMB_imbuf_types.h"
#include "BLI_math.h" #include "BLI_math.h"
#include "BLI_blenlib.h" #include "BLI_blenlib.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
@ -56,6 +54,7 @@
#include "BKE_scene.h" #include "BKE_scene.h"
#include "BKE_texture.h" #include "BKE_texture.h"
#include "IMB_imbuf_types.h"
#include "GPU_extensions.h" #include "GPU_extensions.h"
#include "GPU_material.h" #include "GPU_material.h"
@ -1041,6 +1040,19 @@ static void do_material_tex(GPUShadeInput *shi)
newnor = tnor; newnor = tnor;
norfac = MIN2(fabsf(mtex->norfac), 1.0); norfac = MIN2(fabsf(mtex->norfac), 1.0);
if(norfac == 1.0f && !GPU_link_changed(stencil)) {
shi->vn = newnor;
}
else {
tnorfac = GPU_uniform(&norfac);
if(GPU_link_changed(stencil))
GPU_link(mat, "math_multiply", tnorfac, stencil, &tnorfac);
GPU_link(mat, "mtex_blend_normal", tnorfac, shi->vn, newnor, &shi->vn);
}
} else if( mtex->texflag & (MTEX_3TAP_BUMP|MTEX_5TAP_BUMP)) { } else if( mtex->texflag & (MTEX_3TAP_BUMP|MTEX_5TAP_BUMP)) {
/* ntap bumpmap image */ /* ntap bumpmap image */
float hScale = 0.1f; // compatibility adjustment factor for all bumpspace types float hScale = 0.1f; // compatibility adjustment factor for all bumpspace types
@ -1050,6 +1062,12 @@ static void do_material_tex(GPUShadeInput *shi)
GPUNodeLink *vR1, *vR2, *fDet; GPUNodeLink *vR1, *vR2, *fDet;
GPUNodeLink *dBs, *dBt, *vN; GPUNodeLink *dBs, *dBt, *vN;
norfac = hScale * mtex->norfac;
tnorfac = GPU_uniform(&norfac);
if(GPU_link_changed(stencil))
GPU_link(mat, "math_multiply", tnorfac, stencil, &tnorfac);
if( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) if( mtex->texflag & MTEX_BUMP_OBJECTSPACE )
GPU_link( mat, "mtex_bump_init_objspace", GPU_link( mat, "mtex_bump_init_objspace",
surf_pos, shi->vn, surf_pos, shi->vn,
@ -1062,52 +1080,27 @@ static void do_material_tex(GPUShadeInput *shi)
if( mtex->texflag & MTEX_3TAP_BUMP ) if( mtex->texflag & MTEX_3TAP_BUMP )
GPU_link( mat, "mtex_bump_tap3", GPU_link( mat, "mtex_bump_tap3",
texco, GPU_image(tex->ima, &tex->iuser), GPU_uniform(&hScale), texco, GPU_image(tex->ima, &tex->iuser), tnorfac,
&dBs, &dBt ); &dBs, &dBt );
else else
GPU_link( mat, "mtex_bump_tap5", GPU_link( mat, "mtex_bump_tap5",
texco, GPU_image(tex->ima, &tex->iuser), GPU_uniform(&hScale), texco, GPU_image(tex->ima, &tex->iuser), tnorfac,
&dBs, &dBt ); &dBs, &dBt );
if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) { if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
float ima_x= 512.0f, ima_y= 512.f; // prevent calling textureSize, glsl 1.3 only float ima_x= 512.0f, ima_y= 512.f; // prevent calling textureSize, glsl 1.3 only
ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, &tex->iuser); ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, &tex->iuser);
if(ibuf)
if(ibuf) {
ima_x= ibuf->x; ima_y= ibuf->y; ima_x= ibuf->x; ima_y= ibuf->y;
}
GPU_link( mat, "mtex_bump_apply_texspace", GPU_link( mat, "mtex_bump_apply_texspace",
fDet, dBs, dBt, vR1, vR2, vN, GPU_image(tex->ima, &tex->iuser), texco, GPU_uniform(&fScaleTex), fDet, dBs, dBt, vR1, vR2, vN, GPU_image(tex->ima, &tex->iuser), texco, GPU_uniform(&fScaleTex),
GPU_uniform(&ima_x), GPU_uniform(&ima_y), &tnor ); GPU_uniform(&ima_x), GPU_uniform(&ima_y), &shi->vn );
} } else
else if( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) GPU_link( mat, "mtex_bump_apply",
GPU_link( mat, "mtex_bump_apply_objspace",
fDet, dBs, dBt, vR1, vR2, vN, GPU_builtin(GPU_INVERSE_VIEW_MATRIX), GPU_builtin(GPU_INVERSE_OBJECT_MATRIX),
&tnor, &vR1, &vR2, &vN );
else
GPU_link( mat, "mtex_bump_apply_viewspace",
fDet, dBs, dBt, vR1, vR2, vN, fDet, dBs, dBt, vR1, vR2, vN,
&tnor ); &shi->vn );
newnor = tnor;
norfac = mtex->norfac;
} else {
/* original or compatible bump - don't have shaders */
newnor = shi->vn;
norfac = mtex->norfac;
}
if(norfac == 1.0f && !GPU_link_changed(stencil)) {
shi->vn = newnor;
}
else {
tnorfac = GPU_uniform(&norfac);
if(GPU_link_changed(stencil))
GPU_link(mat, "math_multiply", tnorfac, stencil, &tnorfac);
GPU_link(mat, "mtex_blend_normal", tnorfac, shi->vn, newnor, &shi->vn);
} }
} }

@ -1185,24 +1185,13 @@ void mtex_bump_tap5( vec3 texco, sampler2D ima, float hScale,
dBt = hScale * (Hu - Hd); dBt = hScale * (Hu - Hd);
} }
void mtex_bump_apply_viewspace( float fDet, float dBs, float dBt, vec3 vR1, vec3 vR2, vec3 vN, void mtex_bump_apply( float fDet, float dBs, float dBt, vec3 vR1, vec3 vR2, vec3 vN,
out vec3 perturbed_norm ) out vec3 perturbed_norm )
{ {
vec3 vSurfGrad = sign(fDet) * ( dBs * vR1 + dBt * vR2 ); vec3 vSurfGrad = sign(fDet) * ( dBs * vR1 + dBt * vR2 );
perturbed_norm = normalize( abs(fDet) * vN - vSurfGrad ); perturbed_norm = normalize( abs(fDet) * vN - vSurfGrad );
} }
void mtex_bump_apply_objspace( float fDet, float dBs, float dBt, vec3 vR1in, vec3 vR2in, vec3 vNin,
mat4 mViewInv, mat4 mObjInv,
out vec3 perturbed_norm, out vec3 vR1, out vec3 vR2, out vec3 vN )
{
vec3 vSurfGrad = sign(fDet) * ( dBs * vR1in + dBt * vR2in );
perturbed_norm = normalize( abs(fDet) * vNin - vSurfGrad );
vR1= vR1in;
vR2= vR2in;
vN= vNin;
}
void mtex_bump_apply_texspace( float fDet, float dBs, float dBt, vec3 vR1, vec3 vR2, vec3 vN, void mtex_bump_apply_texspace( float fDet, float dBs, float dBt, vec3 vR1, vec3 vR2, vec3 vN,
sampler2D ima, vec3 texco, float scale, float ima_x, float ima_y, out vec3 perturbed_norm ) sampler2D ima, vec3 texco, float scale, float ima_x, float ima_y, out vec3 perturbed_norm )

File diff suppressed because it is too large Load Diff