1 // OpenSG example: testSHL
3 // Demonstrates the use of the SHLChunk
4 // Implements a simple bumpmapping via vertex and fragment shader.
12 #include "OSGSimpleGeometry.h"
14 #include "OSGGLUTWindow.h"
15 #include "OSGSimpleSceneManager.h"
16 #include "OSGAction.h"
17 #include "OSGSceneFileHandler.h"
18 #include "OSGBaseFunctions.h"
22 #include "OSGTransform.h"
23 #include "OSGPointLight.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"
41 " void pointLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3)\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"
51 " // Compute vector from surface to light position\n"
52 " VP = vec3 (gl_LightSource[i].position) - ecPosition3;\n"
54 " // Compute distance between surface and light position\n"
57 " // Normalize the vector from surface to light position\n"
58 " VP = normalize(VP);\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"
68 " if (nDotVP == 0.0)\n"
71 " pf = pow(nDotHV, gl_FrontMaterial.shininess);\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"
78 " vec3 fnormal(void)\n"
80 " //Compute the normal\n"
81 " vec3 normal = gl_NormalMatrix * gl_Normal;\n"
82 " normal = normalize(normal);\n"
86 " void flight(in vec3 normal, in vec4 ecPosition, float alphaFade)\n"
89 " vec3 ecPosition3;\n"
92 " ecPosition3 = (vec3 (ecPosition)) / ecPosition.w;\n"
93 " eye = vec3 (0.0, 0.0, 1.0);\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"
100 " if(OSGLight0Active)\n"
101 " pointLight(0, normal, eye, ecPosition3);\n"
103 " if(OSGLight1Active)\n"
104 " pointLight(1, normal, eye, ecPosition3);\n"
106 " if(OSGLight2Active)\n"
107 " pointLight(2, normal, eye, ecPosition3);\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"
121 " vec3 transformedNormal;\n"
122 " float alphaFade = 1.0;\n"
124 " // Eye-coordinate position of vertex, needed in various calculations\n"
125 " vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;\n"
127 " // Do fixed functionality vertex transform\n"
128 " gl_Position = ftransform();\n"
129 " transformedNormal = fnormal();\n"
130 " flight(transformedNormal, ecPosition, alphaFade);\n"
133 // fragment shader program for bump mapping in surface local coordinates
134 static std::string _fp_program
=
138 " color = gl_Color;\n"
139 " gl_FragColor = color;\n"
143 // ------------------- global vars ----------------------
145 // The SimpleSceneManager to manage simple applications
146 static OSG::SimpleSceneManagerRefPtr _mgr
;
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
,
166 void updateSpecialParameter(
167 OSG::SHLChunk::GetUniformLocProc getUniformLocation
,
168 OSG::DrawEnv
*action
,
172 if(action
->getCamera() == NULL
|| action
->getViewport() == NULL
)
174 FWARNING(("updateSpecialParameter : Can't update OSGSpecialParameter"
175 "parameter, camera or viewport is NULL!\n"));
179 // uploads the camera orientation.
181 action
->getCamera()->getViewing(m
,
182 action
->getViewport()->getPixelWidth(),
183 action
->getViewport()->getPixelHeight());
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");
198 uniformMatrix4fv(location
, 1, GL_FALSE
, m
.getValues());
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");
207 OSG::osgInit(argc
,argv
);
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 );
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);
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
);
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
);
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
336 // enable local lights.
337 // OSG::RenderAction *ract =
338 // dynamic_cast<OSG::RenderAction *>(_mgr->getRenderAction());
340 // ract->setLocalLights(true);
345 // Initialize GLUT & OpenSG and set up the scene
346 int main(int argc
, char **argv
)
348 if(doMain(argc
, argv
) != 0)
358 // GLUT callback functions
368 // react to size changes
369 void reshape(int w
, int h
)
375 // react to mouse button presses
376 void mouse(int button
, int state
, int x
, int y
)
379 _mgr
->mouseButtonRelease(button
, x
, y
);
381 _mgr
->mouseButtonPress(button
, x
, y
);
386 // react to mouse motions with pressed buttons
387 void motion(int x
, int y
)
389 _mgr
->mouseMove(x
, y
);
394 void keyboard(unsigned char k
, int x
, int y
)
412 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
413 printf("wrote scene.osb.gz\n");
417 if(_point1_core
->getOn() == false)
418 _point1_core
->setOn(true);
420 _point1_core
->setOn(false);
426 if(_point2_core
->getOn() == false)
427 _point2_core
->setOn(true);
429 _point2_core
->setOn(false);
435 if(_point3_core
->getOn() == false)
436 _point3_core
->setOn(true);
438 _point3_core
->setOn(false);
443 shl
->subUniformParameter("OSGLight0Active");
446 shl
->setUniformParameter("OSGLight0Active", 0);
449 shl
->subUniformParameter("OSGLight1Active");
452 shl
->setUniformParameter("OSGLight1Active", 0);
455 shl
->subUniformParameter("OSGLight2Active");
458 shl
->setUniformParameter("OSGLight2Active", 0);
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
);
484 int main(int argc
, char **argv
)