Introduced flame post processing shader.

This commit is contained in:
Faerbit 2015-03-03 23:27:41 +01:00
parent 60c106f863
commit 35b77c9c01
4 changed files with 95 additions and 14 deletions

View File

@ -0,0 +1,11 @@
#version 150
in vec2 vTexCoord;
uniform sampler2D flame_fbo;
out vec4 oColor;
void main() {
oColor = texture(flame_fbo, vTexCoord).rgba;
}

View File

@ -0,0 +1,11 @@
#version 150
in vec2 aPosition;
in vec2 aTexCoord;
out vec2 vTexCoord;
void main() {
vTexCoord = aTexCoord;
gl_Position = vec4(aPosition, 0.0, 1.0);
}

View File

@ -36,6 +36,25 @@ void Graphics::init(Level* level) {
glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
glEnable(GL_MULTISAMPLE); glEnable(GL_MULTISAMPLE);
fullscreen_quad_ab = SharedArrayBuffer(new ArrayBuffer());
fullscreen_quad_ab->defineAttribute("aPosition", GL_FLOAT, 2);
fullscreen_quad_ab->defineAttribute("aTexCoord", GL_FLOAT, 2);
float quadData[] = {
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f
};
fullscreen_quad_ab->setDataElements(6, quadData);
fullscreen_quad = SharedVertexArrayObject(new VertexArrayObject);
fullscreen_quad->attachAllAttributes(fullscreen_quad_ab);
// update lights on creation // update lights on creation
lastUpdate = -lightUpdateDelay; lastUpdate = -lightUpdateDelay;
@ -67,11 +86,14 @@ void Graphics::init(Level* level) {
flameShader = ShaderProgramCreator("flame") flameShader = ShaderProgramCreator("flame")
.attributeLocations(flame_positions->getAttributeLocations()).create(); .attributeLocations(flame_positions->getAttributeLocations()).create();
flamePostShader = ShaderProgramCreator("flame_post")
.attributeLocations(fullscreen_quad->getAttributeLocations()).create();
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &number_of_texture_units); glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &number_of_texture_units);
printf("Your graphics card supports %d texture units.\n", number_of_texture_units); printf("Your graphics card supports %d texture units.\n", number_of_texture_units);
// Exit if we need more texture units // Exit if we need more texture units
if (number_of_texture_units < 14) { if (number_of_texture_units < 16) {
printf("You need at least 14 texture units to run this application. Exiting\n"); printf("You need at least 16 texture units to run this application. Exiting\n");
exit(-1); exit(-1);
} }
@ -113,13 +135,32 @@ void Graphics::init(Level* level) {
framebuffer_cube = SharedFrameBufferObject(new FrameBufferObject()); framebuffer_cube = SharedFrameBufferObject(new FrameBufferObject());
if (level->getLights()->size() > 0) { if (level->getLights()->size() > 0) {
for(unsigned int i = 0; i<depth_cubeMaps.size(); i++){ for(unsigned int i = 0; i<depth_cubeMaps.size(); i++){
// start with texture unit 4 because the first four are used by the texture and the directional shadow map // start with texture unit 4 because the first four are used by the texture and the directional shadow map
lightingShader->setTexture("shadowMap_cube" + std::to_string(i), depth_cubeMaps.at(i), i+4); lightingShader->setTexture("shadowMap_cube" + std::to_string(i), depth_cubeMaps.at(i), i+4);
} }
} }
flame_fbo_color_texture = SharedTexture2D(new Texture2D(windowSize, GL_RGBA8));
flame_fbo_color_texture->setMinFilter(GL_NEAREST);
flame_fbo_color_texture->setMagFilter(GL_NEAREST);
flame_fbo_color_texture->setWrapS(GL_CLAMP_TO_BORDER);
flame_fbo_color_texture->setWrapT(GL_CLAMP_TO_BORDER);
flame_fbo_depth_texture = SharedTexture2D(new Texture2D(windowSize, GL_DEPTH_COMPONENT24));
flame_fbo_depth_texture->setMinFilter(GL_NEAREST);
flame_fbo_depth_texture->setMagFilter(GL_NEAREST);
flame_fbo_depth_texture->setWrapS(GL_CLAMP_TO_BORDER);
flame_fbo_depth_texture->setWrapT(GL_CLAMP_TO_BORDER);
framebuffer_flames = SharedFrameBufferObject(new FrameBufferObject());
framebuffer_flames->attachColorTexture("oColor", flame_fbo_color_texture);
framebuffer_flames->setDepthTexture(flame_fbo_depth_texture);
framebuffer_flames->setClearColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.0f));
framebuffer_flames->validate();
flamePostShader->use();
flamePostShader->setTexture("flame_fbo", flame_fbo_color_texture, 15);
updateClosestLights(); updateClosestLights();
} }
@ -187,7 +228,7 @@ void Graphics::render(double time)
} }
} }
// final render pass // lighting render pass
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -239,6 +280,10 @@ void Graphics::render(double time)
glCullFace(GL_BACK); glCullFace(GL_BACK);
// draw flames on top // draw flames on top
framebuffer_flames->bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
flameShader->use(); flameShader->use();
flameShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix); flameShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix);
flameShader->setUniform("time", (float) time); flameShader->setUniform("time", (float) time);
@ -255,6 +300,12 @@ void Graphics::render(double time)
flame_positions->render(); flame_positions->render();
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
flamePostShader->use();
fullscreen_quad->render();
} }
bool Graphics::compareLightDistances(Light a, Light b) { bool Graphics::compareLightDistances(Light a, Light b) {

View File

@ -8,6 +8,8 @@
#include "level.hh" #include "level.hh"
using namespace ACGL::OpenGL;
class Graphics { class Graphics {
public: public:
Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane, int cube_size, unsigned int maxShadowRenderCount); Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane, int cube_size, unsigned int maxShadowRenderCount);
@ -28,16 +30,22 @@ class Graphics {
float nearPlane; float nearPlane;
float farPlane; float farPlane;
std::vector<Light> closestLights; std::vector<Light> closestLights;
ACGL::OpenGL::SharedShaderProgram lightingShader; SharedShaderProgram lightingShader;
ACGL::OpenGL::SharedShaderProgram depthCubeShader; SharedShaderProgram depthCubeShader;
ACGL::OpenGL::SharedShaderProgram depthShader; SharedShaderProgram depthShader;
ACGL::OpenGL::SharedShaderProgram flameShader; SharedShaderProgram flameShader;
std::vector<ACGL::OpenGL::SharedTexture2D> depth_directionalMaps; SharedShaderProgram flamePostShader;
std::vector<ACGL::OpenGL::SharedFrameBufferObject> framebuffer_directional; std::vector<SharedTexture2D> depth_directionalMaps;
std::vector<ACGL::OpenGL::SharedTextureCubeMap> depth_cubeMaps; std::vector<SharedFrameBufferObject> framebuffer_directional;
ACGL::OpenGL::SharedFrameBufferObject framebuffer_cube; std::vector<SharedTextureCubeMap> depth_cubeMaps;
ACGL::OpenGL::SharedVertexArrayObject flame_positions; SharedFrameBufferObject framebuffer_cube;
ACGL::OpenGL::SharedArrayBuffer flame_positions_ab; SharedFrameBufferObject framebuffer_flames;
SharedTexture2D flame_fbo_color_texture;
SharedTexture2D flame_fbo_depth_texture;
SharedVertexArrayObject flame_positions;
SharedArrayBuffer flame_positions_ab;
SharedVertexArrayObject fullscreen_quad;
SharedArrayBuffer fullscreen_quad_ab;
int cube_size; int cube_size;
unsigned int maxShadowRenderCount; unsigned int maxShadowRenderCount;
Level* level; Level* level;