#include<osgDB/ReadFile>
#include<osgViewer/Viewer>
#include<osg/Node>
#include<osg/Geode>
#include<osg/Geometry>
#include <osg/StateSet>
#include <osgUtil/SmoothingVisitor>
#include <osg/CullFace>
#include <osg/Material>
osg::ref_ptr<osg::Node> createQuad();
void main()
{
osgViewer::Viewer viewer;
osg::Group *root=new osg::Group();
osg::ref_ptr<osg::Node> node=createQuad();
//获得属性
osg::ref_ptr<osg::StateSet> state=new osg::StateSet();
state=node->getOrCreateStateSet();
//创建材质对象
osg::ref_ptr<osg::Material> mat=new osg::Material;
//设置正面散射颜色
mat->setDiffuse(osg::Material::FRONT,osg::Vec4(1.0,0.0,0.0,1.0));
//设置正面镜面颜色
mat->setSpecular(osg::Material::FRONT,osg::Vec4(1.0,0.0,0.0,1.0));
//设置正面指数
mat->setShininess(osg::Material::FRONT,90.0);
state->setAttribute(mat.get());
//设置背部剔除看不见背面东西
osg::ref_ptr<osg::CullFace> cullface=new osg::CullFace(osg::CullFace::BACK);
state->setAttribute(cullface.get());
state->setMode(GL_CULL_FACE,osg::StateAttribute::ON);
root->addChild(node.get());
viewer.setSceneData(root);
viewer.realize();
viewer.run();
}
osg::ref_ptr<osg::Node> createQuad()
{
osg::ref_ptr<osg::Geometry> geom=new osg::Geometry;
//首先定义四个点
osg::ref_ptr<osg::Vec3Array> v=new osg::Vec3Array;
geom->setVertexArray(v.get());
v->push_back(osg::Vec3(-1.f,0.f,-1.f));
v->push_back(osg::Vec3(1.f,0.f,-1.f));
v->push_back(osg::Vec3(1.f,0.f,1.f));
v->push_back(osg::Vec3(-1.f,0.f,1.f));
//定义颜色数组
osg::ref_ptr<osg::Vec4Array> c=new osg::Vec4Array;
geom->setColorArray(c.get());
geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
c->push_back(osg::Vec4(1.f,0.f,0.f,1.f));
c->push_back(osg::Vec4(0.f,1.f,0.f,1.f));
c->push_back(osg::Vec4(0.f,0.f,1.f,1.f));
c->push_back(osg::Vec4(1.f,1.f,1.f,1.f));
//设置定点关联方式
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
//方法一
//手动定义法线
osg::ref_ptr<osg::Vec3Array> n=new osg::Vec3Array;
geom->setNormalArray(n.get());
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
n->push_back(osg::Vec3(-2.f,-1.f,10.f));
//方法二
//自动生成法线
osgUtil::SmoothingVisitor::smooth(*(geom.get()));//自动生成法线
osg::ref_ptr<osg::Geode> geode=new osg::Geode;
geode->addDrawable(geom.get());
return geode.get();
}
#include<osgViewer/Viewer>
#include<osg/Node>
#include<osg/Geode>
#include<osg/Geometry>
#include <osg/StateSet>
#include <osgUtil/SmoothingVisitor>
#include <osg/CullFace>
#include <osg/Material>
osg::ref_ptr<osg::Node> createQuad();
void main()
{
osgViewer::Viewer viewer;
osg::Group *root=new osg::Group();
osg::ref_ptr<osg::Node> node=createQuad();
//获得属性
osg::ref_ptr<osg::StateSet> state=new osg::StateSet();
state=node->getOrCreateStateSet();
//创建材质对象
osg::ref_ptr<osg::Material> mat=new osg::Material;
//设置正面散射颜色
mat->setDiffuse(osg::Material::FRONT,osg::Vec4(1.0,0.0,0.0,1.0));
//设置正面镜面颜色
mat->setSpecular(osg::Material::FRONT,osg::Vec4(1.0,0.0,0.0,1.0));
//设置正面指数
mat->setShininess(osg::Material::FRONT,90.0);
state->setAttribute(mat.get());
//设置背部剔除看不见背面东西
osg::ref_ptr<osg::CullFace> cullface=new osg::CullFace(osg::CullFace::BACK);
state->setAttribute(cullface.get());
state->setMode(GL_CULL_FACE,osg::StateAttribute::ON);
root->addChild(node.get());
viewer.setSceneData(root);
viewer.realize();
viewer.run();
}
osg::ref_ptr<osg::Node> createQuad()
{
osg::ref_ptr<osg::Geometry> geom=new osg::Geometry;
//首先定义四个点
osg::ref_ptr<osg::Vec3Array> v=new osg::Vec3Array;
geom->setVertexArray(v.get());
v->push_back(osg::Vec3(-1.f,0.f,-1.f));
v->push_back(osg::Vec3(1.f,0.f,-1.f));
v->push_back(osg::Vec3(1.f,0.f,1.f));
v->push_back(osg::Vec3(-1.f,0.f,1.f));
//定义颜色数组
osg::ref_ptr<osg::Vec4Array> c=new osg::Vec4Array;
geom->setColorArray(c.get());
geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
c->push_back(osg::Vec4(1.f,0.f,0.f,1.f));
c->push_back(osg::Vec4(0.f,1.f,0.f,1.f));
c->push_back(osg::Vec4(0.f,0.f,1.f,1.f));
c->push_back(osg::Vec4(1.f,1.f,1.f,1.f));
//设置定点关联方式
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
//方法一
//手动定义法线
osg::ref_ptr<osg::Vec3Array> n=new osg::Vec3Array;
geom->setNormalArray(n.get());
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
n->push_back(osg::Vec3(-2.f,-1.f,10.f));
//方法二
//自动生成法线
osgUtil::SmoothingVisitor::smooth(*(geom.get()));//自动生成法线
osg::ref_ptr<osg::Geode> geode=new osg::Geode;
geode->addDrawable(geom.get());
return geode.get();
}