Skip to content

Instantly share code, notes, and snippets.

@zhangzhensong
Last active October 11, 2022 20:52
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zhangzhensong/03f67947c22acb5ee922 to your computer and use it in GitHub Desktop.
Save zhangzhensong/03f67947c22acb5ee922 to your computer and use it in GitHub Desktop.
Converting OpenCV Mat to OpenGL texture
// don't forget to include related head files
void BindCVMat2GLTexture(cv::Mat& image, GLuint& imageTexture)
{
if(image.empty()){
std::cout << "image empty" << std::endl;
}else{
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glGenTextures(1, &imageTexture1);
glBindTexture(GL_TEXTURE_2D, imageTexture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
cv::cvtColor(image, image, CV_RGB2BGR);
glTexImage2D(GL_TEXTURE_2D, // Type of texture
0, // Pyramid level (for mip-mapping) - 0 is the top level
GL_RGB, // Internal colour format to convert to
image.cols, // Image width i.e. 640 for Kinect in standard mode
image.rows, // Image height i.e. 480 for Kinect in standard mode
0, // Border width in pixels (can either be 1 or 0)
GL_RGB, // Input image format (i.e. GL_RGB, GL_RGBA, GL_BGR etc.)
GL_UNSIGNED_BYTE, // Image data type
image.ptr()); // The actual image data itself
}
}
@LiamMaclean216
Copy link

does anyone know the java equivalent of image.ptr() ?

@icarolelis1
Copy link

very good thank you

@DrBAXA
Copy link

DrBAXA commented Dec 17, 2019

does anyone know the java equivalent of image.ptr() ?

Most probably there will be ByteBuffer.wrap(image.data) for java

@willbetheone
Copy link

Thank you for your code.

@mohsenkondori
Copy link

HI,
how to use this function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment