Subversion Repositories gelsvn

Rev

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