Comments on: Custom Drawing with OpenGL ES 2.0 in Cocos2d 2.0 http://heyalda.com/drawing-with-opengl-es-2-0-in-cocos2d-2-0/ Heyalda specializes in mobile app, web service and responsive web app development. Tue, 15 Sep 2015 03:02:19 +0000 hourly 1 https://wordpress.org/?v=5.3.2 By: Jim http://heyalda.com/drawing-with-opengl-es-2-0-in-cocos2d-2-0/comment-page-1/#comment-334 Tue, 30 Oct 2012 14:23:08 +0000 http://heyalda.com/?p=954#comment-334 Thanks for the comment!

Check out the code in the Cocos2d CCSprite class’s draw method. That shows all of the basics for drawing a texture in Cocos2d.
You can then modify that code to do things such as use GL_REPEAT to repeat the texture to fill an area.

I just did a quick search and found on Ray Wenderlich’s site a tutorial by Allen Tan that might be what you are looking for.
http://www.raywenderlich.com/14302/how-to-make-a-game-like-fruit-ninja-with-box2d-and-cocos2d-part-1
While that tutorial covers a lot of stuff, search in that tutorial for: // Replace the draw method with this
That block of code looks to be the basic setup to draw a texture in Cocos2d 2.x and use GL_REPEAT, but I have not read the tutorial yet so I am not certain.
Hope this helps,
Jim

]]>
By: Maxxx http://heyalda.com/drawing-with-opengl-es-2-0-in-cocos2d-2-0/comment-page-1/#comment-332 Tue, 30 Oct 2012 10:42:17 +0000 http://heyalda.com/?p=954#comment-332 Great example – and thanks for taking the time to upload it.

I came across it while looking for an example of drawing a texture in OpenGL ES 2 using Cocos2d. So far I haven’t found anything that I can just grab – so I’m going to see if I can work out how to change your code to use a texture for the background…

Any hints wold be gratefully appreciated !!

]]>
By: Jim http://heyalda.com/drawing-with-opengl-es-2-0-in-cocos2d-2-0/comment-page-1/#comment-267 Fri, 26 Oct 2012 18:06:49 +0000 http://heyalda.com/?p=954#comment-267 I should have noticed that when I looked at the ccGLEnableVertexAttribs method definition… was rushed. I took a further look around in the Cocos 2d 2.0 game engine and noticed that when ccGLEnableVertexAttribs is called, it is called in draw methods instead of a one time initialization like I did, so I updated the code to OR the bit masks and moved the call to the draw method of the HeyaldaGLDrawNode CCNode subclass.

I updated the code in the post and in the HeyaldaOpenGLCustomDraw.zip to reflect these changes.

Jim

]]>
By: Liam http://heyalda.com/drawing-with-opengl-es-2-0-in-cocos2d-2-0/comment-page-1/#comment-266 Fri, 26 Oct 2012 17:27:30 +0000 http://heyalda.com/?p=954#comment-266 Thanks Jim,

Your information is good and enabled me to get up and running. One thing to note though:
kCCVertexAttribFlag_Position and kCCVertexAttribFlag_Color are bit masks and thus your two seperate calls overwrite each other. The correct call (to match your text in the comment above) is

ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color);

this sets both the position and color flags at the same time. If you need textures then you also need to “or” in the texture flag to the above call.

Thanks for your feedback, I could not find this information elsewhere on the Internet.
Liam

]]>
By: Jim http://heyalda.com/drawing-with-opengl-es-2-0-in-cocos2d-2-0/comment-page-1/#comment-260 Wed, 24 Oct 2012 20:01:58 +0000 http://heyalda.com/?p=954#comment-260 The zip file of the project has been updated as well as the code in the above post to address the issue Liam identified and to add iOS 6 and iOS <=5.1 rotation support. Jim

]]>
By: Jim http://heyalda.com/drawing-with-opengl-es-2-0-in-cocos2d-2-0/comment-page-1/#comment-259 Wed, 24 Oct 2012 19:26:51 +0000 http://heyalda.com/?p=954#comment-259 Hey Liam,

Thanks for the comment and for identifying a problem with the example I posted.

The cause of the problem you identified is that in my example I was not enabling vertex attributes with the ccGLEnableVertexAttribs function from the ccGLStateCache. When anything else such as a sprite or drawing primitive is drawn in Cocos2d, at least the kCCVertexAttribFlag is enabled. This is why the custom drawing worked when the labels and intro node were run but does not work when nothing else is drawn (i.e. the vertex attributes are not initialized).

To fix this in the example, I added ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); and ccGLEnableVertexAttribs( kCCVertexAttribFlag_Color ); to the init method after initializing the shaderProgram in the HeyaldaGLDrawLayer class.

I will post the updated sample code soon. The update will also have iOS 6 and iOS <=5.1.1 rotation support. Jim

]]>
By: Liam http://heyalda.com/drawing-with-opengl-es-2-0-in-cocos2d-2-0/comment-page-1/#comment-258 Wed, 24 Oct 2012 17:22:40 +0000 http://heyalda.com/?p=954#comment-258 Hi,

I wonder if you could shed some light on a problem I have.

I have created a scene with a single child node. This node is a subclass of ccnode and has a custom draw method (very similar to your example where you draw the background, just stripped to a core). The problem is that nothing get drawn. If I add a single ccsprite (in my case a CCLabelTTF) to the scene, then both the sprite and the custom drawing of the node is shown.

It seems that having only ccnodes with overridden draw methods has a problem, and nothing gets drawn.

I tried this with your code too. I delete the label you use and removed the intro layer (making the app delegate run the HelloWorld scene directly). And nothing gets drawn.

Any ideas or insights would be most welcome.

Liam

]]>