[SOLVED] Node 3D to NDC coordinates


photo

Recommended Posts

Hi,

I need the 2D screen Position of a 3D Node transformation.

Can you write me a short code example with unigine matrices form the Player and Node and perspective division to get the NDC coordinates?

	auto proj = m_player->getProjection();
	auto view = m_player->getTransform();
	auto obj = instance->second->getNode()->getTransform();
	auto projectedPosition = Unigine::Math::Mat4(proj)*view*obj*Unigine::Math::Vec4(0.0, 0.0, 0.0, 1.0);
	projectedPosition /= glm::abs(projectedPosition.w);
	x = (projectedPosition.x+1.0f)*0.5f * m_ungineAppHandle->getWidth();
	y = (1.f-(projectedPosition.z+1.0f)*0.5f) * m_ungineAppHandle->getHeight();

Thank you in advance,

Sebastian

Link to post

Hi,

I have found the solution.

You must take the inverse of the view matrix, and divide by z and not w, and take y instead of z for the ndc up axis

	auto proj = m_player->getProjection();
	auto view = m_player->getTransform();
	auto obj = instance->second->getNode()->getTransform();
	auto projectedPosition = Unigine::Math::Vec4(0.0, 0.0, 0.0, 1.0);
	projectedPosition = obj*projectedPosition;
	projectedPosition = Unigine::Math::inverse(view)*projectedPosition;
	projectedPosition = Unigine::Math::Mat4(proj)*projectedPosition;

	projectedPosition /= std::abs(projectedPosition.z);
	x = (projectedPosition.x+1.0f)*0.5f * m_ungineAppHandle->getWidth();
	y = (1.f-(projectedPosition.y+1.0f)*0.5f) * m_ungineAppHandle->getHeight();

 

 

 

Link to post
  • silent changed the title to [SOLVED] Node 3D to NDC coordinates