File: vtkOpenGLMovieSphere.cxx

package info (click to toggle)
paraview 5.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 497,236 kB
  • sloc: cpp: 3,171,290; ansic: 1,315,072; python: 134,290; xml: 103,324; sql: 65,887; sh: 5,286; javascript: 4,901; yacc: 4,383; java: 3,977; perl: 2,363; lex: 1,909; f90: 1,255; objc: 143; makefile: 119; tcl: 59; pascal: 50; fortran: 29
file content (308 lines) | stat: -rw-r--r-- 12,346 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
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkOpenGLMovieSphere.cxx

  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 "vtkOpenGLMovieSphere.h"

#include "vtkCamera.h"
#include "vtkCommand.h"
#include "vtkFFMPEGVideoSource.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLActor.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLPolyDataMapper.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLRenderer.h"
#include "vtkOpenGLShaderProperty.h"
#include "vtkOpenGLState.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkProperty.h"
#include "vtkRenderWindow.h"
#include "vtkShaderProgram.h"
#include "vtkTexture.h"
#include "vtkTextureObject.h"

#include <cmath>

VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkOpenGLMovieSphere);

vtkOpenGLMovieSphere::vtkOpenGLMovieSphere()
{
  for (int i = 0; i < 6; ++i)
  {
    this->TextureData[i] = nullptr;
  }
  this->ReadIndex = 0;
  this->WriteIndex = 3;
  this->BuildIndex = 0;
  this->DrawIndex = 3;
  this->Height = 0;
  this->Width = 0;
  this->NewData = 0;
  this->HaveData = 0;

  this->CubeMapper->RemoveAllObservers();
  this->CubeMapper->AddObserver(
    vtkCommand::UpdateShaderEvent, this, &vtkOpenGLMovieSphere::UpdateUniforms);
}

vtkOpenGLMovieSphere::~vtkOpenGLMovieSphere()
{
  if (this->VideoSource)
  {
    this->VideoSource->SetVideoCallback(nullptr, nullptr);
  }
  for (int i = 0; i < 6; ++i)
  {
    delete[] this->TextureData[i];
  }
}

void vtkOpenGLMovieSphere::UpdateUniforms(vtkObject* a, unsigned long eid, void* calldata)
{
  this->Superclass::UpdateUniforms(a, eid, calldata);
  vtkShaderProgram* program = reinterpret_cast<vtkShaderProgram*>(calldata);

  program->SetUniformi("YTexture", this->YTexture);
  program->SetUniformi("UTexture", this->UTexture);
  program->SetUniformi("VTexture", this->VTexture);
}

vtkFFMPEGVideoSource* vtkOpenGLMovieSphere::GetVideoSource()
{
  return this->VideoSource.Get();
}

void vtkOpenGLMovieSphere::SetVideoSource(vtkFFMPEGVideoSource* video)
{
  if (this->VideoSource == video)
  {
    return;
  }

  if (this->VideoSource)
  {
    this->VideoSource->SetVideoCallback(nullptr, nullptr);
  }

  this->VideoSource = video;

  this->NewData = 0;
  this->HaveData = 0;

  video->SetVideoCallback(
    std::bind(&vtkOpenGLMovieSphere::VideoCallback, this, std::placeholders::_1), nullptr);
  this->Modified();
}

void vtkOpenGLMovieSphere::VideoCallback(vtkFFMPEGVideoSourceVideoCallbackData const& cbd)
{
  // make sure the data is allocated
  int* fsize = cbd.Caller->GetFrameSize();
  if (fsize[1] != this->Height)
  {
    const std::lock_guard<std::mutex> lock(this->TextureUpdateMutex);
    (void)lock;
    for (int i = 0; i < 6; ++i)
    {
      delete[] this->TextureData[i];
    }
    this->Height = fsize[1];
    this->Width = fsize[0];
    this->TextureData[0] = new unsigned char[this->Height * this->Width];
    this->TextureData[3] = new unsigned char[this->Height * this->Width];
    this->UVHeight = this->Height / 2;
    this->UVWidth = this->Width / 2;
    this->TextureData[1] = new unsigned char[this->UVHeight * this->UVWidth];
    this->TextureData[2] = new unsigned char[this->UVHeight * this->UVWidth];
    this->TextureData[4] = new unsigned char[this->UVHeight * this->UVWidth];
    this->TextureData[5] = new unsigned char[this->UVHeight * this->UVWidth];
  }

  // copy each row due to linesize possibly being larger than width
  for (int i = 0; i < this->Height; ++i)
  {
    memcpy(this->TextureData[this->WriteIndex] + this->Width * i, cbd.Data[0] + i * cbd.LineSize[0],
      this->Width);
  }
  for (int i = 0; i < this->UVHeight; ++i)
  {
    memcpy(this->TextureData[this->WriteIndex + 1] + this->UVWidth * i,
      cbd.Data[1] + i * cbd.LineSize[1], this->UVWidth);
    memcpy(this->TextureData[this->WriteIndex + 2] + this->UVWidth * i,
      cbd.Data[2] + i * cbd.LineSize[2], this->UVWidth);
  }

  {
    const std::lock_guard<std::mutex> lock(this->TextureUpdateMutex);
    (void)lock;
    this->ReadIndex = this->WriteIndex;
  }
  this->WriteIndex = this->ReadIndex ? 0 : 3;
  this->NewData = 1;
  this->HaveData = 1;
}

