|
本帖最后由 jihuitian11 于 2014-7-15 17:38 编辑
我想用一块dem数据,读取高程数据之后,在匹配上纹理,这里主要指遥感影像,请问纹理,坐标如何计算和匹配,我自己做的显示不出来啊?!
废话不多,给出我的程序,求论坛的大神,王锐,freesouth等人指导:
- #pragma region
- osg::ref_ptr<osg::Vec3Array> vc = new osg::Vec3Array();
- osg::ref_ptr<osg::Vec2Array> vt = new osg::Vec2Array();
- short int*z_value=(short int*)ZValue;
- //i,行,j列
- for (int i=0;i<width-1;i++)
- {
- for (int j=0;j<heigth-1;j++)
- {
-
- float x0=rasterio->adfGeoTransform[0] + i* rasterio->adfGeoTransform[1] + j* rasterio->adfGeoTransform[2];
- float y0=rasterio->adfGeoTransform[3] + i* rasterio->adfGeoTransform[4] + j* rasterio->adfGeoTransform[5];
- float z0=(float)z_value[i*width+j];
- if (z0<=NoDataValue)
- {
- z0=0;
- }
- float x1=rasterio->adfGeoTransform[0] + i* rasterio->adfGeoTransform[1] + (j+1)* rasterio->adfGeoTransform[2];
- float y1=rasterio->adfGeoTransform[3] + i* rasterio->adfGeoTransform[4] + (j+1)* rasterio->adfGeoTransform[5]
- float z1=(float)z_value[i*width+j+1];
- if (z1<=NoDataValue)
- {
- z1=0;
- }
- float x2=rasterio->adfGeoTransform[0] + (i+1)* rasterio->adfGeoTransform[1] + (j+1)* rasterio->adfGeoTransform[2];
- float y2=rasterio->adfGeoTransform[3] + (i+1)* rasterio->adfGeoTransform[4] + (j+1)* rasterio->adfGeoTransform[5];
- float z2=(float)z_value[(i+1)*width+j+1];
- if (z2<=NoDataValue)
- {
- z2=0;
- }
- float x3=rasterio->adfGeoTransform[0] + (i+1)* rasterio->adfGeoTransform[1]+j*rasterio->adfGeoTransform[2];
- float y3=rasterio->adfGeoTransform[3] + (i+1)* rasterio->adfGeoTransform[4]+j*rasterio->adfGeoTransform[5];
- float z3=(float)z_value[(i+1)*width+j];
- if (z3<=NoDataValue)
- {
- z3=0;
- }
- osg::Vec3 vec0(x0,y0,z0);
- osg::Vec3 vec1(x1,y1,z1);
- osg::Vec3 vec2(x2,y2,z2);
- osg::Vec3 vec3(x3,y3,z3);
- vc->push_back(vec0);
- vc->push_back(vec1);
- vc->push_back(vec2);
- vc->push_back(vec3);
- float tx0=(float)((vec0.y()-uly)/heigth);
- float ty0=(float)((vec0.x()-ulx)/width);
- float tx1=(float)((vec1.y()-uly)/heigth);
- float ty1=(float)((vec1.x()-ulx)/width);
- float tx2=(float)((vec2.y()-uly)/heigth);
- float ty2=(float)((vec2.x()-ulx)/width);
- float tx3=(float)((vec3.y()-uly)/heigth);
- float ty3=(float)((vec3.x()-ulx)/width);
- float tx1=(float)((vec1.x()-ulx)/width);
- float ty1=(float)((vec1.y()-uly)/heigth);
- float tx2=(float)((vec2.x()-ulx)/width);
- float ty2=(float)((vec2.y()-uly)/heigth);
- float tx3=(float)((vec3.x()-ulx)/width);
- float ty3=(float)((vec3.y()-uly)/heigth);
- vt->push_back(osg::Vec2(tx0,ty0));
- vt->push_back(osg::Vec2(tx1,ty1));
- vt->push_back(osg::Vec2(tx2,ty2));
- vt->push_back(osg::Vec2(tx3,ty3));
- }
- }
复制代码
1、这一步是读取dem数据,并计算气纹理坐标,由于我用的是new osg:rawArrays(osg:rimitiveSet:UADS,0,4),所以,vc 里面,我按照逆时针存了一个正方形 ,同样纹理的坐标我也相应的存了4个点的坐标,我知道这样做浪费内存,以后在优化吧,现在主要为了能显示;
2、-
- osg::ref_ptr<osg::Image>image=osgDB::readImageFile("D:\\osgproject\\data\\ps_texture_1k.png");
-
复制代码
读遥感影像,
-
- geometry->setVertexArray(vc.get());//设置顶点数组
- geometry->setTexCoordArray(0,vt.get());//设置纹理坐标
- osg::ref_ptr<osg::StateSet>stateset=new osg::StateSet();//创建状态集
-
- if(image.get())
- {
- osg::ref_ptr<osg::Texture2D>texture=new osg::Texture2D;//创建二维纹理对象
- texture->setDataVariance(osg::Object::DYNAMIC);
- texture->setImage(image.get());//设置贴图
- stateset->setTextureAttributeAndModes(0,texture.get(),osg::StateAttribute::ON);
- }
- geometry->setTexCoordArray(0,vt.get());//设置纹理坐标
- //geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);//设置纹理坐标的绑定方式是单个顶点
-
- geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
-
- geode->setStateSet(stateset.get());
- geode->addDrawable(geometry);
复制代码
匹配遥感影像作为纹理,结果显示的很奇葩,不在给图片了;
请论坛里的大神们指教!!收费可以私聊。
|
|