Custom openGL drawing in cocos2dx

Im trying to draw custom openGL objects using cocos2dx. Currently i have subclassed a “Node” and am trying to override its “draw(Renderer* renderer, const Mat4 &transform, uint32_t flags)” function to do my custom drawing.

This is what im trying to draw (a simple triangle) and heres how im trying to setup my vertices, shaders and draw functions…

const GLfloat vertices[9] = {
    0.0f, 0.5f, 0.0f,
    -0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f
};


void MyNode::loadOurShaders(){
              GLProgram* myShader = new GLProgram();
              myShader->initWithFilenames("shader.vert", "shader.frag");
              myShader->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
              myShader->link();
              myShader->updateUniforms();
   }
        


void MyNode::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags){
   glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE,0, vertices);
    glDrawArrays(GL_TRIANGLES, 0, 3);
}  

While the draw function is being called, i dont see anything on the screen. Ive setup my ‘director’ and set its ‘EAGLView’ also. I dont know what I’m missing here and what i should do to see my triangle on the screen!

wrap your OpenGL in a CustomCommand search the forums for help

http://www.cocos2d-x.org/reference/native-cpp/V3.2/db/d74/classcocos2d_1_1_custom_command.html#aee6bf6d9cc073021bd23b44368a29e76

Thanks for that! Followed the link that led me to this . Now im able to draw cocos primitive objects on screen.

But ,If i change the drawing to openGL drawing (like my code above) it still does not show on the screen even though i have added the draw command to the render pipeline. Are there any other changes i need to make for the cocos2dx engine to render my vertices on screen?

and you are overriding draw()?

No. Im overriding draw(Renderer* renderer, const Mat4 &transform, uint32_t flags) and adding my custom command to the renderer. Like so…

 customCommand = new CustomCommand();
   _customCommand->init(_globalZOrder);
   _customCommand->func = CC_CALLBACK_0(MapLayer::drawCustom, this);

void MyNodeSubclass::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags){
   
    renderer->addCommand(_customCommand);
     }

void MyNodeSubclass::drawCustom(){
    
   //my openGL drawing goes here
    
}