x


Cannot update texture from plugin (windows)

Hi all,

I'm trying to update a texture within a plugin using opengl (Windows, Visual C++), since SetPixlels() + Apply() seems to be way too slow for frequent updates.

It all compiles and runs fine, but the data does not make it into the texture, it displays random stuff. In my plugin, glBindTexture( GL_TEXTURE_2D, tex_id ) finishes with glGetError = GL_INVALID_OPERATION, which seems to be the root of the problem.

Does the Windows standard opengl implementation (GL/gl.h) even work well with unity in this way?

Unity Behaviour:

void init() {
m_Texture = new Texture2D( tex_rgb_width, tex_rgb_height, TextureFormat.ARGB32, false);
m_Texture.Apply();

GUITexture gui = GameObject.Find("gui").GetComponent<GUITexture>() as GUITexture;
gui.texture = m_Texture;
}

void update() {
bool res = update_rgb_texture( m_Texture.GetNativeTextureID(), tex_rgb_width, tex_rgb_height, (int) Time.timeSinceLevelLoad*10 );
}

Plugin:

bool update_rgb_texture( int tex_id, int width, int height, int time ) {
  glBindTexture( GL_TEXTURE_2D, tex_id );
  int id;
  int w = width;
  int h = height;
  vector<unsigned char> t( w*h*4 );
  for (int i=0; i<height; i++) {
    for (int j=0; j<width; j++) {
      id = (4*j)+(4*w*i);
      t[id] = (GLubyte) time%255;
      t[id+1] = (GLubyte) time%255;
      t[id+2] = (GLubyte) (255-time)%255;
      t[id+3] = (GLubyte) (255-time)%255;
    }
  }
  glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, &t[0] );
  return true;
}

Thanks a lot!! Chris.

more ▼

asked Dec 22 '10 at 01:31 AM

Christian 4 gravatar image

Christian 4
12 1 1 2

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

Are you forcing unity to be running in OpenGL mode? Windows defaults to DirectX by default so forcing opengl textures probably won't do a great deal

more ▼

answered Dec 22 '10 at 02:01 AM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

(comments are locked)
10|3000 characters needed characters left

Excellent, that fixed it. Thanks!

Are there any expected side effect of forcing OpenGL like that? Frame rates seem to be similar... haven't tested fully yet.

Cheers, Chris.

more ▼

answered Dec 30 '10 at 12:04 AM

Christian 4 gravatar image

Christian 4
12 1 1 2

(comments are locked)
10|3000 characters needed characters left

any chance of a code snippet of your working version?,any chance of a code snippet on how you did it?

more ▼

answered Apr 13 '12 at 05:10 PM

Tito gravatar image

Tito
0

(comments are locked)
10|3000 characters needed characters left

One more note for any onlookers: the bind & update call has to be on the render thread, or it will fail. GL.IssuePluginEvent did the trick for me, and now I have fast streaming alpha video textures with no calls to Texture2D.Apply!

more ▼

answered Apr 08 '12 at 03:32 AM

wtgo gravatar image

wtgo
31 2 2 2

(comments are locked)
10|3000 characters needed characters left

Hello! I've been trying to use nativeTextureIDs myself for a while, but they just doesn't seem to work [or, I just can't seem to get them working ;-) ].

I have an educational pro license, and I'm quite familiar with dll development. I modified the GL.IssuePluginEvent example project in order to send a RenderTexture ID to an external dll, while the dll UnityRenderEvent just binds to the texture id and applies the texture to a quad and then to a triangle. What I get is just the "for educational use only" string used as a texture over both the quad and triangle. Is that some limitation of trial/educational pro versions, or am I just forgetting something?

I also tried to randomly change the texture ID, and the result is always the same [and quite funny ;-) ]:

alt text

Thanks!!

Cheers, D.

P.S. The very same OpenGL code works with textures sent as Color32 arrays to secondary OpenGL/GLUT window (a different dll), but that was way too slow for my needs (sending RenderTexture on every frame). I tried to use native IDs in that case too, but with no luck (different opengl contexts: didn't work even if I explicitly tried to get the contexts handles).

screenshot.jpg (69.3 kB)
more ▼

answered Aug 30 '12 at 11:06 AM

darmaz gravatar image

darmaz
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2205
x396
x114

asked: Dec 22 '10 at 01:31 AM

Seen: 2401 times

Last Updated: Aug 30 '12 at 11:06 AM