If all you want is a semi-transparent quad like in your example, I think
adding transparency through a uniform texture with alpa is a little
overkill.
Just use a color with an alpha value < 1.

--
Roland


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Steven T.
Hatton
Sent: woensdag 1 november 2006 17:01
To: osg users
Subject: [osg-users] Best way to make glass?

Is there an established method for creating glass in OSG/OpenGL?  The
code below is my attempt at doing so.  It seems to work for now, but I
wondering if it is the best way to proceed.  Suggestions?


#include <osg/Node>
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osg/Geode>
#include <osgDB/ReadFile>
#include <osgProducer/Viewer>

osg::Node* createFilterWall(const std::string& filename, float w=5.0f) {
  osg::Group* group = new osg::Group;
  osg::Geometry* geom = new osg::Geometry;
    
  osg::Vec3Array* vertices(new osg::Vec3Array);
  vertices->push_back(osg::Vec3(-w, w, w));
  vertices->push_back(osg::Vec3(-w, w,-w));
  vertices->push_back(osg::Vec3(-w,-w,-w));
  vertices->push_back(osg::Vec3(-w,-w, w));
  geom->setVertexArray(vertices);

  osg::Vec2Array* texcoords(new osg::Vec2Array);
  texcoords->push_back(osg::Vec2(0.0f,1.0f));
  texcoords->push_back(osg::Vec2(0.0f,0.0f));
  texcoords->push_back(osg::Vec2(1.0f,0.0f));
  texcoords->push_back(osg::Vec2(1.0f,1.0f));
  geom->setTexCoordArray(0,texcoords);
  
  osg::Vec3Array* normals(new osg::Vec3Array);
  normals->push_back(osg::Vec3(1.0f,0.0f,0.0f));

  geom->setNormalArray(normals);
  geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
  
  osg::Vec4Array* colors(new osg::Vec4Array);
  colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
  geom->setColorArray(colors);
  geom->setColorBinding(osg::Geometry::BIND_OVERALL);
  
  geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
  
  osg::Geode* geom_geode = new osg::Geode;
  geom_geode->addDrawable(geom);
  group->addChild(geom_geode);
  
  osg::Texture2D* texture = new osg::Texture2D;
  texture->setImage(osgDB::readImageFile(filename));
  
  osg::StateSet* stateset = geom->getOrCreateStateSet();
 
stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON)
;
  
  return group;
}


osg::Node* createGlassWall(float w=5.0f) {
  osg::Group* group = new osg::Group;
  osg::Geometry* geom = new osg::Geometry;
  
  osg::Vec3Array* vertices(new osg::Vec3Array);
  vertices->push_back(osg::Vec3(w, w, w));
  vertices->push_back(osg::Vec3(w, w,-w));
  vertices->push_back(osg::Vec3(w,-w,-w));
  vertices->push_back(osg::Vec3(w,-w, w));
  geom->setVertexArray(vertices);
  
  osg::Vec2Array* texcoords(new osg::Vec2Array);
  texcoords->push_back(osg::Vec2(0.0f,1.0f));
  texcoords->push_back(osg::Vec2(0.0f,0.0f));
  texcoords->push_back(osg::Vec2(1.0f,0.0f));
  texcoords->push_back(osg::Vec2(1.0f,1.0f));
  geom->setTexCoordArray(0,texcoords);
  
  osg::Vec3Array* normals(new osg::Vec3Array);
  normals->push_back(osg::Vec3(1.0f,0.0f,0.0f));
  geom->setNormalArray(normals);
  geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
  geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
  
  osg::Geode* geom_geode = new osg::Geode;
  geom_geode->addDrawable(geom);
  group->addChild(geom_geode);
  
  osg::Texture2D* texture(new osg::Texture2D);
  osg::Image * image(new osg::Image);
  image->allocateImage(64, 64, 1, GL_RGBA, GL_UNSIGNED_BYTE);
  texture->setImage(image);
  
  const unsigned sz(image->getImageSizeInBytes());
  unsigned char* cptr(image->data());
  
  for(unsigned i=0; i < sz;++i)
    switch(i%4)
      {
      case 0:*cptr++=127;break;
      case 1:*cptr++=127;break;
      case 2:*cptr++=255;break;
      case 3:*cptr++=63 ;break;
      }
  
  osg::StateSet* ss = geom->getOrCreateStateSet();
  ss->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
  ss->setMode(GL_BLEND,osg::StateAttribute::ON);
  ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
  return group;
}

osg::Node* createModel() {
  osg::Group* root = new osg::Group();
  root->addChild(createFilterWall("Images/lz.rgb"));
  root->addChild(createGlassWall());
  return root;
}

int main( int argc, char **argv )
{
  osg::ArgumentParser arguments(&argc,argv);
  
  
arguments.getApplicationUsage()->setDescription(arguments.getApplication
Name()+" 
is the example which demonstrates use of 2D textures.");
  
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplic
ationName()+" 
[options] filename ...");
  arguments.getApplicationUsage()->addCommandLineOption("-h
or --help","Display this information");
  
  osgProducer::Viewer viewer(arguments);
  viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
  viewer.getUsage(*arguments.getApplicationUsage());
  if (arguments.read("-h") || arguments.read("--help")) { 
    arguments.getApplicationUsage()->write(std::cout); return 1;
  }
  arguments.reportRemainingOptionsAsUnrecognized();
  if (arguments.errors()) { arguments.writeErrorMessages(std::cout);
return 1; }

  osg::Node* rootNode = createModel();
  viewer.setSceneData( rootNode );
  viewer.realize();

  while( !viewer.done() ) {
    viewer.sync();  
    viewer.update();
    viewer.frame();
  }
  viewer.sync();
  return 0;
}
_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

This e-mail and its contents are subject to the DISCLAIMER at 
http://www.tno.nl/disclaimer/email.html
_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

Reply via email to