fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / Chunks / testSHLBumpMapAttr_shaderprog.cpp
blob1ed8a56c08d484c411dc48ebab260b40aef405d9
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 Normal;\n"
37 "attribute vec2 TexCoord0;\n"
38 "attribute vec3 Position;\n"
40 "void main(void)\n"
41 "{\n"
42 " // Do standard vertex stuff\n"
44 " gl_Position = gl_ModelViewProjectionMatrix * vec4(Position, 1.0);\n"
45 " gl_TexCoord[0] = vec4(TexCoord0, 0.0, 0.0);\n"
47 " // Compute the binormal\n"
49 " vec3 n = normalize(gl_NormalMatrix * 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(Position, 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();
171 shl_vp->setProgram(_vp_program);
172 shl_vp->setProgramAttribute(OSG::ShaderConstants::TexCoordsIndex, "TexCoord0");
173 shl_vp->setProgramAttribute(OSG::ShaderConstants::NormalsIndex, "Normal" );
174 shl_vp->setProgramAttribute(OSG::ShaderConstants::PositionsIndex, "Position");
176 shl->addShader(shl_vp);
178 OSG::ShaderProgramUnrecPtr shl_fp =
179 OSG::ShaderProgram::createFragmentShader();
181 shl_fp->setProgram(_fp_program);
183 shl->addShader(shl_fp);
186 OSG::TextureObjChunkUnrecPtr tex_normal_map =
187 OSG::TextureObjChunk::create();
188 OSG::TextureEnvChunkUnrecPtr tex_normal_map_env =
189 OSG::TextureEnvChunk::create();
191 tex_normal_map->setImage(normal_map_img);
192 tex_normal_map->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
193 tex_normal_map->setMagFilter(GL_LINEAR);
194 tex_normal_map->setWrapS(GL_REPEAT);
195 tex_normal_map->setWrapT(GL_REPEAT);
196 tex_normal_map_env->setEnvMode(GL_MODULATE);
198 //cmat->addChunk(matc);
199 cmat->addChunk(shl);
200 cmat->addChunk(tex_normal_map);
201 cmat->addChunk(tex_normal_map_env);
204 // create root node
205 _scene = OSG::Node::create();
207 // create geometry
208 //GeometryPtr geo = makeLatLongSphereGeo (100, 100, 1.0);
209 OSG::GeometryUnrecPtr geo = OSG::makePlaneGeo(1.0, 1.0, 100, 100);
211 geo->setMaterial(cmat);
213 OSG::NodeUnrecPtr torus = OSG::Node::create();
214 torus->setCore(geo);
216 // add torus to scene
217 OSG::GroupUnrecPtr group = OSG::Group::create();
219 _scene->setCore(group);
220 _scene->addChild(torus);
222 // create the SimpleSceneManager helper
223 _mgr = OSG::SimpleSceneManager::create();
225 // tell the manager what to manage
226 _mgr->setWindow(gwin );
227 _mgr->setRoot(_scene);
229 // show the whole scene
230 _mgr->showAll();
232 return 0;
235 int main(int argc, char **argv)
237 doMain(argc, argv);
239 // GLUT main loop
240 glutMainLoop();
242 return 0;
246 // GLUT callback functions
249 // redraw the window
250 void display(void)
252 // render scene
253 _mgr->redraw();
256 // react to size changes
257 void reshape(int w, int h)
259 _mgr->resize(w, h);
260 glutPostRedisplay();
263 // react to mouse button presses
264 void mouse(int button, int state, int x, int y)
266 if (state)
267 _mgr->mouseButtonRelease(button, x, y);
268 else
269 _mgr->mouseButtonPress(button, x, y);
271 glutPostRedisplay();
274 // react to mouse motions with pressed buttons
275 void motion(int x, int y)
277 _mgr->mouseMove(x, y);
278 glutPostRedisplay();
281 // react to keys
282 void keyboard(unsigned char k, int x, int y)
284 switch(k)
286 case 27:
287 case 'q':
288 _mgr = NULL;
289 _scene = NULL;
291 OSG::osgExit();
292 exit(1);
293 break;
294 case 'w':
295 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
296 printf("wrote scene.osb.gz\n");
297 break;
300 glutPostRedisplay();
303 // setup the GLUT library which handles the windows for us
304 int setupGLUT(int *argc, char *argv[])
306 glutInit(argc, argv);
307 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
309 int winid = glutCreateWindow("OpenSG CG Shader");
311 glutReshapeFunc(reshape);
312 glutDisplayFunc(display);
313 glutMouseFunc(mouse);
314 glutMotionFunc(motion);
315 glutKeyboardFunc(keyboard);
317 return winid;