changed: gcc8 base update
[opensg.git] / Source / System / State / Shader / Chunks / testSHLGeometryShader_shaderprog.cpp
blob1139caae5abdce7d64495a711563808d9756b32e
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 "OSGShaderProgramChunk.h"
26 #include "OSGShaderProgram.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::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,
140 GL_TRIANGLES);
141 shl_gp->setProgramParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT,
142 GL_TRIANGLE_STRIP);
143 shl_gp->setProgramParameter(GL_GEOMETRY_VERTICES_OUT_EXT, 6);
145 shl->addShader(shl_gp);
147 cmat->addChunk(shl);
149 // create root node
150 _scene = OSG::Node::create();
152 // create torus
153 OSG::GeometryUnrecPtr geo = OSG::makeTorusGeo(.8f, 1.8f, 128, 128);
155 geo->setMaterial(cmat);
157 OSG::NodeUnrecPtr torus = OSG::Node::create();
159 torus->setCore(geo);
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
175 _mgr->showAll();
177 return 0;
180 // Initialize GLUT & OpenSG and set up the scene
181 int main(int argc, char **argv)
183 if(doMain(argc, argv) != 0)
184 return 1;
186 // GLUT main loop
187 glutMainLoop();
189 return 0;
192 // GLUT callback functions
195 // redraw the window
196 void display(void)
198 // render scene
199 _mgr->redraw();
202 // react to size changes
203 void reshape(int w, int h)
205 _mgr->resize(w, h);
206 glutPostRedisplay();
209 // react to mouse button presses
210 void mouse(int button, int state, int x, int y)
212 if (state)
213 _mgr->mouseButtonRelease(button, x, y);
214 else
215 _mgr->mouseButtonPress(button, x, y);
217 glutPostRedisplay();
220 // react to mouse motions with pressed buttons
221 void motion(int x, int y)
223 _mgr->mouseMove(x, y);
224 glutPostRedisplay();
227 // react to keys
228 void keyboard(unsigned char k, int x, int y)
230 switch(k)
232 case 27:
233 case 'q':
235 _scene = NULL;
236 _mgr = NULL;
238 OSG::osgExit();
240 exit(1);
241 break;
242 case 'w':
243 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
244 printf("wrote scene.osb.gz\n");
245 break;
248 glutPostRedisplay();
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);
265 return winid;