File: LoadShaderFromFile.m

package info (click to toggle)
psychtoolbox-3 3.0.9%2Bsvn2579.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 63,408 kB
  • sloc: ansic: 73,310; cpp: 11,139; objc: 3,129; sh: 1,669; python: 382; php: 272; makefile: 172; java: 113
file content (92 lines) | stat: -rwxr-xr-x 2,605 bytes parent folder | download
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
function handle = LoadShaderFromFile(filename, shadertype, debug)
% handle = LoadShaderFromFile(filename [, shadertype] [, debug])
%
% Loads a GLSL OpenGL shader definition from textfile 'filename' and
% creates an OpenGL GLSL shader of type 'shadertype'. Returns a handle to
% the new shader. If shadertype is omitted, the type of the shader is
% derived from the filename extension: .vert == GL_VERTEX_SHADER, .frag ==
% GL_FRAGMENT_SHADER. The optional 'debug' flag allows to enable debugging
% output.
%
% On successfull load & creation, a 'handle' to the new shader is returned.
%

% 29-Mar-2006 written by MK
% 29-Jul-2009 Bugfixes by MK: debug must default to 1, not 2!

global GL;

if isempty(GL)
    InitializeMatlabOpenGL([],[],1);
end;

% Make sure we run on a GLSL capable system.
AssertGLSL;

if nargin < 3
    debug = 1;
end;

if nargin < 1 
    filename = [];
end

if isempty(filename)
    % Set default shader file name:
    error('LoadShaderFromFile() called without any shader filename!');
end;


% Add path to our own shader directory if no path given:
if isempty(fileparts(filename))
    % No path to the shader defined as part of the filename. Add our
    % default path to our standard shader directory:
    filename = [ PsychtoolboxRoot 'PsychOpenGL/PsychGLSLShaders/' filename ];
end;

if nargin < 2
    shadertype = [];
end

if isempty(shadertype)
    % Try to autodetect shadertype from file extension:
    if ~isempty(strfind(filename, '.frag'))
        shadertype = GL.FRAGMENT_SHADER;
        if debug>0, fprintf('Building a fragment shader:'); end;
    elseif ~isempty(strfind(filename, '.vert'))
        shadertype = GL.VERTEX_SHADER;
        if debug>0, fprintf('Building a vertex shader:'); end;
    else
        error('No shadertype provided and could not derive type from filename extension!');
    end;
end;

if debug>0
    fprintf('Reading shader from file %s ...\n', filename);
end;

% Read shader source code from file:
[fid errmsg]=fopen(filename, 'rt');
if fid<0
    error('Could not open shader definition file %s [%s]!', filename, errmsg);
end;
shadersrc = fread(fid);
fclose(fid);

% Create shader, assign sourcecode and compile it:
handle = glCreateShader(shadertype);
if handle <= 0
    fprintf('The handle created by glCreateShader is %i -- An invalid handle!\n', handle);
    error('LoadShaderFromFile: glCreateShader failed to create a valid shader! Something is wrong with your graphics drivers!');
end

if debug > 1
    glShaderSource(handle, shadersrc, debug);
else
    glShaderSource(handle, shadersrc);
end;

glCompileShader(handle);

% Done.
return;