]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/renderingpipeline.cxx
First round of changes toward a programmable rendering pipeline.
[flightgear.git] / src / Viewer / renderingpipeline.cxx
1 // renderingpipeline.cxx -- description of the cameras needed by the Rembrandt renderer
2 //
3 // Written by Curtis Olson, started May 1997.
4 // This file contains parts of main.cxx prior to october 2004
5 //
6 // Copyright (C) 1997 - 2012  Curtis L. Olson  - http://www.flightgear.org/~curt
7 //
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.
12 //
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.
17 //
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.
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #ifdef HAVE_WINDOWS_H
27 #  include <windows.h>
28 #endif
29
30 #include <boost/lexical_cast.hpp>
31
32 #include <osg/GL>
33 #include <osg/FrameBufferObject> // For texture formats
34
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>
43
44 #include "renderingpipeline.hxx"
45 #include "CameraGroup.hxx"
46 #include <Main/globals.hxx>
47 #include "renderer.hxx"
48
49 namespace flightgear {
50
51 FGRenderingPipeline* makeRenderingPipeline(const std::string& name,
52                    const simgear::SGReaderWriterOptions* options)
53 {
54     std::string fileName = "Effects/" + name;
55     fileName += ".xml";
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 << "\"");
60         return 0;
61     }
62     SGPropertyNode_ptr effectProps = new SGPropertyNode();
63     try {
64         readProperties(absFileName, effectProps.ptr(), 0, true);
65     }
66     catch (sg_io_exception& e) {
67         SG_LOG(SG_INPUT, SG_ALERT, "error reading \"" << absFileName << "\": "
68                << e.getFormattedMessage());
69         return 0;
70     }
71
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]));
76     }
77
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]));
81     }
82
83     return pipeline.release();
84 }
85
86 }
87
88 template<typename T>
89 void findAttrOrHex(const simgear::effect::EffectPropertyMap<T>& pMap,
90               const SGPropertyNode* prop,
91               T& result)
92 {
93     try {
94         simgear::findAttr(pMap, prop, result);
95     } catch (simgear::effect::BuilderException&) {
96         std::string val = prop->getStringValue();
97         try {
98             result = boost::lexical_cast<T>(val);
99         } catch (boost::bad_lexical_cast &) {
100             throw simgear::effect::BuilderException(string("findAttrOrHex: could not find attribute ")
101                                    + string(val));
102         }
103     }
104 }
105
106 const SGPropertyNode* getPropertyNode(const SGPropertyNode* prop)
107 {
108     if (!prop)
109         return 0;
110     if (prop->nChildren() > 0) {
111         const SGPropertyNode* propertyProp = prop->getChild("property");
112         if (!propertyProp)
113             return prop;
114         return globals->get_props()->getNode(propertyProp->getStringValue());
115     }
116     return prop;
117 }
118
119 const SGPropertyNode* getPropertyChild(const SGPropertyNode* prop,
120                                              const char* name)
121 {
122     const SGPropertyNode* child = prop->getChild(name);
123     if (!child)
124         return 0;
125     else
126         return getPropertyNode(child);
127 }
128
129 simgear::effect::EffectNameValue<GLint> internalFormatInit[] =
130 {
131     { "rgb8", GL_RGB8 },
132     { "rgba8", GL_RGBA8 },
133     { "rgb16", GL_RGB16 },
134     { "rgba16", GL_RGBA16 },
135     { "rg16", 0x822C },
136     { "depth-component24", GL_DEPTH_COMPONENT24 },
137     { "depth-component32", GL_DEPTH_COMPONENT32 }
138 };
139 simgear::effect::EffectPropertyMap<GLint> internalFormats(internalFormatInit);
140
141 simgear::effect::EffectNameValue<GLenum> sourceFormatInit[] =
142 {
143     { "rg", 0x8227 },
144     { "rgb", GL_RGB },
145     { "rgba", GL_RGBA },
146     { "depth-component", GL_DEPTH_COMPONENT }
147 };
148 simgear::effect::EffectPropertyMap<GLenum> sourceFormats(sourceFormatInit);
149
150 simgear::effect::EffectNameValue<GLenum> sourceTypeInit[] =
151 {
152     { "unsigned-byte", GL_UNSIGNED_BYTE },
153     { "unsigned-short", GL_UNSIGNED_SHORT },
154     { "unsigned-int", GL_UNSIGNED_INT },
155     { "float", GL_FLOAT }
156 };
157 simgear::effect::EffectPropertyMap<GLenum> sourceTypes(sourceTypeInit);
158
159 simgear::effect::EffectNameValue<GLenum> wrapModesInit[] =
160 {
161     {"clamp", GL_CLAMP},
162     {"clamp-to-border", GL_CLAMP_TO_BORDER_ARB},
163     {"clamp-to-edge", GL_CLAMP_TO_EDGE},
164     {"mirror", GL_MIRRORED_REPEAT_IBM},
165     {"repeat", GL_REPEAT}
166 };
167 simgear::effect::EffectPropertyMap<GLenum> wrapModes(wrapModesInit);
168
169 FGRenderingPipeline::Buffer::Buffer(SGPropertyNode* prop)
170 {
171     SGPropertyNode_ptr nameProp = prop->getChild("name");
172     if (!nameProp.valid()) {
173         throw sg_exception("Buffer name is mandatory");
174     }
175     name = nameProp->getStringValue();
176     findAttrOrHex(internalFormats, prop->getChild("internal-format"), internalFormat);
177     findAttrOrHex(sourceFormats, prop->getChild("source-format"), sourceFormat);
178     findAttrOrHex(sourceTypes, prop->getChild("source-type"), sourceType);
179     findAttrOrHex(wrapModes, prop->getChild("wrap-mode"), wrapMode);
180     SGConstPropertyNode_ptr widthProp = getPropertyChild(prop, "width");
181     if (!widthProp.valid())
182         width = -1;
183     else if (widthProp->getStringValue() == std::string("screen"))
184         width = -1;
185     else {
186         width = widthProp->getIntValue();
187     }
188     SGConstPropertyNode_ptr heightProp = getPropertyChild(prop, "height");
189     if (!heightProp.valid())
190         height = -1;
191     else if (heightProp->getStringValue() == std::string("screen"))
192         height = -1;
193     else
194         height = heightProp->getIntValue();
195
196     scaleFactor = prop->getFloatValue("scale-factor", 1.f);
197     shadowComparison = prop->getBoolValue("shadow-comparison", false);
198 }
199
200 simgear::effect::EffectNameValue<osg::Camera::BufferComponent> componentsInit[] =
201 {
202     {"depth",   osg::Camera::DEPTH_BUFFER},
203     {"stencil", osg::Camera::STENCIL_BUFFER},
204     {"packed-depth-stencil",   osg::Camera::PACKED_DEPTH_STENCIL_BUFFER},
205     {"color0",  osg::Camera::COLOR_BUFFER0},
206     {"color1",  osg::Camera::COLOR_BUFFER1},
207     {"color2",  osg::Camera::COLOR_BUFFER2},
208     {"color3",  osg::Camera::COLOR_BUFFER3}
209 };
210 simgear::effect::EffectPropertyMap<osg::Camera::BufferComponent> components(componentsInit);
211
212 FGRenderingPipeline::Stage::Stage(SGPropertyNode* prop)
213 {
214     SGPropertyNode_ptr nameProp = prop->getChild("name");
215     if (!nameProp.valid()) {
216         throw sg_exception("Stage name is mandatory");
217     }
218     name = nameProp->getStringValue();
219     SGPropertyNode_ptr typeProp = prop->getChild("type");
220     if (!typeProp.valid()) {
221         type = nameProp->getStringValue();
222     } else {
223         type = typeProp->getStringValue();
224     }
225
226     orderNum = prop->getIntValue("order-num", -1);
227
228     effect = prop->getStringValue("effect", "");
229
230     std::vector<SGPropertyNode_ptr> attachments = prop->getChildren("attachment");
231     for (int i = 0; i < (int)attachments.size(); ++i) {
232         this->attachments.push_back(new FGRenderingPipeline::Attachment(attachments[i]));
233     }
234 }
235
236 FGRenderingPipeline::Attachment::Attachment(SGPropertyNode* prop)
237 {
238     simgear::findAttr(components, prop->getChild("component"), component);
239     SGPropertyNode_ptr bufferProp = prop->getChild("buffer");
240     if (!bufferProp.valid()) {
241         throw sg_exception("Attachment buffer is mandatory");
242     }
243     buffer = bufferProp->getStringValue();
244 }
245
246 FGRenderingPipeline::FGRenderingPipeline()
247 {
248 }
249
250 class FGStageCameraCullCallback : public osg::NodeCallback {
251 public:
252     FGStageCameraCullCallback(FGRenderingPipeline::Stage* s, flightgear::CameraInfo* i) : stage(s), info(i) {}
253     virtual void operator()( osg::Node *n, osg::NodeVisitor *nv) {
254         simgear::EffectCullVisitor* cv = dynamic_cast<simgear::EffectCullVisitor*>(nv);
255         osg::Camera* camera = static_cast<osg::Camera*>(n);
256
257         cv->clearBufferList();
258         for (flightgear::RenderBufferMap::iterator ii = info->buffers.begin(); ii != info->buffers.end(); ++ii) {
259             cv->addBuffer(ii->first, ii->second.texture );
260         }
261
262                 if ( !info->getRenderStageInfo(stage->name).fullscreen )
263                         info->setMatrices( camera );
264
265         cv->traverse( *camera );
266     }
267
268 private:
269     osg::ref_ptr<FGRenderingPipeline::Stage> stage;
270     flightgear::CameraInfo* info;
271 };