[osg-users] different materials for a geometry and highlight
Sebastian Messerschmidt
sebastian.messerschmidt at gmx.de
Wed Sep 28 08:34:17 PDT 2016
There you go:
The example without vertex sharing.
>
>
> Am 9/28/2016 um 4:57 PM schrieb Gianni Ambrosio:
>> Hi Sebastian,
>> I would like to adopt you solution if possible but I was not able to
>> implement with textures the same behaviour of the example I did with
>> primitive sets.
>>
>> I know that on windows the example crashes in debug mode because of
>> an assertion inside Microsoft implementaition of std::vector. It's a
>> matter of _ITERATOR_DEBUG_LEVEL. In release mode it works fine. Just
>> to explain the reason. Anyway the movie should not crash :D
> Movie is working ;) But if it is giving you an assertion in debug and
> works in release mode it simply means you're using undefined behavior.
> You should never rely on something like this.
>>
>> So, do you think it would be possible to implement the same behaviour
>> with textures?
> Not directly. If you really need to tag individual triangles it isn't
> doable with a simple texture. In this case my next approach actually
> would include using vertex colors and some shader to draw flat colored
> triangles based on the provoking vertex.
> The problem that makes this non-trivial is simply due to the vertex
> sharing. If you duplicate the vertices (or at least some of them) it
> is solveable without fancy tricks in 5 minutes, but depending on the
> number of triangles in your road this might not be an option.
> So how many triangles will be in your road?
>
> Cheers
> Sebastian
>>
>> Regards,
>> Gianni
>>
>> ------------------
>> Read this topic online here:
>> http://forum.openscenegraph.org/viewtopic.php?p=68814#68814
>>
>>
>>
>>
>>
>> _______________________________________________
>> osg-users mailing list
>> osg-users at lists.openscenegraph.org
>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
-------------- next part --------------
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/MatrixTransform>
#include <osg/PolygonOffset>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/MultiTouchTrackballManipulator>
#include <osg/PolygonMode>
#include <osg/LineWidth>
#include <osg/ShadeModel>
#include <osgGA/StateSetManipulator>
#include <osgDB/ReadFile>
//#include <osgQt/GraphicsWindowQt>
#include <iostream>
const unsigned int DIMENSION = 64;
class SelectModelHandler : public osgGA::GUIEventHandler
{
public:
SelectModelHandler()
: _selector(0), currentPrimitiveSetIndex(0), _root(0), _image(0)
{}
virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE &&
ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON &&
ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_CTRL)
{
osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
if (viewer)
{
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
osgUtil::IntersectionVisitor iv(intersector.get());
osg::Camera* camera = viewer->getCamera();
camera->accept(iv);
if (intersector->containsIntersections())
{
osgUtil::LineSegmentIntersector::Intersection result = *(intersector->getIntersections().begin());
doUserOperationsColor(result);
}
}
}
return false;
}
virtual void doUserOperations(osgUtil::LineSegmentIntersector::Intersection& result)
{
osg::Geometry* geom = dynamic_cast<osg::Geometry*>(result.drawable.get());
osg::Vec3 tc;
//the result seems slightly off
osg::Texture* tex = result.getTextureLookUp(tc);
if (tex && tex->getImage(0))
{
tex->getImage(0)->setColor(osg::Vec4d(1, 1, 1, 1), tc);
tex->getImage(0)->dirty();
tex->dirtyTextureObject();
}
}
virtual void doUserOperationsColor(osgUtil::LineSegmentIntersector::Intersection& result)
{
osg::Geometry* geom = dynamic_cast<osg::Geometry*>(result.drawable.get());
osg::Vec4Array& color = dynamic_cast<osg::Vec4Array&>(*geom->getColorArray());
color[result.indexList[0]] = osg::Vec4(1, 0, 0, 1);
color[result.indexList[1]] = osg::Vec4(1, 0, 0, 1);
color[result.indexList[2]] = osg::Vec4(1, 0, 0, 1);
geom->dirtyDisplayList();
color.dirty();
}
protected:
osg::ref_ptr<osg::Geometry> _selector;
unsigned int currentPrimitiveSetIndex;
osg::ref_ptr<osg::Group> _root;
osg::ref_ptr<osg::Image> _image;
};
osg::Vec3Array* buildVertices() {
osg::Vec3Array* vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(0, 0, 0));
vertices->push_back(osg::Vec3(10, 0, 0));
vertices->push_back(osg::Vec3(10, 10, 0));
vertices->push_back(osg::Vec3(0, 10, 0));
vertices->push_back(osg::Vec3(20, 0, 0));
vertices->push_back(osg::Vec3(20, 10, 0));
vertices->push_back(osg::Vec3(20, 20, 0));
vertices->push_back(osg::Vec3(10, 20, 0));
vertices->push_back(osg::Vec3(0, 20, 0));
return vertices;
}
osg::Vec3Array* buildVertices(unsigned int num_rows)
{
float length = 1.0;
osg::Vec3Array* vertices = new osg::Vec3Array;
for (unsigned int j = 0; j < num_rows; ++j)
{
for (unsigned int i = 0; i < num_rows; ++i)
{
vertices->push_back(osg::Vec3(i * length, j *length, 0.0));
vertices->push_back(osg::Vec3((i+1) * length, j *length, 0.0));
vertices->push_back(osg::Vec3((i) * length, (j+1) *length, 0.0));
vertices->push_back(osg::Vec3((i + 1) * length, j *length, 0.0));
vertices->push_back(osg::Vec3((i + 1) * length, (j+1) *length, 0.0));
vertices->push_back(osg::Vec3(i* length, (j + 1) * length, 0.0));
}
}
return vertices;
}
osg::DrawElementsUInt* buildElements(unsigned int num_rows)
{
osg::DrawElementsUInt* element = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES);
for (unsigned int i = 0; i < (num_rows) *(num_rows) * 6; ++i)
{
element->push_back(i);
}
return element;
}
osg::Vec4Array* buildColors(unsigned int num_rows) {
osg::Vec4Array* colors = new osg::Vec4Array(num_rows *num_rows * 6);
std::fill(std::begin(*colors), std::end(*colors), osg::Vec4(0.50f, 0.5f, 0.5f, 1.0f));
return colors;
}
osg::Geometry* buildGeometry() {
osg::Geometry* geometry = new osg::Geometry;
geometry->setDataVariance(osg::Object::DYNAMIC);
geometry->setVertexArray(buildVertices(255));
geometry->setColorArray(buildColors(255), osg::Array::BIND_PER_VERTEX);
geometry->addPrimitiveSet(buildElements(255));
return geometry;
}
osg::Node* createScene() {
osg::Geode* geode = new osg::Geode;
geode->addDrawable(buildGeometry());
return geode;
}
int main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc, argv);
osgViewer::Viewer viewer(arguments);
viewer.setUpViewInWindow(0, 0, 1000, 1000);
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(createScene());
root->getOrCreateStateSet()->setAttribute(new osg::ShadeModel(osg::ShadeModel::FLAT));
osg::ref_ptr<SelectModelHandler> selector = new SelectModelHandler;
viewer.setSceneData(root);
viewer.addEventHandler(selector.get());
viewer.setCameraManipulator(new osgGA::TrackballManipulator);
// add the window size toggle handler
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
viewer.run();
}
More information about the osg-users
mailing list