Skip to content

Commit

Permalink
UPBGE: Use float array for vertex conversion.
Browse files Browse the repository at this point in the history
Previously the vertex was using float array converted to Moto
type and converted back to float array. To avoid these two
conversion stages the function RAS_IDisplayArray::CreateVertex now
accept float arrays.
The previous CreateVertex function is still keep for future mesh builder.
  • Loading branch information
panzergame committed Nov 11, 2017
1 parent 7ba5931 commit f874e52
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
20 changes: 10 additions & 10 deletions source/gameengine/Converter/BL_BlenderDataConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ 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, MT_Vector2 uvs[RAS_Texture::MaxUnits], unsigned int rgba[RAS_Vertex::MAX_UNIT])
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])
{
// No need to initialize layers to zero as all the converted layer are all the layers needed.

Expand All @@ -371,7 +371,7 @@ static void BL_GetUvRgba(const RAS_MeshObject::LayerList& layers, std::vector<ML
case RAS_MeshObject::Layer::UV:
{
const MLoopUV& uv = uvLayers[index][loop];
uvs[index].setValue(uv.uv);
copy_v2_v2(uvs[index], uv.uv);
break;
}
}
Expand All @@ -382,7 +382,7 @@ static void BL_GetUvRgba(const RAS_MeshObject::LayerList& layers, std::vector<ML
* when no uv or color layer exist.
*/
if (uvCount == 0) {
uvs[0] = MT_Vector2(0.0f, 0.0f);
zero_v2((uvs[0]));
}
if (colorCount == 0) {
rgba[0] = 0xFFFFFFFF;
Expand Down Expand Up @@ -564,15 +564,15 @@ void BL_ConvertDerivedMeshToArray(DerivedMesh *dm, Mesh *me, const std::vector<B
const unsigned int vertid = mloop.v;
const MVert& mvert = mverts[vertid];

const MT_Vector3 pt(mvert.co);
const MT_Vector3 no(normals[j]);
const MT_Vector4 tan = tangent ? MT_Vector4(tangent[j]) : MT_Vector4(0.0f, 0.0f, 0.0f, 0.0f);
MT_Vector2 uvs[RAS_Texture::MaxUnits];
static const float dummyTangent[4] = {0.0f, 0.0f, 0.0f, 0.0f};
const float (&tan)[4] = tangent ? tangent[j] : dummyTangent;

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);

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

BL_SharedVertexList& sharedList = sharedMap[vertid];
BL_SharedVertexList::iterator it = std::find_if(sharedList.begin(), sharedList.end(),
Expand Down
11 changes: 11 additions & 0 deletions source/gameengine/Rasterizer/RAS_DisplayArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ friend class RAS_BatchDisplayArray<VertexData>;
return RAS_Vertex(data, m_format);
}

virtual RAS_Vertex CreateVertex(
const float xyz[3],
const float (*uvs)[2],
const float tangent[4],
const unsigned int *rgba,
const float normal[3])
{
VertexData *data = new VertexData(xyz, uvs, tangent, rgba, normal);
return RAS_Vertex(data, m_format);
}

virtual void UpdateCache()
{
const unsigned int size = GetVertexCount();
Expand Down
7 changes: 7 additions & 0 deletions source/gameengine/Rasterizer/RAS_IDisplayArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ class RAS_IDisplayArray
const unsigned int *rgba,
const MT_Vector3& normal) = 0;

virtual RAS_Vertex CreateVertex(
const float xyz[3],
const float (*uvs)[2],
const float tangent[4],
const unsigned int *rgba,
const float normal[3]) = 0;

/** Copy vertex data from an other display array. Different vertex type is allowed.
* \param other The other display array to copy from.
* \param flag The flag coresponding to datas to copy.
Expand Down
34 changes: 34 additions & 0 deletions source/gameengine/Rasterizer/RAS_VertexData.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "MT_Vector4.h"

#include "BLI_math_vector.h"

struct RAS_VertexDataBasic
{
float position[3];
Expand All @@ -17,6 +19,13 @@ struct RAS_VertexDataBasic
_normal.getValue(normal);
_tangent.getValue(tangent);
}

inline RAS_VertexDataBasic(const float _position[3], const float _normal[3], const float _tangent[4])
{
copy_v3_v3(position, _position);
copy_v3_v3(normal, _normal);
copy_v4_v4(tangent, _tangent);
}
};

template <unsigned short uvSize, unsigned short colorSize>
Expand All @@ -38,6 +47,16 @@ struct RAS_VertexDataExtra
}
}

inline RAS_VertexDataExtra(const float _uvs[uvSize][2], const unsigned int _colors[colorSize])
{
for (unsigned short i = 0; i < uvSize; ++i) {
copy_v2_v2(uvs[i], _uvs[i]);
}

for (unsigned short i = 0; i < colorSize; ++i) {
colors[i] = _colors[i];
}
}
};

struct RAS_IVertexData : RAS_VertexDataBasic
Expand All @@ -48,6 +67,11 @@ struct RAS_IVertexData : RAS_VertexDataBasic
:RAS_VertexDataBasic(position, normal, tangent)
{
}

inline RAS_IVertexData(const float position[3], const float normal[3], const float tangent[4])
:RAS_VertexDataBasic(position, normal, tangent)
{
}
};

template <unsigned short uvSize, unsigned short colorSize>
Expand All @@ -69,6 +93,16 @@ struct RAS_VertexData : RAS_IVertexData, RAS_VertexDataExtra<uvSize, colorSize>
RAS_VertexDataExtra<uvSize, colorSize>(uvs, rgba)
{
}

inline RAS_VertexData(const float xyz[3],
const float uvs[uvSize][2],
const float tangent[4],
const unsigned int rgba[colorSize],
const float normal[3])
:RAS_IVertexData(xyz, normal, tangent),
RAS_VertexDataExtra<uvSize, colorSize>(uvs, rgba)
{
}
};

#endif // __RAS_VERTEX_DATA_H__

0 comments on commit f874e52

Please sign in to comment.