Subversion Repositories gelsvn

Rev

Rev 594 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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