http://discuss.cocos2d-x.org/t/solved-texture-mapping-in-cocos2dx-3-2/16687/10
in V3.0
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.
in V3.2
uses a command queue to render nodes and thus custom drawing routines should be wrapped with the command.
o 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.
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();
}
综合以上例子跟下面的图表,可以非常清楚看到这个新的Render 机制。
rendering Node during vist will not call into OpengGL anymore