1 /*
2  Copyright (c) 2013 yvt
3 
4  This file is part of OpenSpades.
5 
6  OpenSpades is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OpenSpades is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OpenSpades.  If not, see <http://www.gnu.org/licenses/>.
18 
19  */
20 
21 #include <vector>
22 
23 #include <Core/Debug.h>
24 #include <Core/Math.h>
25 #include "GLNonlinearizeFilter.h"
26 #include "GLProgram.h"
27 #include "GLProgramAttribute.h"
28 #include "GLProgramUniform.h"
29 #include "GLQuadRenderer.h"
30 #include "GLRenderer.h"
31 #include "IGLDevice.h"
32 #include <Core/Settings.h>
33 
34 namespace spades {
35 	namespace draw {
GLNonlinearlizeFilter(GLRenderer * renderer)36 		GLNonlinearlizeFilter::GLNonlinearlizeFilter(GLRenderer *renderer) : renderer(renderer) {
37 			lens = renderer->RegisterProgram("Shaders/PostFilters/Nonlinearize.program");
38 		}
Filter(GLColorBuffer input)39 		GLColorBuffer GLNonlinearlizeFilter::Filter(GLColorBuffer input) {
40 			SPADES_MARK_FUNCTION();
41 
42 			IGLDevice *dev = renderer->GetGLDevice();
43 			GLQuadRenderer qr(dev);
44 
45 			static GLProgramAttribute lensPosition("positionAttribute");
46 			static GLProgramUniform lensTexture("mainTexture");
47 			static GLProgramUniform lensGamma("gamma");
48 
49 			dev->Enable(IGLDevice::Blend, false);
50 
51 			lensPosition(lens);
52 			lensTexture(lens);
53 			lensGamma(lens);
54 
55 			lens->Use();
56 
57 			lensTexture.SetValue(0);
58 			lensGamma.SetValue(1.f / (float)renderer->GetSettings().r_hdrGamma);
59 
60 			// composite to the final image
61 			GLColorBuffer output = input.GetManager()->CreateBufferHandle();
62 
63 			qr.SetCoordAttributeIndex(lensPosition());
64 			dev->BindTexture(IGLDevice::Texture2D, input.GetTexture());
65 			dev->BindFramebuffer(IGLDevice::Framebuffer, output.GetFramebuffer());
66 			dev->Viewport(0, 0, output.GetWidth(), output.GetHeight());
67 			qr.Draw();
68 			dev->BindTexture(IGLDevice::Texture2D, 0);
69 
70 			return output;
71 		}
72 	}
73 }
74