Subversion Repositories gelsvn

Rev

Rev 502 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
369 jab 1
#include <iostream>
2
#include <fstream>
3
#include <string>
4
#include <GL/glew.h>
5
#include <GLGraphics/gel_glu.h>
6
using namespace std;
7
 
373 jrf 8
namespace GLGraphics
369 jab 9
{
502 jrf 10
  void print_glsl_program_log(GLuint program)
373 jrf 11
  {
502 jrf 12
    GLint infoLog_length = 0;
13
    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLog_length);
14
 
503 jrf 15
    // Some drivers return 1 as infoLog_length when the infoLog is an empty string
502 jrf 16
    if(infoLog_length > 1) 
17
    {
18
      char* infoLog = new char[infoLog_length];
19
      glGetProgramInfoLog(program, infoLog_length, 0, infoLog);
20
      cerr << "InfoLog:" << endl << infoLog << endl << endl;
21
      delete [] infoLog;
22
    }
373 jrf 23
  }
369 jab 24
 
502 jrf 25
  void print_glsl_shader_log(GLuint shader)
26
  {
27
    GLint infoLog_length = 0;
28
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLog_length);
29
 
503 jrf 30
    // Some drivers return 1 as infoLog_length when the infoLog is an empty string
502 jrf 31
    if(infoLog_length > 1)
32
    {
33
      char* infoLog = new char[infoLog_length];
34
      glGetShaderInfoLog(shader, infoLog_length, 0, infoLog);
35
      cerr << "InfoLog:" << endl << infoLog << endl << endl;
36
      delete [] infoLog;
37
    }
38
  }
39
 
40
  bool check_glsl_shader(GLuint shader)
41
  {
42
    GLint status;
43
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
44
    return status == GL_TRUE;
45
  }
46
 
373 jrf 47
  const std::string read_glsl_source(const std::string& path, const std::string& file)
48
  {
502 jrf 49
    string fullpath = path + "/" + file;
50
    ifstream is(fullpath.c_str(), ios::binary);
51
    string str, contents;
52
    while(getline(is,str)) 
53
    {
54
      contents += str;
55
      contents += "\n";
56
    }
57
    return contents;
373 jrf 58
  }
369 jab 59
 
502 jrf 60
  GLuint create_glsl_shader(GLuint stype, const std::string& src, bool print_log)
373 jrf 61
  {
502 jrf 62
    GLuint S = glCreateShader(stype);
63
    const GLchar* src_cstr = src.c_str();
64
    glShaderSource(S, 1, &src_cstr, 0);
65
    glCompileShader(S);
66
 
67
    if(print_log && !check_glsl_shader(S))
68
    {
69
      print_glsl_shader_log(S);
70
      glDeleteShader(S);
71
      return 0;
72
    }
73
    return S;
373 jrf 74
  }
369 jab 75
 
373 jrf 76
  GLuint create_glsl_shader(GLuint stype, const std::string& path, const std::string& file)
77
  {
502 jrf 78
    string str = read_glsl_source(path, file);
79
    if(!str.empty())
80
    {
81
      GLuint shader = create_glsl_shader(stype, str, false);
82
      if(!check_glsl_shader(shader))
83
      {
84
        cerr << path << file << endl;
85
        print_glsl_shader_log(shader);
86
        glDeleteShader(shader);
87
        return 0;
88
      }
89
      return shader;
90
    }
91
    return 0;
373 jrf 92
  }
369 jab 93
}