Is it possible to setup something sort of like a class in C++ but in c in the simplest terms? I want to avoid using c++ but I would like to create a simple struct that has glsl shader program.
for example
struct shader_program
{
GLuint shader;
void (*create_shader)(const char* vss, const char* fss);
void (*enableShader)(void);
GLuint (*getUniformLoc(struct shader_program* sp, char* name);
void (*disableShader)(void);
}
sample usage.
struct shader_program simple_shader = create_shader(.., ..);
struct shader_program advanced_shader = create_shader(.., ..);
draaw()
{
simple_shader->enableShader();
//...bind vai, draw, etc..
simple_shader->disableShader();
advanced_shader->enableShader();
//draw
advanced_shader->disableShader();
}
the main question is how would i set this up in a header and source file so that I can use a struct like this to do my rendering?
simple_shader->enableShader()
is going to be impossible. The closest you could get issimple_shader->enableShader(simple_shader)
. That's because C does not have athis
keyword. That means functions (even if pointed to by the data of the struct) have no frame of reference to the variable you're using to call them, unless you explicitly pass it. I personally would recommend the approach of Honeybunchs answer, unless you have a reason to dosimple_shader->enableShader(simple_shader)
(the C version of a virtual function, effectively) – Wolfgang Skyler Jul 13 at 19:43