osg如何畫網格

2021-07-02 18:40:22 字數 1822 閱讀 5159

osg畫網格只要是通過畫線來實現的的,**的的結構主要在於如何構造圖元資訊,及相應的頂點資訊,將geometry新增至節點即可實現,**如下:

osg::ref_ptrtransformgrid = new osg::matrixtransform();

osg::geode *geode = new osg::geode();

osg::geometry *geom = new osg::geometry();

//構造頂點資訊

osg::vec3array *vertices = new osg::vec3array();

int i;

int num = 10;

float rad = 5;

vertices->push_back( osg::vec3(0.0f,-rad,0.0f) );

vertices->push_back( osg::vec3(0.0f, rad,0.0f) );

vertices->push_back( osg::vec3(-rad,0.0f,0.0f) );

vertices->push_back( osg::vec3( rad,0.0f,0.0f) );

for (i = 1; i<=num; i++)

geom->setvertexarray(vertices);

//指定顏色陣列

osg::vec4array* colors = new osg::vec4array();

colors->push_back( osg::vec4(.9f,.9f,.9f,1.0f) );

colors->push_back( osg::vec4(.9f,.9f,.9f,1.0f) );

geom->setcolorarray(colors);

geom->setcolorbinding(osg::geometry::bind_per_primitive_set);

//指定法線

osg::vec3array* normals = new osg::vec3array;

normals->push_back( osg::vec3(0.0f,-1.0f,0.0f) );

geom->setnormalarray(normals);

geom->setnormalbinding(osg::geometry::bind_overall);

//將圖元新增至geometry

geom->addprimitiveset( new osg::drawarrays(osg::primitiveset::lines,0,4) );

geom->addprimitiveset( new osg::drawarrays(osg::primitiveset::lines,4,vertices->size() - 4) );

geode->adddrawable(geom);

osg::stateset *set = new osg::stateset();

set->setmode(gl_lighting,osg::stateattribute::off);

set->setattributeandmodes(new osg::polygonoffset(1.0f,1.0f),osg::stateattribute::on);

set->setmode(gl_line_smooth,osg::stateattribute::on);

geode->setstateset(set);

transformgrid->addchild(geode);

transformgrid->setname("transformgrid");

用Mapx畫網格的心得

1 如果要在圖中根據點,角度劃網格,不要採取求線和邊框交點,再連線的方法,這中方法只對水平垂直的線準確,對斜線均不准。因為邊框的範圍一般較大,幾十公里的尺度,畫出來的角度就有誤差。比如,根據三角計算出來的乙個75度角的交點,連線後再驗證角度,卻只有71度。4 劃線可以採用逐點連線的方法,可以有效的縮...

WinForm畫網格並填充顏色

因為研究codecombat上的最後一題,自己嘗試分解題目,然後想到需要畫網格,還有最優化的方法 源 如下 using system using system.collections.generic using system.componentmodel using system.data usin...

Unity 畫自定義網格

有時候需要程式化動態生成網格 例如骨骼 先3d建乙個模型,然後匯入到unity 除錯檢視mesh.vertices 的排列,用excel幾下索引。然後賦值 的時候記得按照 123456 的順序排列,因為unity就是這樣匯入的貌似,否則畫的網格不正確,最後記得重新計算下法線 這裡不需要設定矩陣 因為...