1 // OpenSG example: testSHL
3 // Demonstrates the use of the SHLChunk
4 // Ok this creates 1 CGChunk with different parameter sets for each geometry.
12 #include "OSGSimpleGeometry.h"
14 #include "OSGGLUTWindow.h"
15 #include "OSGSimpleSceneManager.h"
16 #include "OSGAction.h"
17 #include "OSGSceneFileHandler.h"
18 #include "OSGBaseFunctions.h"
20 #include "OSGGradientBackground.h"
23 #include "OSGTransform.h"
24 #include "OSGPointLight.h"
25 #include "OSGMaterialGroup.h"
28 #include "OSGChunkMaterial.h"
29 #include "OSGMaterialChunk.h"
30 #include "OSGSHLChunk.h"
31 #include "OSGSHLParameterChunk.h"
34 // vertex shader program for
35 static std::string _vp_program
=
36 "varying vec3 DiffuseColor;\n"
37 "varying vec3 SpecularColor;\n"
41 " vec3 LightColor = vec3(1.0);\n"
42 " vec3 Specular = vec3(1.0);\n"
43 " vec3 Ambient = vec3(0.2);\n"
46 " vec3 LightPosition = gl_LightSource[0].position.xyz;\n"
48 " vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
50 " vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);\n"
52 " vec3 lightVec = normalize(LightPosition - ecPosition);\n"
54 " vec3 hvec = normalize(lightVec - ecPosition);\n"
56 " float spec = clamp(dot(hvec, tnorm), 0.0, 1.0);\n"
57 " spec = pow(spec, 16.0);\n"
59 " DiffuseColor = LightColor * vec3(Kd * dot(lightVec, tnorm));\n"
60 " DiffuseColor = clamp(Ambient + DiffuseColor, 0.0, 1.0);\n"
61 " SpecularColor = clamp((LightColor * Specular * spec), 0.0 ,1.0);\n"
63 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
64 " gl_Position = ftransform();\n"
68 // fragment shader program for
69 static std::string _fp_program
=
70 "varying vec3 DiffuseColor;\n"
71 "varying vec3 SpecularColor;\n"
73 "uniform vec2 Scale;\n"
74 "uniform vec2 Threshold;\n"
75 "uniform vec3 SurfaceColor;\n"
79 " float ss = fract(gl_TexCoord[0].s * Scale.s);\n"
80 " float tt = fract(gl_TexCoord[0].t * Scale.t);\n"
82 " if ((ss > Threshold.s) && (tt > Threshold.t))\n"
85 " vec3 finalColor = SurfaceColor * DiffuseColor + SpecularColor;\n"
86 " gl_FragColor = vec4(finalColor, 1.0);\n"
91 // ------------------- global vars ----------------------
93 // The SimpleSceneManager to manage simple applications
94 static OSG::SimpleSceneManagerRefPtr _mgr
;
96 static OSG::NodeRecPtr _scene
;
98 static OSG::Int32 _animation
= 1;
99 static OSG::SHLParameterChunkRecPtr _shlparameter
= NULL
;
102 // forward declaration so we can have the interesting stuff upfront
103 int setupGLUT( int *argc
, char *argv
[] );
105 // Initialize GLUT & OpenSG and set up the scene
106 int doMain(int argc
, char **argv
)
109 OSG::osgInit(argc
,argv
);
112 int winid
= setupGLUT(&argc
, argv
);
114 // the connection between GLUT and OpenSG
115 OSG::GLUTWindowUnrecPtr gwin
= OSG::GLUTWindow::create();
117 gwin
->setGlutId(winid
);
118 gwin
->setSize( 800, 800 );
122 _scene
= OSG::makeCoredNode
<OSG::Group
>();
124 OSG::GeometryUnrecPtr geo
= OSG::makeBoxGeo(0.5, 0.5, 0.5, 1, 1, 1);
127 OSG::SHLChunkUnrecPtr shl
= OSG::SHLChunk::create();
129 shl
->setVertexProgram(_vp_program
);
130 shl
->setFragmentProgram(_fp_program
);
131 // These parameters are the same for all geometries so we
132 // keep them in here.
133 shl
->setUniformParameter("Scale", OSG::Vec2f(20.0f
, 20.0f
));
134 shl
->setUniformParameter("Threshold", OSG::Vec2f(0.7f
, 0.7f
));
139 OSG::Vec3f
sc(0.0, 0.0, 0.0);
142 OSG::Vec3f
ec(1.0, 1.0, 1.0);
144 OSG::Real32 sr
= (ec
[0] - sc
[0]) / OSG::Real32((size
*2));
145 OSG::Real32 sg
= (ec
[1] - sc
[1]) / OSG::Real32((size
*2));
146 OSG::Real32 sb
= (ec
[2] - sc
[2]) / OSG::Real32((size
*2));
148 OSG::Vec3f
color(sc
);
150 OSG::Int32 x
= - size
;
151 OSG::Int32 y
= - size
;
152 OSG::Int32 z
= - size
;
154 OSG::UInt32 iterations
= size
*2 * size
*2 * size
*2;
156 printf("Creating %u cubes ...\n", iterations
);
157 for(OSG::UInt32 i
=0;i
<iterations
;++i
)
159 OSG::ChunkMaterialUnrecPtr cmat
= OSG::ChunkMaterial::create();
161 // ok use one SHLChunk and n SHLParameterChunks
162 // Assing a different "SurfaceColor" parameter to each geometry.
163 OSG::SHLParameterChunkUnrecPtr shlparameter
=
164 OSG::SHLParameterChunk::create();
166 shlparameter
->setSHLChunk(shl
);
167 shlparameter
->setUniformParameter("SurfaceColor", color
);
169 _shlparameter
= shlparameter
;
172 cmat
->addChunk(shlparameter
);
174 OSG::TransformUnrecPtr trans
;
175 OSG::NodeUnrecPtr trans_node
=
176 OSG::makeCoredNode
<OSG::Transform
>(&trans
);
178 trans
->editMatrix().setTranslate(OSG::Real32(x
),
182 OSG::MaterialGroupUnrecPtr mg
;
184 OSG::NodeUnrecPtr mg_node
= OSG::makeCoredNode
<OSG::MaterialGroup
>(&mg
);
186 mg
->setMaterial(cmat
);
188 OSG::NodeUnrecPtr geonode
= OSG::Node::create();
190 geonode
->setCore(geo
);
192 mg_node
->addChild(geonode
);
194 trans_node
->addChild(mg_node
);
197 _scene
->addChild(trans_node
);
220 // create the SimpleSceneManager helper
221 _mgr
= OSG::SimpleSceneManager::create();
223 // tell the manager what to manage
224 _mgr
->setWindow(gwin
);
225 _mgr
->setRoot(_scene
);
227 // show the whole scene
230 // create a gradient background.
231 OSG::GradientBackgroundUnrecPtr gback
= OSG::GradientBackground::create();
234 gback
->addLine(OSG::Color3f(0.7, 0.7, 0.8), 0);
235 gback
->addLine(OSG::Color3f(0.0, 0.1, 0.3), 1);
237 OSG::Window
*win
= _mgr
->getWindow();
239 for(OSG::UInt32 i
=0;i
<win
->getMFPort()->size();++i
)
241 OSG::Viewport
*vp
= win
->getPort(i
);
243 vp
->setBackground(gback
);
249 // Initialize GLUT & OpenSG and set up the scene
250 int main(int argc
, char **argv
)
252 if(doMain(argc
, argv
) != 0)
262 // GLUT callback functions
268 OSG::Real64 t
= OSG::getSystemTime();
271 t
= OSG::getSystemTime() - t
;
273 printf("fps: %f\r", 1.0f
/ OSG::Real32(t
));
275 printf("fps: very fast ...\r");
278 // react to size changes
279 void reshape(int w
, int h
)
285 // react to mouse button presses
286 void mouse(int button
, int state
, int x
, int y
)
289 _mgr
->mouseButtonRelease(button
, x
, y
);
291 _mgr
->mouseButtonPress(button
, x
, y
);
296 // react to mouse motions with pressed buttons
297 void motion(int x
, int y
)
299 _mgr
->mouseMove(x
, y
);
304 void keyboard(unsigned char k
, int x
, int y
)
312 _shlparameter
= NULL
;
318 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
319 printf("wrote scene.osb.gz\n");
322 _animation
= 1 - _animation
;
325 if(_shlparameter
!= NULL
)
327 _shlparameter
->setUniformParameter("SurfaceColor",
338 // setup the GLUT library which handles the windows for us
339 int setupGLUT(int *argc
, char *argv
[])
341 glutInit(argc
, argv
);
342 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
344 int winid
= glutCreateWindow("OpenSG SHL Shader");
346 glutReshapeFunc(reshape
);
347 glutDisplayFunc(display
);
348 glutMouseFunc(mouse
);
349 glutMotionFunc(motion
);
350 glutKeyboardFunc(keyboard
);
352 glutIdleFunc(display
);
360 int main(int argc
, char **argv
)