// Actual Skybox render method.
void vtkOpenGLMovieSphere::Render(vtkRenderer* ren, vtkMapper* mapper)
{
  vtkOpenGLClearErrorMacro();

  if (this->LastProjection != this->Projection)
  {
    vtkOpenGLShaderProperty* sp =
      vtkOpenGLShaderProperty::SafeDownCast(this->OpenGLActor->GetShaderProperty());
    if (this->Projection == vtkSkybox::Sphere)
    {
      // Replace VTK fragment shader
      sp->SetFragmentShaderCode("//VTK::System::Dec\n" // always start with this line
                                "//VTK::Output::Dec\n" // always have this line in your FS
                                "in vec3 TexCoords;\n"
                                "uniform vec3 cameraPos;\n" // wc camera position
                                "uniform sampler2D YTexture;\n"
                                "uniform sampler2D UTexture;\n"
                                "uniform sampler2D VTexture;\n"
                                "uniform vec4 floorPlane;\n" // floor plane eqn
                                "uniform vec3 floorRight;\n" // floor plane right
                                "uniform vec3 floorFront;\n" // floor plane front
                                "void main () {\n"
                                "  vec3 diri = normalize(TexCoords - cameraPos);\n"
                                "  vec3 dirv = vec3(dot(diri,floorRight),\n"
                                "    dot(diri,floorPlane.xyz),\n"
                                "    dot(diri,floorFront));\n"
                                "  float phix = length(vec2(dirv.x, dirv.z));\n"
                                "  vec2 tval = vec2(0.5*atan(dirv.x, dirv.z)/3.1415927 + 0.5, 1.0 "
                                "- atan(dirv.y,phix)/3.1415927 - 0.5);\n"
                                // See https://www.fourcc.org/fccyvrgb.php note we use the
                                // Video Demystified version which I believe is correct for
                                // ffmpeg going from yuv NOT in the full range of 0 to 1.0
                                // (due to the 16,235 limits) to RGB IN the full range of
                                // 0 to 1.0.
                                "  float y = 1.164*(texture2D(YTexture, tval).r - 0.0627);\n"
                                "  float u = texture2D(UTexture, tval).r - 0.5;\n"
                                "  float v = texture2D(VTexture, tval).r - 0.5;\n"
                                "  float r = y + 1.596 * v;\n"
                                "  float g = y - 0.391 * u - 0.813 * v;\n"
                                "  float b = y + 2.018 * u;\n"
                                "  gl_FragData[0] = vec4(r,g,b,1.0);\n"
                                "}\n");
    }
    if (this->Projection == vtkSkybox::StereoSphere)
    {
      // Replace VTK fragment shader
      sp->SetFragmentShaderCode("//VTK::System::Dec\n" // always start with this line
                                "//VTK::Output::Dec\n" // always have this line in your FS
                                "in vec3 TexCoords;\n"
                                "uniform vec3 cameraPos;\n" // wc camera position
                                "uniform sampler2D YTexture;\n"
                                "uniform sampler2D UTexture;\n"
                                "uniform sampler2D VTexture;\n"
                                "uniform vec4 floorPlane;\n" // floor plane eqn
                                "uniform vec3 floorRight;\n" // floor plane right
                                "uniform vec3 floorFront;\n" // floor plane front
                                "uniform float leftEye;\n"   // 1.0 for left, 0.0 for right
                                "void main () {\n"
                                "  vec3 diri = normalize(TexCoords - cameraPos);\n"
                                "  vec3 dirv = vec3(dot(diri,floorRight),\n"
                                "    dot(diri,floorPlane.xyz),\n"
                                "    dot(diri,floorFront));\n"
                                "  float phix = length(vec2(dirv.x, dirv.z));\n"
                                "  vec2 tval = vec2(0.5*atan(dirv.x, dirv.z)/3.1415927 + 0.5, 1.0 "
                                "- 0.5*atan(dirv.y,phix)/3.1415927 - 0.25 + 0.5*leftEye);\n"
                                // See https://www.fourcc.org/fccyvrgb.php note we use the
                                // Video Demystified version which I believe is correct for
                                // ffmpeg going from yuv NOT in the full range of 0 to 1.0
                                // (due to the 16,235 limits) to RGB IN the full range of
                                // 0 to 1.0.
                                "  float y = 1.164*(texture2D(YTexture, tval).r - 0.0627);\n"
                                "  float u = texture2D(UTexture, tval).r - 0.5;\n"
                                "  float v = texture2D(VTexture, tval).r - 0.5;\n"
                                "  float r = y + 1.596 * v;\n"
                                "  float g = y - 0.391 * u - 0.813 * v;\n"
                                "  float b = y + 2.018 * u;\n"
                                "  gl_FragData[0] = vec4(r,g,b,1.0);\n"
                                "}\n");
    }
    this->CubeMapper->Modified();
    this->LastProjection = this->Projection;
  }

  double* pos = ren->GetActiveCamera()->GetPosition();
  this->LastCameraPosition[0] = pos[0];
  this->LastCameraPosition[1] = pos[1];
  this->LastCameraPosition[2] = pos[2];

  this->CurrentRenderer = ren;

  // get opacity
  static_cast<vtkOpenGLRenderer*>(ren)->GetState()->vtkglDepthMask(GL_TRUE);
  static_cast<vtkOpenGLRenderer*>(ren)->GetState()->vtkglDepthFunc(GL_LEQUAL);

  vtkOpenGLRenderWindow* renWin = static_cast<vtkOpenGLRenderWindow*>(ren->GetVTKWindow());

  if (this->Textures[0]->GetHandle() == 0)
  {
    for (int i = 0; i < 6; ++i)
    {
      this->Textures[i]->SetContext(renWin);
      this->Textures[i]->SetMinificationFilter(vtkTextureObject::Linear);
      this->Textures[i]->SetMagnificationFilter(vtkTextureObject::Linear);
    }
  }

  // send a render to the mapper; update pipeline
  if (this->HaveData.load() == 0)
  {
    return;
  }

  if (this->NewData.load() == 1)
  {
    const std::lock_guard<std::mutex> lock(this->TextureUpdateMutex);
    (void)lock;
    this->Textures[this->BuildIndex]->Create2DFromRaw(
      this->Width, this->Height, 1, VTK_UNSIGNED_CHAR, this->TextureData[this->ReadIndex]);
    this->Textures[this->BuildIndex + 1]->Create2DFromRaw(
      this->UVWidth, this->UVHeight, 1, VTK_UNSIGNED_CHAR, this->TextureData[this->ReadIndex + 1]);
    this->Textures[this->BuildIndex + 2]->Create2DFromRaw(
      this->UVWidth, this->UVHeight, 1, VTK_UNSIGNED_CHAR, this->TextureData[this->ReadIndex + 2]);
    this->NewData = 0;
    this->DrawIndex = this->BuildIndex;
    this->BuildIndex = this->DrawIndex ? 0 : 3;
  }

  this->Textures[this->DrawIndex]->Activate();
  this->Textures[this->DrawIndex + 1]->Activate();
  this->Textures[this->DrawIndex + 2]->Activate();

  this->YTexture = this->Textures[this->DrawIndex]->GetTextureUnit();
  this->UTexture = this->Textures[this->DrawIndex + 1]->GetTextureUnit();
  this->VTexture = this->Textures[this->DrawIndex + 2]->GetTextureUnit();

  mapper->Render(ren, this->OpenGLActor);

  this->Textures[this->DrawIndex]->Deactivate();
  this->Textures[this->DrawIndex + 1]->Deactivate();
  this->Textures[this->DrawIndex + 2]->Deactivate();

  vtkOpenGLCheckErrorMacro("failed after Render");
}

//----------------------------------------------------------------------------
void vtkOpenGLMovieSphere::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}
VTK_ABI_NAMESPACE_END