vtkDepthImageProcessingPass.cxx 6.75 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
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
/*=========================================================================

   Program: ParaView
   Module:    vtkDepthImageProcessingPass.cxx

   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
   All rights reserved.

   ParaView is a free software; you can redistribute it and/or modify it
   under the terms of the ParaView license version 1.2.

   See License_v1.2.txt for the full ParaView license.
   A copy of this license can be obtained by contacting
   Kitware Inc.
   28 Corporate Drive
   Clifton Park, NY 12065
   USA

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/
/*----------------------------------------------------------------------
Acknowledgement:
This algorithm is the result of joint work by Electricité de France,
CNRS, Collège de France and Université J. Fourier as part of the
Ph.D. thesis of Christian BOUCHENY.
------------------------------------------------------------------------*/

#include "vtkDepthImageProcessingPass.h"
#include "vtkObjectFactory.h"
Sean McBride's avatar
Sean McBride committed
41
#include <cassert>
Ken Martin's avatar
Ken Martin committed
42 43
#include "vtkRenderState.h"
#include "vtkRenderer.h"
44
#include "vtkOpenGLFramebufferObject.h"
Ken Martin's avatar
Ken Martin committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include "vtkTextureObject.h"
#include "vtkOpenGLRenderWindow.h"

#include "vtkPixelBufferObject.h"
#include "vtkCamera.h"
#include "vtkMath.h"

vtkCxxSetObjectMacro(vtkDepthImageProcessingPass,DelegatePass,vtkRenderPass);

// ----------------------------------------------------------------------------
vtkDepthImageProcessingPass::vtkDepthImageProcessingPass()
{
  this->DelegatePass = 0;
  this->Width = 0;
  this->Height = 0;
  this->W = 0;
  this->H = 0;
  this->ExtraPixels = 0;
}

// ----------------------------------------------------------------------------
vtkDepthImageProcessingPass::~vtkDepthImageProcessingPass()
{
  if(this->DelegatePass!=0)
69
  {
Ken Martin's avatar
Ken Martin committed
70 71
    this->DelegatePass->Delete();
    this->DelegatePass=0;
72
  }
Ken Martin's avatar
Ken Martin committed
73 74 75 76 77 78 79 80 81
}

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

  os << indent << "DelegatePass:";
  if(this->DelegatePass!=0)
82
  {
Ken Martin's avatar
Ken Martin committed
83
    this->DelegatePass->PrintSelf(os,indent);
84
  }
Ken Martin's avatar
Ken Martin committed
85
  else
86
  {
Ken Martin's avatar
Ken Martin committed
87
    os << "(none)" <<endl;
88
  }
