View Full Version : Shader from files

11-08-2010, 07:41 AM
I am new to GLSL. Right now I am reading the Book "OpenGL Shading Language Thrid Edition". My problem is that I don't know how I can load Shaders from files and use them with glShaderSource. Is there a way to that?

Hope that there are not too many spelling mistakes in my text.
Thank you.

11-08-2010, 08:26 AM
It is quite simple. Just load a content of the file into a string and pass it to the glShaderSource() function. For example:

char* CGLShader::FileRead(CString fileName)
FILE *fp;
char *content = NULL;

int count=0;

if (!fileName.IsEmpty()) {
fp = fopen(fileName,_T("rt"));
if (fp != NULL)
fseek(fp, 0, SEEK_END);
count = ftell(fp);
if (count > 0)
content = new char [count+1];
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
return content;