使用glfw库将OpenCV读取到的图片作为OpenGL的背景纹理贴图

本文介绍了如何利用glfw库在多线程环境中与OpenCV配合,将通过OpenCV读取的图片作为OpenGL窗口的背景纹理进行显示。重点强调了glfw的初始化和键盘响应函数必须在主线程中调用,以及代码实现的基本流程。
摘要由CSDN通过智能技术生成

转载请注明出处:http://my.csdn.NET/ye_shen_wei_mian

前段时间接触过一点glfw,个人而言不太喜欢freeglut的回调机制,glfw不失为一个可以替代的选择。

使用glfw应当注意以下几点:

1;glfw是可以使用在多线程当中使用的。

2.如果使用多线程,记住glfw的初始化函数glfwInit()只能调用一次,并且在主线程中调用,切勿调用多次,否则会报错。

3、glfw的键盘响应函数貌似也是只能在主线程中调用的。

从网上得到的代码修改了一下,实现的功能是将OpenCV读进来的图片贴到OpenGL窗口中作为背景纹理,可以运行:

#include <iostream>
#include <thread>
#include<chrono>


using std::cout;
using std::endl;


#include "opencv.hpp"
//#include "GL/glew.h"
#include "GL/freeglut.h"
#include "GLFW/glfw3.h"


GLint   windowWidth = 640;     // Define our window width
GLint   windowHeight = 480;     // Define our window height
GLfloat fieldOfView = 45.0f;   // FoV
GLfloat zNear = 0.1f;    // Near clip plane
GLfloat zFar = 200.0f;  // Far clip plane


// Frame counting and limiting
int    frameCount = 0;
double frameStartTime, frameEndTime, frameDrawTime;


bool quit = false;


GLFWwindow* window;


void handleKeypress(int theKey, int theAction);


// Function turn a cv::Mat into a texture, and return the texture ID as a GLuint for use
GLuint matToTexture(cv::Mat &mat, GLenum minFilter, GLenum magFilter, GLenum wrapFilter)
{
	// Generate a number for our textureID's unique handle
	GLuint textureID;
	glGenTextures(1, &textureID);


	// Bind to our texture handle
	glBindTexture(GL_TEXTURE_2D, textureID);


	 Catch silly-mistake texture interpolation method for magnification
	//if (magFilter == GL_LINEAR_MIPMAP_LINEAR ||
	//	magFilter == GL_LINEAR_MIPMAP_NEAREST ||
	//	magFilter == GL_NEAREST_MIPMAP_LINEAR ||
	//	magFilter == GL_NEAREST_MIPMAP_NEAREST)
	//{
	//	cout << "You can't use MIPMAPs for magnification - setting filter to GL_LINEAR" << endl;
	//	magFilter = GL_LINEAR;
	//}
	glPixelStoref(GL_UNPACK_ALIGNMENT, 1);
	// Set texture interpolation methods for minification and magnification
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);


	// Set texture clamping method
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapFilter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapFilter);


	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);


	// Set incoming texture format to:
	// GL_BGR       for CV_CAP_OPENNI_BGR_IMAGE,
	// GL_LUMINANCE for CV_CAP_OPENNI_DISPARITY_MAP,
	// Work out other mappings as required ( there's a list in comments in main() )
	GLenum inputColourFormat = GL_RGB;
	if (mat.channels() == 1)
	{
		inputColourFormat = GL_LUMINANCE;
	<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值