2018-09-13 12:50:34 +00:00
|
|
|
#ifndef _R3DSHADERTRIANGLES_H_
|
|
|
|
#define _R3DSHADERTRIANGLES_H_
|
|
|
|
|
|
|
|
static const char *vertexShaderR3D = R"glsl(
|
|
|
|
|
2022-11-07 21:33:01 +00:00
|
|
|
#version 410 core
|
2018-09-13 12:50:34 +00:00
|
|
|
|
|
|
|
// uniforms
|
|
|
|
uniform float modelScale;
|
2023-10-14 19:05:00 +00:00
|
|
|
uniform float nodeAlpha;
|
2024-08-01 22:24:46 +00:00
|
|
|
uniform float cota;
|
2018-09-13 12:50:34 +00:00
|
|
|
uniform mat4 modelMat;
|
|
|
|
uniform mat4 projMat;
|
2020-05-11 09:05:46 +00:00
|
|
|
uniform bool translatorMap;
|
2018-09-13 12:50:34 +00:00
|
|
|
|
|
|
|
// attributes
|
2022-11-07 21:33:01 +00:00
|
|
|
in vec4 inVertex;
|
|
|
|
in vec3 inNormal;
|
|
|
|
in vec2 inTexCoord;
|
|
|
|
in vec4 inColour;
|
|
|
|
in vec3 inFaceNormal; // used to emulate r3d culling
|
|
|
|
in float inFixedShade;
|
2024-07-05 14:57:25 +00:00
|
|
|
in float inTextureNP;
|
2018-09-13 12:50:34 +00:00
|
|
|
|
|
|
|
// outputs to fragment shader
|
2022-11-07 21:33:01 +00:00
|
|
|
out vec3 fsViewVertex;
|
|
|
|
out vec3 fsViewNormal; // per vertex normal vector
|
|
|
|
out vec2 fsTexCoord;
|
|
|
|
out vec4 fsColor;
|
|
|
|
out float fsDiscard; // can't have varying bool (glsl spec)
|
|
|
|
out float fsFixedShade;
|
2024-08-01 22:24:46 +00:00
|
|
|
out float fsLODBase;
|
2018-09-13 12:50:34 +00:00
|
|
|
|
2020-05-11 09:05:46 +00:00
|
|
|
vec4 GetColour(vec4 colour)
|
|
|
|
{
|
|
|
|
vec4 c = colour;
|
|
|
|
|
|
|
|
if(translatorMap) {
|
|
|
|
c.rgb *= 16.0;
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:05:00 +00:00
|
|
|
c.a *= nodeAlpha;
|
|
|
|
|
2020-05-11 09:05:46 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-09-13 12:50:34 +00:00
|
|
|
float CalcBackFace(in vec3 viewVertex)
|
|
|
|
{
|
|
|
|
vec3 vt = viewVertex - vec3(0.0);
|
|
|
|
vec3 vn = (mat3(modelMat) * inFaceNormal);
|
|
|
|
|
|
|
|
// dot product of face normal with view direction
|
|
|
|
return dot(vt, vn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
|
|
|
fsViewVertex = vec3(modelMat * inVertex);
|
|
|
|
fsViewNormal = (mat3(modelMat) * inNormal) / modelScale;
|
|
|
|
fsDiscard = CalcBackFace(fsViewVertex);
|
2020-05-11 09:05:46 +00:00
|
|
|
fsColor = GetColour(inColour);
|
2018-09-13 12:50:34 +00:00
|
|
|
fsTexCoord = inTexCoord;
|
|
|
|
fsFixedShade = inFixedShade;
|
2024-08-01 22:24:46 +00:00
|
|
|
fsLODBase = -fsDiscard * cota * inTextureNP;
|
2018-09-13 12:50:34 +00:00
|
|
|
gl_Position = projMat * modelMat * inVertex;
|
|
|
|
}
|
|
|
|
)glsl";
|
|
|
|
|
|
|
|
static const char *fragmentShaderR3D = R"glsl(
|
|
|
|
|
2022-11-07 21:33:01 +00:00
|
|
|
#version 410 core
|
2018-09-13 12:50:34 +00:00
|
|
|
|
2024-06-13 12:36:30 +00:00
|
|
|
uniform usampler2D textureBank[2]; // entire texture sheet
|
2018-09-13 12:50:34 +00:00
|
|
|
|
|
|
|
// texturing
|
|
|
|
uniform bool textureEnabled;
|
|
|
|
uniform bool microTexture;
|
2024-07-05 14:57:25 +00:00
|
|
|
uniform float microTextureMinLOD;
|
2022-11-07 21:33:01 +00:00
|
|
|
uniform int microTextureID;
|
|
|
|
uniform ivec4 baseTexInfo; // x/y are x,y positions in the texture sheet. z/w are with and height
|
|
|
|
uniform int baseTexType;
|
2018-09-13 12:50:34 +00:00
|
|
|
uniform bool textureInverted;
|
|
|
|
uniform bool textureAlpha;
|
|
|
|
uniform bool alphaTest;
|
|
|
|
uniform bool discardAlpha;
|
2018-10-13 13:29:45 +00:00
|
|
|
uniform ivec2 textureWrapMode;
|
2024-06-13 12:36:30 +00:00
|
|
|
uniform int texturePage;
|
2018-09-13 12:50:34 +00:00
|
|
|
|
|
|
|
// general
|
|
|
|
uniform vec3 fogColour;
|
|
|
|
uniform vec4 spotEllipse; // spotlight ellipse position: .x=X position (screen coordinates), .y=Y position, .z=half-width, .w=half-height)
|
|
|
|
uniform vec2 spotRange; // spotlight Z range: .x=start (viewspace coordinates), .y=limit
|
|
|
|
uniform vec3 spotColor; // spotlight RGB color
|
|
|
|
uniform vec3 spotFogColor; // spotlight RGB color on fog
|
|
|
|
uniform vec3 lighting[2]; // lighting state (lighting[0] = sun direction, lighting[1].x,y = diffuse, ambient intensities from 0-1.0)
|
|
|
|
uniform bool lightEnabled; // lighting enabled (1.0) or luminous (0.0), drawn at full intensity
|
|
|
|
uniform bool sunClamp; // not used by daytona and la machine guns
|
|
|
|
uniform bool intensityClamp; // some games such as daytona and
|
|
|
|
uniform bool specularEnabled; // specular enabled
|
|
|
|
uniform float specularValue; // specular coefficient
|
|
|
|
uniform float shininess; // specular shininess
|
|
|
|
uniform float fogIntensity;
|
|
|
|
uniform float fogDensity;
|
|
|
|
uniform float fogStart;
|
|
|
|
uniform float fogAttenuation;
|
|
|
|
uniform float fogAmbient;
|
|
|
|
uniform bool fixedShading;
|
2024-07-26 16:22:35 +00:00
|
|
|
uniform bool smoothShading;
|
2018-09-13 12:50:34 +00:00
|
|
|
uniform int hardwareStep;
|
2023-10-14 19:05:00 +00:00
|
|
|
uniform int colourLayer;
|
2023-12-22 16:31:05 +00:00
|
|
|
uniform bool polyAlpha;
|
2018-09-13 12:50:34 +00:00
|
|
|
|
2023-12-22 02:00:47 +00:00
|
|
|
// matrices (shared with vertex shader)
|
|
|
|
uniform mat4 projMat;
|
|
|
|
|
2018-09-13 12:50:34 +00:00
|
|
|
//interpolated inputs from vertex shader
|
2022-11-07 21:33:01 +00:00
|
|
|
in vec3 fsViewVertex;
|
|
|
|
in vec3 fsViewNormal; // per vertex normal vector
|
|
|
|
in vec4 fsColor;
|
|
|
|
in vec2 fsTexCoord;
|
|
|
|
in float fsDiscard;
|
|
|
|
in float fsFixedShade;
|
2024-08-01 22:24:46 +00:00
|
|
|
in float fsLODBase;
|
2022-11-07 21:33:01 +00:00
|
|
|
|
|
|
|
//outputs
|
2023-10-14 19:05:00 +00:00
|
|
|
layout(location = 0) out vec4 out0; // opaque
|
|
|
|
layout(location = 1) out vec4 out1; // trans layer 1
|
|
|
|
layout(location = 2) out vec4 out2; // trans layer 2
|
2018-10-13 13:29:45 +00:00
|
|
|
|
2023-10-14 19:05:00 +00:00
|
|
|
// forward declarations (see common file)
|
2018-09-13 12:50:34 +00:00
|
|
|
|
2023-10-14 19:05:00 +00:00
|
|
|
float CalcFog();
|
|
|
|
void Step15Luminous(inout vec4 colour);
|
|
|
|
vec4 GetTextureValue();
|
|
|
|
void WriteOutputs(vec4 colour, int layer);
|
2023-10-27 09:45:16 +00:00
|
|
|
float Sqr(float a);
|
|
|
|
float SqrLength(vec2 a);
|
2018-09-13 12:50:34 +00:00
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
vec4 tex1Data;
|
|
|
|
vec4 colData;
|
|
|
|
vec4 finalData;
|
|
|
|
vec4 fogData;
|
|
|
|
|
2019-01-16 01:07:56 +00:00
|
|
|
if(fsDiscard > 0) {
|
2018-09-13 12:50:34 +00:00
|
|
|
discard; //emulate back face culling here
|
|
|
|
}
|
2023-12-22 02:00:47 +00:00
|
|
|
|
|
|
|
gl_FragDepth = projMat[3][2] * gl_FragCoord.w;
|
|
|
|
|
2018-09-13 12:50:34 +00:00
|
|
|
fogData = vec4(fogColour.rgb * fogAmbient, CalcFog());
|
|
|
|
tex1Data = vec4(1.0, 1.0, 1.0, 1.0);
|
|
|
|
|
|
|
|
if(textureEnabled) {
|
|
|
|
tex1Data = GetTextureValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
colData = fsColor;
|
|
|
|
Step15Luminous(colData); // no-op for step 2.0+
|
|
|
|
finalData = tex1Data * colData;
|
|
|
|
|
2023-12-22 02:00:47 +00:00
|
|
|
if (finalData.a < (1.0/32.0)) { // basically chuck out any totally transparent pixels value = 1/16 the smallest transparency level h/w supports
|
2018-09-13 12:50:34 +00:00
|
|
|
discard;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ellipse;
|
2023-10-27 09:45:16 +00:00
|
|
|
ellipse = SqrLength((gl_FragCoord.xy - spotEllipse.xy) / spotEllipse.zw); // decay rate = square of distance from center
|
2018-09-13 12:50:34 +00:00
|
|
|
ellipse = 1.0 - ellipse; // invert
|
|
|
|
ellipse = max(0.0, ellipse); // clamp
|
|
|
|
|
|
|
|
// Compute spotlight and apply lighting
|
|
|
|
float enable, absExtent, d, inv_r, range;
|
|
|
|
|
|
|
|
// start of spotlight
|
|
|
|
enable = step(spotRange.x, -fsViewVertex.z);
|
|
|
|
|
|
|
|
if (spotRange.y == 0.0) {
|
|
|
|
range = 0.0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
absExtent = abs(spotRange.y);
|
|
|
|
|
|
|
|
d = spotRange.x + absExtent + fsViewVertex.z;
|
|
|
|
d = min(d, 0.0);
|
|
|
|
|
|
|
|
// slope of decay function
|
|
|
|
inv_r = 1.0 / (1.0 + absExtent);
|
|
|
|
|
|
|
|
// inverse-linear falloff
|
|
|
|
// Reference: https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
|
|
|
|
// y = 1 / (d/r + 1)^2
|
2023-10-27 09:45:16 +00:00
|
|
|
range = 1.0 / Sqr(d * inv_r - 1.0);
|
2018-09-13 12:50:34 +00:00
|
|
|
range *= enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
float lobeEffect = range * ellipse;
|
|
|
|
float lobeFogEffect = enable * ellipse;
|
|
|
|
|
|
|
|
if (lightEnabled) {
|
|
|
|
vec3 lightIntensity;
|
|
|
|
vec3 sunVector; // sun lighting vector (as reflecting away from vertex)
|
|
|
|
float sunFactor; // sun light projection along vertex normal (0.0 to 1.0)
|
|
|
|
|
|
|
|
// Sun angle
|
|
|
|
sunVector = lighting[0];
|
|
|
|
|
|
|
|
// Compute diffuse factor for sunlight
|
|
|
|
if(fixedShading) {
|
|
|
|
sunFactor = fsFixedShade;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sunFactor = dot(sunVector, fsViewNormal);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clamp ceil, fix for upscaled models without "modelScale" defined
|
|
|
|
sunFactor = clamp(sunFactor,-1.0,1.0);
|
|
|
|
|
|
|
|
// Optional clamping, value is allowed to be negative
|
2023-12-22 02:00:47 +00:00
|
|
|
// We suspect that translucent polygons are always clamped (e.g. lasers in Daytona 2)
|
2023-12-22 16:31:05 +00:00
|
|
|
if(sunClamp || polyAlpha) {
|
2018-09-13 12:50:34 +00:00
|
|
|
sunFactor = max(sunFactor,0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Total light intensity: sum of all components
|
|
|
|
lightIntensity = vec3(sunFactor*lighting[1].x + lighting[1].y); // diffuse + ambient
|
|
|
|
|
|
|
|
lightIntensity.rgb += spotColor*lobeEffect;
|
|
|
|
|
|
|
|
// Upper clamp is optional, step 1.5+ games will drive brightness beyond 100%
|
|
|
|
if(intensityClamp) {
|
|
|
|
lightIntensity = min(lightIntensity,1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
finalData.rgb *= lightIntensity;
|
|
|
|
|
|
|
|
// for now assume fixed shading doesn't work with specular
|
|
|
|
if (specularEnabled) {
|
|
|
|
|
2024-07-26 16:22:35 +00:00
|
|
|
float specularFactor;
|
2018-09-13 12:50:34 +00:00
|
|
|
|
2024-07-26 16:22:35 +00:00
|
|
|
if (smoothShading)
|
|
|
|
{
|
|
|
|
// Always clamp floor to zero
|
|
|
|
float NdotL = max(0.0, sunFactor);
|
2018-09-13 12:50:34 +00:00
|
|
|
|
2024-07-26 16:22:35 +00:00
|
|
|
vec4 expIndex = vec4(8.0, 16.0, 32.0, 64.0);
|
|
|
|
vec4 multIndex = vec4(1.6, 1.6, 2.4, 3.2);
|
|
|
|
float exponent = expIndex[int(shininess)];
|
|
|
|
|
|
|
|
specularFactor = pow(NdotL, exponent);
|
|
|
|
specularFactor *= multIndex[int(shininess)];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// flat shaded polys use Phong reflection model (R dot V) without any exponent or multiplier
|
|
|
|
// V = (0.0, 0.0, 1.0) is used by Model 3 as a fast approximation, so R dot V = R.z
|
|
|
|
vec3 R = reflect(-sunVector, fsViewNormal);
|
|
|
|
specularFactor = max(0.0, R.z);
|
|
|
|
}
|
2018-09-13 12:50:34 +00:00
|
|
|
|
|
|
|
specularFactor *= specularValue;
|
|
|
|
specularFactor *= lighting[1].x;
|
|
|
|
|
|
|
|
if (colData.a < 1.0) {
|
|
|
|
/// Specular hi-light affects translucent polygons alpha channel ///
|
|
|
|
finalData.a = max(finalData.a, specularFactor);
|
|
|
|
}
|
|
|
|
|
|
|
|
finalData.rgb += vec3(specularFactor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Final clamp: we need it for proper shading in dimmed light and dark ambients
|
|
|
|
finalData.rgb = min(finalData.rgb, vec3(1.0));
|
|
|
|
|
|
|
|
// Spotlight on fog
|
|
|
|
vec3 lSpotFogColor = spotFogColor * fogAttenuation * fogColour.rgb * lobeFogEffect;
|
|
|
|
|
|
|
|
// Fog & spotlight applied
|
|
|
|
finalData.rgb = mix(finalData.rgb, fogData.rgb + lSpotFogColor, fogData.a);
|
|
|
|
|
2023-10-14 19:05:00 +00:00
|
|
|
// Write outputs to colour buffers
|
|
|
|
WriteOutputs(finalData,colourLayer);
|
2018-09-13 12:50:34 +00:00
|
|
|
}
|
|
|
|
)glsl";
|
|
|
|
|
|
|
|
#endif
|