changed: gcc8 base update
[opensg.git] / Source / System / State / Shader / SHL / testSHLSharedParameters.cpp
blobd9152e046621f32ab1887991842c4ce560953302
1 // OpenSG example: testSHL
2 //
3 // Demonstrates the use of the SHLChunk
4 // Ok this creates 1 CGChunk with different parameter sets for each geometry.
6 // Headers
7 #include "OSGGLUT.h"
8 #include "OSGConfig.h"
9 #include "OSGSimpleGeometry.h"
10 #include "OSGGLUT.h"
11 #include "OSGGLUTWindow.h"
12 #include "OSGSimpleSceneManager.h"
13 #include "OSGAction.h"
14 #include "OSGSceneFileHandler.h"
15 #include "OSGBaseFunctions.h"
17 #include "OSGGradientBackground.h"
18 #include "OSGNode.h"
19 #include "OSGGroup.h"
20 #include "OSGTransform.h"
21 #include "OSGPointLight.h"
22 #include "OSGMaterialGroup.h"
24 #include "OSGImage.h"
25 #include "OSGChunkMaterial.h"
26 #include "OSGMaterialChunk.h"
27 #include "OSGSimpleSHLChunk.h"
28 #include "OSGSimpleSHLVariableChunk.h"
30 // vertex shader program for
31 static std::string _vp_program =
32 "varying vec3 DiffuseColor;\n"
33 "varying vec3 SpecularColor;\n"
34 "\n"
35 "void main(void)\n"
36 "{\n"
37 " vec3 LightColor = vec3(1.0);\n"
38 " vec3 Specular = vec3(1.0);\n"
39 " vec3 Ambient = vec3(0.2);\n"
40 " float Kd = 0.3;\n"
41 "\n"
42 " vec3 LightPosition = gl_LightSource[0].position.xyz;\n"
43 "\n"
44 " vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
45 "\n"
46 " vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);\n"
47 "\n"
48 " vec3 lightVec = normalize(LightPosition - ecPosition);\n"
49 "\n"
50 " vec3 hvec = normalize(lightVec - ecPosition);\n"
51 "\n"
52 " float spec = clamp(dot(hvec, tnorm), 0.0, 1.0);\n"
53 " spec = pow(spec, 16.0);\n"
54 "\n"
55 " DiffuseColor = LightColor * vec3(Kd * dot(lightVec, tnorm));\n"
56 " DiffuseColor = clamp(Ambient + DiffuseColor, 0.0, 1.0);\n"
57 " SpecularColor = clamp((LightColor * Specular * spec), 0.0 ,1.0);\n"
58 "\n"
59 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
60 " gl_Position = ftransform();\n"
61 "}\n";
64 // fragment shader program for
65 static std::string _fp_program =
66 "varying vec3 DiffuseColor;\n"
67 "varying vec3 SpecularColor;\n"
68 "\n"
69 "uniform vec2 Scale;\n"
70 "uniform vec2 Threshold;\n"
71 "uniform vec3 SurfaceColor;\n"
72 "\n"
73 "void main (void)\n"
74 "{\n"
75 " float ss = fract(gl_TexCoord[0].s * Scale.s);\n"
76 " float tt = fract(gl_TexCoord[0].t * Scale.t);\n"
77 "\n"
78 " if ((ss > Threshold.s) && (tt > Threshold.t))\n"
79 " discard;\n"
80 "\n"
81 " vec3 finalColor = SurfaceColor * DiffuseColor + SpecularColor;\n"
82 " gl_FragColor = vec4(finalColor, 1.0);\n"
83 "}\n";
87 // ------------------- global vars ----------------------
89 // The SimpleSceneManager to manage simple applications
90 static OSG::SimpleSceneManagerRefPtr _mgr;
91 // The scene
92 static OSG::NodeRecPtr _scene;
94 static OSG::Int32 _animation = 1;
95 static OSG::SimpleSHLVariableChunkRecPtr _shlparameter = NULL;
98 // forward declaration so we can have the interesting stuff upfront
99 int setupGLUT( int *argc, char *argv[] );
101 // Initialize GLUT & OpenSG and set up the scene
102 int doMain(int argc, char **argv)
104 // OSG init
105 OSG::osgInit(argc,argv);
107 // GLUT init
108 int winid = setupGLUT(&argc, argv);
110 // the connection between GLUT and OpenSG
111 OSG::GLUTWindowUnrecPtr gwin = OSG::GLUTWindow::create();
113 gwin->setGlutId(winid);
114 gwin->setSize( 800, 800 );
115 gwin->init();
117 // create root node
118 _scene = OSG::makeCoredNode<OSG::Group>();
120 OSG::GeometryUnrecPtr geo = OSG::makeBoxGeo(0.5, 0.5, 0.5, 1, 1, 1);
122 // share the chunk
123 OSG::SimpleSHLChunkUnrecPtr shl = OSG::SimpleSHLChunk::create();
125 shl->setVertexProgram(_vp_program);
126 shl->setFragmentProgram(_fp_program);
127 // These parameters are the same for all geometries so we
128 // keep them in here.
129 shl->addUniformVariable("Scale", OSG::Vec2f(20.0f, 20.0f));
130 shl->addUniformVariable("Threshold", OSG::Vec2f(0.7f, 0.7f));
132 OSG::Int32 size = 4;
134 // start color
135 OSG::Vec3f sc(0.0, 0.0, 0.0);
137 // end color
138 OSG::Vec3f ec(1.0, 1.0, 1.0);
140 OSG::Real32 sr = (ec[0] - sc[0]) / OSG::Real32((size*2));
141 OSG::Real32 sg = (ec[1] - sc[1]) / OSG::Real32((size*2));
142 OSG::Real32 sb = (ec[2] - sc[2]) / OSG::Real32((size*2));
144 OSG::Vec3f color(sc);
146 OSG::Int32 x = - size;
147 OSG::Int32 y = - size;
148 OSG::Int32 z = - size;
150 OSG::UInt32 iterations = size*2 * size*2 * size*2;
152 printf("Creating %u cubes ...\n", iterations);
153 for(OSG::UInt32 i=0;i<iterations;++i)
155 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
157 // ok use one SHLChunk and n SHLParameterChunks
158 // Assing a different "SurfaceColor" parameter to each geometry.
159 OSG::SimpleSHLVariableChunkUnrecPtr shlparameter =
160 OSG::SimpleSHLVariableChunk::create();
162 // shlparameter->setSHLChunk(shl);
163 shlparameter->addUniformVariable("SurfaceColor", color);
165 _shlparameter = shlparameter;
167 cmat->addChunk(shl);
168 cmat->addChunk(shlparameter);
170 OSG::TransformUnrecPtr trans;
171 OSG::NodeUnrecPtr trans_node =
172 OSG::makeCoredNode<OSG::Transform>(&trans);
174 trans->editMatrix().setTranslate(OSG::Real32(x),
175 OSG::Real32(y),
176 OSG::Real32(z));
178 OSG::MaterialGroupUnrecPtr mg;
180 OSG::NodeUnrecPtr mg_node = OSG::makeCoredNode<OSG::MaterialGroup>(&mg);
182 mg->setMaterial(cmat);
184 OSG::NodeUnrecPtr geonode = OSG::Node::create();
186 geonode->setCore(geo);
188 mg_node->addChild(geonode);
190 trans_node->addChild(mg_node);
192 // add to scene
193 _scene->addChild(trans_node);
195 // ----
196 ++x;
197 color[0] += sr;
199 if(x == size)
201 x = - size;
202 ++y;
203 color[0] = sc[0];
204 color[1] += sg;
205 if(y == size)
207 y = - size;
208 ++z;
209 color[1] = sc[1];
210 color[2] += sb;
216 // create the SimpleSceneManager helper
217 _mgr = OSG::SimpleSceneManager::create();
219 // tell the manager what to manage
220 _mgr->setWindow(gwin );
221 _mgr->setRoot(_scene);
223 // show the whole scene
224 _mgr->showAll();
226 // create a gradient background.
227 OSG::GradientBackgroundUnrecPtr gback = OSG::GradientBackground::create();
229 gback->clearLines();
230 gback->addLine(OSG::Color3f(0.7f, 0.7f, 0.8f), 0);
231 gback->addLine(OSG::Color3f(0.0f, 0.1f, 0.3f), 1);
233 OSG::Window *win = _mgr->getWindow();
235 for(unsigned int i=0;i<win->getMFPort()->size();++i)
237 OSG::Viewport *vp = win->getPort(i);
238 vp->setBackground(gback);
241 return 0;
244 // Initialize GLUT & OpenSG and set up the scene
245 int main(int argc, char **argv)
247 if(doMain(argc, argv) != 0)
248 return 1;
250 // GLUT main loop
251 glutMainLoop();
253 return 0;
257 // GLUT callback functions
260 // redraw the window
261 void display(void)
263 OSG::Real64 t = OSG::getSystemTime();
264 // render scene
265 _mgr->redraw();
266 t = OSG::getSystemTime() - t;
267 if(t > 0.0)
268 printf("fps: %f\r", 1.0f / OSG::Real32(t));
269 else
270 printf("fps: very fast ...\r");
273 // react to size changes
274 void reshape(int w, int h)
276 _mgr->resize(w, h);
277 glutPostRedisplay();
280 // react to mouse button presses
281 void mouse(int button, int state, int x, int y)
283 if (state)
284 _mgr->mouseButtonRelease(button, x, y);
285 else
286 _mgr->mouseButtonPress(button, x, y);
288 glutPostRedisplay();
291 // react to mouse motions with pressed buttons
292 void motion(int x, int y)
294 _mgr->mouseMove(x, y);
295 glutPostRedisplay();
298 // react to keys
299 void keyboard(unsigned char k, int x, int y)
301 switch(k)
303 case 27:
304 case 'q':
306 _scene = NULL;
307 _shlparameter = NULL;
308 _mgr = NULL;
310 OSG::osgExit();
312 exit(1);
313 break;
314 case 'w':
315 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
316 printf("wrote scene.osb.gz\n");
317 break;
318 case 'a':
319 _animation = 1 - _animation;
320 break;
321 case 'c':
322 if(_shlparameter != NULL)
324 _shlparameter->updateUniformVariable(
325 "SurfaceColor",
326 OSG::Vec3f(1.0f, 1.0f, 1.0f));
328 break;
331 glutPostRedisplay();
334 // setup the GLUT library which handles the windows for us
335 int setupGLUT(int *argc, char *argv[])
337 glutInit(argc, argv);
338 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
340 int winid = glutCreateWindow("OpenSG SHL Shader");
342 glutReshapeFunc(reshape);
343 glutDisplayFunc(display);
344 glutMouseFunc(mouse);
345 glutMotionFunc(motion);
346 glutKeyboardFunc(keyboard);
348 glutIdleFunc(display);
350 return winid;