openspades/Sources/Draw/GLFXAAFilter.cpp

72 lines
2.1 KiB
C++
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
2013-08-29 11:45:22 +09:00
This file is part of OpenSpades.
2013-08-29 11:45:22 +09:00
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2013-08-29 11:45:22 +09:00
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2013-08-29 11:45:22 +09:00
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
2013-08-29 11:45:22 +09:00
*/
2013-08-22 22:39:39 +09:00
#include <vector>
#include <Core/Debug.h>
#include <Core/Math.h>
#include "GLFXAAFilter.h"
2013-08-22 22:39:39 +09:00
#include "GLProgram.h"
#include "GLProgramAttribute.h"
#include "GLProgramUniform.h"
#include "GLQuadRenderer.h"
2013-08-22 22:39:39 +09:00
#include "GLRenderer.h"
#include "IGLDevice.h"
2013-08-22 22:39:39 +09:00
namespace spades {
namespace draw {
GLFXAAFilter::GLFXAAFilter(GLRenderer *renderer) : renderer(renderer) {
2013-09-06 11:44:36 +09:00
lens = renderer->RegisterProgram("Shaders/PostFilters/FXAA.program");
2013-08-22 22:39:39 +09:00
}
GLColorBuffer GLFXAAFilter::Filter(GLColorBuffer input) {
SPADES_MARK_FUNCTION();
2013-08-22 22:39:39 +09:00
IGLDevice *dev = renderer->GetGLDevice();
GLQuadRenderer qr(dev);
2013-08-22 22:39:39 +09:00
static GLProgramAttribute lensPosition("positionAttribute");
static GLProgramUniform lensTexture("mainTexture");
2013-08-22 22:39:39 +09:00
static GLProgramUniform inverseVP("inverseVP");
2013-08-22 22:39:39 +09:00
dev->Enable(IGLDevice::Blend, false);
2013-08-22 22:39:39 +09:00
lensPosition(lens);
lensTexture(lens);
inverseVP(lens);
2013-08-22 22:39:39 +09:00
lens->Use();
inverseVP.SetValue(1.f / dev->ScreenWidth(), 1.f / dev->ScreenHeight());
2013-08-22 22:39:39 +09:00
lensTexture.SetValue(0);
2013-08-22 22:39:39 +09:00
// composite to the final image
GLColorBuffer output = input.GetManager()->CreateBufferHandle();
2013-08-22 22:39:39 +09:00
qr.SetCoordAttributeIndex(lensPosition());
dev->BindTexture(IGLDevice::Texture2D, input.GetTexture());
dev->BindFramebuffer(IGLDevice::Framebuffer, output.GetFramebuffer());
dev->Viewport(0, 0, output.GetWidth(), output.GetHeight());
qr.Draw();
dev->BindTexture(IGLDevice::Texture2D, 0);
2013-08-22 22:39:39 +09:00
return output;
}
}
}