Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

You can create custom dependency graph nodes, which carry out custom operations based on their incoming data. Each kind of node you write can be added to the dependency graph of an agent.

This is an example of a node that returns the intersection between a ray and and a height field.


Code Block
languagecpp
#include <AtomsGraph/Globals.h>
#include <AtomsGraph/Ports.h>
#include <Atoms/HeightFields.h>
#include <Atoms/HeightField.h>

class HeighFieldIntersectorNode:
		public AtomsGraph::Node
{
	public:

		NODE_STANDARD_MEMBERS

		HeighFieldIntersectorNode();

		virtual ~HeighFieldIntersectorNode();

		virtual bool compute();

	private:

		AtomsGraph::StringPort *m_heightFieldNamePort;

		AtomsGraph::VectorPort *m_rayOriginPort;

		AtomsGraph::VectorPort *m_rayDirectionPort;

		AtomsGraph::VectorPort *m_outPositionPort;

		AtomsGraph::VectorPort *m_outNormalPort;
};

#define HEIGHTFIELDINTERSECTOR_NODE_ID 999999 // this must be unique

NODE_STANDARD_MEMBERS_IMPL(HeighFieldIntersectorNode)
	unsigned int HeighFieldIntersectorNode::staticTypeId() { return HEIGHTFIELDINTERSECTOR_NODE_ID; }
	std::string HeighFieldIntersectorNode::staticTypeStr() { return std::string("HeighFieldIntersectorNode");}

	HeighFieldIntersectorNode::HeighFieldIntersectorNode()
	{
		m_heightFieldNamePort = new AtomsGraph::StringPort("heightField");
		m_rayOriginPort = new AtomsGraph::VectorPort("rayOrigin");
		m_rayDirectionPort = new AtomsGraph::VectorPort("rayDirection");
		m_outPositionPort = new AtomsGraph::VectorPort("outPosition");
		m_outNormalPort = new AtomsGraph::VectorPort("outNormal");

		addInputPort(m_heightFieldNamePort);
		addInputPort(m_rayOriginPort);
		addInputPort(m_rayDirectionPort);
		addOutputPort(m_outPositionPort);
		addOutputPort(m_outNormalPort);
	}

	HeighFieldIntersectorNode::~HeighFieldIntersectorNode()
	{
		// the parent destructor automatically delete all the ports so don't need to delete them here
	}

	bool HeighFieldIntersectorNode::compute()
	{
		Atoms::HeightFields& fields = Atoms::HeightFields::instance();

		const std::string& heightFieldName = heightFieldNamePort->getRef();

		AtomsUtils::Mesh* heightField = fields.heightField(heightFieldName);
		if (!heightField)
			return false;

		if (heightField->intersect(rayOriginPort->getRef(), -upDirVec, param, faceId, u, v, false))
		{
		}
		
		return true;
	}