00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <GLSL/GLSLShader.h>
00022 #include <GLEXT/GLStateExtension.h>
00023 #include <common/Defines.h>
00024 #include <string>
00025
00026 GLSLShader::GLSLShader(const char *filename, Type stype,
00027 const defines_list& dl) :
00028 id_(0)
00029 {
00030 DIALOG_ASSERT(GLStateExtension::hasShaders());
00031
00032 switch (stype)
00033 {
00034 case VERTEX:
00035 id_ = glCreateShader(GL_VERTEX_SHADER);
00036 break;
00037 case FRAGMENT:
00038 id_ = glCreateShader(GL_FRAGMENT_SHADER);
00039 break;
00040 default:
00041 DIALOG_ASSERT(0);
00042 }
00043
00044 DIALOG_ASSERT(id_);
00045
00046
00047 std::string prg;
00048
00049
00050 for (defines_list::const_iterator it = dl.begin(); it != dl.end(); ++it) {
00051 prg += std::string("#define ") + *it + "\n";
00052 }
00053
00054
00055 FILE *in = fopen(filename, "rb");
00056 if (!in) S3D::dialogExit("GLSLShader",
00057 S3D::formatStringBuffer("ERROR: Cannot find shader file \"%s\"", filename));
00058
00059 char buffer[256];
00060 while (fgets(buffer, 256, in) != 0)
00061 {
00062 prg += buffer;
00063 }
00064 fclose(in);
00065
00066 const char* prg_cstr = prg.c_str();
00067 glShaderSource(id_, 1, &prg_cstr, 0);
00068
00069 glCompileShader(id_);
00070
00071 GLint compiled = GL_FALSE;
00072 glGetShaderiv(id_, GL_COMPILE_STATUS, &compiled);
00073
00074
00075 GLint maxlength = 0;
00076 glGetShaderiv(id_, GL_INFO_LOG_LENGTH, &maxlength);
00077 std::string logText(maxlength+1, ' ');
00078 GLsizei length = 0;
00079 glGetShaderInfoLog(id_, maxlength, &length, &logText[0]);
00080
00081 if (compiled == GL_FALSE)
00082 {
00083 S3D::dialogExit("GLSLShader",
00084 S3D::formatStringBuffer("Shader compiling failed %s : %s", filename, logText.c_str()));
00085 }
00086 }
00087
00088 GLSLShader::~GLSLShader()
00089 {
00090 glDeleteShader(id_);
00091 }