glsCV/glsMat.cpp
//-----------------------------------------------------------------------------
//Upload texture from cv::Mat to GL texture
Upload texture from cv::Mat to GL texture
Upload texture from cv :: Mat to GL texture
void GlsMat::upload(const Mat&src){
	_TMR_("-upload :\t");
	CV_Assert(src.type() == type());
	CV_Assert(src.dims == dims);
	if (src.dims == 2){
		CV_Assert(src.size() == size());
	}
	else{
		CV_Assert(src.size[0] == size[0]);
		CV_Assert(src.size[1] == size[1]);
		CV_Assert(src.size[2] == size[2]);
	}
	Mat _src;
	if (src.isContinuous()){
		_src = src;
	}
	else{
		_src = src.clone();
	}
	CV_Assert(_src.isContinuous());
	{
		int _size = (int)total() * (int)_src.elemSize();
		GLuint pbo;
		glGenBuffers(1, &pbo);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
		glBufferData(GL_PIXEL_UNPACK_BUFFER, _size, 0, GL_DYNAMIC_READ);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
		mat2pbo(_src, pbo);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); 
		if (src.dims == 2){
			glBindTexture(GL_TEXTURE_2D, texid());
			glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); 
			glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, cols, rows, glFormat(), glType(), 0);
			GL_CHECK_ERROR();
		}
		else if (src.dims == 3){
			glBindTexture(GL_TEXTURE_2D_ARRAY, texid());
			glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, size[2], size[1], size[0], glFormat(), glType(), 0);
			GL_CHECK_ERROR();
			glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
		}
		else {
			GLS_Assert(0 && "dims must be 2 or 3.");
		}
		glDeleteBuffers(1, &pbo);
	}
}
void GlsMat :: upload ( const Mat & src ) { _TMR_ ( " \t " ) ; CV_Assert ( src . type ( ) == type ( ) ) ; CV_Assert ( src . dims == dims ) ; if ( src . dims == 2 ) { CV_Assert ( src . size ( ) == size ( ) ) ; } else { CV_Assert ( src . size [ 0 ] == size [ 0 ] ) ; CV_Assert ( src . size [ 1 ] == size [ 1 ] ) ; CV_Assert ( src . size [ 2 ] == size [ 2 ] ) ; } Mat _src ; if ( src . isContinuous ( ) ) { _src = src ; } else { _src = src . clone ( ) ; } CV_Assert ( _src . isContinuous ( ) ) ; { int _size = ( int ) total ( ) * ( int ) _src . elemSize ( ) ; GLuint pbo ; glGenBuffers ( 1 , & pbo ) ; glBindBuffer ( GL_PIXEL_UNPACK_BUFFER , pbo ) ; glBufferData ( GL_PIXEL_UNPACK_BUFFER , _size , 0 , GL_DYNAMIC_READ ) ; glBindBuffer ( GL_PIXEL_UNPACK_BUFFER , 0 ) ; mat2pbo ( _src , pbo ) ; glBindBuffer ( GL_PIXEL_UNPACK_BUFFER , pbo ) ; if ( src . dims == 2 ) { glBindTexture ( GL_TEXTURE_2D , texid ( ) ) ; glBindBuffer ( GL_PIXEL_UNPACK_BUFFER , pbo ) ; glTexSubImage2D ( GL_TEXTURE_2D , 0 , 0 , 0 , cols , rows , glFormat ( ) , glType ( ) , 0 ) ; GL_CHECK_ERROR ( ) ; } else if ( src . dims == 3 ) { glBindTexture ( GL_TEXTURE_2D_ARRAY , texid ( ) ) ; glTexSubImage3D ( GL_TEXTURE_2D_ARRAY , 0 , 0 , 0 , 0 , size [ 2 ] , size [ 1 ] , size [ 0 ] , glFormat ( ) , glType ( ) , 0 ) ; GL_CHECK_ERROR ( ) ; glBindTexture ( GL_TEXTURE_2D_ARRAY , 0 ) ; } else { GLS_Assert ( 0 && " " ) ; } glDeleteBuffers ( 1 , & pbo ) ; } }
Upload texture from cv::Mat to GL texture
Upload texture from cv :: Mat to GL texture
//bind current pbo for app->pbo transfer //bind pbo //Copy pixels from pbo to texture object //bind pbo