void VisualizationProcessor::createAndAttachTexture(GLint internalFormat) {
        GLenum attachment = 0;
        switch(internalFormat) {
            case GL_R32F:
            case GL_RGB:
            case GL_RGB16F_ARB:
            case GL_RGBA:
            case GL_RGBA8:
            case GL_RGBA16:
            case GL_RGBA16F:
            case GL_RGBA32F:
                if (_fbo->getNumColorAttachments() >= static_cast<size_t>(GpuCaps.getMaxColorAttachments())) {
                    cgtAssert(false, "Tried to attach more color textures to FBO than supported!");
                    LWARNING("Tried to attach more color textures to FBO than supported, aborted.");
                    return;
                }
                attachment = static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + _fbo->getNumColorAttachments());
                break;

            case GL_DEPTH_COMPONENT16:
            case GL_DEPTH_COMPONENT24:
#ifdef GL_DEPTH_COMPONENT32F
            case GL_DEPTH_COMPONENT32F:
#endif
                cgtAssert(_fbo->getDepthAttachment() == 0, "Tried to attach more than one depth texture.");
                attachment = GL_DEPTH_ATTACHMENT;
                break;

            default:
                cgtAssert(false, "Unknown internal format!");
        }
        createAndAttachTexture(internalFormat, attachment);
    }
    void SimpleRaycaster::processImpl(DataContainer& data, ImageRepresentationGL::ScopedRepresentation& image) {
        ScopedTypedData<LightSourceData> light(data, p_lightId.getValue());

        if (p_enableShading.getValue() == false || light != nullptr) {
            FramebufferActivationGuard f*g(this);
            createAndAttachTexture(GL_RGBA8);
            createAndAttachTexture(GL_RGBA32F);
            createAndAttachTexture(GL_RGBA32F);
            createAndAttachDepthTexture();

            static const GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 , GL_COLOR_ATTACHMENT2 };
            glDrawBuffers(3, buffers);

            if (p_enableShading.getValue() && light != nullptr) {
                light->bind(_shader, "_lightSource");
            }
            if (p_enableShadowing.getValue()) {
                _shader->setUniform("_shadowIntensity", p_shadowIntensity.getValue());
            }

            glEnable(GL_DEPTH_TEST);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            QuadRdr.renderQuad();

            // restore state
            glDrawBuffers(1, buffers);
            glDisable(GL_DEPTH_TEST);
            LGL_ERROR;

            data.addData(p_targetImageID.getValue(), new RenderData(_fbo));


        
        }
        else {
            LDEBUG("Could not load light source from DataContainer.");
        }
    }
