OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

SetAttribute / RemoveAttribute Question


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
Valzul (Paul Bigeon)
Newbie


Joined: 16 Aug 2017
Posts: 5

PostPosted: Tue Nov 28, 2017 4:35 pm    Post subject:
SetAttribute / RemoveAttribute Question
Reply with quote

Hi,

Considering I've created an osg::Material this way, and that the nodes are shown on screen :

Code:

m_material_selected = new osg::Material();
m_material_selected->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(0.2f, 0.2f, 0.75f, 0.5f));
m_material_selected->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4f(0.2f, 0.2f, 0.75f, 0.5f));
m_material_selected->setShininess(osg::Material::FRONT_AND_BACK, 75.0);
m_material_selected->setColorMode(osg::Material::AMBIENT);


And then I set it to a node like this ( this is apparently working so far ... ) :

Code:

// Where node_sel is a previously created osg::Node*
node_sel->getOrCreateStateSet()->setAttribute(m_material_selected, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);


But when I try to remove this attribute from the previously created node like this :

Code:

// Where node_unsel = node_sel;
node_unsel->getOrCreateStateSet()->removeAttribute(m_material_selected);


My app crashes.

What did I do wrong ? Is it unsafe to removeAttribute from a node this way ?
I've seen on the forum things like NodeVisitor class, should I be using this or can I change nodes manually ?


Thank you for your interest in my question,

Have a good day,

Paul
Back to top
View user's profile Send private message Send e-mail
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 12094

PostPosted: Tue Nov 28, 2017 4:48 pm    Post subject:
SetAttribute / RemoveAttribute Question
Reply with quote

Hi Paul,

You haven't provided a stack trace, or how you manage your nodes and
pointers so there isn't much we can say specifically.

As a general comment, these type of crashes on what should otherwise
be a perfect reasonable operation, is usually down to dangling
pointers of some kind. In your case perhaps the node has been deleted
elsewhere so your node_unsel pointer is invalid, or the pointer to the
material is invalid.

If you are keeping pointers around to objects in the scene graph you
shouldn't use C pointers as they can easily become dangling pointers.
If you do need to keep a pointer around either use an osg::ref_ptr<>
to make sure you keep the object alive even when all other references
to it are removed, or use an osg::observer_ptr<> that automatically
gets reset to 0 when the object it pointers is deleted.

Robert.


On 28 November 2017 at 16:35, Paul Bigeon <> wrote:
Quote:
Hi,

Considering I've created an osg::Material this way, and that the nodes are shown on screen :


Code:

m_material_selected = new osg::Material();
m_material_selected->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(0.2f, 0.2f, 0.75f, 0.5f));
m_material_selected->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4f(0.2f, 0.2f, 0.75f, 0.5f));
m_material_selected->setShininess(osg::Material::FRONT_AND_BACK, 75.0);
m_material_selected->setColorMode(osg::Material::AMBIENT);




And then I set it to a node like this ( this is apparently working so far ... ) :


Code:

// Where node_sel is a previously created osg::Node*
node_sel->getOrCreateStateSet()->setAttribute(m_material_selected, osg::StateAttribute:ShockedN | osg::StateAttribute::OVERRIDE);




But when I try to remove this attribute from the previously created node like this :


Code:

// Where node_unsel = node_sel;
node_unsel->getOrCreateStateSet()->removeAttribute(m_material_selected);




My app crashes.

What did I do wrong ? Is it unsafe to removeAttribute from a node this way ?
I've seen on the forum things like NodeVisitor class, should I be using this or can I change nodes manually ?


Thank you for your interest in my question,

Have a good day,

Paul

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=72479#72479








------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
Valzul (Paul Bigeon)
Newbie


Joined: 16 Aug 2017
Posts: 5

PostPosted: Tue Nov 28, 2017 5:21 pm    Post subject:
Reply with quote

Hi Robert,

Thanks for your answer,
Sorry I rushed into it directly without giving more context :

I'm trying to select and deselect nodes on screen, like in my previous post
by applying a specific material to a node, and deselect it by erasing this specific material.

My UI architecture is as such that, the view containing OSG nodes has to communicate directly with a tree of items ( where those items are representing infos about nodes in the view )

In fact, I've implemented a class that is storing a set of nodes coming from both the view and the tree ( an item does have the corresponding node pointer attached to it).

There is no deletion of items or nodes whatsoever in any part of my program.

I will try to take a look at the type of pointer i'm using through the program first.

As for the call stack, unfortunately MSVC gives me an exception that I cannot understand. I've tried to compile my application in debug x64 mode but couldn't make it work, so I'm a bit short in terms of error reporting ... but i'll try to come up with a debug version

Again, thanks for all your work Robert !

Paul
Back to top
View user's profile Send private message Send e-mail
GeekOwl
Newbie


Joined: 15 Sep 2017
Posts: 6

PostPosted: Wed Nov 29, 2017 12:58 pm    Post subject:
Reply with quote

Hey,

question = you ve a tree of items which correspond to osg nodes ? and whenever you select any of them you wish to show "it selected " ???

If so :

Code:


const osg::Vec4 normalColor(1.0f, 1.0f, 1.0f, 1.0f);
const osg::Vec4 selectedColor(1.0f, 0.0f, 0.0f, 0.8f);

void setSelected(bool value)
{
   osg::ref_ptr<osg::Material> mat = new osg::Material;
   auto color = value ? selectedColor : normalColor;
   mat->setDiffuse(osg::Material::FRONT_AND_BACK, color);
   mModel->getOrCreateStateSet()->setAttributeAndModes(mat, osg::StateAttribute::ON);
   mModel->getOrCreateStateSet()->setAttributeAndModes(mat, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
}



If its a wrong answer sorry - i didnt understand well !!![/code]
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts gl_vertexID and OpenSceneGraph question EonStrife General 0 Wed Mar 21, 2018 1:52 pm View latest post
No new posts Touchscreen and GUIEventHandler question jeladi General 0 Thu Mar 08, 2018 10:09 pm View latest post
No new posts osgFX::Outline question jeladi General 0 Wed Dec 20, 2017 8:09 pm View latest post
No new posts osggeometryshaders example question mgreene General 3 Tue Sep 12, 2017 3:12 pm View latest post
No new posts setAttribute / Material issue Valzul General 4 Wed Aug 16, 2017 8:15 am View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP