Can I change the name of an OpenGL texture?
I am working with a sample Rendering Plugin for Unity.
Unity calls the plugin and passes it a texture pointer, which is the name of the OpenGL texture (SetTextureFromUnity function for reference). I have another texture that I create and manage in my plugin. I would like to somehow get a reference to the Unity text so that I can use my texture instead of the one that suits me.
Is it possible for a Unity texture to just point to my texture? I am very rude to the low level of OpenGL.
Alternatively: is it possible to create a Texture2D from an openGL texture that already exists? Then I could just create my own texture and bring it back to unity to wrap the Texture2D.
I know I can copy data from my texture to a Unity texture, but it's incredibly slow and inefficient when the data for both is already on the GPU. I go through copies of the FBO, but it all seems overkill when I just want pointer A to point to the same thing that pointer B points to (in theory in any way) ...
source to share
#define SIZE 1024
#define MASK (SIZE - 1)
struct texture
{
my_image_object *image;
};
struct texture table[SIZE];
void
bind_my_image_object(my_image_object *image)
{
uintmax_t hash;
size_t index;
hash = my_pointer_hash_function((uintptr_t)image);
index = hash & MASK;
texture = table + index;
glBindTexture(GL_TEXTURE_2D, index);
if (texture->image != image)
{
your_texture_upload_routine(image);
texture->image = image;
}
}
Use a decent hash function. Shift the pointer value to avoid the typical object alignment in your hash function. for example: (uintptr_t)image >> 3
on a 64-bit PC. And use a table large enough not to waste texture on every frame. Also use a table size that has a cardinality of 2, so hash & MASK
will wrap properly.
General hash table guidelines apply. Your hash table may be different.
source to share