fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / Chunks / testSHLOSGLights_shaderprog.cpp
blobef14eb3bf4fe9ddb70874d76c6991e2c153fffde
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 "OSGShaderProgramChunk.h"
26 #include "OSGShaderProgram.h"
28 // vertex shader program for bump mapping in surface local coordinates
29 static std::string _vp_program =
30 "uniform bool OSGLight0Active;\n"
31 "uniform bool OSGLight1Active;\n"
32 "uniform bool OSGLight2Active;\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(OSGLight0Active)\n"
98 " pointLight(0, normal, eye, ecPosition3);\n"
99 "\n"
100 " if(OSGLight1Active)\n"
101 " pointLight(1, normal, eye, ecPosition3);\n"
102 "\n"
103 " if(OSGLight2Active)\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[] );
158 // Initialize GLUT & OpenSG and set up the scene
159 int doMain(int argc, char **argv)
161 printf("Press key '1', '2', or '3' to toggle the light sources.\n");
162 // OSG init
163 OSG::osgInit(argc,argv);
165 // GLUT init
166 int winid = setupGLUT(&argc, argv);
168 // the connection between GLUT and OpenSG
169 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
171 gwin->setGlutId(winid);
172 gwin->setSize( 800, 800 );
173 gwin->init();
175 // Create the shader material
177 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
179 OSG::MaterialChunkUnrecPtr matc = OSG::MaterialChunk::create();
181 matc->setAmbient(OSG::Color4f(0.1f, 0.1f, 0.1f, 1.0f));
182 matc->setDiffuse(OSG::Color4f(0.3f, 0.3f, 0.3f, 1.0f));
183 matc->setSpecular(OSG::Color4f(0.8f, 0.8f, 0.8f, 1.0f));
184 matc->setShininess(100);
185 matc->setLit(true);
187 OSG::ShaderProgramChunkUnrecPtr shl = OSG::ShaderProgramChunk::create();
189 OSG::ShaderProgramUnrecPtr shl_vp =
190 OSG::ShaderProgram::createVertexShader();
192 shl_vp->setProgram(_vp_program);
194 shl->addShader(shl_vp);
197 OSG::ShaderProgramUnrecPtr shl_fp =
198 OSG::ShaderProgram::createFragmentShader();
200 shl_fp->setProgram(_fp_program);
202 shl->addShader(shl_fp);
205 shl_vp->addOSGVariable("OSGLight0Active");
206 shl_vp->addOSGVariable("OSGLight1Active");
207 shl_vp->addOSGVariable("OSGLight2Active");
209 cmat->addChunk(matc);
210 cmat->addChunk(shl);
212 // create root node
213 _scene = OSG::Node::create();
215 // create two light sources.
217 OSG::TransformUnrecPtr point1_trans;
219 OSG::NodeUnrecPtr point1 =
220 OSG::makeCoredNode<OSG::PointLight>(&_point1_core);
221 point1_beacon = OSG::makeCoredNode<OSG::Transform >(&point1_trans);
223 point1_trans->editMatrix().setTranslate(-10.0, 5.0, 5.0);
225 _point1_core->setAmbient(0.0f, 0.0f, 0.0f , 1.0f);
226 _point1_core->setDiffuse(1.0f, 0.0f, 0.0f, 1.0f);
227 _point1_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
228 _point1_core->setBeacon(point1_beacon);
229 _point1_core->setOn(true);
232 OSG::TransformUnrecPtr point2_trans;
234 OSG::NodeUnrecPtr point2 =
235 OSG::makeCoredNode<OSG::PointLight>(&_point2_core);
236 point2_beacon = OSG::makeCoredNode<OSG::Transform >(&point2_trans);
238 point2_trans->editMatrix().setTranslate(10.0, 5.0, 5.0);
240 _point2_core->setAmbient(0.0f, 0.0f, 0.0f, 1.0f);
241 _point2_core->setDiffuse(0.0f, 1.0f, 0.0f, 1.0f);
242 _point2_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
243 _point2_core->setBeacon(point2_beacon);
244 _point2_core->setOn(true);
246 point1->addChild(point2);
248 OSG::TransformUnrecPtr point3_trans;
250 OSG::NodeUnrecPtr point3 =
251 OSG::makeCoredNode<OSG::PointLight>(&_point3_core);
253 point3_beacon = OSG::makeCoredNode<OSG::Transform >(&point3_trans);
255 point3_trans->editMatrix().setTranslate(0.0, -12.0, 5.0);
257 _point3_core->setAmbient(0.0f, 0.0f, 0.0f, 1.0f);
258 _point3_core->setDiffuse(0.5f, 0.0f, 1.0f, 1.0f);
259 _point3_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
260 _point3_core->setBeacon(point3_beacon);
261 _point3_core->setOn(true);
263 point2->addChild(point3);
266 // create a sphere.
267 OSG::GeometryUnrecPtr geo = OSG::makeLatLongSphereGeo (100, 100, 1.0);
269 geo->setMaterial(cmat);
272 OSG::NodeUnrecPtr sphere = OSG::makeNodeFor(geo);
274 point3->addChild(sphere);
277 _scene->setCore(OSG::Group::create());
278 _scene->addChild(point1);
280 // create the SimpleSceneManager helper
281 _mgr = OSG::SimpleSceneManager::create();
283 // tell the manager what to manage
284 _mgr->setWindow(gwin );
285 _mgr->setRoot(_scene);
287 _mgr->turnHeadlightOff();
289 // show the whole scene
290 _mgr->showAll();
292 // enable local lights.
293 // OSG::RenderAction *ract =
294 // dynamic_cast<OSG::RenderAction *>(_mgr->getRenderAction());
296 // ract->setLocalLights(true);
298 return 0;
301 // Initialize GLUT & OpenSG and set up the scene
302 int main(int argc, char **argv)
304 if(doMain(argc, argv) != 0)
305 return 1;
307 // GLUT main loop
308 glutMainLoop();
310 return 0;
314 // GLUT callback functions
317 // redraw the window
318 void display(void)
320 // render scene
321 _mgr->redraw();
324 // react to size changes
325 void reshape(int w, int h)
327 _mgr->resize(w, h);
328 glutPostRedisplay();
331 // react to mouse button presses
332 void mouse(int button, int state, int x, int y)
334 if (state)
335 _mgr->mouseButtonRelease(button, x, y);
336 else
337 _mgr->mouseButtonPress(button, x, y);
339 glutPostRedisplay();
342 // react to mouse motions with pressed buttons
343 void motion(int x, int y)
345 _mgr->mouseMove(x, y);
346 glutPostRedisplay();
349 // react to keys
350 void keyboard(unsigned char k, int x, int y)
352 switch(k)
354 case 27:
355 case 'q':
356 _scene = NULL;
357 _point1_core = NULL;
358 _point2_core = NULL;
359 _point3_core = NULL;
361 point1_beacon = NULL;
362 point2_beacon = NULL;
363 point3_beacon = NULL;
365 _mgr = NULL;
367 OSG::osgExit();
369 exit(1);
370 break;
371 case 'w':
372 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
373 printf("wrote scene.osb.gz\n");
374 break;
375 case '1':
377 if(_point1_core->getOn() == false)
378 _point1_core->setOn(true);
379 else
380 _point1_core->setOn(false);
381 break;
384 case '2':
386 if(_point2_core->getOn() == false)
387 _point2_core->setOn(true);
388 else
389 _point2_core->setOn(false);
390 break;
393 case '3':
395 if(_point3_core->getOn() == false)
396 _point3_core->setOn(true);
397 else
398 _point3_core->setOn(false);
399 break;
403 glutPostRedisplay();
406 // setup the GLUT library which handles the windows for us
407 int setupGLUT(int *argc, char *argv[])
409 glutInit(argc, argv);
410 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
412 int winid = glutCreateWindow("OpenSG CG Shader");
414 glutReshapeFunc(reshape);
415 glutDisplayFunc(display);
416 glutMouseFunc(mouse);
417 glutMotionFunc(motion);
418 glutKeyboardFunc(keyboard);
420 return winid;