1 // OpenSG example: testSHL
3 // Demonstrates the use of the SHLChunk
4 // Implements a simple bumpmapping via vertex and fragment shader.
8 #include "OSGSimpleGeometry.h"
11 #include "OSGGLUTWindow.h"
12 #include "OSGSimpleSceneManager.h"
13 #include "OSGAction.h"
14 #include "OSGSceneFileHandler.h"
15 #include "OSGBaseFunctions.h"
19 #include "OSGTransform.h"
20 #include "OSGPointLight.h"
23 #include "OSGChunkMaterial.h"
24 #include "OSGMaterialChunk.h"
25 #include "OSGShaderProgramChunk.h"
26 #include "OSGShaderProgram.h"
29 // ------------------- global vars ----------------------
31 // The SimpleSceneManager to manage simple applications
32 static OSG::SimpleSceneManagerRefPtr _mgr
;
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
=
42 " gl_Position = gl_Vertex;\n"
43 " gl_TexCoord[0] = vec4(abs(gl_Normal), 0.0);\n"
47 static std::string _fragment_shader
=
50 " gl_FragColor = gl_Color;\n"
54 // ok we create some triangles to draw the face normals.
55 static std::string _geometry_shader
=
57 "#extension GL_EXT_geometry_shader4 : enable\n"
61 " vec4 v1 = gl_PositionIn[0];\n"
62 " vec4 v2 = gl_PositionIn[1];\n"
63 " vec4 v3 = gl_PositionIn[2];\n"
65 " vec4 l1 = v2 - v1;\n"
66 " vec4 l2 = v3 - v1;\n"
68 " gl_Position = gl_ModelViewProjectionMatrix * v1;\n"
69 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
71 " gl_Position = gl_ModelViewProjectionMatrix * v2;\n"
72 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
74 " gl_Position = gl_ModelViewProjectionMatrix * v3;\n"
75 " gl_FrontColor = vec4(0.0, 1.0, 0.0, 0.0);\n"
79 " vec3 l1n = l1.xyz;\n"
80 " vec3 l2n = l2.xyz;\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"
88 " gl_FrontColor = gl_TexCoordIn[0][0];\n"
89 " gl_Position = gl_ModelViewProjectionMatrix * (middle + 0.1 * vec4(N, 0.0));\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"
98 // Initialize GLUT & OpenSG and set up the scene
99 int doMain(int argc
, char **argv
)
102 OSG::osgInit(argc
,argv
);
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 );
113 // Create the shader material
114 OSG::ChunkMaterialUnrecPtr cmat
= OSG::ChunkMaterial::create();
116 OSG::ShaderProgramChunkUnrecPtr shl
= OSG::ShaderProgramChunk::create();
118 OSG::ShaderProgramUnrecPtr shl_vp
=
119 OSG::ShaderProgram::createVertexShader();
121 shl_vp
->setProgram(_vertex_shader
);
123 shl
->addShader(shl_vp
);
126 OSG::ShaderProgramUnrecPtr shl_fp
=
127 OSG::ShaderProgram::createFragmentShader();
129 shl_fp
->setProgram(_fragment_shader
);
131 shl
->addShader(shl_fp
);
134 OSG::ShaderProgramUnrecPtr shl_gp
=
135 OSG::ShaderProgram::createGeometryShader();
137 shl_gp
->setProgram(_geometry_shader
);
139 shl_gp
->setProgramParameter(GL_GEOMETRY_INPUT_TYPE_EXT
,
141 shl_gp
->setProgramParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT
,
143 shl_gp
->setProgramParameter(GL_GEOMETRY_VERTICES_OUT_EXT
, 6);
145 shl
->addShader(shl_gp
);
150 _scene
= OSG::Node::create();
153 OSG::GeometryUnrecPtr geo
= OSG::makeTorusGeo(.8f
, 1.8f
, 128, 128);
155 geo
->setMaterial(cmat
);
157 OSG::NodeUnrecPtr torus
= OSG::Node::create();
161 // add torus to scene
162 OSG::GroupUnrecPtr group
= OSG::Group::create();
164 _scene
->setCore(group
);
165 _scene
->addChild(torus
);
167 // create the SimpleSceneManager helper
168 _mgr
= OSG::SimpleSceneManager::create();
170 // tell the manager what to manage
171 _mgr
->setWindow(gwin
);
172 _mgr
->setRoot(_scene
);
174 // show the whole scene
180 // Initialize GLUT & OpenSG and set up the scene
181 int main(int argc
, char **argv
)
183 if(doMain(argc
, argv
) != 0)
192 // GLUT callback functions
202 // react to size changes
203 void reshape(int w
, int h
)
209 // react to mouse button presses
210 void mouse(int button
, int state
, int x
, int y
)
213 _mgr
->mouseButtonRelease(button
, x
, y
);
215 _mgr
->mouseButtonPress(button
, x
, y
);
220 // react to mouse motions with pressed buttons
221 void motion(int x
, int y
)
223 _mgr
->mouseMove(x
, y
);
228 void keyboard(unsigned char k
, int x
, int y
)
243 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
244 printf("wrote scene.osb.gz\n");
251 // setup the GLUT library which handles the windows for us
252 int setupGLUT(int *argc
, char *argv
[])
254 glutInit(argc
, argv
);
255 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
257 int winid
= glutCreateWindow("OpenSG GLSL Geometry Shader");
259 glutReshapeFunc(reshape
);
260 glutDisplayFunc(display
);
261 glutMouseFunc(mouse
);
262 glutMotionFunc(motion
);
263 glutKeyboardFunc(keyboard
);