Added triangle index functor to src/osgUtil/SphereSegment.cpp for computing
the intersection between an indivual geometry and the sphere segment. Converted the terrain in osgspheresegment to be an osg::Geometry based drawable rather than a osg::ShapeDrawable/osg::HeightField one to make it more practicle to use as a test for the new SphereSegment intersection code.
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
#include <osg/MatrixTransform>
|
||||
#include <osg/Geometry>
|
||||
|
||||
#include <osgUtil/SmoothingVisitor>
|
||||
|
||||
#include <osgDB/ReadFile>
|
||||
|
||||
#include <osgText/Text>
|
||||
@@ -233,24 +235,84 @@ void build_world(osg::Group *root)
|
||||
|
||||
terrainGeode->setStateSet( stateset );
|
||||
|
||||
float size = 1000; // 10km;
|
||||
float scale = size/39.0f; // 10km;
|
||||
float z_scale = scale*3.0f;
|
||||
|
||||
osg::HeightField* grid = new osg::HeightField;
|
||||
grid->allocate(38,39);
|
||||
grid->setXInterval(scale);
|
||||
grid->setYInterval(scale);
|
||||
|
||||
for(unsigned int r=0;r<39;++r)
|
||||
{
|
||||
for(unsigned int c=0;c<38;++c)
|
||||
{
|
||||
grid->setHeight(c,r,z_scale*vertex[r+c*39][2]);
|
||||
}
|
||||
}
|
||||
terrainGeode->addDrawable(new osg::ShapeDrawable(grid));
|
||||
|
||||
{
|
||||
unsigned int numColumns = 38;
|
||||
unsigned int numRows = 39;
|
||||
unsigned int r, c;
|
||||
|
||||
osg::Vec3 origin(0.0f,0.0f,0.0f);
|
||||
osg::Vec3 size(1000.0f,1000.0f,250.0f);
|
||||
|
||||
osg::Geometry* geometry = new osg::Geometry;
|
||||
|
||||
osg::Vec3Array& v = *(new osg::Vec3Array(numColumns*numRows));
|
||||
osg::Vec2Array& tc = *(new osg::Vec2Array(numColumns*numRows));
|
||||
osg::Vec4ubArray& color = *(new osg::Vec4ubArray(1));
|
||||
|
||||
color[0].set(255,255,255,255);
|
||||
|
||||
float rowCoordDelta = size.y()/(float)(numRows-1);
|
||||
float columnCoordDelta = size.x()/(float)(numColumns-1);
|
||||
|
||||
float rowTexDelta = 1.0f/(float)(numRows-1);
|
||||
float columnTexDelta = 1.0f/(float)(numColumns-1);
|
||||
|
||||
// compute z range of z values of grid data so we can scale it.
|
||||
float min_z = FLT_MAX;
|
||||
float max_z = -FLT_MAX;
|
||||
for(r=0;r<numRows;++r)
|
||||
{
|
||||
for(c=0;c<numColumns;++c)
|
||||
{
|
||||
min_z = osg::minimum(min_z,vertex[r+c*numRows][2]);
|
||||
max_z = osg::maximum(max_z,vertex[r+c*numRows][2]);
|
||||
}
|
||||
}
|
||||
|
||||
float scale_z = size.z()/(max_z-min_z);
|
||||
|
||||
osg::Vec3 pos = origin;
|
||||
osg::Vec2 tex(0.0f,0.0f);
|
||||
int vi=0;
|
||||
for(r=0;r<numRows;++r)
|
||||
{
|
||||
pos.x() = origin.x();
|
||||
tex.x() = 0.0f;
|
||||
for(c=0;c<numColumns;++c)
|
||||
{
|
||||
v[vi].set(pos.x(),pos.y(),pos.z()+(vertex[r+c*numRows][2]-min_z)*scale_z);
|
||||
tc[vi] = tex;
|
||||
pos.x()+=columnCoordDelta;
|
||||
tex.x()+=columnTexDelta;
|
||||
++vi;
|
||||
}
|
||||
pos.y() += rowCoordDelta;
|
||||
tex.y() += rowTexDelta;
|
||||
}
|
||||
|
||||
geometry->setVertexArray(&v);
|
||||
geometry->setTexCoordArray(0, &tc);
|
||||
geometry->setColorArray(&color);
|
||||
geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
|
||||
|
||||
for(r=0;r<numRows-1;++r)
|
||||
{
|
||||
osg::DrawElementsUShort& drawElements = *(new osg::DrawElementsUShort(GL_QUAD_STRIP,2*numColumns));
|
||||
geometry->addPrimitiveSet(&drawElements);
|
||||
int ei=0;
|
||||
for(c=0;c<numColumns;++c)
|
||||
{
|
||||
drawElements[ei++] = (r+1)*numColumns+c;
|
||||
drawElements[ei++] = (r)*numColumns+c;
|
||||
}
|
||||
}
|
||||
|
||||
osgUtil::SmoothingVisitor smoother;
|
||||
smoother.smooth(*geometry);
|
||||
|
||||
terrainGeode->addDrawable(geometry);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -272,7 +334,7 @@ void build_world(osg::Group *root)
|
||||
root->addChild(ss.get());
|
||||
}
|
||||
|
||||
osgSim::SphereSegment::LineList lines = ss->computeIntersection(terrainGeode.get(), osg::Matrixd::identity());
|
||||
osgSim::SphereSegment::LineList lines = ss->computeIntersection(osg::Matrixd::identity(), terrainGeode.get());
|
||||
if (!lines.empty())
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"We've found intersections!!!!"<<std::endl;
|
||||
|
||||
Reference in New Issue
Block a user