1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
char *Shaders::textFileRead(char *fn)
{
FILE *fp;
char *content = NULL;
int count=0;
if (fn != NULL)
{
fp = fopen(fn,"rt");
if (fp != NULL)
{
fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > 0)
{
content = (char *)malloc(sizeof(char) * (count+1));
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}
GLuint Shaders::loadBasicShaders()
{
log->logMessage(log->GL, "Creating basic shaders...");
GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
const char* vSource = textFileRead("../Shaders/basicShader.vert");
glShaderSource(vs, 1, &vSource, NULL);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
if (0 == compile_ok)
{
log->logMessage(log->GLERR, "Error in vertex shader\n");
return false;
}
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const char* fSource = textFileRead("../Shaders/basicShader.frag");
glShaderSource(fs, 1, &fSource, NULL);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok)
{
log->logMessage(log->GLERR, "Error in fragment shader");
return false;
}
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
log->logMessage(log->GLERR, "Could not link basic shaders!");
return false;
}
log->logMessage(log->GL, "Done!");
return program;
}
|