Shader implementation

Published May 06, 2010
Advertisement
I started the shader implementation today, starting with a bumpmap shader.

It doesn't look any good at all yet, because I have no real normal maps :P I haven't spent any time on a good placement of the light yet either (only use a simple directional light in the shader for now).

I've optimized some code on the c++ side. Among other things I now only recalculate the view and normal matrices when it's absolutely necessary.

Here's the bumpmap shader:
bump.vert:
#version 140uniform struct DirectionalLight{	vec3 direction;	vec4 ambient;	vec4 diffuse;	vec4 specular;} DirectionalLight1;uniform struct Material{	vec4 ambient;	vec4 diffuse;	vec4 specular;	float shininess;} Material1;uniform struct Transform{	mat4 projectionMatrix;	mat4 viewMatrix;	mat4 modelMatrix;	mat4 normalMatrix;} Tranform1; in  vec3 in_Position;in  vec3 in_Normal;in  vec2 in_TexCoord;in  vec4 in_Color;out vec2 ex_TexCoord;out vec3 ex_ViewDir;out vec3 ex_LightDir; vec3 calcTangent(in vec3 normal){	vec3 tangent;	vec3 c1 = cross(normal, vec3(0.0, 0.0, 1.0)); 	vec3 c2 = cross(normal, vec3(0.0, 1.0, 0.0)); 	if(length(c1) > length(c2))	{		tangent = c1;		}	else	{		tangent = c2;		}	return tangent;}void main(void){		mat4 mvMatrix = Tranform1.viewMatrix * Tranform1.modelMatrix;		vec3 n = normalize(Tranform1.normalMatrix * vec4(in_Normal, 1.0)).xyz;	vec3 t = normalize(Tranform1.ormalMatrix * vec4(calcTangent(in_Normal), 1.0)).xyz;	vec3 b = cross(n, t);		mat3 tbnMatrix = mat3(t.x, b.x, n.x,                          t.y, b.y, n.y,                          t.z, b.z, n.z);				//Transform view dir to eye space (inverse of any vertex that has been transformed to eye space), then to tangent space	vec4 pos = mvMatrix * vec4(in_Position, 1.0);	ex_ViewDir = -(pos.xyz / pos.w);	ex_ViewDir = tbnMatrix * ex_ViewDir;		//Transform light dir to eye space, then to tangent space	ex_LightDir = vec3(Tranform1.viewMatrix * vec4(-DirectionalLight1.direction, 0.0f));	ex_LightDir = tbnMatrix * ex_LightDir;		ex_TexCoord = in_TexCoord;		gl_Position = Tranform1.projectionMatrix * mvMatrix * vec4(in_Position, 1.0);}


bump.frag
#version 140uniform struct DirectionalLight{	vec3 direction;	vec4 ambient;	vec4 diffuse;	vec4 specular;} DirectionalLight1;uniform struct Material{	vec4 ambient;	vec4 diffuse;	vec4 specular;	float shininess;} Material1;uniform sampler2D uni_DiffuseTex;uniform sampler2D uni_NormalTex;in vec2 ex_TexCoord;in vec3 ex_ViewDir;in vec3 ex_LightDir; out vec4 out_Color;void main(void){		vec3 L = normalize(ex_LightDir);	vec3 N = normalize(texture2D(uni_NormalTex, ex_TexCoord.st).xyz * 2.0 - 1.0);	vec3 V = normalize(ex_ViewDir);	vec3 R = normalize(- reflect(L, N));	float nDotL = max(0.0, dot(N, L));	float rDotV = max(0.0, dot(R, V));		vec4 ambient = DirectionalLight1.ambient * Material1.ambient;	vec4 diffuse = DirectionalLight1.diffuse * Material1.diffuse * nDotL;	vec4 specular = DirectionalLight1.specular * Material1.specular * pow(rDotV, Material1.shininess);	vec4 texel = texture2D(uni_DiffuseTex, ex_TexCoord.st);	float gloss = 1.0;		out_Color = (ambient + diffuse + (gloss * specular)) * texel;}




It obviously doesn't look very good yet :P So I have some work to do! Today I'll focus on making the scene look a bit better using shaders.
Previous Entry Almost there...
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement