Is it possible to use several different textures in one CustomCommand? Below is a piece of my code for illustration purposes. I’m using cocos2d-x v4.0. With one texture I’m able to draw a 2d terrain nicely but is it possible somehow to perform the drawing so that different triangles have different texture in drawing?
Or do I just have to create a separate CustomCommand for each texture and associated set of vertices?
void MyClass::init()
{
auto& pipelineDescriptor = m_customCommand.getPipelineDescriptor();
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
_programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
setVertexLayout(m_customCommand);
m_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
m_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
m_customCommand.createVertexBuffer(sizeof(V2F_C4B_T2F), m_vertexDataCount, CustomCommand::BufferUsage::DYNAMIC);
m_mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation(“u_MVPMatrix”);
m_textureLocation = pipelineDescriptor.programState->getUniformLocation(“u_texture”);
}
void MyClass::setVertexLayout(CustomCommand& cmd)
{
auto* programState = cmd.getPipelineDescriptor().programState;
auto vertexLayout = programState->getVertexLayout();
const auto& attributeInfo = programState->getProgram()->getActiveAttributes();
auto iter = attributeInfo.find(“a_position”);
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute(“a_position”, iter->second.location, backend::VertexFormat::FLOAT2, 0, false);
}
iter = attributeInfo.find(“a_texCoord”);
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute(“a_texCoord”, iter->second.location, backend::VertexFormat::FLOAT2, 2 * sizeof(float), false);
}
iter = attributeInfo.find(“a_color”);
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute(“a_color”, iter->second.location, backend::VertexFormat::UBYTE4, 4 * sizeof(float), true);
}
vertexLayout->setLayout(4 * sizeof(float) + 4 * sizeof(uint8_t));
}
void MyClass::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
m_customCommand.init(_globalZOrder);
m_customCommand.setVertexDrawInfo(0, m_vertexDataCount);
renderer->addCommand(&m_customCommand);
auto programState = m_customCommand.getPipelineDescriptor().programState;
programState->setTexture(m_textureLocation, 0, getTexture()->getBackendTexture());
const auto& projectionMat =
Director::getInstance()>getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
Mat4 finalMat = projectionMat * transform;
programState->setUniform(m_mvpMatrixLocation, finalMat.m, sizeof(Mat4));
int offset = 0;
for (unsigned int i = 0; i < m_vertexDataCount; ++i)
{
m_customCommand.updateVertexBuffer(&m_vertexData[i].vertices, offset, sizeof(m_vertexData[0].vertices));
m_customCommand.updateVertexBuffer(&m_vertexData[i].texCoords, offset + sizeof(m_vertexData[0].vertices), sizeof(m_vertexData[0].texCoords));
m_customCommand.updateVertexBuffer(&m_vertexData[i].colors, offset + sizeof(m_vertexData[0].vertices) + sizeof(m_vertexData[0].texCoords), 4 * sizeof(uint8_t));
offset += vertexSize;
}
}