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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

#include "fsCVertexRenderer-v3.h"

#if defined fsVertexRenderer

#include <base/CCDirector.h>
#include <2d/CCLayer.h>
#include <2d/CCCamera.h>
#include <renderer/CCRenderer.h>
#include <renderer/ccGLStateCache.h>

#include <fsCore/src/fsNRnd.h>
#include <fsCore/debug/fsAssert.h>

#include <fs3d/fsC3dSprite.h>
#include <fs3d/fsC3dCamera.h>
#include <fs3d/fsC3dSceneSprite.h>

#include "../../../renderer/fsISpriteRenderData.h"
#include "../../../renderer/fsILineRenderData.h"
#include "../../../renderer/fsITextEntryRenderData.h"
#include "../../../renderer/fsCMaterial.h"

#if defined fsPLATFORM_CX_V3
	#include "../vertexStream/fsCVertexStreamTextSpriteRenderData-v3.h"
#else
	#include "../vertexStream/fsCVertexStreamTextSpriteRenderData.h"
#endif

#include "../fsCTexture.h"
#include "../fsCFont.h"


#if defined fs3d

void fsCRenderer::sprite3dBatchRender(fsC3dSceneSprite* const p3dScene)
{
	prepareFor3d();
	p3dScene->renderScene();
}

#endif

void fsCRenderer::frameStartDo()
{
}

void fsCRenderer::begin2dDo()
{
	mSpriteIndex = 0;
	mTxtSprIndex = 0;
	mLineBatch = 0;
}

void fsCRenderer::blendModeSet(fsS32 pBlendMode)<--- The member function 'fsCRenderer::blendModeSet' can be static.
{
	switch (pBlendMode)
	{
		case fsCMaterial::eALPHA_NONE:
			cocos2d::GL::blendFunc(cocos2d::BlendFunc::DISABLE.src, cocos2d::BlendFunc::DISABLE.dst);
			break;

		case fsCMaterial::eALPHA_ADD:
			cocos2d::GL::blendFunc(cocos2d::BlendFunc::ADDITIVE.src, cocos2d::BlendFunc::ADDITIVE.dst);
			break;
		
		case fsCMaterial::eALPHA_SUB:
			cocos2d::GL::blendFunc(cocos2d::BlendFunc::DISABLE.src, cocos2d::BlendFunc::DISABLE.dst);
			break;

		case fsCMaterial::eALPHA_BLEND:
			//If pre multiplied textures are used then....
			//cocos2d::GL::blendFunc(cocos2d::BlendFunc::ALPHA_PREMULTIPLIED.src, cocos2d::BlendFunc::ALPHA_PREMULTIPLIED.dst);
			cocos2d::GL::blendFunc(cocos2d::BlendFunc::ALPHA_NON_PREMULTIPLIED.src, cocos2d::BlendFunc::ALPHA_NON_PREMULTIPLIED.dst);
			break;	
	}
}

void fsCRenderer::spriteBatchRender(fsS32 pFirstMaterialIndex, fsS32 pLastMaterialIndex)
{
    blendModeSet(fsCMaterial::eALPHA_BLEND);
	for (fsS32 material = pFirstMaterialIndex; material <= pLastMaterialIndex; ++material)
	{
		spriteBatchDataAllocateFromCache(numVertsGet(material));
		spriteBatchDataPopulate(material);
		spriteBatchDataRender(material);
	}
}

void fsCRenderer::spriteBatchDataAllocateFromCache(fsS32 pNumVerts)
{
	mRenderBuffers.mBatchedVerts.init((pNumVerts+2) * 2);
	mRenderBuffers.mBatchedUVs.init(pNumVerts * 2);
	mRenderBuffers.mBatchedColours.init(pNumVerts * 4);
}

void fsCRenderer::spriteBatchDataPopulate(fsS32 pMaterial)
{
	fsRenderBuffersPtr buf(mRenderBuffers);
	eSPRITE_STRIP_PLACE pos = eFIRST;
	for (fsS32 sprite = 0 ; sprite < mSpritesPerMaterial[ pMaterial ] ; ++sprite)
 	{
 		const fsISpriteRenderData* const sprRenData = mSpriteRenderData[mSpriteIndex++];
		sprRenData->batchDataPopulate(&buf, pos);
		(sprite + 1 == mSpritesPerMaterial[pMaterial]) ? pos = eLAST : pos = eSTRIPPED;
	}
}

void fsCRenderer::spriteBatchDataRender(fsS32 pMaterial)
{
	if (const fsCTexture * const texPtr = static_cast<const fsCTexture*>(mMaterials[pMaterial]->texturePtrGet()))
	{
		cocos2d::Texture2D* const texImpl = texPtr->textureGet();

		cocos2d::GLProgramState* const glState = cocos2d::GLProgramState::getOrCreateWithGLProgramName(
			cocos2d::GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, texImpl);

		glState->getGLProgram()->use();
		glState->getGLProgram()->setUniformsForBuiltins();

		cocos2d::GL::bindTexture2D(texImpl->getName());
		blendModeSet(mMaterials[pMaterial]->alphaBlendGet());

        glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION);
		glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR);
		glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_TEX_COORD);

		glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, mRenderBuffers.mBatchedVerts.mBuffer);
		glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, mRenderBuffers.mBatchedColours.mBuffer);
		glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, mRenderBuffers.mBatchedUVs.mBuffer);

		glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertsGet(pMaterial));
	}
	else
	{
		cocos2d::GLProgramState* const glState = cocos2d::GLProgramState::getOrCreateWithGLProgramName(
			cocos2d::GLProgram::SHADER_NAME_POSITION_COLOR);


        glState->getGLProgram()->use();
        glState->getGLProgram()->setUniformsForBuiltins();
		blendModeSet(mMaterials[pMaterial]->alphaBlendGet());

        glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION);
		glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR);

        glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, mRenderBuffers.mBatchedVerts.mBuffer);
		glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, mRenderBuffers.mBatchedColours.mBuffer);

		glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertsGet(pMaterial));
	}
}

