Subversion Repositories gelsvn

Rev

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

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