New Paste :: Recent Pastes:: No Line Numbers
A Paste by Anonymous
1
void texture::composenormalmap(texture * input,float scale) { unsigned int i; unsigned int x,y; float nx,ny,nz; // normal values float len; float dneg,dpos,val_xy,val_last,val_next; for (i=0; i<size_sqr; i++) { x = (i % size); y = (i / size); // calculate dx val_xy =input->hmap [getpixeladress(TC_HMAP,x ,y)]; val_next=input->hmap [getpixeladress(TC_HMAP,x+1,y)]; val_last=input->hmap [getpixeladress(TC_HMAP,x-1,y)]; // calculate difference along x axis in both directions relative to the current pixel at (x,y) dneg = val_xy - val_last; dpos = val_next - val_xy; nx=scale*(-(dpos+dneg)/2); // calculate dy val_xy =input->hmap [getpixeladress(TC_HMAP,x,y )]; val_next=input->hmap [getpixeladress(TC_HMAP,x,y+1)]; val_last=input->hmap [getpixeladress(TC_HMAP,x,y-1)]; // calculate difference along y axis in both directions relative to the current pixel at (x,y) dneg = val_xy - val_last; dpos = val_next - val_xy; ny= scale*((dpos+dneg)/2); nz=0.12f; // calculate normalized normal vector & convert it from -1.0f..1.0f range to 0.0f..1.0f range len = (float) (1/sqrt(nx*nx + ny*ny + nz*nz)); nx*=len; ny*=len; nz*=len; nx+=1.0f; ny+=1.0f; nz+=1.0f; nx/=2.0f; ny/=2.0f; nz/=2.0f; colarr [getpixeladress(TC_RGB,x,y)+0] = clampval(nx); colarr [getpixeladress(TC_RGB,x,y)+1] = clampval(ny); colarr [getpixeladress(TC_RGB,x,y)+2] = clampval(nz); } }