fsS32 fsCRenderer::numVertsGet(fsS32 pMaterial) const
{
	return mSpritesPerMaterial[pMaterial] * 4 + (mSpritesPerMaterial[pMaterial] - 0) * 2; // quad => 4 verts plus 2 for degenerate join ups
}

void fsCRenderer::textBatchRender(fsS32 pFirstFontIndex, fsS32 pLastFontIndex)
{
	blendModeSet(fsCMaterial::eALPHA_BLEND);
	cocos2d::GLProgramState* glState = cocos2d::GLProgramState::getOrCreateWithGLProgramName(cocos2d::GLProgram::SHADER_NAME_LABEL_NORMAL);
    
    glState->getGLProgram()->use();
    glState->getGLProgram()->setUniformsForBuiltins();

    glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION);
    glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR);
    glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_TEX_COORD);
    
    GLuint _uniformTextColor = glGetUniformLocation(glState->getGLProgram()->getProgram(), "u_textColor");

    for (fsS32 font = pFirstFontIndex; font <= pLastFontIndex; ++font)
	{
		for (fsS32 txtStr = 0; txtStr < mTextStringsPerFont[font]; ++ txtStr)
		{
            glState->getGLProgram()->setUniformLocationWith4f(_uniformTextColor, 1, 1, 1, 1);
			mTextRenderData[mTxtSprIndex]->implRender();
			++mTxtSprIndex;
		}
	}
}

void fsCRenderer::textEntryBatchRender(fsITextEntryRenderData* const pTextEntry)
{
//    pTextEntry->render();
}

void fsCRenderer::lineBatchRender(fsS32 pBatchIndex)
{
	lineBatchDataAllocateFromCache(pBatchIndex);
	lineBatchDataPopulate(pBatchIndex);
	lineBatchDataRender(pBatchIndex);
}

void fsCRenderer::lineBatchDataAllocateFromCache(fsS32 pBatchIndex)
{
	const fsS32 numVerts = mLinesPerBatch[pBatchIndex];
	mRenderBuffers.mBatchedVerts.init(numVerts * 2);
	mRenderBuffers.mBatchedColours.init(numVerts * 4);
}

void fsCRenderer::lineBatchDataPopulate(fsS32 pBatchIndex)
{
	fsRenderBuffersPtr buf(mRenderBuffers);
	const fsILineRenderData* const lineRenderData = mLineRenderData[mLineBatch++];
	lineRenderData->batchDataPopulate(&buf, eFIRST);
}

void fsCRenderer::lineBatchDataRender(fsS32 pBatchIndex)
{
	cocos2d::GLProgramState* glState = cocos2d::GLProgramState::getOrCreateWithGLProgramName(
		cocos2d::GLProgram::SHADER_NAME_POSITION_COLOR);

	glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION);
	glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR);
	glState->getGLProgram()->use();
	glState->getGLProgram()->setUniformsForBuiltins();

	glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, mRenderBuffers.mBatchedVerts.mBuffer);
	glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, mRenderBuffers.mBatchedColours.mBuffer);

	glDrawArrays(GL_LINES, 0, mLinesPerBatch[pBatchIndex]);
}

void fsCRenderer::end2dDo()
{
	cocos2d::Director::getInstance()->getRenderer()->render();
}

void fsCRenderer::begin3dDo()
{
}

void fsCRenderer::end3dDo()
{
	cocos2d::Director::getInstance()->getRenderer()->render();
}

void fsCRenderer::frameEndDo()
{
}

void fsCRenderer::initialiseDo()
{
}

void fsCRenderer::uninitialiseDo()
{
}

fsCRenderer::fsCRenderer( fsS32 pMaxSprites, fsS32 pMaxMaterialChanges ) :
fsIRenderer( pMaxSprites, pMaxMaterialChanges ),
mSpriteIndex( 0 ),
mTxtSprIndex(0),
mLineBatch(0),
mZoom(1.0f),
mTrackingScale(1.0f)
{
}

fsCRenderer::~fsCRenderer()
{
}

fsCRenderer::fsSF32VertBuffer::fsSF32VertBuffer() :
mSize(0),
mBuffer(nullptr) {}

fsCRenderer::fsSF32VertBuffer::~fsSF32VertBuffer()
{
	if (mSize)
	{
		delete mBuffer;<--- Struct 'fsSF32VertBuffer' does not have a copy constructor which is recommended since it has dynamic memory/resource management.<--- Struct 'fsSF32VertBuffer' does not have a operator= which is recommended since it has dynamic memory/resource management.
	}
}

void fsCRenderer::fsSF32VertBuffer::init(fsS32 pSize)
{
	if (pSize > mSize)
	{
		delete[]mBuffer;
		mBuffer = new fsF32[pSize];
		mSize = pSize;
	}
}

#endif