Saturday, May 14, 2011

glUseProgram example c c++ objc

[glUseProgram example] Following code fragment shows a glUseProgram example. After a program object is linked successfully, we are ready to load the shader program into the GPU. glUseProgram(id) performs this work. The shader behaviour of the GPU will change immediately. If you want to go back to default shader, call glUseProgram(0).  [glUseProgram example]

Code collected from GLWiki.

/* The vertex shader */
 char *vsSource = file2string("wave.vert");
 char *fsSource = file2string("wave.frag");
 
 /* Compile and load the program */
 
 GLuint vs, /* Vertex Shader */
     fs, /* Fragment Shader */
     sp; /* Shader Program */
 
 
 vs = glCreateShader(GL_VERTEX_SHADER);
 glShaderSource(vs, 1, &vsSource, NULL);
 glCompileShader(vs);
 printLog(vs);
 
 fs = glCreateShader(GL_FRAGMENT_SHADER);
 glShaderSource(fs, 1, &fsSource, NULL);
 glCompileShader(fs);
 printLog(fs);
 
 free(vsSource);
 free(fsSource);
 
 sp = glCreateProgram();
 glAttachShader(sp, vs);
 glAttachShader(sp, fs);
 glLinkProgram(sp);
 printLog(sp);
 
 glUseProgram(sp);