Subversion Repositories gelsvn

Rev

Rev 502 | Rev 594 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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