void bindTexture(const Volume& volume, const TextureUnit& texUnit) { if (auto volumeGL = volume.getRepresentation<VolumeGL>()) { volumeGL->bindTexture(texUnit.getEnum()); } else { LogErrorCustom("TextureUtils", "Could not get a GL representation from volume"); } }
void ImageMapping::preProcess() { TextureUnit transFuncUnit; const Layer* tfLayer = transferFunction_.get().getData(); const LayerGL* transferFunctionGL = tfLayer->getRepresentation<LayerGL>(); transferFunctionGL->bindTexture(transFuncUnit.getEnum()); shader_.setUniform("transferFunc_", transFuncUnit.getUnitNumber()); }
void DepthPeelingProcessor::process() { outport_.activateTarget(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // set modelview and projection matrices glMatrixMode(GL_PROJECTION); tgt::loadMatrix(camera_.get().getProjectionMatrix()); glMatrixMode(GL_MODELVIEW); tgt::loadMatrix(camera_.get().getViewMatrix()); LGL_ERROR; TextureUnit depthTexUnit; inport_.bindDepthTexture(depthTexUnit.getEnum()); LGL_ERROR; // initialize shader shaderPrg_->activate(); // set common uniforms used by all shaders tgt::Camera cam = camera_.get(); setGlobalShaderParameters(shaderPrg_, &cam); // pass the remaining uniforms to the shader shaderPrg_->setUniform("depthTex_", depthTexUnit.getUnitNumber()); inport_.setTextureParameters(shaderPrg_, "depthTexParameters_"); std::vector<GeometryRendererBase*> portData = cpPort_.getConnectedProcessors(); for (size_t i=0; i<portData.size(); i++) { GeometryRendererBase* pdcp = portData.at(i); if(pdcp->isReady()) { pdcp->setCamera(camera_.get()); pdcp->setViewport(outport_.getSize()); pdcp->render(); LGL_ERROR; } } shaderPrg_->deactivate(); outport_.deactivateTarget(); // restore matrices glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); LGL_ERROR; }
void CanvasGL::renderLayer(size_t idx) { previousRenderedLayerIdx_ = idx; if (imageGL_) { const LayerGL* layerGL = imageGL_->getLayerGL(layerType_,idx); if (layerGL) { TextureUnit textureUnit; layerGL->bindTexture(textureUnit.getEnum()); renderTexture(textureUnit.getUnitNumber()); layerGL->unbindTexture(); return; } else { renderNoise(); } } if (!image_) renderNoise(); }
void bindTextures(const ImageOutport& outport, const TextureUnit& colorTexUnit, const TextureUnit& depthTexUnit, const TextureUnit& pickingTexUnit) { bindTextures(*outport.getData(), true, true, true, colorTexUnit.getEnum(), depthTexUnit.getEnum(), pickingTexUnit.getEnum()); }
void OcclusionSlicer::process() { // compile program if needed if (getInvalidationLevel() >= Processor::INVALID_PROGRAM) compile(); LGL_ERROR; occlusionbuffer0_.activateTarget(); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); occlusionbuffer1_.activateTarget(); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); outport_.activateTarget(); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); LGL_ERROR; // bind transfer function TextureUnit transferUnit; transferUnit.activate(); if (transferFunc_.get()) transferFunc_.get()->bind(); transferFunc_.setVolumeHandle(volumeInport_.getData()); // vector containing the volumes to bind; is passed to bindVolumes() std::vector<VolumeStruct> volumeTextures; // add main volume TextureUnit volUnit; volumeTextures.push_back(VolumeStruct( volumeInport_.getData(), &volUnit, "volume_","volumeStruct_") ); // initialize slicing shader tgt::Shader* slicingPrg = shaderProp_.getShader(); slicingPrg->activate(); // fragment shader uniforms TextureUnit occlusionUnit; transferFunc_.get()->setUniform(slicingPrg, "transferFunc_", "transferFuncTex_", transferUnit.getUnitNumber()); slicingPrg->setUniform("occlusion_", occlusionUnit.getUnitNumber()); occlusionbuffer1_.setTextureParameters(slicingPrg, "occlusionParams_"); //clipping uniforms setupUniforms(slicingPrg); // set common uniforms used by all shaders tgt::Camera cam = camera_.get(); setGlobalShaderParameters(slicingPrg, &cam); slicingPrg->setUniform("sigma_", sigma_.get()); slicingPrg->setUniform("radius_", radius_.get()); slicingPrg->setUniform("lightPos_", lightPosition_.get().xyz()); // bind the volumes and pass the necessary information to the shader bindVolumes(slicingPrg, volumeTextures, &cam, lightPosition_.get()); glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glPushMatrix(); tgt::loadMatrix(camera_.get().getProjectionMatrix(outport_.getSize())); glMatrixMode(GL_MODELVIEW); glPushMatrix(); tgt::loadMatrix(camera_.get().getViewMatrix()); unsigned int numSlices = static_cast<unsigned int>(maxLength_ / sliceDistance_); slicingPrg->activate(); for (unsigned int curSlice=0; curSlice<numSlices; curSlice++) { // first pass slicingPrg->setUniform("secondPass_", false); outport_.activateTarget(); glEnable(GL_BLEND); glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE); occlusionbuffer0_.bindColorTexture(occlusionUnit.getEnum()); glBegin(GL_POLYGON); for (unsigned int curPoint=0; curPoint<6; curPoint++) glVertex2i(curPoint, curSlice); glEnd(); glDisable(GL_BLEND); outport_.deactivateTarget(); // second pass slicingPrg->setUniform("secondPass_", true); occlusionbuffer1_.activateTarget(); slicingPrg->setUniform("blurDirection_", tgt::vec2(1.f, 0.f)); glBegin(GL_POLYGON); for (unsigned int curPoint=0; curPoint<6; curPoint++) glVertex2i(curPoint, curSlice); glEnd(); occlusionbuffer1_.deactivateTarget(); occlusionbuffer0_.activateTarget(); occlusionbuffer1_.bindColorTexture(occlusionUnit.getEnum()); slicingPrg->setUniform("blurDirection_", tgt::vec2(0.f, 1.f)); glBegin(GL_POLYGON); for (unsigned int curPoint=0; curPoint<6; curPoint++) glVertex2i(curPoint, curSlice); glEnd(); occlusionbuffer0_.deactivateTarget(); } slicingPrg->deactivate(); glEnable(GL_DEPTH_TEST); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glBlendFunc(GL_ONE, GL_ZERO); TextureUnit::setZeroUnit(); LGL_ERROR; }
void LightVolumeGL::process() { bool lightColorChanged = false; if (lightSource_.isChanged()) { lightColorChanged = lightSourceChanged(); } bool reattach = false; if (internalVolumesInvalid_ || lightColorChanged || inport_.isChanged()) { reattach = volumeChanged(lightColorChanged); } VolumeGL* outVolumeGL = volume_->getEditableRepresentation<VolumeGL>(); TextureUnit volUnit; const VolumeGL* inVolumeGL = inport_.getData()->getRepresentation<VolumeGL>(); inVolumeGL->bindTexture(volUnit.getEnum()); TextureUnit transFuncUnit; const Layer* tfLayer = transferFunction_.get().getData(); const LayerGL* transferFunctionGL = tfLayer->getRepresentation<LayerGL>(); transferFunctionGL->bindTexture(transFuncUnit.getEnum()); TextureUnit lightVolUnit[2]; propParams_[0].vol->bindTexture(lightVolUnit[0].getEnum()); propParams_[1].vol->bindTexture(lightVolUnit[1].getEnum()); propagationShader_.activate(); propagationShader_.setUniform("volume_", volUnit.getUnitNumber()); utilgl::setShaderUniforms(propagationShader_, *inport_.getData(), "volumeParameters_"); propagationShader_.setUniform("transferFunc_", transFuncUnit.getUnitNumber()); propagationShader_.setUniform("lightVolumeParameters_.dimensions", volumeDimOutF_); propagationShader_.setUniform("lightVolumeParameters_.reciprocalDimensions", volumeDimOutFRCP_); BufferObjectArray* rectArray = utilgl::enableImagePlaneRect(); //Perform propagation passes for (int i=0; i<2; ++i) { propParams_[i].fbo->activate(); glViewport(0, 0, static_cast<GLsizei>(volumeDimOut_.x), static_cast<GLsizei>(volumeDimOut_.y)); if (reattach) propParams_[i].fbo->attachColorTexture(propParams_[i].vol->getTexture().get(), 0); propagationShader_.setUniform("lightVolume_", lightVolUnit[i].getUnitNumber()); propagationShader_.setUniform("permutationMatrix_", propParams_[i].axisPermutation); if (lightSource_.getData()->getLightSourceType() == LightSourceType::LIGHT_POINT) { propagationShader_.setUniform("lightPos_", lightPos_); propagationShader_.setUniform("permutedLightMatrix_", propParams_[i].axisPermutationLight); } else { propagationShader_.setUniform("permutedLightDirection_", propParams_[i].permutedLightDirection); } for (unsigned int z=0; z<volumeDimOut_.z; ++z) { glFramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_3D, propParams_[i].vol->getTexture()->getID(), 0, z); propagationShader_.setUniform("sliceNum_", static_cast<GLint>(z)); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glFlush(); } propParams_[i].fbo->deactivate(); } utilgl::disableImagePlaneRect(rectArray); propagationShader_.deactivate(); mergeShader_.activate(); mergeShader_.setUniform("lightVolume_", lightVolUnit[0].getUnitNumber()); mergeShader_.setUniform("lightVolumeSec_", lightVolUnit[1].getUnitNumber()); mergeShader_.setUniform("lightVolumeParameters_.dimensions", volumeDimOutF_); mergeShader_.setUniform("lightVolumeParameters_.reciprocalDimensions", volumeDimOutFRCP_); mergeShader_.setUniform("permMatInv_", propParams_[0].axisPermutationINV); mergeShader_.setUniform("permMatInvSec_", propParams_[1].axisPermutationINV); mergeShader_.setUniform("blendingFactor_", blendingFactor_); //Perform merge pass mergeFBO_->activate(); glViewport(0, 0, static_cast<GLsizei>(volumeDimOut_.x), static_cast<GLsizei>(volumeDimOut_.y)); if (reattach) mergeFBO_->attachColorTexture(outVolumeGL->getTexture().get(), 0); utilgl::multiDrawImagePlaneRect(static_cast<int>(volumeDimOut_.z)); mergeShader_.deactivate(); mergeFBO_->deactivate(); }
bool ImageGL::copyRepresentationsTo(ImageGL* target) const { const ImageGL* source = this; auto singleChannel = source->getColorLayerGL()->getDataFormat()->getComponents() == 1; // Set shader to copy all color layers if (!shader_.isReady() || singleChanelCopy_ != singleChannel || colorLayerCopyCount_ != colorLayersGL_.size()) { std::stringstream ssUniform; for (size_t i = 1; i < colorLayersGL_.size(); ++i) { ssUniform << "layout(location = " << i + 1 << ") out vec4 FragData" << i << ";"; } for (size_t i = 1; i < colorLayersGL_.size(); ++i) { ssUniform << "uniform sampler2D color" << i << ";"; } shader_.getFragmentShaderObject()->addShaderDefine("ADDITIONAL_COLOR_LAYER_OUT_UNIFORMS", ssUniform.str()); std::stringstream ssWrite; for (size_t i = 1; i < colorLayersGL_.size(); ++i) { if (singleChannel) { ssWrite << "FragData" << i << " = vec4(texture(color" << i << ", texCoord_.xy).r);"; } else { ssWrite << "FragData" << i << " = texture(color" << i << ", texCoord_.xy);"; } } shader_.getFragmentShaderObject()->addShaderDefine("ADDITIONAL_COLOR_LAYER_WRITE", ssWrite.str()); if (colorLayersGL_.size() > 1) { shader_.getFragmentShaderObject()->addShaderDefine("ADDITIONAL_COLOR_LAYERS"); } else { shader_.getFragmentShaderObject()->removeShaderDefine("ADDITIONAL_COLOR_LAYERS"); } if (singleChannel) { shader_.getFragmentShaderObject()->addShaderDefine("SINGLE_CHANNEL"); } else { shader_.getFragmentShaderObject()->removeShaderDefine("SINGLE_CHANNEL"); } colorLayerCopyCount_ = colorLayersGL_.size(); singleChanelCopy_ = singleChannel; shader_.build(); } TextureUnit colorUnit, depthUnit, pickingUnit; source->getColorLayerGL()->bindTexture(colorUnit.getEnum()); if (source->getDepthLayerGL()) { source->getDepthLayerGL()->bindTexture(depthUnit.getEnum()); } if (source->getPickingLayerGL()) { source->getPickingLayerGL()->bindTexture(pickingUnit.getEnum()); } TextureUnitContainer additionalColorUnits; for (size_t i = 0; i < colorLayersGL_.size(); ++i) { TextureUnit unit; source->getColorLayerGL(i)->bindTexture(unit.getEnum()); additionalColorUnits.push_back(std::move(unit)); } // Render to FBO, with correct scaling target->activateBuffer(ImageType::AllLayers); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); float ratioSource = (float)source->getDimensions().x / (float)source->getDimensions().y; float ratioTarget = (float)target->getDimensions().x / (float)target->getDimensions().y; glm::mat4 scale; if (ratioTarget < ratioSource) scale = glm::scale(glm::vec3(1.0f, ratioTarget / ratioSource, 1.0f)); else scale = glm::scale(glm::vec3(ratioSource / ratioTarget, 1.0f, 1.0f)); GLint prog; glGetIntegerv(GL_CURRENT_PROGRAM, &prog); shader_.activate(); shader_.setUniform("color_", colorUnit.getUnitNumber()); if (source->getDepthLayerGL()) { shader_.setUniform("depth_", depthUnit.getUnitNumber()); } if (source->getPickingLayerGL()) { shader_.setUniform("picking_", pickingUnit.getUnitNumber()); } for (size_t i = 0; i < additionalColorUnits.size(); ++i) { shader_.setUniform("color" + toString<size_t>(i + 1), additionalColorUnits[i].getUnitNumber()); } shader_.setUniform("dataToClip", scale); LGL_ERROR; target->renderImagePlaneRect(); LGL_ERROR; shader_.deactivate(); target->deactivateBuffer(); LGL_ERROR; glUseProgram(prog); return true; }
void bindDepthTexture(const Image& image, const TextureUnit& texUnit) { bindTextures(image, false, true, false, 0, texUnit.getEnum(), 0); }
void bindTexture(const TransferFunctionProperty& tfp, const TextureUnit& texUnit) { if (auto tfLayer = tfp.get().getData()) { auto transferFunctionGL = tfLayer->getRepresentation<LayerGL>(); transferFunctionGL->bindTexture(texUnit.getEnum()); } }
void bindDepthTexture(const ImageInport& inport, const TextureUnit& texUnit) { bindTextures(*inport.getData(), false, true, false, 0, texUnit.getEnum(), 0); }
void bindColorTexture(const ImageOutport& outport, const TextureUnit& texUnit) { bindTextures(*outport.getData(), true, false, false, texUnit.getEnum(), 0, 0); }
void bindTexture(const Texture& texture, const TextureUnit& texUnit) { glActiveTexture(texUnit.getEnum()); texture.bind(); glActiveTexture(GL_TEXTURE0); }
void bindTextures(const Image& image, const TextureUnit& colorTexUnit, const TextureUnit& depthTexUnit, const TextureUnit& pickingTexUnit) { bindTextures(image, true, true, true, colorTexUnit.getEnum(), depthTexUnit.getEnum(), pickingTexUnit.getEnum()); }
void bindTextures(const ImageInport& inport, const TextureUnit& colorTexUnit, const TextureUnit& depthTexUnit) { bindTextures(*inport.getData(), true, true, false, colorTexUnit.getEnum(), depthTexUnit.getEnum(), 0); }
void bindTextures(const Image& image, const TextureUnit& colorTexUnit, const TextureUnit& depthTexUnit) { bindTextures(image, true, true, false, colorTexUnit.getEnum(), depthTexUnit.getEnum(), 0); }
void bindPickingTexture(const ImageOutport& outport, const TextureUnit& texUnit) { bindTextures(*outport.getData(), false, false, true, 0, 0, texUnit.getEnum()); }
void bindPickingTexture(const Image& image, const TextureUnit& texUnit) { bindTextures(image, false, false, true, 0, 0, texUnit.getEnum()); }