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
|
/*=========================================================================
Program: Visualization Toolkit
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 "vtkOpenGLCamera.h"
#include "vtkMatrix4x4.h"
#include "vtkMatrix3x3.h"
#include "vtkObjectFactory.h"
#include "vtkRenderer.h"
#include "vtkOutputWindow.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLError.h"
#include <cmath>
vtkStandardNewMacro(vtkOpenGLCamera);
vtkOpenGLCamera::vtkOpenGLCamera()
{
this->WCDCMatrix = vtkMatrix4x4::New();
this->WCVCMatrix = vtkMatrix4x4::New();
this->NormalMatrix = vtkMatrix3x3::New();
this->VCDCMatrix = vtkMatrix4x4::New();
this->LastRenderer = 0;
}
vtkOpenGLCamera::~vtkOpenGLCamera()
{
this->WCDCMatrix->Delete();
this->WCVCMatrix->Delete();
this->NormalMatrix->Delete();
this->VCDCMatrix->Delete();
}
// Implement base class method.
void vtkOpenGLCamera::Render(vtkRenderer *ren)
{
vtkOpenGLClearErrorMacro();
int lowerLeft[2];
int usize, vsize;
vtkOpenGLRenderWindow *win = vtkOpenGLRenderWindow::SafeDownCast(ren->GetRenderWindow());
// find out if we should stereo render
this->Stereo = (ren->GetRenderWindow())->GetStereoRender();
ren->GetTiledSizeAndOrigin(&usize, &vsize, lowerLeft, lowerLeft+1);
// if were on a stereo renderer draw to special parts of screen
if (this->Stereo)
{
switch ((ren->GetRenderWindow())->GetStereoType())
{
case VTK_STEREO_CRYSTAL_EYES:
if (this->LeftEye)
{
if (ren->GetRenderWindow()->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 (ren->GetRenderWindow()->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:
this->LeftEye = 1;
break;
case VTK_STEREO_RIGHT:
this->LeftEye = 0;
break;
default:
break;
}
}
else
{
if (ren->GetRenderWindow()->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()));
}
}
glViewport(lowerLeft[0], lowerLeft[1], usize, vsize);
glEnable(GL_SCISSOR_TEST);
if (this->UseScissor)
{
glScissor(this->ScissorRect.GetX(),this->ScissorRect.GetY(),
this->ScissorRect.GetWidth(), this->ScissorRect.GetHeight());
this->UseScissor = false;
}
else
{
glScissor(lowerLeft[0], lowerLeft[1], usize, vsize);
}
if ((ren->GetRenderWindow())->GetErase() && ren->GetErase()
&& !ren->GetIsPicking())
{
ren->Clear();
}
vtkOpenGLCheckErrorMacro("failed after Render");
}
//----------------------------------------------------------------------------
void vtkOpenGLCamera::UpdateViewport(vtkRenderer *ren)
{
vtkOpenGLClearErrorMacro();
int lowerLeft[2];
int usize, vsize;
ren->GetTiledSizeAndOrigin(&usize, &vsize, lowerLeft, lowerLeft+1);
glViewport(lowerLeft[0], lowerLeft[1], usize, vsize);
glEnable(GL_SCISSOR_TEST);
if (this->UseScissor)
{
glScissor(this->ScissorRect.GetX(),this->ScissorRect.GetY(),
this->ScissorRect.GetWidth(), this->ScissorRect.GetHeight());
this->UseScissor = false;
}
else
{
glScissor(lowerLeft[0], lowerLeft[1], usize, vsize);
}
vtkOpenGLCheckErrorMacro("failed after UpdateViewport");
}
//----------------------------------------------------------------------------
void vtkOpenGLCamera::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
void vtkOpenGLCamera::GetKeyMatrices(vtkRenderer *ren, vtkMatrix4x4 *&wcvc,
vtkMatrix3x3 *&normMat, vtkMatrix4x4 *&vcdc, vtkMatrix4x4 *&wcdc)
{
// has the camera changed?
if (ren != this->LastRenderer ||
this->MTime > this->KeyMatrixTime ||
ren->GetMTime() > this->KeyMatrixTime)
{
this->WCVCMatrix->DeepCopy(this->GetModelViewTransformMatrix());
for(int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
this->NormalMatrix->SetElement(i, j, this->WCVCMatrix->GetElement(i, j));
}
}
this->NormalMatrix->Invert();
this->WCVCMatrix->Transpose();
double aspect[2];
int lowerLeft[2];
int usize, vsize;
ren->GetTiledSizeAndOrigin(&usize, &vsize, lowerLeft, lowerLeft+1);
ren->ComputeAspect();
ren->GetAspect(aspect);
double aspect2[2];
ren->vtkViewport::ComputeAspect();
ren->vtkViewport::GetAspect(aspect2);
double aspectModification = aspect[0] * aspect2[1] / (aspect[1] * aspect2[0]);
if (usize && vsize)
{
this->VCDCMatrix->DeepCopy(this->GetProjectionTransformMatrix(
aspectModification * usize / vsize, -1, 1));
this->VCDCMatrix->Transpose();
}
vtkMatrix4x4::Multiply4x4(this->WCVCMatrix, this->VCDCMatrix, this->WCDCMatrix);
this->KeyMatrixTime.Modified();
this->LastRenderer = ren;
}
wcvc = this->WCVCMatrix;
normMat = this->NormalMatrix;
vcdc = this->VCDCMatrix;
wcdc = this->WCDCMatrix;
}
|