Saturday, May 14, 2011

glShaderSource example c c++ objc

Following code fragment shows a glShaderSource example. After creating shader object, you provide shader source to the object by calling glShaderSource function. The associated shader source will be compiled by glCompileShader function. [glShaderSource]

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);