From 034494a422b1a8fb8c1d69fded4f534e065b80aa Mon Sep 17 00:00:00 2001 From: Porteries Tristan Date: Mon, 22 May 2017 22:38:54 +0200 Subject: [PATCH] UPBGE: Simplify derived mesh draw. The derived mesh draw was simplified by always use GLSL render, this modification allowed to remove the material function GetBlenderImage and GetMTexPoly. --- .../Converter/BL_BlenderDataConversion.cpp | 9 +- .../gameengine/Ketsji/KX_BlenderMaterial.cpp | 21 ----- source/gameengine/Ketsji/KX_BlenderMaterial.h | 5 - source/gameengine/Ketsji/KX_TextMaterial.cpp | 10 -- source/gameengine/Ketsji/KX_TextMaterial.h | 2 - .../Rasterizer/RAS_IPolygonMaterial.h | 4 - .../RAS_OpenGLRasterizer.cpp | 91 ++++++------------- 7 files changed, 33 insertions(+), 109 deletions(-) diff --git a/source/gameengine/Converter/BL_BlenderDataConversion.cpp b/source/gameengine/Converter/BL_BlenderDataConversion.cpp index c82509b9a418..a64ed796f298 100644 --- a/source/gameengine/Converter/BL_BlenderDataConversion.cpp +++ b/source/gameengine/Converter/BL_BlenderDataConversion.cpp @@ -430,7 +430,6 @@ static void GetUVs(const RAS_MeshObject::LayerList& layers, MFace *mface, MTFace static KX_BlenderMaterial *ConvertMaterial( Material *mat, - MTFace *tface, int lightlayer, KX_Scene *scene) { @@ -440,7 +439,7 @@ static KX_BlenderMaterial *ConvertMaterial( name = "MA"; } - KX_BlenderMaterial *kx_blmat = new KX_BlenderMaterial(scene, mat, name, (mat ? &mat->game : nullptr), tface, lightlayer); + KX_BlenderMaterial *kx_blmat = new KX_BlenderMaterial(scene, mat, name, (mat ? &mat->game : nullptr), lightlayer); return kx_blmat; } @@ -456,12 +455,12 @@ static void uvsRgbFromMesh(Material *ma, MFace *mface, MTFace *tface, const RAS_ } } -static RAS_MaterialBucket *material_from_mesh(Material *ma, MTFace *tface, int lightlayer, KX_Scene *scene, KX_BlenderSceneConverter& converter) +static RAS_MaterialBucket *material_from_mesh(Material *ma, int lightlayer, KX_Scene *scene, KX_BlenderSceneConverter& converter) { RAS_IPolyMaterial* polymat = converter.FindPolyMaterial(ma); if (!polymat) { - polymat = ConvertMaterial(ma, tface, lightlayer, scene); + polymat = ConvertMaterial(ma, lightlayer, scene); // this is needed to free up memory afterwards. converter.RegisterPolyMaterial(polymat, ma); } @@ -602,7 +601,7 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, KX_Scene* scene, ma = &defmaterial; } - RAS_MaterialBucket *bucket = material_from_mesh(ma, tface, lightlayer, scene, converter); + RAS_MaterialBucket *bucket = material_from_mesh(ma, lightlayer, scene, converter); meshobj->AddMaterial(bucket, i, vertformat); } diff --git a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp index 9192150c1c90..e1a09af2bb20 100644 --- a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp +++ b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp @@ -50,7 +50,6 @@ KX_BlenderMaterial::KX_BlenderMaterial( Material *mat, const std::string& name, GameSettings *game, - MTFace *mtface, int lightlayer) :RAS_IPolyMaterial(name, game), m_material(mat), @@ -78,13 +77,6 @@ KX_BlenderMaterial::KX_BlenderMaterial( m_alphablend = mat->game.alpha_blend; - m_mtexPoly = new MTexPoly(); - memset(m_mtexPoly, 0, sizeof(MTexPoly)); - - if (mtface) { - ME_MTEXFACE_CPY(m_mtexPoly, mtface); - } - // with ztransp enabled, enforce alpha blending mode if ((mat->mode & MA_TRANSP) && (mat->mode & MA_ZTRANSP) && (m_alphablend == GEMAT_SOLID)) { m_alphablend = GEMAT_ALPHA; @@ -126,8 +118,6 @@ KX_BlenderMaterial::~KX_BlenderMaterial() m_material->amb = m_savedData.ambient; m_material->spectra = m_savedData.specularalpha; - delete m_mtexPoly; - // cleanup work if (m_constructed) { // clean only if material was actually used @@ -135,12 +125,6 @@ KX_BlenderMaterial::~KX_BlenderMaterial() } } -MTexPoly *KX_BlenderMaterial::GetMTexPoly() const -{ - // fonts on polys - return m_mtexPoly; -} - void KX_BlenderMaterial::GetRGBAColor(unsigned char *rgba) const { if (m_material) { @@ -163,11 +147,6 @@ Material *KX_BlenderMaterial::GetBlenderMaterial() const return m_material; } -Image *KX_BlenderMaterial::GetBlenderImage() const -{ - return (m_mtexPoly ? m_mtexPoly->tpage : nullptr); -} - Scene *KX_BlenderMaterial::GetBlenderScene() const { return m_scene->GetBlenderScene(); diff --git a/source/gameengine/Ketsji/KX_BlenderMaterial.h b/source/gameengine/Ketsji/KX_BlenderMaterial.h index 243c744efded..03308c136210 100644 --- a/source/gameengine/Ketsji/KX_BlenderMaterial.h +++ b/source/gameengine/Ketsji/KX_BlenderMaterial.h @@ -19,7 +19,6 @@ class KX_Scene; class BL_BlenderShader; class BL_Shader; struct Material; -struct MTFace; #ifdef USE_MATHUTILS void KX_BlenderMaterial_Mathutils_Callback_Init(void); @@ -35,7 +34,6 @@ class KX_BlenderMaterial : public CValue, public RAS_IPolyMaterial Material *mat, const std::string& name, GameSettings *game, - MTFace *mtface, int lightlayer); virtual ~KX_BlenderMaterial(); @@ -53,8 +51,6 @@ class KX_BlenderMaterial : public CValue, public RAS_IPolyMaterial virtual bool UseInstancing() const; virtual const std::string GetTextureName() const; virtual Material *GetBlenderMaterial() const; - virtual Image *GetBlenderImage() const; - virtual MTexPoly *GetMTexPoly() const; virtual bool UsesLighting(RAS_Rasterizer *rasty) const; virtual void GetRGBAColor(unsigned char *rgba) const; virtual Scene *GetBlenderScene() const; @@ -116,7 +112,6 @@ class KX_BlenderMaterial : public CValue, public RAS_IPolyMaterial private: Material *m_material; - MTexPoly *m_mtexPoly; BL_Shader *m_shader; BL_BlenderShader *m_blenderShader; KX_Scene *m_scene; diff --git a/source/gameengine/Ketsji/KX_TextMaterial.cpp b/source/gameengine/Ketsji/KX_TextMaterial.cpp index abdefe3519be..ba7f28756aea 100644 --- a/source/gameengine/Ketsji/KX_TextMaterial.cpp +++ b/source/gameengine/Ketsji/KX_TextMaterial.cpp @@ -77,16 +77,6 @@ Material *KX_TextMaterial::GetBlenderMaterial() const return nullptr; } -Image *KX_TextMaterial::GetBlenderImage() const -{ - return nullptr; -} - -MTexPoly *KX_TextMaterial::GetMTexPoly() const -{ - return nullptr; -} - Scene *KX_TextMaterial::GetBlenderScene() const { return nullptr; diff --git a/source/gameengine/Ketsji/KX_TextMaterial.h b/source/gameengine/Ketsji/KX_TextMaterial.h index 4a72a9d7b266..1d6c547d6754 100644 --- a/source/gameengine/Ketsji/KX_TextMaterial.h +++ b/source/gameengine/Ketsji/KX_TextMaterial.h @@ -44,8 +44,6 @@ class KX_TextMaterial : public RAS_IPolyMaterial virtual const std::string GetTextureName() const; virtual Material *GetBlenderMaterial() const; - virtual Image *GetBlenderImage() const; - virtual MTexPoly *GetMTexPoly() const; virtual Scene *GetBlenderScene() const; virtual SCA_IScene *GetScene() const; virtual bool UseInstancing() const; diff --git a/source/gameengine/Rasterizer/RAS_IPolygonMaterial.h b/source/gameengine/Rasterizer/RAS_IPolygonMaterial.h index 36b7380e3be6..2b87f6722667 100644 --- a/source/gameengine/Rasterizer/RAS_IPolygonMaterial.h +++ b/source/gameengine/Rasterizer/RAS_IPolygonMaterial.h @@ -42,9 +42,7 @@ #include -struct MTexPoly; struct Material; -struct Image; struct Scene; class SCA_IScene; struct GameSettings; @@ -120,8 +118,6 @@ class RAS_IPolyMaterial virtual const std::string GetTextureName() const = 0; virtual Material *GetBlenderMaterial() const = 0; - virtual Image *GetBlenderImage() const = 0; - virtual MTexPoly *GetMTexPoly() const = 0; virtual Scene *GetBlenderScene() const = 0; virtual SCA_IScene *GetScene() const = 0; virtual bool UseInstancing() const = 0; diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp index 805cad7ea257..eb8077662b17 100644 --- a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp +++ b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp @@ -311,98 +311,65 @@ void RAS_OpenGLRasterizer::DrawOverlayPlane() // Code for hooking into Blender's mesh drawing for derived meshes. // If/when we use more of Blender's drawing code, we may be able to // clean this up -static bool current_wireframe; -static RAS_MaterialBucket *current_bucket; -static RAS_IPolyMaterial *current_polymat; -static RAS_MeshSlot *current_ms; -static RAS_MeshObject *current_mesh; static int current_blmat_nr; static GPUVertexAttribs current_gpu_attribs; -static Image *current_image; static int CheckMaterialDM(int matnr, void *attribs) { // only draw the current material - if (matnr != current_blmat_nr) + if (matnr != current_blmat_nr) { return 0; + } GPUVertexAttribs *gattribs = (GPUVertexAttribs *)attribs; - if (gattribs) + if (gattribs) { memcpy(gattribs, ¤t_gpu_attribs, sizeof(GPUVertexAttribs)); - return 1; -} - -static DMDrawOption CheckTexDM(MTexPoly *mtexpoly, const bool has_mcol, int matnr) -{ - - // index is the original face index, retrieve the polygon - if (matnr == current_blmat_nr && - (mtexpoly == nullptr || mtexpoly->tpage == current_image)) { - // must handle color. - if (current_wireframe) - return DM_DRAW_OPTION_NO_MCOL; - if (!has_mcol) { - // we have to set the color from the material - unsigned char rgba[4]; - current_polymat->GetRGBAColor(rgba); - glColor4ubv((const GLubyte *)rgba); - return DM_DRAW_OPTION_NORMAL; - } - return DM_DRAW_OPTION_NORMAL; } - return DM_DRAW_OPTION_SKIP; + return 1; } void RAS_OpenGLRasterizer::DrawDerivedMesh(RAS_MeshSlot *ms, RAS_Rasterizer::DrawType drawingmode) { // mesh data is in derived mesh - current_bucket = ms->m_bucket; - current_polymat = current_bucket->GetPolyMaterial(); - current_ms = ms; - current_mesh = ms->m_mesh; - current_wireframe = drawingmode <= RAS_Rasterizer::RAS_WIREFRAME; - // MCol *mcol = (MCol*)ms->m_pDerivedMesh->getFaceDataArray(ms->m_pDerivedMesh, CD_MCOL); /* UNUSED */ + RAS_MaterialBucket *bucket = ms->m_bucket; + RAS_IPolyMaterial *material = bucket->GetPolyMaterial(); // handle two-side - if (current_polymat->GetDrawingMode() & RAS_Rasterizer::RAS_BACKCULL) + if (material->GetDrawingMode() & RAS_Rasterizer::RAS_BACKCULL) { m_rasterizer->SetCullFace(true); - else + } + else { m_rasterizer->SetCullFace(false); + } - if (current_bucket->IsWire()) { + if (bucket->IsWire()) { SetLines(true); } bool wireframe = (drawingmode == RAS_Rasterizer::RAS_WIREFRAME); - if (current_polymat->GetFlag() & RAS_BLENDERGLSL) { + if (material->GetFlag() & RAS_BLENDERGLSL) { // GetMaterialIndex return the original mface material index, // increment by 1 to match what derived mesh is doing - current_blmat_nr = current_ms->m_meshMaterial->m_index + 1; + current_blmat_nr = ms->m_meshMaterial->m_index + 1; // For GLSL we need to retrieve the GPU material attribute - Material* blmat = current_polymat->GetBlenderMaterial(); - Scene* blscene = current_polymat->GetBlenderScene(); - if (!current_wireframe && blscene && blmat) - GPU_material_vertex_attributes(GPU_material_from_blender(blscene, blmat, false, current_polymat->UseInstancing()), ¤t_gpu_attribs); - else - memset(¤t_gpu_attribs, 0, sizeof(current_gpu_attribs)); - // DM draw can mess up blending mode, restore at the end - int current_blend_mode = GPU_get_material_alpha_blend(); - - if (wireframe) { - glColor4f(0.0f, 0.0f, 0.0f, 1.0f); + Material *blmat = material->GetBlenderMaterial(); + Scene *blscene = material->GetBlenderScene(); + if (wireframe && blscene && blmat) { + GPU_material_vertex_attributes(GPU_material_from_blender(blscene, blmat, false, material->UseInstancing()), ¤t_gpu_attribs); } - ms->m_pDerivedMesh->drawFacesGLSL(ms->m_pDerivedMesh, CheckMaterialDM); - GPU_set_material_alpha_blend(current_blend_mode); - } else { - //ms->m_pDerivedMesh->drawMappedFacesTex(ms->m_pDerivedMesh, CheckTexfaceDM, mcol); - current_blmat_nr = current_ms->m_meshMaterial->m_index; - current_image = current_polymat->GetBlenderImage(); - - if (wireframe) { - glColor4f(0.0f, 0.0f, 0.0f, 1.0f); + else { + memset(¤t_gpu_attribs, 0, sizeof(current_gpu_attribs)); } - ms->m_pDerivedMesh->drawFacesTex(ms->m_pDerivedMesh, CheckTexDM, nullptr, nullptr, DM_DRAW_USE_ACTIVE_UV); } - if (current_bucket->IsWire()) { + // DM draw can mess up blending mode, restore at the end + int current_blend_mode = GPU_get_material_alpha_blend(); + + if (wireframe) { + glColor4f(0.0f, 0.0f, 0.0f, 1.0f); + } + ms->m_pDerivedMesh->drawFacesGLSL(ms->m_pDerivedMesh, CheckMaterialDM); + GPU_set_material_alpha_blend(current_blend_mode); + + if (bucket->IsWire()) { SetLines(false); } }