GLSLShader.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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); // ("invalid shader type");
00042         }
00043 
00044         DIALOG_ASSERT(id_); // runtime_error("can't create glsl shader");
00045 
00046         // the program as string
00047         std::string prg;
00048 
00049         // add defines to top of list for preprocessor
00050         for (defines_list::const_iterator it = dl.begin(); it != dl.end(); ++it) {
00051                 prg += std::string("#define ") + *it + "\n";
00052         }
00053 
00054         // read lines.
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         // get compile log
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 }

Generated on Mon Feb 16 15:14:45 2009 for Scorched3D by  doxygen 1.5.3