2016-03-21 04:10:14 +00:00
|
|
|
#include "R3DShader.h"
|
|
|
|
#include "Graphics/Shader.h"
|
|
|
|
|
|
|
|
namespace New3D {
|
2017-04-05 17:57:38 +00:00
|
|
|
|
2017-07-08 10:55:14 +00:00
|
|
|
static const char *vertexShaderR3D = R"glsl(
|
2017-04-05 17:57:38 +00:00
|
|
|
|
2017-08-09 23:25:01 +00:00
|
|
|
#version 120
|
|
|
|
|
2016-03-21 04:10:14 +00:00
|
|
|
// uniforms
|
2017-07-29 16:30:30 +00:00
|
|
|
uniform float modelScale;
|
2017-04-05 17:57:38 +00:00
|
|
|
|
2017-08-10 15:43:03 +00:00
|
|
|
// attributes
|
2017-08-19 20:06:31 +00:00
|
|
|
attribute vec4 inVertex;
|
2017-08-14 23:30:34 +00:00
|
|
|
attribute vec3 inNormal;
|
|
|
|
attribute vec2 inTexCoord;
|
|
|
|
attribute vec4 inColour;
|
2017-08-29 10:27:29 +00:00
|
|
|
attribute vec3 inFaceNormal; // used to emulate r3d culling
|
2017-08-14 23:30:34 +00:00
|
|
|
attribute float inFixedShade;
|
2017-08-10 15:43:03 +00:00
|
|
|
|
2017-08-09 16:56:56 +00:00
|
|
|
// outputs to fragment shader
|
2017-07-08 10:55:14 +00:00
|
|
|
varying vec3 fsViewVertex;
|
|
|
|
varying vec3 fsViewNormal; // per vertex normal vector
|
2017-08-10 15:43:03 +00:00
|
|
|
varying vec2 fsTexCoord;
|
2017-07-08 10:55:14 +00:00
|
|
|
varying vec4 fsColor;
|
2017-08-29 10:27:29 +00:00
|
|
|
varying float fsDiscard; // can't have varying bool (glsl spec)
|
2017-08-19 20:06:31 +00:00
|
|
|
varying float fsFixedShade;
|
2017-08-14 09:14:06 +00:00
|
|
|
|
2017-08-29 10:27:29 +00:00
|
|
|
float CalcBackFace(in vec3 viewVertex)
|
|
|
|
{
|
|
|
|
vec3 vt = viewVertex - vec3(0.0);
|
|
|
|
vec3 vn = (mat3(gl_ModelViewMatrix) * inFaceNormal);
|
|
|
|
|
|
|
|
// dot product of face normal with view direction
|
|
|
|
return dot(vt, vn);
|
|
|
|
}
|
|
|
|
|
2017-07-08 10:55:14 +00:00
|
|
|
void main(void)
|
|
|
|
{
|
2017-08-19 20:06:31 +00:00
|
|
|
fsViewVertex = vec3(gl_ModelViewMatrix * inVertex);
|
2017-08-10 15:43:03 +00:00
|
|
|
fsViewNormal = (mat3(gl_ModelViewMatrix) * inNormal) / modelScale;
|
2017-08-29 10:27:29 +00:00
|
|
|
fsDiscard = CalcBackFace(fsViewVertex);
|
2017-08-19 20:06:31 +00:00
|
|
|
fsColor = inColour;
|
2017-08-10 15:43:03 +00:00
|
|
|
fsTexCoord = inTexCoord;
|
2017-08-19 20:06:31 +00:00
|
|
|
fsFixedShade = inFixedShade;
|
|
|
|
gl_Position = gl_ModelViewProjectionMatrix * inVertex;
|
2017-07-08 10:55:14 +00:00
|
|
|
}
|
|
|
|
)glsl";
|
|
|
|
|
|
|
|
static const char *fragmentShaderR3D = R"glsl(
|
|
|
|
|
2017-08-09 23:25:01 +00:00
|
|
|
#version 120
|
|
|
|
|
2017-07-08 10:55:14 +00:00
|
|
|
uniform sampler2D tex1; // base tex
|
|
|
|
uniform sampler2D tex2; // micro tex (optional)
|
|
|
|
|
2017-07-22 17:15:14 +00:00
|
|
|
// texturing
|
2017-07-08 10:55:14 +00:00
|
|
|
uniform bool textureEnabled;
|
|
|
|
uniform bool microTexture;
|
|
|
|
uniform float microTextureScale;
|
|
|
|
uniform vec2 baseTexSize;
|
|
|
|
uniform bool textureInverted;
|
|
|
|
uniform bool textureAlpha;
|
2017-07-22 17:15:14 +00:00
|
|
|
uniform bool alphaTest;
|
|
|
|
|
|
|
|
// general
|
2017-07-08 10:55:14 +00:00
|
|
|
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)
|
2017-08-09 16:56:56 +00:00
|
|
|
uniform bool lightEnabled; // lighting enabled (1.0) or luminous (0.0), drawn at full intensity
|
2017-08-03 10:41:18 +00:00
|
|
|
uniform bool sunClamp; // not used by daytona and la machine guns
|
|
|
|
uniform bool intensityClamp; // some games such as daytona and
|
2017-08-09 16:56:56 +00:00
|
|
|
uniform bool specularEnabled; // specular enabled
|
|
|
|
uniform float specularValue; // specular coefficient
|
2017-07-08 10:55:14 +00:00
|
|
|
uniform float shininess; // specular shininess
|
2017-11-23 17:51:51 +00:00
|
|
|
uniform float fogIntensity;
|
|
|
|
uniform float fogDensity;
|
|
|
|
uniform float fogStart;
|
2017-07-08 10:55:14 +00:00
|
|
|
uniform float fogAttenuation;
|
|
|
|
uniform float fogAmbient;
|
2017-08-14 09:14:06 +00:00
|
|
|
uniform bool fixedShading;
|
2017-08-19 20:06:31 +00:00
|
|
|
uniform int hardwareStep;
|
2017-04-05 17:57:38 +00:00
|
|
|
|
|
|
|
//interpolated inputs from vertex shader
|
2017-07-08 10:55:14 +00:00
|
|
|
varying float fsFogFactor;
|
|
|
|
varying vec3 fsViewVertex;
|
|
|
|
varying vec3 fsViewNormal; // per vertex normal vector
|
2017-07-22 17:15:14 +00:00
|
|
|
varying vec4 fsColor;
|
2017-08-10 15:43:03 +00:00
|
|
|
varying vec2 fsTexCoord;
|
2017-08-29 10:27:29 +00:00
|
|
|
varying float fsDiscard;
|
2017-08-19 20:06:31 +00:00
|
|
|
varying float fsFixedShade;
|
2017-07-08 10:55:14 +00:00
|
|
|
|
|
|
|
vec4 GetTextureValue()
|
|
|
|
{
|
2017-08-10 15:43:03 +00:00
|
|
|
vec4 tex1Data = texture2D( tex1, fsTexCoord.st);
|
2017-07-08 10:55:14 +00:00
|
|
|
|
|
|
|
if(textureInverted) {
|
|
|
|
tex1Data.rgb = vec3(1.0) - vec3(tex1Data.rgb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (microTexture) {
|
|
|
|
vec2 scale = baseTexSize/256.0;
|
2017-08-10 15:43:03 +00:00
|
|
|
vec4 tex2Data = texture2D( tex2, fsTexCoord.st * scale * microTextureScale);
|
2017-07-08 10:55:14 +00:00
|
|
|
tex1Data = (tex1Data+tex2Data)/2.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alphaTest) {
|
|
|
|
if (tex1Data.a < (8.0/16.0)) {
|
|
|
|
discard;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (textureAlpha == false) {
|
|
|
|
tex1Data.a = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tex1Data;
|
|
|
|
}
|
|
|
|
|
2017-11-05 13:25:33 +00:00
|
|
|
void Step15Luminous(inout vec4 colour)
|
2017-08-19 20:06:31 +00:00
|
|
|
{
|
2017-11-05 13:25:33 +00:00
|
|
|
// luminous polys seem to behave very differently on step 1.5 hardware
|
|
|
|
// when fixed shading is enabled the colour is modulated by the vp ambient + fixed shade value
|
|
|
|
// when disabled it appears to be multiplied by 1.5, presumably to allow a higher range
|
2017-08-19 20:06:31 +00:00
|
|
|
if(hardwareStep==0x15) {
|
2017-11-05 13:25:33 +00:00
|
|
|
if(!lightEnabled && textureEnabled) {
|
|
|
|
if(fixedShading) {
|
|
|
|
colour.rgb *= 1.0 + fsFixedShade + lighting[1].y;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
colour.rgb *= vec3(1.5);
|
|
|
|
}
|
2017-08-19 20:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 17:51:51 +00:00
|
|
|
float CalcFog()
|
|
|
|
{
|
|
|
|
float z = -fsViewVertex.z;
|
|
|
|
float fog = fogIntensity * clamp(fogStart + z * fogDensity, 0.0, 1.0);
|
|
|
|
|
|
|
|
return fog;
|
|
|
|
}
|
|
|
|
|
2017-07-08 10:55:14 +00:00
|
|
|
void main()
|
|
|
|
{
|
|
|
|
vec4 tex1Data;
|
|
|
|
vec4 colData;
|
|
|
|
vec4 finalData;
|
|
|
|
vec4 fogData;
|
|
|
|
|
2017-08-30 21:45:25 +00:00
|
|
|
if(fsDiscard>=0) {
|
2017-08-29 10:27:29 +00:00
|
|
|
discard; //emulate back face culling here
|
|
|
|
}
|
|
|
|
|
2017-11-23 17:51:51 +00:00
|
|
|
fogData = vec4(fogColour.rgb * fogAmbient, CalcFog());
|
2017-07-08 10:55:14 +00:00
|
|
|
tex1Data = vec4(1.0, 1.0, 1.0, 1.0);
|
|
|
|
|
|
|
|
if(textureEnabled) {
|
|
|
|
tex1Data = GetTextureValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
colData = fsColor;
|
2017-11-05 13:25:33 +00:00
|
|
|
Step15Luminous(colData); // no-op for step 2.0+
|
2017-07-08 10:55:14 +00:00
|
|
|
finalData = tex1Data * colData;
|
|
|
|
|
2017-08-19 20:06:31 +00:00
|
|
|
if (finalData.a < (1.0/16.0)) { // basically chuck out any totally transparent pixels value = 1/16 the smallest transparency level h/w supports
|
2017-07-08 10:55:14 +00:00
|
|
|
discard;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ellipse;
|
|
|
|
ellipse = length((gl_FragCoord.xy - spotEllipse.xy) / spotEllipse.zw);
|
|
|
|
ellipse = pow(ellipse, 2.0); // decay rate = square of distance from center
|
2017-10-07 12:39:30 +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
|
|
|
|
range = 1.0 / pow(d * inv_r - 1.0, 2.0);
|
|
|
|
range *= enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
float lobeEffect = range * ellipse;
|
|
|
|
float lobeFogEffect = enable * ellipse;
|
|
|
|
|
|
|
|
if (lightEnabled) {
|
|
|
|
vec3 lightIntensity;
|
|
|
|
vec3 sunVector; // sun lighting vector (as reflecting away from vertex)
|
2017-07-08 10:55:14 +00:00
|
|
|
float sunFactor; // sun light projection along vertex normal (0.0 to 1.0)
|
2017-04-05 17:57:38 +00:00
|
|
|
|
2017-06-25 19:50:02 +00:00
|
|
|
// Sun angle
|
2017-07-08 10:55:14 +00:00
|
|
|
sunVector = lighting[0];
|
2017-04-05 17:57:38 +00:00
|
|
|
|
|
|
|
// Compute diffuse factor for sunlight
|
2017-08-19 20:06:31 +00:00
|
|
|
if(fixedShading) {
|
|
|
|
sunFactor = fsFixedShade;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sunFactor = dot(sunVector, fsViewNormal);
|
|
|
|
}
|
2017-07-22 17:15:14 +00:00
|
|
|
|
2017-08-09 16:56:56 +00:00
|
|
|
// Clamp ceil, fix for upscaled models without "modelScale" defined
|
|
|
|
sunFactor = clamp(sunFactor,-1.0,1.0);
|
|
|
|
|
2017-08-03 10:41:18 +00:00
|
|
|
// Optional clamping, value is allowed to be negative
|
|
|
|
if(sunClamp) {
|
2017-07-22 17:15:14 +00:00
|
|
|
sunFactor = max(sunFactor,0.0);
|
|
|
|
}
|
2017-04-05 17:57:38 +00:00
|
|
|
|
|
|
|
// Total light intensity: sum of all components
|
2017-07-22 17:15:14 +00:00
|
|
|
lightIntensity = vec3(sunFactor*lighting[1].x + lighting[1].y); // diffuse + ambient
|
2017-04-05 17:57:38 +00:00
|
|
|
|
2017-08-09 16:56:56 +00:00
|
|
|
// Upper clamp is optional, step 1.5+ games will drive brightness beyond 100%
|
2017-08-03 10:41:18 +00:00
|
|
|
if(intensityClamp) {
|
2017-10-07 12:39:30 +00:00
|
|
|
lightIntensity = min(lightIntensity,1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
lightIntensity.rgb += spotColor*lobeEffect;
|
|
|
|
|
|
|
|
finalData.rgb *= lightIntensity;
|
2017-04-05 17:57:38 +00:00
|
|
|
|
2017-08-19 20:06:31 +00:00
|
|
|
// for now assume fixed shading doesn't work with specular
|
2017-10-07 12:39:30 +00:00
|
|
|
if (specularEnabled) {
|
2017-08-09 16:56:56 +00:00
|
|
|
|
|
|
|
float exponent, NdotL, specularFactor;
|
|
|
|
vec4 biasIndex, expIndex, multIndex;
|
|
|
|
|
|
|
|
// Always clamp floor to zero, we don't want deep black areas
|
|
|
|
NdotL = max(0.0,sunFactor);
|
|
|
|
|
|
|
|
expIndex = vec4(8.0, 16.0, 32.0, 64.0);
|
|
|
|
multIndex = vec4(2.0, 2.0, 3.0, 4.0);
|
|
|
|
biasIndex = vec4(0.95, 0.95, 1.05, 1.0);
|
|
|
|
exponent = expIndex[int(shininess)] / biasIndex[int(shininess)];
|
|
|
|
|
|
|
|
specularFactor = pow(NdotL, exponent);
|
|
|
|
specularFactor *= multIndex[int(shininess)];
|
|
|
|
specularFactor *= biasIndex[int(shininess)];
|
|
|
|
|
|
|
|
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);
|
2017-07-08 10:55:14 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-05 17:57:38 +00:00
|
|
|
|
2017-08-09 16:56:56 +00:00
|
|
|
// Final clamp: we need it for proper shading in dimmed light and dark ambients
|
2017-10-07 12:39:30 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
gl_FragColor = finalData;
|
|
|
|
}
|
2017-07-08 10:55:14 +00:00
|
|
|
)glsl";
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2017-08-11 00:41:10 +00:00
|
|
|
R3DShader::R3DShader(const Util::Config::Node &config)
|
|
|
|
: m_config(config)
|
2016-03-21 04:10:14 +00:00
|
|
|
{
|
|
|
|
m_shaderProgram = 0;
|
|
|
|
m_vertexShader = 0;
|
|
|
|
m_fragmentShader = 0;
|
|
|
|
|
|
|
|
Start(); // reset attributes
|
|
|
|
}
|
|
|
|
|
|
|
|
void R3DShader::Start()
|
|
|
|
{
|
2017-04-05 17:57:38 +00:00
|
|
|
m_textured1 = false;
|
|
|
|
m_textured2 = false;
|
|
|
|
m_textureAlpha = false; // use alpha in texture
|
|
|
|
m_alphaTest = false; // discard fragment based on alpha (ogl does this with fixed function)
|
|
|
|
m_lightEnabled = false;
|
2017-08-09 16:56:56 +00:00
|
|
|
m_specularEnabled = false;
|
2017-04-05 17:57:38 +00:00
|
|
|
m_layered = false;
|
|
|
|
m_textureInverted = false;
|
2017-08-14 09:14:06 +00:00
|
|
|
m_fixedShading = false;
|
2017-07-29 16:30:30 +00:00
|
|
|
m_modelScale = 1.0f;
|
2017-08-09 16:56:56 +00:00
|
|
|
m_shininess = 0;
|
|
|
|
m_specularValue = 0;
|
|
|
|
m_microTexScale = 0;
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2017-03-28 20:24:44 +00:00
|
|
|
m_baseTexSize[0] = 0;
|
|
|
|
m_baseTexSize[1] = 0;
|
|
|
|
|
2016-03-22 23:39:59 +00:00
|
|
|
m_dirtyMesh = true; // dirty means all the above are dirty, ie first run
|
|
|
|
m_dirtyModel = true;
|
2016-03-21 04:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool R3DShader::LoadShader(const char* vertexShader, const char* fragmentShader)
|
|
|
|
{
|
|
|
|
const char* vShader;
|
|
|
|
const char* fShader;
|
|
|
|
bool success;
|
|
|
|
|
|
|
|
if (vertexShader) {
|
|
|
|
vShader = vertexShader;
|
|
|
|
}
|
|
|
|
else {
|
2017-04-05 17:57:38 +00:00
|
|
|
vShader = vertexShaderR3D;
|
2016-03-21 04:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fragmentShader) {
|
|
|
|
fShader = fragmentShader;
|
|
|
|
}
|
|
|
|
else {
|
2017-04-05 17:57:38 +00:00
|
|
|
fShader = fragmentShaderR3D;
|
2016-03-21 04:10:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 00:41:10 +00:00
|
|
|
success = LoadShaderProgram(&m_shaderProgram, &m_vertexShader, &m_fragmentShader, m_config["VertexShader"].ValueAs<std::string>(), m_config["FragmentShader"].ValueAs<std::string>(), vShader, fShader);
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2016-05-04 00:35:07 +00:00
|
|
|
m_locTexture1 = glGetUniformLocation(m_shaderProgram, "tex1");
|
|
|
|
m_locTexture2 = glGetUniformLocation(m_shaderProgram, "tex2");
|
|
|
|
m_locTexture1Enabled= glGetUniformLocation(m_shaderProgram, "textureEnabled");
|
|
|
|
m_locTexture2Enabled= glGetUniformLocation(m_shaderProgram, "microTexture");
|
2016-03-21 04:10:14 +00:00
|
|
|
m_locTextureAlpha = glGetUniformLocation(m_shaderProgram, "textureAlpha");
|
|
|
|
m_locAlphaTest = glGetUniformLocation(m_shaderProgram, "alphaTest");
|
2016-12-09 14:13:46 +00:00
|
|
|
m_locMicroTexScale = glGetUniformLocation(m_shaderProgram, "microTextureScale");
|
2017-03-28 20:24:44 +00:00
|
|
|
m_locBaseTexSize = glGetUniformLocation(m_shaderProgram, "baseTexSize");
|
2017-04-05 19:32:01 +00:00
|
|
|
m_locTextureInverted= glGetUniformLocation(m_shaderProgram, "textureInverted");
|
2016-03-21 04:10:14 +00:00
|
|
|
|
|
|
|
m_locFogIntensity = glGetUniformLocation(m_shaderProgram, "fogIntensity");
|
2017-04-05 17:57:38 +00:00
|
|
|
m_locFogDensity = glGetUniformLocation(m_shaderProgram, "fogDensity");
|
|
|
|
m_locFogStart = glGetUniformLocation(m_shaderProgram, "fogStart");
|
|
|
|
m_locFogColour = glGetUniformLocation(m_shaderProgram, "fogColour");
|
|
|
|
m_locFogAttenuation = glGetUniformLocation(m_shaderProgram, "fogAttenuation");
|
|
|
|
m_locFogAmbient = glGetUniformLocation(m_shaderProgram, "fogAmbient");
|
|
|
|
|
|
|
|
m_locLighting = glGetUniformLocation(m_shaderProgram, "lighting");
|
2017-08-09 16:56:56 +00:00
|
|
|
m_locLightEnabled = glGetUniformLocation(m_shaderProgram, "lightEnabled");
|
2017-08-03 10:41:18 +00:00
|
|
|
m_locSunClamp = glGetUniformLocation(m_shaderProgram, "sunClamp");
|
|
|
|
m_locIntensityClamp = glGetUniformLocation(m_shaderProgram, "intensityClamp");
|
2016-03-21 04:10:14 +00:00
|
|
|
m_locShininess = glGetUniformLocation(m_shaderProgram, "shininess");
|
2017-08-09 16:56:56 +00:00
|
|
|
m_locSpecularValue = glGetUniformLocation(m_shaderProgram, "specularValue");
|
|
|
|
m_locSpecularEnabled= glGetUniformLocation(m_shaderProgram, "specularEnabled");
|
2017-08-14 09:14:06 +00:00
|
|
|
m_locFixedShading = glGetUniformLocation(m_shaderProgram, "fixedShading");
|
2017-08-09 16:56:56 +00:00
|
|
|
|
2017-04-05 17:57:38 +00:00
|
|
|
m_locSpotEllipse = glGetUniformLocation(m_shaderProgram, "spotEllipse");
|
|
|
|
m_locSpotRange = glGetUniformLocation(m_shaderProgram, "spotRange");
|
|
|
|
m_locSpotColor = glGetUniformLocation(m_shaderProgram, "spotColor");
|
|
|
|
m_locSpotFogColor = glGetUniformLocation(m_shaderProgram, "spotFogColor");
|
2017-07-29 16:30:30 +00:00
|
|
|
m_locModelScale = glGetUniformLocation(m_shaderProgram, "modelScale");
|
2017-08-14 09:14:06 +00:00
|
|
|
|
|
|
|
m_locHardwareStep = glGetUniformLocation(m_shaderProgram, "hardwareStep");
|
2017-08-10 15:43:03 +00:00
|
|
|
|
2017-04-05 17:57:38 +00:00
|
|
|
return success;
|
|
|
|
}
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2017-08-11 11:59:41 +00:00
|
|
|
GLint R3DShader::GetVertexAttribPos(const char* attrib)
|
|
|
|
{
|
|
|
|
return glGetAttribLocation(m_shaderProgram, attrib); // probably should cache this but only called 1x per frame anyway
|
|
|
|
}
|
|
|
|
|
2016-03-21 04:10:14 +00:00
|
|
|
void R3DShader::SetShader(bool enable)
|
|
|
|
{
|
|
|
|
if (enable) {
|
|
|
|
glUseProgram(m_shaderProgram);
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
glUseProgram(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void R3DShader::SetMeshUniforms(const Mesh* m)
|
|
|
|
{
|
|
|
|
if (m == nullptr) {
|
|
|
|
return; // sanity check
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:39:59 +00:00
|
|
|
if (m_dirtyMesh) {
|
2016-05-04 00:35:07 +00:00
|
|
|
glUniform1i(m_locTexture1, 0);
|
|
|
|
glUniform1i(m_locTexture2, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_dirtyMesh || m->textured != m_textured1) {
|
|
|
|
glUniform1i(m_locTexture1Enabled, m->textured);
|
|
|
|
m_textured1 = m->textured;
|
2016-03-21 04:10:14 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 00:35:07 +00:00
|
|
|
if (m_dirtyMesh || m->microTexture != m_textured2) {
|
|
|
|
glUniform1i(m_locTexture2Enabled, m->microTexture);
|
|
|
|
m_textured2 = m->microTexture;
|
2016-03-21 04:10:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 14:13:46 +00:00
|
|
|
if (m_dirtyMesh || m->microTextureScale != m_microTexScale) {
|
|
|
|
glUniform1f(m_locMicroTexScale, m->microTextureScale);
|
|
|
|
m_microTexScale = m->microTextureScale;
|
|
|
|
}
|
|
|
|
|
2017-03-28 20:24:44 +00:00
|
|
|
if (m_dirtyMesh || m->microTexture && (m_baseTexSize[0] != m->width || m_baseTexSize[1] != m->height)) {
|
|
|
|
m_baseTexSize[0] = (float)m->width;
|
|
|
|
m_baseTexSize[1] = (float)m->height;
|
|
|
|
glUniform2fv(m_locBaseTexSize, 1, m_baseTexSize);
|
|
|
|
}
|
|
|
|
|
2017-04-05 17:57:38 +00:00
|
|
|
if (m_dirtyMesh || m->inverted != m_textureInverted) {
|
|
|
|
glUniform1i(m_locTextureInverted, m->inverted);
|
|
|
|
m_textureInverted = m->inverted;
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:39:59 +00:00
|
|
|
if (m_dirtyMesh || m->alphaTest != m_alphaTest) {
|
2016-03-21 04:10:14 +00:00
|
|
|
glUniform1i(m_locAlphaTest, m->alphaTest);
|
|
|
|
m_alphaTest = m->alphaTest;
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:39:59 +00:00
|
|
|
if (m_dirtyMesh || m->textureAlpha != m_textureAlpha) {
|
2016-03-21 04:10:14 +00:00
|
|
|
glUniform1i(m_locTextureAlpha, m->textureAlpha);
|
|
|
|
m_textureAlpha = m->textureAlpha;
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:39:59 +00:00
|
|
|
if (m_dirtyMesh || m->fogIntensity != m_fogIntensity) {
|
2016-03-21 04:10:14 +00:00
|
|
|
glUniform1f(m_locFogIntensity, m->fogIntensity);
|
|
|
|
m_fogIntensity = m->fogIntensity;
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:39:59 +00:00
|
|
|
if (m_dirtyMesh || m->lighting != m_lightEnabled) {
|
2017-08-09 16:56:56 +00:00
|
|
|
glUniform1i(m_locLightEnabled, m->lighting);
|
2016-03-22 23:39:59 +00:00
|
|
|
m_lightEnabled = m->lighting;
|
|
|
|
}
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2016-05-15 16:24:49 +00:00
|
|
|
if (m_dirtyMesh || m->shininess != m_shininess) {
|
2017-08-09 16:56:56 +00:00
|
|
|
glUniform1f(m_locShininess, m->shininess);
|
2016-05-15 16:24:49 +00:00
|
|
|
m_shininess = m->shininess;
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:56:56 +00:00
|
|
|
if (m_dirtyMesh || m->specular != m_specularEnabled) {
|
|
|
|
glUniform1i(m_locSpecularEnabled, m->specular);
|
|
|
|
m_specularEnabled = m->specular;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_dirtyMesh || m->specularValue != m_specularValue) {
|
|
|
|
glUniform1f(m_locSpecularValue, m->specularValue);
|
|
|
|
m_specularValue = m->specularValue;
|
2016-05-15 16:24:49 +00:00
|
|
|
}
|
2016-03-22 23:39:59 +00:00
|
|
|
|
2017-08-14 09:14:06 +00:00
|
|
|
if (m_dirtyMesh || m->fixedShading != m_fixedShading) {
|
|
|
|
glUniform1i(m_locFixedShading, m->fixedShading);
|
|
|
|
m_fixedShading = m->fixedShading;
|
|
|
|
}
|
|
|
|
|
2016-05-27 19:30:40 +00:00
|
|
|
if (m_dirtyMesh || m->layered != m_layered) {
|
|
|
|
m_layered = m->layered;
|
|
|
|
if (m_layered) {
|
|
|
|
glEnable(GL_STENCIL_TEST);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
glDisable(GL_STENCIL_TEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:39:59 +00:00
|
|
|
m_dirtyMesh = false;
|
2016-03-21 04:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void R3DShader::SetViewportUniforms(const Viewport *vp)
|
|
|
|
{
|
|
|
|
//didn't bother caching these, they don't get frequently called anyway
|
2017-04-05 17:57:38 +00:00
|
|
|
glUniform1f (m_locFogDensity, vp->fogParams[3]);
|
|
|
|
glUniform1f (m_locFogStart, vp->fogParams[4]);
|
|
|
|
glUniform3fv(m_locFogColour, 1, vp->fogParams);
|
|
|
|
glUniform1f (m_locFogAttenuation, vp->fogParams[5]);
|
|
|
|
glUniform1f (m_locFogAmbient, vp->fogParams[6]);
|
|
|
|
|
|
|
|
glUniform3fv(m_locLighting, 2, vp->lightingParams);
|
2017-08-03 10:41:18 +00:00
|
|
|
glUniform1i (m_locSunClamp, vp->sunClamp);
|
|
|
|
glUniform1i (m_locIntensityClamp, vp->intensityClamp);
|
2017-04-05 17:57:38 +00:00
|
|
|
glUniform4fv(m_locSpotEllipse, 1, vp->spotEllipse);
|
|
|
|
glUniform2fv(m_locSpotRange, 1, vp->spotRange);
|
|
|
|
glUniform3fv(m_locSpotColor, 1, vp->spotColor);
|
|
|
|
glUniform3fv(m_locSpotFogColor, 1, vp->spotFogColor);
|
2017-08-14 09:14:06 +00:00
|
|
|
|
|
|
|
glUniform1i (m_locHardwareStep, vp->hardwareStep);
|
2017-04-05 17:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void R3DShader::SetModelStates(const Model* model)
|
2016-03-22 23:39:59 +00:00
|
|
|
{
|
2017-07-29 16:30:30 +00:00
|
|
|
if (m_dirtyModel || model->scale != m_modelScale) {
|
|
|
|
glUniform1f(m_locModelScale, model->scale);
|
|
|
|
m_modelScale = model->scale;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:27:29 +00:00
|
|
|
m_dirtyModel = false;
|
2016-03-22 23:39:59 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 04:10:14 +00:00
|
|
|
} // New3D
|