[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [33707] trunk/blender/source/gameengine: Patch:[#25163] BGE support for Blender Font objects - unicode support

Dalai Felinto dfelinto at gmail.com
Thu Dec 16 11:25:41 CET 2010


Revision: 33707
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=33707
Author:   dfelinto
Date:     2010-12-16 11:25:41 +0100 (Thu, 16 Dec 2010)

Log Message:
-----------
Patch:[#25163] BGE support for Blender Font objects - unicode support
Problem/Bug:
------------
There were no way to have proper unicode characters (e.g. Japanese) in Blender Game Engine. Now we can :)
You can see a sample here: http://blog.mikepan.com/multi-language-support-in-blender/

Functionality Explanation:
--------------------------
This patch converts the Blender Font Objects to a new BGE type: KX_FontObject
This object inherits KX_GameObject.cpp and has the following properties:
- text (the text of the object)
- size (taken from the Blender object, usually is 1.0)
- resolution (1.0 by default, maybe not really needed, but at least for debugging/the time being it's nice to have) 

The way we deal with linked objects is different than Blender. In Blender the text and size are a property of the Text databock. Therefore linked objects necessarily share the same text (and size, although the size of the object datablock affects that too). In BGE they are stored and accessed per object. Without that it would be problematic to have addObject adding texts that don't share the same data.

Known problems/limitations/ToDo:
--------------------------------
1) support for packed font and the <builtin>
2) figure why some fonts are displayed in a different size in 3DView/BGE (BLF)
3) investigate some glitches I see some times
4) support for multiline
5) support for more Blender Font Object options (text aligment, text boxes, ...)

[1] Diego (bdiego) evantually will help on that. For the time being we are using the "default" (ui) font to replace the <builtin>.
[2] but not all of them. I need to cross check who is calculating the size/dpi in/correctly - Blender or BLF. (e.g. fonts that work well - MS Gothic)
[3] I think this may be related to the resolution we are drawing the font
[4] It can't/will not be handled inside BFL. So the way I see it is to implement a mini text library/api that works as a middlelayer between the drawing step and BLF. 
    So instead of:
      BLF_draw(fontid, (char *)text, strlen(text));
    We would do:
      MAGIC_ROUTINE_IM_NOT_BLF_draw(fontir, (char *)text, styleflag, width, height);
[5] don't hold your breath ... but if someone wants to have fun in the holidays the (4) and (5) are part of the same problem.

Code Explanation:
-----------------
The patch should be simple to read. They are three may parts:
1) BL_BlenderDataConversion.cpp:: converts the OB_FONT object into a KX_FontObject.cpp and store it in the KX_Scene->m_fonts
2) KetsjiEngine.cpp::RenderFonts:: loop through the texts and call their internal drawing routine.
3) KX_FontObject.cpp::
  a) constructor: load the font of the object, and store other values.
  b) DrawText: calculate the aspect for the given size (sounds hacky but this is how blf works) and call the render routine in RenderTools
4) KX_BlenderGL.cpp (called from rendertools) ::BL_print_game_line:: Draws the text. Using the BLF API

*) In order to handle visibility of the object added with AddObject I'm adding to the m_scene.m_fonts list only the Fonts in a visible layer - unlike Cameras and Lamps where all the objects are added.

Acknowledgements:
----------------
Thanks Benoit for the review and adjustment suggestions.
Thanks Diego for the BFL expertise, patches and support (Latin community ftw)
Thanks my boss for letting me do part of this patch during work time. Good thing we are starting a project in a partnership with a Japanese Foundation and eventual will need unicode in BGE :) for more details on that - www.nereusprogram.org - let's call it the main sponsor of this "bug feature" ;)

Modified Paths:
--------------
    trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderGL.cpp
    trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderGL.h
    trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderRenderTools.cpp
    trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderRenderTools.h
    trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp
    trunk/blender/source/gameengine/GamePlayer/common/CMakeLists.txt
    trunk/blender/source/gameengine/GamePlayer/common/GPC_RenderTools.cpp
    trunk/blender/source/gameengine/GamePlayer/common/GPC_RenderTools.h
    trunk/blender/source/gameengine/GamePlayer/common/Makefile
    trunk/blender/source/gameengine/GamePlayer/common/SConscript
    trunk/blender/source/gameengine/Ketsji/CMakeLists.txt
    trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp
    trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.cpp
    trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h
    trunk/blender/source/gameengine/Ketsji/KX_PythonInitTypes.cpp
    trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp
    trunk/blender/source/gameengine/Ketsji/KX_Scene.h
    trunk/blender/source/gameengine/Ketsji/Makefile
    trunk/blender/source/gameengine/Ketsji/SConscript
    trunk/blender/source/gameengine/Rasterizer/RAS_IRenderTools.h

