Author Topic: assign a GL texture to an Object3D  (Read 25665 times)

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
assign a GL texture to an Object3D
« on: January 23, 2014, 03:20:53 pm »
is it possible to assign an Object3D a GL texture which is created outside of jPCT?

with this code i can create a special texture to which i can redirect Android's Camera or MediaPlayer output, kind of render to texture. if i can assign that texture to an Object3d, i can play perspectively correct videos inside jPCT world.

is it possible?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: assign a GL texture to an Object3D
« Reply #1 on: January 23, 2014, 06:15:50 pm »
Not by default, but it might be if i would expose the GL id of a texture. It would be 100% up to you then to setup the texture, manage context changes and such. I'll look into it.
« Last Edit: January 23, 2014, 08:46:20 pm by EgonOlsen »

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: assign a GL texture to an Object3D
« Reply #2 on: January 23, 2014, 06:53:52 pm »
sounds good :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: assign a GL texture to an Object3D
« Reply #3 on: January 23, 2014, 08:50:04 pm »
Ok, here you go: http://jpct.de/download/beta/jpct_ae.jar...i've no idea if this works, it's 100% untested. I've added a method

Code: [Select]
Texture.setExternalId(<int>);

In your case, that would be the value that

Code: [Select]
GLES20.glGenTextures(1, textures, 0);

returns. Setting this value should override everything else, i.e. the renderer will use this id regardless of the current context or if it exists or not. So it's up to you to keep this id fresh. Other settings on the texture like mip mapping, compression etc. will have no effect anymore.

Please let me know if it works or not.

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: assign a GL texture to an Object3D
« Reply #4 on: January 24, 2014, 11:15:18 am »
thanks :)

well, i have a view. for a fast start, i first tried to redirect camera view to texture. below is a screenshot. an UV adjustment was needed since as can be expected the created texture has a 2^n size.



the camera view on the back is a texture blitted by jPCT. the debug text in black is part of the texture (printed by OpenCV before image is converted to texture) so it's also blitted.

the red rectangles, axes arrows and and the plane are all 3d objects in jPCT world. the plane uses the new external texture so it also displays the camera view.

the weird thing is, plane also displays the debug text. i cant understand how and why :o

also I had a repeating GL error 0x502 at SurfaceTexture.updateTexImage() in logs. after some googling I've found this is caused by an error happening before SurfaceTexture.updateTexImage(). so I call before updateTexImage:

Code: [Select]
int error = GLES20.glGetError();
if (error != 0) {
    System.out.println("GL error " + error + ": " + GLU.gluErrorString(error));
}

and got:

Code: [Select]
GL error 1282: invalid operation
if I dont assign this texture to plane but still call SurfaceTexture.updateTexImage(), the error goes away. i cant see inside of updateTexImage since it's a native method.

any ideas?

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: assign a GL texture to an Object3D
« Reply #5 on: January 24, 2014, 03:06:43 pm »
i've skipped blitting generated camera texture to back of the scene and the result is as below:



this is the texture generated by GLFont. if i skip using GLFont the plane uses the texture of axis arrows. in short seems as plane uses the last texture used.

should I use this external texture like a regular texture? or should i activate it with something like:

Code: [Select]
GL10.glBindTexture(..)

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: assign a GL texture to an Object3D
« Reply #6 on: January 24, 2014, 03:18:36 pm »
and as a reminder, this is not a regular texture but an external texture, maybe that requires special processing?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: assign a GL texture to an Object3D
« Reply #7 on: January 24, 2014, 03:42:56 pm »
That might be an indicator that the ID that you've set as external texture id is not (or no longer) valid. There shouldn't be any need to do some special treatment. It will harm more than it helps.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: assign a GL texture to an Object3D
« Reply #8 on: January 24, 2014, 04:10:05 pm »
At which stage, i.e. in which GL context are you creating the external texture?

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: assign a GL texture to an Object3D
« Reply #9 on: January 24, 2014, 04:15:17 pm »
argh :-[ you're right. texture was never initialized.

after fixing that, and forcibly switching to GL 2, same error code is detected and thrown by jPCT. this happens just after the plane is created and added to world.

Code: [Select]
FATAL EXCEPTION: GLThread 52706
java.lang.RuntimeException: [ 1390575883177 ] - ERROR: before: glError 1282
        at com.threed.jpct.Logger.log(Logger.java:193)
        at com.threed.jpct.GL20.checkError(GL20.java:152)
        at com.threed.jpct.GL20.glGenBuffers(GL20.java:1362)
        at com.threed.jpct.CompiledInstance.compileToVBO(CompiledInstance.java:1455)
        at com.threed.jpct.CompiledInstance.render(CompiledInstance.java:593)
        at com.threed.jpct.GLRenderer.drawVertexArray(GLRenderer.java:2289)
        at com.threed.jpct.World.draw(World.java:1361)
        at com.threed.jpct.World.draw(World.java:1099)

At which stage, i.e. in which GL context are you creating the external texture?
at onSurfaceCreated(..) method

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: assign a GL texture to an Object3D
« Reply #10 on: January 24, 2014, 04:19:53 pm »
Quote
at onSurfaceCreated(..) method
this is where the shaders are compiled and glGenTextures is called. the plane and external jPCT texture is created later in UI-Thread, not in the GL-thread.

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: assign a GL texture to an Object3D
« Reply #11 on: January 24, 2014, 04:25:18 pm »
same happens when plane and texture is created in GL-thread

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: assign a GL texture to an Object3D
« Reply #12 on: January 24, 2014, 04:28:45 pm »
jPCT just detects the error at that stage. It might or might not have caused it itself. It might be a problem with the current GL state. You have to make sure that after setting up the texture, the state is 'clean' i.e. it should be like as if you have never done anything to it.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: assign a GL texture to an Object3D
« Reply #13 on: January 24, 2014, 04:38:08 pm »
and as a reminder, this is not a regular texture but an external texture, maybe that requires special processing?
I'm not sure about this...as long you can bind it in a normal way and as long as a shader can access it in a normal way, i don't see why that should be a problem.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: assign a GL texture to an Object3D
« Reply #14 on: January 24, 2014, 04:44:10 pm »
Have you checked what actually causes this error? The problem with gl errors is that they persist until somebody reads them. They might have happened waaaaay before they surface. You can only be sure if you check for a gl error after each and every gl call.