Is is possible to have custom opengl drawing, e.g. custom shaders?


I know cocos2d-x is designed for 2D rendering. As cocos2d-x is based on ES 2.0 context, I am wondering if I can draw my own gl stuff on it.

I did google a lot. It is said overriding draw() of CCLayer works. I tried that. Shaders and gl drawing functions are working. However, those custom calls take over the whole scene. All other layers or sprites are covered, after I saw them in a blink.

My plan is having cocos2d-x sprites as user GUI or background and custom gl drawing as game objects. Is this possible?

I am currently working on android. But I think this is a cross platform problem.


If you use v2.x, you can refer to cocos2d-x/samples/Cpp/TestCpp/Classes/ShaderTest. If you use v3.0rc0 you can refer to cocos2d-x/tests/cpp-tests/Classes/ShaderTest


Hi, I am also attempting to draw with custom OpenGL, but it draws nothing on the screen.

void CustomSprite::draw(float x, float y, float rot)
    if (NULL == m_tex)

    GL::bindTexture2D( m_tex->getName() );
    const float w = m_tex->getPixelsWide();
    const float h = m_tex->getPixelsHigh();
    kmGLTranslatef(x, y, 0.0f);
    kmGLRotatef(rot, 0.0f, 0.0f, 1.0f);
    kmGLScalef(w, h, 1.0f);
    static const float box[] =
		-0.5f,	-0.5f,
		0.5f ,	-0.5f,
		-0.5f,	0.5f,
		0.5f,	0.5f
	const float u1 = 0;
	const float u2 = w;
	const float v1 = 0;
	const float v2 = h;
	const float tex[] = {
        u1, v1,
        u2, v1,
        u1, v2,
        u2, v2
    const GLubyte colors[] = {255, 255, 255, 255};
    glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0 , &box);
    glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, &tex);
    glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, &colors);

I call this function from the overriden Scene::draw() function.
I wonder what is the problem with above function? Any help would be greatly appreciated.


You should use custom command. You can refer to Sprite for more information. Now, draw() just send command to renderer, not send to GPU directly. And renderer will send all commands to GPU.


were u able to solve this? if yes then please share the codeā€¦ it will be of great help to me