fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / SHL / testSHLGeometryShader.cpp
blob3ec4532f928a9f74c65809f2b1d915ca6af112b5
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"
8 #include "OSGSimpleGeometry.h"
9 #include "OSGGLUT.h"
10 #include "OSGGLEXT.h"
11 #include "OSGGLUTWindow.h"
12 #include "OSGSimpleSceneManager.h"
13 #include "OSGAction.h"
14 #include "OSGSceneFileHandler.h"
15 #include "OSGBaseFunctions.h"
17 #include "OSGNode.h"
18 #include "OSGGroup.h"
19 #include "OSGTransform.h"
20 #include "OSGPointLight.h"
22 #include "OSGImage.h"
23 #include "OSGChunkMaterial.h"
24 #include "OSGMaterialChunk.h"
25 #include "OSGSimpleSHLChunk.h"
29 // ------------------- global vars ----------------------
31 // The SimpleSceneManager to manage simple applications
32 static OSG::SimpleSceneManagerRefPtr _mgr;
33 // The scene
34 static OSG::NodeRecPtr _scene;
36 // forward declaration so we can have the interesting stuff upfront
37 int setupGLUT( int *argc, char *argv[] );
39 static std::string _vertex_shader =
40 "void main(void)\n"
41 "{\n"
42 " gl_Position = gl_Vertex;\n"
43 " gl_TexCoord[0] = vec4(abs(gl_Normal), 0.0);\n"
44 "}\n"
45 "\n";
47 static std::string _fragment_shader =
48 "void main (void)\n"
49 "{\n"
50 " gl_FragColor = gl_Color;\n"
51 "\n"
52 "}\n";
54 // ok we create some triangles to draw the face normals.
55 static std::string _geometry_shader =
56 "#version 120\n"
57 "#extension GL_EXT_geometry_shader4 : enable\n"
58 "\n"
59 "void main(void)\n"
60 "{\n"
61 " vec4 v1 = gl_PositionIn[0];\n"
62 " vec4 v2 = gl_PositionIn[1];\n"
63 " vec4 v3 = gl_PositionIn[2];\n"
64 "\n"
65 " vec4 l1 = v2 - v1;\n"
66 " vec4 l2 = v3 - v1;\n"
67 "\n"
68 " gl_Position = gl_ModelViewProjectionMatrix * v1;\n"
69 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
70 " EmitVertex();\n"
71 " gl_Position = gl_ModelViewProjectionMatrix * v2;\n"
72 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
73 " EmitVertex();\n"
74 " gl_Position = gl_ModelViewProjectionMatrix * v3;\n"
75 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
76 " EmitVertex();\n"
77 " EndPrimitive();\n"
78 "\n"
79 " vec3 l1n = l1.xyz;\n"
80 " vec3 l2n = l2.xyz;\n"
81 "\n"
82 " vec3 N = cross(l1n.xyz, l2n.xyz);\n"
83 " N = normalize(N);\n"
84 " vec4 middle = v1 + 0.333 * l1 + 0.333 * l2;\n"
85 " gl_Position = gl_ModelViewProjectionMatrix * middle;\n"
86 " gl_FrontColor = gl_TexCoordIn[0][0];\n"
87 " EmitVertex();\n"
88 " gl_FrontColor = gl_TexCoordIn[0][0];\n"
89 " gl_Position = gl_ModelViewProjectionMatrix * (middle + 0.1 * vec4(N, 0.0));\n"
90 " EmitVertex();\n"
91 " gl_FrontColor = gl_TexCoordIn[0][0];\n"
92 " gl_Position = gl_ModelViewProjectionMatrix * (middle + vec4(0.01,0.01,0.01,0.0));\n"
93 " EmitVertex();\n"
94 " EndPrimitive();\n"
95 "\n"
96 "}\n";
98 // Initialize GLUT & OpenSG and set up the scene
99 int doMain(int argc, char **argv)
101 // OSG init
102 OSG::osgInit(argc,argv);
104 // GLUT init
105 int winid = setupGLUT(&argc, argv);
107 // the connection between GLUT and OpenSG
108 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
109 gwin->setGlutId(winid);
110 gwin->setSize( 800, 800 );
111 gwin->init();
113 // Create the shader material
114 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
116 OSG::SimpleSHLChunkUnrecPtr shl = OSG::SimpleSHLChunk::create();
118 shl->setProgramParameter(GL_GEOMETRY_INPUT_TYPE_EXT,
119 GL_TRIANGLES);
120 shl->setProgramParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT,
121 GL_TRIANGLE_STRIP);
122 shl->setProgramParameter(GL_GEOMETRY_VERTICES_OUT_EXT, 6);
124 shl->setVertexProgram(_vertex_shader);
125 shl->setFragmentProgram(_fragment_shader);
126 shl->setGeometryProgram(_geometry_shader);
128 cmat->addChunk(shl);
130 // create root node
131 _scene = OSG::Node::create();
133 // create torus
134 OSG::GeometryUnrecPtr geo = OSG::makeTorusGeo(.8f, 1.8f, 128, 128);
136 geo->setMaterial(cmat);
138 OSG::NodeUnrecPtr torus = OSG::Node::create();
140 torus->setCore(geo);
142 // add torus to scene
143 OSG::GroupUnrecPtr group = OSG::Group::create();
145 _scene->setCore(group);
146 _scene->addChild(torus);
148 // create the SimpleSceneManager helper
149 _mgr = OSG::SimpleSceneManager::create();
151 // tell the manager what to manage
152 _mgr->setWindow(gwin );
153 _mgr->setRoot(_scene);
155 // show the whole scene
156 _mgr->showAll();
158 return 0;
161 // Initialize GLUT & OpenSG and set up the scene
162 int main(int argc, char **argv)
164 if(doMain(argc, argv) != 0)
165 return 1;
167 // GLUT main loop
168 glutMainLoop();
170 return 0;
173 // GLUT callback functions
176 // redraw the window
177 void display(void)
179 // render scene
180 _mgr->redraw();
183 // react to size changes
184 void reshape(int w, int h)
186 _mgr->resize(w, h);
187 glutPostRedisplay();
190 // react to mouse button presses
191 void mouse(int button, int state, int x, int y)
193 if (state)
194 _mgr->mouseButtonRelease(button, x, y);
195 else
196 _mgr->mouseButtonPress(button, x, y);
198 glutPostRedisplay();
201 // react to mouse motions with pressed buttons
202 void motion(int x, int y)
204 _mgr->mouseMove(x, y);
205 glutPostRedisplay();
208 // react to keys
209 void keyboard(unsigned char k, int x, int y)
211 switch(k)
213 case 27:
214 case 'q':
216 _scene = NULL;
217 _mgr = NULL;
219 exit(1);
220 break;
221 case 'w':
222 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
223 printf("wrote scene.osb.gz\n");
224 break;
227 glutPostRedisplay();
230 // setup the GLUT library which handles the windows for us
231 int setupGLUT(int *argc, char *argv[])
233 glutInit(argc, argv);
234 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
236 int winid = glutCreateWindow("OpenSG GLSL Geometry Shader");
238 glutReshapeFunc(reshape);
239 glutDisplayFunc(display);
240 glutMouseFunc(mouse);
241 glutMotionFunc(motion);
242 glutKeyboardFunc(keyboard);
244 return winid;