Skip to content

Commit

Permalink
UPBGE: Simplify derived mesh draw.
Browse files Browse the repository at this point in the history
The derived mesh draw was simplified by always use GLSL render, this
modification allowed to remove the material function GetBlenderImage
and GetMTexPoly.
  • Loading branch information
panzergame committed May 22, 2017
1 parent 9c1d09d commit 034494a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 109 deletions.
9 changes: 4 additions & 5 deletions source/gameengine/Converter/BL_BlenderDataConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}

Expand Down
21 changes: 0 additions & 21 deletions source/gameengine/Ketsji/KX_BlenderMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -126,21 +118,13 @@ 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
OnExit();
}
}

MTexPoly *KX_BlenderMaterial::GetMTexPoly() const
{
// fonts on polys
return m_mtexPoly;
}

void KX_BlenderMaterial::GetRGBAColor(unsigned char *rgba) const
{
if (m_material) {
Expand All @@ -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();
Expand Down
5 changes: 0 additions & 5 deletions source/gameengine/Ketsji/KX_BlenderMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 0 additions & 10 deletions source/gameengine/Ketsji/KX_TextMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions source/gameengine/Ketsji/KX_TextMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions source/gameengine/Rasterizer/RAS_IPolygonMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@

#include <map>

struct MTexPoly;
struct Material;
struct Image;
struct Scene;
class SCA_IScene;
struct GameSettings;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, &current_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()), &current_gpu_attribs);
else
memset(&current_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()), &current_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(&current_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);
}
}
Expand Down

0 comments on commit 034494a

Please sign in to comment.