static UsdPrim _GetMaterialParent(const UsdStageRefPtr& stage, const PxrUsdMayaShadingModeExportContext::AssignmentVector& assignments) { SdfPath commonAncestor; TF_FOR_ALL(iter, assignments) { const SdfPath& assn = iter->first; if (stage->GetPrimAtPath(assn)) { if (commonAncestor.IsEmpty()) { commonAncestor = assn; } else { commonAncestor = commonAncestor.GetCommonPrefix(assn); } } } if (commonAncestor.IsEmpty()) { return UsdPrim(); } if (commonAncestor == SdfPath::AbsoluteRootPath()) { return stage->GetPseudoRoot(); } SdfPath shaderExportLocation = commonAncestor; while (!shaderExportLocation.IsRootPrimPath()) { shaderExportLocation = shaderExportLocation.GetParentPath(); } shaderExportLocation = shaderExportLocation.AppendChild(TfToken("Looks")); return UsdGeomScope::Define(stage, shaderExportLocation).GetPrim(); }
void GusdUSD_StageProxy::_StageData::Update(const UsdStageRefPtr& stage) { UT_ASSERT(stage); double preroll = 0.0; double postroll = 0.0; if (const auto& pseudoRoot = stage->GetPseudoRoot()) { const TfToken prerollName("shot:preroll"); const TfToken postrollName("shot:postroll"); static const Usd_PrimFlagsPredicate pred(UsdPrimIsActive && UsdPrimIsDefined && !UsdPrimIsAbstract); for(const auto& prim : pseudoRoot.GetFilteredChildren(pred)) { VtValue val; double dval; if (auto prerollAttr = prim.GetAttribute(prerollName)) { prerollAttr.Get(&val); dval = 0.0; if (val.IsHolding<int>()) { dval = double(val.UncheckedGet<int>()); } else if (val.IsHolding<double>()) { dval = val.UncheckedGet<double>(); } preroll = std::max(preroll, dval); } if (auto postrollAttr = prim.GetAttribute(postrollName)) { postrollAttr.Get(&val); dval = 0.0; if (val.IsHolding<int>()) { dval = double(val.UncheckedGet<int>()); } else if (val.IsHolding<double>()) { dval = val.UncheckedGet<double>(); } postroll = std::max(postroll, dval); } } } const auto& lyr = stage->GetRootLayer(); startTimeCode = lyr->GetStartTimeCode() - preroll; endTimeCode = lyr->GetEndTimeCode() + postroll; }
NdrNodeDiscoveryResultVec UsdHydraDiscoveryPlugin::DiscoverNodes(const Context &context) { NdrNodeDiscoveryResultVec result; static std::string shaderDefsFile = _GetShaderResourcePath( "shaderDefs.usda"); if (shaderDefsFile.empty()) return result; auto resolverContext = ArGetResolver().CreateDefaultContextForAsset( shaderDefsFile); const UsdStageRefPtr stage = UsdStage::Open(shaderDefsFile, resolverContext); if (!stage) { TF_RUNTIME_ERROR("Could not open file '%s' on a USD stage.", shaderDefsFile.c_str()); return result; } ArResolverContextBinder binder(resolverContext); const TfToken discoveryType(ArGetResolver().GetExtension( shaderDefsFile)); auto rootPrims = stage->GetPseudoRoot().GetChildren(); for (const auto &shaderDef : rootPrims) { UsdShadeShader shader(shaderDef); if (!shader) { continue; } auto discoveryResults = UsdShadeShaderDefUtils::GetNodeDiscoveryResults( shader, shaderDefsFile); result.insert(result.end(), discoveryResults.begin(), discoveryResults.end()); if (discoveryResults.empty()) { TF_RUNTIME_ERROR("Found shader definition <%s> with no valid " "discovery results. This is likely because there are no " "resolvable info:sourceAsset values.", shaderDef.GetPath().GetText()); } } return result; }
void My_TestGLDrawing::InitTest() { std::cout << "My_TestGLDrawing::InitTest()\n"; _stage = UsdStage::Open(GetStageFilePath()); SdfPathVector excludedPaths; if (UsdImagingGLEngine::IsHydraEnabled()) { std::cout << "Using HD Renderer.\n"; _engine.reset(new UsdImagingGLEngine( _stage->GetPseudoRoot().GetPath(), excludedPaths)); if (!_GetRenderer().IsEmpty()) { if (!_engine->SetRendererPlugin(_GetRenderer())) { std::cerr << "Couldn't set renderer plugin: " << _GetRenderer().GetText() << std::endl; exit(-1); } else { std::cout << "Renderer plugin: " << _GetRenderer().GetText() << std::endl; } } } else{ std::cout << "Using Reference Renderer.\n"; _engine.reset( new UsdImagingGLEngine(_stage->GetPseudoRoot().GetPath(), excludedPaths)); } std::cout << glGetString(GL_VENDOR) << "\n"; std::cout << glGetString(GL_RENDERER) << "\n"; std::cout << glGetString(GL_VERSION) << "\n"; if (_ShouldFrameAll()) { TfTokenVector purposes; purposes.push_back(UsdGeomTokens->default_); purposes.push_back(UsdGeomTokens->proxy); // Extent hints are sometimes authored as an optimization to avoid // computing bounds, they are particularly useful for some tests where // there is no bound on the first frame. bool useExtentHints = true; UsdGeomBBoxCache bboxCache(UsdTimeCode::Default(), purposes, useExtentHints); GfBBox3d bbox = bboxCache.ComputeWorldBound(_stage->GetPseudoRoot()); GfRange3d world = bbox.ComputeAlignedRange(); GfVec3d worldCenter = (world.GetMin() + world.GetMax()) / 2.0; double worldSize = world.GetSize().GetLength(); std::cerr << "worldCenter: " << worldCenter << "\n"; std::cerr << "worldSize: " << worldSize << "\n"; if (UsdGeomGetStageUpAxis(_stage) == UsdGeomTokens->z) { // transpose y and z centering translation _translate[0] = -worldCenter[0]; _translate[1] = -worldCenter[2]; _translate[2] = -worldCenter[1] - worldSize; } else { _translate[0] = -worldCenter[0]; _translate[1] = -worldCenter[1]; _translate[2] = -worldCenter[2] - worldSize; } } else { _translate[0] = GetTranslate()[0]; _translate[1] = GetTranslate()[1]; _translate[2] = GetTranslate()[2]; } if(IsEnabledTestLighting()) { if(UsdImagingGLEngine::IsHydraEnabled()) { // set same parameter as GlfSimpleLightingContext::SetStateFromOpenGL // OpenGL defaults _lightingContext = GlfSimpleLightingContext::New(); GlfSimpleLight light; if (IsEnabledCameraLight()) { light.SetPosition(GfVec4f(_translate[0], _translate[2], _translate[1], 0)); } else { light.SetPosition(GfVec4f(0, -.5, .5, 0)); } light.SetDiffuse(GfVec4f(1,1,1,1)); light.SetAmbient(GfVec4f(0,0,0,1)); light.SetSpecular(GfVec4f(1,1,1,1)); GlfSimpleLightVector lights; lights.push_back(light); _lightingContext->SetLights(lights); GlfSimpleMaterial material; material.SetAmbient(GfVec4f(0.2, 0.2, 0.2, 1.0)); material.SetDiffuse(GfVec4f(0.8, 0.8, 0.8, 1.0)); material.SetSpecular(GfVec4f(0,0,0,1)); material.SetShininess(0.0001f); _lightingContext->SetMaterial(material); _lightingContext->SetSceneAmbient(GfVec4f(0.2,0.2,0.2,1.0)); } else { glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); if (IsEnabledCameraLight()) { float position[4] = {_translate[0], _translate[2], _translate[1], 0}; glLightfv(GL_LIGHT0, GL_POSITION, position); } else { float position[4] = {0,-.5,.5,0}; glLightfv(GL_LIGHT0, GL_POSITION, position); } } } }
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++; }