Hello there,
I try to implement custom drawing and used a PrimitiveCommand. Unfortunatley I have some problems moving them around on my screen with for exmple SetPosition. I’m new to OpgenGl and cocos2dx and maybe I missunderstand some parts. I try to dump it down so that I can explain it more easily
- I derived a new class from a Sprite.
- Init this new class with a texture
- AddChild to my Scene
- Setup my data of
V3F_C4B_T2F
structs (texcoord, color and vertices) - Setup VertexBuffer
m_pVertexData = VertexData::create();
m_pVertexData->setStream(m_pVertexBuffer, VertexStreamAttribute(0 , GLProgram::VERTEX_ATTRIB_POSITION , GL_FLOAT , 3, false ));
m_pVertexData->setStream(m_pVertexBuffer, VertexStreamAttribute(12, GLProgram::VERTEX_ATTRIB_COLOR , GL_UNSIGNED_BYTE , 4, true ));
m_pVertexData->setStream(m_pVertexBuffer, VertexStreamAttribute(16, GLProgram::VERTEX_ATTRIB_TEX_COORD , GL_FLOAT , 2, false ));
- Setup IndexBuffer
m_pIndexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, m_vecIndex.size());
- Setup Primitive
m_pPrimitve = Primitive::create(m_pVertexData, m_pIndexBuffer, GL_TRIANGLES);
- Overload draw function and setup PrimitiveCommand
m_oPrimitiveCommand.init( _globalZOrder, getTexture()->getName(), getGLProgramState(), _blendFunc, m_pPrimitve, transform, flags );
renderer->addCommand(&m_oPrimitiveCommand);
- Be happy everything looks fine.
Problem: Where do I update the position of my Sprite? If I set to my custom sprite class SetPositionX( 100 ) the transform Matrix (parameter of overloaded Draw function) will update accordingly but my Sprite on the screen won’t move. Isn’t the _mv
matrix inside the PrimitiveCommand responsible for that? If I take a look into the PrimitiveCommand::execute()
function the _mv
is correct and is also set. If I call Sprite::Draw(...)
in my overloaded function and ignore my own implementation, the sprite gets rendered to the correct position via the default TriangleCommand from CCSprite. I’m not sure waht to do now.
Of course I could update the vertices directly each frame by calling GetPosition and move the around like that but I think that isn’t the way to go. Are there some matrices I need to update or apply manually? Is PrimitiveCommand not the best way to do that?
Nisi
PS: cocos2d-x-3.15.1