The following source code contains all the steps described previously considering a program with three types of shaders: vertex, geometry, and fragment. The variable p, is assumed to be declared globally as GLuint.
void setShaders() {
GLuint v, g, f;
char *vs,*gs, *fs;
// Create shader handlers
v = glCreateShader(GL_VERTEX_SHADER);
g = glCreateShader(GL_GEOMETRY_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
// Read source code from files
vs = textFileRead("example.vert");
gs = textFileRead("example.geom");
fs = textFileRead("example.frag");
const char * vv = vs;
const char * gg = gs;
const char * ff = fs;
// Set shader source
glShaderSource(v, 1, &vv,NULL);
glShaderSource(g, 1, &gg,NULL);
glShaderSource(f, 1, &ff,NULL);
free(vs);free(gs);free(fs);
// Compile all shaders
glCompileShader(v);
glCompileShader(g);
glCompileShader(f);
// Create the program
p = glCreateProgram();
// Attach shaders to program
glAttachShader(p,v);
glAttachShader(p,g);
glAttachShader(p,f);
// Link and set program to use
glLinkProgram(p);
glUseProgram(p);
}
Thanks for the tutorial. The structure is very concise compared to others out there. I will, say, however, that we use variable names and not memory offsets for a reason 😉
Thanks for the comments Mike. I guess I should have been more verbose 🙂