| | 326 | osg::Node* createSimpleTextureTestModel() |
| | 327 | { |
| | 328 | osg::Group* group = new osg::Group; |
| | 329 | |
| | 330 | osg::Geode* geode = new osg::Geode; |
| | 331 | group->addChild(geode); |
| | 332 | |
| | 333 | osg::Geometry* geometry = new osg::Geometry; |
| | 334 | geode->addDrawable(geometry); |
| | 335 | |
| | 336 | osg::Vec3Array* vertices = new osg::Vec3Array; |
| | 337 | vertices->push_back(osg::Vec3(0.0,0.0,0.0)); |
| | 338 | vertices->push_back(osg::Vec3(0.0,0.0,1.0)); |
| | 339 | vertices->push_back(osg::Vec3(1.0,0.0,0.0)); |
| | 340 | vertices->push_back(osg::Vec3(1.0,0.0,1.0)); |
| | 341 | geometry->setVertexArray(vertices); |
| | 342 | |
| | 343 | osg::Vec2Array* texcoords = new osg::Vec2Array; |
| | 344 | texcoords->push_back(osg::Vec2(0.0,0.0)); |
| | 345 | texcoords->push_back(osg::Vec2(0.0,1.0)); |
| | 346 | texcoords->push_back(osg::Vec2(1.0,0.0)); |
| | 347 | texcoords->push_back(osg::Vec2(1.0,1.0)); |
| | 348 | geometry->setTexCoordArray(0, texcoords); |
| | 349 | |
| | 350 | geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, 4)); |
| | 351 | |
| | 352 | char vertexShaderSource[] = |
| | 353 | "varying vec2 texCoord;\n" |
| | 354 | "void main(void)\n" |
| | 355 | "{\n" |
| | 356 | " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" |
| | 357 | " texCoord = gl_MultiTexCoord0.xy;\n" |
| | 358 | "}\n"; |
| | 359 | |
| | 360 | char fragmentShaderSource[] = |
| | 361 | "varying vec2 texCoord;\n" |
| | 362 | "uniform sampler2D baseTexture;\n" |
| | 363 | "void main(void)\n" |
| | 364 | "{\n" |
| | 365 | " gl_FragColor = texture2D(baseTexture, texCoord); \n" |
| | 366 | "}\n"; |
| | 367 | |
| | 368 | osg::Program* program = new osg::Program; |
| | 369 | program->addShader(new osg::Shader(osg::Shader::VERTEX, vertexShaderSource)); |
| | 370 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource)); |
| | 371 | |
| | 372 | osg::StateSet* stateset = geometry->getOrCreateStateSet(); |
| | 373 | stateset->setAttribute(program); |
| | 374 | |
| | 375 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
| | 376 | osg::Texture2D* texture = new osg::Texture2D(image); |
| | 377 | texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); |
| | 378 | texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); |
| | 379 | stateset->setTextureAttribute(0, texture); |
| | 380 | |
| | 381 | osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); |
| | 382 | stateset->addUniform(baseTextureSampler); |
| | 383 | |
| | 384 | return group; |
| | 385 | } |
| | 386 | |