Created
November 15, 2011 02:53
-
-
Save Luomu/1365986 to your computer and use it in GitHub Desktop.
Ship colour shaders, Rendermonkey variety
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Vertex shader */ | |
uniform vec3 fvLightPosition; | |
uniform vec3 fvEyePosition; | |
varying vec2 Texcoord; | |
varying vec3 ViewDirection; | |
varying vec3 LightDirection; | |
varying vec3 Normal; | |
varying vec4 Decalcoord; | |
attribute vec3 rm_Binormal; | |
attribute vec3 rm_Tangent; | |
uniform vec2 decalAdjustment; | |
//#define NORMALMAP | |
void main( void ) | |
{ | |
gl_Position = ftransform(); | |
Texcoord = gl_MultiTexCoord0.xy; | |
vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex; | |
#ifdef NORMALMAP | |
vec3 fvViewDirection = fvEyePosition - fvObjectPosition.xyz; | |
vec3 fvLightDirection = fvLightPosition - fvObjectPosition.xyz; | |
vec3 fvNormal = gl_NormalMatrix * gl_Normal; | |
vec3 fvBinormal = gl_NormalMatrix * rm_Binormal; | |
vec3 fvTangent = gl_NormalMatrix * rm_Tangent; | |
ViewDirection.x = dot( fvTangent, fvViewDirection.xyz ); | |
ViewDirection.y = dot( fvBinormal, fvViewDirection.xyz ); | |
ViewDirection.z = dot( fvNormal, fvViewDirection.xyz ); | |
LightDirection.x = dot ( fvTangent, fvLightDirection.xyz ); | |
LightDirection.y = dot ( fvBinormal, fvLightDirection.xyz ); | |
LightDirection.z = dot ( fvNormal, fvLightDirection.xyz ); | |
#else | |
ViewDirection = fvEyePosition - fvObjectPosition.xyz; | |
LightDirection = fvLightPosition - fvObjectPosition.xyz; | |
Normal = gl_NormalMatrix * gl_Normal; | |
#endif | |
//hackety hack | |
Decalcoord = gl_Vertex; | |
Decalcoord.x += decalAdjustment.x; | |
Decalcoord.y += decalAdjustment.y; | |
} | |
/* Fragment shader */ | |
uniform vec4 fvAmbient; | |
uniform vec4 fvSpecular; | |
uniform vec4 fvDiffuse; | |
uniform float fSpecularPower; | |
uniform sampler2D baseMap; | |
uniform sampler2D patternMap; | |
uniform sampler2D colorMap; | |
uniform sampler2D specularMap; | |
uniform sampler2D decalMap; | |
varying vec2 Texcoord; | |
varying vec3 ViewDirection; | |
varying vec3 LightDirection; | |
uniform sampler2D normalMap; | |
varying vec3 Normal; | |
varying vec4 Decalcoord; | |
//#define NORMALMAP | |
#define SPECULARMAP | |
#define COLORMAP | |
#define DECAL | |
void main( void ) | |
{ | |
vec3 fvLightDirection = normalize( LightDirection ); | |
#ifdef NORMALMAP | |
vec3 fvNormal = normalize( ( texture2D( normalMap, Texcoord ).xyz * 2.0 ) - 1.0 ); | |
#else | |
vec3 fvNormal = normalize( Normal ); | |
#endif | |
float fNDotL = dot( fvNormal, fvLightDirection ); | |
vec3 fvReflection = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection ); | |
vec3 fvViewDirection = normalize( ViewDirection ); | |
float fRDotV = max( 0.0, dot( fvReflection, fvViewDirection ) ); | |
#ifdef COLORMAP | |
vec4 pattern = texture2D( patternMap, Texcoord ); | |
//float foo = (pattern.r+pattern.g+pattern.b)/3.0; | |
//red channel should be enough | |
vec4 color = texture2D( colorMap, vec2(pattern.r, 0.0) ); | |
vec4 fvBaseColor = texture2D( baseMap, Texcoord ) * color; | |
#else | |
vec4 fvBaseColor = texture2D( baseMap, Texcoord ); | |
#endif | |
#ifdef DECAL | |
vec4 decalColor = texture2DProj(decalMap, Decalcoord); | |
fvBaseColor = mix(fvBaseColor, | |
decalColor, decalColor.a * 0.9); | |
#endif | |
vec4 fvTotalAmbient = fvAmbient * fvBaseColor; | |
vec4 fvTotalDiffuse = fvDiffuse * fNDotL * fvBaseColor; | |
#ifdef SPECULARMAP | |
float specpow = 0.8; | |
vec4 specu = specpow * texture2D( specularMap, Texcoord ).r; | |
vec4 fvTotalSpecular = specu * ( pow( fRDotV, fSpecularPower ) ); | |
#else | |
vec4 fvTotalSpecular = fvSpecular * ( pow( fRDotV, fSpecularPower ) ); | |
#endif | |
gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment