Convert 3D points to 2D points using GUIDemo

Here I'll create and show how to use a function to convert 3D points to 2D points, which is useful for debugging purposes, for showing names over character heads, to highlight some locations/objects, etc.

This is made for projects which are based on the GUIDemo.

Header
unsigned int screenWidth, screenHeight; float ratio; MOCamera * GUI_camera; MOCamera * Game_camera; MScene * GUI_scene; MScene * Game_scene;

onBeginScene
MEngine * engine = MEngine::getInstance; // get the engine instance MLevel * level = engine->getLevel; // get the current level GUI_scene = level->getSceneByName("GUI"); Game_scene = level->getSceneByName("MyScene");

MSystemContext * system = engine->getSystemContext; system->getScreenSize(&screenWidth, &screenHeight); ratio = (float)screenWidth/(float)screenHeight;

GUI_camera = GUI_scene->getCameraByName("Camera"); Game_camera = Game_scene->getCameraByName("MyCamera");

Function to convert 3D points to 2D points
MVector3 MyGame::to2D(MVector3 pos) {	pos = Game_camera->getProjectedPoint(pos); if(pos.z<1) {	pos.x = pos.x / screenWidth - 0.5f; pos.y = pos.y = 1 - (pos.y / screenHeight) - 0.5f; pos.x = GUI_camera->getPosition.x + pos.x * GUI_camera->getFov * ratio; pos.y = GUI_camera->getPosition.y + pos.y * GUI_camera->getFov; pos.z = -10; }	else pos.z = -10000;	//out of GUI_camera return pos; }

Use example
MVector3 pos(50,50,50); pos = to2D(pos); char string[30]; sprintf_s(string,30,"%f %f %f",pos.x,pos.y,pos.z); MOText * text = GUI_scene->getTextByName("MyText"); text->setText(string); pos.y -= text->getBoundingBox->min.y/2; //move it to its vertical center text->setPosition(pos);