Skip to content

Commit

Permalink
UPBGE: Render text objects as mesh objects.
Browse files Browse the repository at this point in the history
Previously all text objects was rendered at the frame render end. It caused that
as text objects use transparency, they were all on top of all other transparent
regular meshes which are not writing to the depth buffer.

To solve this issue the text objects should be rendered like the other regular meshes
and enjoy the ordering made for transparency objects. But this would assume that
text objects are meshes which is not really the case as they don't use any materials.

To first considerate the text objects as meshes they need a material. This material
is particular as it doesn't bind any shader. It exists only to say that meshes
using it are texts. This material must be global to the game engine to make sure
that we use as much as possible a common RAS_DisplayArrayBucket and RAS_MaterialBucket.

The material is then a subclass of RAS_IPolyMaterial named KX_TextMaterial which
contains mostly empty functions, only it's constructor enable material text flags.
An instance of this material is stored statically in KX_TextMaterial.cpp and get its
pointer with GetTextMaterial from KX_TextMaterial.h.

RAS_IPolyMaterial contains a new function IsText which returns true for KX_TextMaterial
depending of a text flag. This function is used when creating the material bucket to
add the material bucket in a specific list accessed with m_buckets[TEXT_BUCKET].
The function is used again when rendering to call IndexPrimitivesText with the mesh
user.

As texts objects doesn't have any RAS_MeshObject and RAS_DisplayArray, then the
RAS_DisplayArrayBucket must check if the m_mesh is non NULL before for example get if
it is modified. Also m_mesh is used to differ a modifier display array bucket than a
text display array bucket in FindDisplayArrayBucket as the both have a NULL display
array.

As KX_FontObject inherit of KX_GameObject, we just have to make virtual functions
managing mesh user and mesh slots (AddMeshUser, UpdateBuckets). The override of
AddMeshUser just find or create the material bucket with the global KX_TextMaterial.
UpdateBuckets updates all data needed to render the texts in the RAS_TextUser.

The RAS_TextUser is a subclass of RAS_MeshUser containing extra data used to render
the texts:
- dpi
- size
- aspect
- resolution
- fontid
- offset
- spacing
- texts[]

In IndexPrimitivesText we loop on all text contained in RAS_TextUser::m_texts, translate
text matrix and call RenderText3D with all other values contained in RAS_TextUser.

Two bugs are fixed with this patch coresponding to issues #119 and #114. #119 is about
unordered transparency and #114 is for text rendered even if they are inactive because
the scene render all object in the list of texts which is wrong as some of them can be
inactive. This is fixed by the fact that only active objects update theirs mesh user and
add theirs mesh slot to be rendered.

This patch allow for the moment to draw text objects as meshes with an empty material,
but later if a way is found to render texts with original material, the rest will be
already done.
  • Loading branch information
panzergame committed Jul 17, 2016
1 parent 366017f commit 7038db2
Show file tree
Hide file tree
Showing 26 changed files with 521 additions and 90 deletions.
4 changes: 1 addition & 3 deletions source/gameengine/Converter/BL_BlenderDataConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1141,9 +1141,7 @@ static KX_GameObject *gameobject_from_blenderobject(
/* font objects have no bounding box */
gameobj = new KX_FontObject(kxscene,KX_Scene::m_callbacks, rendertools, ob, do_color_management);

/* add to the list only the visible fonts */
if ((ob->lay & kxscene->GetBlenderScene()->lay) != 0)
kxscene->AddFont(static_cast<KX_FontObject*>(gameobj));
kxscene->AddFont(static_cast<KX_FontObject*>(gameobj));
break;
}

