1 // OpenSG example: testSHL
3 // Demonstrates the use of the SHLChunk
4 // Implements a simple bumpmapping via vertex and fragment shader.
11 #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 "OSGSHLChunk.h"
32 // ------------------- global vars ----------------------
34 // The SimpleSceneManager to manage simple applications
35 static OSG::SimpleSceneManagerRefPtr _mgr
;
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
=
45 " gl_Position = gl_Vertex;\n"
46 " gl_TexCoord[0] = vec4(abs(gl_Normal), 0.0);\n"
50 static std::string _fragment_shader
=
53 " gl_FragColor = gl_Color;\n"
57 // ok we create some triangles to draw the face normals.
58 static std::string _geometry_shader
=
60 "#extension GL_EXT_geometry_shader4 : enable\n"
64 " vec4 v1 = gl_PositionIn[0];\n"
65 " vec4 v2 = gl_PositionIn[1];\n"
66 " vec4 v3 = gl_PositionIn[2];\n"
68 " vec4 l1 = v2 - v1;\n"
69 " vec4 l2 = v3 - v1;\n"
71 " gl_Position = gl_ModelViewProjectionMatrix * v1;\n"
72 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
74 " gl_Position = gl_ModelViewProjectionMatrix * v2;\n"
75 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
77 " gl_Position = gl_ModelViewProjectionMatrix * v3;\n"
78 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
82 " vec3 l1n = l1.xyz;\n"
83 " vec3 l2n = l2.xyz;\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"
91 " gl_FrontColor = gl_TexCoordIn[0][0];\n"
92 " gl_Position = gl_ModelViewProjectionMatrix * (middle + 0.1 * vec4(N, 0.0));\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"
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();
112 gwin
->setGlutId(winid
);
113 gwin
->setSize( 800, 800 );
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
,
123 shl
->setProgramParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT
,
125 shl
->setProgramParameter(GL_GEOMETRY_VERTICES_OUT_EXT
, 6);
127 shl
->setVertexProgram(_vertex_shader
);
128 shl
->setFragmentProgram(_fragment_shader
);
129 shl
->setGeometryProgram(_geometry_shader
);
134 _scene
= OSG::Node::create();
137 OSG::GeometryUnrecPtr geo
= OSG::makeTorusGeo(.8, 1.8, 128, 128);
139 geo
->setMaterial(cmat
);
141 OSG::NodeUnrecPtr torus
= OSG::Node::create();
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
164 // Initialize GLUT & OpenSG and set up the scene
165 int main(int argc
, char **argv
)
167 if(doMain(argc
, argv
) != 0)
176 // GLUT callback functions
186 // react to size changes
187 void reshape(int w
, int h
)
193 // react to mouse button presses
194 void mouse(int button
, int state
, int x
, int y
)
197 _mgr
->mouseButtonRelease(button
, x
, y
);
199 _mgr
->mouseButtonPress(button
, x
, y
);
204 // react to mouse motions with pressed buttons
205 void motion(int x
, int y
)
207 _mgr
->mouseMove(x
, y
);
212 void keyboard(unsigned char k
, int x
, int y
)
227 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
228 printf("wrote scene.osb.gz\n");
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
);