Added Paths:
-----------
    trunk/blender/source/gameengine/Ketsji/KX_FontObject.cpp
    trunk/blender/source/gameengine/Ketsji/KX_FontObject.h

Modified: trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderGL.cpp
===================================================================
--- trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderGL.cpp	2010-12-16 10:18:32 UTC (rev 33706)
+++ trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderGL.cpp	2010-12-16 10:25:41 UTC (rev 33707)
@@ -123,6 +123,32 @@
 	}
 }
 
+/* Print 3D text */
+void BL_print_game_line(int fontid, const char* text, int size, int dpi, float* color, double* mat, float aspect)
+{
+	/* gl prepping */
+	DisableForText();
+
+	/* the actual drawing */
+	glColor4fv(color);
+
+	/* multiply the text matrix by the object matrix */
+	BLF_enable(fontid, BLF_MATRIX|BLF_ASPECT);
+	BLF_matrix(fontid, mat);
+
+	/* aspect is the inverse scale that allows you to increase */
+	/* your resolution without sizing the final text size      */
+	/* the bigger the size, the smaller the aspect	           */
+	BLF_aspect(fontid, aspect, aspect, aspect);
+
+	BLF_size(fontid, size, dpi);
+	BLF_position(fontid, 0, 0, 0);
+	BLF_draw(fontid, (char *)text, strlen(text));
+
+	BLF_disable(fontid, BLF_MATRIX|BLF_ASPECT);
+	glEnable(GL_DEPTH_TEST);
+}
+
 void BL_print_gamedebug_line(const char* text, int xco, int yco, int width, int height)
 {	
 	/* gl prepping */

Modified: trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderGL.h
===================================================================
--- trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderGL.h	2010-12-16 10:18:32 UTC (rev 33706)
+++ trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderGL.h	2010-12-16 10:25:41 UTC (rev 33707)
@@ -47,6 +47,7 @@
 void	BL_NormalMouse(struct wmWindow *win);
 void	BL_WaitMouse(struct wmWindow *win);
 
+void BL_print_game_line(int fontid, const char* text, int size, int dpi, float* color, double* mat, float aspect);
 void BL_print_gamedebug_line(const char* text, int xco, int yco, int width, int height);
 void BL_print_gamedebug_line_padded(const char* text, int xco, int yco, int width, int height);
 

Modified: trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderRenderTools.cpp
===================================================================
--- trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderRenderTools.cpp	2010-12-16 10:18:32 UTC (rev 33706)
+++ trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderRenderTools.cpp	2010-12-16 10:25:41 UTC (rev 33707)
@@ -275,8 +275,17 @@
 		}
 	}
 }
+void KX_BlenderRenderTools::RenderText3D(int fontid,
+										 const char* text,
+										 int size,
+										 int dpi,
+										 float* color,
+										 double* mat,
+										 float aspect)
+{
+	BL_print_game_line(fontid, text, size, dpi, color, mat, aspect);
+}
 
