Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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