| 1 | #include <iostream> |
|---|
| 2 | #include <osg/Geode> |
|---|
| 3 | #include <osg/TexGen> |
|---|
| 4 | #include <osg/Material> |
|---|
| 5 | #include <osg/Texture2D> |
|---|
| 6 | #include <osg/MatrixTransform> |
|---|
| 7 | #include <osg/ShapeDrawable> |
|---|
| 8 | #include <osg/BlendFunc> |
|---|
| 9 | #include <osgText/Text> |
|---|
| 10 | #include <osgDB/ReadFile> |
|---|
| 11 | #include <osgViewer/Viewer> |
|---|
| 12 | #include <osgViewer/View> |
|---|
| 13 | #include <osgViewer/CompositeViewer> |
|---|
| 14 | #include <osgGA/TrackballManipulator> |
|---|
| 15 | |
|---|
| 16 | #include "VirtualProgram.h" |
|---|
| 17 | |
|---|
| 18 | using osgCandidate::VirtualProgram; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | char MainVertexShaderSource[] = |
|---|
| 31 | "vec4 texture( in vec3 position, in vec3 normal ); \n" |
|---|
| 32 | "void lighting( in vec3 position, in vec3 normal ); \n" |
|---|
| 33 | " \n" |
|---|
| 34 | "void main () \n" |
|---|
| 35 | "{ \n" |
|---|
| 36 | " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n" |
|---|
| 37 | " vec4 position4 = gl_ModelViewMatrix * gl_Vertex; \n" |
|---|
| 38 | " vec3 position = position4.xyz / position4.w; \n" |
|---|
| 39 | " vec3 normal = normalize( gl_NormalMatrix * gl_Normal ); \n" |
|---|
| 40 | " gl_TexCoord[0] = texture( position, normal ); \n" |
|---|
| 41 | " lighting( position, normal ); \n" |
|---|
| 42 | "} \n"; |
|---|
| 43 | |
|---|
| 44 | char TexCoordTextureVertexShaderSource[] = |
|---|
| 45 | "vec4 texture( in vec3 position, in vec3 normal ) \n" |
|---|
| 46 | "{ \n" |
|---|
| 47 | " return gl_TextureMatrix[0] * gl_MultiTexCoord0; \n" |
|---|
| 48 | "} \n"; |
|---|
| 49 | |
|---|
| 50 | char SphereMapTextureVertexShaderSource[] = |
|---|
| 51 | "vec4 texture( in vec3 position, in vec3 normal ) \n" |
|---|
| 52 | "{ \n" |
|---|
| 53 | " vec3 u = normalize( position ); \n" |
|---|
| 54 | " vec3 r = reflect(u, normal); \n" |
|---|
| 55 | " float m = 2.0 * sqrt(r.x * r.x + r.y * r.y + (r.z+1.0) * (r.z+1.0)); \n" |
|---|
| 56 | " return vec4(r.x / m + 0.5, r.y / m + 0.5, 1.0, 1.0 ); \n" |
|---|
| 57 | "} \n"; |
|---|
| 58 | |
|---|
| 59 | char PerVertexDirectionalLightingVertexShaderSource[] = |
|---|
| 60 | "void lighting( in vec3 position, in vec3 normal ) \n" |
|---|
| 61 | "{ \n" |
|---|
| 62 | " float NdotL = dot( normal, normalize(gl_LightSource[0].position.xyz) );\n" |
|---|
| 63 | " NdotL = max( 0.0, NdotL ); \n" |
|---|
| 64 | " float NdotHV = dot( normal, gl_LightSource[0].halfVector.xyz ); \n" |
|---|
| 65 | " NdotHV = max( 0.0, NdotHV ); \n" |
|---|
| 66 | " \n" |
|---|
| 67 | " gl_FrontColor = gl_FrontLightModelProduct.sceneColor + \n" |
|---|
| 68 | " gl_FrontLightProduct[0].ambient + \n" |
|---|
| 69 | " gl_FrontLightProduct[0].diffuse * NdotL; \n" |
|---|
| 70 | " \n" |
|---|
| 71 | " gl_FrontSecondaryColor = vec4(0.0); \n" |
|---|
| 72 | " \n" |
|---|
| 73 | " if ( NdotL * NdotHV > 0.0 ) \n" |
|---|
| 74 | " gl_FrontSecondaryColor = gl_FrontLightProduct[0].specular * \n" |
|---|
| 75 | " pow( NdotHV, gl_FrontMaterial.shininess );\n" |
|---|
| 76 | " \n" |
|---|
| 77 | " gl_BackColor = gl_FrontColor; \n" |
|---|
| 78 | " gl_BackSecondaryColor = gl_FrontSecondaryColor; \n" |
|---|
| 79 | "} \n"; |
|---|
| 80 | |
|---|
| 81 | char MainFragmentShaderSource[] = |
|---|
| 82 | "vec4 texture( void ); \n" |
|---|
| 83 | "void lighting( inout vec4 color ); \n" |
|---|
| 84 | " \n" |
|---|
| 85 | "void main () \n" |
|---|
| 86 | "{ \n" |
|---|
| 87 | " vec4 color = texture(); \n" |
|---|
| 88 | " lighting( color ); \n" |
|---|
| 89 | " gl_FragColor = color; \n" |
|---|
| 90 | "} \n"; |
|---|
| 91 | |
|---|
| 92 | char TextureFragmentShaderSource[] = |
|---|
| 93 | "uniform sampler2D baseTexture; \n" |
|---|
| 94 | "vec4 texture( void ) \n" |
|---|
| 95 | "{ \n" |
|---|
| 96 | " return texture2D( baseTexture, gl_TexCoord[0].xy ); \n" |
|---|
| 97 | "} \n"; |
|---|
| 98 | |
|---|
| 99 | char ProceduralBlueTextureFragmentShaderSource[] = |
|---|
| 100 | "vec4 texture( void ) \n" |
|---|
| 101 | "{ \n" |
|---|
| 102 | " return vec4( 0.3, 0.3, 1.0, 1.0 ); \n" |
|---|
| 103 | "} \n"; |
|---|
| 104 | |
|---|
| 105 | char PerVertexLightingFragmentShaderSource[] = |
|---|
| 106 | "void lighting( inout vec4 color ) \n" |
|---|
| 107 | "{ \n" |
|---|
| 108 | " color = color * gl_Color + gl_SecondaryColor; \n" |
|---|
| 109 | "} \n"; |
|---|
| 110 | |
|---|
| 111 | char PerFragmentLightingVertexShaderSource[] = |
|---|
| 112 | "varying vec3 Normal; \n" |
|---|
| 113 | "varying vec3 Position; \n" |
|---|
| 114 | " \n" |
|---|
| 115 | "void lighting( in vec3 position, in vec3 normal ) \n" |
|---|
| 116 | "{ \n" |
|---|
| 117 | " Normal = normal; \n" |
|---|
| 118 | " Position = position; \n" |
|---|
| 119 | "} \n"; |
|---|
| 120 | |
|---|
| 121 | char PerFragmentDirectionalLightingFragmentShaderSource[] = |
|---|
| 122 | "varying vec3 Normal; \n" |
|---|
| 123 | "varying vec3 Position; // not used for directional lighting \n" |
|---|
| 124 | " \n" |
|---|
| 125 | "void lighting( inout vec4 color ) \n" |
|---|
| 126 | "{ \n" |
|---|
| 127 | " vec3 n = normalize( Normal ); \n" |
|---|
| 128 | " float NdotL = dot( n, normalize(gl_LightSource[0].position.xyz) ); \n" |
|---|
| 129 | " NdotL = max( 0.0, NdotL ); \n" |
|---|
| 130 | " float NdotHV = dot( n, gl_LightSource[0].halfVector.xyz ); \n" |
|---|
| 131 | " NdotHV = max( 0.0, NdotHV ); \n" |
|---|
| 132 | " \n" |
|---|
| 133 | " color *= gl_FrontLightModelProduct.sceneColor + \n" |
|---|
| 134 | " gl_FrontLightProduct[0].ambient + \n" |
|---|
| 135 | " gl_FrontLightProduct[0].diffuse * NdotL; \n" |
|---|
| 136 | " \n" |
|---|
| 137 | " if ( NdotL * NdotHV > 0.0 ) \n" |
|---|
| 138 | " color += gl_FrontLightProduct[0].specular * \n" |
|---|
| 139 | " pow( NdotHV, gl_FrontMaterial.shininess ); \n" |
|---|
| 140 | "} \n"; |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | osg::Node * CreateModel( const char * file ) |
|---|
| 144 | { |
|---|
| 145 | if ( file ) { |
|---|
| 146 | osg::Node * node = NULL; |
|---|
| 147 | node = osgDB::readNodeFile( file ); |
|---|
| 148 | if( node ) |
|---|
| 149 | return node; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | osg::Geode * geode = new osg::Geode; |
|---|
| 154 | osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints; |
|---|
| 155 | hints->setDetailRatio( 0.3 ); |
|---|
| 156 | |
|---|
| 157 | #if 1 |
|---|
| 158 | osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable |
|---|
| 159 | ( new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 4.0 ), hints.get() ); |
|---|
| 160 | #else |
|---|
| 161 | osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable |
|---|
| 162 | ( new osg::Box( osg::Vec3(-1.0f, -1.0f, -1.0f), 2.0, 2.0, 2.0 ) ); |
|---|
| 163 | #endif |
|---|
| 164 | |
|---|
| 165 | shape->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f)); |
|---|
| 166 | |
|---|
| 167 | geode->addDrawable( shape.get() ); |
|---|
| 168 | |
|---|
| 169 | osg::StateSet * stateSet = new osg::StateSet; |
|---|
| 170 | |
|---|
| 171 | osg::Texture2D * texture = new osg::Texture2D( |
|---|
| 172 | osgDB::readImageFile("Images/land_shallow_topo_2048.jpg") |
|---|
| 173 | ); |
|---|
| 174 | |
|---|
| 175 | osg::Material * material = new osg::Material; |
|---|
| 176 | |
|---|
| 177 | material->setAmbient |
|---|
| 178 | ( osg::Material::FRONT_AND_BACK, osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) ); |
|---|
| 179 | |
|---|
| 180 | material->setDiffuse |
|---|
| 181 | ( osg::Material::FRONT_AND_BACK, osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) ); |
|---|
| 182 | |
|---|
| 183 | #if 1 |
|---|
| 184 | material->setSpecular |
|---|
| 185 | ( osg::Material::FRONT_AND_BACK, osg::Vec4( 0.7, 0.3, 0.3, 1.0 ) ); |
|---|
| 186 | |
|---|
| 187 | material->setShininess( osg::Material::FRONT_AND_BACK, 25 ); |
|---|
| 188 | |
|---|
| 189 | #endif |
|---|
| 190 | |
|---|
| 191 | stateSet->setAttributeAndModes( material ); |
|---|
| 192 | stateSet->setTextureAttributeAndModes( 0,texture, osg::StateAttribute::ON ); |
|---|
| 193 | |
|---|
| 194 | geode->setStateSet( stateSet ); |
|---|
| 195 | return geode; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | void SetVirtualProgramShader( VirtualProgram * virtualProgram, |
|---|
| 200 | std::string shader_semantics, |
|---|
| 201 | osg::Shader::Type shader_type, |
|---|
| 202 | std::string shader_name, |
|---|
| 203 | std::string shader_source ) |
|---|
| 204 | { |
|---|
| 205 | osg::Shader * shader = new osg::Shader( shader_type ); |
|---|
| 206 | shader->setName( shader_name ); |
|---|
| 207 | shader->setShaderSource( shader_source ); |
|---|
| 208 | virtualProgram->setShader( shader_semantics, shader ); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | void AddLabel( osg::Group * group, const std::string & label, float offset ) |
|---|
| 212 | { |
|---|
| 213 | osg::Vec3 center( 0, 0, offset * 0.5 ); |
|---|
| 214 | osg::Geode * geode = new osg::Geode; |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | geode->getOrCreateStateSet()->setAttribute |
|---|
| 218 | ( new osg::Program, osg::StateAttribute::ON | osg::StateAttribute::PROTECTED ); |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | geode->getOrCreateStateSet()->setTextureMode( 1, GL_TEXTURE_2D, osg::StateAttribute::OFF ); |
|---|
| 222 | |
|---|
| 223 | group->addChild( geode ); |
|---|
| 224 | |
|---|
| 225 | osgText::Text* text = new osgText::Text; |
|---|
| 226 | geode->addDrawable( text ); |
|---|
| 227 | text->setFont("fonts/times.ttf"); |
|---|
| 228 | text->setCharacterSize( offset * 0.1 ); |
|---|
| 229 | text->setPosition(center); |
|---|
| 230 | text->setAlignment( osgText::TextBase::CENTER_CENTER ); |
|---|
| 231 | text->setAxisAlignment(osgText::Text::SCREEN); |
|---|
| 232 | |
|---|
| 233 | osg::Vec4 characterSizeModeColor(1.0f,0.0f,0.5f,1.0f); |
|---|
| 234 | #if 1 |
|---|
| 235 | |
|---|
| 236 | text->setBackdropType(osgText::Text::OUTLINE); |
|---|
| 237 | text->setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX); |
|---|
| 238 | #endif |
|---|
| 239 | |
|---|
| 240 | text->setText( label ); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | osg::Node * CreateAdvancedHierarchy( const char * file = NULL ) |
|---|
| 244 | { |
|---|
| 245 | osg::Node * model = CreateModel( file ); |
|---|
| 246 | if( !model ) return NULL; |
|---|
| 247 | float offset = model->getBound().radius() * 1.3; |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | osg::MatrixTransform * transformCenterMiddle = new osg::MatrixTransform( ); |
|---|
| 251 | transformCenterMiddle->setMatrix( osg::Matrix::translate( 0,0, offset * 0.5 ) ); |
|---|
| 252 | transformCenterMiddle->addChild( model ); |
|---|
| 253 | |
|---|
| 254 | osg::MatrixTransform * transformCenterTop = new osg::MatrixTransform( ); |
|---|
| 255 | transformCenterMiddle->addChild( transformCenterTop ); |
|---|
| 256 | transformCenterTop->setMatrix( osg::Matrix::translate( 0,0,offset ) ); |
|---|
| 257 | transformCenterTop->addChild( model ); |
|---|
| 258 | |
|---|
| 259 | osg::MatrixTransform * transformCenterBottom = new osg::MatrixTransform( ); |
|---|
| 260 | transformCenterMiddle->addChild( transformCenterBottom ); |
|---|
| 261 | transformCenterBottom->setMatrix( osg::Matrix::translate( 0,0,-offset ) ); |
|---|
| 262 | transformCenterBottom->addChild( model ); |
|---|
| 263 | |
|---|
| 264 | osg::MatrixTransform * transformLeftBottom = new osg::MatrixTransform( ); |
|---|
| 265 | transformCenterBottom->addChild( transformLeftBottom ); |
|---|
| 266 | transformLeftBottom->setMatrix( osg::Matrix::translate( -offset * 0.8,0, -offset * 0.8 ) ); |
|---|
| 267 | transformLeftBottom->addChild( model ); |
|---|
| 268 | |
|---|
| 269 | osg::MatrixTransform * transformRightBottom = new osg::MatrixTransform( ); |
|---|
| 270 | transformCenterBottom->addChild( transformRightBottom ); |
|---|
| 271 | transformRightBottom->setMatrix( osg::Matrix::translate( offset * 0.8,0, -offset * 0.8 ) ); |
|---|
| 272 | transformRightBottom->addChild( model ); |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | if( 1 ) |
|---|
| 281 | { |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | VirtualProgram * vp = new VirtualProgram( ); |
|---|
| 288 | transformCenterMiddle->getOrCreateStateSet()->setAttribute( vp ); |
|---|
| 289 | AddLabel( transformCenterMiddle, "Per Vertex Lighting Virtual Program", offset ); |
|---|
| 290 | |
|---|
| 291 | SetVirtualProgramShader( vp, "main", osg::Shader::VERTEX, |
|---|
| 292 | "Vertex Main", MainVertexShaderSource ); |
|---|
| 293 | |
|---|
| 294 | SetVirtualProgramShader( vp, "main", osg::Shader::FRAGMENT, |
|---|
| 295 | "Fragment Main", MainFragmentShaderSource ); |
|---|
| 296 | |
|---|
| 297 | SetVirtualProgramShader( vp, "texture",osg::Shader::VERTEX, |
|---|
| 298 | "Vertex Texture Coord 0", TexCoordTextureVertexShaderSource ); |
|---|
| 299 | |
|---|
| 300 | SetVirtualProgramShader( vp, "texture",osg::Shader::FRAGMENT, |
|---|
| 301 | "Fragment Texture", TextureFragmentShaderSource ); |
|---|
| 302 | |
|---|
| 303 | SetVirtualProgramShader( vp, "lighting",osg::Shader::VERTEX, |
|---|
| 304 | "Vertex Lighting", PerVertexDirectionalLightingVertexShaderSource ); |
|---|
| 305 | |
|---|
| 306 | SetVirtualProgramShader( vp, "lighting",osg::Shader::FRAGMENT, |
|---|
| 307 | "Fragment Lighting", PerVertexLightingFragmentShaderSource ); |
|---|
| 308 | |
|---|
| 309 | transformCenterMiddle->getOrCreateStateSet()-> |
|---|
| 310 | addUniform( new osg::Uniform( "baseTexture", 0 ) ); |
|---|
| 311 | |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | if( 1 ) |
|---|
| 317 | { |
|---|
| 318 | AddLabel( transformCenterBottom, "Per Pixel Lighting VP", offset ); |
|---|
| 319 | VirtualProgram * vp = new VirtualProgram( ); |
|---|
| 320 | transformCenterBottom->getOrCreateStateSet()->setAttribute( vp ); |
|---|
| 321 | |
|---|
| 322 | SetVirtualProgramShader( vp, "lighting",osg::Shader::VERTEX, |
|---|
| 323 | "Vertex Shader For Per Pixel Lighting", |
|---|
| 324 | PerFragmentLightingVertexShaderSource ); |
|---|
| 325 | |
|---|
| 326 | SetVirtualProgramShader( vp, "lighting",osg::Shader::FRAGMENT, |
|---|
| 327 | "Fragment Shader For Per Pixel Lighting", |
|---|
| 328 | PerFragmentDirectionalLightingFragmentShaderSource ); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | if( 1 ) |
|---|
| 334 | { |
|---|
| 335 | AddLabel( transformLeftBottom, "Blue Tex VP", offset ); |
|---|
| 336 | VirtualProgram * vp = new VirtualProgram( ); |
|---|
| 337 | transformLeftBottom->getOrCreateStateSet()->setAttribute( vp ); |
|---|
| 338 | |
|---|
| 339 | SetVirtualProgramShader( vp, "texture",osg::Shader::FRAGMENT, |
|---|
| 340 | "Fragment Shader Procedural Blue Tex", |
|---|
| 341 | ProceduralBlueTextureFragmentShaderSource ); |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | if( 1 ) |
|---|
| 346 | { |
|---|
| 347 | AddLabel( transformRightBottom, "EnvMap Sphere VP", offset ); |
|---|
| 348 | |
|---|
| 349 | osg::StateSet * ss = transformRightBottom->getOrCreateStateSet(); |
|---|
| 350 | VirtualProgram * vp = new VirtualProgram( ); |
|---|
| 351 | ss->setAttribute( vp ); |
|---|
| 352 | SetVirtualProgramShader( vp, "texture",osg::Shader::VERTEX, |
|---|
| 353 | "Vertex Texture Sphere Map", SphereMapTextureVertexShaderSource ); |
|---|
| 354 | |
|---|
| 355 | osg::Texture2D * texture = new osg::Texture2D( |
|---|
| 356 | |
|---|
| 357 | osgDB::readImageFile("Images/skymap.jpg") |
|---|
| 358 | ); |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | ss->setTextureAttributeAndModes( 1, texture, osg::StateAttribute::ON ); |
|---|
| 364 | ss->addUniform( new osg::Uniform( "baseTexture", 1 ) ); |
|---|
| 365 | |
|---|
| 366 | #if 0 // Could be useful with Fixed Vertex Pipeline |
|---|
| 367 | osg::TexGen * texGen = new osg::TexGen(); |
|---|
| 368 | texGen->setMode( osg::TexGen::SPHERE_MAP ); |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | ss->setTextureAttributeAndModes( 1, texGen, osg::StateAttribute::ON ); |
|---|
| 372 | #endif |
|---|
| 373 | |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | if( 1 ) |
|---|
| 379 | { |
|---|
| 380 | AddLabel( transformCenterTop, "Fixed Vertex + Simple Fragment osg::Program", offset ); |
|---|
| 381 | osg::Program * program = new osg::Program; |
|---|
| 382 | program->setName( "Trivial Fragment + Fixed Vertex Program" ); |
|---|
| 383 | |
|---|
| 384 | transformCenterTop->getOrCreateStateSet( )->setAttributeAndModes |
|---|
| 385 | ( program, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); |
|---|
| 386 | |
|---|
| 387 | osg::Shader * shader = new osg::Shader( osg::Shader::FRAGMENT ); |
|---|
| 388 | shader->setName( "Trivial Fragment Shader" ); |
|---|
| 389 | shader->setShaderSource( |
|---|
| 390 | "uniform sampler2D baseTexture; \n" |
|---|
| 391 | "void main(void) \n" |
|---|
| 392 | "{ \n" |
|---|
| 393 | " gl_FragColor = gl_Color * texture2D( baseTexture,gl_TexCoord[0].xy);\n" |
|---|
| 394 | "} \n" |
|---|
| 395 | ); |
|---|
| 396 | |
|---|
| 397 | program->addShader( shader ); |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | return transformCenterMiddle; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | char LightingVertexShaderSource[] = |
|---|
| 406 | "// Forward declarations \n" |
|---|
| 407 | " \n" |
|---|
| 408 | "void SpotLight( in int i, in vec3 eye, in vec3 position, in vec3 normal, \n" |
|---|
| 409 | " inout vec4 ambient, inout vec4 diffuse, inout vec4 specular ); \n" |
|---|
| 410 | " \n" |
|---|
| 411 | "void PointLight( in int i, in vec3 eye, in vec3 position, in vec3 normal, \n" |
|---|
| 412 | " inout vec4 ambient, inout vec4 diffuse, inout vec4 specular ); \n" |
|---|
| 413 | " \n" |
|---|
| 414 | "void DirectionalLight( in int i, in vec3 normal, \n" |
|---|
| 415 | " inout vec4 ambient, inout vec4 diffuse, inout vec4 specular ); \n" |
|---|
| 416 | " \n" |
|---|
| 417 | "const int NumEnabledLights = 1; \n" |
|---|
| 418 | " \n" |
|---|
| 419 | "void lighting( in vec3 position, in vec3 normal ) \n" |
|---|
| 420 | "{ \n" |
|---|
| 421 | " vec3 eye = vec3( 0.0, 0.0, 1.0 ); \n" |
|---|
| 422 | " //vec3 eye = -normalize(position); \n" |
|---|
| 423 | " \n" |
|---|
| 424 | " // Clear the light intensity accumulators \n" |
|---|
| 425 | " vec4 amb = vec4(0.0); \n" |
|---|
| 426 | " vec4 diff = vec4(0.0); \n" |
|---|
| 427 | " vec4 spec = vec4(0.0); \n" |
|---|
| 428 | " \n" |
|---|
| 429 | " // Loop through enabled lights, compute contribution from each \n" |
|---|
| 430 | " for (int i = 0; i < NumEnabledLights; i++) \n" |
|---|
| 431 | " { \n" |
|---|
| 432 | " if (gl_LightSource[i].position.w == 0.0) \n" |
|---|
| 433 | " DirectionalLight(i, normal, amb, diff, spec); \n" |
|---|
| 434 | " else if (gl_LightSource[i].spotCutoff == 180.0) \n" |
|---|
| 435 | " PointLight(i, eye, position, normal, amb, diff, spec); \n" |
|---|
| 436 | " else \n" |
|---|
| 437 | " SpotLight(i, eye, position, normal, amb, diff, spec); \n" |
|---|
| 438 | " } \n" |
|---|
| 439 | " \n" |
|---|
| 440 | " gl_FrontColor = gl_FrontLightModelProduct.sceneColor + \n" |
|---|
| 441 | " amb * gl_FrontMaterial.ambient + \n" |
|---|
| 442 | " diff * gl_FrontMaterial.diffuse; \n" |
|---|
| 443 | " \n" |
|---|
| 444 | " gl_FrontSecondaryColor = vec4(spec*gl_FrontMaterial.specular); \n" |
|---|
| 445 | " \n" |
|---|
| 446 | " gl_BackColor = gl_FrontColor; \n" |
|---|
| 447 | " gl_BackSecondaryColor = gl_FrontSecondaryColor; \n" |
|---|
| 448 | "} \n"; |
|---|
| 449 | |
|---|
| 450 | char SpotLightShaderSource[] = |
|---|
| 451 | "void SpotLight(in int i, \n" |
|---|
| 452 | " in vec3 eye, \n" |
|---|
| 453 | " in vec3 position, \n" |
|---|
| 454 | " in vec3 normal, \n" |
|---|
| 455 | " inout vec4 ambient, \n" |
|---|
| 456 | " inout vec4 diffuse, \n" |
|---|
| 457 | " inout vec4 specular) \n" |
|---|
| 458 | "{ \n" |
|---|
| 459 | " float nDotVP; // normal . light direction \n" |
|---|
| 460 | " float nDotHV; // normal . light half vector \n" |
|---|
| 461 | " float pf; // power factor \n" |
|---|
| 462 | " float spotDot; // cosine of angle between spotlight \n" |
|---|
| 463 | " float spotAttenuation; // spotlight attenuation factor \n" |
|---|
| 464 | " float attenuation; // computed attenuation factor \n" |
|---|
| 465 | " float d; // distance from surface to light source \n" |
|---|
| 466 | " vec3 VP; // direction from surface to light position \n" |
|---|
| 467 | " vec3 halfVector; // direction of maximum highlights \n" |
|---|
| 468 | " \n" |
|---|
| 469 | " // Compute vector from surface to light position \n" |
|---|
| 470 | " VP = vec3(gl_LightSource[i].position) - position; \n" |
|---|
| 471 | " \n" |
|---|
| 472 | " // Compute distance between surface and light position \n" |
|---|
| 473 | " d = length(VP); \n" |
|---|
| 474 | " \n" |
|---|
| 475 | " // Normalize the vector from surface to light position \n" |
|---|
| 476 | " VP = normalize(VP); \n" |
|---|
| 477 | " \n" |
|---|
| 478 | " // Compute attenuation \n" |
|---|
| 479 | " attenuation = 1.0 / (gl_LightSource[i].constantAttenuation + \n" |
|---|
| 480 | " gl_LightSource[i].linearAttenuation * d + \n" |
|---|
| 481 | " gl_LightSource[i].quadraticAttenuation *d*d); \n" |
|---|
| 482 | " \n" |
|---|
| 483 | " // See if point on surface is inside cone of illumination \n" |
|---|
| 484 | " spotDot = dot(-VP, normalize(gl_LightSource[i].spotDirection)); \n" |
|---|
| 485 | " \n" |
|---|
| 486 | " if (spotDot < gl_LightSource[i].spotCosCutoff) \n" |
|---|
| 487 | " spotAttenuation = 0.0; // light adds no contribution \n" |
|---|
| 488 | " else \n" |
|---|
| 489 | " spotAttenuation = pow(spotDot, gl_LightSource[i].spotExponent); \n" |
|---|
| 490 | " \n" |
|---|
| 491 | " // Combine the spotlight and distance attenuation. \n" |
|---|
| 492 | " attenuation *= spotAttenuation; \n" |
|---|
| 493 | " \n" |
|---|
| 494 | " halfVector = normalize(VP + eye); \n" |
|---|
| 495 | " \n" |
|---|
| 496 | " nDotVP = max(0.0, dot(normal, VP)); \n" |
|---|
| 497 | " nDotHV = max(0.0, dot(normal, halfVector)); \n" |
|---|
| 498 | " \n" |
|---|
| 499 | " if (nDotVP == 0.0) \n" |
|---|
| 500 | " pf = 0.0; \n" |
|---|
| 501 | " else \n" |
|---|
| 502 | " pf = pow(nDotHV, gl_FrontMaterial.shininess); \n" |
|---|
| 503 | " \n" |
|---|
| 504 | " ambient += gl_LightSource[i].ambient * attenuation; \n" |
|---|
| 505 | " diffuse += gl_LightSource[i].diffuse * nDotVP * attenuation; \n" |
|---|
| 506 | " specular += gl_LightSource[i].specular * pf * attenuation; \n" |
|---|
| 507 | "} \n"; |
|---|
| 508 | |
|---|
| 509 | char PointLightShaderSource[] = |
|---|
| 510 | "void PointLight(in int i, \n" |
|---|
| 511 | " in vec3 eye, \n" |
|---|
| 512 | " in vec3 position, \n" |
|---|
| 513 | " in vec3 normal, \n" |
|---|
| 514 | " inout vec4 ambient, \n" |
|---|
| 515 | " inout vec4 diffuse, \n" |
|---|
| 516 | " inout vec4 specular) \n" |
|---|
| 517 | "{ \n" |
|---|
| 518 | " float nDotVP; // normal . light direction \n" |
|---|
| 519 | " float nDotHV; // normal . light half vector \n" |
|---|
| 520 | " float pf; // power factor \n" |
|---|
| 521 | " float attenuation; // computed attenuation factor \n" |
|---|
| 522 | " float d; // distance from surface to light source \n" |
|---|
| 523 | " vec3 VP; // direction from surface to light position \n" |
|---|
| 524 | " vec3 halfVector; // direction of maximum highlights \n" |
|---|
| 525 | " \n" |
|---|
| 526 | " // Compute vector from surface to light position \n" |
|---|
| 527 | " VP = vec3(gl_LightSource[i].position) - position; \n" |
|---|
| 528 | " \n" |
|---|
| 529 | " // Compute distance between surface and light position \n" |
|---|
| 530 | " d = length(VP); \n" |
|---|
| 531 | " \n" |
|---|
| 532 | " // Normalize the vector from surface to light position \n" |
|---|
| 533 | " VP = normalize(VP); \n" |
|---|
| 534 | " \n" |
|---|
| 535 | " // Compute attenuation \n" |
|---|
| 536 | " attenuation = 1.0 / (gl_LightSource[i].constantAttenuation + \n" |
|---|
| 537 | " gl_LightSource[i].linearAttenuation * d + \n" |
|---|
| 538 | " gl_LightSource[i].quadraticAttenuation * d*d); \n" |
|---|
| 539 | " \n" |
|---|
| 540 | " halfVector = normalize(VP + eye); \n" |
|---|
| 541 | " \n" |
|---|
| 542 | " nDotVP = max(0.0, dot(normal, VP)); \n" |
|---|
| 543 | " nDotHV = max(0.0, dot(normal, halfVector)); \n" |
|---|
| 544 | " \n" |
|---|
| 545 | " if (nDotVP == 0.0) \n" |
|---|
| 546 | " pf = 0.0; \n" |
|---|
| 547 | " else \n" |
|---|
| 548 | " pf = pow(nDotHV, gl_FrontMaterial.shininess); \n" |
|---|
| 549 | " \n" |
|---|
| 550 | " ambient += gl_LightSource[i].ambient * attenuation; \n" |
|---|
| 551 | " diffuse += gl_LightSource[i].diffuse * nDotVP * attenuation; \n" |
|---|
| 552 | " specular += gl_LightSource[i].specular * pf * attenuation; \n" |
|---|
| 553 | "} \n"; |
|---|
| 554 | |
|---|
| 555 | char DirectionalLightShaderSource[] = |
|---|
| 556 | "void DirectionalLight(in int i, \n" |
|---|
| 557 | " in vec3 normal, \n" |
|---|
| 558 | " inout vec4 ambient, \n" |
|---|
| 559 | " inout vec4 diffuse, \n" |
|---|
| 560 | " inout vec4 specular) \n" |
|---|
| 561 | "{ \n" |
|---|
| 562 | " float nDotVP; // normal . light direction \n" |
|---|
| 563 | " float nDotHV; // normal . light half vector \n" |
|---|
| 564 | " float pf; // power factor \n" |
|---|
| 565 | " \n" |
|---|
| 566 | " nDotVP = max(0.0, dot(normal, \n" |
|---|
| 567 | " normalize(vec3(gl_LightSource[i].position)))); \n" |
|---|
| 568 | " nDotHV = max(0.0, dot(normal, \n" |
|---|
| 569 | " vec3(gl_LightSource[i].halfVector))); \n" |
|---|
| 570 | " \n" |
|---|
| 571 | " if (nDotVP == 0.0) \n" |
|---|
| 572 | " pf = 0.0; \n" |
|---|
| 573 | " else \n" |
|---|
| 574 | " pf = pow(nDotHV, gl_FrontMaterial.shininess); \n" |
|---|
| 575 | " \n" |
|---|
| 576 | " ambient += gl_LightSource[i].ambient; \n" |
|---|
| 577 | " diffuse += gl_LightSource[i].diffuse * nDotVP; \n" |
|---|
| 578 | " specular += gl_LightSource[i].specular * pf; \n" |
|---|
| 579 | "} \n"; |
|---|