Example #3
0
void ofFbo::setup(Settings settings) {
	checkGLSupport();

	destroy();

	if(settings.width == 0) settings.width = ofGetWidth();
	if(settings.height == 0) settings.height = ofGetHeight();
	this->settings = settings;

	// create main fbo
	// this is the main one we bind for drawing into
	// all the renderbuffers are attached to this (whether MSAA is enabled or not)
	glGenFramebuffers(1, &fbo);
	bind();

		
	// If we want both a depth AND a stencil buffer tehn combine them into a single buffer
	if( settings.useDepth && settings.useStencil )
	{
		stencilBuffer = depthBuffer = createAndAttachRenderbuffer(GL_DEPTH_STENCIL, GL_DEPTH_STENCIL_ATTACHMENT);
	}
	else
	{
	// if we want a depth buffer, create it, and attach to our main fbo
	if(settings.useDepth) depthBuffer = createAndAttachRenderbuffer(GL_DEPTH_COMPONENT, GL_DEPTH_ATTACHMENT);

	// if we want a stencil buffer, create it, and attach to our main fbo
	if(settings.useStencil) stencilBuffer = createAndAttachRenderbuffer(GL_STENCIL_INDEX, GL_STENCIL_ATTACHMENT);
	}

	// if we want MSAA, create a new fbo for textures
	if(settings.numSamples) glGenFramebuffers(1, &fboTextures);
	else fboTextures = fbo;

	// now create all textures and color buffers
	for(int i=0; i<settings.numColorbuffers; i++) createAndAttachTexture(i);

	// if textures are attached to a different fbo (e.g. if using MSAA) check it's status
	if(fbo != fboTextures) {
		glBindFramebuffer(GL_FRAMEBUFFER, fboTextures);
	}

	// check everything is ok with this fbo
	checkStatus();
	
	// unbind it
	unbind();
}
Example #4
0
void ofFbo::allocate(Settings _settings) {
	if(!checkGLSupport()) return;

	destroy();

	if(_settings.width == 0) _settings.width = ofGetWidth();
	if(_settings.height == 0) _settings.height = ofGetHeight();
	if(_settings.numSamples > maxSamples()) {
		ofLogWarning() << "clamping numSamples (" << _settings.numSamples << ") to maxSamples (" << maxSamples() << ")";
		_settings.numSamples = maxSamples();
	}
	settings = _settings;

	// create main fbo
	// this is the main one we bind for drawing into
	// all the renderbuffers are attached to this (whether MSAA is enabled or not)
	glGenFramebuffers(1, &fbo);
	retainFB(fbo);
	bind();

	if(settings.depthStencilAsTexture && settings.numSamples){
		ofLogWarning() << "multisampling not supported with depth as texture, setting 0 samples";
		settings.numSamples = 0;
	}

	//currently depth only works if stencil is enabled. 
	// http://forum.openframeworks.cc/index.php/topic,6837.0.html
#ifdef TARGET_OPENGLES
	if(settings.useDepth){
	  	settings.useStencil = true;
	}
    if( settings.depthStencilAsTexture ){
        settings.depthStencilAsTexture = false;
        ofLogWarning() << "ofFbo::Settings depthStencilAsTexture is not available for iOS" << endl;
    }
#endif

	GLenum depthAttachment;
	GLint depthPixelType;
	GLint depthFormat;

	if( settings.useDepth && settings.useStencil ){
		depthFormat = GL_DEPTH_STENCIL;
		settings.depthStencilInternalFormat = GL_DEPTH_STENCIL;
		depthPixelType = GL_UNSIGNED_INT_24_8;
		#ifdef TARGET_OPENGLES
			depthAttachment = GL_DEPTH_ATTACHMENT;
		#else
			depthAttachment = GL_DEPTH_STENCIL_ATTACHMENT;
		#endif
	}else if(settings.useDepth){
		depthPixelType = GL_UNSIGNED_SHORT;
		if(settings.depthStencilInternalFormat==GL_DEPTH_COMPONENT16){
			depthPixelType = GL_UNSIGNED_SHORT;
		}else if(settings.depthStencilInternalFormat==GL_DEPTH_COMPONENT24){
			depthPixelType = GL_UNSIGNED_INT;
		}
        #ifdef GL_DEPTH_COMPONENT32 
        else if(settings.depthStencilInternalFormat==GL_DEPTH_COMPONENT32){
			depthPixelType = GL_UNSIGNED_INT;
		}
		#endif 
		depthAttachment = GL_DEPTH_ATTACHMENT;
		depthFormat = GL_DEPTH_COMPONENT;
	}else if(settings.useStencil){
		depthAttachment = GL_STENCIL_ATTACHMENT;
		settings.depthStencilInternalFormat = GL_STENCIL_INDEX;
		depthFormat = GL_STENCIL_INDEX;
		depthPixelType = GL_UNSIGNED_BYTE;
	}

	//- USE REGULAR RENDER BUFFER
	if(!settings.depthStencilAsTexture){
		if(settings.useDepth && settings.useStencil){
			stencilBuffer = depthBuffer = createAndAttachRenderbuffer(settings.depthStencilInternalFormat, depthAttachment);
			retainRB(stencilBuffer);
			retainRB(depthBuffer);	
		}else if(settings.useDepth){
			depthBuffer = createAndAttachRenderbuffer(settings.depthStencilInternalFormat, depthAttachment);
			retainRB(depthBuffer);
		}else if(settings.useStencil){
			stencilBuffer = createAndAttachRenderbuffer(settings.depthStencilInternalFormat, depthAttachment);
			retainRB(stencilBuffer);
		}
	//- INSTEAD USE TEXTURE
	}else{
		if(settings.useDepth || settings.useStencil){
		createAndAttachDepthStencilTexture(settings.textureTarget,settings.depthStencilInternalFormat,depthFormat,depthPixelType,depthAttachment);
#ifdef TARGET_OPENGLES
		// if there's depth and stencil the texture should be attached as
		// depth and stencil attachments
		// http://www.khronos.org/registry/gles/extensions/OES/OES_packed_depth_stencil.txt
		if(settings.useDepth && settings.useStencil){
	        glFramebufferTexture2D(GL_FRAMEBUFFER,
	                               GL_STENCIL_ATTACHMENT,
	                               GL_TEXTURE_2D, depthBufferTex.texData.textureID, 0);
		}
#endif
	}
	}

	// if we want MSAA, create a new fbo for textures
	#ifndef TARGET_OPENGLES
		if(settings.numSamples){
			glGenFramebuffers(1, &fboTextures);
			retainFB(fboTextures);
		}else{
			fboTextures = fbo;
		}
	#else
		fboTextures = fbo;
		if(settings.numSamples){
			ofLog(OF_LOG_WARNING,"ofFbo: multisampling not supported in opengles");
		}
	#endif

	// now create all textures and color buffers
	for(int i=0; i<settings.numColorbuffers; i++) createAndAttachTexture(i);

	// if textures are attached to a different fbo (e.g. if using MSAA) check it's status
	if(fbo != fboTextures) {
		glBindFramebuffer(GL_FRAMEBUFFER, fboTextures);
	}

	// check everything is ok with this fbo
	checkStatus();

	// unbind it
	unbind();

	bIsAllocated = true;
}
Example #5
0
//--------------------------------------------------------------
void ofFbo::allocate(Settings _settings) {
	if(!checkGLSupport()) return;

	clear();
	auto renderer = _settings.renderer.lock();
	if(renderer){
		settings.renderer = renderer;
	}else{
		settings.renderer = ofGetGLRenderer();
	}

	// check that passed values are correct
	if(_settings.width <= 0 || _settings.height <= 0){
		ofLogError("ofFbo") << "width and height have to be more than 0";
	}
	if(_settings.numSamples > maxSamples() && maxSamples() > -1) {
		ofLogWarning("ofFbo") << "allocate(): clamping numSamples " << _settings.numSamples << " to maxSamples " << maxSamples() << " for frame buffer object" << fbo;
		_settings.numSamples = maxSamples();
	}

	if(_settings.depthStencilAsTexture && _settings.numSamples){
		ofLogWarning("ofFbo") << "allocate(): multisampling not supported with depthStencilAsTexture, setting 0 samples for frame buffer object " << fbo;
		_settings.numSamples = 0;
	}

	//currently depth only works if stencil is enabled. 
	// http://forum.openframeworks.cc/index.php/topic,6837.0.html
#ifdef TARGET_OPENGLES
	if(_settings.useDepth){
	  	_settings.useStencil = true;
	}
    if( _settings.depthStencilAsTexture ){
        _settings.depthStencilAsTexture = false;
        ofLogWarning("ofFbo") << "allocate(): depthStencilAsTexture is not available for iOS";
    }
#endif
    
	GLenum depthAttachment = GL_DEPTH_ATTACHMENT;

	if( _settings.useDepth && _settings.useStencil ){
		_settings.depthStencilInternalFormat = GL_DEPTH_STENCIL;
		#ifdef TARGET_OPENGLES
			depthAttachment = GL_DEPTH_ATTACHMENT;
		#else
			depthAttachment = GL_DEPTH_STENCIL_ATTACHMENT;
		#endif
	}else if(_settings.useDepth){
		depthAttachment = GL_DEPTH_ATTACHMENT;
	}else if(_settings.useStencil){
		depthAttachment = GL_STENCIL_ATTACHMENT;
		_settings.depthStencilInternalFormat = GL_STENCIL_INDEX;
	}

	// set needed values for allocation on instance settings
	// the rest will be set by the corresponding methods during allocation
	settings.width = _settings.width;
	settings.height = _settings.height;
	settings.numSamples = _settings.numSamples;

	// create main fbo
	// this is the main one we bind for drawing into
	// all the renderbuffers are attached to this (whether MSAA is enabled or not)
	glGenFramebuffers(1, &fbo);
	retainFB(fbo);

	GLint previousFboId = 0;

	// note that we are using a glGetInteger method here, which may stall the pipeline.
	// in the allocate() method, this is not that tragic since this will not be called 
	// within the draw() loop. Here, we need not optimise for performance, but for 
	// simplicity and readability .

	glGetIntegerv(GL_FRAMEBUFFER_BINDING, &previousFboId);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);

	//- USE REGULAR RENDER BUFFER
	if(!_settings.depthStencilAsTexture){
		if(_settings.useDepth && _settings.useStencil){
			stencilBuffer = depthBuffer = createAndAttachRenderbuffer(_settings.depthStencilInternalFormat, depthAttachment);
			retainRB(stencilBuffer);
			retainRB(depthBuffer);
		}else if(_settings.useDepth){
			depthBuffer = createAndAttachRenderbuffer(_settings.depthStencilInternalFormat, depthAttachment);
			retainRB(depthBuffer);
		}else if(_settings.useStencil){
			stencilBuffer = createAndAttachRenderbuffer(_settings.depthStencilInternalFormat, depthAttachment);
			retainRB(stencilBuffer);
		}
	//- INSTEAD USE TEXTURE
	}else{
		if(_settings.useDepth || _settings.useStencil){
			createAndAttachDepthStencilTexture(_settings.textureTarget,_settings.depthStencilInternalFormat,depthAttachment);
			#ifdef TARGET_OPENGLES
				// if there's depth and stencil the texture should be attached as
				// depth and stencil attachments
				// http://www.khronos.org/registry/gles/extensions/OES/OES_packed_depth_stencil.txt
				if(_settings.useDepth && _settings.useStencil){
					glFramebufferTexture2D(GL_FRAMEBUFFER,
										   GL_STENCIL_ATTACHMENT,
										   GL_TEXTURE_2D, depthBufferTex.texData.textureID, 0);
				}
			#endif
		}
	}
    
    settings.useDepth = _settings.useDepth;
    settings.useStencil = _settings.useStencil;
    settings.depthStencilInternalFormat = _settings.depthStencilInternalFormat;
    settings.depthStencilAsTexture = _settings.depthStencilAsTexture;
    settings.textureTarget = _settings.textureTarget;
    settings.wrapModeHorizontal = _settings.wrapModeHorizontal;
    settings.wrapModeVertical = _settings.wrapModeVertical;
    settings.maxFilter = _settings.maxFilter;
    settings.minFilter = _settings.minFilter;

	// if we want MSAA, create a new fbo for textures
	#ifndef TARGET_OPENGLES
		if(_settings.numSamples){
			glGenFramebuffers(1, &fboTextures);
			retainFB(fboTextures);
		}else{
			fboTextures = fbo;
		}
	#else
		fboTextures = fbo;
		if(_settings.numSamples){
			ofLogWarning("ofFbo") << "allocate(): multisampling not supported in OpenGL ES";
		}
	#endif

	// now create all textures and color buffers
	if(_settings.colorFormats.size() > 0) {
		for(int i=0; i<(int)_settings.colorFormats.size(); i++) createAndAttachTexture(_settings.colorFormats[i], i);
	} else if(_settings.numColorbuffers > 0) {
		for(int i=0; i<_settings.numColorbuffers; i++) createAndAttachTexture(_settings.internalformat, i);
		_settings.colorFormats = settings.colorFormats;
	} else {
		ofLogWarning("ofFbo") << "allocate(): no color buffers specified for frame buffer object " << fbo;
	}
	settings.internalformat = _settings.internalformat;
	
	dirty.resize(_settings.colorFormats.size(), true); // we start with all color buffers dirty.

	// if textures are attached to a different fbo (e.g. if using MSAA) check it's status
	if(fbo != fboTextures) {
		glBindFramebuffer(GL_FRAMEBUFFER, fboTextures);
	}

	// check everything is ok with this fbo
	bIsAllocated = checkStatus();

	// restore previous framebuffer id
	glBindFramebuffer(GL_FRAMEBUFFER, previousFboId);

    /* UNCOMMENT OUTSIDE OF DOING RELEASES
	
    // this should never happen
	if(settings != _settings) ofLogWarning("ofFbo") << "allocation not complete, passed settings not equal to created ones, this is an internal OF bug";
    
    */
