Project

General

Profile

SO3Engine
SO3DeferredCompositorDebug.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
31SDeferredCompositorDebug::SDeferredCompositorDebug(std::string compositorName) : SDeferredCompositor(compositorName)
32{
33}
34
35SDeferredCompositorDebug::SDeferredCompositorDebug() : SDeferredCompositor("")
36{
37 // Forbiden ctor
38}
39
40Ogre::CompositorPtr SDeferredCompositorDebug::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 // Reference to gbuffer texture
51 Ogre::CompositionTechnique::TextureDefinition* gBufferTexture = compositionTechnique->createTextureDefinition("mrt_output");
52 gBufferTexture->refCompName = "SO3/Deferred/Compositor/GBuffer/" + gBuffer->GetName();
53 gBufferTexture->refTexName = "mrt_output";
54
55 // Get the output target
56 Ogre::CompositionTargetPass* compositionTargetPass = compositionTechnique->getOutputTargetPass();
57 compositionTargetPass->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
58
59 // Create a pass on the target
60 Ogre::CompositionPass* compositionPass = compositionTargetPass->createPass();
61 compositionPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
62
63 // Set the material to use, ie, the effect to apply
64 compositionPass->setMaterial(GenerateCompositorMaterial(gBuffer));
65
66 // Set the input render targets
67 unsigned int numberMrt = gBuffer->GetNumberMrt();
68 for(unsigned int i = 0; i<numberMrt; i++)
69 compositionPass->setInput(i, "mrt_output", i);
70
71 // Compositor is created!
72 return compositor;
73}
74
75Ogre::MaterialPtr SDeferredCompositorDebug::GenerateCompositorMaterial(SGBuffer* gBuffer)
76{
77 Ogre::String matName = "SO3/Deferred/Compositor/Material/"+ name +"/"+ gBuffer->GetName();
78
79 // Material
80 Ogre::MaterialPtr matPtr = Ogre::MaterialManager::getSingleton().create(matName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
81 Ogre::Pass* pass = matPtr->getTechnique(0)->getPass(0);
82 pass->setCullingMode(Ogre::CULL_NONE);
83 pass->setManualCullingMode(Ogre::MANUAL_CULL_NONE);
84 pass->setDepthFunction(Ogre::CMPF_ALWAYS_PASS);
85
86 // Shaders
87 pass->setVertexProgram(GenerateCompositorVertexShader(gBuffer)->getName());
88 pass->setFragmentProgram(GenerateCompositorPixelShader(gBuffer)->getName());
89
90 // TU
91 unsigned int numberOfMrt = gBuffer->GetNumberMrt();
92 for (Ogre::uint32 i=0; i<numberOfMrt; i++)
93 {
94 Ogre::TextureUnitState* tu = pass->createTextureUnitState();
95 tu->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
96 tu->setTextureFiltering(Ogre::TFO_NONE);
97 tu->setTextureCoordSet(i);
98 }
99
100 return matPtr;
101}
102
103Ogre::GpuProgramPtr SDeferredCompositorDebug::GenerateCompositorVertexShader(SGBuffer* gBuffer)
104{
105 Ogre::StringStream ss;
106
107 // Post shader: Generic fullscreen quad
108 ss << "void main(float4 Pos: POSITION," << std::endl;
109 ss << " out float4 oPos: POSITION," << std::endl;
110 ss << " out float2 oTexCoord: TEXCOORD0," << std::endl;
111 ss << " out float3 oRay : TEXCOORD1," << std::endl;
112 ss << " uniform float3 farCorner," << 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 ss << " oPos = float4(Pos.xy, 0, 1);" << std::endl;
121 ss << " oPos.y *= flip;" << std::endl;
122
123 // Image-space
124 ss << " oTexCoord.x = 0.5 * (1 + Pos.x); // bound [0:1]" << std::endl;
125 ss << " oTexCoord.y = 0.5 * (1 - Pos.y); // bound [1:0]" << std::endl;
126
127 // This ray will be interpolated and will be the ray from the camera to the far clip plane, per pixel
128 ss << " oRay = farCorner * float3(Pos.xy, 1);" << std::endl;
129 ss << "}" << std::endl;
130
131 // Prepare shader generation
132 Ogre::String programSource = ss.str();
133 Ogre::String programName = "SO3/Deferred/Compositor/shader/"+ name +"/"+ gBuffer->GetName() +"_vs";
134
135#if SO3_DEBUG
136 // Show the generated vertex program in the log.
137 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(programSource, Ogre::LML_CRITICAL);
138#endif
139
140 // Create shader object
141 // TODO in another group than Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME?
142 Ogre::HighLevelGpuProgramPtr ptrProgram = Ogre::HighLevelGpuProgramManager::getSingleton().createProgram(programName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "cg", Ogre::GPT_VERTEX_PROGRAM);
143 ptrProgram->setSource(programSource);
144 ptrProgram->setParameter("entry_point", "main");
145 ptrProgram->setParameter("profiles", "vs_1_1 arbvp1");
146
147 // Bind our function parameters to Ogre's related values.
148 const Ogre::GpuProgramParametersSharedPtr& params = ptrProgram->getDefaultParameters();
149 if (params->_findNamedConstantDefinition("flip"))
150 params->setNamedAutoConstant("flip", Ogre::GpuProgramParameters::ACT_RENDER_TARGET_FLIPPING);
151
152 if (params->_findNamedConstantDefinition("farCorner"))
153 params->setNamedConstant("farCorner", Ogre::Vector3::UNIT_SCALE);
154
155 ptrProgram->load();
156
157 // Return compositor vertex shader
158 return Ogre::GpuProgramPtr(ptrProgram);
159}
160
161Ogre::GpuProgramPtr SDeferredCompositorDebug::GenerateCompositorPixelShader(SGBuffer* gBuffer)
162{
163 Ogre::StringStream ss;
164
165 // Structures defines
166 ss << gBuffer->GenerateGBufferCompositorPixelInputStructure();
167
168 // Compositors commons parameters
169 ss << "float4 main(float2 texCoord: TEXCOORD0," << std::endl;
170 ss << " float2 projCoord: TEXCOORD1," << std::endl;
171
172 // Input parameters (list of mrts)
173 ss << " " << gBuffer->GetGBufferCompositorPixelInputStructureTypeName() << " gBuffer" << std::endl;
174 ss << " ) : COLOR" << std::endl;
175
176 // Shader implementation
177 ss << "{" << std::endl;
179 ss << "}" << std::endl;
180
181 // Prepare shader generation
182 Ogre::String programSource = ss.str();
183 Ogre::String programName = "SO3/Deferred/Compositor/shader/"+ name +"/"+ gBuffer->GetName() +"_ps";
184
185#if SO3_DEBUG
186 // Show the generated vertex program in the log.
187 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(programSource, Ogre::LML_CRITICAL);
188#endif
189
190 // Create shader object
191 // TODO in another group than Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME?
192 Ogre::HighLevelGpuProgramPtr ptrProgram = Ogre::HighLevelGpuProgramManager::getSingleton().createProgram(programName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "cg", Ogre::GPT_FRAGMENT_PROGRAM);
193 ptrProgram->setSource(programSource);
194 ptrProgram->setParameter("entry_point", "main");
195 ptrProgram->setParameter("profiles", "ps_2_0 arbfp1");
196 ptrProgram->load();
197
198 // Return compositor pixel shader
199 return Ogre::GpuProgramPtr(ptrProgram);
200}
201
202}
std::string GetName() const
std::string name
Definition SO3DataScol.h:44
virtual Ogre::String GenerateCompositorPixelShaderImpl()=0