Skip to content

Commit

Permalink
Converted Oblivion/Fallout 3/New Vegas shaders to core profile GLSL
Browse files Browse the repository at this point in the history
  • Loading branch information
fo76utils committed Jan 6, 2025
1 parent fd20db3 commit 44655a7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 48 deletions.
47 changes: 23 additions & 24 deletions res/shaders/ob_default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,24 @@ uniform float uvRotation;

uniform vec4 falloffParams;

uniform vec4 frontMaterialDiffuse;
uniform vec4 frontMaterialSpecular;
uniform vec4 frontMaterialAmbient;
uniform vec4 frontMaterialEmission;
uniform float frontMaterialShininess;

in mat3 reflMatrix;

in vec3 LightDir;
in vec3 ViewDir;

in vec2 texCoord;

in vec4 A;
in vec4 C;
in vec4 D;
in vec4 A;
in float toneMapScale;

out vec4 fragColor;


vec3 tonemap(vec3 x)
Expand All @@ -55,9 +64,9 @@ vec3 tonemap(vec3 x)
float e = 0.02;
float f = 0.30;

vec3 z = x * x * gl_LightSource[0].diffuse.a * (toneMapScale * 4.22978723);
vec3 z = x * x * D.a * (A.a * 4.22978723);
z = (z * (a * z + b * c) + d * e) / (z * (a * z + b) + d * f) - e / f;
return sqrt(z / (toneMapScale * 0.93333333));
return sqrt(z / (A.a * 0.93333333));
}

// parallax occlusion mapping based on code from
Expand Down Expand Up @@ -111,7 +120,7 @@ void main()
vec3 L = normalize( LightDir );
vec3 E = normalize( ViewDir );

vec2 offset = gl_TexCoord[0].st - uvCenter;
vec2 offset = texCoord.st - uvCenter;
float r_c = cos( uvRotation );
float r_s = sin( uvRotation ) * -1.0;
offset = vec2( offset.x * r_c - offset.y * r_s, offset.x * r_s + offset.y * r_c ) * uvScale + uvCenter + uvOffset;
Expand Down Expand Up @@ -143,22 +152,12 @@ void main()
vec3 H = normalize( L + E );
float NdotL = max( dot(normal, L), 0.0 );

// work around the lack of bitwise operators in GLSL 1.20
int tmp = vertexColorFlags;
bool vcfBit5 = ( tmp >= 32 );
if ( vcfBit5 )
tmp -= 32;
bool vcfBit4 = ( tmp >= 16 );
if ( vcfBit4 )
tmp -= 16;
bool vcfBit3 = ( tmp >= 8 );

if ( vcfBit3 && vcfBit5 ) {
if ( ( vertexColorFlags & 0x28 ) == 0x28 ) {
color *= C;
color.rgb *= A.rgb + ( D.rgb * NdotL );
} else if ( vcfBit3 ) {
color.rgb *= ( A.rgb * gl_FrontMaterial.ambient.rgb ) + ( D.rgb * gl_FrontMaterial.diffuse.rgb * NdotL );
color.a *= min( gl_FrontMaterial.ambient.a + gl_FrontMaterial.diffuse.a, 1.0 );
} else if ( ( vertexColorFlags & 0x08 ) != 0 ) {
color.rgb *= ( A.rgb * frontMaterialAmbient.rgb ) + ( D.rgb * frontMaterialDiffuse.rgb * NdotL );
color.a *= min( frontMaterialAmbient.a + frontMaterialDiffuse.a, 1.0 );
} else {
color.rgb *= A.rgb + ( D.rgb * NdotL );
}
Expand All @@ -173,13 +172,13 @@ void main()
cube *= texture( EnvironmentMap, offset ).r * cubeMapScale;
else
cube *= normalMap.a * cubeMapScale;
color.rgb += cube * sqrt( gl_LightSource[0].ambient.rgb );
color.rgb += cube * A.rgb * ( 1.0 / 0.375 );
}

