- Home /
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.
Answer by Mike 3 · Dec 22, 2010 at 02:01 AM
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
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.
Answer by Tito · Apr 13, 2012 at 05:10 PM
any chance of a code snippet of your working version?,any chance of a code snippet on how you did it?
Answer by wtgo · Apr 08, 2012 at 03:32 AM
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!
Answer by darmaz · Aug 30, 2012 at 11:06 AM
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 ;-) ]:
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).
Hi! I didn't solve the problem, sorry : (
Good luck with your project!
Your answer
Follow this Question
Related Questions
Creating a Texture or Texture2D from a plugin. 0 Answers
Extremely slow texture upload in plugin when a model is being drawn 1 Answer
Problem, Textures updated by plugin won't behave as expected in combination with HLSL shader 2 Answers
Issue with glDrawArrays in plugin 2 Answers
GL_LUMINANCE format for texture uploads in Android plugin 0 Answers