-
Notifications
You must be signed in to change notification settings - Fork 21
/
vtkOpenGLTextureImage.cxx
369 lines (315 loc) · 10.1 KB
/
vtkOpenGLTextureImage.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: vtkOpenGLTextureImage.cxx,v $
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#include "vtkOpenGLTextureImage.h"
#include "vtkDataArray.h"
#include "vtkImageData.h"
#include "vtkMapper.h"
#include "vtkMatrix4x4.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLRenderer.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include <math.h>
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkOpenGLTextureImage);
//----------------------------------------------------------------------------
vtkOpenGLTextureImage::vtkOpenGLTextureImage()
{
this->ShaderComputation = nullptr;
this->ImageData = nullptr;
this->TextureName = 0;
this->Interpolate = 1;
this->TextureMTime = 0;
this->TextureWrap = vtkOpenGLTextureImage::ClampToEdge;
}
//----------------------------------------------------------------------------
vtkOpenGLTextureImage::~vtkOpenGLTextureImage()
{
this->SetShaderComputation(nullptr);
this->SetImageData(nullptr);
}
//----------------------------------------------------------------------------
// adapted from Rendering/OpenGL2/vtkTextureObject.cxx
GLenum vtkOpenGLTextureImage::vtkScalarTypeToGLType(int vtk_scalar_type)
{
// DON'T DEAL with VTK_CHAR as this is platform dependent.
switch (vtk_scalar_type)
{
case VTK_SIGNED_CHAR:
return GL_BYTE;
case VTK_UNSIGNED_CHAR:
return GL_UNSIGNED_BYTE;
case VTK_SHORT:
return GL_SHORT;
case VTK_UNSIGNED_SHORT:
return GL_UNSIGNED_SHORT;
case VTK_INT:
return GL_INT;
case VTK_UNSIGNED_INT:
return GL_UNSIGNED_INT;
case VTK_FLOAT:
case VTK_VOID: // used for depth component textures.
return GL_FLOAT;
}
return 0;
}
//----------------------------------------------------------------------------
// Reload the texture if needed
//
bool vtkOpenGLTextureImage::UpdateTexture()
{
if (!this->ShaderComputation || !this->ShaderComputation->GetInitialized())
{
vtkErrorMacro("No initialized ShaderComputation instance is set.");
return false;
}
this->ShaderComputation->MakeCurrent();
if (this->ImageData == nullptr)
{
return false;
}
if (this->ImageData->GetMTime() > this->TextureMTime)
{
if (this->TextureName != 0)
{
glDeleteTextures (1, &(this->TextureName) );
}
this->TextureMTime = 0;
}
else
{
return true;
}
int componentCount = this->ImageData->GetNumberOfScalarComponents();
GLenum format;
GLenum internalFormat;
if ( componentCount == 1 )
{
format = GL_RED;
internalFormat = GL_RED;
}
else if ( componentCount == 3 )
{
format = GL_RGB;
internalFormat = GL_RGB;
}
else if ( componentCount == 4 )
{
format = GL_RGBA;
internalFormat = GL_RGBA;
}
else
{
vtkErrorMacro("Must have 1, 3 or 4 component image data for texture");
return false;
}
int dimensions[3];
this->ImageData->GetDimensions(dimensions);
vtkPointData *pointData = this->ImageData->GetPointData();
vtkDataArray *scalars = pointData->GetScalars();
void *pixels = scalars->GetVoidPointer(0);
vtkOpenGLCheckErrorMacro("before uploading");
glGenTextures(1, &(this->TextureName));
glBindTexture(GL_TEXTURE_3D, this->TextureName);
if (this->Interpolate)
{
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else
{
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
if (this->TextureWrap == vtkOpenGLTextureImage::MirroredRepeat)
{
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
}
else
{
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
glTexImage3D(
/* target */ GL_TEXTURE_3D,
/* level */ 0,
/* internal format */ internalFormat,
/* width */ dimensions[0],
/* height */ dimensions[1],
/* depth */ dimensions[2],
/* border */ 0,
/* format */ format,
/* type */ vtkScalarTypeToGLType(this->ImageData->GetScalarType()),
/* pixels */ pixels
);
vtkOpenGLCheckErrorMacro("after uploading");
this->TextureMTime = this->GetMTime();
return true;
}
//----------------------------------------------------------------------------
void vtkOpenGLTextureImage::Activate(vtkTypeUInt32 unit)
{
vtkOpenGLCheckErrorMacro("before activating");
if (!this->ShaderComputation || !this->ShaderComputation->GetInitialized())
{
vtkErrorMacro("No initialized ShaderComputation instance is set.");
return;
}
this->ShaderComputation->MakeCurrent();
// TODO: check the actual number (also expose way to check the
// number from a wrapped language). For now use the minimum max value.
// of the enums, which only go to 15 even though 48 are meant to be
// supported according to the OpenGL spec.
// glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &texture_units)
#define __TEXTURE_UNIT_COUNT 16
if (unit > __TEXTURE_UNIT_COUNT-1)
{
vtkErrorMacro("Only " << __TEXTURE_UNIT_COUNT << " texture units are available.");
return;
}
if (!this->UpdateTexture())
{
vtkErrorMacro("Could not update texture.");
return;
}
// TODO:
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_3D, this->TextureName);
vtkOpenGLCheckErrorMacro("after activating");
}
//----------------------------------------------------------------------------
void vtkOpenGLTextureImage::AttachAsDrawTarget(int attachmentIndex, int layer, int attachment)
{
if (!this->ShaderComputation || !this->ShaderComputation->GetInitialized())
{
vtkErrorMacro("No initialized ShaderComputation instance is set.");
return;
}
this->ShaderComputation->MakeCurrent();
vtkOpenGLCheckErrorMacro("before attaching");
// attachment is 0 (color), 1 (depth), 2 (stencil), 3 (depth-stencil)
if (attachmentIndex != 0 || attachment != 0)
{
vtkErrorMacro("Only GL_COLOR_ATTACHMENT0 supported for now.");
return;
}
if (this->TextureName == 0)
{
if (!this->UpdateTexture())
{
vtkErrorMacro("Could not update texture.");
return;
}
}
vtkOpenGLClearErrorMacro();
int dimensions[3] = {0,0,0};
this->ImageData->GetDimensions(dimensions);
//
// Set up a normalized rendering environment
//
glViewport(0, 0, dimensions[0], dimensions[1]);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_3D, this->TextureName);
glFramebufferTextureLayer(
/* target */ GL_FRAMEBUFFER,
/* attachment */ GL_COLOR_ATTACHMENT0,
/* texture */ this->TextureName,
/* level */ 0,
/* layer */ layer);
vtkOpenGLCheckErrorMacro("after attaching");
//
// Does the GPU support current Framebuffer configuration?
//
GLenum status;
status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
switch(status)
{
case GL_FRAMEBUFFER_COMPLETE:
break;
default:
vtkOpenGLCheckErrorMacro("after bad framebuffer status");
vtkErrorMacro("Bad framebuffer configuration, status is: " << status);
return;
}
}
//----------------------------------------------------------------------------
void vtkOpenGLTextureImage::ReadBack()
{
vtkOpenGLCheckErrorMacro("before getting");
if (!this->ShaderComputation || !this->ShaderComputation->GetInitialized())
{
vtkErrorMacro("No initialized ShaderComputation instance is set.");
return;
}
this->ShaderComputation->MakeCurrent();
int componentCount = this->ImageData->GetNumberOfScalarComponents();
GLenum format;
if ( componentCount == 1 )
{
format = GL_RED;
}
else if (componentCount == 3)
{
format = GL_RGB;
}
else if ( componentCount == 4 )
{
format = GL_RGBA;
}
else
{
vtkErrorMacro("Must have 1, 3 or 4 component image data for texture");
return;
}
vtkPointData *pointData = this->ImageData->GetPointData();
vtkDataArray *scalars = pointData->GetScalars();
void *pixels = scalars->GetVoidPointer(0);
// TODO:
glBindTexture(GL_TEXTURE_3D, this->TextureName);
glGetTexImage(
/* target */ GL_TEXTURE_3D,
/* level */ 0,
/* format */ format,
/* type */ vtkScalarTypeToGLType(this->ImageData->GetScalarType()),
/* pixels */ pixels);
pointData->Modified();
vtkOpenGLCheckErrorMacro("after getting");
}
//----------------------------------------------------------------------------
void vtkOpenGLTextureImage::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
if ( this->ShaderComputation )
{
os << indent << "ShaderComputation: " << this->ShaderComputation << "\n";
}
else
{
os << indent << "ShaderComputation: (none)\n";
}
if ( this->ImageData )
{
os << indent << "ImageData: " << this->ImageData << "\n";
}
else
{
os << indent << "ImageData: (none)\n";
}
os << indent << "TextureName: " << this->TextureName << "\n";
os << indent << "TextureMTime: " << this->TextureMTime << "\n";
}