Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How to replace a function but still use the original function in it?

I want to modify the glBindTexture() function to keep track of the previously binded texture ID's. At the moment i just created new function for it, but then i realised that if i use other codes that use glBindTexture: then my whole system might go down to toilet.

So how do i do it?

Edit: Now when i thought it, checking if i should bind texture or not is quite useless since opengl probably does this already. But i still need to keep track on the previously used texture.

like image 654
Newbie Avatar asked Oct 20 '25 13:10

Newbie


2 Answers

As Andreas is saying in the comment, you should check this is necessary. Still, if you want to do such a thing, and you use gnu linker (you don't specify the operating system) you could use the linker option:

--wrap glBindTexture

(if given directly to gcc you should write):

-Wl,--wrap,glBindTexture

As this is done at linker stage, you can use your new function with an already existing library (edit: by 'library' I mean some existing code which you can recompile but which you wouldn't want to modify).

The code for the 'replacement' function will look like:

void * __wrap_glBindTexture (GLenum target, GLuint texture) {
   printf ("glBindTexture wrapper\n");
   return __real_glBindTexture (target,texture);
}
like image 122
vladmihaisima Avatar answered Oct 23 '25 03:10

vladmihaisima


You actually can do this. Take a look at LD_PRELOAD. Create a shared library that defines glBindTexture. To call the original implementation from within the wrapper, dlopen the real OpenGL library and use dlsym to call the right function from there.

Now have all client code LD_PRELOAD your shared lib so that their OpenGL calls go to your wrapper.

This is the most common method of intercepting and modifying calls to shared libraries.

like image 23
Borealid Avatar answered Oct 23 '25 04:10

Borealid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!