#ifdef TARGET_ANDROID
	ofAddListener(ofxAndroidEvents().reloadGL,this,&ofFbo::reloadFbo);
#endif
}
Example #6
0
void ofFbo::setup(Settings _settings) {
	checkGLSupport();

	destroy();

	if(_settings.width == 0) _settings.width = ofGetWidth();
	if(_settings.height == 0) _settings.height = ofGetHeight();
	settings = _settings;

	// create main fbo
	// this is the main one we bind for drawing into
	// all the renderbuffers are attached to this (whether MSAA is enabled or not)
	glGenFramebuffers(1, &fbo);
	retainFB(fbo);
	bind();

		
	// If we want both a depth AND a stencil buffer tehn combine them into a single buffer
#ifndef TARGET_OPENGLES
	if( settings.useDepth && settings.useStencil )
	{
		stencilBuffer = depthBuffer = createAndAttachRenderbuffer(GL_DEPTH_STENCIL, GL_DEPTH_STENCIL_ATTACHMENT);
		retainRB(depthBuffer);
		retainRB(stencilBuffer);
	}else
#endif
	{
		// if we want a depth buffer, create it, and attach to our main fbo
		if(settings.useDepth){
			depthBuffer = createAndAttachRenderbuffer(GL_DEPTH_COMPONENT, GL_DEPTH_ATTACHMENT);
			retainRB(depthBuffer);
		}

		// if we want a stencil buffer, create it, and attach to our main fbo
		if(settings.useStencil){
			stencilBuffer = createAndAttachRenderbuffer(GL_STENCIL_INDEX, GL_STENCIL_ATTACHMENT);
			retainRB(stencilBuffer);
		}
	}
	// if we want MSAA, create a new fbo for textures
#ifndef TARGET_OPENGLES
	if(settings.numSamples){
		glGenFramebuffers(1, &fboTextures);
		retainFB(fboTextures);
	}else{
		fboTextures = fbo;
	}
#else
	fboTextures = fbo;
	if(settings.numSamples){
		ofLog(OF_LOG_WARNING,"ofFbo: multisampling not supported in opengles");
	}
#endif
	// now create all textures and color buffers
	for(int i=0; i<settings.numColorbuffers; i++) createAndAttachTexture(i);

	// if textures are attached to a different fbo (e.g. if using MSAA) check it's status
	if(fbo != fboTextures) {
		glBindFramebuffer(GL_FRAMEBUFFER, fboTextures);
	}

	// check everything is ok with this fbo
	checkStatus();
	
	// unbind it
	unbind();
}
Example #7
0
void ofFbo::allocate(Settings _settings) {
	if(!checkGLSupport()) return;

	destroy();

	// check that passed values are correct
	if(_settings.width == 0) _settings.width = ofGetWidth();
	if(_settings.height == 0) _settings.height = ofGetHeight();
	if(_settings.numSamples > maxSamples() && maxSamples() > -1) {
		ofLogWarning("ofFbo") << "allocate(): clamping numSamples " << _settings.numSamples << " to maxSamples " << maxSamples() << " for frame buffer object" << fbo;
		_settings.numSamples = maxSamples();
	}

	if(_settings.depthStencilAsTexture && _settings.numSamples){
		ofLogWarning("ofFbo") << "allocate(): multisampling not supported with depthStencilAsTexture, setting 0 samples for frame buffer object " << fbo;
		_settings.numSamples = 0;
	}

	//currently depth only works if stencil is enabled. 
	// http://forum.openframeworks.cc/index.php/topic,6837.0.html
#ifdef TARGET_OPENGLES
	if(_settings.useDepth){
	  	_settings.useStencil = true;
	}
    if( _settings.depthStencilAsTexture ){
        _settings.depthStencilAsTexture = false;
        ofLogWarning("ofFbo") << "allocate(): depthStencilAsTexture is not available for iOS";
    }
#endif
    
	GLenum depthAttachment = GL_DEPTH_ATTACHMENT;

	if( _settings.useDepth && _settings.useStencil ){
		_settings.depthStencilInternalFormat = GL_DEPTH_STENCIL;
		#ifdef TARGET_OPENGLES
			depthAttachment = GL_DEPTH_ATTACHMENT;
		#else
			depthAttachment = GL_DEPTH_STENCIL_ATTACHMENT;
		#endif
	}else if(_settings.useDepth){
		depthAttachment = GL_DEPTH_ATTACHMENT;
	}else if(_settings.useStencil){
		depthAttachment = GL_STENCIL_ATTACHMENT;
		_settings.depthStencilInternalFormat = GL_STENCIL_INDEX;
	}

	// set needed values for allocation on instance settings
	// the rest will be set by the corresponding methods during allocation
	settings.width = _settings.width;
	settings.height = _settings.height;
	settings.numSamples = _settings.numSamples;

	// create main fbo
	// this is the main one we bind for drawing into
	// all the renderbuffers are attached to this (whether MSAA is enabled or not)
	glGenFramebuffers(1, &fbo);
	retainFB(fbo);
	bind();

	//- USE REGULAR RENDER BUFFER
	if(!_settings.depthStencilAsTexture){
		if(_settings.useDepth && _settings.useStencil){
			stencilBuffer = depthBuffer = createAndAttachRenderbuffer(_settings.depthStencilInternalFormat, depthAttachment);
			retainRB(stencilBuffer);
			retainRB(depthBuffer);
		}else if(_settings.useDepth){
			depthBuffer = createAndAttachRenderbuffer(_settings.depthStencilInternalFormat, depthAttachment);
			retainRB(depthBuffer);
		}else if(_settings.useStencil){
			stencilBuffer = createAndAttachRenderbuffer(_settings.depthStencilInternalFormat, depthAttachment);
			retainRB(stencilBuffer);
		}
	//- INSTEAD USE TEXTURE
	}else{
		if(_settings.useDepth || _settings.useStencil){
			createAndAttachDepthStencilTexture(_settings.textureTarget,_settings.depthStencilInternalFormat,depthAttachment);
			#ifdef TARGET_OPENGLES
				// if there's depth and stencil the texture should be attached as
				// depth and stencil attachments
				// http://www.khronos.org/registry/gles/extensions/OES/OES_packed_depth_stencil.txt
				if(_settings.useDepth && _settings.useStencil){
					glFramebufferTexture2D(GL_FRAMEBUFFER,
										   GL_STENCIL_ATTACHMENT,
										   GL_TEXTURE_2D, depthBufferTex.texData.textureID, 0);
				}
			#endif
		}
	}
    
    settings.useDepth = _settings.useDepth;
    settings.useStencil = _settings.useStencil;
    settings.depthStencilInternalFormat = _settings.depthStencilInternalFormat;
    settings.depthStencilAsTexture = _settings.depthStencilAsTexture;
    settings.textureTarget = _settings.textureTarget;
    settings.wrapModeHorizontal = _settings.wrapModeHorizontal;
    settings.wrapModeVertical = _settings.wrapModeVertical;

	// if we want MSAA, create a new fbo for textures
	#ifndef TARGET_OPENGLES
		if(_settings.numSamples){
			glGenFramebuffers(1, &fboTextures);
			retainFB(fboTextures);
		}else{
			fboTextures = fbo;
		}
	#else
		fboTextures = fbo;
		if(_settings.numSamples){
			ofLogWarning("ofFbo") << "allocate(): multisampling not supported in OpenGL ES";
		}
	#endif

	// now create all textures and color buffers
	if(_settings.colorFormats.size() > 0) {
		for(int i=0; i<(int)_settings.colorFormats.size(); i++) createAndAttachTexture(_settings.colorFormats[i], i);
	} else if(_settings.numColorbuffers > 0) {
		for(int i=0; i<_settings.numColorbuffers; i++) createAndAttachTexture(_settings.internalformat, i);
		_settings.colorFormats = settings.colorFormats;
	} else {
		ofLogWarning("ofFbo") << "allocate(): no color buffers specified for frame buffer object " << fbo;
	}
	settings.internalformat = _settings.internalformat;


	// if textures are attached to a different fbo (e.g. if using MSAA) check it's status
	if(fbo != fboTextures) {
		glBindFramebuffer(GL_FRAMEBUFFER, fboTextures);
	}

	// check everything is ok with this fbo
	bIsAllocated = checkStatus();

	// unbind it
	unbind();

    /* UNCOMMENT OUTSIDE OF DOING RELEASES
	
    // this should never happen
	if(settings != _settings) ofLogWarning("ofFbo") << "allocation not complete, passed settings not equal to created ones, this is an internal OF bug";
    
    */
}
Example #8
0
void ofFbo::allocate(Settings _settings) {
	if(!checkGLSupport()) return;

	destroy();

	if(_settings.width == 0) _settings.width = ofGetWidth();
	if(_settings.height == 0) _settings.height = ofGetHeight();
	settings = _settings;

	// create main fbo
	// this is the main one we bind for drawing into
	// all the renderbuffers are attached to this (whether MSAA is enabled or not)
	glGenFramebuffers(1, &fbo);
	retainFB(fbo);
	bind();

		
	// If we want both a depth AND a stencil buffer tehn combine them into a single buffer
#ifndef TARGET_OPENGLES
	if( settings.useDepth && settings.useStencil )
	{
		stencilBuffer = depthBuffer = createAndAttachRenderbuffer(GL_DEPTH_STENCIL, GL_DEPTH_STENCIL_ATTACHMENT);
		retainRB(depthBuffer);
		retainRB(stencilBuffer);
	}else
#endif
	{
		// if we want a depth buffer, create it, and attach to our main fbo
		if(settings.useDepth){
			if(!settings.depthAsTexture){
				depthBuffer = createAndAttachRenderbuffer(GL_DEPTH_COMPONENT, GL_DEPTH_ATTACHMENT);
				retainRB(depthBuffer);
			}else{
				glGenTextures(1, &depthBuffer);
				//retainRB(depthBuffer);
				glBindTexture(GL_TEXTURE_2D, depthBuffer);

				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			#ifndef TARGET_OPENGLES
				glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
				glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
			#else
				glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
				glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
			#endif
				glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, settings.width, settings.height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0 );
				glBindTexture( GL_TEXTURE_2D, 0 );
				glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D, depthBuffer, 0);
			}
		}

		// if we want a stencil buffer, create it, and attach to our main fbo
		if(settings.useStencil){
			stencilBuffer = createAndAttachRenderbuffer(GL_STENCIL_INDEX, GL_STENCIL_ATTACHMENT);
			retainRB(stencilBuffer);
		}
	}
	// if we want MSAA, create a new fbo for textures
