Project

General

Profile

SO3Engine
SO3DeferredCompositorAA.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
27
28namespace SO3
29{
30
31SDeferredCompositorAA::SDeferredCompositorAA(std::string compositorName) : SDeferredCompositor(compositorName)
32{
33}
34
35SDeferredCompositorAA::SDeferredCompositorAA() : SDeferredCompositor("")
36{
37 // Forbiden ctor
38}
39
40Ogre::CompositorPtr SDeferredCompositorAA::GenerateCompositor(SGBuffer* gBuffer)
41{
42 Ogre::String compoName = "SO3/Deferred/Compositor/"+ name +"/"+ gBuffer->GetName();
43
44 // Create compo
45 Ogre::CompositorPtr compositor = Ogre::CompositorManager::getSingleton().create(compoName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
46
47 // Create the technique
48 Ogre::CompositionTechnique* compositionTechnique = compositor->createTechnique();
49
50 // Create a "scene" texture
51 Ogre::CompositionTechnique::TextureDefinition* sceneTexture = compositionTechnique->createTextureDefinition("scene");
52 sceneTexture->width = 0;
53 sceneTexture->height = 0;
54 Ogre::PixelFormatList pixelFormat;
55 pixelFormat.push_back(Ogre::PF_R8G8B8A8);
56 sceneTexture->formatList = pixelFormat;
57
58 // Render scene to "scene" texture
59 Ogre::CompositionTargetPass* sceneTargetPass = compositionTechnique->createTargetPass();
60 sceneTargetPass->setInputMode(Ogre::CompositionTargetPass::IM_PREVIOUS);
61 sceneTargetPass->setOutputName("scene");
62
63 // Get the output target
64 Ogre::CompositionTargetPass* compositionTargetPass = compositionTechnique->getOutputTargetPass();
65 compositionTargetPass->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
66
67 // Create a pass on the target
68 Ogre::CompositionPass* compositionPass = compositionTargetPass->createPass();
69 compositionPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
70
71 // Set the material to use, ie, the effect to apply
72 compositionPass->setMaterial(GenerateCompositorMaterial(gBuffer));
73
74 // Set the input scene texture
75 compositionPass->setInput(0, "scene");
76
77 // Compositor is created!
78 return compositor;
79}
80
81Ogre::MaterialPtr SDeferredCompositorAA::GenerateCompositorMaterial(SGBuffer* gBuffer)
82{
83 Ogre::String matName = "SO3/Deferred/Compositor/Material/"+ name +"/"+ gBuffer->GetName();
84
85 // Material
86 Ogre::MaterialPtr matPtr = Ogre::MaterialManager::getSingleton().create(matName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
87 Ogre::Pass* pass = matPtr->getTechnique(0)->getPass(0);
88 pass->setCullingMode(Ogre::CULL_NONE);
89 pass->setManualCullingMode(Ogre::MANUAL_CULL_NONE);
90 pass->setDepthFunction(Ogre::CMPF_ALWAYS_PASS);
91
92 // Shaders
93 pass->setVertexProgram(GenerateCompositorVertexShader(gBuffer)->getName());
94 pass->setFragmentProgram(GenerateCompositorPixelShader(gBuffer)->getName());
95
96 // TU
97 Ogre::TextureUnitState* tu = pass->createTextureUnitState();
98 tu->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
99 tu->setTextureFiltering(Ogre::TFO_TRILINEAR);
100 tu->setTextureCoordSet(0);
101
102 return matPtr;
103}
104
105Ogre::GpuProgramPtr SDeferredCompositorAA::GenerateCompositorVertexShader(SGBuffer* gBuffer)
106{
107 Ogre::StringStream ss;
108
109 // Post shader: Generic fullscreen quad
110 ss << "void main(float4 Pos: POSITION," << std::endl;
111 ss << " out float4 oPos: POSITION," << std::endl;
112 ss << " out float2 oTexCoord: TEXCOORD0," << std::endl;
113 ss << " uniform float flip)" << std::endl;
114 ss << "{" << std::endl;
115
116 // Clean up inaccuracies
117 ss << " Pos.xy = sign(Pos.xy);" << std::endl;
118
119 // DirectX / OpenGL view transformation
120 //uv0 = (float2(inPos.x, -inPos.y) + 1.0f) * 0.5f;
121 ss << " oPos = float4(Pos.xy, 0, 1);" << std::endl;
122 ss << " oPos.y *= flip;" << std::endl;
123
124 // Image-space
125 ss << " oTexCoord.x = 0.5 * (1 + Pos.x); // bound [0:1]" << std::endl;
126 ss << " oTexCoord.y = 0.5 * (1 - Pos.y); // bound [1:0]" << std::endl;
127 ss << "}" << std::endl;
128
129 // Prepare shader generation
130 Ogre::String programSource = ss.str();
131 Ogre::String programName = "SO3/Deferred/Compositor/shader/"+ name +"/"+ gBuffer->GetName() +"_vs";
132
133#if SO3_DEBUG
134 // Show the generated vertex program in the log.
135 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(programSource, Ogre::LML_CRITICAL);
136#endif
137
138 // Create shader object
139 // TODO in another group than Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME?
140 Ogre::HighLevelGpuProgramPtr ptrProgram = Ogre::HighLevelGpuProgramManager::getSingleton().createProgram(programName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "cg", Ogre::GPT_VERTEX_PROGRAM);
141 ptrProgram->setSource(programSource);
142 ptrProgram->setParameter("entry_point", "main");
143 ptrProgram->setParameter("profiles", "vs_1_1 arbvp1");
144
145 // Bind our function parameters to Ogre's related values.
146 const Ogre::GpuProgramParametersSharedPtr& params = ptrProgram->getDefaultParameters();
147 if (params->_findNamedConstantDefinition("flip"))
148 params->setNamedAutoConstant("flip", Ogre::GpuProgramParameters::ACT_RENDER_TARGET_FLIPPING);
149 ptrProgram->load();
150
151 // Return compositor vertex shader
152 return Ogre::GpuProgramPtr(ptrProgram);
153}
154
155Ogre::GpuProgramPtr SDeferredCompositorAA::GenerateCompositorPixelShader(SGBuffer* gBuffer)
156{
157 Ogre::StringStream ss;
158
159 // Structures defines
160 ss << gBuffer->GenerateGBufferCompositorPixelInputStructure();
161
162 // Compositors commons parameters
163 ss << "float4 main(float2 texCoord: TEXCOORD0," << std::endl;
164 ss << " uniform float4 screenSize," << std::endl;
165 // Input parameters (rendered scene)
166 ss << " sampler scene : register(s0)" << std::endl;
167 ss << " ) : COLOR" << std::endl;
168
169 // Shader implementation
170 ss << "{" << std::endl;
172 ss << "}" << std::endl;
173
174 // Prepare shader generation
175 Ogre::String programSource = ss.str();
176 Ogre::String programName = "SO3/Deferred/Compositor/shader/"+ name +"/"+ gBuffer->GetName() +"_ps";
177
178#if SO3_DEBUG
179 // Show the generated vertex program in the log.
180 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(programSource, Ogre::LML_CRITICAL);
181#endif
182
183 // Create shader object
184 // TODO in another group than Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME?
185 Ogre::HighLevelGpuProgramPtr ptrProgram = Ogre::HighLevelGpuProgramManager::getSingleton().createProgram(programName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "cg", Ogre::GPT_FRAGMENT_PROGRAM);
186 ptrProgram->setSource(programSource);
187 ptrProgram->setParameter("entry_point", "main");
188 ptrProgram->setParameter("profiles", "ps_2_0 arbfp1");
189
190 // Bind our function parameters to Ogre's related values.
191 const Ogre::GpuProgramParametersSharedPtr& params = ptrProgram->getDefaultParameters();
192 if (params->_findNamedConstantDefinition("screenSize"))
193 params->setNamedAutoConstant("screenSize", Ogre::GpuProgramParameters::ACT_INVERSE_TEXTURE_SIZE);
194
195 ptrProgram->load();
196
197 // Return compositor pixel shader
198 return Ogre::GpuProgramPtr(ptrProgram);
199}
200
201}
std::string GetName() const
std::string name
Definition SO3DataScol.h:44
virtual Ogre::String GenerateCompositorPixelShaderImpl()=0