Subversion Repositories gelsvn

Rev

Rev 369 | Rev 503 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <iostream>
#include <fstream>
#include <string>
#include <GL/glew.h>
#include <GLGraphics/gel_glu.h>
using namespace std;

namespace GLGraphics
{
  void print_glsl_program_log(GLuint obj)
  {
          GLint infologLength = 0;
          glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &infologLength);
          if (infologLength > 0)
          {
                  char* infoLog = new char[infologLength];
                  glGetProgramInfoLog(obj, infologLength, 0, infoLog);
                  cout << "InfoLog:\n%s\n\n" << infoLog << endl;
                  delete infoLog;
          }
  }

  const std::string read_glsl_source(const std::string& path, const std::string& file)
  {
          string fullpath = path + "/" + file;
          ifstream is(fullpath.c_str(), ios::binary);
          string str, contents;
          while(getline(is,str)) 
          {
                  contents += str;
                  contents += "\n";
          }
          return contents;
  }

  GLuint create_glsl_shader(GLuint stype, const std::string& src)
  {
          GLuint S = glCreateShader(stype);
          const GLchar* src_cstr = src.c_str();
          glShaderSource(S, 1, &src_cstr, 0);
          glCompileShader(S);
        
          GLint status;
          glGetShaderiv(S,GL_COMPILE_STATUS, &status);
          if (status == 0) 
          {
                  GLint infologLength = 0;
                  glGetShaderiv(S, GL_INFO_LOG_LENGTH, &infologLength);
                  if (infologLength > 0)
                  {
                          char* infoLog = new char[infologLength];
                          glGetShaderInfoLog(S, infologLength, 0, infoLog);
                          cout << "InfoLog: " << infoLog << endl;
                          delete infoLog;
                  }
                  return 0;
          }
          return S;
  }

  GLuint create_glsl_shader(GLuint stype, const std::string& path, const std::string& file)
  {
          string str = read_glsl_source(path, file);
          if(str != "")
                  return create_glsl_shader(stype, str);
          return 0;
  }
}