-
 void KX_BlenderRenderTools::RenderText2D(RAS_TEXT_RENDER_MODE mode,
 										 const char* text,
 										 int xco,

Modified: trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderRenderTools.h
===================================================================
--- trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderRenderTools.h	2010-12-16 10:18:32 UTC (rev 33706)
+++ trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderRenderTools.h	2010-12-16 10:25:41 UTC (rev 33707)
@@ -69,8 +69,16 @@
 	void				DisableOpenGLLights();
 	void				ProcessLighting(RAS_IRasterizer *rasty, bool uselights, const MT_Transform& viewmat);
 
-	void			    RenderText2D(RAS_TEXT_RENDER_MODE mode,
+	void				RenderText3D(int fontid,
 									 const char* text,
+									 int size,
+									 int dpi,
+									 float* color,
+									 double* mat,
+									 float aspect);
+
+	void		RenderText2D(RAS_TEXT_RENDER_MODE mode,
+									 const char* text,
 									 int xco,
 									 int yco,
 									 int width,

Modified: trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp	2010-12-16 10:18:32 UTC (rev 33706)
+++ trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp	2010-12-16 10:25:41 UTC (rev 33707)
@@ -64,6 +64,7 @@
 #include "KX_Light.h"
 #include "KX_Camera.h"
 #include "KX_EmptyObject.h"
+#include "KX_FontObject.h"
 #include "MT_Point3.h"
 #include "MT_Transform.h"
 #include "MT_MinMax.h"
@@ -1266,10 +1267,13 @@
 			break;
 		case OB_CURVE:
 		case OB_SURF:
-		case OB_FONT:
 			center[0]= center[1]= center[2]= 0.0;
 			size[0]  = size[1]=size[2]=0.0;
 			break;
+		case OB_FONT:
+			center[0]= center[1]= center[2]= 0.0;
+			size[0]  = size[1]=size[2]=1.0;
+			break;
 		case OB_MBALL:
 			bb= ob->bb;
 			break;
@@ -1801,7 +1805,19 @@
 		// set transformation
 		break;
 	}
+
+	case OB_FONT:
+	{
+		/* font objects have no bounding box */
+		gameobj = new KX_FontObject(kxscene,KX_Scene::m_callbacks, rendertools, ob);
+
+		/* add to the list only the visible fonts */
+		if((ob->lay & kxscene->GetBlenderScene()->lay) != 0)
+			kxscene->AddFont(static_cast<KX_FontObject*>(gameobj));
+		break;
 	}
+
+	}
 	if (gameobj) 
 	{
 		gameobj->SetLayer(ob->lay);
@@ -2703,4 +2719,4 @@
 	MT_Scalar distance = (activecam)? activecam->GetCameraFar() - activecam->GetCameraNear(): 100.0f;
 	RAS_BucketManager *bucketmanager = kxscene->GetBucketManager();
 	bucketmanager->OptimizeBuckets(distance);
-}
+}
\ No newline at end of file

Modified: trunk/blender/source/gameengine/GamePlayer/common/CMakeLists.txt
===================================================================
--- trunk/blender/source/gameengine/GamePlayer/common/CMakeLists.txt	2010-12-16 10:18:32 UTC (rev 33706)
+++ trunk/blender/source/gameengine/GamePlayer/common/CMakeLists.txt	2010-12-16 10:25:41 UTC (rev 33707)
@@ -37,6 +37,7 @@
 	../../../../source/blender/imbuf
 	../../../../source/gameengine/Ketsji
 	../../../../source/blender/blenlib
+	../../../../source/blender/blenfont
 	../../../../source/blender/blenkernel
 	../../../../source/blender
 	../../../../source/blender/makesdna

Modified: trunk/blender/source/gameengine/GamePlayer/common/GPC_RenderTools.cpp
===================================================================
--- trunk/blender/source/gameengine/GamePlayer/common/GPC_RenderTools.cpp	2010-12-16 10:18:32 UTC (rev 33706)
+++ trunk/blender/source/gameengine/GamePlayer/common/GPC_RenderTools.cpp	2010-12-16 10:25:41 UTC (rev 33707)
@@ -53,7 +53,11 @@
 
 #include "GPC_RenderTools.h"
 
+extern "C" {
+#include "BLF_api.h"
+}
 
+
 unsigned int GPC_RenderTools::m_numgllights;
 
 GPC_RenderTools::GPC_RenderTools()
@@ -276,7 +280,36 @@
 	}
 }
 
