Format of texture passed from host to plugin

FFGL, OSC, GLSL. If you like abbreviations, this is the forum for you
Post Reply
davidemania
Is taking Resolume on a second date
Posts: 16
Joined: Sat Dec 21, 2013 15:10

Format of texture passed from host to plugin

Post by davidemania »

dear friends,
I asked a similar question on OF forum (copied here for your convenience). I'd like to ask you the "specifications" of the texture Arena passes to its plugins, it seems there is some mismatch with the format used by OpenFrameworks that is breaking the drawing when binding it with a mesh.

in the effort to write working FFGL plugins with OF080 I am currently struggling with a difficult (for me) problem. The data coming from host app is basically a texture handle with width and height info:

Code: Select all

//FFGLTextureStruct (for ProcessOpenGLStruct)
typedef struct FFGLTextureStructTag
{
  DWORD Width, Height;
  DWORD HardwareWidth, HardwareHeight;
  GLuint Handle; //the actual texture handle, from glGenTextures()
} FFGLTextureStruct;

// ProcessOpenGLStruct
typedef struct ProcessOpenGLStructTag {
  DWORD numInputTextures;
  FFGLTextureStruct **inputTextures;

  //if the host calls ProcessOpenGL with a framebuffer object actively bound
  //(as is the case when the host is capturing the plugins output to an offscreen texture)
  //the host must provide the GL handle to its EXT_framebuffer_object
  //so that the plugin can restore that binding if the plugin
  //makes use of its own FBO's for intermediate rendering
  GLuint HostFBO; 
} ProcessOpenGLStruct;
then the plugin code builds an ofTexture from this data, binding it with the handle it received from host:

Code: Select all

for(int i = 0; i < pGL->numInputTextures; i++ )
{
	// create texture here if it isnt there...
	if( !inputTextures[i] )
		inputTextures[i] = new ofTexture();

	FFGLTextureStruct &tex = *(pGL->inputTextures[i]);
	ofTexture * ofTex = inputTextures[i];

	// adapted from ofQC by vade.
	ofTex->texData.textureID = tex.Handle;
	ofTex->texData.textureTarget = GL_TEXTURE_2D;
	ofTex->texData.width = tex.Width;
	ofTex->texData.height = tex.Height;
	ofTex->texData.bFlipTexture = true;
	ofTex->texData.tex_w = tex.HardwareWidth;
	ofTex->texData.tex_h = tex.HardwareHeight;
	ofTex->texData.tex_t = ((float)tex.Width)/ tex.HardwareWidth;
	ofTex->texData.tex_u = ((float)tex.Height)/ tex.HardwareHeight;
	ofTex->texData.glTypeInternal = GL_RGBA;  // this is just a guess...
	//ofTex->texData.glType = GL_RGBA;
	ofTex->texData.bAllocated = true;
}
And now the weird part: the texture so created is fine if you try to use it to draw on the screen (that is the host window where output goes) but when you try to bind it to a mesh to have it displayed the drawing is corrupted, showing a uniform color with - at times - small triangles of a different shade.

Playing with the code I noticed that writing the texture to an array of pixels (via ofImage) and taking back the texture works, the new texture correctly binds to the mesh and draws.

Code: Select all

ofTexture * tex = inputTextures[0];

fbo.begin();
tex->draw(0, 0);
fbo.end();

fbo.readToPixels(image.getPixelsRef());
image.update();

image.bind();
 ... draw the mesh ...
image.unbind();
This works correctly but of course it is slow (on my machine fps drop to 30 with a 640x480 window).

The question is: how can I find out what's wrong with the texture I get from host? Is there some parameter I could try to match it with the ones inside OF code?

By the way this could be related with a known bug of ofxFFGLPlugin and OF 080, where depth testing does not work.

Any help would be greatly appreciated, thanks
Davide

drand48
Team Resolume
Posts: 14
Joined: Wed Jul 29, 2009 00:33

Re: Format of texture passed from host to plugin

Post by drand48 »

Ciao Davide,

I think the problem is that OF uses GL_TEXTURE_RECTANGLE by default, so texture coordinates are mapped to the image size ( eg. [0...512] for a 512x512 pixel texture ) as opposed to the typical [0..1] range with GL_TEXTURE_2D.

By writing the texture back to an ofImage you creating a new texture (using GL_TEXTURE_RECTANGLE) so everything works as expected.

I am not aware about any method that allows converting a texture from a type to another directly on the GPU, but one thing you could do is adjust the texture coordinates of your mesh to work in the correct range.

Hope it helps, in bocca al lupo!
Daniel

davidemania
Is taking Resolume on a second date
Posts: 16
Joined: Sat Dec 21, 2013 15:10

Re: Format of texture passed from host to plugin

Post by davidemania »

Sorry I couldn't reply sooner, but yes, it turned out it was a problem of coordinate scaling. Fixing that it worked fine and I could get rid of the slow image rebuilding on the CPU. Thanks!

Post Reply