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 "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"
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"
97 " if(OSGLight0Active)\n"
98 " pointLight(0, normal, eye, ecPosition3);\n"
100 " if(OSGLight1Active)\n"
101 " pointLight(1, normal, eye, ecPosition3);\n"
103 " if(OSGLight2Active)\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
[] );
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");
163 OSG::osgInit(argc
,argv
);
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 );
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);
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
);
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
);
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
292 // enable local lights.
293 // OSG::RenderAction *ract =
294 // dynamic_cast<OSG::RenderAction *>(_mgr->getRenderAction());
296 // ract->setLocalLights(true);
301 // Initialize GLUT & OpenSG and set up the scene
302 int main(int argc
, char **argv
)
304 if(doMain(argc
, argv
) != 0)
314 // GLUT callback functions
324 // react to size changes
325 void reshape(int w
, int h
)
331 // react to mouse button presses
332 void mouse(int button
, int state
, int x
, int y
)
335 _mgr
->mouseButtonRelease(button
, x
, y
);
337 _mgr
->mouseButtonPress(button
, x
, y
);
342 // react to mouse motions with pressed buttons
343 void motion(int x
, int y
)
345 _mgr
->mouseMove(x
, y
);
350 void keyboard(unsigned char k
, int x
, int y
)
361 point1_beacon
= NULL
;
362 point2_beacon
= NULL
;
363 point3_beacon
= NULL
;
372 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
373 printf("wrote scene.osb.gz\n");
377 if(_point1_core
->getOn() == false)
378 _point1_core
->setOn(true);
380 _point1_core
->setOn(false);
386 if(_point2_core
->getOn() == false)
387 _point2_core
->setOn(true);
389 _point2_core
->setOn(false);
395 if(_point3_core
->getOn() == false)
396 _point3_core
->setOn(true);
398 _point3_core
->setOn(false);
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
);