forked from drouin-simon/PRISM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkPRISMGlslShader.cxx
260 lines (229 loc) · 6.81 KB
/
vtkPRISMGlslShader.cxx
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*=========================================================================
Ibis Neuronav
Copyright (c) Simon Drouin, Anna Kochanowska, Louis Collins.
All rights reserved.
See Copyright.txt or http://ibisneuronav.org/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// Thanks to Simon Drouin for writing this class
#include "vtkPRISMGlslShader.h"
#include <iostream>
#include <stdio.h>
#include <stdarg.h>
#include "vtkgl.h"
#include "vtkMatrix4x4.h"
using namespace std;
GlslShader::GlslShader()
: m_glslShader(0)
, m_glslVertexShader(0)
, m_glslProg(0)
, m_init( false )
{
}
GlslShader::~GlslShader()
{
Clear();
}
void GlslShader::AddShaderMemSource( const char * src )
{
m_memSources.push_back( std::string( src ) );
}
void GlslShader::AddVertexShaderMemSource( const char * src )
{
m_vertexMemSources.push_back( std::string( src ) );
}
void GlslShader::Reset()
{
m_memSources.clear();
m_vertexMemSources.clear();
}
bool GlslShader::Init()
{
// Fresh start
Clear();
// Load and try compiling vertex shader
if( m_vertexMemSources.size() != 0 )
if( !CreateAndCompileShader( vtkgl::VERTEX_SHADER, m_glslVertexShader, m_vertexMemSources ) )
return false;
// Load and try compiling pixel shader
if( m_memSources.size() != 0 )
if( !CreateAndCompileShader( vtkgl::FRAGMENT_SHADER, m_glslShader, m_memSources ) )
return false;
// Check that at least one of the shaders has been compiled
if( m_glslVertexShader == 0 && m_glslShader == 0 )
return false;
// Create program object and attach shader
m_glslProg = vtkgl::CreateProgram();
if( m_glslVertexShader )
vtkgl::AttachShader( m_glslProg, m_glslVertexShader );
if( m_glslShader )
vtkgl::AttachShader( m_glslProg, m_glslShader );
// Create program and link shaders
vtkgl::LinkProgram( m_glslProg );
GLint success = 0;
vtkgl::GetProgramiv( m_glslProg, vtkgl::LINK_STATUS, &success );
if (!success)
{
GLint logLength = 0;
vtkgl::GetProgramiv( m_glslProg, vtkgl::INFO_LOG_LENGTH, &logLength );
vtkgl::GLchar * infoLog = new vtkgl::GLchar[ logLength + 1 ];
vtkgl::GetProgramInfoLog( m_glslProg, logLength, NULL, infoLog );
vtkErrorMacro( << "Error in glsl program linking:" << infoLog );
m_errorMessage = infoLog;
delete [] infoLog;
return false;
}
m_errorMessage = "";
m_init = true;
return true;
}
bool GlslShader::CreateAndCompileShader( unsigned shaderType, unsigned & shaderId, std::vector< std::string > & memSources )
{
// put all the sources in an array of const GLchar*
const vtkgl::GLchar ** shaderStringPtr = new const vtkgl::GLchar*[ memSources.size() ];
for( unsigned i = 0; i < memSources.size(); ++i )
{
shaderStringPtr[i] = memSources[i].c_str();
}
// Create the shader and set its source
shaderId = vtkgl::CreateShader( shaderType );
vtkgl::ShaderSource( shaderId, memSources.size(), shaderStringPtr, NULL);
delete [] shaderStringPtr;
// Compile the shader
GLint success = 0;
vtkgl::CompileShader( shaderId );
vtkgl::GetShaderiv( shaderId, vtkgl::COMPILE_STATUS, &success );
if (!success)
{
GLint logLength = 0;
vtkgl::GetShaderiv( shaderId, vtkgl::INFO_LOG_LENGTH, &logLength );
vtkgl::GLchar * infoLog = new vtkgl::GLchar[logLength+1];
vtkgl::GetShaderInfoLog( shaderId, logLength, NULL, infoLog);
vtkErrorMacro( << "Error in shader complilation." << infoLog );
m_errorMessage = infoLog;
delete [] infoLog;
return false;
}
m_errorMessage = "";
return true;
}
bool GlslShader::UseProgram( bool use )
{
bool res = true;
if( use && m_init )
{
vtkgl::UseProgram( m_glslProg );
}
else
{
res = true;
vtkgl::UseProgram( 0 );
}
return res;
}
bool GlslShader::SetVariable( const char * name, int value )
{
int location = vtkgl::GetUniformLocation( m_glslProg, name );
if( location != -1 )
{
vtkgl::Uniform1i( location, value );
return true;
}
return false;
}
bool GlslShader::SetVariable( const char * name, int count, int * values )
{
int location = vtkgl::GetUniformLocation( m_glslProg, name );
if( location != -1 )
{
vtkgl::Uniform1iv( location, count, values );
return true;
}
return false;
}
bool GlslShader::SetVariable( const char * name, float value )
{
int location = vtkgl::GetUniformLocation( m_glslProg, name );
if( location != -1 )
{
vtkgl::Uniform1f( location, value );
return true;
}
return false;
}
bool GlslShader::SetVariable( const char * name, double value )
{
int location = vtkgl::GetUniformLocation( m_glslProg, name );
if( location != -1 )
{
vtkgl::Uniform1d( location, value );
return true;
}
return false;
}
bool GlslShader::SetVariable( const char * name, int val1, int val2 )
{
int location = vtkgl::GetUniformLocation( m_glslProg, name );
if( location != -1 )
{
vtkgl::Uniform2i( location, val1, val2 );
return true;
}
return false;
}
bool GlslShader::SetVariable( const char * name, float val1, float val2 )
{
int location = vtkgl::GetUniformLocation( m_glslProg, name );
if( location != -1 )
{
vtkgl::Uniform2f( location, val1, val2 );
return true;
}
return false;
}
bool GlslShader::SetVariable( const char * name, float val1, float val2, float val3 )
{
int location = vtkgl::GetUniformLocation( m_glslProg, name );
if( location != -1 )
{
vtkgl::Uniform3f( location, val1, val2, val3 );
return true;
}
return false;
}
#include "vtkMatrix4x4.h"
bool GlslShader::SetVariable( const char * name, vtkMatrix4x4 * mat )
{
int location = vtkgl::GetUniformLocation( m_glslProg, name );
if( location != -1 )
{
float local[16];
for( int i = 0; i < 4; ++i )
for( int j = 0; j < 4; ++j )
local[ i * 4 + j ] = mat->Element[i][j];
vtkgl::UniformMatrix4fv( location, 1, GL_TRUE, local );
return true;
}
return false;
}
void GlslShader::Clear()
{
if( m_glslVertexShader != 0 )
{
vtkgl::DeleteShader( m_glslVertexShader );
m_glslVertexShader = 0;
}
if( m_glslShader != 0 )
{
vtkgl::DeleteShader( m_glslShader );
m_glslShader = 0;
}
if( m_glslProg != 0 )
{
vtkgl::DeleteProgram( m_glslProg );
m_glslProg = 0;
}
m_init = false;
}