Using GL_TEXTURE_EXTERNAL_OES in Ogre Topic is solved

Discussion of issues specific to mobile platforms such as iOS, Android, Symbian and Meego.
Post Reply
redpearl
Gnoblar
Posts: 17
Joined: Tue Nov 28, 2017 12:19 am

Using GL_TEXTURE_EXTERNAL_OES in Ogre

Post by redpearl »

I am trying to develop an Android app using Ogre for rendering. I want to use the stream from camera as background. I found a 2D background sample code at http://wiki.ogre3d.org/Displaying+2D+Backgrounds, and successfully converted it into Java code. Now I have a static image as background.

However, when I try to change the texture to what I get from camera, I met a problem. In my other program without Ogre (using GL commands for rendering), I create a texture and bind it to target GL_TEXTURE_EXTERNAL_OES, and pass it to the SurfaceTexture that the camera use. So the texture will automatically get updated contents and I can use my own fragment shader to read from it.

It seems Ogre does not support GL_TEXTURE_EXTERNAL_OES. Even if I can get GLID using getCustomAttribute, it is bound to GL_TEXTURE_2D. Is there any way to use GL_TEXTURE_EXTERNAL_OES without modify Ogre itself?
redpearl
Gnoblar
Posts: 17
Joined: Tue Nov 28, 2017 12:19 am

Re: Using GL_TEXTURE_EXTERNAL_OES in Ogre

Post by redpearl »

Another way I can think is a hybrid-way: i.e. using my old GL commands to draw the background and using Ogre to draw the rest. To use my old GL commands, I need to draw into a GLSurfaceView. But I cannot use that GLSurfaceView to initialize Ogre (i.e. by calling initAppForAndroid). It failed with "Fail to create EGLSurface based on NativeWindowType in createSurfaceFromWindow".

Is it possible to use Ogre drawing stuffs into a GLSurfaceView?
paroj
OGRE Team Member
OGRE Team Member
Posts: 1993
Joined: Sun Mar 30, 2014 2:51 pm
x 1073
Contact:

Re: Using GL_TEXTURE_EXTERNAL_OES in Ogre

Post by paroj »

approach 1. is currently not supported and requires modification to Ogre.

for approach 2. you additionally need to tell ogre to use the external context via "currentGLContext" see,
https://ogrecave.github.io/ogre/api/1.1 ... 2f6e6c847e

however this has not been tested on android yet.

So your safest bet without recompiling ogre is to just use
tex->getBuffer()->blitFromMemory(box, box);

and see how big the performance penalty is.
redpearl
Gnoblar
Posts: 17
Joined: Tue Nov 28, 2017 12:19 am

Re: Using GL_TEXTURE_EXTERNAL_OES in Ogre

Post by redpearl »

Thanks for your reply paroj! Could you please provide more details on approach 2 and approach 3 (which you suggested)?

For approach 2, I am currently having errors when calling initAppForAndroid. Do you have ideas on how to resolve it? I do not manually call _createRenderWindow. How can I set currentGLContext for it?

For approach 3, my understanding is that you cannot directly copy contents from GL_TEXTURE_EXTERNAL_OES to GL_TEXTURE_2D. (It seems OpenGL does not allow it. So I suspect that Ogre command can do that.) I may have to write a shader reading from GL_TEXTURE_EXTERNAL_OES and write into GL_TEXTURE_2D with the help of FBO. Since I am quite new to Ogre, can you guide me how to do that?
paroj
OGRE Team Member
OGRE Team Member
Posts: 1993
Joined: Sun Mar 30, 2014 2:51 pm
x 1073
Contact:

Re: Using GL_TEXTURE_EXTERNAL_OES in Ogre

Post by paroj »

2) you will have to override the createWindow method to pass the additional miscParams:
https://ogrecave.github.io/ogre/api/1.1 ... f5b652722a

3) you will not use the external texture that you set up, but instead register a callback to get the bytes:
https://developer.android.com/reference ... lback.html
redpearl
Gnoblar
Posts: 17
Joined: Tue Nov 28, 2017 12:19 am

Re: Using GL_TEXTURE_EXTERNAL_OES in Ogre

Post by redpearl »

2) I am using Java API generated by OGRE_BUILD_COMPONENT_JAVA. How do I override createWidnow? Do I have to modify Ogre and rebuild the Java API?

3) I am not directly using Android camera, but get the external texture from another library. So it have to be an external texture. I can write a shader to copy its content to a GL_TEXTURE_2D. But I just do not figure out how to use my own shader in Ogre.
paroj
OGRE Team Member
OGRE Team Member
Posts: 1993
Joined: Sun Mar 30, 2014 2:51 pm
x 1073
Contact:

Re: Using GL_TEXTURE_EXTERNAL_OES in Ogre

Post by paroj »

2) inheriting from ApplicationContext should be enought. The method is virtual. Also note that you can set the background of the ogre surface transparent:
viewtopic.php?t=78980

3) You would need to setup all the gl state yourself and use the GLID attribute of the Ogre::Texture, as Ogre does not know how to use TEXTURE_EXTERNAL_OES. However if recompiling Ogre is an option, adding TEXTURE_EXTERNAL_OES support is just a matter of adding an enum value here
https://github.com/OGRECave/ogre/blob/v ... ture.h#L99
and correctly handling it here:
https://github.com/OGRECave/ogre/blob/m ... re.cpp#L73

(as well as making sure the limitations of TEXTURE_EXTERNAL_OES are respected). A pull request with this addition would be welcome ;)
redpearl
Gnoblar
Posts: 17
Joined: Tue Nov 28, 2017 12:19 am

Re: Using GL_TEXTURE_EXTERNAL_OES in Ogre

Post by redpearl »

Rebuild Ogre is fine for my case. I would like to modify Ogre if not too complicated.

Since external texture can only be used in shader, I need to write a new shader to use it. Can you guide me how to use my shader for an Ogre material?
redpearl
Gnoblar
Posts: 17
Joined: Tue Nov 28, 2017 12:19 am

Re: Using GL_TEXTURE_EXTERNAL_OES in Ogre

Post by redpearl »

paroj wrote: Fri Feb 02, 2018 11:50 am 2) inheriting from ApplicationContext should be enought. The method is virtual. Also note that you can set the background of the ogre surface transparent:
viewtopic.php?t=78980
In Java API (ApplicationContext.class), this function is not virtual. I see it is virtual in native code, but all native codes are called by Java API and my app only uses Java API. In this case, is there any easy way to do approach 2 without rewrite everything to use native code?
paroj
OGRE Team Member
OGRE Team Member
Posts: 1993
Joined: Sun Mar 30, 2014 2:51 pm
x 1073
Contact:

Re: Using GL_TEXTURE_EXTERNAL_OES in Ogre

Post by paroj »

were you looking for the virtual keyword in java?

you can just use "extends" on wrapped classes like on java classes and override any virtual methods. They will be correctly called from C++ base classes.

However I noticed that the "NameValuePairList" argument is not correctly wrapped for java, so you will not be able to actually set the required properties.
Thats why the Component is marked beta. If you want to continue, you could look into this issue or hard code the properties on the C++ side.
paroj
OGRE Team Member
OGRE Team Member
Posts: 1993
Joined: Sun Mar 30, 2014 2:51 pm
x 1073
Contact:

Re: Using GL_TEXTURE_EXTERNAL_OES in Ogre

Post by paroj »

support landed in master
Post Reply