1 // OpenSG example: testSHL
3 // Demonstrates the use of the SHLChunk
4 // Implements a simple bumpmapping via vertex and fragment shader.
10 #include "OSGSimpleGeometry.h"
12 #include "OSGGLUTWindow.h"
13 #include "OSGSimpleSceneManager.h"
14 #include "OSGAction.h"
15 #include "OSGSceneFileHandler.h"
16 #include "OSGBaseFunctions.h"
20 #include "OSGTransform.h"
21 #include "OSGPointLight.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"
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"
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"
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"
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"
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"
113 // ------------------- global vars ----------------------
115 // The SimpleSceneManager to manage simple applications
116 static OSG::SimpleSceneManagerRefPtr _mgr
;
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";
132 normal_map_img_name
= argv
[1];
135 OSG::osgInit(argc
,argv
);
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 );
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
);
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);
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);
200 cmat
->addChunk(tex_normal_map
);
201 cmat
->addChunk(tex_normal_map_env
);
205 _scene
= OSG::Node::create();
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();
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
235 int main(int argc
, char **argv
)
246 // GLUT callback functions
256 // react to size changes
257 void reshape(int w
, int h
)
263 // react to mouse button presses
264 void mouse(int button
, int state
, int x
, int y
)
267 _mgr
->mouseButtonRelease(button
, x
, y
);
269 _mgr
->mouseButtonPress(button
, x
, y
);
274 // react to mouse motions with pressed buttons
275 void motion(int x
, int y
)
277 _mgr
->mouseMove(x
, y
);
282 void keyboard(unsigned char k
, int x
, int y
)
295 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
296 printf("wrote scene.osb.gz\n");
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
);