0

I use cocos2d engine to render some animation to CCGLView which is placed on app main window next to regular UIViews. My application uses external screen and I would like to render on that screen exactly the same content as in CCGLView but without any other views. And I need to perform this operation in an efficient manner so taking screenshots of CCGLView is not an option.

As I understand there is no such option using cocos2d as It supports presenting only one scene at time (CCDirector updates only one CCGLView at time).

So my question is:

Is it possible to achieve this goal using GLKView? I have access to frame buffer object from CCGLView and I can read pixels from the buffer. I think that the best option would be to use cocos2d runloop and perform this operation next to regular cocos2d rendering. Unfortunately I don't know too much about openGLES and I don't know how I can achieve this. cocos2d uses openGLES 2.0.

Edit:

For now the only suggestion came from @s1ddok (thanks) and the idea is to use CCRenderTexture to draw into CCGLView placed on main window and use CCRenderTexture's data to render to the external window. But I still don't understand how I can render the texture for the second time - this time to another view. Using another CCGLView would require configuring this view as a target for CCDirector, how can I do that? Moreover second CCGLView will share EAGLContext with the first one... So how to force cocos2d to render to the second CCGLView? Any help is appreciated!

parbo
  • 243
  • 3
  • 7

1 Answers1

1

I guess the best way you can go is CCRenderTexture. Render the whole scene onto it and then display the data on external screen.

It is a common practice for multiple purposes, for example, if you need to apply shader to a whole scene or something.

This actually will allow you to render scene only once every frame, and then use the same data for UIKit and external screen.

s1ddok
  • 4,615
  • 1
  • 18
  • 31
  • thanks for the response. Could you clarify how I can use CCRenderTexture to display my data on both CCGLView which is attached to the main window and on external window? I have CCNode which has CCRenderTexture and uses it to display my animation but it is attached to the CCScene which is run by CCDirector. I can't run another scene on the second window... – parbo Mar 07 '16 at 19:50
  • You should draw the whole scene onto CCRenderTexture, then use it's data to draw it into CCGLView (just place into a new scene and add as a child) and into external screen (don't have an idea how you do that, but you should simply transfer image data there) – s1ddok Mar 07 '16 at 19:57
  • If I understood, you suggest to switch scenes? It seems like a very inefficient solution to me, specially that I want to have 30FPS. Any idea how I can use FBO of main CCGLView directly to draw into i.e. GLKView? – parbo Mar 07 '16 at 20:41
  • Well, you are probably not. You should not switch any of your scenes. It is a matter of changing hierarchy a bit. Now you draw your scene directly into CCGLView. But you should place CCRenderTexture between CCGLView and your scene. Then you will be able to render it's data to CCGLView and use it for rendering into other view as a simple texture every frame. Performance will be great. – s1ddok Mar 07 '16 at 21:41
  • Sorry for asking so many questions but I'm new in this area. I don't understand how I can render CCRenderTexture's texture object into other view which is not in the hierarchy of scene (I mean it's not view of CCDirector)? You mentioned about rendering its content as a simple texture, how I can achieve this? Many thanks for your time. – parbo Mar 07 '16 at 23:41
  • You can simply draw two triangles with it's texture or even use image data. – s1ddok Mar 08 '16 at 07:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105684/discussion-between-parbo-and-s1ddok). – parbo Mar 08 '16 at 12:11