Using this [CustomCommand · GitBook], how do I draw that triangle in this [https://learnopengl.com/Getting-started/Hello-Triangle].
I haven’t done it in v4, but IIRC in v3 you could obtain ProgramState
, use CustomCommand
and just use normal openGL syntax. In a few of my v3 games, I override onDraw
Maybe this also helps: Can no longer override draw function?
bool HelloWorld::init()
{
if (!Scene::init())
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
auto origin = Director::getInstance()->getVisibleOrigin();
const char* positionColor_vert = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char* positionColor_frag = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\0";
// vertex and fragment shader
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
program = cocos2d::backend::Device::getInstance()->newProgram(positionColor_vert,positionColor_frag);
_programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
float _vertexData[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
_customCommand.createVertexBuffer(sizeof(_vertexData[0]), //vertex size
9, //vertex count
CustomCommand::BufferUsage::DYNAMIC);
_customCommand.updateVertexBuffer(_vertexData, sizeof(_vertexData));
_customCommand.updateVertexBuffer(_vertexData, 3, 9);
return true;
}
// overriden from Node
void HelloWorld::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
{
_customCommand.init(_globalZOrder+10);
std::function<void()> f_name = [this]() {
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
};
// function call back
_customCommand.setBeforeCallback(f_name);
renderer->addCommand(&_customCommand);
}
I do not know if I am doing it right. I don’t see the triangle that I want. Please try it out. I am using cocos2dx v4.
The implementation of DrawNode
in CCDrawNode.cpp
may help, so take a look at that code.
You’re right, their i have seen the implementation, it will be helpful. Thanks