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 "GLLensFilter.h" 24 #include <Core/Debug.h> 25 #include <Core/Math.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 33 namespace spades { 34 namespace draw { GLLensFilter(GLRenderer * renderer)35 GLLensFilter::GLLensFilter(GLRenderer *renderer) : renderer(renderer) { 36 lens = renderer->RegisterProgram("Shaders/PostFilters/Lens.program"); 37 } Filter(GLColorBuffer input)38 GLColorBuffer GLLensFilter::Filter(GLColorBuffer input) { 39 SPADES_MARK_FUNCTION(); 40 41 IGLDevice *dev = renderer->GetGLDevice(); 42 GLQuadRenderer qr(dev); 43 44 static GLProgramAttribute lensPosition("positionAttribute"); 45 static GLProgramUniform lensTexture("mainTexture"); 46 static GLProgramUniform lensFov("fov"); 47 48 dev->Enable(IGLDevice::Blend, false); 49 50 lensPosition(lens); 51 lensTexture(lens); 52 lensFov(lens); 53 54 lens->Use(); 55 56 client::SceneDefinition def = renderer->GetSceneDef(); 57 lensFov.SetValue(tanf(def.fovX * .5f), tanf(def.fovY * .5f)); 58 lensTexture.SetValue(0); 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