I am using cocos2d-x v4. I can not tell what I have done wrong but I get that weird triangle shape when trying to use TRIANGLE_STRIP.
#include "GameLayer.h"
bool GameLayer::init()
{
if (!Scene::init())
{
return false;
}
visibleSize = Director::getInstance()->getWinSize(); // w = 512, h = 512
Vec2 origin = Director::getInstance()->getVisibleOrigin();
this->onDrawStuffs();
return true;
}
void GameLayer::onDrawStuffs()
{
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec2 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"out vec3 ourColor;\n"
"uniform mat4 u_MVPMatrix;\n"
"void main()\n"
"{\n"
" gl_Position = u_MVPMatrix * vec4(aPos, 0.0, 1.0);\n"
" ourColor = aColor;\n"
"}\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec3 ourColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
"}\0";
auto program = cocos2d::backend::Device::getInstance()->newProgram(vertexShaderSource, fragmentShaderSource);
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
_programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
auto& vertexLayout = pipelineDescriptor.programState->getVertexLayout();
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
auto iter = attributeInfo.find("aPos");
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute("aPos",
iter->second.location,
backend::VertexFormat::FLOAT2,
0,
false);
}
vertexLayout->setLayout(2 * sizeof(float));
_mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
float vertices[] = {
10, 50,
50, 150,
250, 250
};
m_vertexDataCount = sizeof(vertices) / 2 * sizeof(float);
_customCommand.createVertexBuffer(sizeof(vertices), m_vertexDataCount, CustomCommand::BufferUsage::DYNAMIC);
_customCommand.updateVertexBuffer(vertices, sizeof(vertices));
const auto& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
_customCommand.getPipelineDescriptor().programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
}
void GameLayer::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
{
_customCommand.init(0);
_customCommand.setVertexDrawInfo(0, m_vertexDataCount);
renderer->addCommand(&_customCommand);
}