基本圖元
- 學(xué)習(xí)目標(biāo)
1.學(xué)習(xí)基本圖元的概念
2.學(xué)習(xí)基本圖元的生成方法
3.學(xué)習(xí)基本圖元的操作
- 學(xué)習(xí)內(nèi)容
1.基本圖元指的是數(shù)學(xué)描述中的基本模塊妹笆,三位中的模塊為正方體、圓球和圓錐等宵统,在osg中晕讲,我們使用Drawable類將這些圖元進(jìn)行存儲,二維中的模塊為正方形马澈、三角形等瓢省,二維的幾何模塊使用Geometry保存。二維和三維圖元最后都需要保存在Geode容器中痊班。
2.繪制二維和三維圖元時(shí)都可以對這些圖元的屬性和顯示模式進(jìn)行修改和添加勤婚。如三維圖元可以添加色彩、紋理和修改材質(zhì)涤伐。二維圖元可以修改頂點(diǎn)的顏色和線條的粗細(xì)等馒胆。
3.下面我們根據(jù)具體的代碼進(jìn)行學(xué)習(xí):
1)自己編寫的創(chuàng)建三維圖元函數(shù)
osg::ref_ptr<osg::Geode> creatGeode()
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::ShapeDrawable> shape;
osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
//設(shè)置精度為0.5
hints->setDetailRatio(0.5);
//繪制一個(gè)盒子
shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3d(0.0, 0.0, 0.0), 1.0,1.0,10.0), hints.get());
//設(shè)置顏色
shape->setColor(osg::Vec4d(1.0, 1.0, 1.0, 1.0));
//設(shè)置材質(zhì)
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0)); //設(shè)置環(huán)境
shape->getOrCreateStateSet()->setAttributeAndModes(material.get(), osg::StateAttribute::ON); //設(shè)置材質(zhì)的屬性和模式
//設(shè)置紋理
osg::ref_ptr<osg::Texture2D> texture2D = new osg::Texture2D;
osg::Image* image = osgDB::readImageFile("F:\\OSG_2019\\OSG\\OpenSceneGraph-Data\\Images\\whitemetal_normal.jpg"); //獲取紋理圖片
if (image->valid())
{
texture2D->setImage(image);
}
shape->getOrCreateStateSet()->setTextureAttributeAndModes(1, texture2D.get(), osg::StateAttribute::ON); //設(shè)置紋理的屬性和模式
//這里我寫錯(cuò)過,注意:必須把你畫的東西加入Geode類中
geode->addDrawable(shape);
return geode;
}
主函數(shù)進(jìn)行使用
int main()
{
osgViewer::Viewer viewer; //聲明一個(gè)視景器
osg::ref_ptr<osg::Geode> geode = creatGeode(); //創(chuàng)建一個(gè)三維圖元
osg::ref_ptr<osg::Group> group = new osg::Group;
group->addChild(geode); //三維圖元加入到容器中
viewer.setSceneData(group);
viewer.realize();
viewer.run();
return 0;
}
2)自己編寫的創(chuàng)建二維圖元函數(shù)
//創(chuàng)建二維圖元
osg::ref_ptr<osg::Geode> createsimpleGeode()
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
geode->addDrawable(geometry.get());
osg::ref_ptr<osg::Vec3Array> point = new osg::Vec3Array; //存儲頂點(diǎn)坐標(biāo)
osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array; //存儲頂點(diǎn)顏色
osg::ref_ptr<osg::LineWidth> linewidth = new osg::LineWidth; //設(shè)置線條粗細(xì)
osg::ref_ptr<osg::Vec3Array> norm = new osg::Vec3Array; //平面法向量
//設(shè)置頂點(diǎn)
geometry->setVertexArray(point.get());
//添加頂點(diǎn)關(guān)聯(lián)方式
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::LINE_LOOP, 0, 4)); //點(diǎn)依次相連成圈
//設(shè)置顏色
geometry->setColorArray(color.get());
geometry->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX);
//設(shè)置線的寬度
linewidth->setWidth(10);
geode->getOrCreateStateSet()->setAttributeAndModes(linewidth.get(), osg::StateAttribute::ON);
//設(shè)置透明度
geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
osg::Vec3 vec3 = { -10.0, 0.0, -10.0 };
point->push_back(vec3);
vec3 = { -10.0, 0.0, 10.0 };
point->push_back(vec3);
vec3 = { 10.0, 0.0, 10.0 };
point->push_back(vec3);
vec3 = { 10.0, 0.0, -10.0 };
point->push_back(vec3);
osg::Vec4 vec4 = { 1, 0, 0, 0.5 };
color->push_back(vec4);
vec4 = { 0, 1.0, 0, 0.5 };
color->push_back(vec4);
vec4 = { 0, 0, 1.0, 0.5 };
color->push_back(vec4);
vec4 = { 1.0, 1.0, 0, 0.5 };
color->push_back(vec4);
//設(shè)置法向量
norm->push_back(osg::Vec3(0, 1, 0));
geometry->setNormalArray(norm.get());
geometry->setNormalBinding(osg::Geometry::AttributeBinding::BIND_OVERALL); //設(shè)置法向量綁定
return geode;
}
二維圖元在主函數(shù)中使用
int main()
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Geode> geode = createsimpleGeode(); //接受函數(shù)返回值的變量類型必須和函數(shù)返回值的類型相同,返回超級指針指向的對像
osg::ref_ptr<osg::Group> group = new osg::Group;
group->addChild(geode);
viewer.setSceneData(group);
viewer.realize();
return viewer.run();
}