Subversion Repositories gelsvn

Rev

Rev 373 | Rev 503 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 373 Rev 502
Line 5... Line 5...
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 obj)
10
  void print_glsl_program_log(GLuint program)
11
  {
11
  {
12
	  GLint infologLength = 0;
12
    GLint infoLog_length = 0;
13
	  glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &infologLength);
13
    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLog_length);
-
 
14
 
-
 
15
    // Some drivers return 1 as infologLength when the infoLog is an empty string
14
	  if (infologLength > 0)
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
    }
-
 
23
  }
-
 
24
 
-
 
25
  void print_glsl_shader_log(GLuint shader)
15
	  {
26
  {
-
 
27
    GLint infoLog_length = 0;
-
 
28
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLog_length);
-
 
29
 
-
 
30
    // Some drivers return 1 as infologLength when the infoLog is an empty string
-
 
31
    if(infoLog_length > 1)
-
 
32
    {
16
		  char* infoLog = new char[infologLength];
33
      char* infoLog = new char[infoLog_length];
17
		  glGetProgramInfoLog(obj, infologLength, 0, infoLog);
34
      glGetShaderInfoLog(shader, infoLog_length, 0, infoLog);
18
		  cout << "InfoLog:\n%s\n\n" << infoLog << endl;
35
      cerr << "InfoLog:" << endl << infoLog << endl << endl;
19
		  delete infoLog;
36
      delete [] infoLog;
-
 
37
    }
20
	  }
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;
21
  }
45
  }
22
 
46
 
23
  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)
24
  {
48
  {
25
	  string fullpath = path + "/" + file;
49
    string fullpath = path + "/" + file;
26
	  ifstream is(fullpath.c_str(), ios::binary);
50
    ifstream is(fullpath.c_str(), ios::binary);
27
	  string str, contents;
51
    string str, contents;
28
	  while(getline(is,str)) 
52
    while(getline(is,str)) 
29
	  {
53
    {
30
		  contents += str;
54
      contents += str;
31
		  contents += "\n";
55
      contents += "\n";
32
	  }
56
    }
33
	  return contents;
57
    return contents;
34
  }
58
  }
35
 
59
 
36
  GLuint create_glsl_shader(GLuint stype, const std::string& src)
60
  GLuint create_glsl_shader(GLuint stype, const std::string& src, bool print_log)
37
  {
61
  {
38
	  GLuint S = glCreateShader(stype);
62
    GLuint S = glCreateShader(stype);
39
	  const GLchar* src_cstr = src.c_str();
63
    const GLchar* src_cstr = src.c_str();
40
	  glShaderSource(S, 1, &src_cstr, 0);
64
    glShaderSource(S, 1, &src_cstr, 0);
41
	  glCompileShader(S);
65
    glCompileShader(S);
42
  	
-
 
43
	  GLint status;
-
 
44
	  glGetShaderiv(S,GL_COMPILE_STATUS, &status);
-
 
45
	  if (status == 0) 
-
 
46
	  {
66
    
47
		  GLint infologLength = 0;
-
 
48
		  glGetShaderiv(S, GL_INFO_LOG_LENGTH, &infologLength);
-
 
49
		  if (infologLength > 0)
67
    if(print_log && !check_glsl_shader(S))
50
		  {
68
    {
51
			  char* infoLog = new char[infologLength];
69
      print_glsl_shader_log(S);
52
			  glGetShaderInfoLog(S, infologLength, 0, infoLog);
-
 
53
			  cout << "InfoLog: " << infoLog << endl;
-
 
54
			  delete infoLog;
70
      glDeleteShader(S);
55
		  }
-
 
56
		  return 0;
71
      return 0;
57
	  }
72
    }
58
	  return S;
73
    return S;
59
  }
74
  }
60
 
75
 
61
  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)
62
  {
77
  {
63
	  string str = read_glsl_source(path, file);
78
    string str = read_glsl_source(path, file);
64
	  if(str != "")
79
    if(!str.empty())
-
 
80
    {
65
		  return create_glsl_shader(stype, str);
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
    }
66
	  return 0;
91
    return 0;
67
  }
92
  }
68
}
93
}