File: gpuShader.cpp

package info (click to toggle)
meshlab 1.3.2+dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (156 lines) | stat: -rw-r--r-- 4,444 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/****************************************************************************
* Render Radiance Scaling                                                   *
* Meshlab's plugin                                                          *
*                                                                           *
* Copyright(C) 2010                                                         *
* Vergne Romain, Dumas Olivier                                              *
* INRIA - Institut Nationnal de Recherche en Informatique et Automatique    *
*                                                                           *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdlib.h>

#include <QtGui>
#include <QTextStream>
#include <QString>
#include <QFile>

#include "gpuShader.h"

using namespace std;
      
GPUShader::GPUShader(SHADER_TYPE type,const string &filename,bool printLog)
  : _filename(filename),
    _type(type),
    _shaderId(0),
    _printLog(printLog) {
  _created = createShader();

  loadAndCompile();       
}
            
GPUShader::~GPUShader() {
  if(_created){
    glDeleteShader(_shaderId);
  }
}

bool GPUShader::load() {
  QString res;
  QFile f(QString(_filename.c_str()));

  if (!f.open(QFile::ReadOnly)) {
    std::cerr << "failed to load shader file " << _filename << "\n";
    return false;
  }

  QTextStream stream(&f);
  res = stream.readAll();
  f.close();

  string tmp = res.toStdString();
  const char *s = tmp.c_str();

  glShaderSource(_shaderId,1,&s,NULL);
  
  return true;
}
  

bool GPUShader::compile() { 
  glCompileShader(_shaderId);

  if(_printLog)
    printInfoLog();
  
  return true;
}

void GPUShader::printInfoLog() {
  int   infologLength = 0;
  int   charsWritten  = 0;
  char* infolog;
    
  glGetObjectParameterivARB(_shaderId,GL_OBJECT_INFO_LOG_LENGTH_ARB,&infologLength);
    
  if(infologLength>0) {
    infolog = (char*)malloc(infologLength);
    glGetInfoLogARB(_shaderId,infologLength,&charsWritten,infolog);
    if(infolog[0]!='\0') {
      printf("InfoLog ---> %s\n",_filename.c_str());
      printf("%s",infolog);
    }
    free(infolog);
  }
}

bool GPUShader::loadAndCompile() {
  return _created && load() && compile();
}

bool GPUShader::createShader() {
  switch(_type) {

  case VERT:
    if(GLEW_ARB_vertex_shader)
      _shaderId = glCreateShader(GL_VERTEX_SHADER);
    else { 
      cout << "Warning : vertex shader not supported !" << endl; 
      return false;
    }
    break;
      
  case FRAG:
    if(GLEW_ARB_fragment_shader){
      _shaderId = glCreateShader(GL_FRAGMENT_SHADER);
    }
    else { 
      cout << "Warning : fragment shader not supported !" << endl; 
      return false;
    }
    break;
      
  case GEOM:
#ifdef GL_EXT_geometry_shader4
    if(GL_EXT_geometry_shader4){
      _shaderId = glCreateShader(GL_GEOMETRY_SHADER_EXT);
    }
    else { 
      cout << "Warning : geometry shader not supported !" << endl;  
      return false;
    }
#else 
    cout << "Warning : geometry shader not supported !" << endl;  
    return false;
#endif
    break;
      
  default:
    cout << "Warning : unknown shader type !" << endl;
    return false;
    break;
  }
    
  if(_shaderId==0) {
    cout << "Warning : shader " << _filename << " is not created !" << endl;
    return false;
  }
      
  return true;
}