PrimitiveCommand custom drawing transform/position

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

  1. I derived a new class from a Sprite.
  2. Init this new class with a texture
  3. AddChild to my Scene
  4. Setup my data of V3F_C4B_T2F structs (texcoord, color and vertices)
  5. 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	));
  1. Setup IndexBuffer
m_pIndexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, m_vecIndex.size());
  1. Setup Primitive
m_pPrimitve = Primitive::create(m_pVertexData, m_pIndexBuffer, GL_TRIANGLES);
  1. Overload draw function and setup PrimitiveCommand
m_oPrimitiveCommand.init( _globalZOrder, getTexture()->getName(), getGLProgramState(), _blendFunc, m_pPrimitve, transform, flags );
renderer->addCommand(&m_oPrimitiveCommand);
  1. 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

Hello

It depends on what you want to achieve.

I have complex mesh data from another source and want to load and render it. It is a complex object with several sub meshes. A list of indices, vertices and uvs. Also there are animation files, physic data and additional expressions that are blended on top of the base animations.

Each sprite “sub mesh” is implemented as a PrimitiveCommand like I explained above. All “sub meshes” are added to a Node that I just called “mesh”. Each frame the “mesh” gets an update and iterates through all “sub mesh” vertices if necessary (if animation changed). This works fine and it looks like it isn’t a performance problem (yet).

What I want to do is to set my “mesh” to for example SetPositionX(100). If I do that nothing happens. My question is: When I use the PrimitiveCommand why does my vertices are not moved around. The _mv (transform) matrix gets updated if I move submesh itself around, but on the screen my sub meshes won’t move. Also, If I set the position of the parent node or (parents parent etc.) the transform node dosn’t even change. It seems like it is only for the current sprite?!

Do I need to update my vertices manually if I use the PrimitiveCommand? For Example:

Vec2 vecPos = getParent()->getPosition();

for ( unsigned int i=0; i<m_vecDrawable.size(); i++ )
{
	m_vecDrawable[i].vertices = Vec3( vectices->X + vecPos.x , vectices->Y + vecPos.y, 0.0 );
	vectices++;
}

Do I need to use something like convertToWorldSpace or whatever other converstion that exist? I am a little bit lost here.

What is the function of the transform matrix if it isn’t doing anything while using the PrimitiveCommand? Or am I doing it wrong? Even if the transform matrix changes because I use SetPosition on the “sub mesh” itself, nothing will happen.

virtual void draw( Renderer *renderer, const Mat4& transform, uint32_t flags );

I can not help in this case.

@slackmoehrle Do you know anyone who can help?

Also, stackoverflow can be a good place to ask.

Let me review this thread when I get started on work tomorrow…