<commit_before>/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkCameraPass.cxx

  Copyright (c) <NAME>, <NAME>, <NAME>
  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 "vtkCameraPass.h"
#include "vtkObjectFactory.h"
#include <assert.h>
#include "vtkRenderState.h"
#include "vtkRenderer.h"
#include "vtkgl.h"

vtkCxxRevisionMacro(vtkCameraPass, "1.1");
vtkStandardNewMacro(vtkCameraPass);
vtkCxxSetObjectMacro(vtkCameraPass,DelegatePass,vtkRenderPass);

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

// ----------------------------------------------------------------------------
vtkCameraPass::~vtkCameraPass()
{
  if(this->DelegatePass!=0)
    {
      this->DelegatePass->Delete();
    }
}

// ----------------------------------------------------------------------------
void vtkCameraPass::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
  
  os << indent << "DelegatePass:";
  if(this->DelegatePass!=0)
    {
    this->DelegatePass->PrintSelf(os,indent);
    }
  else
    {
    os << "(none)" <<endl;
    }
}

// ----------------------------------------------------------------------------
// Description:
// Perform rendering according to a render state \p s.
// \pre s_exists: s!=0
void vtkCameraPass::Render(const vtkRenderState *s)
{
  assert("pre: s_exists" && s!=0);

  this->NumberOfRenderedProps=0;

  this->UpdateCamera(s->GetRenderer());
  
  if(this->DelegatePass!=0)
    {
    this->DelegatePass->Render(s);
    this->NumberOfRenderedProps+=
      this->DelegatePass->GetNumberOfRenderedProps();
    }
  else
    {
    vtkWarningMacro(<<" no delegate.");
    }
  
  // clean up the model view matrix set up by the camera 
  glMatrixMode(GL_MODELVIEW);<commit_msg>ENH:Don't call UpdateCamera() on the renderer, instead almost copy the code of UpdateCamera and added a few lines to deal with FBOs.<commit_after>/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkCameraPass.cxx

  Copyright (c) <NAME>, <NAME>, <NAME>
  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 "vtkCameraPass.h"
#include "vtkObjectFactory.h"
#include <assert.h>
#include "vtkRenderState.h"
#include "vtkOpenGLRenderer.h"
#include "vtkgl.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkMatrix4x4.h"
#include "vtkgluPickMatrix.h"
#include "vtkCamera.h"
#include "vtkFrameBufferObject.h"

vtkCxxRevisionMacro(vtkCameraPass, "1.2");
vtkStandardNewMacro(vtkCameraPass);
vtkCxxSetObjectMacro(vtkCameraPass,DelegatePass,vtkRenderPass);

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

// ----------------------------------------------------------------------------
vtkCameraPass::~vtkCameraPass()
{
  if(this->DelegatePass!=0)
    {
      this->DelegatePass->Delete();
    }
}

// ----------------------------------------------------------------------------
void vtkCameraPass::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
  
  os << indent << "DelegatePass:";
  if(this->DelegatePass!=0)
    {
    this->DelegatePass->PrintSelf(os,indent);
    }
  else
    {
    os << "(none)" <<endl;
    }
}

