changed: gcc8 base update
[opensg.git] / Source / System / State / Shader / SHL / testSHLLights_compat.cpp
blobf4f0242b7cd3aa6308dec93048d650a57547e66e
1 // OpenSG example: testSHL
2 //
3 // Demonstrates the use of the SHLChunk
4 // Implements a simple bumpmapping via vertex and fragment shader.
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 "OSGNode.h"
21 #include "OSGGroup.h"
22 #include "OSGTransform.h"
23 #include "OSGPointLight.h"
25 #include "OSGImage.h"
26 #include "OSGChunkMaterial.h"
27 #include "OSGMaterialChunk.h"
28 #include "OSGSHLChunk.h"
31 // vertex shader program for bump mapping in surface local coordinates
32 static std::string _vp_program =
33 "uniform bool OSGLight0Active;\n"
34 "uniform bool OSGLight1Active;\n"
35 "uniform bool OSGLight2Active;\n"
36 "vec4 Ambient;\n"
37 "vec4 Diffuse;\n"
38 "vec4 Specular;\n"
39 "\n"
40 "\n"
41 " void pointLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3)\n"
42 " {\n"
43 " float nDotVP; // normal . light direction\n"
44 " float nDotHV; // normal . light half vector\n"
45 " float pf; // power factor\n"
46 " float attenuation; // computed attenuation factor\n"
47 " float d; // distance from surface to light source\n"
48 " vec3 VP; // direction from surface to light position\n"
49 " vec3 halfVector; // direction of maximum highlights\n"
50 "\n"
51 " // Compute vector from surface to light position\n"
52 " VP = vec3 (gl_LightSource[i].position) - ecPosition3;\n"
53 "\n"
54 " // Compute distance between surface and light position\n"
55 " d = length(VP);\n"
56 "\n"
57 " // Normalize the vector from surface to light position\n"
58 " VP = normalize(VP);\n"
59 "\n"
60 " // Compute attenuation\n"
61 " attenuation = 1.0 / (gl_LightSource[i].constantAttenuation +\n"
62 " gl_LightSource[i].linearAttenuation * d +\n"
63 " gl_LightSource[i].quadraticAttenuation * d * d);\n"
64 " halfVector = normalize(VP + eye);\n"
65 " nDotVP = max(0.0, dot(normal, VP));\n"
66 " nDotHV = max(0.0, dot(normal, halfVector));\n"
67 "\n"
68 " if (nDotVP == 0.0)\n"
69 " pf = 0.0;\n"
70 " else\n"
71 " pf = pow(nDotHV, gl_FrontMaterial.shininess);\n"
72 "\n"
73 " Ambient += gl_LightSource[i].ambient * attenuation;\n"
74 " Diffuse += gl_LightSource[i].diffuse * nDotVP * attenuation;\n"
75 " Specular += gl_LightSource[i].specular * pf * attenuation;\n"
76 " }\n"
77 "\n"
78 " vec3 fnormal(void)\n"
79 " {\n"
80 " //Compute the normal\n"
81 " vec3 normal = gl_NormalMatrix * gl_Normal;\n"
82 " normal = normalize(normal);\n"
83 " return normal;\n"
84 " }\n"
85 "\n"
86 " void flight(in vec3 normal, in vec4 ecPosition, float alphaFade)\n"
87 " {\n"
88 " vec4 color;\n"
89 " vec3 ecPosition3;\n"
90 " vec3 eye;\n"
91 "\n"
92 " ecPosition3 = (vec3 (ecPosition)) / ecPosition.w;\n"
93 " eye = vec3 (0.0, 0.0, 1.0);\n"
94 "\n"
95 " // Clear the light intensity accumulators\n"
96 " Ambient = vec4 (0.0);\n"
97 " Diffuse = vec4 (0.0);\n"
98 " Specular = vec4 (0.0);\n"
99 "\n"
100 " if(OSGLight0Active)\n"
101 " pointLight(0, normal, eye, ecPosition3);\n"
102 "\n"
103 " if(OSGLight1Active)\n"
104 " pointLight(1, normal, eye, ecPosition3);\n"
105 "\n"
106 " if(OSGLight2Active)\n"
107 " pointLight(2, normal, eye, ecPosition3);\n"
108 "\n"
109 " color = gl_FrontLightModelProduct.sceneColor +\n"
110 " Ambient * gl_FrontMaterial.ambient +\n"
111 " Diffuse * gl_FrontMaterial.diffuse;\n"
112 " color += Specular * gl_FrontMaterial.specular;\n"
113 " color = clamp( color, 0.0, 1.0 );\n"
114 " gl_FrontColor = color;\n"
115 " gl_FrontColor.a *= alphaFade;\n"
116 "\n"
117 " }\n"
118 "\n"
119 " void main(void)\n"
120 " {\n"
121 " vec3 transformedNormal;\n"
122 " float alphaFade = 1.0;\n"
123 "\n"
124 " // Eye-coordinate position of vertex, needed in various calculations\n"
125 " vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;\n"
126 "\n"
127 " // Do fixed functionality vertex transform\n"
128 " gl_Position = ftransform();\n"
129 " transformedNormal = fnormal();\n"
130 " flight(transformedNormal, ecPosition, alphaFade);\n"
131 " }\n";
133 // fragment shader program for bump mapping in surface local coordinates
134 static std::string _fp_program =
135 "void main (void)\n"
136 "{\n"
137 " vec4 color;\n"
138 " color = gl_Color;\n"
139 " gl_FragColor = color;\n"
140 "}\n";
143 // ------------------- global vars ----------------------
145 // The SimpleSceneManager to manage simple applications
146 static OSG::SimpleSceneManagerRefPtr _mgr;
147 // The scene
148 static OSG::NodeRecPtr _scene;
149 static OSG::PointLightRecPtr _point1_core;
150 static OSG::PointLightRecPtr _point2_core;
151 static OSG::PointLightRecPtr _point3_core;
152 static OSG::SHLChunkUnrecPtr shl;
155 // forward declaration so we can have the interesting stuff upfront
156 int setupGLUT( int *argc, char *argv[] );
158 // Shows how to add your own parameter callbacks.
160 typedef void (OSG_APIENTRY *OSGGLUNIFORMMATRIXFVARBPROC)(GLint location,
161 GLsizei count,
162 GLboolean transpose,
163 GLfloat *value);
165 static
166 void updateSpecialParameter(
167 OSG::SHLChunk::GetUniformLocProc getUniformLocation,
168 OSG::DrawEnv *action,
169 GLuint program )
171 #if 0
172 if(action->getCamera() == NULL || action->getViewport() == NULL)
174 FWARNING(("updateSpecialParameter : Can't update OSGSpecialParameter"
175 "parameter, camera or viewport is NULL!\n"));
176 return;
179 // uploads the camera orientation.
180 Matrix m;
181 action->getCamera()->getViewing(m,
182 action->getViewport()->getPixelWidth(),
183 action->getViewport()->getPixelHeight());
184 m.invert();
185 m[3].setValues(0, 0, 0, 1);
187 //std::cout << "uploading matrix " << m << std::endl;
189 // get "glUniformMatrix4fvARB" function pointer
190 OSGGLUNIFORMMATRIXFVARBPROC uniformMatrix4fv =
191 reinterpret_cast<OSGGLUNIFORMMATRIXFVARBPROC>(
192 action->getWindow()->getFunction(
193 SHLChunk::getFuncUniformMatrix4fv()));
195 GLint location = getUniformLocation(program, "OSGSpecialParameter");
197 if(location != -1)
198 uniformMatrix4fv(location, 1, GL_FALSE, m.getValues());
199 #endif
202 // Initialize GLUT & OpenSG and set up the scene
203 int doMain(int argc, char **argv)
205 printf("Press key '1', '2', or '3' to toggle the light sources.\n");
206 // OSG init
207 OSG::osgInit(argc,argv);
209 // GLUT init
210 int winid = setupGLUT(&argc, argv);
212 // the connection between GLUT and OpenSG
213 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
215 gwin->setGlutId(winid);
216 gwin->setSize( 800, 800 );
217 gwin->init();
219 // Create the shader material
221 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
223 OSG::MaterialChunkUnrecPtr matc = OSG::MaterialChunk::create();
225 matc->setAmbient(OSG::Color4f(0.1, 0.1, 0.1, 1.0));
226 matc->setDiffuse(OSG::Color4f(0.3, 0.3, 0.3, 1.0));
227 matc->setSpecular(OSG::Color4f(0.8, 0.8, 0.8, 1.0));
228 matc->setShininess(100);
229 matc->setLit(true);
231 shl = OSG::SHLChunk::create();
233 shl->setVertexProgram(_vp_program);
234 shl->setFragmentProgram(_fp_program);
235 shl->setUniformParameter("OSGLight0Active", 0);
236 shl->setUniformParameter("OSGLight1Active", 0);
237 shl->setUniformParameter("OSGLight2Active", 0);
238 shl->setUniformParameter("OSGViewMatrix", 0);
239 // The OSGSpecialParameter is not used in the shader just shows
240 // how to add your own parameter callbacks!
241 shl->addParameterCallback(
242 "OSGSpecialParameter",
243 OSG::SHLChunk::ParamFunctor(updateSpecialParameter));
245 cmat->addChunk(matc);
246 cmat->addChunk(shl);
248 // create root node
249 _scene = OSG::Node::create();
251 // create two light sources.
253 OSG::TransformUnrecPtr point1_trans;
255 OSG::NodeUnrecPtr point1 =
256 OSG::makeCoredNode<OSG::PointLight>(&_point1_core);
257 OSG::NodeUnrecPtr point1_beacon =
258 OSG::makeCoredNode<OSG::Transform >(&point1_trans);
260 _scene->addChild(point1_beacon);
262 point1_trans->editMatrix().setTranslate(-10.0, 5.0, 5.0);
264 _point1_core->setAmbient(0.0f, 0.0f, 0.0f , 1.0f);
265 _point1_core->setDiffuse(1.0f, 0.0f, 0.0f, 1.0f);
266 _point1_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
267 _point1_core->setBeacon(point1_beacon);
268 _point1_core->setOn(true);
271 OSG::TransformUnrecPtr point2_trans;
273 OSG::NodeUnrecPtr point2 =
274 OSG::makeCoredNode<OSG::PointLight>(&_point2_core);
275 OSG::NodeUnrecPtr point2_beacon =
276 OSG::makeCoredNode<OSG::Transform >(&point2_trans);
278 _scene->addChild(point2_beacon);
280 point2_trans->editMatrix().setTranslate(10.0, 5.0, 5.0);
282 _point2_core->setAmbient(0.0f, 0.0f, 0.0f, 1.0f);
283 _point2_core->setDiffuse(0.0f, 1.0f, 0.0f, 1.0f);
284 _point2_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
285 _point2_core->setBeacon(point2_beacon);
286 _point2_core->setOn(true);
288 point1->addChild(point2);
290 OSG::TransformUnrecPtr point3_trans;
292 OSG::NodeUnrecPtr point3 =
293 OSG::makeCoredNode<OSG::PointLight>(&_point3_core);
294 OSG::NodeUnrecPtr point3_beacon =
295 OSG::makeCoredNode<OSG::Transform >(&point3_trans);
297 _scene->addChild(point3_beacon);
299 point3_trans->editMatrix().setTranslate(0.0, -12.0, 5.0);
301 _point3_core->setAmbient(0.0f, 0.0f, 0.0f, 1.0f);
302 _point3_core->setDiffuse(0.5f, 0.0f, 1.0f, 1.0f);
303 _point3_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
304 _point3_core->setBeacon(point3_beacon);
305 _point3_core->setOn(true);
307 point2->addChild(point3);
310 // create a sphere.
311 OSG::GeometryUnrecPtr geo = OSG::makeLatLongSphereGeo (100, 100, 1.0);
313 geo->setMaterial(cmat);
316 OSG::NodeUnrecPtr sphere = OSG::makeNodeFor(geo);
318 point3->addChild(sphere);
321 _scene->setCore(OSG::Group::create());
322 _scene->addChild(point1);
324 // create the SimpleSceneManager helper
325 _mgr = OSG::SimpleSceneManager::create();
327 // tell the manager what to manage
328 _mgr->setWindow(gwin );
329 _mgr->setRoot(_scene);
331 _mgr->turnHeadlightOff();
333 // show the whole scene
334 _mgr->showAll();
336 // enable local lights.
337 // OSG::RenderAction *ract =
338 // dynamic_cast<OSG::RenderAction *>(_mgr->getRenderAction());
340 // ract->setLocalLights(true);
342 return 0;
345 // Initialize GLUT & OpenSG and set up the scene
346 int main(int argc, char **argv)
348 if(doMain(argc, argv) != 0)
349 return 1;
351 // GLUT main loop
352 glutMainLoop();
354 return 0;
358 // GLUT callback functions
361 // redraw the window
362 void display(void)
364 // render scene
365 _mgr->redraw();
368 // react to size changes
369 void reshape(int w, int h)
371 _mgr->resize(w, h);
372 glutPostRedisplay();
375 // react to mouse button presses
376 void mouse(int button, int state, int x, int y)
378 if (state)
379 _mgr->mouseButtonRelease(button, x, y);
380 else
381 _mgr->mouseButtonPress(button, x, y);
383 glutPostRedisplay();
386 // react to mouse motions with pressed buttons
387 void motion(int x, int y)
389 _mgr->mouseMove(x, y);
390 glutPostRedisplay();
393 // react to keys
394 void keyboard(unsigned char k, int x, int y)
396 switch(k)
398 case 27:
399 case 'q':
400 _scene = NULL;
401 _point1_core = NULL;
402 _point2_core = NULL;
403 _point3_core = NULL;
405 shl = NULL;
407 _mgr = NULL;
409 exit(1);
410 break;
411 case 'w':
412 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
413 printf("wrote scene.osb.gz\n");
414 break;
415 case '1':
417 if(_point1_core->getOn() == false)
418 _point1_core->setOn(true);
419 else
420 _point1_core->setOn(false);
421 break;
424 case '2':
426 if(_point2_core->getOn() == false)
427 _point2_core->setOn(true);
428 else
429 _point2_core->setOn(false);
430 break;
433 case '3':
435 if(_point3_core->getOn() == false)
436 _point3_core->setOn(true);
437 else
438 _point3_core->setOn(false);
439 break;
442 case 'i':
443 shl->subUniformParameter("OSGLight0Active");
444 break;
445 case 'I':
446 shl->setUniformParameter("OSGLight0Active", 0);
447 break;
448 case 'o':
449 shl->subUniformParameter("OSGLight1Active");
450 break;
451 case 'O':
452 shl->setUniformParameter("OSGLight1Active", 0);
453 break;
454 case 'p':
455 shl->subUniformParameter("OSGLight2Active");
456 break;
457 case 'P':
458 shl->setUniformParameter("OSGLight2Active", 0);
459 break;
462 glutPostRedisplay();
465 // setup the GLUT library which handles the windows for us
466 int setupGLUT(int *argc, char *argv[])
468 glutInit(argc, argv);
469 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
471 int winid = glutCreateWindow("OpenSG CG Shader");
473 glutReshapeFunc(reshape);
474 glutDisplayFunc(display);
475 glutMouseFunc(mouse);
476 glutMotionFunc(motion);
477 glutKeyboardFunc(keyboard);
479 return winid;
482 #else
484 int main(int argc, char **argv)
486 return 0;
489 #endif