fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / SHL / testSHLGeometryShader_compat.cpp
blob8dce0be5a4f457af5b6f475ed4733b73b8a06668
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 "OSGConfig.h"
9 #ifdef OSG_1_COMPAT
11 #include "OSGSimpleGeometry.h"
12 #include "OSGGLUT.h"
13 #include "OSGGLEXT.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 "OSGSHLChunk.h"
32 // ------------------- global vars ----------------------
34 // The SimpleSceneManager to manage simple applications
35 static OSG::SimpleSceneManagerRefPtr _mgr;
36 // The scene
37 static OSG::NodeRecPtr _scene;
39 // forward declaration so we can have the interesting stuff upfront
40 int setupGLUT( int *argc, char *argv[] );
42 static std::string _vertex_shader =
43 "void main(void)\n"
44 "{\n"
45 " gl_Position = gl_Vertex;\n"
46 " gl_TexCoord[0] = vec4(abs(gl_Normal), 0.0);\n"
47 "}\n"
48 "\n";
50 static std::string _fragment_shader =
51 "void main (void)\n"
52 "{\n"
53 " gl_FragColor = gl_Color;\n"
54 "\n"
55 "}\n";
57 // ok we create some triangles to draw the face normals.
58 static std::string _geometry_shader =
59 "#version 120\n"
60 "#extension GL_EXT_geometry_shader4 : enable\n"
61 "\n"
62 "void main(void)\n"
63 "{\n"
64 " vec4 v1 = gl_PositionIn[0];\n"
65 " vec4 v2 = gl_PositionIn[1];\n"
66 " vec4 v3 = gl_PositionIn[2];\n"
67 "\n"
68 " vec4 l1 = v2 - v1;\n"
69 " vec4 l2 = v3 - v1;\n"
70 "\n"
71 " gl_Position = gl_ModelViewProjectionMatrix * v1;\n"
72 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
73 " EmitVertex();\n"
74 " gl_Position = gl_ModelViewProjectionMatrix * v2;\n"
75 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
76 " EmitVertex();\n"
77 " gl_Position = gl_ModelViewProjectionMatrix * v3;\n"
78 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
79 " EmitVertex();\n"
80 " EndPrimitive();\n"
81 "\n"
82 " vec3 l1n = l1.xyz;\n"
83 " vec3 l2n = l2.xyz;\n"
84 "\n"
85 " vec3 N = cross(l1n.xyz, l2n.xyz);\n"
86 " N = normalize(N);\n"
87 " vec4 middle = v1 + 0.333 * l1 + 0.333 * l2;\n"
88 " gl_Position = gl_ModelViewProjectionMatrix * middle;\n"
89 " gl_FrontColor = gl_TexCoordIn[0][0];\n"
90 " EmitVertex();\n"
91 " gl_FrontColor = gl_TexCoordIn[0][0];\n"
92 " gl_Position = gl_ModelViewProjectionMatrix * (middle + 0.1 * vec4(N, 0.0));\n"
93 " EmitVertex();\n"
94 " gl_FrontColor = gl_TexCoordIn[0][0];\n"
95 " gl_Position = gl_ModelViewProjectionMatrix * (middle + vec4(0.01,0.01,0.01,0.0));\n"
96 " EmitVertex();\n"
97 " EndPrimitive();\n"
98 "\n"
99 "}\n";
101 // Initialize GLUT & OpenSG and set up the scene
102 int doMain(int argc, char **argv)
104 // OSG init
105 OSG::osgInit(argc,argv);
107 // GLUT init
108 int winid = setupGLUT(&argc, argv);
110 // the connection between GLUT and OpenSG
111 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
112 gwin->setGlutId(winid);
113 gwin->setSize( 800, 800 );
114 gwin->init();
116 // Create the shader material
117 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
119 OSG::SHLChunkUnrecPtr shl = OSG::SHLChunk::create();
121 shl->setProgramParameter(GL_GEOMETRY_INPUT_TYPE_EXT,
122 GL_TRIANGLES);
123 shl->setProgramParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT,
124 GL_TRIANGLE_STRIP);
125 shl->setProgramParameter(GL_GEOMETRY_VERTICES_OUT_EXT, 6);
127 shl->setVertexProgram(_vertex_shader);
128 shl->setFragmentProgram(_fragment_shader);
129 shl->setGeometryProgram(_geometry_shader);
131 cmat->addChunk(shl);
133 // create root node
134 _scene = OSG::Node::create();
136 // create torus
137 OSG::GeometryUnrecPtr geo = OSG::makeTorusGeo(.8, 1.8, 128, 128);
139 geo->setMaterial(cmat);
141 OSG::NodeUnrecPtr torus = OSG::Node::create();
143 torus->setCore(geo);
145 // add torus to scene
146 OSG::GroupUnrecPtr group = OSG::Group::create();
148 _scene->setCore(group);
149 _scene->addChild(torus);
151 // create the SimpleSceneManager helper
152 _mgr = OSG::SimpleSceneManager::create();
154 // tell the manager what to manage
155 _mgr->setWindow(gwin );
156 _mgr->setRoot(_scene);
158 // show the whole scene
159 _mgr->showAll();
161 return 0;
164 // Initialize GLUT & OpenSG and set up the scene
165 int main(int argc, char **argv)
167 if(doMain(argc, argv) != 0)
168 return 1;
170 // GLUT main loop
171 glutMainLoop();
173 return 0;
176 // GLUT callback functions
179 // redraw the window
180 void display(void)
182 // render scene
183 _mgr->redraw();
186 // react to size changes
187 void reshape(int w, int h)
189 _mgr->resize(w, h);
190 glutPostRedisplay();
193 // react to mouse button presses
194 void mouse(int button, int state, int x, int y)
196 if (state)
197 _mgr->mouseButtonRelease(button, x, y);
198 else
199 _mgr->mouseButtonPress(button, x, y);
201 glutPostRedisplay();
204 // react to mouse motions with pressed buttons
205 void motion(int x, int y)
207 _mgr->mouseMove(x, y);
208 glutPostRedisplay();
211 // react to keys
212 void keyboard(unsigned char k, int x, int y)
214 switch(k)
216 case 27:
217 case 'q':
219 _scene = NULL;
220 _mgr = NULL;
222 OSG::osgExit();
224 exit(1);
225 break;
226 case 'w':
227 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
228 printf("wrote scene.osb.gz\n");
229 break;
232 glutPostRedisplay();
235 // setup the GLUT library which handles the windows for us
236 int setupGLUT(int *argc, char *argv[])
238 glutInit(argc, argv);
239 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
241 int winid = glutCreateWindow("OpenSG GLSL Geometry Shader");
243 glutReshapeFunc(reshape);
244 glutDisplayFunc(display);
245 glutMouseFunc(mouse);
246 glutMotionFunc(motion);
247 glutKeyboardFunc(keyboard);
249 return winid;
253 #else
255 int main(void)
257 return 0;
260 #endif