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
|
/*=========================================================================
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 "vtkVRHMDCamera.h"
#include "vtkMatrix3x3.h"
#include "vtkMatrix4x4.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLState.h"
#include "vtkRenderer.h"
#include "vtkTransform.h"
#include "vtkVRRenderWindow.h"
#include <cmath>
//------------------------------------------------------------------------------
VTK_ABI_NAMESPACE_BEGIN
vtkVRHMDCamera::vtkVRHMDCamera()
{
// approximate for Vive
// we use the projection matrix directly from the vive
// so this is just to help make view <--> display
// adjustments reasonable, not correct, just reasonable
this->SetViewAngle(110.0);
}
vtkVRHMDCamera::~vtkVRHMDCamera() = default;
// a reminder, with vtk order matrices multiplcation goes right to left
// e.g. vtkMatrix4x4::Multiply(BtoC, AtoB, AtoC);
//------------------------------------------------------------------------------
void vtkVRHMDCamera::Render(vtkRenderer* ren)
{
vtkOpenGLClearErrorMacro();
vtkVRRenderWindow* win = vtkVRRenderWindow::SafeDownCast(ren->GetRenderWindow());
vtkOpenGLState* ostate = win->GetState();
int renSize[2];
win->GetRenderBufferSize(renSize[0], renSize[1]);
// if were on a stereo renderer draw to special parts of screen
if (win->GetMultiSamples() && !ren->GetSelector())
{
ostate->vtkglEnable(GL_MULTISAMPLE);
}
ostate->vtkglViewport(0, 0, renSize[0], renSize[1]);
ostate->vtkglScissor(0, 0, renSize[0], renSize[1]);
if ((ren->GetRenderWindow())->GetErase() && ren->GetErase())
{
ren->Clear();
}
vtkOpenGLCheckErrorMacro("failed after Render");
}
//------------------------------------------------------------------------------
void vtkVRHMDCamera::GetKeyMatrices(vtkRenderer* ren, vtkMatrix4x4*& wcvc, vtkMatrix3x3*& normMat,
vtkMatrix4x4*& vcdc, vtkMatrix4x4*& wcdc)
{
if (ren->GetSelector())
{
return this->Superclass::GetKeyMatrices(ren, wcvc, normMat, vcdc, wcdc);
}
// has the camera changed?
if (ren != this->LastRenderer || this->MTime > this->KeyMatrixTime ||
ren->GetMTime() > this->KeyMatrixTime)
{
vtkVRRenderWindow* win = vtkVRRenderWindow::SafeDownCast(ren->GetRenderWindow());
// on left eye render we compute the NormalMatrix, this should be fairly
// accurate for both eyes.
if (this->LeftEye)
{
this->UpdateWorldToEyeMatrices(ren);
this->UpdateEyeToProjectionMatrices(ren);
vtkMatrix4x4* w2v = this->GetModelViewTransformMatrix();
this->WCVCMatrix->DeepCopy(w2v);
// only compute normal matrix once
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
this->NormalMatrix->SetElement(i, j, w2v->GetElement(i, j));
}
}
this->NormalMatrix->Invert();
}
if (this->LeftEye)
{
this->SetCameraFromWorldToDeviceMatrix(this->WorldToLeftEyeMatrix, win->GetPhysicalScale());
this->ModelViewTransform->SetMatrix(this->WorldToLeftEyeMatrix);
this->WCVCMatrix->DeepCopy(this->WorldToLeftEyeMatrix);
this->WCVCMatrix->Transpose();
vtkMatrix4x4::Multiply4x4(
this->LeftEyeToProjectionMatrix, this->WorldToLeftEyeMatrix, this->WCDCMatrix);
this->WCDCMatrix->Transpose();
vtkMatrix4x4::Multiply4x4(this->LeftEyeToProjectionMatrix, this->PhysicalToLeftEyeMatrix,
this->PhysicalToProjectionMatrixForLeftEye);
this->VCDCMatrix->DeepCopy(this->LeftEyeToProjectionMatrix);
this->VCDCMatrix->Transpose();
}
else
{
this->SetCameraFromWorldToDeviceMatrix(this->WorldToRightEyeMatrix, win->GetPhysicalScale());
this->ModelViewTransform->SetMatrix(this->WorldToRightEyeMatrix);
this->WCVCMatrix->DeepCopy(this->WorldToRightEyeMatrix);
this->WCVCMatrix->Transpose();
vtkMatrix4x4::Multiply4x4(
this->RightEyeToProjectionMatrix, this->WorldToRightEyeMatrix, this->WCDCMatrix);
this->WCDCMatrix->Transpose();
vtkMatrix4x4::Multiply4x4(this->RightEyeToProjectionMatrix, this->PhysicalToRightEyeMatrix,
this->PhysicalToProjectionMatrixForRightEye);
this->VCDCMatrix->DeepCopy(this->RightEyeToProjectionMatrix);
this->VCDCMatrix->Transpose();
}
this->KeyMatrixTime.Modified();
this->LastRenderer = ren;
}
wcdc = this->WCDCMatrix;
wcvc = this->WCVCMatrix;
normMat = this->NormalMatrix;
vcdc = this->VCDCMatrix;
}
//------------------------------------------------------------------------------
void vtkVRHMDCamera::GetPhysicalToProjectionMatrix(vtkMatrix4x4*& physToProjectionMat)
{
if (this->LeftEye)
{
physToProjectionMat = this->PhysicalToProjectionMatrixForLeftEye;
}
else
{
physToProjectionMat = this->PhysicalToProjectionMatrixForRightEye;
}
}
//------------------------------------------------------------------------------
void vtkVRHMDCamera::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "PhysicalToLeftEyeMatrix: ";
this->PhysicalToLeftEyeMatrix->PrintSelf(os, indent);
os << indent << "PhysicalToRightEyeMatrix: ";
this->PhysicalToRightEyeMatrix->PrintSelf(os, indent);
os << indent << "WorldToLeftEyeMatrix: ";
this->WorldToLeftEyeMatrix->PrintSelf(os, indent);
os << indent << "WorldToRightEyeMatrix: ";
this->WorldToRightEyeMatrix->PrintSelf(os, indent);
os << indent << "LeftEyeToProjectionMatrix: ";
this->LeftEyeToProjectionMatrix->PrintSelf(os, indent);
os << indent << "RightEyeToProjectionMatrix: ";
this->RightEyeToProjectionMatrix->PrintSelf(os, indent);
os << indent << "PhysicalToProjectionMatrixForLeftEye: ";
this->PhysicalToProjectionMatrixForLeftEye->PrintSelf(os, indent);
os << indent << "PhysicalToProjectionMatrixForRightEye: ";
this->PhysicalToProjectionMatrixForRightEye->PrintSelf(os, indent);
}
VTK_ABI_NAMESPACE_END
|