Skip to content

Commit

Permalink
UPBGE: Cleanup mesh layers info.
Browse files Browse the repository at this point in the history
Previously the layers were all sotred in the same list, so any code
reading them needed to check the type of the layer. In some code it
can be inconvenient.
A better system is to store uv and color layers info in separated
listes. In consideration the type doesn't have any reason to stay.
  • Loading branch information
panzergame committed Dec 6, 2017
1 parent 404070c commit 39ae7f9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 67 deletions.
82 changes: 33 additions & 49 deletions source/gameengine/Converter/BL_BlenderDataConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,47 +342,41 @@ SCA_IInputDevice::SCA_EnumInputs BL_ConvertKeyCode(int key_code)
return gReverseKeyTranslateTable[key_code];
}

static void BL_GetUvRgba(const RAS_MeshObject::LayerList& layers, std::vector<MLoopUV *>& uvLayers,
std::vector<MLoopCol *>& colorLayers, unsigned short uvCount, unsigned short colorCount,
unsigned int loop, float uvs[RAS_Texture::MaxUnits][2], unsigned int rgba[RAS_Vertex::MAX_UNIT])
static void BL_GetUvRgba(const RAS_MeshObject::LayersInfo& layersInfo, std::vector<MLoopUV *>& uvLayers,
std::vector<MLoopCol *>& colorLayers, unsigned int loop, float uvs[RAS_Texture::MaxUnits][2],
unsigned int rgba[RAS_Vertex::MAX_UNIT])
{
// No need to initialize layers to zero as all the converted layer are all the layers needed.

for (const RAS_MeshObject::Layer& layer : layers) {
for (const RAS_MeshObject::Layer& layer : layersInfo.colorLayers) {
const unsigned short index = layer.index;
switch (layer.type) {
case RAS_MeshObject::Layer::COLOR:
{
const MLoopCol& col = colorLayers[index][loop];
const MLoopCol& col = colorLayers[index][loop];

union Convert{
// Color isn't swapped in MLoopCol.
MLoopCol col;
unsigned int val;
};
Convert con;
con.col = col;
union Convert{
// Color isn't swapped in MLoopCol.
MLoopCol col;
unsigned int val;
};
Convert con;
con.col = col;

rgba[index] = con.val;
break;
}
case RAS_MeshObject::Layer::UV:
{
const MLoopUV& uv = uvLayers[index][loop];
copy_v2_v2(uvs[index], uv.uv);
break;
}
}
rgba[index] = con.val;
}

for (const RAS_MeshObject::Layer& layer : layersInfo.uvLayers) {
const unsigned short index = layer.index;
const MLoopUV& uv = uvLayers[index][loop];
copy_v2_v2(uvs[index], uv.uv);
}

/* All vertices have at least one uv and color layer accessible to the user
* even if it they are not used in any shaders. Initialize this layer to zero
* when no uv or color layer exist.
*/
if (uvCount == 0) {
if (layersInfo.uvLayers.empty()) {
zero_v2((uvs[0]));
}
if (colorCount == 0) {
if (layersInfo.colorLayers.empty()) {
rgba[0] = 0xFFFFFFFF;
}
}
Expand Down Expand Up @@ -441,18 +435,16 @@ RAS_MeshObject *BL_ConvertMesh(Mesh *me, Object *blenderobj, KX_Scene *scene, BL
RAS_MeshObject::LayersInfo layersInfo;
layersInfo.activeUv = (activeUv == -1) ? 0 : activeUv;
layersInfo.activeColor = (activeColor == -1) ? 0 : activeColor;
layersInfo.uvCount = uvCount;
layersInfo.colorCount = colorCount;

// Extract UV loops.
for (unsigned short i = 0; i < uvCount; ++i) {
const std::string name = CustomData_get_layer_name(&dm->loopData, CD_MLOOPUV, i);
layersInfo.layers.push_back({RAS_MeshObject::Layer::UV, i, name});
layersInfo.uvLayers.push_back({i, name});
}
// Extract color loops.
for (unsigned short i = 0; i < colorCount; ++i) {
const std::string name = CustomData_get_layer_name(&dm->loopData, CD_MLOOPCOL, i);
layersInfo.layers.push_back({RAS_MeshObject::Layer::COLOR, i, name});
layersInfo.colorLayers.push_back({i, name});
}

// Initialize vertex format with used uv and color layers.
Expand Down Expand Up @@ -512,34 +504,26 @@ void BL_ConvertDerivedMeshToArray(DerivedMesh *dm, Mesh *me, const std::vector<B
const float (*normals)[3] = (float(*)[3])dm->getLoopDataArray(dm, CD_NORMAL);

float (*tangent)[4] = nullptr;
if (layersInfo.uvCount > 0) {
if (!layersInfo.uvLayers.empty()) {
if (CustomData_get_layer_index(&dm->loopData, CD_TANGENT) == -1) {
DM_calc_loop_tangents(dm, true, nullptr, 0);
}
tangent = (float(*)[4])dm->getLoopDataArray(dm, CD_TANGENT);
}

// List of MLoopUV per uv layer index.
std::vector<MLoopUV *> uvLayers(layersInfo.uvCount);
std::vector<MLoopUV *> uvLayers(layersInfo.uvLayers.size());
// List of MLoopCol per color layer index.
std::vector<MLoopCol *> colorLayers(layersInfo.colorCount);
std::vector<MLoopCol *> colorLayers(layersInfo.uvLayers.size());

for (const RAS_MeshObject::Layer& layer : layersInfo.layers) {
for (const RAS_MeshObject::Layer& layer : layersInfo.uvLayers) {
const unsigned short index = layer.index;
switch (layer.type) {
case RAS_MeshObject::Layer::UV:
{
uvLayers[index] = (MLoopUV *)CustomData_get_layer_n(&dm->loopData, CD_MLOOPUV, index);
break;
}
case RAS_MeshObject::Layer::COLOR:
{
colorLayers[index] = (MLoopCol *)CustomData_get_layer_n(&dm->loopData, CD_MLOOPCOL, index);
break;
}
}
uvLayers[index] = (MLoopUV *)CustomData_get_layer_n(&dm->loopData, CD_MLOOPUV, index);
}
for (const RAS_MeshObject::Layer& layer : layersInfo.colorLayers) {
const unsigned short index = layer.index;
colorLayers[index] = (MLoopCol *)CustomData_get_layer_n(&dm->loopData, CD_MLOOPCOL, index);
}


BL_SharedVertexMap sharedMap(totverts);

Expand Down Expand Up @@ -568,7 +552,7 @@ void BL_ConvertDerivedMeshToArray(DerivedMesh *dm, Mesh *me, const std::vector<B
float uvs[RAS_Texture::MaxUnits][2];
unsigned int rgba[RAS_Texture::MaxUnits];

BL_GetUvRgba(layersInfo.layers, uvLayers, colorLayers, layersInfo.uvCount, layersInfo.colorCount, j, uvs, rgba);
BL_GetUvRgba(layersInfo, uvLayers, colorLayers, j, uvs, rgba);

RAS_Vertex vertex = array->CreateVertex(mvert.co, uvs, tan, rgba, normals[j]);

Expand Down
12 changes: 8 additions & 4 deletions source/gameengine/Ketsji/BL_BlenderShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ const RAS_AttributeArray::AttribList BL_BlenderShader::GetAttribs(const RAS_Mesh
continue;
}

for (const RAS_MeshObject::Layer& layer : layersInfo.layers) {
if (layer.name == attribname) {
if (type == CD_MTFACE && layer.type == RAS_MeshObject::Layer::UV) {
if (type == CD_MTFACE) {
for (const RAS_MeshObject::Layer& layer : layersInfo.uvLayers) {
if (layer.name == attribname) {
attribs.push_back({glindex, RAS_AttributeArray::RAS_ATTRIB_UV, false, layer.index});
break;
}
else if (type == CD_MCOL && layer.type == RAS_MeshObject::Layer::COLOR) {
}
}
else {
for (const RAS_MeshObject::Layer& layer : layersInfo.colorLayers) {
if (layer.name == attribname) {
attribs.push_back({glindex, RAS_AttributeArray::RAS_ATTRIB_COLOR, false, layer.index});
break;
}
Expand Down
4 changes: 2 additions & 2 deletions source/gameengine/Ketsji/BL_Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ RAS_AttributeArray::AttribList BL_Shader::GetAttribs(const RAS_MeshObject::Layer
}

// Search for the UV layer index used by the texture.
for (const RAS_MeshObject::Layer& layer : layersInfo.layers) {
if (layer.type == RAS_MeshObject::Layer::UV && layer.name == mtex->uvname) {
for (const RAS_MeshObject::Layer& layer : layersInfo.uvLayers) {
if (layer.name == mtex->uvname) {
attribs.push_back({i, RAS_AttributeArray::RAS_ATTRIB_UV, true, layer.index});
break;
}
Expand Down
18 changes: 6 additions & 12 deletions source/gameengine/Rasterizer/RAS_MeshObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ class RAS_MeshObject
* attribute's names in shader and names of the mesh layers here.
*/
struct Layer {
/// The type of the layer, uv or color.
enum Type {
UV,
COLOR
} type;
/// The index of the color or uv layer in the vertices.
unsigned short index;
/// The name of the color or uv layer used to find corresponding material attributes.
Expand All @@ -76,15 +71,14 @@ class RAS_MeshObject
typedef std::vector<Layer> LayerList;

struct LayersInfo {
LayerList layers;
// The active color layer index as default.
/// UV layers info.
LayerList uvLayers;
/// Color layers info.
LayerList colorLayers;
/// The active color layer index as default.
unsigned short activeColor;
// The active uv layer index as default.
/// The active uv layer index as default.
unsigned short activeUv;
// The number of uv layers.
unsigned short uvCount;
// The number of color layers.
unsigned short colorCount;
};

/** Polygon info generate when getting a polygon through
Expand Down

0 comments on commit 39ae7f9

Please sign in to comment.