[Solved] Texture mapping in cocos2dx 3.2

Hi

I’m working on a project where I map an png image with openGL to a number of points (Vertex).
I override the draw method with following code which works fine on cocos3dx v3.0 but now when I update to 3.2 no image is shown.

Works fine in 3.0:

void JellyLogic::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) {
    kmGLPushMatrix();
    GL::bindTexture2D(texture->getName());
    texture->getShaderProgram()->use();
    texture->getShaderProgram()->setUniformsForBuiltins();
    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, triangleStripPos);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, textCoords);
    glDrawArrays(GL_TRIANGLE_STRIP, 0,  NUM_VERTEX);
    kmGLPopMatrix();
}

I guess I need to change the texture->getShaderProgram()->use() to texture->getGlProgram()->use() in 3.2
The code compiles fine but no images is showing.

What have I missed? Why does not the image show in version 3.2?

Do you have a screenshot of when it works and when i doesn’t?

I don’t have a screenshot at the moment but, it is just a black screen in 3.2. There is no problem drawing ordinary sprite nodes or tiledMap.

ok, I wanted to test this, what is texture, triangleStripPos, textCoords, NUM_VERTEX

Thanks for helping

Texture:

texture = Director::getInstance()->getTextureCache()->addImage("green.png");

triangleStripPos:

typedef struct {
    GLfloat x;
    GLfloat y;
} Vertex2D;    

Vertex2D triangleStripPos[13];

textCoords:

Vertex2D textCoords[13];

NUM_VERTEX:

#define NUM_VERTEX 13

I’m mapping a square png-image with 9 points (3 x 3). This square is then made jelly-ish using soft physics.

Sure, I’ll work with this.

In 3.3, latest from GitHub, so car I am doing:

void HelloWorld::draw(cocos2d::Renderer* renderer, const cocos2d::Mat4 &transform, bool transformUpdated)
{
    kmGLPushMatrix();
    GL::bindTexture2D(texture->getName());
    texture->getGLProgram()->use();
    texture->getGLProgram()->setUniformsForBuiltins();
    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, triangleStripPos);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, textCoords);
    glDrawArrays(GL_TRIANGLE_STRIP, 0,  NUM_VERTEX);
    kmGLPopMatrix();
}

Nothing is showing for me. I’ll need to try 3.2 and 3.1 etc to see what they produce.

Since this question appears quite frequently Cocos2d-x devs or maintainers probably should create a sticked post here which informs that Cocos2d-x uses a command queue to render nodes and thus custom drawing routines should be wrapped with the command.

Do you have any code samples or examples that shows this wrapping?

There are definitely some examples in cocos2d-x\tests\cpp-tests. Try searching for command in cpp files.

so we need to use customCommand to do this.

CustomCommand _customCommand; // in header or wherever makes sense for you

 _customCommand.init(_globalZOrder);

_customCommand.func = CC_CALLBACK_0(JellyLogic::onDraw, this, transform, flags);

renderer->addCommand(&_customCommand);

create JellyLogic::onDraw(...) and put that code above in it.

Thanks @dotsquid for the reminder.

Work like a charm!

Thanks

Just wanted to post the working code sample:

void JellyLogic::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    _customCommand.init(_globalZOrder);
    _customCommand.func = CC_CALLBACK_0(JellyLogic::onDraw, this, transform, flags);
    renderer->addCommand(&_customCommand);
}

void JellyLogic::onDraw(const Mat4 &transform, uint32_t flags) {
    kmGLPushMatrix();
    GL::bindTexture2D(texture->getName());
    texture->getGLProgram()->use();
    texture->getGLProgram()->setUniformsForBuiltins();
    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, triangleStripPos);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, textCoords);
    glDrawArrays(GL_TRIANGLE_STRIP, 0,  NUM_VERTEX);
    kmGLPopMatrix();
}

great!! Glad you are working now!

Only one problem… the images is showing in the bottom-left corner even though it is positioned some where else. I guess this has something to do with one of the flags.

EDIT: My bad, I messed up with the positioning. Everything works as it should :slight_smile:

I have a problem when moving around my screen, custom drawing always stays in same position,
how to fix this ?

EDIT: forgot to set matrix, here is the code
glProgram->use();
glProgram->setUniformsForBuiltins(transform);