obtaining scene node from shader callback

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
kornwaretm
Competition winner
Posts: 189
Joined: Tue Oct 16, 2007 3:53 am
Location: Indonesia
Contact:

obtaining scene node from shader callback

Post by kornwaretm »

the purpose is to make closer object to be transparent. now i implement GLSL shading and using

Code: Select all

vec3 ecPosition = vec3(mWorldViewProj * gl_Vertex);
distanceFromCamera = distance(ecPosition, mEyePos);
to obtain the distance. here's the screenshoot
Image
i think it will look nicer if the transparent is flat on whole node. is there any way that ican obtain the distance from the shaded node and the active cammera?
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Re: obtaining scene node from shader callback

Post by Halifax »

kornwaretm wrote:the purpose is to make closer object to be transparent. now i implement GLSL shading and using

Code: Select all

vec3 ecPosition = vec3(mWorldViewProj * gl_Vertex);
distanceFromCamera = distance(ecPosition, mEyePos);
to obtain the distance. here's the screenshoot
Image
i think it will look nicer if the transparent is flat on whole node. is there any way that ican obtain the distance from the shaded node and the active cammera?
In software you could calculate the distance to the front of the AABB in the center, or you could calculate the the distance to the volumetric (I guess that's the right word) center of the AABB and use that to scale the alpha factor.
TheQuestion = 2B || !2B
kornwaretm
Competition winner
Posts: 189
Joined: Tue Oct 16, 2007 3:53 am
Location: Indonesia
Contact:

Post by kornwaretm »

so i have to obtain the Shaded ISceneNode via IShaderConstantSetCallBack, am i rigth? then how to do that?

the parameters in IShaderConstantSetCallBack only IMaterialRendererServices* and user data. and i need to know which SceneNode is reder when the video driver calling the callback. did i miss something?
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I don't see any obvious way to do what you want without making chanes to the library. The IShaderConstantSetCallBack doesn't get any information about the ISceneNode or geometry being rendered.

If you only need to do this for one scene node, you could put an ISceneNode* in your IShaderConstantSetCallBack derived class. When the OnSetConstants() function is called, you'd just use that pointer.

If you need to do this for multiple nodes, you could hijack one of the MaterialTypeParam fields and treat that as a scene node ID. This would assume that f32 is the same size as s32, which should be safe if the type names reflect the actual implementation.

So your code would look something like this...

Code: Select all

class MyShaderCallback : public video::IShaderConstantSetCallback
{
public:
  MyShaderCallback (scene::ISceneManager* smgr)
    : SceneManager (smgr)
    , ActiveSceneNode (0)
  {
  }

  virtual void OnSetMaterial(const video::SMaterial& m);

  virtual void OnSetConstants(video::IMaterialRenderServices* s, s32 userData);

private:
  scene::ISceneManager* SceneManager;
  scene::ISceneNode* ActiveNode;
};

void MyShaderCallback::OnSetMaterial (const SMaterial& m)
{
  // abuse MaterialTypeParam2 as the scene node id
  const s32 nodeID = *(s32*)&m.MaterialTypeParam2;

  // find the scene node that has the given id
  ActiveSceneNode = SceneManager->getSceneNodeFromId(nodeID);
}

void MyShaderCallback::OnSetConstants (IMaterialRenderServices* s, s32 userData)
{
  if (ActiveSceneNode)
  {
    core::aabbox3df bbox = ActiveNode->getBoundingBox();
    ActiveNode->getAbsoluteTransformation().transformBoxEx(bbox);

    // do whatever you need to do
  }
}
Then, when setting the material you'd do this...

Code: Select all

const s32 nodeID = node->getID();

for (u32 i = 0; i < node->getMaterialCount(); ++i)
{
  video::SMaterial& mat = node->getMaterial(i);
  mat.MaterialType = (video::E_MATERIAL_TYPE)matID;
  mat.MaterialTypeParam2 = *(f32*)&nodeID;
}
Travis
Post Reply