TerraForge3D  2.3.1
3D Terrain And Landscape Generator
GLSLHandler.cpp
1#include "Shading/GLSLHandler.h"
2
3#include <sstream>
4#include <string>
5#include <vector>
6#include <iostream>
7#include <unordered_map>
8#include <algorithm>
9
10static std::string dashes = "---------------------------";
11
12#define DASHIT(x) dashes + x + dashes
13
14
15GLSLUniform::GLSLUniform(std::string name, std::string type, std::string value)
16 :name(name), type(type), value(value), comment("")
17{
18}
19
20GLSLUniform::~GLSLUniform()
21{
22}
23
24std::string GLSLUniform::GenerateGLSL()
25{
26 std::stringstream c;
27 c << "uniform " << type << " " << name;
28
29 if(value.size() > 0)
30 {
31 c << " = " << value;
32 }
33
34 c << ";";
35
36 if(comment.size() > 0)
37 {
38 c << " // " << comment;
39 }
40
41 return c.str();
42}
43
44GLSLMacro::GLSLMacro(std::string name, std::string value, std::string comment)
45 :name(name), value(value), comment(comment)
46{
47}
48
49GLSLMacro::~GLSLMacro()
50{
51}
52
53std::string GLSLMacro::GenerateGLSL()
54{
55 std::stringstream c;
56
57 if(comment.size() > 0)
58 {
59 c << "// " << comment << "\n";
60 }
61
62 c << "#define " << name << " " << value;
63 return c.str();
64}
65
66GLSLLine::GLSLLine(std::string line, std::string comment)
67 : line(line), comment(comment)
68{
69}
70
71GLSLLine::~GLSLLine()
72{
73}
74
75std::string GLSLLine::GenerateGLSL()
76{
77 std::stringstream c;
78 c << line;
79
80 if(comment.size() > 0)
81 {
82 c << " // " << comment;
83 }
84
85 return c.str();
86}
87
88GLSLSSBO::GLSLSSBO(std::string name, std::string binding, std::string comment)
89 :name(name), binding(binding), comment(comment)
90{
91 lines.clear();
92}
93
94GLSLSSBO::~GLSLSSBO()
95{
96}
97
98void GLSLSSBO::AddLine(GLSLLine line)
99{
100 lines.push_back(line);
101}
102
103std::string GLSLSSBO::GenerateGLSL()
104{
105 std::stringstream c;
106
107 if(comment.size() > 0)
108 {
109 c << "// " << comment << "\n";
110 }
111
112 c << "layout(std430, binding = " << binding << ") buffer " << name << "\n";
113 c << "{\n";
114
115 for(GLSLLine line : lines)
116 {
117 c << "\t" << line.GenerateGLSL() << "\n";
118 }
119
120 c << "};\n";
121 return c.str();
122}
123
124GLSLFunction::GLSLFunction(std::string name, std::string params, std::string returnType)
125 : name(name), returnType(returnType), params(params), comment("")
126{
127 lines.clear();
128}
129
130GLSLFunction::~GLSLFunction()
131{
132}
133
134std::string GLSLFunction::GenerateGLSL()
135{
136 std::stringstream c;
137
138 if(comment.size() > 0)
139 {
140 c << comment << "\n";
141 }
142
143 c << returnType << " " << name << "(" << params << ")" << "\n";
144 c << "{\n";
145
146 for(GLSLLine &line : lines)
147 {
148 c << "\t" << line.GenerateGLSL() << "\n";
149 }
150
151 c << "}";
152 return c.str();
153}
154
155void GLSLFunction::AddLine(GLSLLine line)
156{
157 lines.push_back(line);
158}
159
160GLSLHandler::GLSLHandler(std::string name)
161 : name(name)
162{
163}
164
165GLSLHandler::~GLSLHandler()
166{
167}
168
169std::string GLSLHandler::GenerateGLSL()
170{
171 code = "";
172 std::stringstream c;
173 c << "#version " << version << "\n\n";
174 c << "// This code has benn generated by TerraForge3D Shader Generator\n";
175 c << "// Shader Name : " << name << "\n";
176 c << "\n\n";
177
178 if(topLines.size() > 0)
179 {
180 c << "// " << DASHIT("MISC") << "\n";
181
182 for(GLSLLine &line : topLines)
183 {
184 c << line.GenerateGLSL() << "\n";
185 }
186
187 c << "\n\n";
188 }
189
190 if(macros.size() > 0)
191 {
192 c << "// " << DASHIT("MACROS") << "\n";
193
194 for(GLSLMacro macro : macros)
195 {
196 c << macro.GenerateGLSL() << "\n";
197 }
198
199 c << "\n\n";
200 }
201
202 if(ssbos.size() > 0)
203 {
204 c << "// " << DASHIT("SSBOS") << "\n";
205
206 for(GLSLSSBO ssbo : ssbos)
207 {
208 c << ssbo.GenerateGLSL() << "\n";
209 }
210
211 c << "\n\n";
212 }
213
214 if(uniforms.size() > 0)
215 {
216 c << "//" << DASHIT("UNIFORMS") << "\n";
217
218 for(GLSLUniform uniform : uniforms)
219 {
220 c << uniform.GenerateGLSL() << "\n";
221 }
222
223 c << "\n\n";
224 }
225
226 if(functions.size() > 0)
227 {
228 c << "//" << DASHIT("FUNCTIONS") << "\n";
229
230 for(GLSLFunction function : functions)
231 {
232 c << "// " << function.name << "\n";
233 c << function.GenerateGLSL() << "\n\n";
234 }
235 }
236
237 code = c.str();
238 return code;
239}
240
241void GLSLHandler::AddTopLine(GLSLLine line)
242{
243 topLines.push_back(line);
244}
245
246void GLSLHandler::AddUniform(GLSLUniform uniform)
247{
248 uniforms.push_back(uniform);
249}
250
251void GLSLHandler::AddMacro(GLSLMacro macro)
252{
253 macros.push_back(macro);
254}
255
256void GLSLHandler::AddSSBO(GLSLSSBO ssbo)
257{
258 ssbos.push_back(ssbo);
259}
260
261void GLSLHandler::AddFunction(GLSLFunction function)
262{
263 if(!HasFunction(function.name))
264 {
265 functions.push_back(function);
266 functionNames.push_back(function.name);
267 }
268}
269
270bool GLSLHandler::HasFunction(std::string name)
271{
272 return std::find(functionNames.begin(), functionNames.end(), name) != functionNames.end();
273}
274
275void GLSLHandler::Clear()
276{
277 uniforms.clear();
278 functions.clear();
279 functionNames.clear();
280 macros.clear();
281 ssbos.clear();
282 topLines.clear();
283 code = "";
284}
@ value
the parser finished reading a JSON value