changed: gcc8 base update
[opensg.git] / Source / System / State / Shader / SHL / testSHL_compat.cpp
blob0628f65f86cc6dcb27b227ab346381cd49f008d5
1 // OpenSG example: testSHL
2 //
3 // Demonstrates the use of the SHLChunk
4 // Implements a simple bumpmapping via vertex and fragment shader.
6 // Headers
7 #include "OSGGLUT.h"
8 #include "OSGConfig.h"
10 #ifdef OSG_1_COMPAT
12 #include "OSGSimpleGeometry.h"
13 #include "OSGGLUT.h"
14 #include "OSGGLUTWindow.h"
15 #include "OSGSimpleSceneManager.h"
16 #include "OSGAction.h"
17 #include "OSGSceneFileHandler.h"
18 #include "OSGBaseFunctions.h"
20 #include "OSGNode.h"
21 #include "OSGGroup.h"
22 #include "OSGTransform.h"
23 #include "OSGPointLight.h"
25 #include "OSGImage.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;
38 // The scene
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");
49 if( argc < 3 )
50 return 0;
52 // OSG init
53 OSG::osgInit(argc,argv);
55 // GLUT init
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 );
62 gwin->init();
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);
73 matc->setLit(true);
75 OSG::SHLChunkUnrecPtr shl = OSG::SHLChunk::create();
77 shl->readVertexProgram(argv[1]);
78 shl->readFragmentProgram(argv[2]);
80 cmat->addChunk(shl);
83 // create root node
84 _scene = OSG::Node::create();
86 // create torus
87 OSG::GeometryUnrecPtr geo = OSG::makeTorusGeo(.8, 1.8, 128, 128);
88 geo->setMaterial(cmat);
90 OSG::NodeUnrecPtr torus = OSG::Node::create();
91 torus->setCore(geo);
93 // add torus to scene
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();
110 beginEditCP(light);
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());
115 endEditCP(light);
116 beginEditCP(_scene);
117 _scene->setCore(light);
118 endEditCP(_scene);
121 // show the whole scene
122 _mgr->showAll();
124 // GLUT main loop
125 glutMainLoop();
127 return 0;
131 // GLUT callback functions
134 // redraw the window
135 void display(void)
137 // render scene
138 _mgr->redraw();
141 // react to size changes
142 void reshape(int w, int h)
144 _mgr->resize(w, h);
145 glutPostRedisplay();
148 // react to mouse button presses
149 void mouse(int button, int state, int x, int y)
151 if (state)
152 _mgr->mouseButtonRelease(button, x, y);
153 else
154 _mgr->mouseButtonPress(button, x, y);
156 glutPostRedisplay();
159 // react to mouse motions with pressed buttons
160 void motion(int x, int y)
162 _mgr->mouseMove(x, y);
163 glutPostRedisplay();
166 // react to keys
167 void keyboard(unsigned char k, int x, int y)
169 switch(k)
171 case 27:
172 case 'q':
174 _mgr = NULL;
175 _scene = NULL;
177 OSG::osgExit();
178 exit(1);
179 break;
180 case 'w':
181 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
182 printf("wrote scene.osb.gz\n");
183 break;
186 glutPostRedisplay();
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);
203 return winid;
206 #else
208 int main(void)
210 return 0;
213 #endif