Convert 3D points to 2D points using GUIDemo

From MaratisWiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "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...")
 
 
(2 intermediate revisions by one user not shown)
Line 5: Line 5:
 
==Header==
 
==Header==
 
<pre>
 
<pre>
unsigned int screenWidth, screenHeight;
+
unsigned int screenWidth, screenHeight;
float ratio;
+
float ratio;
MOCamera * GUI_camera;
+
MOCamera * GUI_camera;
MOCamera * Game_camera;
+
MOCamera * Game_camera;
MScene * GUI_scene;
+
MScene * GUI_scene;
MScene * Game_scene;
+
MScene * Game_scene;
 
</pre>
 
</pre>
  
 
==onBeginScene==
 
==onBeginScene==
 
<pre>
 
<pre>
MEngine * engine = MEngine::getInstance(); // get the engine instance
+
MEngine * engine = MEngine::getInstance(); // get the engine instance
MLevel * level = engine->getLevel(); // get the current level
+
MLevel * level = engine->getLevel(); // get the current level
GUI_scene = level->getSceneByName("GUI");
+
GUI_scene = level->getSceneByName("GUI");
Game_scene = level->getSceneByName("MyScene");
+
Game_scene = level->getSceneByName("MyScene");
  
MSystemContext * system = engine->getSystemContext();
+
MSystemContext * system = engine->getSystemContext();
system->getScreenSize(&screenWidth, &screenHeight);
+
system->getScreenSize(&screenWidth, &screenHeight);
ratio = (float)screenWidth/(float)screenHeight;
+
ratio = (float)screenWidth/(float)screenHeight;
  
GUI_camera = GUI_scene->getCameraByName("Camera");
+
GUI_camera = GUI_scene->getCameraByName("Camera");
Game_camera = Game_scene->getCameraByName("MyCamera");
+
Game_camera = Game_scene->getCameraByName("MyCamera");
 
</pre>
 
</pre>
  
Line 60: Line 60:
  
 
[http://www.cplusplus.com/reference/cstdio/printf/ printf (for the specifier characters)]
 
[http://www.cplusplus.com/reference/cstdio/printf/ printf (for the specifier characters)]
 +
 +
[[Category: User manual]]

Latest revision as of 11:06, 5 February 2014

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.

Contents

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);

External links

sprintf

printf (for the specifier characters)

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox