vtkOSPRayPass.cxx 6.95 KB
Newer Older
1
2
3
/*=========================================================================

  Program:   Visualization Toolkit
David DeMarle's avatar
David DeMarle committed
4
  Module:    vtkOSPRayPass.cxx
5
6
7
8
9
10
11
12
13
14
15

  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.

=========================================================================*/

16
17
#include "vtkCameraPass.h"
#include "vtkLightsPass.h"
18
#include "vtkObjectFactory.h"
David DeMarle's avatar
David DeMarle committed
19
20
21
#include "vtkOSPRayPass.h"
#include "vtkOSPRayRendererNode.h"
#include "vtkOSPRayViewNodeFactory.h"
22
23
24
#include "vtkOverlayPass.h"
#include "vtkOpaquePass.h"
#include "vtkRenderPassCollection.h"
25
#include "vtkRenderState.h"
26
#include "vtkRenderWindow.h"
27
#include "vtkRenderer.h"
28
29
30
31
32
33
#include "vtkSequencePass.h"
#include "vtkSequencePass.h"
#include "vtkVolumetricPass.h"

#include "ospray/ospray.h"

Carson Brownlee's avatar
Carson Brownlee committed
34
#include <sstream>
35
36
#include <stdexcept>

David DeMarle's avatar
David DeMarle committed
37
class vtkOSPRayPassInternals : public vtkRenderPass
38
39
{
public:
David DeMarle's avatar
David DeMarle committed
40
41
42
  static vtkOSPRayPassInternals *New();
  vtkTypeMacro(vtkOSPRayPassInternals,vtkRenderPass);
  vtkOSPRayPassInternals()
43
44
45
    {
    this->Factory = 0;
    }
David DeMarle's avatar
David DeMarle committed
46
  ~vtkOSPRayPassInternals()
47
48
49
50
51
52
53
    {
    this->Factory->Delete();
    }
  void Render(const vtkRenderState *s)
    {
    this->Parent->RenderInternal(s);
    }
54

David DeMarle's avatar
David DeMarle committed
55
56
  vtkOSPRayViewNodeFactory *Factory;
  vtkOSPRayPass *Parent;
57
58
59
};

// ----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
60
vtkStandardNewMacro(vtkOSPRayPassInternals);
61
62

// ----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
63
vtkStandardNewMacro(vtkOSPRayPass);
64
65

// ----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
66
vtkOSPRayPass::vtkOSPRayPass()
67
68
{
  this->SceneGraph = NULL;
69
70

  int ac = 1;
Carson Brownlee's avatar
Carson Brownlee committed
71
72
  const char* envArgs = getenv("VTKOSPRAY_ARGS");
  if (envArgs)
73
    {
Carson Brownlee's avatar
Carson Brownlee committed
74
75
76
77
78
79
80
81
    std::stringstream ss(envArgs);
    std::string arg;
    std::vector<std::string> args;
    while (ss >> arg)
      {
      args.push_back(arg);
      }
    int ac =args.size()+1;
82
    const char** av = new const char*[ac];
83
    av[0] = "pvOSPRay";
Carson Brownlee's avatar
Carson Brownlee committed
84
85
    for(int i=1;i < ac; i++)
      {
86
      av[i] = args[i - 1].c_str();
Carson Brownlee's avatar
Carson Brownlee committed
87
88
89
      }
    try
      {
90
      ospInit(&ac, av);
Carson Brownlee's avatar
Carson Brownlee committed
91
92
93
94
95
      }
    catch (std::runtime_error &vtkNotUsed(e))
      {
      //todo: request addition of ospFinalize() to ospray
      }
96
    delete [] av;
97
    }
Carson Brownlee's avatar
Carson Brownlee committed
98
  else
99
    {
Carson Brownlee's avatar
Carson Brownlee committed
100
101
102
103
104
105
106
107
108
    const char* av[] = {"pvOSPRay\0"};
    try
      {
      ospInit(&ac, av);
      }
    catch (std::runtime_error &vtkNotUsed(e))
      {
      //todo: request addition of ospFinalize() to ospray
      }
109
110
    }

David DeMarle's avatar
David DeMarle committed
111
112
  vtkOSPRayViewNodeFactory *vnf = vtkOSPRayViewNodeFactory::New();
  this->Internal = vtkOSPRayPassInternals::New();
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  this->Internal->Factory = vnf;
  this->Internal->Parent = this;

  this->CameraPass = vtkCameraPass::New();
  this->LightsPass = vtkLightsPass::New();
  this->SequencePass = vtkSequencePass::New();
  this->VolumetricPass = vtkVolumetricPass::New();
  this->OverlayPass = vtkOverlayPass::New();

  this->RenderPassCollection = vtkRenderPassCollection::New();
  this->RenderPassCollection->AddItem(this->LightsPass);
  this->RenderPassCollection->AddItem(this->Internal);
  this->RenderPassCollection->AddItem(this->OverlayPass);

  this->SequencePass->SetPasses(this->RenderPassCollection);
  this->CameraPass->SetDelegatePass(this->SequencePass);

130
131
132
}