Ken Martin's avatar
Ken Martin committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
}
// ----------------------------------------------------------------------------
// Description:
// Render delegate with a image of different dimensions than the
// original one.
// \pre s_exists: s!=0
// \pre fbo_exists: fbo!=0
// \pre fbo_has_context: fbo->GetContext()!=0
// \pre target_exists: target!=0
// \pre target_has_context: target->GetContext()!=0
void vtkDepthImageProcessingPass::RenderDelegate(const vtkRenderState *s,
                                            int width,
                                            int height,
                                            int newWidth,
                                            int newHeight,
104
                                            vtkOpenGLFramebufferObject *fbo,
Ken Martin's avatar
Ken Martin committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                                            vtkTextureObject *colortarget,
                                            vtkTextureObject *depthtarget)
{
  assert("pre: s_exists" && s!=0);
  assert("pre: fbo_exists" && fbo!=0);
  assert("pre: fbo_has_context" && fbo->GetContext()!=0);
  assert("pre: colortarget_exists" && colortarget!=0);
  assert("pre: colortarget_has_context" && colortarget->GetContext()!=0);
  assert("pre: depthtarget_exists" && depthtarget!=0);
  assert("pre: depthtarget_has_context" && depthtarget->GetContext()!=0);

  vtkRenderer *r=s->GetRenderer();
  vtkRenderState s2(r);
  s2.SetPropArrayAndCount(s->GetPropArray(),s->GetPropArrayCount());

  // Adapt camera to new window size
  vtkCamera *savedCamera=r->GetActiveCamera();
  savedCamera->Register(this);
  vtkCamera *newCamera=vtkCamera::New();
  newCamera->DeepCopy(savedCamera);

  r->SetActiveCamera(newCamera);

  if(newCamera->GetParallelProjection())
129
  {
Ken Martin's avatar
Ken Martin committed
130 131
    newCamera->SetParallelScale(
      newCamera->GetParallelScale()*newHeight/static_cast<double>(height));
132
  }
Ken Martin's avatar
Ken Martin committed
133
  else
134
  {
Ken Martin's avatar
Ken Martin committed
135 136 137
    double large;
    double small;
    if(newCamera->GetUseHorizontalViewAngle())
138
    {
Ken Martin's avatar
Ken Martin committed
139 140
      large=newWidth;
      small=width;
141
    }
Ken Martin's avatar
Ken Martin committed
142
    else
143
    {
Ken Martin's avatar
Ken Martin committed
144 145 146
      large=newHeight;
      small=height;

147
    }
Ken Martin's avatar
Ken Martin committed
148 149 150 151
    double angle=vtkMath::RadiansFromDegrees(newCamera->GetViewAngle());
    angle = 2.0*atan(tan(angle/2.0)*large/static_cast<double>(small));

    newCamera->SetViewAngle(vtkMath::DegreesFromRadians(angle));
152
  }
Ken Martin's avatar
Ken Martin committed
153 154 155

  s2.SetFrameBuffer(fbo);

156 157
  fbo->AddColorAttachment(
    fbo->GetDrawMode(), 0,colortarget);
Ken Martin's avatar
Ken Martin committed
158 159 160 161

  // because the same FBO can be used in another pass but with several color
  // buffers, force this pass to use 1, to avoid side effects from the
  // render of the previous frame.
162
  fbo->ActivateDrawBuffer(0);
Ken Martin's avatar
Ken Martin committed
163

164 165
  fbo->AddDepthAttachment(fbo->GetDrawMode(), depthtarget);
  fbo->StartNonOrtho(newWidth, newHeight);
Ken Martin's avatar
Ken Martin committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

  // 2. Delegate render in FBO
  //glEnable(GL_DEPTH_TEST);
  this->DelegatePass->Render(&s2);
  this->NumberOfRenderedProps+=
    this->DelegatePass->GetNumberOfRenderedProps();

  newCamera->Delete();
  r->SetActiveCamera(savedCamera);
  savedCamera->UnRegister(this);
}

// ----------------------------------------------------------------------------
// Description:
// Read window size from parent
// \pre s_exists: s!=0
//
void vtkDepthImageProcessingPass::ReadWindowSize(const vtkRenderState* s)
{
    assert("pre: s_exists" && s!=0);

187
    vtkOpenGLFramebufferObject *fbo=vtkOpenGLFramebufferObject::SafeDownCast
188
      (s->GetFrameBuffer());
Ken Martin's avatar
Ken Martin committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    vtkRenderer *r = s->GetRenderer();
    if(fbo==0)
    {
      r->GetTiledSize(&this->Width,&this->Height);
    }
    else
    {
      int size[2];
      fbo->GetLastSize(size);
      this->Width=size[0];
      this->Height=size[1];
    }
}

// ----------------------------------------------------------------------------
// Description:
// Release graphics resources and ask components to release their own
// resources.
// \pre w_exists: w!=0
void vtkDepthImageProcessingPass::ReleaseGraphicsResources(vtkWindow *w)
{
  assert("pre: w_exists" && w!=0);
  if(this->DelegatePass!=0)
212
  {
Ken Martin's avatar
Ken Martin committed
213
    this->DelegatePass->ReleaseGraphicsResources(w);
214
  }
Ken Martin's avatar
Ken Martin committed
215
}