string Shader::Compile(const char *name, const char *vscode, const char *pscode) { program = glCreateProgram(); string err; vs = CompileGLSLShader(GL_VERTEX_SHADER, program, vscode, err); if (!vs) return string("couldn't compile vertex shader: ") + name + "\n" + err; ps = CompileGLSLShader(GL_FRAGMENT_SHADER, program, pscode, err); if (!ps) return string("couldn't compile pixel shader: ") + name + "\n" + err; glBindAttribLocation(program, 0, "apos"); glBindAttribLocation(program, 1, "anormal"); glBindAttribLocation(program, 2, "atc"); glBindAttribLocation(program, 3, "acolor"); glBindAttribLocation(program, 4, "aweights"); glBindAttribLocation(program, 5, "aindices"); Link(name); return ""; }
string Shader::Compile(const char *name, const char *vscode, const char *pscode) { program = glCreateProgram(); string err; vs = CompileGLSLShader(GL_VERTEX_SHADER, program, vscode, err); if (!vs) return string("couldn't compile vertex shader: ") + name + "\n" + err; ps = CompileGLSLShader(GL_FRAGMENT_SHADER, program, pscode, err); if (!ps) return string("couldn't compile pixel shader: ") + name + "\n" + err; glBindAttribLocation(program, 0, "apos"); glBindAttribLocation(program, 1, "anormal"); glBindAttribLocation(program, 2, "atc"); glBindAttribLocation(program, 3, "acolor"); glBindAttribLocation(program, 4, "aweights"); glBindAttribLocation(program, 5, "aindices"); glLinkProgram(program); GLint status; glGetProgramiv(program, GL_LINK_STATUS, &status); if (status != GL_TRUE) { GLSLError(program, true, NULL); throw string("linking failed for shader: ") + name; } mvp_i = glGetUniformLocation(program, "mvp"); col_i = glGetUniformLocation(program, "col"); camera_i = glGetUniformLocation(program, "camera"); light1_i = glGetUniformLocation(program, "light1"); bones_i = glGetUniformLocation(program, "bones"); glUseProgram(program); for (int i = 0; i < MAX_SAMPLERS; i++) { tex_i[i] = glGetUniformLocation(program, (string("tex") + inttoa(i)).c_str()); if (tex_i[i] >= 0) glUniform1i(tex_i[i], i); } return ""; }
string Shader::Compile(const char *name, const char *cscode) { #if !defined(PLATFORM_ES2) && !defined(__APPLE__) program = glCreateProgram(); string err; cs = CompileGLSLShader(GL_COMPUTE_SHADER, program, cscode, err); if (!cs) return string("couldn't compile compute shader: ") + name + "\n" + err; Link(name); return ""; #else return "compute shaders not supported"; #endif }
GLuint CompileGLSLShaderFromFile(GLenum type, const char* filename) { FILE *shaderFile; char *text; long size; //Check for no filename if (filename == NULL) return 0; //Must read files as binary to prevent problems from newline translation shaderFile = fopen(filename, "rb"); if (shaderFile == NULL) { CLOGGER_ERROR(WCLogManager::RootLogger(), "CompileGLSLShaderFromFile - Unable to open " << filename); return 0; } fseek(shaderFile, 0, SEEK_END); size = ftell(shaderFile); fseek(shaderFile, 0, SEEK_SET); text = new char[size+1]; fread(text, size, 1, shaderFile); fclose(shaderFile); text[size] = '\0'; GLuint object = CompileGLSLShader(type, text); delete []text; return object; }