WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

 
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Prev Previous Post   Next Post Next
  #6  
Old 06-21-2013, 04:50 PM
shivanangel shivanangel is offline
Member
 
Join Date: Feb 2006
Location: New Jersey
Posts: 182
I went ahead and attached the model and textures.

Thanks,
George

Vertex:
Code:
#version 120

uniform int NumLights;

varying vec3 vPosition;
varying vec3 tPosition;
varying vec3 tView;

varying vec3 tang;
varying vec3 binorm;
varying vec3 normal;

varying vec3 tLightPosition[gl_MaxLights];
varying vec3 tLightDirection[gl_MaxLights];

attribute vec3 Tangent;
attribute vec3 Binormal;

void main()
{
	//Calculate components in view space
	vec3 position = vec3(gl_ModelViewMatrix * gl_Vertex);
	normal = normalize(gl_NormalMatrix * gl_Normal);
	tang = normalize(gl_NormalMatrix * Tangent);
	binorm = normalize(gl_NormalMatrix * Binormal);
	
	mat3 mToTangentSpace = mat3(
	tang.x, binorm.x, normal.x, 
	tang.y, binorm.y, normal.y,
	tang.z, binorm.z, normal.z);
	

	tView = mToTangentSpace * (position * -1.0); 
	tPosition = mToTangentSpace * position;
	//Iterate over every light and transition position into tangent space
	for( int i = 0 ; i < NumLights ; i++)
	{
		tLightPosition[i] = mToTangentSpace * gl_LightSource[i].spotDirection.xyz;
		tLightPosition[i] = mToTangentSpace * gl_LightSource[i].position.xyz;
	}

	//Pass and transform texture coordinates
	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

	//Pass Perspecitive Coordinates
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	
	//Compute Fog
	gl_FogFragCoord = gl_FogCoord;
}
Fragment:
Code:
#version 120

uniform int NumLights;

varying vec3 vPosition;
varying vec3 tPosition;
varying vec3 tView;

varying vec3 normal;
varying vec3 tang;
varying vec3 binorm;

varying vec3 tLightPosition[gl_MaxLights];
varying vec3 tLightDirection[gl_MaxLights];

uniform sampler2D Albedo;
uniform sampler2D NormalMap;
uniform sampler2D AmbientOcclusionMap;

//Calculates and returns the contribution of a directional light
void DirectionalLight(in int i,
						in vec3 position,
						in vec3 view,
						in vec3 normal,
						inout vec4 ambient,
						inout vec4 diffuse, 
						inout vec4 specular)
{	
	float nDotL; //normal dotted with light vector
	float nDotH; //normal dotted with half vector
	float pf;
	
	nDotL = max(0.0, dot(normal, normalize(tLightPosition[i])));
		
	nDotH = max(0.0, dot(normal, normalize( normalize(tLightPosition[i]) + view) ));
	
	if( nDotL > 0.0)
		pf = pow(nDotH, gl_FrontMaterial.shininess);
	else
		pf = 0.0;
	
	ambient += gl_LightSource[i].ambient;
	diffuse += gl_LightSource[i].diffuse * nDotL;
	specular += gl_LightSource[i].specular * pf;
	
}

//Caculates and returns the contribution of a spotlight
void PointLight( in int i,
					in vec3 position,
					in vec3 view,
					in vec3 iNormal,
					inout vec4 ambient,
					inout vec4 diffuse,
					inout vec4 specular)
{
	float nDotL;
	float nDotH;
	float pf;
	float attenuation;
	float d;
	vec3 LP;
	vec3 halfVector;

	LP = tLightPosition[i] - position; 
	d = length(LP); // distance from surface to light for attenuation
	vec3 tLP = normalize(tLightPosition[i]);
	
	attenuation = 1.0 / (gl_LightSource[i].constantAttenuation
						+ gl_LightSource[i].linearAttenuation * d
						+ gl_LightSource[i].quadraticAttenuation * d * d);

	halfVector = normalize(tLP + view);

	nDotL = max(0.0, dot(iNormal, tLP));
	nDotH = max(0.0, dot(iNormal, halfVector));
	
	if (nDotL > 0.0)
		pf = pow(nDotH, gl_FrontMaterial.shininess);
	else
		pf = 0.0;
	
	ambient += gl_LightSource[i].ambient * attenuation;
	diffuse += gl_LightSource[i].diffuse * nDotL * attenuation;
	specular += gl_LightSource[i].specular * pf * attenuation;
}

