changed: gcc8 base update
[opensg.git] / Source / System / State / Shader / SHL / testSHLProcLights.cpp
blobe8aa66d28a29e013360982a1a5b1163a6b1b2644
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"
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 "OSGNode.h"
18 #include "OSGGroup.h"
19 #include "OSGTransform.h"
20 #include "OSGPointLight.h"
22 #include "OSGImage.h"
23 #include "OSGChunkMaterial.h"
24 #include "OSGMaterialChunk.h"
25 #include "OSGSimpleSHLChunk.h"
26 #include "OSGGLFuncProtos.h"
28 // vertex shader program for bump mapping in surface local coordinates
29 static std::string _vp_program =
30 "uniform bool Light0Active;\n"
31 "uniform bool Light1Active;\n"
32 "uniform bool Light2Active;\n"
33 "vec4 Ambient;\n"
34 "vec4 Diffuse;\n"
35 "vec4 Specular;\n"
36 "\n"
37 "\n"
38 " void pointLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3)\n"
39 " {\n"
40 " float nDotVP; // normal . light direction\n"
41 " float nDotHV; // normal . light half vector\n"
42 " float pf; // power factor\n"
43 " float attenuation; // computed attenuation factor\n"
44 " float d; // distance from surface to light source\n"
45 " vec3 VP; // direction from surface to light position\n"
46 " vec3 halfVector; // direction of maximum highlights\n"
47 "\n"
48 " // Compute vector from surface to light position\n"
49 " VP = vec3 (gl_LightSource[i].position) - ecPosition3;\n"
50 "\n"
51 " // Compute distance between surface and light position\n"
52 " d = length(VP);\n"
53 "\n"
54 " // Normalize the vector from surface to light position\n"
55 " VP = normalize(VP);\n"
56 "\n"
57 " // Compute attenuation\n"
58 " attenuation = 1.0 / (gl_LightSource[i].constantAttenuation +\n"
59 " gl_LightSource[i].linearAttenuation * d +\n"
60 " gl_LightSource[i].quadraticAttenuation * d * d);\n"
61 " halfVector = normalize(VP + eye);\n"
62 " nDotVP = max(0.0, dot(normal, VP));\n"
63 " nDotHV = max(0.0, dot(normal, halfVector));\n"
64 "\n"
65 " if (nDotVP == 0.0)\n"
66 " pf = 0.0;\n"
67 " else\n"
68 " pf = pow(nDotHV, gl_FrontMaterial.shininess);\n"
69 "\n"
70 " Ambient += gl_LightSource[i].ambient * attenuation;\n"
71 " Diffuse += gl_LightSource[i].diffuse * nDotVP * attenuation;\n"
72 " Specular += gl_LightSource[i].specular * pf * attenuation;\n"
73 " }\n"
74 "\n"
75 " vec3 fnormal(void)\n"
76 " {\n"
77 " //Compute the normal\n"
78 " vec3 normal = gl_NormalMatrix * gl_Normal;\n"
79 " normal = normalize(normal);\n"
80 " return normal;\n"
81 " }\n"
82 "\n"
83 " void flight(in vec3 normal, in vec4 ecPosition, float alphaFade)\n"
84 " {\n"
85 " vec4 color;\n"
86 " vec3 ecPosition3;\n"
87 " vec3 eye;\n"
88 "\n"
89 " ecPosition3 = (vec3 (ecPosition)) / ecPosition.w;\n"
90 " eye = vec3 (0.0, 0.0, 1.0);\n"
91 "\n"
92 " // Clear the light intensity accumulators\n"
93 " Ambient = vec4 (0.0);\n"
94 " Diffuse = vec4 (0.0);\n"
95 " Specular = vec4 (0.0);\n"
96 "\n"
97 " if(Light0Active)\n"
98 " pointLight(0, normal, eye, ecPosition3);\n"
99 "\n"
100 " if(Light1Active)\n"
101 " pointLight(1, normal, eye, ecPosition3);\n"
102 "\n"
103 " if(Light2Active)\n"
104 " pointLight(2, normal, eye, ecPosition3);\n"
105 "\n"
106 " color = gl_FrontLightModelProduct.sceneColor +\n"
107 " Ambient * gl_FrontMaterial.ambient +\n"
108 " Diffuse * gl_FrontMaterial.diffuse;\n"
109 " color += Specular * gl_FrontMaterial.specular;\n"
110 " color = clamp( color, 0.0, 1.0 );\n"
111 " gl_FrontColor = color;\n"
112 " gl_FrontColor.a *= alphaFade;\n"
113 "\n"
114 " }\n"
115 "\n"
116 " void main(void)\n"
117 " {\n"
118 " vec3 transformedNormal;\n"
119 " float alphaFade = 1.0;\n"
120 "\n"
121 " // Eye-coordinate position of vertex, needed in various calculations\n"
122 " vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;\n"
123 "\n"
124 " // Do fixed functionality vertex transform\n"
125 " gl_Position = ftransform();\n"
126 " transformedNormal = fnormal();\n"
127 " flight(transformedNormal, ecPosition, alphaFade);\n"
128 " }\n";
130 // fragment shader program for bump mapping in surface local coordinates
131 static std::string _fp_program =
132 "void main (void)\n"
133 "{\n"
134 " vec4 color;\n"
135 " color = gl_Color;\n"
136 " gl_FragColor = color;\n"
137 "}\n";
140 // ------------------- global vars ----------------------
142 // The SimpleSceneManager to manage simple applications
143 static OSG::SimpleSceneManagerRefPtr _mgr;
144 // The scene
145 static OSG::NodeRecPtr _scene;
146 static OSG::PointLightRecPtr _point1_core;
147 static OSG::PointLightRecPtr _point2_core;
148 static OSG::PointLightRecPtr _point3_core;
150 OSG::NodeUnrecPtr point1_beacon;
151 OSG::NodeUnrecPtr point2_beacon;
152 OSG::NodeUnrecPtr point3_beacon;
154 // forward declaration so we can have the interesting stuff upfront
155 int setupGLUT( int *argc, char *argv[] );
157 // Shows how to add your own parameter callbacks.
159 static void light0Active(OSG::DrawEnv *pEnv, OSG::Int32 iLoc)
161 if(iLoc != -1)
163 OSGGETGLFUNC_EXT(glUniform1i,
164 osgGlUniform1i,
165 OSG::ShaderProgram::getFuncIdUniform1i());
167 osgGlUniform1i(iLoc,
168 GLint(pEnv->getLightState() & 0x0001));
172 static void light1Active(OSG::DrawEnv *pEnv, OSG::Int32 iLoc)
174 if(iLoc != -1)
176 OSGGETGLFUNC_EXT(glUniform1i,
177 osgGlUniform1i,
178 OSG::ShaderProgram::getFuncIdUniform1i());
180 osgGlUniform1i(iLoc,
181 GLint(pEnv->getLightState() & 0x0002));
185 static void light2Active(OSG::DrawEnv *pEnv, OSG::Int32 iLoc)
187 if(iLoc != -1)
189 OSGGETGLFUNC_EXT(glUniform1i,
190 osgGlUniform1i,
191 OSG::ShaderProgram::getFuncIdUniform1i());
193 osgGlUniform1i(iLoc,
194 GLint(pEnv->getLightState() & 0x0004));
198 // Initialize GLUT & OpenSG and set up the scene
199 int doMain(int argc, char **argv)
201 printf("Press key '1', '2', or '3' to toggle the light sources.\n");
202 // OSG init
203 OSG::osgInit(argc,argv);
205 // GLUT init
206 int winid = setupGLUT(&argc, argv);
208 // the connection between GLUT and OpenSG
209 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
211 gwin->setGlutId(winid);
212 gwin->setSize( 800, 800 );
213 gwin->init();
215 // Create the shader material
217 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
219 OSG::MaterialChunkUnrecPtr matc = OSG::MaterialChunk::create();
221 matc->setAmbient(OSG::Color4f(0.1f, 0.1f, 0.1f, 1.0f));
222 matc->setDiffuse(OSG::Color4f(0.3f, 0.3f, 0.3f, 1.0f));
223 matc->setSpecular(OSG::Color4f(0.8f, 0.8f, 0.8f, 1.0f));
224 matc->setShininess(100);
225 matc->setLit(true);
227 OSG::SimpleSHLChunkUnrecPtr shl = OSG::SimpleSHLChunk::create();
229 shl->setVertexProgram(_vp_program);
230 shl->setFragmentProgram(_fp_program);
231 shl->addProceduralVariable("Light0Active", &light0Active);
232 shl->addProceduralVariable("Light1Active", &light1Active);
233 shl->addProceduralVariable("Light2Active", &light2Active);
235 cmat->addChunk(matc);
236 cmat->addChunk(shl);
238 // create root node
239 _scene = OSG::Node::create();
241 // create two light sources.
243 OSG::TransformUnrecPtr point1_trans;
245 OSG::NodeUnrecPtr point1 =
246 OSG::makeCoredNode<OSG::PointLight>(&_point1_core);
247 point1_beacon = OSG::makeCoredNode<OSG::Transform >(&point1_trans);
249 point1_trans->editMatrix().setTranslate(-10.0, 5.0, 5.0);
251 _point1_core->setAmbient(0.0f, 0.0f, 0.0f , 1.0f);
252 _point1_core->setDiffuse(1.0f, 0.0f, 0.0f, 1.0f);
253 _point1_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
254 _point1_core->setBeacon(point1_beacon);
255 _point1_core->setOn(true);
258 OSG::TransformUnrecPtr point2_trans;
260 OSG::NodeUnrecPtr point2 =
261 OSG::makeCoredNode<OSG::PointLight>(&_point2_core);
262 point2_beacon = OSG::makeCoredNode<OSG::Transform >(&point2_trans);
264 point2_trans->editMatrix().setTranslate(10.0, 5.0, 5.0);
266 _point2_core->setAmbient(0.0f, 0.0f, 0.0f, 1.0f);
267 _point2_core->setDiffuse(0.0f, 1.0f, 0.0f, 1.0f);
268 _point2_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
269 _point2_core->setBeacon(point2_beacon);
270 _point2_core->setOn(true);
272 point1->addChild(point2);
274 OSG::TransformUnrecPtr point3_trans;
276 OSG::NodeUnrecPtr point3 =
277 OSG::makeCoredNode<OSG::PointLight>(&_point3_core);
279 point3_beacon = OSG::makeCoredNode<OSG::Transform >(&point3_trans);
281 point3_trans->editMatrix().setTranslate(0.0, -12.0, 5.0);
283 _point3_core->setAmbient(0.0f, 0.0f, 0.0f, 1.0f);
284 _point3_core->setDiffuse(0.5f, 0.0f, 1.0f, 1.0f);
285 _point3_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
286 _point3_core->setBeacon(point3_beacon);
287 _point3_core->setOn(true);
289 point2->addChild(point3);
292 // create a sphere.
293 OSG::GeometryUnrecPtr geo = OSG::makeLatLongSphereGeo (100, 100, 1.0);
295 geo->setMaterial(cmat);
298 OSG::NodeUnrecPtr sphere = OSG::makeNodeFor(geo);
300 point3->addChild(sphere);
303 _scene->setCore(OSG::Group::create());
304 _scene->addChild(point1);
306 // create the SimpleSceneManager helper
307 _mgr = OSG::SimpleSceneManager::create();
309 // tell the manager what to manage
310 _mgr->setWindow(gwin );
311 _mgr->setRoot(_scene);
313 _mgr->turnHeadlightOff();
315 // show the whole scene
316 _mgr->showAll();
318 // enable local lights.
319 // OSG::RenderAction *ract =
320 // dynamic_cast<OSG::RenderAction *>(_mgr->getRenderAction());
322 // ract->setLocalLights(true);
324 return 0;
327 // Initialize GLUT & OpenSG and set up the scene
328 int main(int argc, char **argv)
330 if(doMain(argc, argv) != 0)
331 return 1;
333 // GLUT main loop
334 glutMainLoop();
336 return 0;
340 // GLUT callback functions
343 // redraw the window
344 void display(void)
346 // render scene
347 _mgr->redraw();
350 // react to size changes
351 void reshape(int w, int h)
353 _mgr->resize(w, h);
354 glutPostRedisplay();
357 // react to mouse button presses
358 void mouse(int button, int state, int x, int y)
360 if (state)
361 _mgr->mouseButtonRelease(button, x, y);
362 else
363 _mgr->mouseButtonPress(button, x, y);
365 glutPostRedisplay();
368 // react to mouse motions with pressed buttons
369 void motion(int x, int y)
371 _mgr->mouseMove(x, y);
372 glutPostRedisplay();
375 // react to keys
376 void keyboard(unsigned char k, int x, int y)
378 switch(k)
380 case 27:
381 case 'q':
382 _scene = NULL;
383 _point1_core = NULL;
384 _point2_core = NULL;
385 _point3_core = NULL;
387 point1_beacon = NULL;
388 point2_beacon = NULL;
389 point3_beacon = NULL;
391 _mgr = NULL;
393 OSG::osgExit();
395 exit(1);
396 break;
397 case 'w':
398 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
399 printf("wrote scene.osb.gz\n");
400 break;
401 case '1':
403 if(_point1_core->getOn() == false)
404 _point1_core->setOn(true);
405 else
406 _point1_core->setOn(false);
407 break;
410 case '2':
412 if(_point2_core->getOn() == false)
413 _point2_core->setOn(true);
414 else
415 _point2_core->setOn(false);
416 break;
419 case '3':
421 if(_point3_core->getOn() == false)
422 _point3_core->setOn(true);
423 else
424 _point3_core->setOn(false);
425 break;
429 glutPostRedisplay();
432 // setup the GLUT library which handles the windows for us
433 int setupGLUT(int *argc, char *argv[])
435 glutInit(argc, argv);
436 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
438 int winid = glutCreateWindow("OpenSG CG Shader");
440 glutReshapeFunc(reshape);
441 glutDisplayFunc(display);
442 glutMouseFunc(mouse);
443 glutMotionFunc(motion);
444 glutKeyboardFunc(keyboard);
446 return winid;