// Emissive
vec3 emissive = glowColor.rgb * glowMult;
if ( !vcfBit4 )
emissive *= gl_FrontMaterial.emission.rgb;
if ( ( vertexColorFlags & 0x10 ) == 0 )
emissive *= frontMaterialEmission.rgb;
else
emissive *= C.rgb;
if ( hasGlowMap )
Expand All @@ -191,7 +190,7 @@ void main()
if ( hasSpecular && NdotL > 0.0 ) {
float NdotH = dot( normal, H );
if ( NdotH > 0.0 ) {
vec4 spec = gl_FrontMaterial.specular * pow( NdotH, gl_FrontMaterial.shininess );
vec4 spec = frontMaterialSpecular * pow( NdotH, frontMaterialShininess );
color.rgb += spec.rgb * normalMap.a;
}
}
Expand All @@ -206,5 +205,5 @@ void main()
discard;
}

gl_FragColor = vec4( tonemap( color.rgb ), color.a );
fragColor = vec4( tonemap( color.rgb ), color.a );
}
67 changes: 43 additions & 24 deletions res/shaders/ob_default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,62 @@
out vec3 LightDir;
out vec3 ViewDir;

out vec2 texCoord;

out vec4 A;
out vec4 C;
out vec4 D;
out vec4 A;
out float toneMapScale;

out vec3 N;
out vec3 t;
out vec3 b;
out vec3 v;

out mat3 reflMatrix;

uniform mat3 viewMatrix;
uniform mat3 normalMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform vec4 lightSourcePosition[3]; // W0 = environment map rotation (-1.0 to 1.0), W1, W2 = viewport X, Y
uniform vec4 lightSourceDiffuse[3]; // A0 = overall brightness, A1, A2 = viewport width, height
uniform vec4 lightSourceAmbient; // A = tone mapping control (1.0 = full tone mapping)

uniform vec4 vertexColorOverride; // components greater than zero replace the vertex color

layout ( location = 0 ) in vec3 vertexPosition;
layout ( location = 1 ) in vec4 vertexColor;
layout ( location = 2 ) in vec3 normalVector;
layout ( location = 3 ) in vec3 tangentVector;
layout ( location = 4 ) in vec3 bitangentVector;
layout ( location = 5 ) in vec4 boneWeights0;
layout ( location = 6 ) in vec4 boneWeights1;
layout ( location = 7 ) in vec2 multiTexCoord0;

#include "bonetransform.glsl"

void main()
{
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
vec4 v = vec4( vertexPosition, 1.0 );
vec3 n = normalVector;
vec3 t = tangentVector;
vec3 b = bitangentVector;

N = normalize(gl_NormalMatrix * gl_Normal);
t = normalize(gl_NormalMatrix * gl_MultiTexCoord1.xyz);
b = normalize(gl_NormalMatrix * gl_MultiTexCoord2.xyz);
if ( numBones > 0 )
boneTransform( v, n, t, b );

// NOTE: b<->t
mat3 tbnMatrix = mat3(b.x, t.x, N.x,
b.y, t.y, N.y,
b.z, t.z, N.z);
v = modelViewMatrix * v;
gl_Position = projectionMatrix * v;
texCoord = multiTexCoord0;

v = vec3(gl_ModelViewMatrix * gl_Vertex);
// NOTE: b<->t
mat3 btnMatrix = mat3( normalize(b * normalMatrix), normalize(t * normalMatrix), normalize(n * normalMatrix) );
btnMatrix = transpose( btnMatrix );

reflMatrix = viewMatrix * transpose(tbnMatrix);
reflMatrix = btnMatrix * viewMatrix;

ViewDir = tbnMatrix * -v.xyz;
LightDir = tbnMatrix * gl_LightSource[0].position.xyz;
if ( projectionMatrix[3][3] == 1.0 )
ViewDir = btnMatrix * vec3(0.0, 0.0, 1.0); // orthographic view
else
ViewDir = btnMatrix * -v.xyz;
LightDir = btnMatrix * lightSourcePosition[0].xyz;

C = gl_Color;
D = vec4( sqrt(gl_LightSource[0].diffuse.rgb), 1.0 );
A = vec4( sqrt(gl_LightSource[0].ambient.rgb) * 0.375, 1.0 );
toneMapScale = gl_LightSource[0].ambient.a;
A = vec4( sqrt(lightSourceAmbient.rgb) * 0.375, lightSourceAmbient.a );
C = mix( vertexColor, vertexColorOverride, greaterThan( vertexColorOverride, vec4( 0.0 ) ) );
D = vec4( sqrt(lightSourceDiffuse[0].rgb), lightSourceDiffuse[0].a );
}

0 comments on commit 44655a7

Please sign in to comment.