openGL之API学习(三十五)texture和texture2D

在OpenGL 3.3及以上版本,texture2D()函数被弃用,建议使用新的texture()函数替代。texture()函数接受各种类型的sampler,支持1D、2D、3D及Cube等纹理类型,并且可以设置bias参数进行LOD计算。在OpenGL ES 3.0中,类似的改动也发生了,texture2D()被替换为texture()。
摘要由CSDN通过智能技术生成

纹理采样函数

texture2D() is deprecated as of (at least) OpenGL 3.3; see page 99 of the 3.30 GLSL specification. It will continue to be supported in OpenGL compatibility profiles to avoid breaking existing code, but its usage in new code is strongly discouraged.

Between glsl 120 and 130 they changed the function to texture and made it accept every kind of samplers, not just a sampler2D.

EDIT: The details are slightly different for OpenGL ES, but the end result is the same: texture2D() was deprecated and replaced by texture() in OpenGL ES 3.0; see section 8.8 of the 3.0 GLSL ES specification.

 

gvec4 texture(     gsampler1D sampler,
      float P,
      [float bias]);
 
gvec4 texture(  

OpenGL是一个用于渲染3D图形的跨平台API,而`cv::Mat`是OpenCV库中的核心数据结构,通常用于处理图像。如果你想要将`cv::Mat`的数据应用到OpenGL的纹理(`texture2D`)上,你需要做以下几个步骤: 1. **创建GL texture**: - 首先,你需要在OpenGL上下文中创建一个纹理ID (`GLuint texID`),这将代表你的纹理资源。 ```cpp glGenTextures(1, &texID); ``` 2. **加载并转换图像到GL internal format**: - 转换`cv::Mat`到GL支持的格式(如`GL_RGB`、`GL_RGBA`),并将像素数据复制到一个合适的数据结构(例如,`GLubyte*`数组)。 ```cpp cv::Mat image; // ...从文件或内存中读取image int width = image.cols, height = image.rows; GLubyte *data = new GLubyte[width * height * 4]; // Assuming GL_RGBA and byte order matches OpenCV's image.convertTo(data, CV_8UC4); // Convert to 32-bit unsigned bytes (RGBA) ``` 3. **绑定纹理并上传数据**: - 将纹理ID绑定,并将数据上传到GPU。 ```cpp glBindTexture(GL_TEXTURE_2D, texID); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); ``` 4. **设置纹理参数**: - 可能需要设置纹理过滤器和重复模式等属性。 ```cpp glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); ``` 5. **在OpenGL场景中使用纹理**: - 当你在渲染模型时,通过设置材质的纹理单位,指定刚创建的纹理作为贴图。 ```cpp glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texID); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值