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"
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"
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"
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"
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"
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"
109 // ------------------- global vars ----------------------
111 // The SimpleSceneManager to manage simple applications
112 static OSG::SimpleSceneManagerRefPtr _mgr
;
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";
128 normal_map_img_name
= argv
[1];
131 OSG::osgInit(argc
,argv
);
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 );
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
);
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);
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);
193 cmat
->addChunk(tex_normal_map
);
194 cmat
->addChunk(tex_normal_map_env
);
198 _scene
= OSG::Node::create();
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();
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
228 int main(int argc
, char **argv
)
239 // GLUT callback functions
249 // react to size changes
250 void reshape(int w
, int h
)
256 // react to mouse button presses
257 void mouse(int button
, int state
, int x
, int y
)
260 _mgr
->mouseButtonRelease(button
, x
, y
);
262 _mgr
->mouseButtonPress(button
, x
, y
);
267 // react to mouse motions with pressed buttons
268 void motion(int x
, int y
)
270 _mgr
->mouseMove(x
, y
);
275 void keyboard(unsigned char k
, int x
, int y
)
289 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
290 printf("wrote scene.osb.gz\n");
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
);