Project

General

Profile

SO3Engine
SO3DeferredLightMaterialGenerator.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
26
27namespace SO3
28{
29
30SDeferredLightMaterialGenerator::SDeferredLightMaterialGenerator(const Ogre::String& typeName) : SMaterialGenerator<SDeferredLightPermutation, Ogre::uint32>(Ogre::String("Lights/") + typeName)
31{
32 debugVertexProgram = GenerateDebugVertexShader();
33 debugFragmentProgram = GenerateDebugFragmentShader();
34}
35
36SDeferredLightMaterialGenerator::SDeferredLightMaterialGenerator() : SMaterialGenerator<SDeferredLightPermutation, Ogre::uint32>("")
37{
38}
39
43
44Ogre::MaterialPtr SDeferredLightMaterialGenerator::GenerateTemplateMaterial(SGBuffer* gbuffer, Ogre::uint32 permutationValue)
45{
46 Ogre::String matName = materialBaseName + Ogre::StringConverter::toString(permutationValue);
47
48 // Create a new material
49 Ogre::MaterialPtr matPtr = Ogre::MaterialManager::getSingleton().create(matName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
50
51 matPtr->getTechnique(0)->setName("DeferredTechnique");
52 Ogre::Pass* pass = matPtr->getTechnique(0)->getPass(0);
53 pass->setName("DeferredPass");
54 pass->setLightingEnabled(false);
55 pass->setDepthWriteEnabled(false);
56 pass->setDepthCheckEnabled(true);
57
58 unsigned int numberMrt = gbuffer->GetNumberMrt();
59 for (unsigned int i=0; i<numberMrt; i++)
60 {
61 Ogre::TextureUnitState* tu = pass->createTextureUnitState();
62 tu->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
63 tu->setTextureFiltering(Ogre::TFO_NONE);
64 tu->setContentType(Ogre::TextureUnitState::CONTENT_COMPOSITOR);
65 tu->setCompositorReference("SO3/Deferred/Compositor/GBuffer/"+ gbuffer->GetName() , "mrt_output", i);
66 }
67
68 // Shadows
70 {
71 Ogre::TextureUnitState* tu = pass->createTextureUnitState();
72 tu->setName("ShadowMap");
73 tu->setContentType(Ogre::TextureUnitState::CONTENT_SHADOW);
74 }
75
76 // Debug pass
77 if(permutationValue & SDeferredLightPermutation::SO3_LP_DEBUG)
78 {
79 pass = matPtr->getTechnique(0)->createPass();
80 pass->setName("DeferredDebugPass");
81 pass->setPolygonMode(Ogre::PM_WIREFRAME);
82 /*pass->setCullingMode(Ogre::CULL_NONE);
83 pass->setManualCullingMode(Ogre::MANUAL_CULL_NONE);*/
84
85 pass->setVertexProgram(debugVertexProgram->getName());
86 pass->setFragmentProgram(debugFragmentProgram->getName());
87 }
88
89 return matPtr;
90}
91
92Ogre::GpuProgramPtr SDeferredLightMaterialGenerator::GenerateDebugVertexShader()
93{
94 Ogre::StringStream ss;
95
96 ss << "float4 SO3_DeferredLightDebug_vs(float4 pos: POSITION," << std::endl;
97 ss << " uniform float4x4 wvp) : POSITION" << std::endl;
98 ss << "{" << std::endl;
99 ss << " return mul(wvp, pos);" << std::endl;
100 ss << "}" << std::endl;
101
102 // Prepare shader generation
103 Ogre::String programSource = ss.str();
104 Ogre::String programName = materialBaseName +"Shaders/Vertex/Debug";
105
106#if SO3_DEBUG
107 // Show the generated vertex program in the log.
108 //Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(programSource, Ogre::LML_CRITICAL);
109#endif
110
111 // Create shader object
112 // TODO in another group than Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME?
113 Ogre::HighLevelGpuProgramPtr ptrProgram = Ogre::HighLevelGpuProgramManager::getSingleton().createProgram(programName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "cg", Ogre::GPT_VERTEX_PROGRAM);
114 ptrProgram->setSource(programSource);
115 ptrProgram->setParameter("entry_point", "SO3_DeferredLightDebug_vs");
116 ptrProgram->setParameter("profiles", "vs_1_1 arbvp1");
117
118 // Bind our function parameters to Ogre's related values.
119 const Ogre::GpuProgramParametersSharedPtr& params = ptrProgram->getDefaultParameters();
120 if (params->_findNamedConstantDefinition("wvp"))
121 params->setNamedAutoConstant("wvp", Ogre::GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
122
123 // Return light vertex shader
124 ptrProgram->load();
125 return Ogre::GpuProgramPtr(ptrProgram);
126}
127
128Ogre::GpuProgramPtr SDeferredLightMaterialGenerator::GenerateDebugFragmentShader()
129{
130 Ogre::StringStream ss;
131
132 ss << "float4 SO3_DeferredLightDebug_ps(uniform float4 debugColor) : COLOR" << std::endl;
133 ss << "{" << std::endl;
134 ss << " return float4(debugColor);" << std::endl;
135 ss << "}" << std::endl;
136
137 // Prepare shader generation
138 Ogre::String programSource = ss.str();
139 Ogre::String programName = materialBaseName +"Shaders/Pixel/Debug/";
140
141#if SO3_DEBUG
142 // Show the generated vertex program in the log.
143 //Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(programSource, Ogre::LML_CRITICAL);
144#endif
145
146 // Create shader object
147 // TODO in another group than Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME?
148 Ogre::HighLevelGpuProgramPtr ptrProgram = Ogre::HighLevelGpuProgramManager::getSingleton().createProgram(programName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "cg", Ogre::GPT_FRAGMENT_PROGRAM);
149 ptrProgram->setSource(programSource);
150 ptrProgram->setParameter("entry_point", "SO3_DeferredLightDebug_ps");
151 ptrProgram->setParameter("profiles", "ps_4_0 ps_2_x arbfp1");
152
153 // Bind our function parameters to Ogre's related values.
154 const Ogre::GpuProgramParametersSharedPtr& params = ptrProgram->getDefaultParameters();
155 if (params->_findNamedConstantDefinition("debugColor"))
156 params->setNamedConstant("debugColor", Ogre::ColourValue(1, 0, 0, 1));
157
158 // Return compositor pixel shader
159 ptrProgram->load();
160 return Ogre::GpuProgramPtr(ptrProgram);
161}
162
163}
std::string GetName() const
virtual Ogre::MaterialPtr GenerateTemplateMaterial(SGBuffer *gbuffer, Ogre::uint32 permutationValue)
unsigned int GetNumberMrt() const
const Ogre::String materialBaseName
Base name of materials generated by this.