2016-10-09 16:34:12 +00:00
|
|
|
#include "R3DScrollFog.h"
|
|
|
|
#include "Graphics/Shader.h"
|
|
|
|
|
|
|
|
namespace New3D {
|
|
|
|
|
2017-07-08 10:55:14 +00:00
|
|
|
static const char *vertexShaderFog = R"glsl(
|
|
|
|
|
2022-11-07 21:33:01 +00:00
|
|
|
#version 410 core
|
2017-07-08 10:55:14 +00:00
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
2022-11-07 21:33:01 +00:00
|
|
|
const vec4 vertices[] = vec4[](vec4(-1.0, -1.0, 0.0, 1.0),
|
|
|
|
vec4(-1.0, 1.0, 0.0, 1.0),
|
|
|
|
vec4( 1.0, -1.0, 0.0, 1.0),
|
|
|
|
vec4( 1.0, 1.0, 0.0, 1.0));
|
|
|
|
|
|
|
|
gl_Position = vertices[gl_VertexID % 4];
|
2018-06-18 10:24:46 +00:00
|
|
|
}
|
2017-07-08 10:55:14 +00:00
|
|
|
|
|
|
|
)glsl";
|
|
|
|
|
|
|
|
static const char *fragmentShaderFog = R"glsl(
|
|
|
|
|
2022-11-07 21:33:01 +00:00
|
|
|
#version 410 core
|
2022-07-22 22:18:02 +00:00
|
|
|
|
2017-07-08 10:55:14 +00:00
|
|
|
uniform float fogAttenuation;
|
|
|
|
uniform float fogAmbient;
|
|
|
|
uniform vec4 fogColour;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
void WriteOutputs(vec4 colour)
|
|
|
|
{
|
|
|
|
vec4 blank = vec4(0.0);
|
|
|
|
|
2023-10-15 16:16:52 +00:00
|
|
|
out0 = colour;
|
|
|
|
out1 = blank;
|
|
|
|
out2 = blank;
|
2023-10-14 19:05:00 +00:00
|
|
|
}
|
2022-11-07 21:33:01 +00:00
|
|
|
|
2017-07-08 10:55:14 +00:00
|
|
|
void main()
|
|
|
|
{
|
|
|
|
// Scroll fog base color
|
2023-10-24 09:17:15 +00:00
|
|
|
vec3 lFogColor = fogColour.rgb * fogAmbient;
|
2017-07-08 10:55:14 +00:00
|
|
|
|
|
|
|
// Scroll fog density
|
2023-10-24 09:17:15 +00:00
|
|
|
vec4 scrollFog = vec4(lFogColor, fogColour.a * (1.0 - fogAttenuation));
|
2017-07-08 10:55:14 +00:00
|
|
|
|
|
|
|
// Final Color
|
2023-10-14 19:05:00 +00:00
|
|
|
WriteOutputs(scrollFog);
|
2018-06-18 10:24:46 +00:00
|
|
|
}
|
2017-07-08 10:55:14 +00:00
|
|
|
|
|
|
|
)glsl";
|
|
|
|
|
|
|
|
|
2017-08-11 00:41:10 +00:00
|
|
|
R3DScrollFog::R3DScrollFog(const Util::Config::Node &config)
|
2022-11-07 21:33:01 +00:00
|
|
|
: m_config(config),
|
|
|
|
m_vao(0)
|
|
|
|
{
|
2016-10-09 16:34:12 +00:00
|
|
|
m_shaderProgram = 0;
|
|
|
|
m_vertexShader = 0;
|
|
|
|
m_fragmentShader = 0;
|
|
|
|
|
|
|
|
AllocResources();
|
2022-11-07 21:33:01 +00:00
|
|
|
|
|
|
|
glGenVertexArrays(1, &m_vao);
|
|
|
|
glBindVertexArray(m_vao);
|
|
|
|
// no states needed since we do it in the shader
|
|
|
|
glBindVertexArray(0);
|
2016-10-09 16:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R3DScrollFog::~R3DScrollFog()
|
|
|
|
{
|
2017-07-08 10:55:14 +00:00
|
|
|
DeallocResources();
|
2022-11-07 21:33:01 +00:00
|
|
|
|
|
|
|
if (m_vao) {
|
|
|
|
glDeleteVertexArrays(1, &m_vao);
|
|
|
|
m_vao = 0;
|
|
|
|
}
|
2017-07-08 10:55:14 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 09:17:15 +00:00
|
|
|
void R3DScrollFog::DrawScrollFog(float rgba[4], float attenuation, float ambient)
|
2017-07-08 10:55:14 +00:00
|
|
|
{
|
2016-10-09 16:34:12 +00:00
|
|
|
// some ogl states
|
|
|
|
glDepthMask (GL_FALSE); // disable z writes
|
|
|
|
glDisable (GL_DEPTH_TEST); // disable depth testing
|
2017-07-08 10:55:14 +00:00
|
|
|
|
2022-11-07 21:33:01 +00:00
|
|
|
glBindVertexArray (m_vao);
|
2017-07-08 10:55:14 +00:00
|
|
|
glUseProgram (m_shaderProgram);
|
2022-11-07 21:33:01 +00:00
|
|
|
glUniform4fv (m_locFogColour, 1, rgba);
|
2017-07-08 10:55:14 +00:00
|
|
|
glUniform1f (m_locFogAttenuation, attenuation);
|
|
|
|
glUniform1f (m_locFogAmbient, ambient);
|
|
|
|
|
2022-11-07 21:33:01 +00:00
|
|
|
glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
|
2016-10-09 16:34:12 +00:00
|
|
|
|
|
|
|
glUseProgram (0);
|
2022-11-07 21:33:01 +00:00
|
|
|
glBindVertexArray (0);
|
2016-10-09 16:34:12 +00:00
|
|
|
|
|
|
|
glDisable (GL_BLEND);
|
|
|
|
glDepthMask (GL_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void R3DScrollFog::AllocResources()
|
|
|
|
{
|
2017-08-11 00:41:10 +00:00
|
|
|
bool success = LoadShaderProgram(&m_shaderProgram, &m_vertexShader, &m_fragmentShader, m_config["VertexShaderFog"].ValueAs<std::string>(), m_config["FragmentShaderFog"].ValueAs<std::string>(), vertexShaderFog, fragmentShaderFog);
|
2017-07-08 10:55:14 +00:00
|
|
|
|
|
|
|
m_locFogColour = glGetUniformLocation(m_shaderProgram, "fogColour");
|
|
|
|
m_locFogAttenuation = glGetUniformLocation(m_shaderProgram, "fogAttenuation");
|
|
|
|
m_locFogAmbient = glGetUniformLocation(m_shaderProgram, "fogAmbient");
|
|
|
|
}
|
2016-10-09 16:34:12 +00:00
|
|
|
|
|
|
|
void R3DScrollFog::DeallocResources()
|
|
|
|
{
|
|
|
|
if (m_shaderProgram) {
|
|
|
|
DestroyShaderProgram(m_shaderProgram, m_vertexShader, m_fragmentShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_shaderProgram = 0;
|
|
|
|
m_vertexShader = 0;
|
|
|
|
m_fragmentShader = 0;
|
|
|
|
}
|
|
|
|
|
2022-07-11 16:10:41 +00:00
|
|
|
}
|