#ifndef TARGET_OPENGLES
	if(settings.numSamples){
		glGenFramebuffers(1, &fboTextures);
		retainFB(fboTextures);
	}else{
		fboTextures = fbo;
	}
#else
	fboTextures = fbo;
	if(settings.numSamples){
		ofLog(OF_LOG_WARNING,"ofFbo: multisampling not supported in opengles");
	}
#endif
	// now create all textures and color buffers
	for(int i=0; i<settings.numColorbuffers; i++) createAndAttachTexture(i);

	// if textures are attached to a different fbo (e.g. if using MSAA) check it's status
	if(fbo != fboTextures) {
		glBindFramebuffer(GL_FRAMEBUFFER, fboTextures);
	}

	// check everything is ok with this fbo
	checkStatus();
	
	// unbind it
	unbind();
}
    RenderData* MultiVolumeRaycaster::performRaycasting(DataContainer& dataContainer, const std::vector<const ImageRepresentationGL*>& images, const CameraData* camera, const RenderData* entrypoints, const RenderData* exitpoints, const LightSourceData* light) {
        cgtAssert(_rcShader != nullptr, "EEP Shader must not be 0.");

        _rcShader->activate();

        decorateRenderProlog(dataContainer, _rcShader);
        _rcShader->setUniform("_viewportSizeRCP", 1.f / cgt::vec2(getEffectiveViewportSize()));
        _rcShader->setUniform("_jitterStepSizeMultiplier", p_jitterStepSizeMultiplier.getValue());

        // compute sampling step size relative to volume size
        float samplingStepSize = .001f / p_samplingRate.getValue();
        if (p_lqMode.getValue())
            samplingStepSize *= 4.f;
        _rcShader->setUniform("_samplingStepSize", samplingStepSize);

        // compute and set camera parameters
        const cgt::Camera& cam = camera->getCamera();
        float n = cam.getNearDist();
        float f = cam.getFarDist();
        _rcShader->setUniform("_cameraPosition", cam.getPosition());
        _rcShader->setUniform("const_to_z_e_1", 0.5f + 0.5f*((f+n)/(f-n)));
        _rcShader->setUniform("const_to_z_e_2", ((f-n)/(f*n)));
        _rcShader->setUniform("const_to_z_w_1", ((f*n)/(f-n)));
        _rcShader->setUniform("const_to_z_w_2", 0.5f*((f+n)/(f-n))+0.5f);

        // bind input textures
        cgt::TextureUnit volumeUnit1, volumeUnit2, volumeUnit3, entryUnit, entryUnitDepth, exitUnit, exitUnitDepth, tf1Unit, tf2Unit, tf3Unit;
        images[0]->bind(_rcShader, volumeUnit1, "_volume1", "_volumeParams1");
        images[1]->bind(_rcShader, volumeUnit2, "_volume2", "_volumeParams2");
        images[2]->bind(_rcShader, volumeUnit3, "_volume3", "_volumeParams3");
        p_transferFunction1.getTF()->bind(_rcShader, tf1Unit, "_transferFunction1", "_transferFunctionParams1");
        p_transferFunction2.getTF()->bind(_rcShader, tf2Unit, "_transferFunction2", "_transferFunctionParams2");
        p_transferFunction3.getTF()->bind(_rcShader, tf3Unit, "_transferFunction3", "_transferFunctionParams3");
        entrypoints->bind(_rcShader, entryUnit, entryUnitDepth, "_entryPoints", "_entryPointsDepth", "_entryParams");
        exitpoints->bind(_rcShader, exitUnit, exitUnitDepth, "_exitPoints", "_exitPointsDepth", "_exitParams");
        light->bind(_rcShader, "_lightSource");

        // bind voxel hierarchies
        cgt::TextureUnit xorUnit, vhUnit1, vhUnit2, vhUnit3;
        xorUnit.activate();
        _vhm1->getXorBitmaskTexture()->bind();
        _rcShader->setUniform("_xorBitmask", xorUnit.getUnitNumber());

        vhUnit1.activate();
        _vhm1->getHierarchyTexture()->bind();
        _rcShader->setUniform("_voxelHierarchy1", vhUnit1.getUnitNumber());
        _rcShader->setUniform("_vhMaxMipMapLevel1", static_cast<int>(_vhm1->getMaxMipmapLevel()));

        if (_vhm2) {
            vhUnit2.activate();
            _vhm2->getHierarchyTexture()->bind();
            _rcShader->setUniform("_voxelHierarchy2", vhUnit2.getUnitNumber());
            _rcShader->setUniform("_vhMaxMipMapLevel2", static_cast<int>(_vhm2->getMaxMipmapLevel()));
        }

        if (_vhm3) {
            vhUnit3.activate();
            _vhm3->getHierarchyTexture()->bind();
            _rcShader->setUniform("_voxelHierarchy3", vhUnit3.getUnitNumber());
            _rcShader->setUniform("_vhMaxMipMapLevel3", static_cast<int>(_vhm3->getMaxMipmapLevel()));
        }

        FramebufferActivationGuard f*g(this);
        createAndAttachTexture(GL_RGBA8);
        createAndAttachTexture(GL_RGBA32F);
        createAndAttachTexture(GL_RGBA32F);
        createAndAttachDepthTexture();

        static const GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 , GL_COLOR_ATTACHMENT2 };
        glDrawBuffers(3, buffers);

        glEnable(GL_DEPTH_TEST);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        QuadRdr.renderQuad();

        // restore state
        glDrawBuffers(1, buffers);
        glDisable(GL_DEPTH_TEST);

        decorateRenderEpilog(_rcShader);
        _rcShader->deactivate();

        LGL_ERROR;
        return new RenderData(_fbo);
    }
    std::pair<RenderData*, RenderData*> MultiVolumeRaycaster::computeEntryExitPoints(const std::vector<const ImageRepresentationGL*>& images, const CameraData* camera, const RenderData* geometryImage) {
        cgtAssert(_eepShader != nullptr, "EEP Shader must not be 0.");
        const cgt::Camera& cam = camera->getCamera();

        // construct bounding box for all images
        cgt::Bounds b;
        for (size_t i = 0; i < images.size(); ++i) {
            b.addVolume(images[i]->getParent()->getWorldBounds());
        }

        // create proxy geometry
        std::unique_ptr<MeshGeometry> cube(GeometryDataFactory::createCube(b, b));
        // clip proxy geometry against near-plane to support camera in volume
        float nearPlaneDistToOrigin = cgt::dot(cam.getPosition(), -cam.getLook()) - cam.getNearDist() - .002f;
        MeshGeometry clipped = cube->clipAgainstPlane(nearPlaneDistToOrigin, -cam.getLook(), true, 0.02f);

        _eepShader->activate();

        cgt::TextureUnit geometryDepthUnit, entryDepthUnit;
        _eepShader->setUniform("_viewportSizeRCP", 1.f / cgt::vec2(getEffectiveViewportSize()));
        _eepShader->setUniform("_projectionMatrix", cam.getProjectionMatrix());
        _eepShader->setUniform("_viewMatrix", cam.getViewMatrix());

        if (geometryImage != nullptr) {
            geometryImage->bindDepthTexture(_eepShader, geometryDepthUnit, "_geometryDepthTexture", "_geometryDepthTexParams");

            _eepShader->setUniform("_integrateGeometry", true);
            _eepShader->setUniform("_near", cam.getNearDist());
            _eepShader->setUniform("_far", cam.getFarDist());

            cgt::mat4 inverseView = cgt::mat4::identity;
            if (cam.getViewMatrix().invert(inverseView))
                _eepShader->setUniform("_inverseViewMatrix", inverseView);

            cgt::mat4 inverseProjection = cgt::mat4::identity;
            if (cam.getProjectionMatrix().invert(inverseProjection))
                _eepShader->setUniform("_inverseProjectionMatrix", inverseProjection);
        }
        else {
            _eepShader->setUniform("_integrateGeometry", false);
        }

        glEnable(GL_CULL_FACE);
        glEnable(GL_DEPTH_TEST);

        // create entry points texture
        FramebufferActivationGuard f*g(this);
        createAndAttachTexture(GL_RGBA32F);
        createAndAttachDepthTexture();
        _eepShader->setUniform("_isEntrypoint", true);

        glDepthFunc(GL_LESS);
        glClearDepth(1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glCullFace(GL_BACK);
        clipped.render(GL_TRIANGLE_FAN);

        RenderData* entrypoints = new RenderData(_fbo);
        _fbo->detachAll();

        // create exit points texture
        createAndAttachTexture(GL_RGBA32F);
        createAndAttachDepthTexture();
        _eepShader->setUniform("_isEntrypoint", false);

        if (geometryImage != nullptr) {
            entrypoints->bindDepthTexture(_eepShader, entryDepthUnit, "_entryDepthTexture", "_entryDepthTexParams");
        }

        glDepthFunc(GL_GREATER);
        glClearDepth(0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glCullFace(GL_FRONT);
        clipped.render(GL_TRIANGLE_FAN);

        RenderData* exitpoints = new RenderData(_fbo);
        decorateRenderEpilog(_eepShader);
        _eepShader->deactivate();

        glDepthFunc(GL_LESS);
        glClearDepth(1.0f);
        glCullFace(GL_BACK);
        glDisable(GL_CULL_FACE);
        glDisable(GL_DEPTH_TEST);

        _eepShader->deactivate();
        LGL_ERROR;

        return std::make_pair(entrypoints, exitpoints);
    }
 void VisualizationProcessor::createAndAttachDepthTexture() {
     createAndAttachTexture(GL_DEPTH_COMPONENT24);
 }
 void VisualizationProcessor::createAndAttachColorTexture() {
     createAndAttachTexture(GL_RGBA8);
 }