|
读取一张地形图,按照行主序将每个顶点的数据存入到顶点数组中,按照逆时针方向存储顶点索引,想通过着色器来渲染地形网格,按照索引绘制画不出来,求大神帮忙看看问题出在哪里,部分代码如下:
osg::ref_ptr<osg::Vec3Array> veca = new osg::Vec3Array;
osg::ref_ptr<osg::UIntArray> indexA = new osg::UIntArray;
//顶点
for (int r = 0; r < numRows; r++) //r相当于z c相当于x
{
for (int c = 0; c < numColumns; c++)
{
veca->push_back(osg::Vec3f((float)c,(float)r,((*heightMap->data(c,r)))));
}
}
//索引
for (int r = 0; r < numRows-1; r++)
{
for (int c = 0; c < numColumns-1; c++)
{
nLocIndex = r*numColumns + c;
indexA->push_back(nLocIndex);
indexA->push_back(nLocIndex+numColumns);
indexA->push_back(nLocIndex+1);
indexA->push_back(nLocIndex+1);
indexA->push_back(nLocIndex+numColumns);
indexA->push_back(nLocIndex+numColumns+1);
}
}
//这样绘制可以显示,但画出来的不是网格
geom->addPrimitiveSet(new osg:rawArrays(osg:rimitiveSet::TRIANGLES,0,veca->size()));
//用索引画则不能显示
geom->addPrimitiveSet(new osg:rawElementsUInt(osg:rimitiveSet::TRIANGLES,indexA->size()));
geom->setVertexAttribArray(0,veca);
geom->setVertexAttribBinding(0,osg::Geometry::BIND_PER_VERTEX);
//vshader
static char * vertexShader= {
"#version 430 \n"
"layout (location=0) in vec3 VertexPosition;\n"
"uniform mat4 MVP;"
"out vec4 Color;\n"
"void main()\n"
"{\n"
" Color = vec4(1.0,1.0,0.0,1.0);\n"
" gl_Position = MVP * vec4(VertexPosition,1.0);\n"
" }\n"
}; |
|