// ----------------------------------------------------------------------------
// Description:
// Perform rendering according to a render state \p s.
// \pre s_exists: s!=0
void vtkCameraPass::Render(const vtkRenderState *s)
{
  assert("pre: s_exists" && s!=0);

  this->NumberOfRenderedProps=0;
  
  vtkRenderer *ren=s->GetRenderer();
  
  if (!ren->IsActiveCameraCreated())
    {
    vtkDebugMacro(<< "No cameras are on, creating one.");
    // the get method will automagically create a camera
    // and reset it since one hasn't been specified yet.
    // If is very unlikely that this can occur - if this
    // renderer is part of a vtkRenderWindow, the camera
    // will already have been created as part of the
    // DoStereoRender() method.
    
    // this is ren->GetActiveCameraAndResetIfCreated();
    ren->GetActiveCamera();
    ren->ResetCamera();
    }
  
  vtkCamera *camera=ren->GetActiveCamera();
  
  int lowerLeft[2];
  int usize;
  int vsize;
  vtkMatrix4x4 *matrix=vtkMatrix4x4::New();

  vtkFrameBufferObject *fbo=s->GetFrameBuffer();
  
  if(fbo==0)
    {
    vtkOpenGLRenderWindow *win=vtkOpenGLRenderWindow::SafeDownCast(ren->GetRenderWindow());
    
    // find out if we should stereo render
    bool stereo = win->GetStereoRender();
    ren->GetTiledSizeAndOrigin(&usize,&vsize,lowerLeft,lowerLeft+1);
    
    // if were on a stereo renderer draw to special parts of screen
    if(stereo)
      {
      switch (win->GetStereoType())
        {
        case VTK_STEREO_CRYSTAL_EYES:
          if (camera->GetLeftEye())
            {
            if(win->GetDoubleBuffer())
              {
              glDrawBuffer(static_cast<GLenum>(win->GetBackLeftBuffer()));
              glReadBuffer(static_cast<GLenum>(win->GetBackLeftBuffer()));
              }
            else
              {
              glDrawBuffer(static_cast<GLenum>(win->GetFrontLeftBuffer()));
              glReadBuffer(static_cast<GLenum>(win->GetFrontLeftBuffer()));
              }
            }
          else
            {
            if(win->GetDoubleBuffer())
              {
              glDrawBuffer(static_cast<GLenum>(win->GetBackRightBuffer()));
              glReadBuffer(static_cast<GLenum>(win->GetBackRightBuffer()));
              }
            else
              {
              glDrawBuffer(static_cast<GLenum>(win->GetFrontRightBuffer()));
              glReadBuffer(static_cast<GLenum>(win->GetFrontRightBuffer()));
              }
            }
          break;
        case VTK_STEREO_LEFT:
          camera->SetLeftEye(1);
          break;
        case VTK_STEREO_RIGHT:
          camera->SetLeftEye(0);
          break;
        default:
          break;
        }
      }
    else
      {
      if (win->GetDoubleBuffer())
        {
        glDrawBuffer(static_cast<GLenum>(win->GetBackBuffer()));
        
        // Reading back buffer means back left. see OpenGL spec.
        // because one can write to two buffers at a time but can only read from
        // one buffer at a time.
        glReadBuffer(static_cast<GLenum>(win->GetBackBuffer()));
      }
      else
        {
        glDrawBuffer(static_cast<GLenum>(win->GetFrontBuffer()));
        
        // Reading front buffer means front left. see OpenGL spec.
      // because one can write to two buffers at a time but can only read from
      // one buffer at a time.
        glReadBuffer(static_cast<GLenum>(win->GetFrontBuffer()));
        }
      }
    }
  else
    {
    // FBO size. This is the renderer size as a renderstate is per renderer.
    int size[2];
    fbo->GetLastSize(size);
    usize=size[0];
    vsize=size[1];
    lowerLeft[0]=0;
    lowerLeft[1]=0;
    // we assume the drawbuffer state is already initialized before.
    }
  
  glViewport(lowerLeft[0],lowerLeft[1], usize, vsize);
  glEnable( GL_SCISSOR_TEST );
  glScissor(lowerLeft[0],lowerLeft[1], usize, vsize);
  
  double aspectModification = 1.0;
  
  glMatrixMode( GL_PROJECTION);
  if(usize && vsize)
    {
    matrix->DeepCopy(camera->GetPerspectiveTransformMatrix(
                       aspectModification*usize/vsize, -1,1));
    matrix->Transpose();
    }
  if(ren->GetIsPicking())
    {
    int size[2]; size[0] = usize; size[1] = vsize;
    glLoadIdentity();
    vtkgluPickMatrix(ren->GetPickX(), ren->GetPickY(), 
                     ren->GetPickWidth(), ren->GetPickHeight(),
                     lowerLeft, size);
    glMultMatrixd(matrix->Element[0]);
    }
  else
    {
    // insert camera view transformation 
    glLoadMatrixd(matrix->Element[0]);
    }
  
  // push the model view matrix onto the stack, make sure we 
  // adjust the mode first
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();

  matrix->DeepCopy(camera->GetViewTransformMatrix());
  matrix->Transpose();
  
  // insert camera view transformation 
  glMultMatrixd(matrix->Element[0]);

  if ((ren->GetRenderWindow())->GetErase() && ren->GetErase() 
      && !ren->GetIsPicking())
    {
    ren->Clear();
    }
  
  matrix->Delete();
  
  // Done with camera initialization. The delegate can be called.
  
  if(this->DelegatePass!=0)
    {
    this->DelegatePass->Render(s);
    this->NumberOfRenderedProps+=
      this->DelegatePass->GetNumberOfRenderedProps();
    }
  else
    {
    vtkWarningMacro(<<" no delegate.");
    }
  
  // clean up the model view matrix set up by the camera 
  glMatrixMode(GL_MODELVIEW);
{'fraction_non_alphanumeric': 0.2094915970827608, 'fraction_numerical': 0.005284853609555015, 'mean_word_length': 2.314185639229422, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4981525', 'n_tokens_mistral': 2776, 'n_tokens_neox': 2728, 'n_words': 587}