Expand Down
2 changes: 2 additions & 0 deletions source/gameengine/Ketsji/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ set(SRC
KX_SoundActuator.cpp
KX_StateActuator.cpp
KX_SteeringActuator.cpp
KX_TextMaterial.cpp
KX_TimeCategoryLogger.cpp
KX_TimeLogger.cpp
KX_TouchEventManager.cpp
Expand Down Expand Up @@ -216,6 +217,7 @@ set(SRC
KX_SoundActuator.h
KX_StateActuator.h
KX_SteeringActuator.h
KX_TextMaterial.h
KX_TimeCategoryLogger.h
KX_TimeLogger.h
KX_TouchEventManager.h
Expand Down
134 changes: 83 additions & 51 deletions source/gameengine/Ketsji/KX_FontObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@
#include "KX_Scene.h"
#include "KX_Globals.h"
#include "KX_PythonInit.h"
#include "KX_TextMaterial.h"
#include "BLI_math.h"
#include "EXP_StringValue.h"
#include "RAS_IRasterizer.h"
#include "RAS_BucketManager.h"
#include "RAS_MaterialBucket.h"
#include "RAS_TextUser.h"

/* paths needed for font load */
#include "BLI_blenlib.h"
Expand Down Expand Up @@ -113,6 +117,85 @@ void KX_FontObject::ProcessReplica()
KX_GameObject::ProcessReplica();
}

void KX_FontObject::AddMeshUser()
{
m_meshUser = new RAS_TextUser(m_pClient_info);

// set the part of the mesh slot that never change
float *fl = GetOpenGLMatrixPtr()->getPointer();
m_meshUser->SetMatrix(fl);

RAS_BucketManager *bucketManager = GetScene()->GetBucketManager();
bool created = false;
RAS_MaterialBucket *bucket = bucketManager->FindBucket(GetTextMaterial(), created);

// If the material bucket is just created then we add a new mesh slot.
if (created) {
bucket->AddMesh(NULL);
}

/* We copy the original mesh slot which is at the begin of the list, if it's not the case it
* doesn't matter as the mesh slot are all similar exepted their mesh user pointer which is
* set to NULL in copy. By copying instead of adding a mesh slot we reuse the same display
* array bucket.
*/
RAS_MeshSlot *ms = bucket->CopyMesh(*bucket->msBegin());
ms->SetMeshUser(m_meshUser);
ms->SetDeformer(NULL);
m_meshUser->AddMeshSlot(ms);
}

void KX_FontObject::UpdateBuckets()
{
// Update datas and add mesh slot to be rendered only if the object is not culled.
if (m_bVisible && m_meshUser) {
if (m_pSGNode->IsDirty()) {
GetOpenGLMatrix();
}

// Allow for some logic brick control
if (GetProperty("Text")) {
m_text = split_string(GetProperty("Text")->GetText());
}

// update the animated color
GetObjectColor().getValue(m_color);

// Font Objects don't use the glsl shader, this color management code is copied from gpu_shader_material.glsl
float color[4];
if (m_do_color_management) {
linearrgb_to_srgb_v4(color, m_color);
}
else {
copy_v4_v4(color, m_color);
}

// HARDCODED MULTIPLICATION FACTOR - this will affect the render resolution directly
const float RES = BGE_FONT_RES * m_resolution;

const float size = m_fsize * NodeGetWorldScaling()[0] * RES;
const float aspect = m_fsize / size;

// Account for offset
MT_Vector3 offset = NodeGetWorldOrientation() * m_offset * NodeGetWorldScaling();
// Orient the spacing vector
MT_Vector3 spacing = MT_Vector3(0.0f, m_fsize * m_line_spacing, 0.0f);

RAS_TextUser *textUser = (RAS_TextUser *)m_meshUser;

textUser->SetColor(MT_Vector4(color));
textUser->SetFrontFace(!m_bIsNegativeScaling);
textUser->SetFontId(m_fontid);
textUser->SetSize(size);
textUser->SetDpi(m_dpi);
textUser->SetAspect(aspect);
textUser->SetOffset(offset);
textUser->SetSpacing(spacing);
textUser->SetTexts(m_text);
textUser->ActivateMeshSlots();
}
}

int GetFontId(VFont *vfont)
{
PackedFile *packedfile = NULL;
Expand Down Expand Up @@ -158,57 +241,6 @@ int GetFontId(VFont *vfont)
return fontid;
}

void KX_FontObject::DrawFontText()
{
// Allow for some logic brick control
if (GetProperty("Text"))
m_text = split_string(GetProperty("Text")->GetText());

// only draws the text if visible
if (GetVisible() == 0) {
return;
}

// update the animated color
GetObjectColor().getValue(m_color);

// Font Objects don't use the glsl shader, this color management code is copied from gpu_shader_material.glsl
float color[4];
if (m_do_color_management) {
linearrgb_to_srgb_v4(color, m_color);
}
else {
copy_v4_v4(color, m_color);
}

// HARDCODED MULTIPLICATION FACTOR - this will affect the render resolution directly
const float RES = BGE_FONT_RES * m_resolution;

const float size = m_fsize * NodeGetWorldScaling()[0] * RES;
const float aspect = m_fsize / size;

/* Get a working copy of the OpenGLMatrix to use */
float *mat = GetOpenGLMatrix();

/* Account for offset */
MT_Vector3 offset = NodeGetWorldOrientation() * m_offset * NodeGetWorldScaling();
mat[12] += offset[0]; mat[13] += offset[1]; mat[14] += offset[2];

/* Orient the spacing vector */
MT_Vector3 spacing = MT_Vector3(0.0f, m_fsize * m_line_spacing, 0.0f);
spacing = NodeGetWorldOrientation() * spacing * NodeGetWorldScaling()[1];

/* Draw each line, taking spacing into consideration */
for (unsigned int i = 0; i < m_text.size(); ++i) {
if (i != 0) {
mat[12] -= spacing[0];
mat[13] -= spacing[1];
mat[14] -= spacing[2];
}
m_rasterizer->RenderText3D(m_fontid, m_text[i], int(size), m_dpi, color, mat, aspect);
}
}

#ifdef WITH_PYTHON

/* ------------------------------------------------------------------------- */
Expand Down
3 changes: 2 additions & 1 deletion source/gameengine/Ketsji/KX_FontObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class KX_FontObject : public KX_GameObject

virtual ~KX_FontObject();

void DrawFontText();
virtual void AddMeshUser();
virtual void UpdateBuckets();

/**
* Inherited from CValue -- return a new copy of this
Expand Down
4 changes: 2 additions & 2 deletions source/gameengine/Ketsji/KX_GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ class KX_GameObject : public SCA_IObject
* Update buckets to indicate that there is a new
* user of this object's meshes.
*/
void
virtual void
AddMeshUser(
);

Expand All @@ -730,7 +730,7 @@ class KX_GameObject : public SCA_IObject
* creating or duplicating the object, changing
* visibility, object color, .. .
*/
void UpdateBuckets();
virtual void UpdateBuckets();

/**
* Clear the meshes associated with this class
Expand Down
7 changes: 0 additions & 7 deletions source/gameengine/Ketsji/KX_KetsjiEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ void KX_KetsjiEngine::RenderDome()

// do the rendering
m_dome->RenderDomeFrame(scene, cam, i);
// render all the font objects for this scene
scene->RenderFonts();
}

list<class KX_Camera *> *cameras = scene->GetCameras();
Expand All @@ -315,8 +313,6 @@ void KX_KetsjiEngine::RenderDome()

// do the rendering
m_dome->RenderDomeFrame(scene, (*it), i);
// render all the font objects for this scene
scene->RenderFonts();
}

it++;
Expand Down Expand Up @@ -1184,9 +1180,6 @@ void KX_KetsjiEngine::RenderFrame(KX_Scene *scene, KX_Camera *cam)

scene->RenderBuckets(camtrans, m_rasterizer);

// render all the font objects for this scene
scene->RenderFonts();

if (scene->GetPhysicsEnvironment())
scene->GetPhysicsEnvironment()->DebugDrawWorld();
}
Expand Down
9 changes: 0 additions & 9 deletions source/gameengine/Ketsji/KX_Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1749,15 +1749,6 @@ void KX_Scene::RenderBuckets(const MT_Transform & cameratransform,
KX_BlenderMaterial::EndFrame(rasty);
}

void KX_Scene::RenderFonts()
{
list<KX_FontObject*>::iterator it = m_fonts.begin();
while (it != m_fonts.end()) {
(*it)->DrawFontText();
++it;
}
}

void KX_Scene::UpdateObjectLods()
{
if (!m_active_camera)
Expand Down
5 changes: 0 additions & 5 deletions source/gameengine/Ketsji/KX_Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,6 @@ class KX_Scene : public CValue, public SCA_IScene
KX_FontObject*
);

/** Render the fonts in this scene. */
void
RenderFonts(
);

/** Camera Routines */

std::list<class KX_Camera*>*
Expand Down
116 changes: 116 additions & 0 deletions source/gameengine/Ketsji/KX_TextMaterial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Tristan Porteries.
*
* ***** END GPL LICENSE BLOCK *****
*/

/** \file gameengine/Ketsji/KX_TextMaterial.cpp
* \ingroup ketsji
*/

#include "KX_TextMaterial.h"

#include "DNA_material_types.h"

static KX_TextMaterial textMaterial = KX_TextMaterial();

KX_TextMaterial *GetTextMaterial()
{
return &textMaterial;
}

KX_TextMaterial::KX_TextMaterial()
:RAS_IPolyMaterial("__TextMaterial__", NULL)
{
m_rasMode |= (RAS_ALPHA | RAS_TEXT);
m_flag |= RAS_BLENDERGLSL;
m_alphablend = GEMAT_ALPHA;
}

KX_TextMaterial::~KX_TextMaterial()
{
}

void KX_TextMaterial::Activate(RAS_IRasterizer *rasty)
{
}

void KX_TextMaterial::Desactivate(RAS_IRasterizer *rasty)
{
}

void KX_TextMaterial::ActivateInstancing(RAS_IRasterizer *rasty, void *matrixoffset, void *positionoffset, void *coloroffset, unsigned int stride)
{
}

void KX_TextMaterial::DesactivateInstancing()
{
}

void KX_TextMaterial::ActivateMeshSlot(RAS_MeshSlot *ms, RAS_IRasterizer *rasty)
{
}

const STR_String& KX_TextMaterial::GetTextureName() const
{
static STR_String empty = "";
return empty;
}

Material *KX_TextMaterial::GetBlenderMaterial() const
{
return NULL;
}

Image *KX_TextMaterial::GetBlenderImage() const
{
return NULL;
}

MTexPoly *KX_TextMaterial::GetMTexPoly() const
{
return NULL;
}

Scene *KX_TextMaterial::GetBlenderScene() const
{
return NULL;
}

bool KX_TextMaterial::UseInstancing() const
{
return false;
}

void KX_TextMaterial::ReleaseMaterial()
{
}

void KX_TextMaterial::UpdateIPO(MT_Vector4 rgba, MT_Vector3 specrgb, MT_Scalar hard, MT_Scalar spec, MT_Scalar ref,
MT_Scalar emit, MT_Scalar ambient, MT_Scalar alpha, MT_Scalar specalpha)
{
}

void KX_TextMaterial::Replace_IScene(SCA_IScene *val)
{
}

void KX_TextMaterial::OnConstruction()
{
}
Loading

0 comments on commit 7038db2

Please sign in to comment.