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 "OSGTextureObjChunk.h"
29 #include "OSGTextureEnvChunk.h"
30 #include "OSGSHLChunk.h"
34 // ------------------- global vars ----------------------
36 // The SimpleSceneManager to manage simple applications
37 static OSG::SimpleSceneManagerRefPtr _mgr
;
39 static OSG::NodeRecPtr _scene
;
41 // forward declaration so we can have the interesting stuff upfront
42 int setupGLUT( int *argc
, char *argv
[] );
44 // Initialize GLUT & OpenSG and set up the scene
45 int main(int argc
, char **argv
)
47 printf("Usage: testCGShader <filename.vp> <filename.fp>\n");
53 OSG::osgInit(argc
,argv
);
56 int winid
= setupGLUT(&argc
, argv
);
58 // the connection between GLUT and OpenSG
59 OSG::GLUTWindowUnrecPtr gwin
= OSG::GLUTWindow::create();
60 gwin
->setGlutId(winid
);
61 gwin
->setSize( 800, 800 );
64 // Create the shader material
65 OSG::ChunkMaterialUnrecPtr cmat
= OSG::ChunkMaterial::create();
67 OSG::MaterialChunkUnrecPtr matc
= OSG::MaterialChunk::create();
69 matc
->setAmbient(OSG::Color4f(0.1, 0.1, 0.1, 1.0));
70 matc
->setDiffuse(OSG::Color4f(0.3, 0.3, 0.3, 1.0));
71 matc
->setSpecular(OSG::Color4f(0.8, 0.8, 0.8, 1.0));
72 matc
->setShininess(100);
75 OSG::SHLChunkUnrecPtr shl
= OSG::SHLChunk::create();
77 shl
->readVertexProgram(argv
[1]);
78 shl
->readFragmentProgram(argv
[2]);
84 _scene
= OSG::Node::create();
87 OSG::GeometryUnrecPtr geo
= OSG::makeTorusGeo(.8, 1.8, 128, 128);
88 geo
->setMaterial(cmat
);
90 OSG::NodeUnrecPtr torus
= OSG::Node::create();
94 OSG::GroupUnrecPtr group
= OSG::Group::create();
95 _scene
->setCore(group
);
96 _scene
->addChild(torus
);
98 // create the SimpleSceneManager helper
99 _mgr
= OSG::SimpleSceneManager::create();
101 // tell the manager what to manage
102 _mgr
->setWindow(gwin
);
103 _mgr
->setRoot(_scene
);
106 // create point headlight
107 _mgr->turnHeadlightOff();
108 NodePtr headlight = _mgr->getHighlight();
109 PointLightPtr light = PointLight::create();
111 light->setAmbient (.3, .3, .3, 1);
112 light->setDiffuse ( 1, 1, 1, 1);
113 light->setSpecular ( 1, 1, 1, 1);
114 light->setBeacon (_mgr->getCamera()->getBeacon());
117 _scene->setCore(light);
121 // show the whole scene
131 // GLUT callback functions
141 // react to size changes
142 void reshape(int w
, int h
)
148 // react to mouse button presses
149 void mouse(int button
, int state
, int x
, int y
)
152 _mgr
->mouseButtonRelease(button
, x
, y
);
154 _mgr
->mouseButtonPress(button
, x
, y
);
159 // react to mouse motions with pressed buttons
160 void motion(int x
, int y
)
162 _mgr
->mouseMove(x
, y
);
167 void keyboard(unsigned char k
, int x
, int y
)
181 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
182 printf("wrote scene.osb.gz\n");
189 // setup the GLUT library which handles the windows for us
190 int setupGLUT(int *argc
, char *argv
[])
192 glutInit(argc
, argv
);
193 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
195 int winid
= glutCreateWindow("OpenSG CG Shader");
197 glutReshapeFunc(reshape
);
198 glutDisplayFunc(display
);
199 glutMouseFunc(mouse
);
200 glutMotionFunc(motion
);
201 glutKeyboardFunc(keyboard
);