四.处理多多边形
作者:互联网
处理多多边形跟处理多边形差不多,就是多一层循环,然后把需要的gpu信息存起来就行
bool AkGisWidget::SendGpuMultiPolygon(OGRMultiPolygon *pMultiPolygonIn)
{
QOpenGLVertexArrayObject *vao;
QOpenGLBuffer *vbo;//{QOpenGLBuffer::VertexBuffer};
QOpenGLBuffer *ebo;//{QOpenGLBuffer::IndexBuffer};
vao = new QOpenGLVertexArrayObject;
vbo = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
// ebo = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
int pointsCount =0;
int sizeOffset = 0;
int countOffset = 0;
FeatureDesc featureDesc;
featureDesc.vecVex.push_back(0);
featureDesc.vecIdx.push_back(0);
int polyCnt= pMultiPolygonIn->getNumGeometries();
std::vector<float> verticess; //所有点
std::vector<unsigned int> indicess; //所有索引
//循环多边形
for (int i = 0; i < polyCnt; ++i)
{
OGRPolygon* pPolygon = pMultiPolygonIn->getGeometryRef(i)->toPolygon();
OGRPoint ptTemp;
int numInteriorRings = pPolygon->getNumInteriorRings();
// 外环
OGRLinearRing* poExteriorRing = pPolygon->getExteriorRing();
int numExteriorRingPoints = poExteriorRing->getNumPoints();
int polygonPointsCount = numExteriorRingPoints;
float* vertices = new float[polygonPointsCount * 5 * sizeof(float)];
int iStride = 0;
using Point = std::array<double, 2>;
std::vector<std::vector<Point>> polygon;
std::vector<Point> exteriorRing;
exteriorRing.reserve(numExteriorRingPoints);
int index = 0;
for (int i = 0; i < numExteriorRingPoints; ++i) {
poExteriorRing->getPoint(i, &ptTemp);
exteriorRing.push_back({ptTemp.getX(),ptTemp.getY()});
vertices[index] = ptTemp.getX();
vertices[index + 1] = ptTemp.getY();
vertices[index + 2] = 0.5;
vertices[index + 3] = 0.6;
vertices[index + 4] = 0.1;
index += 5;
}
for (int i = 0; i < index; ++i) {
verticess.push_back(vertices[i]);
}
polygon.emplace_back(exteriorRing);
std::vector<unsigned int> indices = mapbox::earcut<unsigned int>(polygon);
if(numExteriorRingPoints==4)
qDebug()<<"exteriorRingPointsCount interiorRingsCount"<<numExteriorRingPoints<<"indices.size()"<<indices.size()
<<vertices[0]<<vertices[1]<<vertices[5]<<vertices[6]<<vertices[10]<<vertices[11]<<vertices[15]<<vertices[16]
<<exteriorRing.size()<<exteriorRing[0].at(0)<<exteriorRing[0].at(1);
//qDebug()<<"indices"<<sizeof(unsigned int)<<index<<indices.size();
//增加索引号
if (countOffset > 0)
std::for_each(indices.begin(), indices.end(), [&countOffset](auto& value) {value += countOffset; });
for(int i = 0 ;i <indices.size();i++)
indicess.push_back(indices[i]);
countOffset += polygonPointsCount;
featureDesc.vecVex.push_back(polygonPointsCount);//点数
featureDesc.vecIdx.push_back(indices.size());//索引数
}
qDebug()<<"polyCnt"<<polyCnt<<"verticess"<<verticess.size()<<"indicess"<<indicess.size();
//发送到gpu
vbo->create();
vbo->bind();
vbo->allocate(&verticess[0],verticess.size()*4);
QOpenGLVertexArrayObject::Binder vaoBind(vao);
int offset=0;
//分段存索引
//for(int j=0;j<1;j++)
for(int j=0;j<featureDesc.vecIdx.size()-1;j++)
{
ebo = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
ebo->create();
ebo->bind();
ebo->allocate(&indicess[offset],(featureDesc.vecIdx[j+1])*4);
featureDesc.vecEbo.push_back(ebo);
offset+= featureDesc.vecIdx[j+1];
qDebug()<<"ebo"<<ebo->bufferId()<<"offset"<<offset;
}
featureDesc.type = 6 ;
featureDesc.indexCnt = indicess.size();
featureDesc.vexCnt = verticess.size();
featureDesc.vao = vao;
featureDesc.vbo = vbo;
featureDesc.ebo = ebo;
featureDesc.shader = &polygonShader;
vecFeatureDesc.append(featureDesc);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1);
vbo->release();
return true;
}
标签:多边形,处理,ebo,vbo,back,int,featureDesc,QOpenGLBuffer 来源: https://www.cnblogs.com/fkess/p/12506449.html