1

i'm learning opengl programming for study purposes and i'm using openscenegraph wrapper around opengl.

My first goal is to render a 3d grid in which heights data is stored in a file as 2d matrix. First question:can i use TRIANGLE_STRIP to render that grid which can have concave or convex zones?

Second question: Is there a particular alghoritm to render 3d grids (terrains) for this purpose?

I started from a very simple grid data:

10 10 10 10 10
10 10 10 10 10
10 10  5 10 10
10 10 10 10 10
10 10 10 10 10

it is a very simple 5x5 heights grid, but the result, using triangle_strip is follow: enter image description here

as u can see there is a extra-line that i cant drop from my triangle_Strip algorithm.

the code i used is follow (consider it is openscenegraph code). in rows and columns vars (int) i store grid dimensions. data is a osg::Vec3** pointer in which previously i read plain values. m_rpTerrainData is a osg::ref_ptr<osg::Vec3Array> in which i load, in correct order (as triangle_strip wants) the vertices.

for(x=0;x<rows;x++)
{
    for(y=0;y<columns;y++)
    {

        if(x<rows-1 && y<columns-1){
          this->m_rpTerrainData->push_back(data[x][y]);
          this->m_rpTerrainData->push_back(data[x+1][y]);

        }


                     /*degenerate triangle because we at last of a column*/
         if(x<rows-1 && y==columns-1 ){
           this->m_rpTerrainData->push_back(data[x][y]);
           this->m_rpTerrainData->push_back(data[x+1][y]);
                       this->m_rpTerrainData->push_back(data[x+1][y]);
                       this->m_rpTerrainData->push_back(data[x+1][0]);
        }                       
    }
}
    //drop last element
this->m_rpTerrainData->pop_back();
4

1 回答 1

0

我承认我懒得仔细研究你的算法,但如果你不介意重新发明轮子,我建议你使用NvTristrip 库。有了这个,您可以获取索引网格并将其转换为三角网格。

然后,您可以创建一个正常的索引矩形网格,并在每个顶点指定 x、y、h 坐标(其中 h 是矩阵的高度或“数据”)。然后,您使用 NvTristrip 将该索引网格转换为 tristrip,并将此数据提供给 OpenScenegraph。

我自己将 NvTristrip 与 OpenScenegraph 一起使用,它工作正常。

但是,我很好奇为什么您首先需要一个 tristrip,因为索引网格似乎非常适合您的目的。OpenScenegraph 非常高兴地支持 QUAD 原语。我认为这会简化你的工作。

于 2014-02-22T06:23:29.803 回答