Code refactory
Introduction
I’ve spent the last few days (yes, Christmas too…) working on a big refactory of the code of my programs, most of the code is Object Oriented now and I’m planning to make everything OO soon.
The changes
I’ve introduced 3 news classes:
- Shader : a vertex or a fragment shader, the code loads, compiles and manages a shader
- ShaderProgram : a shader program, it’s connected to two Shader
- PingPongTexture : a couple of textures needed by the Ping-Pong technique
Furthermore I’ve rearranged the shaders, now all the sources are in a separate directory and they can be easily used in both the programs.
Example of code
This code manages the erosion using the old code:
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
vs = InitShader(GL_VERTEX_SHADER, “src/shad.vert”);
fs = InitShader(GL_FRAGMENT_SHADER, “src/shad3.frag”);
p = InitProgram(vs, fs);
tex0_loc = glGetUniformLocation(p, “Tex0″);
alpha_loc = glGetUniformLocation(p, “alpha”);
glUniform1i(tex0_loc, 0);
glUniform1f(alpha_loc, alpha);
for(int i = 0; i < eros_it; i++)
{
glDrawBuffer(att_point[write_tex]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex[read_tex]);
ShaderDraw();
if(read_tex)
{
read_tex = 0;
write_tex = 1;
}
else
{
read_tex = 1;
write_tex = 0;
}
}
This code manages the same stuff using the new code:
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
p->AttachShader(eros1_shader);
p->LinkProgram();
p->UseProgram();
eros1_shader->SetUniformInt(“Tex0″, 0);
eros1_shader->SetUniformFloat(“alpha”, alpha);
for(int i = 0; i < eros_it; i++)
{
pp_tex->SetDrawBuffer();
glActiveTexture(GL_TEXTURE0);
pp_tex->BindTexture(READ_TEX);
p->Run(W, H);
pp_tex->Swap();
}
The difference is really evident!











