转载请注明出处: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;
<