1 // OpenSG example: testSHL
3 // Demonstrates the use of the SHLChunk
4 // Ok this creates 1 CGChunk with different parameter sets for each geometry.
9 #include "OSGSimpleGeometry.h"
11 #include "OSGGLUTWindow.h"
12 #include "OSGSimpleSceneManager.h"
13 #include "OSGAction.h"
14 #include "OSGSceneFileHandler.h"
15 #include "OSGBaseFunctions.h"
17 #include "OSGGradientBackground.h"
20 #include "OSGTransform.h"
21 #include "OSGPointLight.h"
22 #include "OSGMaterialGroup.h"
25 #include "OSGChunkMaterial.h"
26 #include "OSGMaterialChunk.h"
27 #include "OSGSimpleSHLChunk.h"
28 #include "OSGSimpleSHLVariableChunk.h"
30 // vertex shader program for
31 static std::string _vp_program
=
32 "varying vec3 DiffuseColor;\n"
33 "varying vec3 SpecularColor;\n"
37 " vec3 LightColor = vec3(1.0);\n"
38 " vec3 Specular = vec3(1.0);\n"
39 " vec3 Ambient = vec3(0.2);\n"
42 " vec3 LightPosition = gl_LightSource[0].position.xyz;\n"
44 " vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
46 " vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);\n"
48 " vec3 lightVec = normalize(LightPosition - ecPosition);\n"
50 " vec3 hvec = normalize(lightVec - ecPosition);\n"
52 " float spec = clamp(dot(hvec, tnorm), 0.0, 1.0);\n"
53 " spec = pow(spec, 16.0);\n"
55 " DiffuseColor = LightColor * vec3(Kd * dot(lightVec, tnorm));\n"
56 " DiffuseColor = clamp(Ambient + DiffuseColor, 0.0, 1.0);\n"
57 " SpecularColor = clamp((LightColor * Specular * spec), 0.0 ,1.0);\n"
59 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
60 " gl_Position = ftransform();\n"
64 // fragment shader program for
65 static std::string _fp_program
=
66 "varying vec3 DiffuseColor;\n"
67 "varying vec3 SpecularColor;\n"
69 "uniform vec2 Scale;\n"
70 "uniform vec2 Threshold;\n"
71 "uniform vec3 SurfaceColor;\n"
75 " float ss = fract(gl_TexCoord[0].s * Scale.s);\n"
76 " float tt = fract(gl_TexCoord[0].t * Scale.t);\n"
78 " if ((ss > Threshold.s) && (tt > Threshold.t))\n"
81 " vec3 finalColor = SurfaceColor * DiffuseColor + SpecularColor;\n"
82 " gl_FragColor = vec4(finalColor, 1.0);\n"
87 // ------------------- global vars ----------------------
89 // The SimpleSceneManager to manage simple applications
90 static OSG::SimpleSceneManagerRefPtr _mgr
;
92 static OSG::NodeRecPtr _scene
;
94 static OSG::Int32 _animation
= 1;
95 static OSG::SimpleSHLVariableChunkRecPtr _shlparameter
= NULL
;
98 // forward declaration so we can have the interesting stuff upfront
99 int setupGLUT( int *argc
, char *argv
[] );
101 // Initialize GLUT & OpenSG and set up the scene
102 int doMain(int argc
, char **argv
)
105 OSG::osgInit(argc
,argv
);
108 int winid
= setupGLUT(&argc
, argv
);
110 // the connection between GLUT and OpenSG
111 OSG::GLUTWindowUnrecPtr gwin
= OSG::GLUTWindow::create();
113 gwin
->setGlutId(winid
);
114 gwin
->setSize( 800, 800 );
118 _scene
= OSG::makeCoredNode
<OSG::Group
>();
120 OSG::GeometryUnrecPtr geo
= OSG::makeBoxGeo(0.5, 0.5, 0.5, 1, 1, 1);
123 OSG::SimpleSHLChunkUnrecPtr shl
= OSG::SimpleSHLChunk::create();
125 shl
->setVertexProgram(_vp_program
);
126 shl
->setFragmentProgram(_fp_program
);
127 // These parameters are the same for all geometries so we
128 // keep them in here.
129 shl
->addUniformVariable("Scale", OSG::Vec2f(20.0f
, 20.0f
));
130 shl
->addUniformVariable("Threshold", OSG::Vec2f(0.7f
, 0.7f
));
135 OSG::Vec3f
sc(0.0, 0.0, 0.0);
138 OSG::Vec3f
ec(1.0, 1.0, 1.0);
140 OSG::Real32 sr
= (ec
[0] - sc
[0]) / OSG::Real32((size
*2));
141 OSG::Real32 sg
= (ec
[1] - sc
[1]) / OSG::Real32((size
*2));
142 OSG::Real32 sb
= (ec
[2] - sc
[2]) / OSG::Real32((size
*2));
144 OSG::Vec3f
color(sc
);
146 OSG::Int32 x
= - size
;
147 OSG::Int32 y
= - size
;
148 OSG::Int32 z
= - size
;
150 OSG::UInt32 iterations
= size
*2 * size
*2 * size
*2;
152 printf("Creating %u cubes ...\n", iterations
);
153 for(OSG::UInt32 i
=0;i
<iterations
;++i
)
155 OSG::ChunkMaterialUnrecPtr cmat
= OSG::ChunkMaterial::create();
157 // ok use one SHLChunk and n SHLParameterChunks
158 // Assing a different "SurfaceColor" parameter to each geometry.
159 OSG::SimpleSHLVariableChunkUnrecPtr shlparameter
=
160 OSG::SimpleSHLVariableChunk::create();
162 // shlparameter->setSHLChunk(shl);
163 shlparameter
->addUniformVariable("SurfaceColor", color
);
165 _shlparameter
= shlparameter
;
168 cmat
->addChunk(shlparameter
);
170 OSG::TransformUnrecPtr trans
;
171 OSG::NodeUnrecPtr trans_node
=
172 OSG::makeCoredNode
<OSG::Transform
>(&trans
);
174 trans
->editMatrix().setTranslate(OSG::Real32(x
),
178 OSG::MaterialGroupUnrecPtr mg
;
180 OSG::NodeUnrecPtr mg_node
= OSG::makeCoredNode
<OSG::MaterialGroup
>(&mg
);
182 mg
->setMaterial(cmat
);
184 OSG::NodeUnrecPtr geonode
= OSG::Node::create();
186 geonode
->setCore(geo
);
188 mg_node
->addChild(geonode
);
190 trans_node
->addChild(mg_node
);
193 _scene
->addChild(trans_node
);
216 // create the SimpleSceneManager helper
217 _mgr
= OSG::SimpleSceneManager::create();
219 // tell the manager what to manage
220 _mgr
->setWindow(gwin
);
221 _mgr
->setRoot(_scene
);
223 // show the whole scene
226 // create a gradient background.
227 OSG::GradientBackgroundUnrecPtr gback
= OSG::GradientBackground::create();
230 gback
->addLine(OSG::Color3f(0.7f
, 0.7f
, 0.8f
), 0);
231 gback
->addLine(OSG::Color3f(0.0f
, 0.1f
, 0.3f
), 1);
233 OSG::Window
*win
= _mgr
->getWindow();
235 for(unsigned int i
=0;i
<win
->getMFPort()->size();++i
)
237 OSG::Viewport
*vp
= win
->getPort(i
);
238 vp
->setBackground(gback
);
244 // Initialize GLUT & OpenSG and set up the scene
245 int main(int argc
, char **argv
)
247 if(doMain(argc
, argv
) != 0)
257 // GLUT callback functions
263 OSG::Real64 t
= OSG::getSystemTime();
266 t
= OSG::getSystemTime() - t
;
268 printf("fps: %f\r", 1.0f
/ OSG::Real32(t
));
270 printf("fps: very fast ...\r");
273 // react to size changes
274 void reshape(int w
, int h
)
280 // react to mouse button presses
281 void mouse(int button
, int state
, int x
, int y
)
284 _mgr
->mouseButtonRelease(button
, x
, y
);
286 _mgr
->mouseButtonPress(button
, x
, y
);
291 // react to mouse motions with pressed buttons
292 void motion(int x
, int y
)
294 _mgr
->mouseMove(x
, y
);
299 void keyboard(unsigned char k
, int x
, int y
)
307 _shlparameter
= NULL
;
315 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
316 printf("wrote scene.osb.gz\n");
319 _animation
= 1 - _animation
;
322 if(_shlparameter
!= NULL
)
324 _shlparameter
->updateUniformVariable(
326 OSG::Vec3f(1.0f
, 1.0f
, 1.0f
));
334 // setup the GLUT library which handles the windows for us
335 int setupGLUT(int *argc
, char *argv
[])
337 glutInit(argc
, argv
);
338 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
340 int winid
= glutCreateWindow("OpenSG SHL Shader");
342 glutReshapeFunc(reshape
);
343 glutDisplayFunc(display
);
344 glutMouseFunc(mouse
);
345 glutMotionFunc(motion
);
346 glutKeyboardFunc(keyboard
);
348 glutIdleFunc(display
);