HdxSelectionTask::HdxSelectionTask(HdSceneDelegate* delegate, SdfPath const& id) : HdSceneTask(delegate, id) , _lastVersion(-1) , _offsetMin(0) , _offsetMax(-1) , _hasSelection(false) , _selUniformBar(nullptr) { _params = {false, GfVec4f(), GfVec4f(), GfVec4f()}; }
void My_TestGLDrawing::InitTest() { _renderIndex = HdRenderIndex::New(&_renderDelegate); TF_VERIFY(_renderIndex != nullptr); _delegate.reset(new Hdx_UnitTestDelegate(_renderIndex)); // prepare render task SdfPath renderSetupTask("/renderSetupTask"); SdfPath renderTask("/renderTask"); SdfPath selectionTask("/selectionTask"); _delegate->AddRenderSetupTask(renderSetupTask); _delegate->AddRenderTask(renderTask); _delegate->AddSelectionTask(selectionTask); // render task parameters. VtValue vParam = _delegate->GetTaskParam(renderSetupTask, HdTokens->params); HdxRenderTaskParams param = vParam.Get<HdxRenderTaskParams>(); param.enableLighting = true; // use default lighting _delegate->SetTaskParam(renderSetupTask, HdTokens->params, VtValue(param)); _delegate->SetTaskParam(renderTask, HdTokens->collection, VtValue(HdRprimCollection(HdTokens->geometry, HdReprSelector(HdReprTokens->hull)))); HdxSelectionTaskParams selParam; selParam.enableSelection = true; selParam.selectionColor = GfVec4f(1, 1, 0, 1); selParam.locateColor = GfVec4f(1, 0, 1, 1); _delegate->SetTaskParam(selectionTask, HdTokens->params, VtValue(selParam)); // prepare scene _InitScene(); SetCameraTranslate(GfVec3f(0, 0, -20)); // picking related init _pickablesCol = HdRprimCollection(_tokens->pickables, HdReprSelector(HdReprTokens->hull)); _marquee.InitGLResources(); _picker.InitIntersector(_renderIndex); _SetPickParams(); // We have to unfortunately explictly add collections besides 'geometry' // See HdRenderIndex constructor. _delegate->GetRenderIndex().GetChangeTracker().AddCollection(_tokens->pickables); // XXX: Setup a VAO, the current drawing engine will not yet do this. glGenVertexArrays(1, &vao); glBindVertexArray(vao); glBindVertexArray(0); }
GlfSimpleLight HdStLight::_ApproximateAreaLight(SdfPath const &id, HdSceneDelegate *sceneDelegate) { // Get the color of the light GfVec3f hdc = sceneDelegate->GetLightParamValue(id, HdStLightTokens->color) .Get<GfVec3f>(); // Extract intensity float intensity = sceneDelegate->GetLightParamValue(id, HdLightTokens->intensity) .Get<float>(); // Extract the exposure of the light float exposure = sceneDelegate->GetLightParamValue(id, HdLightTokens->exposure) .Get<float>(); intensity *= powf(2.0f, GfClamp(exposure, -50.0f, 50.0f)); // Calculate the final color of the light GfVec4f c(hdc[0]*intensity, hdc[1]*intensity, hdc[2]*intensity, 1.0f); // Get the transform of the light GfMatrix4d transform = _params[HdTokens->transform].Get<GfMatrix4d>(); GfVec3d hdp = transform.ExtractTranslation(); GfVec4f p = GfVec4f(hdp[0], hdp[1], hdp[2], 1.0f); // Create the Glf Simple Light object that will be used by the rest // of the pipeline. No support for shadows for this translated light. GlfSimpleLight l; l.SetPosition(p); l.SetDiffuse(c); l.SetHasShadow(false); return l; }
static GfMatrix4d _GetTranslate(float tx, float ty, float tz) { GfMatrix4d m(1.0f); m.SetRow(3, GfVec4f(tx, ty, tz, 1.0)); return m; }
GfVec4f GfHomogeneousCross(const GfVec4f &a, const GfVec4f &b) { GfVec4f ah(GfGetHomogenized(a)); GfVec4f bh(GfGetHomogenized(b)); GfVec3f prod = GfCross(GfVec3f(ah[0], ah[1], ah[2]), GfVec3f(bh[0], bh[1], bh[2])); return GfVec4f(prod[0], prod[1], prod[2], 1); }
void HdStreamTaskController::_CreateSelectionTask() { // Create a selection highlighting task. _selectionTaskId = GetControllerId().AppendChild(_tokens->selectionTask); HdxSelectionTaskParams selectionParams; selectionParams.enableSelection = true; selectionParams.selectionColor = GfVec4f(1,1,0,1); selectionParams.locateColor = GfVec4f(0,0,1,1); GetRenderIndex()->InsertTask<HdxSelectionTask>(&_delegate, _selectionTaskId); _delegate.SetParameter(_selectionTaskId, HdTokens->params, selectionParams); _delegate.SetParameter(_selectionTaskId, HdTokens->children, SdfPathVector()); }
bool MayaMeshWriter::_createRGBAPrimVar( UsdGeomGprim &primSchema, const TfToken& name, const VtArray<GfVec3f>& rgbData, const VtArray<float>& alphaData, const TfToken& interpolation, const VtArray<int>& assignmentIndices, const int unassignedValueIndex, bool clamped) { unsigned int numValues = rgbData.size(); if (numValues == 0 || numValues != alphaData.size()) { return false; } TfToken interp = interpolation; if (numValues == 1 && interp == UsdGeomTokens->constant) { interp = TfToken(); } UsdGeomPrimvar primVar = primSchema.CreatePrimvar(name, SdfValueTypeNames->Color4fArray, interp); VtArray<GfVec4f> rgbaData(numValues); for (size_t i = 0; i < rgbaData.size(); ++i) { rgbaData[i] = GfVec4f(rgbData[i][0], rgbData[i][1], rgbData[i][2], alphaData[i]); } primVar.Set(rgbaData); if (!assignmentIndices.empty()) { primVar.SetIndices(assignmentIndices); if (unassignedValueIndex != primVar.GetUnauthoredValuesIndex()) { primVar.SetUnauthoredValuesIndex(unassignedValueIndex); } } if (clamped) { PxrUsdMayaRoundTripUtil::MarkPrimvarAsClamped(primVar); } return true; }
UsdImaging_DefaultTaskDelegate::UsdImaging_DefaultTaskDelegate( HdRenderIndexSharedPtr const& parentIndex, SdfPath const& delegateID) : UsdImagingTaskDelegate(parentIndex, delegateID) , _viewport(0,0,1,1) , _selectionColor(1,1,0,1) { // create an unique namespace _rootId = delegateID.AppendChild( TfToken(TfStringPrintf("_UsdImaging_%p", this))); _renderTaskId = _rootId.AppendChild(_tokens->renderTask); _idRenderTaskId = _rootId.AppendChild(_tokens->idRenderTask); _selectionTaskId = _rootId.AppendChild(_tokens->selectionTask); _simpleLightTaskId = _rootId.AppendChild(_tokens->simpleLightTask); _simpleLightBypassTaskId = _rootId.AppendChild(_tokens->simpleLightBypassTask); _cameraId = _rootId.AppendChild(_tokens->camera); _activeSimpleLightTaskId = SdfPath(); // TODO: tasks of shadow map generation, accumulation etc will be // prepared here. HdRenderIndex &renderIndex = GetRenderIndex(); // camera { renderIndex.InsertCamera<HdCamera>(this, _cameraId); _ValueCache &cache = _valueCacheMap[_cameraId]; cache[HdShaderTokens->worldToViewMatrix] = VtValue(GfMatrix4d(1)); cache[HdShaderTokens->projectionMatrix] = VtValue(GfMatrix4d(1)); cache[HdTokens->cameraFrustum] = VtValue(); // we don't use GfFrustum. cache[HdTokens->windowPolicy] = VtValue(); // we don't use window policy. } // selection task { renderIndex.InsertTask<HdxSelectionTask>(this, _selectionTaskId); _ValueCache &cache = _valueCacheMap[_selectionTaskId]; HdxSelectionTaskParams params; params.enableSelection = true; params.selectionColor = _selectionColor; params.locateColor = GfVec4f(0,0,1,1); cache[HdTokens->params] = VtValue(params); cache[HdTokens->children] = VtValue(SdfPathVector()); } // simple lighting task (for Hydra native) { renderIndex.InsertTask<HdxSimpleLightTask>(this, _simpleLightTaskId); _ValueCache &cache = _valueCacheMap[_simpleLightTaskId]; HdxSimpleLightTaskParams params; params.cameraPath = _cameraId; cache[HdTokens->params] = VtValue(params); cache[HdTokens->children] = VtValue(SdfPathVector()); } // simple lighting task (for Presto UsdBaseIc compatible) { renderIndex.InsertTask<HdxSimpleLightBypassTask>(this, _simpleLightBypassTaskId); _ValueCache &cache = _valueCacheMap[_simpleLightBypassTaskId]; HdxSimpleLightBypassTaskParams params; params.cameraPath = _cameraId; cache[HdTokens->params] = VtValue(params); cache[HdTokens->children] = VtValue(SdfPathVector()); } // render task _InsertRenderTask(_renderTaskId); _InsertRenderTask(_idRenderTaskId); // initialize HdxRenderTaskParams for render tasks _UpdateCollection(&_rprims, HdTokens->geometry, HdTokens->smoothHull, SdfPathVector(1, SdfPath::AbsoluteRootPath()), _renderTaskId, _idRenderTaskId); _UpdateRenderParams(_renderParams, _renderParams, _renderTaskId); _UpdateRenderParams(_idRenderParams, _idRenderParams, _idRenderTaskId); }
UsdMayaGLBatchRenderer::RenderParams UsdMayaGLBatchRenderer::ShapeRenderer::GetRenderParams( const MDagPath& objPath, const unsigned int& displayStyle, const MHWRender::DisplayStatus& displayStatus, bool* drawShape, bool* drawBoundingBox ) { // VP 2.0 Implementation // RenderParams params( _baseParams ); *drawShape = true; *drawBoundingBox = (displayStyle & MHWRender::MFrameContext::DisplayStyle::kBoundingBox); // If obj is selected, we set the wireframe color. // QueueShapeForDraw(...) will later break this single param set into // two, to perform a two-pass render. // MColor mayaWireframeColor; bool needsWire = _GetWireframeColor( _batchRenderer->GetSoftSelectHelper(), objPath, displayStatus, &mayaWireframeColor); if( needsWire ) { params.wireframeColor = GfVec4f( mayaWireframeColor.r, mayaWireframeColor.g, mayaWireframeColor.b, 1.f ); } // Maya 2015 lacks MHWRender::MFrameContext::DisplayStyle::kFlatShaded for whatever reason... bool flatShaded = #if MAYA_API_VERSION >= 201600 displayStyle & MHWRender::MFrameContext::DisplayStyle::kFlatShaded; #else false; #endif if( flatShaded ) { if( needsWire ) params.drawRepr = HdTokens->wireOnSurf; else params.drawRepr = HdTokens->hull; } else if( displayStyle & MHWRender::MFrameContext::DisplayStyle::kGouraudShaded ) { if( needsWire || (displayStyle & MHWRender::MFrameContext::DisplayStyle::kWireFrame) ) params.drawRepr = HdTokens->refinedWireOnSurf; else params.drawRepr = HdTokens->refined; } else if( displayStyle & MHWRender::MFrameContext::DisplayStyle::kWireFrame ) { params.drawRepr = HdTokens->refinedWire; params.enableLighting = false; } else { *drawShape = false; } // Maya 2016 SP2 lacks MHWRender::MFrameContext::DisplayStyle::kBackfaceCulling for whatever reason... params.cullStyle = HdCullStyleNothing; #if MAYA_API_VERSION >= 201603 if( displayStyle & MHWRender::MFrameContext::DisplayStyle::kBackfaceCulling ) params.cullStyle = HdCullStyleBackUnlessDoubleSided; #endif return params; }
UsdMayaGLBatchRenderer::RenderParams UsdMayaGLBatchRenderer::ShapeRenderer::GetRenderParams( const MDagPath& objPath, const M3dView::DisplayStyle& displayStyle, const M3dView::DisplayStatus& displayStatus, bool* drawShape, bool* drawBoundingBox ) { // VP 1.0 Implementation // RenderParams params( _baseParams ); // VP 1.0 deos not allow shapes and bounding boxes to be drawn at the // same time... // *drawBoundingBox = (displayStyle == M3dView::kBoundingBox); *drawShape = !*drawBoundingBox; // If obj is selected, we set selected and the wireframe color. // QueueShapeForDraw(...) will later break this single param set into // two, to perform a two-pass render. // MColor mayaWireframeColor; bool needsWire = _GetWireframeColor( _batchRenderer->GetSoftSelectHelper(), objPath, _ToMHWRenderDisplayStatus(displayStatus), &mayaWireframeColor); if( needsWire ) { // The legacy viewport does not support color management, // so we roll our own gamma correction via framebuffer effect. // But that means we need to pre-linearize the wireframe color // from Maya. params.wireframeColor = GfConvertDisplayToLinear( GfVec4f( mayaWireframeColor.r, mayaWireframeColor.g, mayaWireframeColor.b, 1.f )); } switch( displayStyle ) { case M3dView::kWireFrame: { params.drawRepr = HdTokens->refinedWire; params.enableLighting = false; break; } case M3dView::kGouraudShaded: { if( needsWire ) params.drawRepr = HdTokens->refinedWireOnSurf; else params.drawRepr = HdTokens->refined; break; } case M3dView::kFlatShaded: { if( needsWire ) params.drawRepr = HdTokens->wireOnSurf; else params.drawRepr = HdTokens->hull; break; } case M3dView::kPoints: { // Points mode is not natively supported by Hydra, so skip it... } default: { *drawShape = false; } }; return params; }
void GlfSimpleLightingContext::BindUniformBlocks(GlfBindingMapPtr const &bindingMap) { if (not _lightingUniformBlock) _lightingUniformBlock = GlfUniformBlock::New(); if (not _shadowUniformBlock) _shadowUniformBlock = GlfUniformBlock::New(); if (not _materialUniformBlock) _materialUniformBlock = GlfUniformBlock::New(); bool shadowExists = false; if ((not _lightingUniformBlockValid or not _shadowUniformBlockValid) and _lights.size() > 0) { int numLights = GetNumLightsUsed(); // 16byte aligned struct LightSource { float position[4]; float ambient[4]; float diffuse[4]; float specular[4]; float spotDirection[4]; float spotCutoff; float spotFalloff; float padding[2]; float attenuation[4]; bool hasShadow; int32_t shadowIndex; int32_t padding2[2]; }; struct Lighting { int32_t useLighting; int32_t useColorMaterialDiffuse; int32_t padding[2]; LightSource lightSource[0]; }; // 16byte aligned struct ShadowMatrix { float viewToShadowMatrix[16]; float basis0[4]; float basis1[4]; float basis2[4]; float bias; float padding[3]; }; struct Shadow { ShadowMatrix shadow[0]; }; size_t lightingSize = sizeof(Lighting) + sizeof(LightSource) * numLights; size_t shadowSize = sizeof(ShadowMatrix) * numLights; Lighting *lightingData = (Lighting *)alloca(lightingSize); Shadow *shadowData = (Shadow *)alloca(shadowSize); memset(shadowData, 0, shadowSize); memset(lightingData, 0, lightingSize); GfMatrix4d viewToWorldMatrix = _worldToViewMatrix.GetInverse(); lightingData->useLighting = _useLighting; lightingData->useColorMaterialDiffuse = _useColorMaterialDiffuse; for (int i = 0; _useLighting and i < numLights; ++i) { GlfSimpleLight const &light = _lights[i]; setVec4(lightingData->lightSource[i].position, light.GetPosition() * _worldToViewMatrix); setVec4(lightingData->lightSource[i].diffuse, light.GetDiffuse()); setVec4(lightingData->lightSource[i].ambient, light.GetAmbient()); setVec4(lightingData->lightSource[i].specular, light.GetSpecular()); setVec3(lightingData->lightSource[i].spotDirection, _worldToViewMatrix.TransformDir(light.GetSpotDirection())); setVec3(lightingData->lightSource[i].attenuation, light.GetAttenuation()); lightingData->lightSource[i].spotCutoff = light.GetSpotCutoff(); lightingData->lightSource[i].spotFalloff = light.GetSpotFalloff(); lightingData->lightSource[i].hasShadow = light.HasShadow(); if (lightingData->lightSource[i].hasShadow) { int shadowIndex = light.GetShadowIndex(); lightingData->lightSource[i].shadowIndex = shadowIndex; GfMatrix4d viewToShadowMatrix = viewToWorldMatrix * _shadows->GetWorldToShadowMatrix(shadowIndex); double invBlur = 1.0/(std::max(0.0001F, light.GetShadowBlur())); GfMatrix4d mat = viewToShadowMatrix.GetInverse(); GfVec4f xVec = GfVec4f(mat.GetRow(0) * invBlur); GfVec4f yVec = GfVec4f(mat.GetRow(1) * invBlur); GfVec4f zVec = GfVec4f(mat.GetRow(2)); shadowData->shadow[shadowIndex].bias = light.GetShadowBias(); setMatrix(shadowData->shadow[shadowIndex].viewToShadowMatrix, viewToShadowMatrix); setVec4(shadowData->shadow[shadowIndex].basis0, xVec); setVec4(shadowData->shadow[shadowIndex].basis1, yVec); setVec4(shadowData->shadow[shadowIndex].basis2, zVec); shadowExists = true; } } _lightingUniformBlock->Update(lightingData, lightingSize); _lightingUniformBlockValid = true; if (shadowExists) { _shadowUniformBlock->Update(shadowData, shadowSize); _shadowUniformBlockValid = true; } } _lightingUniformBlock->Bind(bindingMap, _tokens->lightingUB); if (shadowExists) { _shadowUniformBlock->Bind(bindingMap, _tokens->shadowUB); } if (not _materialUniformBlockValid) { // has to be matched with the definition of simpleLightingShader.glslfx struct Material { float ambient[4]; float diffuse[4]; float specular[4]; float emission[4]; float sceneColor[4]; // XXX: should be separated? float shininess; float padding[3]; } materialData; memset(&materialData, 0, sizeof(materialData)); setVec4(materialData.ambient, _material.GetAmbient()); setVec4(materialData.diffuse, _material.GetDiffuse()); setVec4(materialData.specular, _material.GetSpecular()); setVec4(materialData.emission, _material.GetEmission()); materialData.shininess = _material.GetShininess(); setVec4(materialData.sceneColor, _sceneAmbient); _materialUniformBlock->Update(&materialData, sizeof(materialData)); _materialUniformBlockValid = true; } _materialUniformBlock->Bind(bindingMap, _tokens->materialUB); }
GfVec4f GfApplyGamma(const GfVec4f &v, double g) { return GfVec4f(pow(v[0],g),pow(v[1],g),pow(v[2],g),v[3]); }
static void CameraAndLightTest() { HdStRenderDelegate renderDelegate; std::unique_ptr<HdRenderIndex> index(HdRenderIndex::New(&renderDelegate)); TF_VERIFY(index); std::unique_ptr<Hdx_UnitTestDelegate> delegate( new Hdx_UnitTestDelegate(index.get())); HdChangeTracker& tracker = index->GetChangeTracker(); HdPerfLog& perfLog = HdPerfLog::GetInstance(); perfLog.Enable(); HdRprimCollection collection(HdTokens->geometry, HdTokens->hull); HdRenderPassStateSharedPtr renderPassState(new HdRenderPassState()); HdRenderPassSharedPtr renderPass( new HdSt_RenderPass(index.get(), collection)); HdEngine engine; HdTaskSharedPtr drawTask = boost::make_shared<Hd_TestTask>(renderPass, renderPassState); HdTaskSharedPtrVector tasks = { drawTask }; GfMatrix4d tx(1.0f); tx.SetRow(3, GfVec4f(5, 0, 5, 1.0)); SdfPath cube("geometry"); delegate->AddCube(cube, tx); SdfPath camera("camera"); SdfPath light("light"); delegate->AddCamera(camera); delegate->AddLight(light, GlfSimpleLight()); delegate->SetLight(light, HdStLightTokens->shadowCollection, VtValue(HdRprimCollection(HdTokens->geometry, HdTokens->hull))); engine.Execute(*index, tasks); VERIFY_PERF_COUNT(HdPerfTokens->rebuildBatches, 1); // Update camera matrix delegate->SetCamera(camera, GfMatrix4d(2), GfMatrix4d(2)); tracker.MarkSprimDirty(camera, HdStCamera::DirtyViewMatrix); tracker.MarkSprimDirty(camera, HdStCamera::DirtyProjMatrix); engine.Execute(*index, tasks); // batch should not be rebuilt VERIFY_PERF_COUNT(HdPerfTokens->rebuildBatches, 1); // Update shadow collection delegate->SetLight(light, HdStLightTokens->shadowCollection, VtValue(HdRprimCollection(HdTokens->geometry, HdTokens->refined))); tracker.MarkSprimDirty(light, HdStLight::DirtyCollection); engine.Execute(*index, tasks); // batch rebuilt VERIFY_PERF_COUNT(HdPerfTokens->rebuildBatches, 2); // Update shadow collection again with the same data delegate->SetLight(light, HdStLightTokens->shadowCollection, VtValue(HdRprimCollection(HdTokens->geometry, HdTokens->refined))); tracker.MarkSprimDirty(light, HdStLight::DirtyCollection); engine.Execute(*index, tasks); // batch should not be rebuilt VERIFY_PERF_COUNT(HdPerfTokens->rebuildBatches, 2); }
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); } } } }
bool PxrUsdMayaWriteUtil::SetUsdAttr( const MPlug& attrPlug, const UsdAttribute& usdAttr, const UsdTimeCode& usdTime, const bool translateMayaDoubleToUsdSinglePrecision) { if (!usdAttr || attrPlug.isNull()) { return false; } bool isAnimated = attrPlug.isDestination(); if (usdTime.IsDefault() == isAnimated) { return true; } // We perform a similar set of type-infererence acrobatics here as we do up // above in GetUsdTypeName(). See the comments there for more detail on a // few type-related oddities. MObject attrObj(attrPlug.attribute()); if (attrObj.hasFn(MFn::kEnumAttribute)) { MFnEnumAttribute enumAttrFn(attrObj); const short enumIndex = attrPlug.asShort(); const TfToken enumToken(enumAttrFn.fieldName(enumIndex).asChar()); return usdAttr.Set(enumToken, usdTime); } MFnNumericData::Type numericDataType; MFnData::Type typedDataType; MFnUnitAttribute::Type unitDataType; _GetMayaAttributeNumericTypedAndUnitDataTypes(attrPlug, numericDataType, typedDataType, unitDataType); if (attrObj.hasFn(MFn::kMatrixAttribute)) { typedDataType = MFnData::kMatrix; } switch (typedDataType) { case MFnData::kString: { MFnStringData stringDataFn(attrPlug.asMObject()); const std::string usdVal(stringDataFn.string().asChar()); return usdAttr.Set(usdVal, usdTime); break; } case MFnData::kMatrix: { MFnMatrixData matrixDataFn(attrPlug.asMObject()); const GfMatrix4d usdVal(matrixDataFn.matrix().matrix); return usdAttr.Set(usdVal, usdTime); break; } case MFnData::kStringArray: { MFnStringArrayData stringArrayDataFn(attrPlug.asMObject()); VtStringArray usdVal(stringArrayDataFn.length()); for (unsigned int i = 0; i < stringArrayDataFn.length(); ++i) { usdVal[i] = std::string(stringArrayDataFn[i].asChar()); } return usdAttr.Set(usdVal, usdTime); break; } case MFnData::kDoubleArray: { MFnDoubleArrayData doubleArrayDataFn(attrPlug.asMObject()); if (translateMayaDoubleToUsdSinglePrecision) { VtFloatArray usdVal(doubleArrayDataFn.length()); for (unsigned int i = 0; i < doubleArrayDataFn.length(); ++i) { usdVal[i] = (float)doubleArrayDataFn[i]; } return usdAttr.Set(usdVal, usdTime); } else { VtDoubleArray usdVal(doubleArrayDataFn.length()); for (unsigned int i = 0; i < doubleArrayDataFn.length(); ++i) { usdVal[i] = doubleArrayDataFn[i]; } return usdAttr.Set(usdVal, usdTime); } break; } case MFnData::kFloatArray: { MFnFloatArrayData floatArrayDataFn(attrPlug.asMObject()); VtFloatArray usdVal(floatArrayDataFn.length()); for (unsigned int i = 0; i < floatArrayDataFn.length(); ++i) { usdVal[i] = floatArrayDataFn[i]; } return usdAttr.Set(usdVal, usdTime); break; } case MFnData::kIntArray: { MFnIntArrayData intArrayDataFn(attrPlug.asMObject()); VtIntArray usdVal(intArrayDataFn.length()); for (unsigned int i = 0; i < intArrayDataFn.length(); ++i) { usdVal[i] = intArrayDataFn[i]; } return usdAttr.Set(usdVal, usdTime); break; } case MFnData::kPointArray: { MFnPointArrayData pointArrayDataFn(attrPlug.asMObject()); if (translateMayaDoubleToUsdSinglePrecision) { VtVec3fArray usdVal(pointArrayDataFn.length()); for (unsigned int i = 0; i < pointArrayDataFn.length(); ++i) { MPoint tmpMayaVal = pointArrayDataFn[i]; if (tmpMayaVal.w != 0) { tmpMayaVal.cartesianize(); } usdVal[i] = GfVec3f((float)tmpMayaVal[0], (float)tmpMayaVal[1], (float)tmpMayaVal[2]); } return usdAttr.Set(usdVal, usdTime); } else { VtVec3dArray usdVal(pointArrayDataFn.length()); for (unsigned int i = 0; i < pointArrayDataFn.length(); ++i) { MPoint tmpMayaVal = pointArrayDataFn[i]; if (tmpMayaVal.w != 0) { tmpMayaVal.cartesianize(); } usdVal[i] = GfVec3d(tmpMayaVal[0], tmpMayaVal[1], tmpMayaVal[2]); } return usdAttr.Set(usdVal, usdTime); } break; } case MFnData::kVectorArray: { MFnVectorArrayData vectorArrayDataFn(attrPlug.asMObject()); if (translateMayaDoubleToUsdSinglePrecision) { VtVec3fArray usdVal(vectorArrayDataFn.length()); for (unsigned int i = 0; i < vectorArrayDataFn.length(); ++i) { MVector tmpMayaVal = vectorArrayDataFn[i]; usdVal[i] = GfVec3f((float)tmpMayaVal[0], (float)tmpMayaVal[1], (float)tmpMayaVal[2]); } return usdAttr.Set(usdVal, usdTime); } else { VtVec3dArray usdVal(vectorArrayDataFn.length()); for (unsigned int i = 0; i < vectorArrayDataFn.length(); ++i) { MVector tmpMayaVal = vectorArrayDataFn[i]; usdVal[i] = GfVec3d(tmpMayaVal[0], tmpMayaVal[1], tmpMayaVal[2]); } return usdAttr.Set(usdVal, usdTime); } break; } default: break; } switch (numericDataType) { case MFnNumericData::kBoolean: { const bool usdVal(attrPlug.asBool()); return usdAttr.Set(usdVal, usdTime); break; } case MFnNumericData::kByte: case MFnNumericData::kChar: { const int usdVal(attrPlug.asChar()); return usdAttr.Set(usdVal, usdTime); break; } case MFnNumericData::kShort: { const int usdVal(attrPlug.asShort()); return usdAttr.Set(usdVal, usdTime); break; } case MFnNumericData::kInt: { const int usdVal(attrPlug.asInt()); return usdAttr.Set(usdVal, usdTime); break; } case MFnNumericData::k2Short: { short tmp1, tmp2; MFnNumericData numericDataFn(attrPlug.asMObject()); numericDataFn.getData(tmp1, tmp2); return usdAttr.Set(GfVec2i(tmp1, tmp2), usdTime); break; } case MFnNumericData::k2Int: { int tmp1, tmp2; MFnNumericData numericDataFn(attrPlug.asMObject()); numericDataFn.getData(tmp1, tmp2); return usdAttr.Set(GfVec2i(tmp1, tmp2), usdTime); break; } case MFnNumericData::k3Short: { short tmp1, tmp2, tmp3; MFnNumericData numericDataFn(attrPlug.asMObject()); numericDataFn.getData(tmp1, tmp2, tmp3); return usdAttr.Set(GfVec3i(tmp1, tmp2, tmp3), usdTime); break; } case MFnNumericData::k3Int: { int tmp1, tmp2, tmp3; MFnNumericData numericDataFn(attrPlug.asMObject()); numericDataFn.getData(tmp1, tmp2, tmp3); return usdAttr.Set(GfVec3i(tmp1, tmp2, tmp3), usdTime); break; } case MFnNumericData::kFloat: { const float usdVal(attrPlug.asFloat()); return usdAttr.Set(usdVal, usdTime); break; } case MFnNumericData::k2Float: { float tmp1, tmp2; MFnNumericData numericDataFn(attrPlug.asMObject()); numericDataFn.getData(tmp1, tmp2); return usdAttr.Set(GfVec2f(tmp1, tmp2), usdTime); break; } case MFnNumericData::k3Float: { float tmp1, tmp2, tmp3; MFnNumericData numericDataFn(attrPlug.asMObject()); numericDataFn.getData(tmp1, tmp2, tmp3); return _SetVec(usdAttr, GfVec3f(tmp1, tmp2, tmp3), usdTime); break; } case MFnNumericData::kDouble: { const double usdVal(attrPlug.asDouble()); if (translateMayaDoubleToUsdSinglePrecision) { return usdAttr.Set((float)usdVal, usdTime); } else { return usdAttr.Set(usdVal, usdTime); } break; } case MFnNumericData::k2Double: { double tmp1, tmp2; MFnNumericData numericDataFn(attrPlug.asMObject()); numericDataFn.getData(tmp1, tmp2); if (translateMayaDoubleToUsdSinglePrecision) { return usdAttr.Set(GfVec2f((float)tmp1, (float)tmp2), usdTime); } else { return usdAttr.Set(GfVec2d(tmp1, tmp2), usdTime); } break; } case MFnNumericData::k3Double: { double tmp1, tmp2, tmp3; MFnNumericData numericDataFn(attrPlug.asMObject()); numericDataFn.getData(tmp1, tmp2, tmp3); if (translateMayaDoubleToUsdSinglePrecision) { return _SetVec(usdAttr, GfVec3f((float)tmp1, (float)tmp2, (float)tmp3), usdTime); } else { return _SetVec(usdAttr, GfVec3d(tmp1, tmp2, tmp3), usdTime); } break; } case MFnNumericData::k4Double: { double tmp1, tmp2, tmp3, tmp4; MFnNumericData numericDataFn(attrPlug.asMObject()); numericDataFn.getData(tmp1, tmp2, tmp3, tmp4); if (translateMayaDoubleToUsdSinglePrecision) { return _SetVec(usdAttr, GfVec4f((float)tmp1, (float)tmp2, (float)tmp3, (float)tmp4), usdTime); } else { return _SetVec(usdAttr, GfVec4d(tmp1, tmp2, tmp3, tmp4), usdTime); } break; } default: break; } switch (unitDataType) { case MFnUnitAttribute::kAngle: case MFnUnitAttribute::kDistance: if (translateMayaDoubleToUsdSinglePrecision) { const float usdVal(attrPlug.asFloat()); return usdAttr.Set(usdVal, usdTime); } else { const double usdVal(attrPlug.asDouble()); return usdAttr.Set(usdVal, usdTime); } break; default: break; } return false; }
int main(int argc, char *argv[]) { // GfVec2f { float vals[] = { 1.0f, 2.0f }; GfVec2f v(vals); TF_AXIOM(v == GfVec2f(1,2)); float const *f = v.GetArray(); TF_AXIOM(f[0] == 1 and f[1] == 2); } // GfVec2i { int vals[] = { 1, 2 }; GfVec2i v(vals); TF_AXIOM(v == GfVec2i(1,2)); int const *i = v.GetArray(); TF_AXIOM(i[0] == 1 and i[1] == 2); v.Set(0, 1); TF_AXIOM(v == GfVec2i(0,1)); } // GfVec3i { int vals[] = { 1, 2, 3 }; GfVec3i v(vals); TF_AXIOM(v == GfVec3i(1,2,3)); int const *i = v.GetArray(); TF_AXIOM(i[0] == 1 and i[1] == 2 and i[2] == 3); v.Set(0, 1, 2); TF_AXIOM(v == GfVec3i(0,1,2)); } // GfVec4i { int vals[] = { 1, 2, 3, 4 }; GfVec4i v(vals); TF_AXIOM(v == GfVec4i(1,2,3,4)); int const *i = v.GetArray(); TF_AXIOM(i[0] == 1 and i[1] == 2 and i[2] == 3 and i[3] == 4); v.Set(0, 1, 2, 3); TF_AXIOM(v == GfVec4i(0,1,2,3)); } // GfVec3f { float vals[] = { 1.0f, 2.0f, 3.0f }; GfVec3f v(vals); TF_AXIOM(v == GfVec3f(1,2,3)); float const *f = v.GetArray(); TF_AXIOM(f[0] == 1 and f[1] == 2 and f[2] == 3); } // GfVec4f { float vals[] = { 1.0f, 2.0f, 3.0f, 4.0f }; GfVec4f v(vals); TF_AXIOM(v == GfVec4f(1,2,3,4)); float const *f = v.GetArray(); TF_AXIOM(f[0] == 1 and f[1] == 2 and f[2] == 3 and f[3] == 4); } // GfSize2, GfSize3 { size_t vals[] = {1, 2, 3}; TF_AXIOM(GfSize2(vals) == GfSize2(1,2)); TF_AXIOM(GfSize3(vals) == GfSize3(1,2,3)); } // GfMatrix2d { double vals[2][2] = {{1, 0}, {0, 1}}; TF_AXIOM(GfMatrix2d(vals) == GfMatrix2d(1)); GfMatrix2d m(vals); double const *d = m.GetArray(); TF_AXIOM(d[0] == 1 and d[1] == 0 and d[2] == 0 and d[3] == 1); } // GfMatrix2f { float vals[2][2] = {{1, 0}, {0, 1}}; TF_AXIOM(GfMatrix2f(vals) == GfMatrix2f(1)); GfMatrix2f m(vals); float const *f = m.GetArray(); TF_AXIOM(f[0] == 1 and f[1] == 0 and f[2] == 0 and f[3] == 1); } // GfMatrix3d { double vals[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; TF_AXIOM(GfMatrix3d(vals) == GfMatrix3d(1)); GfMatrix3d m(vals); double const *d = m.GetArray(); TF_AXIOM(d[0] == 1 and d[1] == 0 and d[2] == 0 and d[3] == 0 and d[4] == 1 and d[5] == 0 and d[6] == 0 and d[7] == 0 and d[8] == 1); } // GfMatrix4d { double vals[4][4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; TF_AXIOM(GfMatrix4d(vals) == GfMatrix4d(1)); GfMatrix4d m(vals); double const *d = m.GetArray(); TF_AXIOM(d[ 0] == 1 and d[ 1] == 0 and d[ 2] == 0 and d[ 3] == 0 and d[ 4] == 0 and d[ 5] == 1 and d[ 6] == 0 and d[ 7] == 0 and d[ 8] == 0 and d[ 9] == 0 and d[10] == 1 and d[11] == 0 and d[12] == 0 and d[13] == 0 and d[14] == 0 and d[15] == 1); } // half { float halfPosInf = half::posInf(); TF_AXIOM(not std::isfinite(halfPosInf)); TF_AXIOM(std::isinf(halfPosInf)); float halfNegInf = half::negInf(); TF_AXIOM(not std::isfinite(halfNegInf)); TF_AXIOM(std::isinf(halfNegInf)); float halfqNan = half::qNan(); TF_AXIOM(std::isnan(halfqNan)); float halfsNan = half::sNan(); TF_AXIOM(std::isnan(halfsNan)); } return 0; }
void USDVMP::draw(FnKat::ViewerModifierInput& input) { TF_DEBUG(KATANA_DEBUG_VMP_USD).Msg("%s @ %p : %s\n", TF_FUNC_NAME().c_str(), this, _prim.GetPath().GetString().c_str()); // Render if (_stage) { // Get draw options needed for styling. bool isSelected = input.getDrawOption("selected"); bool drawPoints = input.getDrawOption("fillPoints"); bool drawWireframe = input.getDrawOption("fillWireframe"); bool drawSmooth = input.getDrawOption("shadingSmooth"); bool isPicking = input.getDrawOption("isPicking"); // Clear out override color _params.overrideColor[3] = 0.0f; // Determine the approrpiate draw mode based on the styling options. if ( drawSmooth ) { if (_GetProxyOverlayMode() == _tokens->wireframe) { _params.drawMode = UsdImagingGL::DRAW_WIREFRAME_ON_SURFACE; } else { _params.drawMode = UsdImagingGL::DRAW_SHADED_SMOOTH; } } if ( drawWireframe ) { _params.drawMode = UsdImagingGL::DRAW_WIREFRAME; } if ( drawPoints ) { // TODO: support draw points _params.drawMode = UsdImagingGL::DRAW_POINTS; } // If this gprim is selected setup drawmode and selection color. if ( isSelected ) { _params.drawMode = UsdImagingGL::DRAW_GEOM_SMOOTH; _params.overrideColor = GfVec4f(0.0f, 1.0f, 1.0f, 1.0f); glColor4fv(_params.overrideColor.GetArray()); } if (isPicking) { if(input.getDrawOption("hasPickColor") == 1) { GfVec4f pickColor(0, 0, 0, 1); pickColor[0] = input.getDrawOptionFloat("pickColorR"); pickColor[1] = input.getDrawOptionFloat("pickColorG"); pickColor[2] = input.getDrawOptionFloat("pickColorB"); _params.overrideColor = pickColor; } else { // Most horrible hack in the world :( // Katana does it's picking by setting a shader // that takes a pick id and renders geometry with // the color representation of that id. // Unfortunately if we are using Hydra, we need to // use our own shaders. To get around this, we are // using specific knowledge of the katana pick shader // to extract the pick id and set our own override color // based on this id. This is basically emulating what the // katana shader is doing. GLint program = -1; glGetIntegerv(GL_CURRENT_PROGRAM, &program); TF_VERIFY(program != -1); GLint kat_PickIdLoc = glGetUniformLocation(program, "kat_PickId"); if (TF_VERIFY(kat_PickIdLoc != -1)) { GLint kat_PickId; glGetUniformiv(program, kat_PickIdLoc, &kat_PickId); // Simulate pick id with color _params.overrideColor = GfVec4f( ((float)((kat_PickId >> 0 ) & 0xff)) / 255.0f, ((float)((kat_PickId >> 8 ) & 0xff)) / 255.0f, ((float)((kat_PickId >> 16 ) & 0xff)) / 255.0f, 1.0f); } } // Using DRAW_GEOM_ONLY will disable lighting and make // sure we are rendering a solid color _params.drawMode = UsdImagingGL::DRAW_GEOM_ONLY; } // Save and restore shader settings around render call // because hydra does not restore shader state. GLint oldProgram = -1; glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgram); if (TF_VERIFY(_renderer)) { _renderer->SetCameraStateFromOpenGL(); glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT); if (_GetProxyOverlayMode() == _tokens->ghosted) { glEnable(GL_LIGHT0); float f = 0.1; float params[4] = { f, 0.0, f, 1.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT, params); } _renderer->SetLightingStateFromOpenGL(); glPopAttrib(); // The multi-threaded Usd Op may be loading or unloading models on // the stage we need, so we grab the global lock in reader mode // before rendering. boost::shared_lock<boost::upgrade_mutex> readerLock(UsdKatanaGetStageLock()); _renderer->Render(_prim, _params); } // Restore old shader glUseProgram(oldProgram); }
void My_TestGLDrawing::InitTest() { _renderIndex = HdRenderIndex::New(&_renderDelegate); TF_VERIFY(_renderIndex != nullptr); _delegate = new Hdx_UnitTestDelegate(_renderIndex); _delegate->SetRefineLevel(_refineLevel); // prepare render task SdfPath renderSetupTask("/renderSetupTask"); SdfPath renderTask("/renderTask"); _delegate->AddRenderSetupTask(renderSetupTask); _delegate->AddRenderTask(renderTask); // render task parameters. HdxRenderTaskParams param = _delegate->GetTaskParam( renderSetupTask, HdTokens->params).Get<HdxRenderTaskParams>(); param.enableLighting = true; // use default lighting _delegate->SetTaskParam(renderSetupTask, HdTokens->params, VtValue(param)); _delegate->SetTaskParam(renderTask, HdTokens->collection, VtValue(HdRprimCollection(HdTokens->geometry, _reprName))); // prepare scene // To ensure that the non-aggregated element index returned via picking, // we need to have at least two cubes with uniform colors. GfVec4f red(1,0,0,1), green(0,1,0,1), blue(0,0,1,1), yellow(1,1,0,1), magenta(1,0,1,1), cyan(0,1,1,1), white(1,1,1,1), black(0,0,0,1); GfVec4f faceColors[] = { red, green, blue, yellow, magenta, cyan}; VtValue faceColor = VtValue(_BuildArray(&faceColors[0], sizeof(faceColors)/sizeof(faceColors[0]))); GfVec4f vertColors[] = { white, blue, green, yellow, black, blue, magenta, red}; VtValue vertColor = VtValue(_BuildArray(&vertColors[0], sizeof(vertColors)/sizeof(vertColors[0]))); _delegate->AddCube(SdfPath("/cube0"), _GetTranslate( 5, 0, 5), /*guide=*/false, /*instancerId=*/SdfPath(), /*scheme=*/PxOsdOpenSubdivTokens->catmark, /*color=*/faceColor, /*colorInterpolation=*/Hdx_UnitTestDelegate::UNIFORM); _delegate->AddCube(SdfPath("/cube1"), _GetTranslate(-5, 0, 5), /*guide=*/false, /*instancerId=*/SdfPath(), /*scheme=*/PxOsdOpenSubdivTokens->catmark, /*color=*/faceColor, /*colorInterpolation=*/Hdx_UnitTestDelegate::UNIFORM); _delegate->AddCube(SdfPath("/cube2"), _GetTranslate(-5, 0,-5)); _delegate->AddCube(SdfPath("/cube3"), _GetTranslate( 5, 0,-5), /*guide=*/false, /*instancerId=*/SdfPath(), /*scheme=*/PxOsdOpenSubdivTokens->catmark, /*color=*/vertColor, /*colorInterpolation=*/Hdx_UnitTestDelegate::VERTEX); { _delegate->AddInstancer(SdfPath("/instancerTop")); _delegate->AddCube(SdfPath("/protoTop"), GfMatrix4d(1), false, SdfPath("/instancerTop")); std::vector<SdfPath> prototypes; prototypes.push_back(SdfPath("/protoTop")); VtVec3fArray scale(3); VtVec4fArray rotate(3); VtVec3fArray translate(3); VtIntArray prototypeIndex(3); scale[0] = GfVec3f(1); rotate[0] = GfVec4f(0); translate[0] = GfVec3f(3, 0, 2); prototypeIndex[0] = 0; scale[1] = GfVec3f(1); rotate[1] = GfVec4f(0); translate[1] = GfVec3f(0, 0, 2); prototypeIndex[1] = 0; scale[2] = GfVec3f(1); rotate[2] = GfVec4f(0); translate[2] = GfVec3f(-3, 0, 2); prototypeIndex[2] = 0; _delegate->SetInstancerProperties(SdfPath("/instancerTop"), prototypeIndex, scale, rotate, translate); } { _delegate->AddInstancer(SdfPath("/instancerBottom")); _delegate->AddCube(SdfPath("/protoBottom"), GfMatrix4d(1), false, SdfPath("/instancerBottom")); std::vector<SdfPath> prototypes; prototypes.push_back(SdfPath("/protoBottom")); VtVec3fArray scale(3); VtVec4fArray rotate(3); VtVec3fArray translate(3); VtIntArray prototypeIndex(3); scale[0] = GfVec3f(1); rotate[0] = GfVec4f(0); translate[0] = GfVec3f(3, 0, -2); prototypeIndex[0] = 0; scale[1] = GfVec3f(1); rotate[1] = GfVec4f(0); translate[1] = GfVec3f(0, 0, -2); prototypeIndex[1] = 0; scale[2] = GfVec3f(1); rotate[2] = GfVec4f(0); translate[2] = GfVec3f(-3, 0, -2); prototypeIndex[2] = 0; _delegate->SetInstancerProperties(SdfPath("/instancerBottom"), prototypeIndex, scale, rotate, translate); } SetCameraTranslate(GfVec3f(0, 0, -20)); // XXX: Setup a VAO, the current drawing engine will not yet do this. glGenVertexArrays(1, &vao); glBindVertexArray(vao); glBindVertexArray(0); }
void HdRprim::_PopulateConstantPrimVars(HdSceneDelegate* delegate, HdDrawItem *drawItem, HdDirtyBits *dirtyBits) { HD_TRACE_FUNCTION(); HF_MALLOC_TAG_FUNCTION(); SdfPath const& id = GetId(); HdRenderIndex &renderIndex = delegate->GetRenderIndex(); HdResourceRegistry *resourceRegistry = &HdResourceRegistry::GetInstance(); // XXX: this should be in a different method // XXX: This should be in HdSt getting the HdSt Shader const HdShader *shader = static_cast<const HdShader *>( renderIndex.GetSprim(HdPrimTypeTokens->shader, _surfaceShaderID)); if (shader == nullptr) { shader = static_cast<const HdShader *>( renderIndex.GetFallbackSprim(HdPrimTypeTokens->shader)); } _sharedData.surfaceShader = shader->GetShaderCode(); // update uniforms HdBufferSourceVector sources; if (HdChangeTracker::IsTransformDirty(*dirtyBits, id)) { GfMatrix4d transform = delegate->GetTransform(id); _sharedData.bounds.SetMatrix(transform); // for CPU frustum culling HdBufferSourceSharedPtr source(new HdVtBufferSource( HdTokens->transform, transform)); sources.push_back(source); source.reset(new HdVtBufferSource(HdTokens->transformInverse, transform.GetInverse())); sources.push_back(source); // if this is a prototype (has instancer), // also push the instancer transform separately. if (!_instancerID.IsEmpty()) { // gather all instancer transforms in the instancing hierarchy VtMatrix4dArray rootTransforms = _GetInstancerTransforms(delegate); VtMatrix4dArray rootInverseTransforms(rootTransforms.size()); bool leftHanded = transform.IsLeftHanded(); for (size_t i = 0; i < rootTransforms.size(); ++i) { rootInverseTransforms[i] = rootTransforms[i].GetInverse(); // flip the handedness if necessary leftHanded ^= rootTransforms[i].IsLeftHanded(); } source.reset(new HdVtBufferSource( HdTokens->instancerTransform, rootTransforms, /*staticArray=*/true)); sources.push_back(source); source.reset(new HdVtBufferSource( HdTokens->instancerTransformInverse, rootInverseTransforms, /*staticArray=*/true)); sources.push_back(source); // XXX: It might be worth to consider to have isFlipped // for non-instanced prims as well. It can improve // the drawing performance on older-GPUs by reducing // fragment shader cost, although it needs more GPU memory. // set as int (GLSL needs 32-bit align for bool) source.reset(new HdVtBufferSource( HdTokens->isFlipped, VtValue(int(leftHanded)))); sources.push_back(source); } } if (HdChangeTracker::IsExtentDirty(*dirtyBits, id)) { _sharedData.bounds.SetRange(GetExtent(delegate)); GfVec3d const & localMin = drawItem->GetBounds().GetBox().GetMin(); HdBufferSourceSharedPtr sourceMin(new HdVtBufferSource( HdTokens->bboxLocalMin, VtValue(GfVec4f( localMin[0], localMin[1], localMin[2], 0)))); sources.push_back(sourceMin); GfVec3d const & localMax = drawItem->GetBounds().GetBox().GetMax(); HdBufferSourceSharedPtr sourceMax(new HdVtBufferSource( HdTokens->bboxLocalMax, VtValue(GfVec4f( localMax[0], localMax[1], localMax[2], 0)))); sources.push_back(sourceMax); } if (HdChangeTracker::IsPrimIdDirty(*dirtyBits, id)) { GfVec4f primIdColor; int32_t primId = GetPrimId(); HdBufferSourceSharedPtr source(new HdVtBufferSource( HdTokens->primID, VtValue(primId))); sources.push_back(source); } if (HdChangeTracker::IsAnyPrimVarDirty(*dirtyBits, id)) { TfTokenVector primVarNames = delegate->GetPrimVarConstantNames(id); sources.reserve(sources.size()+primVarNames.size()); TF_FOR_ALL(nameIt, primVarNames) { if (HdChangeTracker::IsPrimVarDirty(*dirtyBits, id, *nameIt)) { VtValue value = delegate->Get(id, *nameIt); // XXX Hydra doesn't support string primvar yet if (value.IsHolding<std::string>()) continue; if (!value.IsEmpty()) { HdBufferSourceSharedPtr source( new HdVtBufferSource(*nameIt, value)); // if it's an unacceptable type, skip it (e.g. std::string) if (source->GetNumComponents() > 0) { sources.push_back(source); } } } } }
void My_TestGLDrawing::InitTest() { _renderIndex = HdRenderIndex::New(&_renderDelegate); TF_VERIFY(_renderIndex != nullptr); _delegate = new Hdx_UnitTestDelegate(_renderIndex); _delegate->SetRefineLevel(_refineLevel); // prepare render task SdfPath renderSetupTask("/renderSetupTask"); SdfPath renderTask("/renderTask"); _delegate->AddRenderSetupTask(renderSetupTask); _delegate->AddRenderTask(renderTask); // render task parameters. HdxRenderTaskParams param = _delegate->GetTaskParam( renderSetupTask, HdTokens->params).Get<HdxRenderTaskParams>(); param.enableLighting = true; // use default lighting _delegate->SetTaskParam(renderSetupTask, HdTokens->params, VtValue(param)); _delegate->SetTaskParam(renderTask, HdTokens->collection, VtValue(HdRprimCollection(HdTokens->geometry, _reprName))); // prepare scene _delegate->AddCube(SdfPath("/cube0"), _GetTranslate( 5, 0, 5)); _delegate->AddCube(SdfPath("/cube1"), _GetTranslate(-5, 0, 5)); _delegate->AddCube(SdfPath("/cube2"), _GetTranslate(-5, 0,-5)); _delegate->AddCube(SdfPath("/cube3"), _GetTranslate( 5, 0,-5)); { _delegate->AddInstancer(SdfPath("/instancerTop")); _delegate->AddCube(SdfPath("/protoTop"), GfMatrix4d(1), false, SdfPath("/instancerTop")); std::vector<SdfPath> prototypes; prototypes.push_back(SdfPath("/protoTop")); VtVec3fArray scale(3); VtVec4fArray rotate(3); VtVec3fArray translate(3); VtIntArray prototypeIndex(3); scale[0] = GfVec3f(1); rotate[0] = GfVec4f(0); translate[0] = GfVec3f(3, 0, 2); prototypeIndex[0] = 0; scale[1] = GfVec3f(1); rotate[1] = GfVec4f(0); translate[1] = GfVec3f(0, 0, 2); prototypeIndex[1] = 0; scale[2] = GfVec3f(1); rotate[2] = GfVec4f(0); translate[2] = GfVec3f(-3, 0, 2); prototypeIndex[2] = 0; _delegate->SetInstancerProperties(SdfPath("/instancerTop"), prototypeIndex, scale, rotate, translate); } { _delegate->AddInstancer(SdfPath("/instancerBottom")); _delegate->AddCube(SdfPath("/protoBottom"), GfMatrix4d(1), false, SdfPath("/instancerBottom")); std::vector<SdfPath> prototypes; prototypes.push_back(SdfPath("/protoBottom")); VtVec3fArray scale(3); VtVec4fArray rotate(3); VtVec3fArray translate(3); VtIntArray prototypeIndex(3); scale[0] = GfVec3f(1); rotate[0] = GfVec4f(0); translate[0] = GfVec3f(3, 0, -2); prototypeIndex[0] = 0; scale[1] = GfVec3f(1); rotate[1] = GfVec4f(0); translate[1] = GfVec3f(0, 0, -2); prototypeIndex[1] = 0; scale[2] = GfVec3f(1); rotate[2] = GfVec4f(0); translate[2] = GfVec3f(-3, 0, -2); prototypeIndex[2] = 0; _delegate->SetInstancerProperties(SdfPath("/instancerBottom"), prototypeIndex, scale, rotate, translate); } SetCameraTranslate(GfVec3f(0, 0, -20)); // XXX: Setup a VAO, the current drawing engine will not yet do this. glGenVertexArrays(1, &vao); glBindVertexArray(vao); glBindVertexArray(0); }
void GlfSimpleLightingContext::SetStateFromOpenGL() { // import classic GL light's parameters into shaded lights SetUseLighting(glIsEnabled(GL_LIGHTING)); GfMatrix4d worldToViewMatrix; glGetDoublev(GL_MODELVIEW_MATRIX, worldToViewMatrix.GetArray()); GfMatrix4d viewToWorldMatrix = worldToViewMatrix.GetInverse(); GLint nLights = 0; glGetIntegerv(GL_MAX_LIGHTS, &nLights); GlfSimpleLightVector lights; lights.reserve(nLights); GlfSimpleLight light; for(int i = 0; i < nLights; ++i) { int lightName = GL_LIGHT0 + i; if (glIsEnabled(lightName)) { GLfloat position[4], color[4]; glGetLightfv(lightName, GL_POSITION, position); light.SetPosition(GfVec4f(position)*viewToWorldMatrix); glGetLightfv(lightName, GL_AMBIENT, color); light.SetAmbient(GfVec4f(color)); glGetLightfv(lightName, GL_DIFFUSE, color); light.SetDiffuse(GfVec4f(color)); glGetLightfv(lightName, GL_SPECULAR, color); light.SetSpecular(GfVec4f(color)); lights.push_back(light); } } SetLights(lights); GlfSimpleMaterial material; GLfloat color[4], shininess; glGetMaterialfv(GL_FRONT, GL_AMBIENT, color); material.SetAmbient(GfVec4f(color)); glGetMaterialfv(GL_FRONT, GL_DIFFUSE, color); material.SetDiffuse(GfVec4f(color)); glGetMaterialfv(GL_FRONT, GL_SPECULAR, color); material.SetSpecular(GfVec4f(color)); glGetMaterialfv(GL_FRONT, GL_EMISSION, color); material.SetEmission(GfVec4f(color)); glGetMaterialfv(GL_FRONT, GL_SHININESS, &shininess); // clamp to 0.0001, since pow(0,0) is undefined in GLSL. shininess = std::max(0.0001f, shininess); material.SetShininess(shininess); SetMaterial(material); GfVec4f sceneAmbient; glGetFloatv(GL_LIGHT_MODEL_AMBIENT, &sceneAmbient[0]); SetSceneAmbient(sceneAmbient); }