0

Converting to cocos2d V3 and this code produces no drawing:

[_shaderProgram use]; //for V2 this was [shaderProgram_ use];

ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, lineVertices);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colorVertices);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);

Why does this code not work in V3? This code is called from the draw method of a CCScene. Any help much appreciated.

UPDATE: partial workaround found by calling ccDrawPoint before this code (drawing a small random point). Then glDrawArrays draws the correct shape (reading my lineVertices array) but ignores my colorVertices array. How can I get glDrawArrays to use my colorVertices array? (this code was working before I converted to V3)

James Webster
  • 31,873
  • 11
  • 70
  • 114

1 Answers1

2

I've been doing a bit of GL drawing on nodes recently, and had similar issues until I dug into the drawing code of CCNode.

Instead of explicitly calling use on the shaderProgram, try to call the following macro:

CC_NODE_DRAW_SETUP()

It expands to essentially the following in Cocos2D v3:

ccGLEnable(_glServerState);
[_shaderProgram use];
[_shaderProgram setUniformsForBuiltins];   
Johan
  • 21
  • 2
  • Johan thanks for your response. However, when I used the macro CC_NODE_DRAW_SETUP() as you suggested it crashed the program with error 'No shader program set for node: ' Do you know how to fix? – Greg Developer Apr 09 '14 at 17:26
  • What you need to do is set your shader program first - Cocos2D has a number of standard ones depending on what you are doing. As you are just setting position and colour, you can use: `self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionColor];` – Johan Apr 17 '14 at 00:07
  • Johan thanks my glDrawArrays is working. I used this code in the draw method before multiple calls to glDrawArrays: self->_shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionColor]; CC_NODE_DRAW_SETUP(); – Greg Developer Apr 17 '14 at 18:40