vtkImageProcessingPass.cxx 6.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkImageProcessingPass.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.

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

16 17
#include "vtk_glew.h"

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
#include "vtkImageProcessingPass.h"
#include "vtkObjectFactory.h"
#include <cassert>
#include "vtkRenderState.h"
#include "vtkRenderer.h"
#include "vtkFrameBufferObject.h"
#include "vtkTextureObject.h"
#include "vtkOpenGLRenderWindow.h"

// to be able to dump intermediate passes into png files for debugging.
// only for vtkImageProcessingPass developers.
//#define VTK_IMAGE_PROCESSING_PASS_DEBUG

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

vtkCxxSetObjectMacro(vtkImageProcessingPass,DelegatePass,vtkRenderPass);


// ----------------------------------------------------------------------------
vtkImageProcessingPass::vtkImageProcessingPass()
{
  this->DelegatePass=0;
}

// ----------------------------------------------------------------------------
vtkImageProcessingPass::~vtkImageProcessingPass()
{
  if(this->DelegatePass!=0)
48
  {
49
      this->DelegatePass->Delete();
50
  }
51 52 53 54 55 56 57 58 59
}

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

  os << indent << "DelegatePass:";
  if(this->DelegatePass!=0)
60
  {
61
    this->DelegatePass->PrintSelf(os,indent);
62
  }
63
  else
64
  {
65
    os << "(none)" <<endl;
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
}
// ----------------------------------------------------------------------------
// 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 vtkImageProcessingPass::RenderDelegate(const vtkRenderState *s,
                                            int width,
                                            int height,
                                            int newWidth,
                                            int newHeight,
                                            vtkFrameBufferObject *fbo,
                                            vtkTextureObject *target)
{
  assert("pre: s_exists" && s!=0);
  assert("pre: fbo_exists" && fbo!=0);
  assert("pre: fbo_has_context" && fbo->GetContext()!=0);
  assert("pre: target_exists" && target!=0);
  assert("pre: target_has_context" && target->GetContext()!=0);

#ifdef VTK_IMAGE_PROCESSING_PASS_DEBUG
  cout << "width=" << width << endl;
  cout << "height=" << height << endl;
  cout << "newWidth=" << newWidth << endl;
  cout << "newHeight=" << newHeight << endl;
#endif

  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);

#ifdef VTK_IMAGE_PROCESSING_PASS_DEBUG
  cout << "old camera params=";
  savedCamera->Print(cout);
  cout << "new camera params=";
  newCamera->Print(cout);
#endif
  r->SetActiveCamera(newCamera);

  if(newCamera->GetParallelProjection())
117
  {
118 119
    newCamera->SetParallelScale(
      newCamera->GetParallelScale()*newHeight/static_cast<double>(height));
120
  }
121
  else
122
  {
123 124 125
    double large;
    double small;
    if(newCamera->GetUseHorizontalViewAngle())
126
    {
127 128
      large=newWidth;
      small=width;
129
    }
130
    else
131
    {
132 133 134
      large=newHeight;
      small=height;

135
    }
136 137 138 139 140 141 142 143 144 145 146 147 148
    double angle=vtkMath::RadiansFromDegrees(newCamera->GetViewAngle());

#ifdef VTK_IMAGE_PROCESSING_PASS_DEBUG
    cout << "old angle =" << angle << " rad="<< vtkMath::DegreesFromRadians(angle) << " deg" <<endl;
#endif

    angle = 2.0*atan(tan(angle/2.0)*large/static_cast<double>(small));

#ifdef VTK_IMAGE_PROCESSING_PASS_DEBUG
    cout << "new angle =" << angle << " rad="<< vtkMath::DegreesFromRadians(angle) << " deg" <<endl;
#endif

    newCamera->SetViewAngle(vtkMath::DegreesFromRadians(angle));
149
  }
150 151 152 153 154

  s2.SetFrameBuffer(fbo);

  if(target->GetWidth()!=static_cast<unsigned int>(newWidth) ||
       target->GetHeight()!=static_cast<unsigned int>(newHeight))
155
  {
156
      target->Create2D(newWidth,newHeight,4,VTK_UNSIGNED_CHAR,false);
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

  fbo->SetNumberOfRenderTargets(1);
  fbo->SetColorBuffer(0,target);

  // 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.
  fbo->SetActiveBuffer(0);

  fbo->SetDepthBufferNeeded(true);
  fbo->StartNonOrtho(newWidth,newHeight,false);
  glViewport(0, 0, newWidth, newHeight);
  glScissor(0, 0, newWidth, newHeight);

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

#ifdef VTK_IMAGE_PROCESSING_PASS_DEBUG
  vtkPixelBufferObject *pbo=target->Download();

  unsigned int dims[2];
  vtkIdType continuousInc[3];

  dims[0]=static_cast<unsigned int>(newWidth);
  dims[1]=static_cast<unsigned int>(newHeight);
  continuousInc[0]=0;
  continuousInc[1]=0;
  continuousInc[2]=0;

  int byteSize=newWidth*newHeight*4*sizeof(float);
  float *buffer=new float[newWidth*newHeight*4];
  pbo->Download2D(VTK_FLOAT,buffer,dims,4,continuousInc);

  vtkImageImport *importer=vtkImageImport::New();
  importer->CopyImportVoidPointer(buffer,static_cast<int>(byteSize));
  importer->SetDataScalarTypeToFloat();
  importer->SetNumberOfScalarComponents(4);
  importer->SetWholeExtent(0,newWidth-1,0,newHeight-1,0,0);
  importer->SetDataExtentToWholeExtent();

  importer->Update();

  vtkPNGWriter *writer=vtkPNGWriter::New();
  writer->SetFileName("ip.png");
  writer->SetInputConnection(importer->GetOutputPort());
  importer->Delete();
  cout << "Writing " << writer->GetFileName() << endl;
  writer->Write();
  cout << "Wrote " << writer->GetFileName() << endl;
  writer->Delete();

  pbo->Delete();
  delete[] buffer;
#endif

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

// ----------------------------------------------------------------------------
// Description:
// Release graphics resources and ask components to release their own
// resources.
// \pre w_exists: w!=0
void vtkImageProcessingPass::ReleaseGraphicsResources(vtkWindow *w)
{
  assert("pre: w_exists" && w!=0);
  if(this->DelegatePass!=0)
230
  {
231
    this->DelegatePass->ReleaseGraphicsResources(w);
232
  }
233
}