+void GPC_RenderTools::RenderText3D(	int fontid,
+									const char* text,
+									int size,
+									int dpi,
+									float* color,
+									double* mat,
+									float aspect)
+{
+	/* the actual drawing */
+	glColor3fv(color);
+ 
+	/* multiply the text matrix by the object matrix */
+	BLF_enable(fontid, BLF_MATRIX|BLF_ASPECT);
+	BLF_matrix(fontid, mat);
 
+	/* aspect is the inverse scale that allows you to increase */
+	/* your resolution without sizing the final text size */
+	/* the bigger the size, the smaller the aspect	*/
+	BLF_aspect(fontid, aspect, aspect, aspect);
+
+	BLF_size(fontid, size, dpi);
+	BLF_position(fontid, 0, 0, 0);
+	BLF_draw(fontid, (char *)text, strlen(text));
+
+	BLF_disable(fontid, BLF_MATRIX|BLF_ASPECT);
+	glEnable(GL_DEPTH_TEST);
+}
+
+
+
 void GPC_RenderTools::RenderText2D(RAS_TEXT_RENDER_MODE mode,
 										 const char* text,
 										 int xco,

Modified: trunk/blender/source/gameengine/GamePlayer/common/GPC_RenderTools.h
===================================================================
--- trunk/blender/source/gameengine/GamePlayer/common/GPC_RenderTools.h	2010-12-16 10:18:32 UTC (rev 33706)
+++ trunk/blender/source/gameengine/GamePlayer/common/GPC_RenderTools.h	2010-12-16 10:25:41 UTC (rev 33707)
@@ -68,6 +68,13 @@
 	void				DisableOpenGLLights();
 	void				ProcessLighting(RAS_IRasterizer *rasty, bool uselights, const MT_Transform& viewmat);
 
+	void				RenderText3D(int fontid,
+									 const char* text,
+									 int size,
+									 int dpi,
+									 float* color,
+									 double* mat,
+									 float aspect);
 	/* @attention mode is ignored here */
 	void			    RenderText2D(RAS_TEXT_RENDER_MODE mode,
 									 const char* text,

Modified: trunk/blender/source/gameengine/GamePlayer/common/Makefile
===================================================================
--- trunk/blender/source/gameengine/GamePlayer/common/Makefile	2010-12-16 10:18:32 UTC (rev 33706)
+++ trunk/blender/source/gameengine/GamePlayer/common/Makefile	2010-12-16 10:25:41 UTC (rev 33707)
@@ -41,6 +41,7 @@
 CPPFLAGS += -I../../../blender/blenkernel
 CPPFLAGS += -I../../../blender/blenloader
 CPPFLAGS += -I../../../blender/blenlib
+CPPFLAGS += -I../../../blender/blenfont
 CPPFLAGS += -I../../../blender/imbuf
 CPPFLAGS += -I../../../blender/makesdna
 CPPFLAGS += -I../../../blender/gpu

Modified: trunk/blender/source/gameengine/GamePlayer/common/SConscript
===================================================================
--- trunk/blender/source/gameengine/GamePlayer/common/SConscript	2010-12-16 10:18:32 UTC (rev 33706)
+++ trunk/blender/source/gameengine/GamePlayer/common/SConscript	2010-12-16 10:25:41 UTC (rev 33707)
@@ -26,6 +26,7 @@
         '#source/blender/imbuf',
         '#source/gameengine/Ketsji',
         '#source/blender/blenlib',
+        '#source/blender/blenfont',
         '#source/blender/blenkernel',
         '#source/blender',
         '#source/blender/include',

Modified: trunk/blender/source/gameengine/Ketsji/CMakeLists.txt
===================================================================
--- trunk/blender/source/gameengine/Ketsji/CMakeLists.txt	2010-12-16 10:18:32 UTC (rev 33706)
+++ trunk/blender/source/gameengine/Ketsji/CMakeLists.txt	2010-12-16 10:25:41 UTC (rev 33707)
@@ -35,6 +35,7 @@
 	../../../intern/moto/include
 	../../../source/gameengine/Ketsji 
 	../../../source/blender/blenlib
+	../../../source/blender/blenfont
 	../../../source/blender/blenkernel
 	../../../source/blender/python
 	../../../source/blender/python/generic
@@ -70,6 +71,7 @@
 	KX_ConvertPhysicsObjects.cpp
 	KX_Dome.cpp
 	KX_EmptyObject.cpp
+	KX_FontObject.cpp
 	KX_GameActuator.cpp
 	KX_GameObject.cpp
 	KX_IPO_SGController.cpp
@@ -135,6 +137,7 @@
 	KX_ConvertPhysicsObject.h
 	KX_Dome.h
 	KX_EmptyObject.h
+	KX_FontObject.h
 	KX_GameActuator.h
 	KX_GameObject.h
 	KX_IInterpolator.h

Added: trunk/blender/source/gameengine/Ketsji/KX_FontObject.cpp

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list