Committing Konrads GLSL preview of bumpmapping, now we no longer have the bizarre situation of being able to view the changes of the normal map but not of regular bump mapping
This commit is contained in:
		@@ -48,6 +48,7 @@
 | 
			
		||||
#include "BKE_colortools.h"
 | 
			
		||||
#include "BKE_DerivedMesh.h"
 | 
			
		||||
#include "BKE_global.h"
 | 
			
		||||
#include "BKE_image.h"
 | 
			
		||||
#include "BKE_main.h"
 | 
			
		||||
#include "BKE_node.h"
 | 
			
		||||
#include "BKE_scene.h"
 | 
			
		||||
@@ -60,6 +61,9 @@
 | 
			
		||||
#include "GPU_extensions.h"
 | 
			
		||||
#include "GPU_material.h"
 | 
			
		||||
 | 
			
		||||
#include "IMB_imbuf.h"
 | 
			
		||||
#include "IMB_imbuf_types.h"
 | 
			
		||||
 | 
			
		||||
#include "gpu_codegen.h"
 | 
			
		||||
 | 
			
		||||
#include <string.h>
 | 
			
		||||
@@ -893,8 +897,10 @@ static void do_material_tex(GPUShadeInput *shi)
 | 
			
		||||
	GPUNodeLink *texco_global, *texco_uv = NULL;
 | 
			
		||||
	GPUNodeLink *newnor, *orn;
 | 
			
		||||
	char *lastuvname = NULL;
 | 
			
		||||
	float one = 1.0f, norfac, ofs[3];
 | 
			
		||||
	float one = 1.0f, norfac, ofs[3], texsize[2];
 | 
			
		||||
	int tex_nr, rgbnor, talpha;
 | 
			
		||||
	void *lock;
 | 
			
		||||
	ImBuf *ibuf;
 | 
			
		||||
 | 
			
		||||
	GPU_link(mat, "set_value", GPU_uniform(&one), &stencil);
 | 
			
		||||
 | 
			
		||||
@@ -960,7 +966,46 @@ static void do_material_tex(GPUShadeInput *shi)
 | 
			
		||||
			rgbnor = 0;
 | 
			
		||||
 | 
			
		||||
			if(tex && tex->type == TEX_IMAGE && tex->ima) {
 | 
			
		||||
				GPU_link(mat, "mtex_image", texco, GPU_image(tex->ima, &tex->iuser), &tin, &trgb, &tnor);
 | 
			
		||||
				ibuf= BKE_image_acquire_ibuf(tex->ima, NULL, &lock);
 | 
			
		||||
				if (ibuf) {
 | 
			
		||||
					texsize[0] = ibuf->x;
 | 
			
		||||
					texsize[1] = ibuf->y;
 | 
			
		||||
				}
 | 
			
		||||
				else 
 | 
			
		||||
				{
 | 
			
		||||
					texsize[0] = 0;
 | 
			
		||||
					texsize[1] = 0;
 | 
			
		||||
				}
 | 
			
		||||
				BKE_image_release_ibuf(tex->ima, lock);
 | 
			
		||||
				if(mtex->mapto & MAP_NORM && (tex->imaflag & TEX_NORMALMAP)==0) {
 | 
			
		||||
					GPU_link(mat, "mtex_height_to_normal", texco, GPU_image(tex->ima, &tex->iuser), GPU_uniform(texsize), &tin, &trgb, &tnor);					
 | 
			
		||||
			
 | 
			
		||||
					if(mtex->norfac < 0.0f)
 | 
			
		||||
						GPU_link(mat, "mtex_negate_texnormal", tnor, &tnor);
 | 
			
		||||
 | 
			
		||||
					if(mtex->normapspace == MTEX_NSPACE_TANGENT)
 | 
			
		||||
						GPU_link(mat, "mtex_nspace_tangent", GPU_attribute(CD_TANGENT, ""), shi->vn, tnor, &newnor);
 | 
			
		||||
					else
 | 
			
		||||
						newnor = tnor;		
 | 
			
		||||
 | 
			
		||||
					/* norfac = MIN2(fabsf(mtex->norfac), 1.0); */
 | 
			
		||||
					norfac = fabsf(mtex->norfac); /* To not limit bumps to [-1, 1]. */
 | 
			
		||||
					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 {
 | 
			
		||||
					GPU_link(mat, "mtex_image", texco, GPU_image(tex->ima, &tex->iuser), &tin, &trgb, &tnor);					
 | 
			
		||||
				}
 | 
			
		||||
				rgbnor= TEX_RGB;
 | 
			
		||||
 | 
			
		||||
				if(tex->imaflag & TEX_USEALPHA)
 | 
			
		||||
 
 | 
			
		||||
@@ -1098,6 +1098,37 @@ void mtex_image(vec3 vec, sampler2D ima, out float value, out vec4 color, out ve
 | 
			
		||||
	normal = 2.0*(vec3(color.r, -color.g, color.b) - vec3(0.5, -0.5, 0.5));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
	Helper function for on the fly normal map generation from height map.
 | 
			
		||||
*/
 | 
			
		||||
void mtex_h2n_rgb2float(vec4 color, out float outval)
 | 
			
		||||
{
 | 
			
		||||
	float scale = 1.0;
 | 
			
		||||
	outval = (color.r + color.g + color .b) / 3.0 * scale;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
	On the fly normal map generation from bump map.
 | 
			
		||||
	
 | 
			
		||||
	This is replacement for mtex_image which generates the normal value from a height value.
 | 
			
		||||
	It is inspired by The GIMP normal map plugin. I took the explicit algorithm and
 | 
			
		||||
	streamlined it to fit implicit GPU computation.
 | 
			
		||||
*/
 | 
			
		||||
void mtex_height_to_normal(vec3 texcoord, sampler2D image, vec2 texsize, out float value, out vec4 color, out vec3 normal)
 | 
			
		||||
{
 | 
			
		||||
	float down, up, right, left;
 | 
			
		||||
	/*texsize.xy = textureSize2D(image, 0);*/
 | 
			
		||||
	
 | 
			
		||||
	mtex_h2n_rgb2float( texture2D(image, texcoord.st+vec2(0,1)/texsize.xy), down );
 | 
			
		||||
	mtex_h2n_rgb2float( texture2D(image, texcoord.st+vec2(0,-1)/texsize.xy), up );
 | 
			
		||||
	mtex_h2n_rgb2float( texture2D(image, texcoord.st+vec2(1,0)/texsize.xy), right );
 | 
			
		||||
	mtex_h2n_rgb2float( texture2D(image, texcoord.st+vec2(-1,0)/texsize.xy), left );
 | 
			
		||||
 | 
			
		||||
	normal = normalize(vec3(left - right, down - up, 1.0));
 | 
			
		||||
	color = texture2D(image, texcoord.xy);
 | 
			
		||||
	value = 1.0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void mtex_negate_texnormal(vec3 normal, out vec3 outnormal)
 | 
			
		||||
{
 | 
			
		||||
	outnormal = vec3(-normal.x, -normal.y, normal.z);
 | 
			
		||||
 
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user