2016-03-21 04:10:14 +00:00
|
|
|
#include "R3DShader.h"
|
2018-09-13 12:50:34 +00:00
|
|
|
#include "R3DShaderQuads.h"
|
|
|
|
#include "R3DShaderTriangles.h"
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2018-09-13 12:50:34 +00:00
|
|
|
// having 2 sets of shaders to maintain is really less than ideal
|
|
|
|
// but hopefully not too many breaking changes at this point
|
2017-10-07 12:39:30 +00:00
|
|
|
|
2018-09-13 12:50:34 +00:00
|
|
|
namespace New3D {
|
2016-03-21 04:10:14 +00:00
|
|
|
|
2017-08-11 00:41:10 +00:00
|
|
|
R3DShader::R3DShader(const Util::Config::Node &config)
|
2018-09-13 12:50:34 +00:00
|
|
|
: m_config(config)
|
2016-03-21 04:10:14 +00:00
|
|
|
{
|
|
|
|
m_shaderProgram = 0;
|
|
|
|
m_vertexShader = 0;
|
2018-09-13 12:50:34 +00:00
|
|
|
m_geoShader = 0;
|
2016-03-21 04:10:14 +00:00
|
|
|
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
|
|
|
|
2018-09-13 12:50:34 +00:00
|
|
|
m_baseTexSize[0] = 0;
|
|
|
|
m_baseTexSize[1] = 0;
|
2017-03-28 20:24:44 +00:00
|
|
|
|
2018-10-13 13:29:45 +00:00
|
|
|
m_texWrapMode[0] = 0;
|
|
|
|
m_texWrapMode[1] = 0;
|
|
|
|
|
2018-09-13 12:50:34 +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)
|
|
|
|
{
|
2018-09-13 12:50:34 +00:00
|
|
|
bool quads = m_config["QuadRendering"].ValueAs<bool>();
|
|
|
|
|
|
|
|
const char* vShader = vertexShaderR3D;
|
|
|
|
const char* gShader = "";
|
|
|
|
const char* fShader = fragmentShaderR3D;
|
|
|
|
|
|
|
|
if (quads) {
|
|
|
|
vShader = vertexShaderR3DQuads;
|
|
|
|
gShader = geometryShaderR3DQuads;
|
|
|
|
fShader = fragmentShaderR3DQuads;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_shaderProgram = glCreateProgram();
|
|
|
|
m_vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
m_fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
|
|
|
|
glShaderSource(m_vertexShader, 1, (const GLchar **)&vShader, NULL);
|
|
|
|
glShaderSource(m_fragmentShader, 1, (const GLchar **)&fShader, NULL);
|
|
|
|
|
|
|
|
glCompileShader(m_vertexShader);
|
|
|
|
glCompileShader(m_fragmentShader);
|
|
|
|
|
|
|
|
if (quads) {
|
|
|
|
m_geoShader = glCreateShader(GL_GEOMETRY_SHADER);
|
|
|
|
glShaderSource(m_geoShader, 1, (const GLchar **)&gShader, NULL);
|
|
|
|
glCompileShader(m_geoShader);
|
|
|
|
glAttachShader(m_shaderProgram, m_geoShader);
|
|
|
|
PrintShaderResult(m_geoShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintShaderResult(m_vertexShader);
|
|
|
|
PrintShaderResult(m_fragmentShader);
|
|
|
|
|
|
|
|
glAttachShader(m_shaderProgram, m_vertexShader);
|
|
|
|
glAttachShader(m_shaderProgram, m_fragmentShader);
|
|
|
|
glLinkProgram(m_shaderProgram);
|
|
|
|
|
|
|
|
PrintProgramResult(m_shaderProgram);
|
|
|
|
|
|
|
|
m_locTexture1 = glGetUniformLocation(m_shaderProgram, "tex1");
|
|
|
|
m_locTexture2 = glGetUniformLocation(m_shaderProgram, "tex2");
|
|
|
|
m_locTexture1Enabled = glGetUniformLocation(m_shaderProgram, "textureEnabled");
|
|
|
|
m_locTexture2Enabled = glGetUniformLocation(m_shaderProgram, "microTexture");
|
|
|
|
m_locTextureAlpha = glGetUniformLocation(m_shaderProgram, "textureAlpha");
|
|
|
|
m_locAlphaTest = glGetUniformLocation(m_shaderProgram, "alphaTest");
|
|
|
|
m_locMicroTexScale = glGetUniformLocation(m_shaderProgram, "microTextureScale");
|
|
|
|
m_locBaseTexSize = glGetUniformLocation(m_shaderProgram, "baseTexSize");
|
|
|
|
m_locTextureInverted = glGetUniformLocation(m_shaderProgram, "textureInverted");
|
2018-10-13 13:29:45 +00:00
|
|
|
m_locTexWrapMode = glGetUniformLocation(m_shaderProgram, "textureWrapMode");
|
2018-09-13 12:50:34 +00:00
|
|
|
|
|
|
|
m_locFogIntensity = glGetUniformLocation(m_shaderProgram, "fogIntensity");
|
|
|
|
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");
|
|
|
|
m_locLightEnabled = glGetUniformLocation(m_shaderProgram, "lightEnabled");
|
|
|
|
m_locSunClamp = glGetUniformLocation(m_shaderProgram, "sunClamp");
|
|
|
|
m_locIntensityClamp = glGetUniformLocation(m_shaderProgram, "intensityClamp");
|
|
|
|
m_locShininess = glGetUniformLocation(m_shaderProgram, "shininess");
|
|
|
|
m_locSpecularValue = glGetUniformLocation(m_shaderProgram, "specularValue");
|
|
|
|
m_locSpecularEnabled = glGetUniformLocation(m_shaderProgram, "specularEnabled");
|
|
|
|
m_locFixedShading = glGetUniformLocation(m_shaderProgram, "fixedShading");
|
|
|
|
|
|
|
|
m_locSpotEllipse = glGetUniformLocation(m_shaderProgram, "spotEllipse");
|
|
|
|
m_locSpotRange = glGetUniformLocation(m_shaderProgram, "spotRange");
|
|
|
|
m_locSpotColor = glGetUniformLocation(m_shaderProgram, "spotColor");
|
|
|
|
m_locSpotFogColor = glGetUniformLocation(m_shaderProgram, "spotFogColor");
|
|
|
|
m_locModelScale = glGetUniformLocation(m_shaderProgram, "modelScale");
|
|
|
|
|
|
|
|
m_locProjMat = glGetUniformLocation(m_shaderProgram, "projMat");
|
|
|
|
m_locModelMat = glGetUniformLocation(m_shaderProgram, "modelMat");
|
|
|
|
|
|
|
|
m_locHardwareStep = glGetUniformLocation(m_shaderProgram, "hardwareStep");
|
|
|
|
m_locDiscardAlpha = glGetUniformLocation(m_shaderProgram, "discardAlpha");
|
|
|
|
|
|
|
|
return true;
|
2017-04-05 17:57:38 +00:00
|
|
|
}
|
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();
|
2018-05-02 20:10:02 +00:00
|
|
|
DiscardAlpha(false); // need some default
|
2016-03-21 04:10:14 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-13 13:29:45 +00:00
|
|
|
if (m_dirtyMesh || (m_baseTexSize[0] != m->width || m_baseTexSize[1] != m->height)) {
|
2017-03-28 20:24:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-13 13:29:45 +00:00
|
|
|
if (m_dirtyMesh || m->wrapModeU != m_texWrapMode[0] || m->wrapModeV != m_texWrapMode[1]) {
|
|
|
|
m_texWrapMode[0] = m->wrapModeU;
|
|
|
|
m_texWrapMode[1] = m->wrapModeV;
|
|
|
|
glUniform2iv(m_locTexWrapMode, 1, m_texWrapMode);
|
|
|
|
}
|
|
|
|
|
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)
|
2018-09-13 12:50:34 +00:00
|
|
|
{
|
2016-03-21 04:10:14 +00:00
|
|
|
//didn't bother caching these, they don't get frequently called anyway
|
2018-09-13 12:50:34 +00:00
|
|
|
glUniform1f(m_locFogDensity, vp->fogParams[3]);
|
|
|
|
glUniform1f(m_locFogStart, vp->fogParams[4]);
|
2017-04-05 17:57:38 +00:00
|
|
|
glUniform3fv(m_locFogColour, 1, vp->fogParams);
|
2018-09-13 12:50:34 +00:00
|
|
|
glUniform1f(m_locFogAttenuation, vp->fogParams[5]);
|
|
|
|
glUniform1f(m_locFogAmbient, vp->fogParams[6]);
|
2017-04-05 17:57:38 +00:00
|
|
|
|
|
|
|
glUniform3fv(m_locLighting, 2, vp->lightingParams);
|
2018-09-13 12:50:34 +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
|
|
|
|
2018-09-13 12:50:34 +00:00
|
|
|
glUniformMatrix4fv(m_locProjMat, 1, GL_FALSE, vp->projectionMatrix);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-13 12:50:34 +00:00
|
|
|
glUniformMatrix4fv(m_locModelMat, 1, GL_FALSE, model->modelMat);
|
|
|
|
|
2017-08-29 10:27:29 +00:00
|
|
|
m_dirtyModel = false;
|
2016-03-22 23:39:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 20:10:02 +00:00
|
|
|
void R3DShader::DiscardAlpha(bool discard)
|
|
|
|
{
|
|
|
|
glUniform1i(m_locDiscardAlpha, discard);
|
|
|
|
}
|
|
|
|
|
2018-09-13 12:50:34 +00:00
|
|
|
void R3DShader::PrintShaderResult(GLuint shader)
|
|
|
|
{
|
|
|
|
//===========
|
|
|
|
GLint result;
|
|
|
|
GLint length;
|
|
|
|
//===========
|
|
|
|
|
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
|
|
|
|
|
|
|
|
if (result == GL_FALSE) {
|
|
|
|
|
|
|
|
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
|
|
|
|
|
|
|
|
if (length > 0) {
|
|
|
|
std::vector<char> msg(length);
|
|
|
|
glGetShaderInfoLog(shader, length, NULL, msg.data());
|
|
|
|
printf("%s\n", msg.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void R3DShader::PrintProgramResult(GLuint program)
|
|
|
|
{
|
|
|
|
//===========
|
|
|
|
GLint result;
|
|
|
|
//===========
|
|
|
|
|
|
|
|
glGetProgramiv(program, GL_LINK_STATUS, &result);
|
|
|
|
|
|
|
|
if (result == GL_FALSE) {
|
|
|
|
|
|
|
|
GLint maxLength = 0;
|
|
|
|
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
|
|
|
|
|
|
|
|
//The maxLength includes the NULL character
|
|
|
|
std::vector<GLchar> infoLog(maxLength);
|
|
|
|
glGetProgramInfoLog(program, maxLength, &maxLength, infoLog.data());
|
|
|
|
printf("%s\n", infoLog.data());
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 04:10:14 +00:00
|
|
|
} // New3D
|