1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "fsCNativeTexturedMeshRenderData.h"

#include <algorithm>
#include <cstdint>

#include <fsCore/fsAffineMtx2d.h>

#include <renderer/Texture2D.h>
#include <renderer/backend/Enums.h>

#include "../fsCTexture.h"
#include "../../../src/fsCBrush.h"
#include "../../../src/fsCMaterial.h"

void fsAxmolNodeVisit(ax::Node* pNode, const ax::Mat4& pRenderMatrix, std::uint32_t pFlags);


namespace
{
	fsU8 colourChannelMultiply(fsU8 pVertexChannel, fsU8 pTintChannel)
	{
		return static_cast<fsU8>(
			(static_cast<fsU16>(pVertexChannel) * pTintChannel) / 255);
	}
}

fsIRenderData* fsCNativeTexturedMeshRenderData::create()
{
	return new fsCNativeTexturedMeshRenderData();
}

void fsCNativeTexturedMeshRenderData::passToEngineRendererDo(
	fsIRenderer*, fsS32, fsS32)
{
	if (!mBrush || mIndices.empty())
	{
		return;
	}

	fsAxmolNodeVisit(mImpl.get(), mRenderMatrix, ax::Node::FLAGS_TRANSFORM_DIRTY);
}

void fsCNativeTexturedMeshRenderData::meshSetDo(const fsSPolyInfo&)
{
	nativeMeshUpdate();
}

void fsCNativeTexturedMeshRenderData::matrixApplyDo(const fsAffineMtx2d& pRenderMatrix)
{
	mRenderMatrix.m[12] = pRenderMatrix.get(0, 2);
	mRenderMatrix.m[13] = pRenderMatrix.get(1, 2);
	mRenderMatrix.m[0] = pRenderMatrix.get(0, 0);
	mRenderMatrix.m[1] = pRenderMatrix.get(1, 0);
	mRenderMatrix.m[4] = pRenderMatrix.get(0, 1);
	mRenderMatrix.m[5] = pRenderMatrix.get(1, 1);
}

void fsCNativeTexturedMeshRenderData::brushSetDo(std::shared_ptr<fsCBrush> pBrush)
{
	mBrush = pBrush;
	mImpl->setTexture(static_cast<fsCTexture*>(pBrush->textureGet().get())->textureGet());
	blendModeApply();
	nativeMeshUpdate();
}

void fsCNativeTexturedMeshRenderData::colourSetDo(const fsSColour4B& pColour)
{
	mTint = pColour;
	nativeMeshUpdate();
}

void fsCNativeTexturedMeshRenderData::alphaBlendModeSetDo(fsS32 pAlphaBlendMode)
{
	mAlphaBlendMode = pAlphaBlendMode;
	blendModeApply();
}

void fsCNativeTexturedMeshRenderData::blendModeApply()
{
	switch (mAlphaBlendMode)
	{
	case fsCMaterial::eALPHA_NONE:
		mImpl->setBlendFunc(ax::BlendFunc::DISABLE);
		break;
	case fsCMaterial::eALPHA_ADD:
		mImpl->setBlendFunc(ax::BlendFunc::ADDITIVE);
		break;
	case fsCMaterial::eALPHA_ADD_PREMULTIPLIED:
		mImpl->setBlendFunc({
			ax::backend::BlendFactor::ONE,
			ax::backend::BlendFactor::ONE});
		break;
	case fsCMaterial::eALPHA_BLEND_PREMULTIPLIED:
		mImpl->setBlendFunc(ax::BlendFunc::ALPHA_PREMULTIPLIED);
		break;
	case fsCMaterial::eALPHA_MULTIPLY:
		mImpl->setBlendFunc({
			ax::backend::BlendFactor::DST_COLOR,
			ax::backend::BlendFactor::ONE_MINUS_SRC_ALPHA});
		break;
	case fsCMaterial::eALPHA_SCREEN:
		mImpl->setBlendFunc({
			ax::backend::BlendFactor::ONE,
			ax::backend::BlendFactor::ONE_MINUS_SRC_COLOR});
		break;
	case fsCMaterial::eALPHA_SUB:
	case fsCMaterial::eALPHA_BLEND:
	default:
		if (mBrush && static_cast<fsCTexture*>(mBrush->textureGet().get())->textureGet()->hasPremultipliedAlpha())
		{
			mImpl->setBlendFunc(ax::BlendFunc::ALPHA_PREMULTIPLIED);
		}
		else
		{
			mImpl->setBlendFunc(ax::BlendFunc::ALPHA_NON_PREMULTIPLIED);
		}
		break;
	}
}

void fsCNativeTexturedMeshRenderData::nativeMeshUpdate()
{
	const fsSPolyInfo& mesh = meshGet();
	mVertices.resize(mesh.mTris.size());
	mIndices.assign(mesh.mTriangles.begin(), mesh.mTriangles.end());

	fsF32 minX = 0.0f;
	fsF32 minY = 0.0f;
	fsF32 maxX = 0.0f;
	fsF32 maxY = 0.0f;
	if (!mesh.mTris.empty())
	{
		minX = maxX = mesh.mTris.front().vertices.x;
		minY = maxY = mesh.mTris.front().vertices.y;
	}

	for (const V3F_C4B_T2F& vertex : mesh.mTris)
	{
		minX = std::min(minX, vertex.vertices.x);
		minY = std::min(minY, vertex.vertices.y);
		maxX = std::max(maxX, vertex.vertices.x);
		maxY = std::max(maxY, vertex.vertices.y);
	}

	for (std::size_t index = 0; index < mesh.mTris.size(); ++index)
	{
		const V3F_C4B_T2F& source = mesh.mTris[index];
		fsSAxmolTexturedMeshVertex& destination = mVertices[index];
		destination.vertices.x = source.vertices.x - minX;
		destination.vertices.y = source.vertices.y - minY;
		destination.vertices.z = source.vertices.z;
		destination.texCoords.u = source.texCoords.mU;
		destination.texCoords.v = source.texCoords.mV;
		destination.colors.r = colourChannelMultiply(source.colors.mR, mTint.mR);
		destination.colors.g = colourChannelMultiply(source.colors.mG, mTint.mG);
		destination.colors.b = colourChannelMultiply(source.colors.mB, mTint.mB);
		destination.colors.a = colourChannelMultiply(source.colors.mA, mTint.mA);
	}

	mImpl->setAnchorPoint(ax::Vec2(0.0f, 0.0f));
	mImpl->setPosition(minX, minY);
	mImpl->setContentSize(ax::Size(maxX - minX, maxY - minY));

	mTriangles.verts = mVertices.empty() ? nullptr : mVertices.data();
	mTriangles.indices = mIndices.empty() ? nullptr : mIndices.data();
	mTriangles.vertCount = static_cast<unsigned int>(mVertices.size());
	mTriangles.indexCount = static_cast<unsigned int>(mIndices.size());
	mPolygonInfo.setTriangles(mTriangles);
	mImpl->setPolygonInfo(mPolygonInfo);
}

fsCNativeTexturedMeshRenderData::fsCNativeTexturedMeshRenderData() :
	mImpl(ax::Sprite::create()),
	mTint(fsSColour4B::fsWHITE()),
	mAlphaBlendMode(fsCMaterial::eALPHA_BLEND)
{
	mImpl->retain();
}

fsCNativeTexturedMeshRenderData::~fsCNativeTexturedMeshRenderData()
{
	if (mImpl->getParent())
	{
		mImpl->removeFromParentAndCleanup(true);
	}
	mImpl->release();
	mImpl.release();<--- Return value of function mImpl.release() is not used.
}