Subversion Repositories gelsvn

Rev

Rev 630 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
594 jab 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
 
369 jab 7
#include <iostream>
8
#include <fstream>
9
#include <string>
601 jab 10
#include "../GL/glew.h"
369 jab 11
using namespace std;
12
 
373 jrf 13
namespace GLGraphics
369 jab 14
{
502 jrf 15
  void print_glsl_program_log(GLuint program)
373 jrf 16
  {
502 jrf 17
    GLint infoLog_length = 0;
18
    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLog_length);
19
 
503 jrf 20
    // Some drivers return 1 as infoLog_length when the infoLog is an empty string
502 jrf 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
    }
373 jrf 28
  }
369 jab 29
 
502 jrf 30
  void print_glsl_shader_log(GLuint shader)
31
  {
32
    GLint infoLog_length = 0;
33
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLog_length);
34
 
503 jrf 35
    // Some drivers return 1 as infoLog_length when the infoLog is an empty string
502 jrf 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
 
373 jrf 52
  const std::string read_glsl_source(const std::string& path, const std::string& file)
53
  {
502 jrf 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;
373 jrf 63
  }
369 jab 64
 
502 jrf 65
  GLuint create_glsl_shader(GLuint stype, const std::string& src, bool print_log)
373 jrf 66
  {
502 jrf 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;
373 jrf 79
  }
369 jab 80
 
373 jrf 81
  GLuint create_glsl_shader(GLuint stype, const std::string& path, const std::string& file)
82
  {
502 jrf 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;
373 jrf 97
  }
369 jab 98
}