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

转载请注明出处: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;
	<
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Unity中使用OpenGLGLFW,您需要执行以下步骤: 1. 下载和安装GLFW: 在您的计算机上安装GLFW。您可以从GLFW官方网站上下载适用于您操作系统的文件。 2. 创建一个自定义插件: 在Unity中创建一个自定义插件来使用GLFW。这将确保Unity知道在使用OpenGL时要使用GLFW。创建自定义插件的步骤如下: - 在项目面板中单击“Create”按钮,然后选择“Folder”。 - 将文件夹重命名为“Plugins”。 - 在“Plugins”文件夹中单击“Create”按钮,然后选择“C# Script”。 - 将脚本重命名为“GLFWPlugin.cs”。 3. 编写GLFWPlugin.cs脚本: 打开GLFWPlugin.cs脚本并编写以下代码: ``` using UnityEngine; using System.Runtime.InteropServices; public class GLFWPlugin : MonoBehaviour { const string LIB_NAME = "glfw3"; [DllImport(LIB_NAME)] public static extern int glfwInit(); [DllImport(LIB_NAME)] public static extern void glfwTerminate(); [DllImport(LIB_NAME)] public static extern void glfwPollEvents(); } ``` 此脚本包括三个函数:glfwInit,glfwTerminate和glfwPollEvents。这些函数将允许您使用GLFW进行窗口创建和事件处理等操作。 4. 在Unity中使用GLFW: 要在Unity中使用GLFW,您可以使用以下代码: ``` void Start() { GLFWPlugin.glfwInit(); } void Update() { GLFWPlugin.glfwPollEvents(); } void OnDestroy() { GLFWPlugin.glfwTerminate(); } ``` 这些函数将在Unity应用程序启动时调用glfwInit函数,然后在Update函数中调用glfwPollEvents函数以处理事件。最后,在关闭应用程序时调用glfwTerminate函数。 这些步骤应该足够让您在Unity中使用OpenGLGLFW

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值