fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / Chunks / testSHLBumpMapDefAttr_shaderprog.cpp
blob65d7372c5c06375f83b24e08f777296747f3c64f
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 "OSGGLUT.h"
8 #include "OSGConfig.h"
10 #include "OSGSimpleGeometry.h"
11 #include "OSGGLUT.h"
12 #include "OSGGLUTWindow.h"
13 #include "OSGSimpleSceneManager.h"
14 #include "OSGAction.h"
15 #include "OSGSceneFileHandler.h"
16 #include "OSGBaseFunctions.h"
18 #include "OSGNode.h"
19 #include "OSGGroup.h"
20 #include "OSGTransform.h"
21 #include "OSGPointLight.h"
23 #include "OSGImage.h"
24 #include "OSGChunkMaterial.h"
25 #include "OSGMaterialChunk.h"
26 #include "OSGTextureObjChunk.h"
27 #include "OSGTextureEnvChunk.h"
28 #include "OSGShaderProgramChunk.h"
29 #include "OSGShaderProgram.h"
31 // vertex shader program for bump mapping in surface local coordinates
32 static std::string _vp_program =
33 "varying vec3 lightDir; // interpolated surface local coordinate light direction\n"
34 "varying vec3 viewDir; // interpolated surface local coordinate view direction\n"
36 "attribute vec3 osg_Normal;\n"
37 "attribute vec2 osg_MultiTexCoord0;\n"
38 "attribute vec3 osg_Vertex;\n"
40 "void main(void)\n"
41 "{\n"
42 " // Do standard vertex stuff\n"
44 " gl_Position = gl_ModelViewProjectionMatrix * vec4(osg_Vertex, 1.0);\n"
45 " gl_TexCoord[0] = vec4(osg_MultiTexCoord0, 0.0, 0.0);\n"
47 " // Compute the binormal\n"
49 " vec3 n = normalize(gl_NormalMatrix * osg_Normal);\n"
50 " //vec3 t = normalize(gl_NormalMatrix * vec3 (gl_Color));\n"
51 " vec3 t = normalize(cross(vec3(1.141, 2.78, 3.14), n));\n"
52 " vec3 b = cross(n, t);\n"
54 " // Transform light position into surface local coordinates\n"
56 " vec3 LightPosition = gl_LightSource[0].position.xyz;\n"
58 " vec3 v;\n"
59 " v.x = dot(LightPosition, t);\n"
60 " v.y = dot(LightPosition, b);\n"
61 " v.z = dot(LightPosition, n);\n"
63 " lightDir = normalize(v);\n"
65 " vec3 pos = vec3 (gl_ModelViewMatrix * vec4(osg_Vertex, 1.0));\n"
67 " v.x = dot(pos, t);\n"
68 " v.y = dot(pos, b);\n"
69 " v.z = dot(pos, n);\n"
71 " viewDir = normalize(v);\n"
72 "\n"
73 "}\n";
75 // fragment shader program for bump mapping in surface local coordinates
76 static std::string _fp_program =
77 "uniform sampler2D sampler2d; // value of sampler2d = 3\n"
78 "varying vec3 lightDir; // interpolated surface local coordinate light direction\n"
79 "varying vec3 viewDir; // interpolated surface local coordinate view direction\n"
81 "const float diffuseFactor = 0.7;\n"
82 "const float specularFactor = 0.7;\n"
83 "vec3 basecolor = vec3 (0.8, 0.7, 0.3);\n"
85 "void main (void)\n"
86 "{\n"
87 " vec3 norm;\n"
88 " vec3 r;\n"
89 " vec3 color;\n"
90 " float intensity;\n"
91 " float spec;\n"
92 " float d;\n"
93 " // Fetch normal from normal map\n"
94 " norm = vec3(texture2D(sampler2d, vec2 (gl_TexCoord[0])));\n"
95 " norm = (norm - 0.5) * 2.0;\n"
96 " norm.y = -norm.y;\n"
97 " intensity = max(dot(lightDir, norm), 0.0) * diffuseFactor;\n"
98 " // Compute specular reflection component\n"
99 " d = 2.0 * dot(lightDir, norm);\n"
100 " r = d * norm;\n"
101 " r = lightDir - r;\n"
102 " spec = pow(max(dot(r, viewDir), 0.0) , 6.0) * specularFactor;\n"
103 " intensity += min (spec, 1.0);\n"
104 " // Compute final color value\n"
105 " color = clamp(basecolor * intensity, 0.0, 1.0);\n"
106 " // Write out final fragment color\n"
107 " gl_FragColor = vec4 (color, 1.0);\n"
108 "\n"
109 "}\n";
113 // ------------------- global vars ----------------------
115 // The SimpleSceneManager to manage simple applications
116 static OSG::SimpleSceneManagerRefPtr _mgr;
117 // The scene
118 static OSG::NodeRecPtr _scene;
120 // forward declaration so we can have the interesting stuff upfront
121 int setupGLUT( int *argc, char *argv[] );
123 // Initialize GLUT & OpenSG and set up the scene
124 int doMain(int argc, char **argv)
126 printf("Usage: testCGShader [normal map filename]\n");
127 const char *normal_map_img_name = "opensg_logoDOT3.png";
129 OSG::Color4f tmp;
131 if( argc > 1 )
132 normal_map_img_name = argv[1];
134 // OSG init
135 OSG::osgInit(argc,argv);
137 // GLUT init
138 int winid = setupGLUT(&argc, argv);
140 // the connection between GLUT and OpenSG
141 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
142 gwin->setGlutId(winid);
143 gwin->setSize( 800, 800 );
144 gwin->init();
146 // Create the shader material
148 // Read the image for the normal texture
149 OSG::ImageUnrecPtr normal_map_img = OSG::Image::create();
150 if(!normal_map_img->read(normal_map_img_name))
152 fprintf(stderr, "Couldn't read normalmap texture '%s'!\n", normal_map_img_name);
153 return 1;
156 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
158 OSG::MaterialChunkUnrecPtr matc = OSG::MaterialChunk::create();
160 matc->setAmbient(OSG::Color4f(0.1, 0.1, 0.1, 1.0));
161 matc->setDiffuse(OSG::Color4f(0.3, 0.3, 0.3, 1.0));
162 matc->setSpecular(OSG::Color4f(0.8, 0.8, 0.8, 1.0));
163 matc->setShininess(100);
164 matc->setLit(true);
166 OSG::ShaderProgramChunkUnrecPtr shl = OSG::ShaderProgramChunk::create();
168 OSG::ShaderProgramUnrecPtr shl_vp =
169 OSG::ShaderProgram::createVertexShader(true);
171 shl_vp->setProgram(_vp_program);
173 shl->addShader(shl_vp);
175 OSG::ShaderProgramUnrecPtr shl_fp =
176 OSG::ShaderProgram::createFragmentShader();
178 shl_fp->setProgram(_fp_program);
180 shl->addShader(shl_fp);
183 OSG::TextureObjChunkUnrecPtr tex_normal_map =
184 OSG::TextureObjChunk::create();
185 OSG::TextureEnvChunkUnrecPtr tex_normal_map_env =
186 OSG::TextureEnvChunk::create();
188 tex_normal_map->setImage(normal_map_img);
189 tex_normal_map->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
190 tex_normal_map->setMagFilter(GL_LINEAR);
191 tex_normal_map->setWrapS(GL_REPEAT);
192 tex_normal_map->setWrapT(GL_REPEAT);
193 tex_normal_map_env->setEnvMode(GL_MODULATE);
195 //cmat->addChunk(matc);
196 cmat->addChunk(shl);
197 cmat->addChunk(tex_normal_map);
198 cmat->addChunk(tex_normal_map_env);
201 // create root node
202 _scene = OSG::Node::create();
204 // create geometry
205 //GeometryPtr geo = makeLatLongSphereGeo (100, 100, 1.0);
206 OSG::GeometryUnrecPtr geo = OSG::makePlaneGeo(1.0, 1.0, 100, 100);
208 geo->setMaterial(cmat);
210 OSG::NodeUnrecPtr torus = OSG::Node::create();
211 torus->setCore(geo);
213 // add torus to scene
214 OSG::GroupUnrecPtr group = OSG::Group::create();
216 _scene->setCore(group);
217 _scene->addChild(torus);
219 // create the SimpleSceneManager helper
220 _mgr = OSG::SimpleSceneManager::create();
222 // tell the manager what to manage
223 _mgr->setWindow(gwin );
224 _mgr->setRoot(_scene);
226 // show the whole scene
227 _mgr->showAll();
229 return 0;
232 int main(int argc, char **argv)
234 doMain(argc, argv);
236 // GLUT main loop
237 glutMainLoop();
239 return 0;
243 // GLUT callback functions
246 // redraw the window
247 void display(void)
249 // render scene
250 _mgr->redraw();
253 // react to size changes
254 void reshape(int w, int h)
256 _mgr->resize(w, h);
257 glutPostRedisplay();
260 // react to mouse button presses
261 void mouse(int button, int state, int x, int y)
263 if (state)
264 _mgr->mouseButtonRelease(button, x, y);
265 else
266 _mgr->mouseButtonPress(button, x, y);
268 glutPostRedisplay();
271 // react to mouse motions with pressed buttons
272 void motion(int x, int y)
274 _mgr->mouseMove(x, y);
275 glutPostRedisplay();
278 // react to keys
279 void keyboard(unsigned char k, int x, int y)
281 switch(k)
283 case 27:
284 case 'q':
286 _mgr = NULL;
287 _scene = NULL;
289 OSG::osgExit();
290 exit(1);
291 break;
292 case 'w':
293 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
294 printf("wrote scene.osb.gz\n");
295 break;
298 glutPostRedisplay();
301 // setup the GLUT library which handles the windows for us
302 int setupGLUT(int *argc, char *argv[])
304 glutInit(argc, argv);
305 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
307 int winid = glutCreateWindow("OpenSG CG Shader");
309 glutReshapeFunc(reshape);
310 glutDisplayFunc(display);
311 glutMouseFunc(mouse);
312 glutMotionFunc(motion);
313 glutKeyboardFunc(keyboard);
315 return winid;