changed: gcc8 base update
[opensg.git] / Source / System / State / Shader / SHL / testSHLSharedParameters_compat.cpp
blob887e9dc7b6fd5bf6aea27a25e8ca555086e5c9f2
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"
10 #ifdef OSG_1_COMPAT
12 #include "OSGSimpleGeometry.h"
13 #include "OSGGLUT.h"
14 #include "OSGGLUTWindow.h"
15 #include "OSGSimpleSceneManager.h"
16 #include "OSGAction.h"
17 #include "OSGSceneFileHandler.h"
18 #include "OSGBaseFunctions.h"
20 #include "OSGGradientBackground.h"
21 #include "OSGNode.h"
22 #include "OSGGroup.h"
23 #include "OSGTransform.h"
24 #include "OSGPointLight.h"
25 #include "OSGMaterialGroup.h"
27 #include "OSGImage.h"
28 #include "OSGChunkMaterial.h"
29 #include "OSGMaterialChunk.h"
30 #include "OSGSHLChunk.h"
31 #include "OSGSHLParameterChunk.h"
34 // vertex shader program for
35 static std::string _vp_program =
36 "varying vec3 DiffuseColor;\n"
37 "varying vec3 SpecularColor;\n"
38 "\n"
39 "void main(void)\n"
40 "{\n"
41 " vec3 LightColor = vec3(1.0);\n"
42 " vec3 Specular = vec3(1.0);\n"
43 " vec3 Ambient = vec3(0.2);\n"
44 " float Kd = 0.3;\n"
45 "\n"
46 " vec3 LightPosition = gl_LightSource[0].position.xyz;\n"
47 "\n"
48 " vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
49 "\n"
50 " vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);\n"
51 "\n"
52 " vec3 lightVec = normalize(LightPosition - ecPosition);\n"
53 "\n"
54 " vec3 hvec = normalize(lightVec - ecPosition);\n"
55 "\n"
56 " float spec = clamp(dot(hvec, tnorm), 0.0, 1.0);\n"
57 " spec = pow(spec, 16.0);\n"
58 "\n"
59 " DiffuseColor = LightColor * vec3(Kd * dot(lightVec, tnorm));\n"
60 " DiffuseColor = clamp(Ambient + DiffuseColor, 0.0, 1.0);\n"
61 " SpecularColor = clamp((LightColor * Specular * spec), 0.0 ,1.0);\n"
62 "\n"
63 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
64 " gl_Position = ftransform();\n"
65 "}\n";
68 // fragment shader program for
69 static std::string _fp_program =
70 "varying vec3 DiffuseColor;\n"
71 "varying vec3 SpecularColor;\n"
72 "\n"
73 "uniform vec2 Scale;\n"
74 "uniform vec2 Threshold;\n"
75 "uniform vec3 SurfaceColor;\n"
76 "\n"
77 "void main (void)\n"
78 "{\n"
79 " float ss = fract(gl_TexCoord[0].s * Scale.s);\n"
80 " float tt = fract(gl_TexCoord[0].t * Scale.t);\n"
81 "\n"
82 " if ((ss > Threshold.s) && (tt > Threshold.t))\n"
83 " discard;\n"
84 "\n"
85 " vec3 finalColor = SurfaceColor * DiffuseColor + SpecularColor;\n"
86 " gl_FragColor = vec4(finalColor, 1.0);\n"
87 "}\n";
91 // ------------------- global vars ----------------------
93 // The SimpleSceneManager to manage simple applications
94 static OSG::SimpleSceneManagerRefPtr _mgr;
95 // The scene
96 static OSG::NodeRecPtr _scene;
98 static OSG::Int32 _animation = 1;
99 static OSG::SHLParameterChunkRecPtr _shlparameter = NULL;
102 // forward declaration so we can have the interesting stuff upfront
103 int setupGLUT( int *argc, char *argv[] );
105 // Initialize GLUT & OpenSG and set up the scene
106 int doMain(int argc, char **argv)
108 // OSG init
109 OSG::osgInit(argc,argv);
111 // GLUT init
112 int winid = setupGLUT(&argc, argv);
114 // the connection between GLUT and OpenSG
115 OSG::GLUTWindowUnrecPtr gwin = OSG::GLUTWindow::create();
117 gwin->setGlutId(winid);
118 gwin->setSize( 800, 800 );
119 gwin->init();
121 // create root node
122 _scene = OSG::makeCoredNode<OSG::Group>();
124 OSG::GeometryUnrecPtr geo = OSG::makeBoxGeo(0.5, 0.5, 0.5, 1, 1, 1);
126 // share the chunk
127 OSG::SHLChunkUnrecPtr shl = OSG::SHLChunk::create();
129 shl->setVertexProgram(_vp_program);
130 shl->setFragmentProgram(_fp_program);
131 // These parameters are the same for all geometries so we
132 // keep them in here.
133 shl->setUniformParameter("Scale", OSG::Vec2f(20.0f, 20.0f));
134 shl->setUniformParameter("Threshold", OSG::Vec2f(0.7f, 0.7f));
136 OSG::Int32 size = 4;
138 // start color
139 OSG::Vec3f sc(0.0, 0.0, 0.0);
141 // end color
142 OSG::Vec3f ec(1.0, 1.0, 1.0);
144 OSG::Real32 sr = (ec[0] - sc[0]) / OSG::Real32((size*2));
145 OSG::Real32 sg = (ec[1] - sc[1]) / OSG::Real32((size*2));
146 OSG::Real32 sb = (ec[2] - sc[2]) / OSG::Real32((size*2));
148 OSG::Vec3f color(sc);
150 OSG::Int32 x = - size;
151 OSG::Int32 y = - size;
152 OSG::Int32 z = - size;
154 OSG::UInt32 iterations = size*2 * size*2 * size*2;
156 printf("Creating %u cubes ...\n", iterations);
157 for(OSG::UInt32 i=0;i<iterations;++i)
159 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
161 // ok use one SHLChunk and n SHLParameterChunks
162 // Assing a different "SurfaceColor" parameter to each geometry.
163 OSG::SHLParameterChunkUnrecPtr shlparameter =
164 OSG::SHLParameterChunk::create();
166 shlparameter->setSHLChunk(shl);
167 shlparameter->setUniformParameter("SurfaceColor", color);
169 _shlparameter = shlparameter;
171 cmat->addChunk(shl);
172 cmat->addChunk(shlparameter);
174 OSG::TransformUnrecPtr trans;
175 OSG::NodeUnrecPtr trans_node =
176 OSG::makeCoredNode<OSG::Transform>(&trans);
178 trans->editMatrix().setTranslate(OSG::Real32(x),
179 OSG::Real32(y),
180 OSG::Real32(z));
182 OSG::MaterialGroupUnrecPtr mg;
184 OSG::NodeUnrecPtr mg_node = OSG::makeCoredNode<OSG::MaterialGroup>(&mg);
186 mg->setMaterial(cmat);
188 OSG::NodeUnrecPtr geonode = OSG::Node::create();
190 geonode->setCore(geo);
192 mg_node->addChild(geonode);
194 trans_node->addChild(mg_node);
196 // add to scene
197 _scene->addChild(trans_node);
199 // ----
200 ++x;
201 color[0] += sr;
203 if(x == size)
205 x = - size;
206 ++y;
207 color[0] = sc[0];
208 color[1] += sg;
209 if(y == size)
211 y = - size;
212 ++z;
213 color[1] = sc[1];
214 color[2] += sb;
220 // create the SimpleSceneManager helper
221 _mgr = OSG::SimpleSceneManager::create();
223 // tell the manager what to manage
224 _mgr->setWindow(gwin );
225 _mgr->setRoot(_scene);
227 // show the whole scene
228 _mgr->showAll();
230 // create a gradient background.
231 OSG::GradientBackgroundUnrecPtr gback = OSG::GradientBackground::create();
233 gback->clearLines();
234 gback->addLine(OSG::Color3f(0.7, 0.7, 0.8), 0);
235 gback->addLine(OSG::Color3f(0.0, 0.1, 0.3), 1);
237 OSG::Window *win = _mgr->getWindow();
239 for(OSG::UInt32 i=0;i<win->getMFPort()->size();++i)
241 OSG::Viewport *vp = win->getPort(i);
243 vp->setBackground(gback);
246 return 0;
249 // Initialize GLUT & OpenSG and set up the scene
250 int main(int argc, char **argv)
252 if(doMain(argc, argv) != 0)
253 return 1;
255 // GLUT main loop
256 glutMainLoop();
258 return 0;
262 // GLUT callback functions
265 // redraw the window
266 void display(void)
268 OSG::Real64 t = OSG::getSystemTime();
269 // render scene
270 _mgr->redraw();
271 t = OSG::getSystemTime() - t;
272 if(t > 0.0)
273 printf("fps: %f\r", 1.0f / OSG::Real32(t));
274 else
275 printf("fps: very fast ...\r");
278 // react to size changes
279 void reshape(int w, int h)
281 _mgr->resize(w, h);
282 glutPostRedisplay();
285 // react to mouse button presses
286 void mouse(int button, int state, int x, int y)
288 if (state)
289 _mgr->mouseButtonRelease(button, x, y);
290 else
291 _mgr->mouseButtonPress(button, x, y);
293 glutPostRedisplay();
296 // react to mouse motions with pressed buttons
297 void motion(int x, int y)
299 _mgr->mouseMove(x, y);
300 glutPostRedisplay();
303 // react to keys
304 void keyboard(unsigned char k, int x, int y)
306 switch(k)
308 case 27:
309 case 'q':
311 _scene = NULL;
312 _shlparameter = NULL;
313 _mgr = NULL;
315 exit(1);
316 break;
317 case 'w':
318 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
319 printf("wrote scene.osb.gz\n");
320 break;
321 case 'a':
322 _animation = 1 - _animation;
323 break;
324 case 'c':
325 if(_shlparameter != NULL)
327 _shlparameter->setUniformParameter("SurfaceColor",
328 OSG::Vec3f(1.0f,
329 1.0f,
330 1.0f));
332 break;
335 glutPostRedisplay();
338 // setup the GLUT library which handles the windows for us
339 int setupGLUT(int *argc, char *argv[])
341 glutInit(argc, argv);
342 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
344 int winid = glutCreateWindow("OpenSG SHL Shader");
346 glutReshapeFunc(reshape);
347 glutDisplayFunc(display);
348 glutMouseFunc(mouse);
349 glutMotionFunc(motion);
350 glutKeyboardFunc(keyboard);
352 glutIdleFunc(display);
354 return winid;
358 #else
360 int main(int argc, char **argv)
362 return 0;
365 #endif