void SpotLight(in int i,
				in vec3 position,
				in vec3 view,
				in vec3 normal,
				inout vec4 ambient,
				inout vec4 diffuse,
				inout vec4 specular)
{
	float nDotL;
	float nDotH;
	float pf;
	float spotDot;
	float spotAttenuation;
	float attenuation;
	float d;
	vec3 LV;
	vec3 halfVector;
	
	LV = tLightPosition[i] - position;
	d = length(LV);
	LV = normalize(LV);
	
	attenuation = 1.0 / (gl_LightSource[i].constantAttenuation + 
						gl_LightSource[i].linearAttenuation * d + 
						gl_LightSource[i].quadraticAttenuation * d * d);
	
	spotDot = dot(LV, normalize( tLightDirection[i]));
	
	if(spotDot < gl_LightSource[i].spotCosCutoff)
		spotAttenuation = 0.0;
	else
		spotAttenuation = pow(spotDot, gl_LightSource[i].spotExponent);
		
	attenuation *= spotAttenuation;
	
	halfVector = normalize(LV + view);
	
	nDotL = max(0.0, dot(normal, LV));
	nDotH = max(0.0, dot(normal, halfVector));
	
	if(nDotL == 0.0) pf = 0.0;
	else pf = pow(nDotH, gl_FrontMaterial.shininess);
	
	ambient += gl_LightSource[i].ambient * attenuation;
	diffuse += gl_LightSource[i].diffuse * nDotL * attenuation;
	specular += gl_LightSource[i].specular * pf * attenuation;
}

void main()
{
	vec4 albedo = 		texture2D(Albedo , gl_TexCoord[0].xy);
	vec3 texNormal = 	normalize(texture2D(NormalMap, gl_TexCoord[0].xy).rgb);
	vec3 ao = 			texture2D(AmbientOcclusionMap, gl_TexCoord[0].xy).rgb;
	
	vec4 ambient = vec4(0.0);
	vec4 diffuse = vec4(0.0);
	vec4 specular = vec4(0.0);
	
	vec3 ntView = normalize(tView);
	
	for(int i = 0; i < NumLights; i++)
	{
		if (gl_LightSource[i].position.w == 0.0)
			DirectionalLight(i, tPosition, ntView, texNormal, ambient, diffuse, specular);
			
		else if (gl_LightSource[i].spotCutoff == 180.0)
			PointLight(i, tPosition, ntView, texNormal, ambient, diffuse, specular);
			
		else
			SpotLight(i, tPosition, ntView, texNormal, ambient, diffuse, specular);
	}
	
	//Linear fog calculation
	float dist = abs(vPosition.z);
	float fogFactor =(gl_Fog.end - dist) * gl_Fog.scale;
	fogFactor = clamp(fogFactor, 0.0, 1.0);
	
	vec4 color = (ambient * gl_FrontMaterial.ambient + diffuse * gl_FrontMaterial.diffuse) * albedo; //Albedo
	color = color + (specular * gl_FrontMaterial.specular);//Spec
	//color = color * vec4(ao,1.0);
	color = mix( gl_Fog.color, color, fogFactor); //Fog
	
	gl_FragColor = color;
}
Attached Files
File Type: zip Church.zip (390.0 KB, 3369 views)
Reply With Quote
 

Tags
binormal, normal, tangent


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Per vertex attributes in OTF objects? stefs Vizard 1 10-23-2009 03:36 PM
Semi-circle array containing target and distractor objects ptjt255 Vizard 3 08-04-2009 03:09 AM
Normal Map Workflow for Vizard shivanangel Vizard 2 03-10-2009 06:54 PM
Could not find plugin to load objects... halley Vizard 1 05-30-2006 11:01 AM


All times are GMT -7. The time now is 12:14 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC