1 // renderingpipeline.cxx -- description of the cameras needed by the Rembrandt renderer
3 // Written by Curtis Olson, started May 1997.
4 // This file contains parts of main.cxx prior to october 2004
6 // Copyright (C) 1997 - 2012 Curtis L. Olson - http://www.flightgear.org/~curt
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <boost/lexical_cast.hpp>
33 #include <osg/FrameBufferObject> // For texture formats
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
37 #include <simgear/scene/model/modellib.hxx>
38 #include <simgear/scene/material/EffectBuilder.hxx>
39 #include <simgear/scene/material/EffectCullVisitor.hxx>
40 #include <simgear/props/props_io.hxx>
41 #include <simgear/structure/exception.hxx>
42 #include <simgear/math/SGMath.hxx>
44 #include "renderingpipeline.hxx"
45 #include "CameraGroup.hxx"
46 #include <Main/globals.hxx>
47 #include "renderer.hxx"
49 namespace flightgear {
51 FGRenderingPipeline* makeRenderingPipeline(const std::string& name,
52 const simgear::SGReaderWriterOptions* options)
54 std::string fileName(name);
56 std::string absFileName
57 = simgear::SGModelLib::findDataFile(fileName, options);
58 if (absFileName.empty()) {
59 SG_LOG(SG_INPUT, SG_ALERT, "can't find \"" << fileName << "\"");
62 SGPropertyNode_ptr effectProps = new SGPropertyNode();
64 readProperties(absFileName, effectProps.ptr(), 0, true);
66 catch (sg_io_exception& e) {
67 SG_LOG(SG_INPUT, SG_ALERT, "error reading \"" << absFileName << "\": "
68 << e.getFormattedMessage());
72 osg::ref_ptr<FGRenderingPipeline> pipeline = new FGRenderingPipeline;
73 std::vector<SGPropertyNode_ptr> buffers = effectProps->getChildren("buffer");
74 for (int i = 0; i < (int)buffers.size(); ++i) {
75 pipeline->buffers.push_back(new FGRenderingPipeline::Buffer(buffers[i]));
78 std::vector<SGPropertyNode_ptr> stages = effectProps->getChildren("stage");
79 for (int i = 0; i < (int)stages.size(); ++i) {
80 pipeline->stages.push_back(new FGRenderingPipeline::Stage(stages[i]));
83 return pipeline.release();
89 void findAttrOrHex(const simgear::effect::EffectPropertyMap<T>& pMap,
90 const SGPropertyNode* prop,
94 simgear::findAttr(pMap, prop, result);
95 } catch (simgear::effect::BuilderException&) {
96 std::string val = prop->getStringValue();
98 result = boost::lexical_cast<T>(val);
99 } catch (boost::bad_lexical_cast &) {
100 throw simgear::effect::BuilderException(string("findAttrOrHex: could not find attribute ")
106 simgear::effect::EffectNameValue<GLint> internalFormatInit[] =
109 { "rgba8", GL_RGBA8 },
110 { "rgb16", GL_RGB16 },
111 { "rgba16", GL_RGBA16 },
113 { "depth-component24", GL_DEPTH_COMPONENT24 },
114 { "depth-component32", GL_DEPTH_COMPONENT32 }
116 simgear::effect::EffectPropertyMap<GLint> internalFormats(internalFormatInit);
118 simgear::effect::EffectNameValue<GLenum> sourceFormatInit[] =
123 { "depth-component", GL_DEPTH_COMPONENT }
125 simgear::effect::EffectPropertyMap<GLenum> sourceFormats(sourceFormatInit);
127 simgear::effect::EffectNameValue<GLenum> sourceTypeInit[] =
129 { "unsigned-byte", GL_UNSIGNED_BYTE },
130 { "unsigned-short", GL_UNSIGNED_SHORT },
131 { "unsigned-int", GL_UNSIGNED_INT },
132 { "float", GL_FLOAT }
134 simgear::effect::EffectPropertyMap<GLenum> sourceTypes(sourceTypeInit);
136 simgear::effect::EffectNameValue<GLenum> wrapModesInit[] =
139 {"clamp-to-border", GL_CLAMP_TO_BORDER_ARB},
140 {"clamp-to-edge", GL_CLAMP_TO_EDGE},
141 {"mirror", GL_MIRRORED_REPEAT_IBM},
142 {"repeat", GL_REPEAT}
144 simgear::effect::EffectPropertyMap<GLenum> wrapModes(wrapModesInit);
146 FGRenderingPipeline::Buffer::Buffer(SGPropertyNode* prop)
148 SGPropertyNode_ptr nameProp = prop->getChild("name");
149 if (!nameProp.valid()) {
150 throw sg_exception("Buffer name is mandatory");
152 name = nameProp->getStringValue();
153 findAttrOrHex(internalFormats, prop->getChild("internal-format"), internalFormat);
154 findAttrOrHex(sourceFormats, prop->getChild("source-format"), sourceFormat);
155 findAttrOrHex(sourceTypes, prop->getChild("source-type"), sourceType);
156 findAttrOrHex(sourceTypes, prop->getChild("wrap-mode"), wrapMode);
157 SGPropertyNode_ptr widthProp = prop->getChild("width");
158 if (!widthProp.valid())
160 else if (widthProp->getStringValue() == std::string("screen"))
163 width = widthProp->getIntValue();
164 SGPropertyNode_ptr heightProp = prop->getChild("height");
165 if (!heightProp.valid())
167 else if (heightProp->getStringValue() == std::string("screen"))
170 height = heightProp->getIntValue();
172 scaleFactor = prop->getFloatValue("scale-factor", 1.f);
173 shadowComparison = prop->getBoolValue("shadow-comparison", false);
176 FGRenderingPipeline::Stage::Stage(SGPropertyNode* prop)
178 SGPropertyNode_ptr nameProp = prop->getChild("name");
179 if (!nameProp.valid()) {
180 throw sg_exception("Stage name is mandatory");
182 name = nameProp->getStringValue();
183 SGPropertyNode_ptr typeProp = prop->getChild("type");
184 if (!typeProp.valid()) {
185 throw sg_exception("Stage type is mandatory");
187 type = typeProp->getStringValue();
190 FGRenderingPipeline::FGRenderingPipeline()
194 flightgear::CameraInfo* FGRenderingPipeline::buildCamera(flightgear::CameraGroup* cgroup, unsigned flags, osg::Camera* camera,
195 const osg::Matrix& view, const osg::Matrix& projection, osg::GraphicsContext* gc)
197 flightgear::CameraInfo* info = new flightgear::CameraInfo(flags);
198 buildBuffers( info );
200 for (size_t i = 0; i < stages.size(); ++i) {
201 osg::ref_ptr<Stage> stage = stages[i];
202 buildStage(info, stage, cgroup, camera, view, projection, gc);
208 osg::Texture2D* buildDeferredBuffer(GLint internalFormat, GLenum sourceFormat, GLenum sourceType, GLenum wrapMode, bool shadowComparison)
210 osg::Texture2D* tex = new osg::Texture2D;
211 tex->setResizeNonPowerOfTwoHint( false );
212 tex->setInternalFormat( internalFormat );
213 tex->setShadowComparison(shadowComparison);
214 if (shadowComparison) {
215 tex->setShadowTextureMode(osg::Texture2D::LUMINANCE);
216 tex->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
218 tex->setSourceFormat(sourceFormat);
219 tex->setSourceType(sourceType);
220 tex->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR );
221 tex->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR );
222 tex->setWrap( osg::Texture::WRAP_S, (osg::Texture::WrapMode)wrapMode );
223 tex->setWrap( osg::Texture::WRAP_T, (osg::Texture::WrapMode)wrapMode );
227 void FGRenderingPipeline::buildBuffers( flightgear::CameraInfo* info )
229 for (size_t i = 0; i < buffers.size(); ++i) {
230 osg::ref_ptr<Buffer> buffer = buffers[i];
231 info->addBuffer(buffer->name, buildDeferredBuffer( buffer->internalFormat,
232 buffer->sourceFormat,
235 buffer->shadowComparison) );
239 class FGStageCameraCullCallback : public osg::NodeCallback {
241 FGStageCameraCullCallback(FGRenderingPipeline::Stage* s, flightgear::CameraInfo* i) : stage(s), info(i) {}
242 virtual void operator()( osg::Node *n, osg::NodeVisitor *nv) {
243 simgear::EffectCullVisitor* cv = dynamic_cast<simgear::EffectCullVisitor*>(nv);
244 osg::Camera* camera = static_cast<osg::Camera*>(n);
246 cv->clearBufferList();
247 for (flightgear::RenderBufferMap::iterator ii = info->buffers.begin(); ii != info->buffers.end(); ++ii) {
248 cv->addBuffer(ii->first, ii->second.texture );
251 if ( !info->getRenderStageInfo(stage->name).fullscreen )
252 info->setMatrices( camera );
254 cv->traverse( *camera );
258 osg::ref_ptr<FGRenderingPipeline::Stage> stage;
259 flightgear::CameraInfo* info;
262 void FGRenderingPipeline::buildStage(flightgear::CameraInfo* info,
264 flightgear::CameraGroup* cgroup,
266 const osg::Matrix& view,
267 const osg::Matrix& projection,
268 osg::GraphicsContext* gc)
270 osg::ref_ptr<osg::Camera> stageCamera;
271 if (stage->type == "main-camera")
272 stageCamera = camera;
274 stageCamera = new osg::Camera;
276 stageCamera->setName(stage->name);
277 stageCamera->setGraphicsContext(gc);
278 stageCamera->setCullCallback(new FGStageCameraCullCallback(stage, info));
279 if (stage->type != "main-camera")
280 stageCamera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT );
283 void FGRenderingPipeline::buildMainCamera(flightgear::CameraInfo* info,
285 flightgear::CameraGroup* cgroup,
287 const osg::Matrix& view,
288 const osg::Matrix& projection,
289 osg::GraphicsContext* gc)