Opencv 片段着色器:texture2D()和texelFetch()

Opencv 片段着色器:texture2D()和texelFetch(),opencv,opengl,Opencv,Opengl,我的程序显示了一个从openGL网络摄像头加载的openCV图像 下面的程序可以正常工作,但代码后面列出了一些问题 主要内容: 片段着色器: #version 330 core in vec2 UV; out vec4 color; // Values that stay constant for the whole mesh. uniform sampler2D myTextureSampler; void main(void) { //color = texture2D(myT

我的程序显示了一个从openGL网络摄像头加载的openCV图像

下面的程序可以正常工作,但代码后面列出了一些问题

主要内容:

片段着色器:

#version 330 core

in vec2 UV;
out vec4 color;

// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;

void main(void)
{
    //color = texture2D(myTextureSampler,UV);
    color = texelFetch(myTextureSampler,ivec2(gl_FragCoord.xy),0);
}
  • 带有texture2D()的片段着色器中的注释行无效!它看起来像这个图像。怎么了
  • texture2D()和texelFetch()之间的区别在哪里?最佳实践是什么
  • 使用texelFetch显示的图像为蓝色。知道为什么会这样吗?(加载的cv::Mat没有着色)

GLSL
纹理
使用标准化坐标(即范围[0,1]内的值)寻址,并执行过滤
texelFetch
按特定mipmap级别的绝对像素索引寻址,不过滤

根据您的屏幕截图判断,您传递给
texture
的纹理坐标错误或处理错误;
texelFetch
代码不使用明确指定的纹理坐标,而是使用视口像素坐标


查看用于纹理坐标调用的glVertexAttribute指针,告诉OpenGL每个纹理坐标有3个元素,而数组只有2个元素。这可能是您的问题。

网络摄像头中的图像是bgr,而不是rgb,请更改glTexImage2D()中的标志更改标志解决了着色问题@BenediktBock:您的GLSL代码也是错误的,当您取消注释该行时,不应编译:
texture2D()
在GLSL 3.30 core中不再有效。该函数现在只被称为
texture()
,正确的变量将从您使用它的采样器类型派生出来。@derhass谢谢您。你说得对(第99页)。将其更改为texture(),但texture2D()仍能成功编译。@BenediktBock:hmm。这是什么GPU/驱动程序?当您明确请求“#version 330 core”时,它实际上不应该编译。但是每一个实现对这类内容的处理都是不同的。。。
#version 330 core

layout (location = 0) in vec3 vertexPosition_modelspace; //input vom vertexbuffer
layout (location = 1) in vec2 UVcoord;

out vec2 UV;

void main(void)
{
    gl_Position.xyz = vertexPosition_modelspace;
    gl_Position.w = 1.0; //Zoomfaktor

    UV = UVcoord;
}
#version 330 core

in vec2 UV;
out vec4 color;

// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;

void main(void)
{
    //color = texture2D(myTextureSampler,UV);
    color = texelFetch(myTextureSampler,ivec2(gl_FragCoord.xy),0);
}