static GfMatrix3d _EulerXYZToMatrix3d(GfVec3d eulerXYZ) { GfMatrix3d result(GfRotation(GfVec3d::XAxis(),eulerXYZ[0]) * GfRotation(GfVec3d::YAxis(),eulerXYZ[1]) * GfRotation(GfVec3d::ZAxis(),eulerXYZ[2])); return result; }
GfMatrix4d Hdx_UnitTestGLDrawing::GetViewMatrix() const { GfMatrix4d viewMatrix; viewMatrix.SetIdentity(); // rotate from z-up to y-up viewMatrix *= GfMatrix4d().SetRotate(GfRotation(GfVec3d(1.0,0.0,0.0), -90.0)); viewMatrix *= GfMatrix4d().SetRotate(GfRotation(GfVec3d(0, 1, 0), _rotate[1])); viewMatrix *= GfMatrix4d().SetRotate(GfRotation(GfVec3d(1, 0, 0), _rotate[0])); viewMatrix *= GfMatrix4d().SetTranslate(GfVec3d(_translate[0], _translate[1], _translate[2])); return viewMatrix; }
PXR_NAMESPACE_OPEN_SCOPE // XXX: // This implementation is ported from MfTransformablePrim:: // MatrixToVectorsWithPivotInvariant; needs to be generalized for arbitrary // rotationOrder (which means lofting that concept to Gf), orthonormalization, // etc. static GfMatrix3d _EulerXYZToMatrix3d(GfVec3d eulerXYZ) { GfMatrix3d result(GfRotation(GfVec3d::XAxis(),eulerXYZ[0]) * GfRotation(GfVec3d::YAxis(),eulerXYZ[1]) * GfRotation(GfVec3d::ZAxis(),eulerXYZ[2])); return result; }
void Hd_TestDriver::_Init(HdReprSelector const &reprSelector) { _renderIndex = HdRenderIndex::New(&_renderDelegate); TF_VERIFY(_renderIndex != nullptr); _sceneDelegate = new HdUnitTestDelegate(_renderIndex, SdfPath::AbsoluteRootPath()); _reprSelector = reprSelector; GfMatrix4d viewMatrix = GfMatrix4d().SetIdentity(); viewMatrix *= GfMatrix4d().SetTranslate(GfVec3d(0.0, 1000.0, 0.0)); viewMatrix *= GfMatrix4d().SetRotate(GfRotation(GfVec3d(1.0, 0.0, 0.0), -90.0)); GfFrustum frustum; frustum.SetPerspective(45, true, 1, 1.0, 10000.0); GfMatrix4d projMatrix = frustum.ComputeProjectionMatrix(); SetCamera(viewMatrix, projMatrix, GfVec4d(0, 0, 512, 512)); // set depthfunc to default _renderPassState->SetDepthFunc(HdCmpFuncLess); }
GfRotation GfMatrix3d::ExtractRotation() const { return GfRotation( ExtractRotationQuaternion() ); }
/* static */ GfMatrix4d UsdGeomXformOp::GetOpTransform(UsdGeomXformOp::Type const opType, VtValue const &opVal, bool isInverseOp) { // This will be the most common case. if (opType == TypeTransform) { GfMatrix4d mat(1.); bool isMatrixVal = true; if (opVal.IsHolding<GfMatrix4d>()) { mat = opVal.UncheckedGet<GfMatrix4d>(); } else if (opVal.IsHolding<GfMatrix4f>()) { mat = GfMatrix4d(opVal.UncheckedGet<GfMatrix4f>()); } else { isMatrixVal = false; TF_CODING_ERROR("Invalid combination of opType (%s) " "and opVal (%s). Returning identity matrix.", TfEnum::GetName(opType).c_str(), TfStringify(opVal).c_str()); return GfMatrix4d(1.); } if (isMatrixVal && isInverseOp) { double determinant=0; mat = mat.GetInverse(&determinant); if (GfIsClose(determinant, 0.0, 1e-9)) { TF_CODING_ERROR("Cannot invert singular transform op with " "value %s.", TfStringify(opVal).c_str()); } } return mat; } double doubleVal = 0.; bool isScalarVal = true; if (opVal.IsHolding<double>()) { doubleVal = opVal.UncheckedGet<double>(); } else if (opVal.IsHolding<float>()) { doubleVal = opVal.UncheckedGet<float>(); } else if (opVal.IsHolding<GfHalf>()) { doubleVal = opVal.UncheckedGet<GfHalf>(); } else { isScalarVal = false; } if (isScalarVal) { if (isInverseOp) doubleVal = -doubleVal; if (opType == TypeRotateX) { return GfMatrix4d(1.).SetRotate(GfRotation(GfVec3d::XAxis(), doubleVal)); } else if (opType == TypeRotateY) { return GfMatrix4d(1.).SetRotate(GfRotation(GfVec3d::YAxis(), doubleVal)); } else if (opType == TypeRotateZ) { return GfMatrix4d(1.).SetRotate(GfRotation(GfVec3d::ZAxis(), doubleVal)); } } GfVec3d vec3dVal = GfVec3d(0.); bool isVecVal = true; if (opVal.IsHolding<GfVec3f>()) { vec3dVal = opVal.UncheckedGet<GfVec3f>(); } else if (opVal.IsHolding<GfVec3d>()) { vec3dVal = opVal.UncheckedGet<GfVec3d>(); } else if (opVal.IsHolding<GfVec3h>()) { vec3dVal = opVal.UncheckedGet<GfVec3h>(); } else { isVecVal = false; } if (isVecVal) { switch(opType) { case TypeTranslate: if (isInverseOp) vec3dVal = -vec3dVal; return GfMatrix4d(1.).SetTranslate(vec3dVal); case TypeScale: if (isInverseOp) { vec3dVal = GfVec3d(1/vec3dVal[0], 1/vec3dVal[1], 1/vec3dVal[2]); } return GfMatrix4d(1.).SetScale(vec3dVal); default: { if (isInverseOp) vec3dVal = -vec3dVal; // Must be one of the 3-axis rotates. GfMatrix3d xRot(GfRotation(GfVec3d::XAxis(), vec3dVal[0])); GfMatrix3d yRot(GfRotation(GfVec3d::YAxis(), vec3dVal[1])); GfMatrix3d zRot(GfRotation(GfVec3d::ZAxis(), vec3dVal[2])); GfMatrix3d rotationMat(1.); switch (opType) { case TypeRotateXYZ: // Inv(ABC) = Inv(C) * Inv(B) * Inv(A) rotationMat = !isInverseOp ? (xRot * yRot * zRot) : (zRot * yRot * xRot); break; case TypeRotateXZY: rotationMat = !isInverseOp ? (xRot * zRot * yRot) : (yRot * zRot * xRot); break; case TypeRotateYXZ: rotationMat = !isInverseOp ? (yRot * xRot * zRot) : (zRot * xRot * yRot); break; case TypeRotateYZX: rotationMat = !isInverseOp ? (yRot * zRot * xRot) : (xRot * zRot * yRot); break; case TypeRotateZXY: rotationMat = !isInverseOp ? (zRot * xRot * yRot) : (yRot * xRot * zRot); break; case TypeRotateZYX: rotationMat = !isInverseOp ? (zRot * yRot * xRot) : (xRot * yRot * zRot); break; default: TF_CODING_ERROR("Invalid combination of opType (%s) " "and opVal (%s). Returning identity matrix.", TfEnum::GetName(opType).c_str(), TfStringify(opVal).c_str()); return GfMatrix4d(1.); } return GfMatrix4d(1.).SetRotate(rotationMat); } } } if (opType == TypeOrient) { GfQuatd quatVal(0); if (opVal.IsHolding<GfQuatd>()) quatVal = opVal.UncheckedGet<GfQuatd>(); else if (opVal.IsHolding<GfQuatf>()) { const GfQuatf &quatf = opVal.UncheckedGet<GfQuatf>(); quatVal = GfQuatd(quatf.GetReal(), quatf.GetImaginary()); } else if (opVal.IsHolding<GfQuath>()) { const GfQuath &quath = opVal.UncheckedGet<GfQuath>(); quatVal = GfQuatd(quath.GetReal(), quath.GetImaginary()); } GfRotation quatRotation(quatVal); if (isInverseOp) quatRotation = quatRotation.GetInverse(); return GfMatrix4d(quatRotation, GfVec3d(0.)); } TF_CODING_ERROR("Invalid combination of opType (%s) and opVal (%s). " "Returning identity matrix.", TfEnum::GetName(opType).c_str(), TfStringify(opVal).c_str()); return GfMatrix4d(1.); }
bool UsdGeomPointInstancer::ComputeInstanceTransformsAtTime( VtArray<GfMatrix4d>* xforms, const UsdTimeCode time, const UsdTimeCode baseTime, const ProtoXformInclusion doProtoXforms, const MaskApplication applyMask) const { // XXX: Need to add handling of velocities/angularVelocities and baseTime. (void)baseTime; if (!xforms) { TF_WARN("%s -- null container passed to ComputeInstanceTransformsAtTime()", GetPrim().GetPath().GetText()); return false; } VtIntArray protoIndices; if (!GetProtoIndicesAttr().Get(&protoIndices, time)) { TF_WARN("%s -- no prototype indices", GetPrim().GetPath().GetText()); return false; } if (protoIndices.empty()) { xforms->clear(); return true; } VtVec3fArray positions; if (!GetPositionsAttr().Get(&positions, time)) { TF_WARN("%s -- no positions", GetPrim().GetPath().GetText()); return false; } if (positions.size() != protoIndices.size()) { TF_WARN("%s -- positions.size() [%zu] != protoIndices.size() [%zu]", GetPrim().GetPath().GetText(), positions.size(), protoIndices.size()); return false; } VtVec3fArray scales; GetScalesAttr().Get(&scales, time); if (!scales.empty() && scales.size() != protoIndices.size()) { TF_WARN("%s -- scales.size() [%zu] != protoIndices.size() [%zu]", GetPrim().GetPath().GetText(), scales.size(), protoIndices.size()); return false; } VtQuathArray orientations; GetOrientationsAttr().Get(&orientations, time); if (!orientations.empty() && orientations.size() != protoIndices.size()) { TF_WARN("%s -- orientations.size() [%zu] != protoIndices.size() [%zu]", GetPrim().GetPath().GetText(), orientations.size(), protoIndices.size()); return false; } // If we're going to include the prototype transforms, verify that we have // prototypes and that all of the protoIndices are in bounds. SdfPathVector protoPaths; if (doProtoXforms == IncludeProtoXform) { const UsdRelationship prototypes = GetPrototypesRel(); if (!prototypes.GetTargets(&protoPaths) || protoPaths.empty()) { TF_WARN("%s -- no prototypes", GetPrim().GetPath().GetText()); return false; } TF_FOR_ALL(iter, protoIndices) { const int protoIndex = *iter; if (protoIndex < 0 || static_cast<size_t>(protoIndex) >= protoPaths.size()) { TF_WARN("%s -- invalid prototype index: %d. Should be in [0, %zu)", GetPrim().GetPath().GetText(), protoIndex, protoPaths.size()); return false; } } } // Compute the mask only if applyMask says we should, otherwise we leave // mask empty so that its application below is a no-op. std::vector<bool> mask; if (applyMask == ApplyMask) { mask = ComputeMaskAtTime(time); if (!mask.empty() && mask.size() != protoIndices.size()) { TF_WARN("%s -- mask.size() [%zu] != protoIndices.size() [%zu]", GetPrim().GetPath().GetText(), mask.size(), protoIndices.size()); return false; } } UsdStageWeakPtr stage = GetPrim().GetStage(); UsdGeomXformCache xformCache(time); xforms->assign(protoIndices.size(), GfMatrix4d(1.0)); for (size_t instanceId = 0; instanceId < protoIndices.size(); ++instanceId) { if (!mask.empty() && !mask[instanceId]) { continue; } GfTransform instanceTransform; if (!scales.empty()) { instanceTransform.SetScale(scales[instanceId]); } if (!orientations.empty()) { instanceTransform.SetRotation(GfRotation(orientations[instanceId])); } instanceTransform.SetTranslation(positions[instanceId]); GfMatrix4d protoXform(1.0); if (doProtoXforms == IncludeProtoXform) { const int protoIndex = protoIndices[instanceId]; const SdfPath& protoPath = protoPaths[protoIndex]; const UsdPrim& protoPrim = stage->GetPrimAtPath(protoPath); if (protoPrim) { // Get the prototype's local transformation. bool resetsXformStack; protoXform = xformCache.GetLocalTransformation(protoPrim, &resetsXformStack); } } (*xforms)[instanceId] = protoXform * instanceTransform.GetMatrix(); } return ApplyMaskToArray(mask, xforms); }
void My_TestGLDrawing::DrawTest(bool offscreen) { std::cout << "My_TestGLDrawing::DrawTest()\n"; HdPerfLog& perfLog = HdPerfLog::GetInstance(); perfLog.Enable(); // Reset all counters we care about. perfLog.ResetCache(HdTokens->extent); perfLog.ResetCache(HdTokens->points); perfLog.ResetCache(HdTokens->topology); perfLog.ResetCache(HdTokens->transform); perfLog.SetCounter(UsdImagingTokens->usdVaryingExtent, 0); perfLog.SetCounter(UsdImagingTokens->usdVaryingPrimvar, 0); perfLog.SetCounter(UsdImagingTokens->usdVaryingTopology, 0); perfLog.SetCounter(UsdImagingTokens->usdVaryingVisibility, 0); perfLog.SetCounter(UsdImagingTokens->usdVaryingXform, 0); int width = GetWidth(), height = GetHeight(); double aspectRatio = double(width)/height; GfFrustum frustum; frustum.SetPerspective(60.0, aspectRatio, 1, 100000.0); GfMatrix4d viewMatrix; viewMatrix.SetIdentity(); viewMatrix *= GfMatrix4d().SetRotate(GfRotation(GfVec3d(0, 1, 0), _rotate[0])); viewMatrix *= GfMatrix4d().SetRotate(GfRotation(GfVec3d(1, 0, 0), _rotate[1])); viewMatrix *= GfMatrix4d().SetTranslate(GfVec3d(_translate[0], _translate[1], _translate[2])); GfMatrix4d projMatrix = frustum.ComputeProjectionMatrix(); GfMatrix4d modelViewMatrix = viewMatrix; if (UsdGeomGetStageUpAxis(_stage) == UsdGeomTokens->z) { // rotate from z-up to y-up modelViewMatrix = GfMatrix4d().SetRotate(GfRotation(GfVec3d(1.0,0.0,0.0), -90.0)) * modelViewMatrix; } GfVec4d viewport(0, 0, width, height); _engine->SetCameraState(modelViewMatrix, projMatrix, viewport); size_t i = 0; TF_FOR_ALL(timeIt, GetTimes()) { UsdTimeCode time = *timeIt; if (*timeIt == -999) { time = UsdTimeCode::Default(); } UsdImagingGLRenderParams params; params.drawMode = GetDrawMode(); params.enableLighting = IsEnabledTestLighting(); params.enableIdRender = IsEnabledIdRender(); params.frame = time; params.complexity = _GetComplexity(); params.cullStyle = IsEnabledCullBackfaces() ? UsdImagingGLCullStyle::CULL_STYLE_BACK : UsdImagingGLCullStyle::CULL_STYLE_NOTHING; glViewport(0, 0, width, height); glEnable(GL_DEPTH_TEST); if(IsEnabledTestLighting()) { if(UsdImagingGLEngine::IsHydraEnabled()) { _engine->SetLightingState(_lightingContext); } else { _engine->SetLightingStateFromOpenGL(); } } if (!GetClipPlanes().empty()) { params.clipPlanes = GetClipPlanes(); for (size_t i=0; i<GetClipPlanes().size(); ++i) { glEnable(GL_CLIP_PLANE0 + i); } } GfVec4f const &clearColor = GetClearColor(); GLfloat clearDepth[1] = { 1.0f }; // Make sure we render to convergence. TfErrorMark mark; do { glClearBufferfv(GL_COLOR, 0, clearColor.data()); glClearBufferfv(GL_DEPTH, 0, clearDepth); _engine->Render(_stage->GetPseudoRoot(), params); } while (!_engine->IsConverged()); TF_VERIFY(mark.IsClean(), "Errors occurred while rendering!"); std::cout << "itemsDrawn " << perfLog.GetCounter(HdTokens->itemsDrawn) << std::endl; std::cout << "totalItemCount " << perfLog.GetCounter(HdTokens->totalItemCount) << std::endl; std::string imageFilePath = GetOutputFilePath(); if (!imageFilePath.empty()) { if (time != UsdTimeCode::Default()) { std::stringstream suffix; suffix << "_" << std::setw(3) << std::setfill('0') << params.frame << ".png"; imageFilePath = TfStringReplace(imageFilePath, ".png", suffix.str()); } std::cout << imageFilePath << "\n"; WriteToFile("color", imageFilePath); } i++; }