JeVoisBase  1.0
JeVois Smart Embedded Machine Vision Toolkit Base Modules
GPUshader.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
19 
20 // ####################################################################################################
22  Src(nullptr), Id(0)
23 { }
24 
25 // ####################################################################################################
27 {
28  if (Id) glDeleteShader(Id);
29  if (Src) delete[] Src;
30 }
31 
32 // ####################################################################################################
33 GLuint GPUshader::id() const
34 { return Id; }
35 
36 // ####################################################################################################
37 void GPUshader::load(char const * filename, GLuint type)
38 {
39  if (Src) delete [] Src;
40  if (Id) glDeleteShader(Id);
41 
42  // Cheeky bit of code to read the whole file into memory:
43  FILE * f = fopen(filename, "rb"); if (f == nullptr) PLFATAL("Failed to read file " << filename);
44  fseek(f, 0, SEEK_END);
45  int sz = ftell(f);
46  fseek(f, 0, SEEK_SET);
47  Src = new GLchar[sz+1];
48  fread(Src, 1, sz, f);
49  Src[sz] = 0; // null terminate it
50  fclose(f);
51 
52  // now create and compile the shader
53  GL_CHECK(Id = glCreateShader(type));
54  GL_CHECK(glShaderSource(Id, 1, (const GLchar**)&Src, 0));
55  GL_CHECK(glCompileShader(Id));
56 
57  // Compilation check
58  GLint compiled; glGetShaderiv(Id, GL_COMPILE_STATUS, &compiled);
59  if (compiled == 0)
60  {
61  GLint loglen = 2048; char log[loglen];
62  glGetShaderInfoLog(Id, loglen, &loglen, &log[0]); log[loglen] = '\0';
63  LERROR("Failed to compile shader from file " << filename << ", Log: " << &log[0]);
64  glDeleteShader(Id);
65  }
66  LINFO("Compiled shader from file " << filename);
67 }
#define PLFATAL(msg)
#define LERROR(msg)
GPUshader()
Constructor.
Definition: GPUshader.C:21
~GPUshader()
Destructor.
Definition: GPUshader.C:26
void load(char const *filename, GLuint type)
Load a shader from file.
Definition: GPUshader.C:37
#define LINFO(msg)
#define GL_CHECK(stmt)
Simple macro to check for OpenGL errors.
Definition: OpenGL.H:49
GLuint id() const
Get the shader's ID.
Definition: GPUshader.C:33