fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / SHL / testSHLBumpMap_compat.cpp
bloba97e81f235b45f15bde32f591a9210db0d335b3e
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 #ifdef OSG_1_COMPAT
12 #include "OSGSimpleGeometry.h"
13 #include "OSGGLUT.h"
14 #include "OSGGLUTWindow.h"
15 #include "OSGSimpleSceneManager.h"
16 #include "OSGAction.h"
17 #include "OSGSceneFileHandler.h"
18 #include "OSGBaseFunctions.h"
20 #include "OSGNode.h"
21 #include "OSGGroup.h"
22 #include "OSGTransform.h"
23 #include "OSGPointLight.h"
25 #include "OSGImage.h"
26 #include "OSGChunkMaterial.h"
27 #include "OSGMaterialChunk.h"
28 #include "OSGTextureObjChunk.h"
29 #include "OSGTextureEnvChunk.h"
30 #include "OSGSHLChunk.h"
32 // vertex shader program for bump mapping in surface local coordinates
33 static std::string _vp_program =
34 "varying vec3 lightDir; // interpolated surface local coordinate light direction\n"
35 "varying vec3 viewDir; // interpolated surface local coordinate view direction\n"
37 "void main(void)\n"
38 "{\n"
39 " // Do standard vertex stuff\n"
41 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
42 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
44 " // Compute the binormal\n"
46 " vec3 n = normalize(gl_NormalMatrix * gl_Normal);\n"
47 " //vec3 t = normalize(gl_NormalMatrix * vec3 (gl_Color));\n"
48 " vec3 t = normalize(cross(vec3(1.141, 2.78, 3.14), n));\n"
49 " vec3 b = cross(n, t);\n"
51 " // Transform light position into surface local coordinates\n"
53 " vec3 LightPosition = gl_LightSource[0].position.xyz;\n"
55 " vec3 v;\n"
56 " v.x = dot(LightPosition, t);\n"
57 " v.y = dot(LightPosition, b);\n"
58 " v.z = dot(LightPosition, n);\n"
60 " lightDir = normalize(v);\n"
62 " vec3 pos = vec3 (gl_ModelViewMatrix * gl_Vertex);\n"
64 " v.x = dot(pos, t);\n"
65 " v.y = dot(pos, b);\n"
66 " v.z = dot(pos, n);\n"
68 " viewDir = normalize(v);\n"
69 "\n"
70 "}\n";
72 // fragment shader program for bump mapping in surface local coordinates
73 static std::string _fp_program =
74 "uniform sampler2D sampler2d; // value of sampler2d = 3\n"
75 "varying vec3 lightDir; // interpolated surface local coordinate light direction\n"
76 "varying vec3 viewDir; // interpolated surface local coordinate view direction\n"
78 "const float diffuseFactor = 0.7;\n"
79 "const float specularFactor = 0.7;\n"
80 "vec3 basecolor = vec3 (0.8, 0.7, 0.3);\n"
82 "void main (void)\n"
83 "{\n"
84 " vec3 norm;\n"
85 " vec3 r;\n"
86 " vec3 color;\n"
87 " float intensity;\n"
88 " float spec;\n"
89 " float d;\n"
90 " // Fetch normal from normal map\n"
91 " norm = vec3(texture2D(sampler2d, vec2 (gl_TexCoord[0])));\n"
92 " norm = (norm - 0.5) * 2.0;\n"
93 " norm.y = -norm.y;\n"
94 " intensity = max(dot(lightDir, norm), 0.0) * diffuseFactor;\n"
95 " // Compute specular reflection component\n"
96 " d = 2.0 * dot(lightDir, norm);\n"
97 " r = d * norm;\n"
98 " r = lightDir - r;\n"
99 " spec = pow(max(dot(r, viewDir), 0.0) , 6.0) * specularFactor;\n"
100 " intensity += min (spec, 1.0);\n"
101 " // Compute final color value\n"
102 " color = clamp(basecolor * intensity, 0.0, 1.0);\n"
103 " // Write out final fragment color\n"
104 " gl_FragColor = vec4 (color, 1.0);\n"
105 "\n"
106 "}\n";
110 // ------------------- global vars ----------------------
112 // The SimpleSceneManager to manage simple applications
113 static OSG::SimpleSceneManagerRefPtr _mgr;
114 // The scene
115 static OSG::NodeRecPtr _scene;
117 // forward declaration so we can have the interesting stuff upfront
118 int setupGLUT( int *argc, char *argv[] );
120 // Initialize GLUT & OpenSG and set up the scene
121 int main(int argc, char **argv)
123 printf("Usage: testCGShader [normal map filename]\n");
124 const char *normal_map_img_name = "opensg_logoDOT3.png";
126 OSG::Color4f tmp;
128 if( argc > 1 )
129 normal_map_img_name = argv[1];
131 // OSG init
132 OSG::osgInit(argc,argv);
134 // GLUT init
135 int winid = setupGLUT(&argc, argv);
137 // the connection between GLUT and OpenSG
138 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
139 gwin->setGlutId(winid);
140 gwin->setSize( 800, 800 );
141 gwin->init();
143 // Create the shader material
145 // Read the image for the normal texture
146 OSG::ImageUnrecPtr normal_map_img = OSG::Image::create();
147 if(!normal_map_img->read(normal_map_img_name))
149 fprintf(stderr, "Couldn't read normalmap texture '%s'!\n", normal_map_img_name);
150 return 1;
153 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
155 OSG::MaterialChunkUnrecPtr matc = OSG::MaterialChunk::create();
157 matc->setAmbient(OSG::Color4f(0.1, 0.1, 0.1, 1.0));
158 matc->setDiffuse(OSG::Color4f(0.3, 0.3, 0.3, 1.0));
159 matc->setSpecular(OSG::Color4f(0.8, 0.8, 0.8, 1.0));
160 matc->setShininess(100);
161 matc->setLit(true);
163 OSG::SHLChunkUnrecPtr shl = OSG::SHLChunk::create();
165 shl->setVertexProgram(_vp_program);
166 shl->setFragmentProgram(_fp_program);
168 OSG::TextureObjChunkUnrecPtr tex_normal_map =
169 OSG::TextureObjChunk::create();
170 OSG::TextureEnvChunkUnrecPtr tex_normal_map_env =
171 OSG::TextureEnvChunk::create();
173 tex_normal_map->setImage(normal_map_img);
174 tex_normal_map->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
175 tex_normal_map->setMagFilter(GL_LINEAR);
176 tex_normal_map->setWrapS(GL_REPEAT);
177 tex_normal_map->setWrapT(GL_REPEAT);
178 tex_normal_map_env->setEnvMode(GL_MODULATE);
180 //cmat->addChunk(matc);
181 cmat->addChunk(shl);
182 cmat->addChunk(tex_normal_map);
183 cmat->addChunk(tex_normal_map_env);
186 // create root node
187 _scene = OSG::Node::create();
189 // create geometry
190 //GeometryPtr geo = makeLatLongSphereGeo (100, 100, 1.0);
191 OSG::GeometryUnrecPtr geo = OSG::makePlaneGeo(1.0, 1.0, 100, 100);
193 geo->setMaterial(cmat);
195 OSG::NodeUnrecPtr torus = OSG::Node::create();
196 torus->setCore(geo);
198 // add torus to scene
199 OSG::GroupUnrecPtr group = OSG::Group::create();
201 _scene->setCore(group);
202 _scene->addChild(torus);
204 // create the SimpleSceneManager helper
205 _mgr = OSG::SimpleSceneManager::create();
207 // tell the manager what to manage
208 _mgr->setWindow(gwin );
209 _mgr->setRoot(_scene);
212 // create point headlight
213 _mgr->turnHeadlightOff();
214 NodePtr headlight = _mgr->getHighlight();
215 PointLightPtr light = PointLight::create();
216 beginEditCP(light);
217 light->setAmbient (.3, .3, .3, 1);
218 light->setDiffuse ( 1, 1, 1, 1);
219 light->setSpecular ( 1, 1, 1, 1);
220 light->setBeacon (_mgr->getCamera()->getBeacon());
221 endEditCP(light);
222 beginEditCP(_scene);
223 _scene->setCore(light);
224 endEditCP(_scene);
227 // show the whole scene
228 _mgr->showAll();
230 // GLUT main loop
231 glutMainLoop();
233 return 0;
237 // GLUT callback functions
240 // redraw the window
241 void display(void)
243 // render scene
244 _mgr->redraw();
247 // react to size changes
248 void reshape(int w, int h)
250 _mgr->resize(w, h);
251 glutPostRedisplay();
254 // react to mouse button presses
255 void mouse(int button, int state, int x, int y)
257 if (state)
258 _mgr->mouseButtonRelease(button, x, y);
259 else
260 _mgr->mouseButtonPress(button, x, y);
262 glutPostRedisplay();
265 // react to mouse motions with pressed buttons
266 void motion(int x, int y)
268 _mgr->mouseMove(x, y);
269 glutPostRedisplay();
272 // react to keys
273 void keyboard(unsigned char k, int x, int y)
275 switch(k)
277 case 27:
278 case 'q':
280 _mgr = NULL;
281 _scene = NULL;
283 OSG::osgExit();
284 exit(1);
285 break;
286 case 'w':
287 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
288 printf("wrote scene.osb.gz\n");
289 break;
292 glutPostRedisplay();
295 // setup the GLUT library which handles the windows for us
296 int setupGLUT(int *argc, char *argv[])
298 glutInit(argc, argv);
299 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
301 int winid = glutCreateWindow("OpenSG CG Shader");
303 glutReshapeFunc(reshape);
304 glutDisplayFunc(display);
305 glutMouseFunc(mouse);
306 glutMotionFunc(motion);
307 glutKeyboardFunc(keyboard);
309 return winid;
312 #else
314 int main(void)
316 return 0;
319 #endif