fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / Chunks / testSHLBumpMap_shaderprog.cpp
blob4d9717712d7186a15a95ed4f5d00202089e68a0a
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 "void main(void)\n"
37 "{\n"
38 " // Do standard vertex stuff\n"
40 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
41 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
43 " // Compute the binormal\n"
45 " vec3 n = normalize(gl_NormalMatrix * gl_Normal);\n"
46 " //vec3 t = normalize(gl_NormalMatrix * vec3 (gl_Color));\n"
47 " vec3 t = normalize(cross(vec3(1.141, 2.78, 3.14), n));\n"
48 " vec3 b = cross(n, t);\n"
50 " // Transform light position into surface local coordinates\n"
52 " vec3 LightPosition = gl_LightSource[0].position.xyz;\n"
54 " vec3 v;\n"
55 " v.x = dot(LightPosition, t);\n"
56 " v.y = dot(LightPosition, b);\n"
57 " v.z = dot(LightPosition, n);\n"
59 " lightDir = normalize(v);\n"
61 " vec3 pos = vec3 (gl_ModelViewMatrix * gl_Vertex);\n"
63 " v.x = dot(pos, t);\n"
64 " v.y = dot(pos, b);\n"
65 " v.z = dot(pos, n);\n"
67 " viewDir = normalize(v);\n"
68 "\n"
69 "}\n";
71 // fragment shader program for bump mapping in surface local coordinates
72 static std::string _fp_program =
73 "uniform sampler2D sampler2d; // value of sampler2d = 3\n"
74 "varying vec3 lightDir; // interpolated surface local coordinate light direction\n"
75 "varying vec3 viewDir; // interpolated surface local coordinate view direction\n"
77 "const float diffuseFactor = 0.7;\n"
78 "const float specularFactor = 0.7;\n"
79 "vec3 basecolor = vec3 (0.8, 0.7, 0.3);\n"
81 "void main (void)\n"
82 "{\n"
83 " vec3 norm;\n"
84 " vec3 r;\n"
85 " vec3 color;\n"
86 " float intensity;\n"
87 " float spec;\n"
88 " float d;\n"
89 " // Fetch normal from normal map\n"
90 " norm = vec3(texture2D(sampler2d, vec2 (gl_TexCoord[0])));\n"
91 " norm = (norm - 0.5) * 2.0;\n"
92 " norm.y = -norm.y;\n"
93 " intensity = max(dot(lightDir, norm), 0.0) * diffuseFactor;\n"
94 " // Compute specular reflection component\n"
95 " d = 2.0 * dot(lightDir, norm);\n"
96 " r = d * norm;\n"
97 " r = lightDir - r;\n"
98 " spec = pow(max(dot(r, viewDir), 0.0) , 6.0) * specularFactor;\n"
99 " intensity += min (spec, 1.0);\n"
100 " // Compute final color value\n"
101 " color = clamp(basecolor * intensity, 0.0, 1.0);\n"
102 " // Write out final fragment color\n"
103 " gl_FragColor = vec4 (color, 1.0);\n"
104 "\n"
105 "}\n";
109 // ------------------- global vars ----------------------
111 // The SimpleSceneManager to manage simple applications
112 static OSG::SimpleSceneManagerRefPtr _mgr;
113 // The scene
114 static OSG::NodeRecPtr _scene;
116 // forward declaration so we can have the interesting stuff upfront
117 int setupGLUT( int *argc, char *argv[] );
119 // Initialize GLUT & OpenSG and set up the scene
120 int doMain(int argc, char **argv)
122 printf("Usage: testCGShader [normal map filename]\n");
123 const char *normal_map_img_name = "opensg_logoDOT3.png";
125 OSG::Color4f tmp;
127 if( argc > 1 )
128 normal_map_img_name = argv[1];
130 // OSG init
131 OSG::osgInit(argc,argv);
133 // GLUT init
134 int winid = setupGLUT(&argc, argv);
136 // the connection between GLUT and OpenSG
137 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
138 gwin->setGlutId(winid);
139 gwin->setSize( 800, 800 );
140 gwin->init();
142 // Create the shader material
144 // Read the image for the normal texture
145 OSG::ImageUnrecPtr normal_map_img = OSG::Image::create();
146 if(!normal_map_img->read(normal_map_img_name))
148 fprintf(stderr, "Couldn't read normalmap texture '%s'!\n", normal_map_img_name);
149 return 1;
152 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
154 OSG::MaterialChunkUnrecPtr matc = OSG::MaterialChunk::create();
156 matc->setAmbient(OSG::Color4f(0.1, 0.1, 0.1, 1.0));
157 matc->setDiffuse(OSG::Color4f(0.3, 0.3, 0.3, 1.0));
158 matc->setSpecular(OSG::Color4f(0.8, 0.8, 0.8, 1.0));
159 matc->setShininess(100);
160 matc->setLit(true);
162 OSG::ShaderProgramChunkUnrecPtr shl = OSG::ShaderProgramChunk::create();
164 OSG::ShaderProgramUnrecPtr shl_vp =
165 OSG::ShaderProgram::createVertexShader();
167 shl_vp->setProgram(_vp_program);
169 shl->addShader(shl_vp);
171 OSG::ShaderProgramUnrecPtr shl_fp =
172 OSG::ShaderProgram::createFragmentShader();
174 shl_fp->setProgram(_fp_program);
176 shl->addShader(shl_fp);
179 OSG::TextureObjChunkUnrecPtr tex_normal_map =
180 OSG::TextureObjChunk::create();
181 OSG::TextureEnvChunkUnrecPtr tex_normal_map_env =
182 OSG::TextureEnvChunk::create();
184 tex_normal_map->setImage(normal_map_img);
185 tex_normal_map->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
186 tex_normal_map->setMagFilter(GL_LINEAR);
187 tex_normal_map->setWrapS(GL_REPEAT);
188 tex_normal_map->setWrapT(GL_REPEAT);
189 tex_normal_map_env->setEnvMode(GL_MODULATE);
191 //cmat->addChunk(matc);
192 cmat->addChunk(shl);
193 cmat->addChunk(tex_normal_map);
194 cmat->addChunk(tex_normal_map_env);
197 // create root node
198 _scene = OSG::Node::create();
200 // create geometry
201 //GeometryPtr geo = makeLatLongSphereGeo (100, 100, 1.0);
202 OSG::GeometryUnrecPtr geo = OSG::makePlaneGeo(1.0, 1.0, 100, 100);
204 geo->setMaterial(cmat);
206 OSG::NodeUnrecPtr torus = OSG::Node::create();
207 torus->setCore(geo);
209 // add torus to scene
210 OSG::GroupUnrecPtr group = OSG::Group::create();
212 _scene->setCore(group);
213 _scene->addChild(torus);
215 // create the SimpleSceneManager helper
216 _mgr = OSG::SimpleSceneManager::create();
218 // tell the manager what to manage
219 _mgr->setWindow(gwin );
220 _mgr->setRoot(_scene);
222 // show the whole scene
223 _mgr->showAll();
225 return 0;
228 int main(int argc, char **argv)
230 doMain(argc, argv);
232 // GLUT main loop
233 glutMainLoop();
235 return 0;
239 // GLUT callback functions
242 // redraw the window
243 void display(void)
245 // render scene
246 _mgr->redraw();
249 // react to size changes
250 void reshape(int w, int h)
252 _mgr->resize(w, h);
253 glutPostRedisplay();
256 // react to mouse button presses
257 void mouse(int button, int state, int x, int y)
259 if (state)
260 _mgr->mouseButtonRelease(button, x, y);
261 else
262 _mgr->mouseButtonPress(button, x, y);
264 glutPostRedisplay();
267 // react to mouse motions with pressed buttons
268 void motion(int x, int y)
270 _mgr->mouseMove(x, y);
271 glutPostRedisplay();
274 // react to keys
275 void keyboard(unsigned char k, int x, int y)
277 switch(k)
279 case 27:
280 case 'q':
282 _mgr = NULL;
283 _scene = NULL;
285 OSG::osgExit();
286 exit(1);
287 break;
288 case 'w':
289 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
290 printf("wrote scene.osb.gz\n");
291 break;
294 glutPostRedisplay();
297 // setup the GLUT library which handles the windows for us
298 int setupGLUT(int *argc, char *argv[])
300 glutInit(argc, argv);
301 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
303 int winid = glutCreateWindow("OpenSG CG Shader");
305 glutReshapeFunc(reshape);
306 glutDisplayFunc(display);
307 glutIdleFunc(display);
308 glutMouseFunc(mouse);
309 glutMotionFunc(motion);
310 glutKeyboardFunc(keyboard);
312 return winid;