1 // OpenSG example: testSHL
3 // Demonstrates the use of the SHLChunk
4 // Implements a simple bumpmapping via vertex and fragment shader.
9 #include "OSGSimpleGeometry.h"
11 #include "OSGGLUTWindow.h"
12 #include "OSGSimpleSceneManager.h"
13 #include "OSGAction.h"
14 #include "OSGSceneFileHandler.h"
15 #include "OSGBaseFunctions.h"
19 #include "OSGTransform.h"
20 #include "OSGPointLight.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"
38 " void pointLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3)\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"
48 " // Compute vector from surface to light position\n"
49 " VP = vec3 (gl_LightSource[i].position) - ecPosition3;\n"
51 " // Compute distance between surface and light position\n"
54 " // Normalize the vector from surface to light position\n"
55 " VP = normalize(VP);\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"
65 " if (nDotVP == 0.0)\n"
68 " pf = pow(nDotHV, gl_FrontMaterial.shininess);\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"
75 " vec3 fnormal(void)\n"
77 " //Compute the normal\n"
78 " vec3 normal = gl_NormalMatrix * gl_Normal;\n"
79 " normal = normalize(normal);\n"
83 " void flight(in vec3 normal, in vec4 ecPosition, float alphaFade)\n"
86 " vec3 ecPosition3;\n"
89 " ecPosition3 = (vec3 (ecPosition)) / ecPosition.w;\n"
90 " eye = vec3 (0.0, 0.0, 1.0);\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"
98 " pointLight(0, normal, eye, ecPosition3);\n"
100 " if(Light1Active)\n"
101 " pointLight(1, normal, eye, ecPosition3);\n"
103 " if(Light2Active)\n"
104 " pointLight(2, normal, eye, ecPosition3);\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"
118 " vec3 transformedNormal;\n"
119 " float alphaFade = 1.0;\n"
121 " // Eye-coordinate position of vertex, needed in various calculations\n"
122 " vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;\n"
124 " // Do fixed functionality vertex transform\n"
125 " gl_Position = ftransform();\n"
126 " transformedNormal = fnormal();\n"
127 " flight(transformedNormal, ecPosition, alphaFade);\n"
130 // fragment shader program for bump mapping in surface local coordinates
131 static std::string _fp_program
=
135 " color = gl_Color;\n"
136 " gl_FragColor = color;\n"
140 // ------------------- global vars ----------------------
142 // The SimpleSceneManager to manage simple applications
143 static OSG::SimpleSceneManagerRefPtr _mgr
;
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
)
163 OSGGETGLFUNC_EXT(glUniform1i
,
165 OSG::ShaderProgram::getFuncIdUniform1i());
168 GLint(pEnv
->getLightState() & 0x0001));
172 static void light1Active(OSG::DrawEnv
*pEnv
, OSG::Int32 iLoc
)
176 OSGGETGLFUNC_EXT(glUniform1i
,
178 OSG::ShaderProgram::getFuncIdUniform1i());
181 GLint(pEnv
->getLightState() & 0x0002));
185 static void light2Active(OSG::DrawEnv
*pEnv
, OSG::Int32 iLoc
)
189 OSGGETGLFUNC_EXT(glUniform1i
,
191 OSG::ShaderProgram::getFuncIdUniform1i());
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");
203 OSG::osgInit(argc
,argv
);
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 );
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);
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
);
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
);
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
318 // enable local lights.
319 // OSG::RenderAction *ract =
320 // dynamic_cast<OSG::RenderAction *>(_mgr->getRenderAction());
322 // ract->setLocalLights(true);
327 // Initialize GLUT & OpenSG and set up the scene
328 int main(int argc
, char **argv
)
330 if(doMain(argc
, argv
) != 0)
340 // GLUT callback functions
350 // react to size changes
351 void reshape(int w
, int h
)
357 // react to mouse button presses
358 void mouse(int button
, int state
, int x
, int y
)
361 _mgr
->mouseButtonRelease(button
, x
, y
);
363 _mgr
->mouseButtonPress(button
, x
, y
);
368 // react to mouse motions with pressed buttons
369 void motion(int x
, int y
)
371 _mgr
->mouseMove(x
, y
);
376 void keyboard(unsigned char k
, int x
, int y
)
387 point1_beacon
= NULL
;
388 point2_beacon
= NULL
;
389 point3_beacon
= NULL
;
398 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
399 printf("wrote scene.osb.gz\n");
403 if(_point1_core
->getOn() == false)
404 _point1_core
->setOn(true);
406 _point1_core
->setOn(false);
412 if(_point2_core
->getOn() == false)
413 _point2_core
->setOn(true);
415 _point2_core
->setOn(false);
421 if(_point3_core
->getOn() == false)
422 _point3_core
->setOn(true);
424 _point3_core
->setOn(false);
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
);