// ----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
133
vtkOSPRayPass::~vtkOSPRayPass()
134
135
{
  this->SetSceneGraph(NULL);
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
  this->Internal->Delete();
  this->Internal = 0;
  if (this->CameraPass)
    {
    this->CameraPass->Delete();
    this->CameraPass = 0;
    }
  if (this->LightsPass)
    {
    this->LightsPass->Delete();
    this->LightsPass = 0;
    }
  if (this->SequencePass)
    {
    this->SequencePass->Delete();
    this->SequencePass = 0;
    }
  if (this->VolumetricPass)
    {
    this->VolumetricPass->Delete();
    this->VolumetricPass = 0;
    }
  if (this->OverlayPass)
    {
    this->OverlayPass->Delete();
    this->OverlayPass = 0;
    }
  if (this->RenderPassCollection)
    {
    this->RenderPassCollection->Delete();
    this->RenderPassCollection = 0;
    }
168
169
170
}

// ----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
171
void vtkOSPRayPass::PrintSelf(ostream& os, vtkIndent indent)
172
173
174
175
176
{
  this->Superclass::PrintSelf(os,indent);
}

// ----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
177
vtkCxxSetObjectMacro(vtkOSPRayPass, SceneGraph, vtkOSPRayRendererNode)
178
179

// ----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
180
void vtkOSPRayPass::Render(const vtkRenderState *s)
181
{
182
183
184
185
186
  if (!this->SceneGraph)
    {
    vtkRenderer *ren = s->GetRenderer();
    if (ren)
      {
David DeMarle's avatar
David DeMarle committed
187
      this->SceneGraph = vtkOSPRayRendererNode::SafeDownCast
188
189
190
191
192
193
194
        (this->Internal->Factory->CreateNode(ren));
      }
    }
  this->CameraPass->Render(s);
}

// ----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
195
void vtkOSPRayPass::RenderInternal(const vtkRenderState *s)
196
{
197
198
199
200
  this->NumberOfRenderedProps=0;

  if (this->SceneGraph)
    {
201
202
203
204
205
206
207
208
209
210
    this->SceneGraph->TraverseAllPasses();

    // copy the result to the window
    vtkRenderer *ren = s->GetRenderer();
    vtkRenderWindow *rwin =
      vtkRenderWindow::SafeDownCast(ren->GetVTKWindow());
    int viewportX, viewportY;
    int viewportWidth, viewportHeight;
    ren->GetTiledSizeAndOrigin(&viewportWidth,&viewportHeight,
                                &viewportX,&viewportY);
211
212
    vtkOSPRayRendererNode* oren= vtkOSPRayRendererNode::SafeDownCast
      (this->SceneGraph->GetViewNodeFor(ren));
213
214
215
216
217
218
219
220
221
222
223
224
225
    int layer = ren->GetLayer();
    if (layer == 0)
      {
      rwin->SetZbufferData(
        viewportX,  viewportY,
        viewportX+viewportWidth-1,
        viewportY+viewportHeight-1,
        this->SceneGraph->GetZBuffer());
      rwin->SetRGBACharPixelData(
        viewportX,  viewportY,
        viewportX+viewportWidth-1,
        viewportY+viewportHeight-1,
        this->SceneGraph->GetBuffer(),
226
        0, vtkOSPRayRendererNode::GetCompositeOnGL(ren) );
227
228
229
      }
    else
      {
230

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
      float *ontoZ = rwin->GetZbufferData
        (viewportX,  viewportY,
         viewportX+viewportWidth-1,
         viewportY+viewportHeight-1);
      unsigned char *ontoRGBA = rwin->GetRGBACharPixelData
        (viewportX,  viewportY,
         viewportX+viewportWidth-1,
         viewportY+viewportHeight-1,
         0);
      oren->WriteLayer(ontoRGBA, ontoZ, viewportWidth, viewportHeight, layer);
      rwin->SetZbufferData(
         viewportX,  viewportY,
         viewportX+viewportWidth-1,
         viewportY+viewportHeight-1,
         ontoZ);
      rwin->SetRGBACharPixelData(
         viewportX,  viewportY,
         viewportX+viewportWidth-1,
         viewportY+viewportHeight-1,
         ontoRGBA,
251
         0, vtkOSPRayRendererNode::GetCompositeOnGL(ren) );
252
253
254
      delete[] ontoZ;
      delete[] ontoRGBA;
      }
255
256
    }
}