Privacy
An open-source, flexible 3D physical simulation framework
yaml-shader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011, 2016, DFKI GmbH Robotics Innovation Center
3  *
4  * This file is part of the MARS simulation framework.
5  *
6  * MARS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3
9  * of the License, or (at your option) any later version.
10  *
11  * MARS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with MARS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include <sstream>
22 #include <iostream>
23 #include <fstream>
24 #include "yaml-shader.h"
25 
26 namespace osg_material_manager {
27 
28  using namespace std;
29  using namespace configmaps;
30 
31  YamlShader::YamlShader(string name, vector<std::string> &args, ConfigMap &map, string resPath)
32  : ShaderFunc(name, args) {
33  if (map.hasKey("source")) {
34  string path = resPath+(string)map["source"];
35  ifstream t(path.c_str());
36  stringstream buffer;
37  buffer << t.rdbuf();
38  source = buffer.str();
39  } else {
40  source = "";
41  }
42 
43  if (map.hasKey("priority")) {
44  funcs[0].priority = (int)map["priority"];
45  } else {
46  funcs[0].priority = 0;
47  }
48 
49  if (map.hasKey("params")) {
50  ConfigVector::iterator it = map["params"].begin();
51  for (;it!=map["params"].end();it++) {
52  funcs[0].arguments.push_back(it.base()->getString());
53  }
54  }
55 
56  if (map.hasKey("varyings")) {
57  ConfigMap::iterator it = map["varyings"].beginMap();
58  for (;it!=map["varyings"].endMap();it++) {
59  string type = it->first;
60  std::size_t a_pos = type.find("[]"); // to determine if value is array or not
61  ConfigVector::iterator it2 = map["varyings"][type].begin();
62  for (;it2!=map["varyings"][type].end();it2++) {
63  ConfigItem &item = *it2;
64  std::stringstream s;
65  string typeName; // should contain type without [] if present at the end
66  if (a_pos != string::npos) {
67  typeName = type.substr(0,a_pos);
68  if (item.hasKey("arraySize")) {
69  string size = map["mappings"][(string)item["arraySize"]];
70  s << "[" << size << "]";
71  } else {
72  s << "[1]"; // fallback arraySize of 1
73  }
74  } else {
75  typeName = type;
76  s << "";
77  }
78  addVarying( (GLSLVarying) { typeName, (string)item["name"] + s.str() } );
79  }
80  }
81  }
82 
83  if (map.hasKey("uniforms")) {
84  ConfigMap::iterator it = map["uniforms"].beginMap();
85  for (;it!=map["uniforms"].endMap();it++) {
86  string type = it->first;
87  std::size_t a_pos = type.find("[]"); // to determine if value is array or not
88  ConfigVector::iterator it2 = map["uniforms"][type].begin();
89  for (;it2!=map["uniforms"][type].end();it2++) {
90  ConfigItem &item = *it2;
91  std::stringstream s;
92  string typeName; // should contain type without [] if present at the end
93  if (a_pos != string::npos) {
94  typeName = type.substr(0,a_pos);
95  if (item.hasKey("arraySize")) {
96  string size = map["mappings"][(string)item["arraySize"]];
97  s << "[" << size << "]";
98  } else {
99  s << "[1]"; // fallback arraySize of 1
100  }
101  } else {
102  typeName = type;
103  s << "";
104  }
105  addUniform( (GLSLUniform) { typeName, (string)item["name"] + s.str() } );
106  }
107  }
108  }
109 
110  if (map.hasKey("attributes")) {
111  ConfigMap::iterator it = map["attributes"].beginMap();
112  for (;it!=map["attributes"].endMap();it++) {
113  string type = it->first;
114  std::size_t a_pos = type.find("[]"); // to determine if value is array or not
115  ConfigVector::iterator it2 = map["attributes"][type].begin();
116  for (;it2!=map["attributes"][type].end();it2++) {
117  ConfigItem &item = *it2;
118  std::stringstream s;
119  string typeName; // should contain type without [] if present at the end
120  if (a_pos != string::npos) {
121  typeName = type.substr(0,a_pos);
122  if (item.hasKey("arraySize")) {
123  string size = map["mappings"][(string)item["arraySize"]];
124  s << "[" << size << "]";
125  } else {
126  s << "[1]"; // fallback arraySize of 1
127  }
128  } else {
129  typeName = type;
130  s << "";
131  }
132  addAttribute( (GLSLAttribute) { typeName, (string)item["name"] + s.str() } );
133  }
134  }
135  }
136 
137  if (map.hasKey("exports")) {
138  ConfigVector::iterator it = map["exports"].begin();
139  for (;it!=map["exports"].end();it++) {
140  ConfigItem &item = *it;
141  addExport( (GLSLExport) { (string)item["name"], (string)item["value"] } );
142  }
143  }
144 
145  if (map.hasKey("mainVarDecs")) {
146  ConfigMap::iterator it = map["mainVarDecs"].beginMap();
147  for (;it!=map["mainVarDecs"].endMap();it++) {
148  string type = it->first;
149  std::size_t a_pos = type.find("[]"); // to determine if value is array or not
150  ConfigVector::iterator it2 = map["mainVarDecs"][type].begin();
151  for (;it2!=map["mainVarDecs"][type].end();it2++) {
152  ConfigItem &item = *it2;
153  std::stringstream s;
154  string typeName;
155  if (a_pos != string::npos) {
156  typeName = type.substr(0,a_pos);
157  if (item.hasKey("arraySize")) {
158  string size = map["mappings"][(string)item["arraySize"]];
159  s << "[" << size << "]";
160  } else {
161  s << "[1]"; // fallback arraySize of 1
162  }
163  } else {
164  typeName = type;
165  s << "";
166  }
167  addMainVarDec((GLSLAttribute) {typeName, (string)item["name"]});
168  }
169  }
170  }
171 
172  if (map.hasKey("mainVars")) {
173  ConfigMap::iterator it = map["mainVars"].beginMap();
174  for (;it!=map["mainVars"].endMap();it++) {
175  string type = it->first;
176  std::size_t a_pos = type.find("[]"); // to determine if value is array or not
177  ConfigVector::iterator it2 = map["mainVars"][type].begin();
178  for (;it2!=map["mainVars"][type].end();it2++) {
179  ConfigItem &item = *it2;
180  std::stringstream s;
181  string typeName; // should contain type without [] if present at the end
182  if (a_pos != string::npos) {
183  typeName = type.substr(0,a_pos);
184  if (item.hasKey("arraySize")) {
185  string size = map["mappings"][(string)item["arraySize"]];
186  s << "[" << size << "]";
187  } else {
188  s << "[1]"; // fallback arraySize of 1
189  }
190  } else {
191  typeName = type;
192  s << "";
193  }
194  int priority = funcs[0].priority;
195  if (item.hasKey("priority")) {
196  priority = (int)item["priority"];
197  }
198  addMainVar( (GLSLVariable) { typeName, (string)item["name"], (string)item["value"] }, priority);
199  }
200  }
201  }
202 
203  if (map.hasKey("snippets")) {
204  ConfigVector::iterator it = map["snippets"].begin();
205  for (;it!=map["snippets"].end();it++) {
206  ConfigItem &item = *it;
207  string snippet = "";
208  if (item.hasKey("source")) {
209  string path = resPath+(string)item["source"];
210  ifstream t(path.c_str());
211  stringstream buffer;
212  buffer << t.rdbuf();
213  snippet = buffer.str();
214  }
215  int priority = funcs[0].priority;
216  if (item.hasKey("priority")) {
217  priority = (int)item["priority"];
218  }
219  addSnippet(snippet, priority);
220  }
221  }
222  }
223 
224  std::string YamlShader::code() const {
225  return source;
226  }
227 
228 }
void addAttribute(GLSLAttribute att)
Definition: tsort.c:44
void addVarying(GLSLVarying varying)
void addMainVar(GLSLVariable var, int priority=0)
Base object of the configmaps library!
Definition: ConfigItem.hpp:58
std::list< FIFOItem< std::string, ConfigItem > >::iterator iterator
Definition: FIFOMap.h:57
void addUniform(GLSLUniform uniform)
bool hasKey(std::string key)
Definition: ConfigItem.cpp:227
void addMainVarDec(GLSLAttribute att)
bool hasKey(std::string key)
Definition: ConfigMap.cpp:131
std::vector< FunctionCall > funcs
YamlShader(std::string name, std::vector< std::string > &args, configmaps::ConfigMap &map, std::string resPath)
Definition: yaml-shader.cpp:31
void addSnippet(std::string line, int priority=0)