将 Qt QImage 传递给 glsl 纹理采样器

发布于 2024-11-29 03:39:48 字数 1613 浏览 2 评论 0原文

我正在使用 Qt 编写渲染引擎,并且在对模型进行纹理处理时遇到问题

我有一个非常简单的着色器来测试纹理: 顶点着色器:

Attribute vec4 Vertex; 
Attribute vec2 texcoords;   
uniform mat4 mvp;   
varying vec2 outTexture;   

void main() {     
  gl_Position = mvp * Vertex;     
  outTexture = texcoords; 
} 

和片段着色器:

uniform sampler2D tex;   
varying vec2 outTexture;
void main() {
  vec4 color = texture2D(tex, outTexture);
  gl_FragColor = color; 
} 

我将纹理坐标正确传递给着色器 我的问题是绑定 QImage 并将其发送到其纹理统一。

我使用以下代码来绑定纹理:

const QString& filename; 
GLuint m_texture;
QImage image(filename);
image = image.convertToFormat(QImage::Format_ARGB32);
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexParameteri(GL_TEXTURE2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.width(), image.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, image.bits());
glGenerateMipmap(GL_TEXTURE2D);
glEnable(GL_TEXTURE_2D); 

着色器工作,我可以将统一传递给矩阵并将属性传递给顶点和纹理坐标,但是当我尝试以相同的方式将统一发送到纹理时

effect->setUniformValue(effect->uniformLocation("tex", texture->m_texture)); 

:程序因“访问冲突读取位置”错误而崩溃,glGetError() 返回“无效枚举”

有趣的是,当我尝试运行程序而不尝试将纹理发送到采样器时,纹理实际上出现在模型上。这让我认为我绑定它的方式与遗留纹理处理有关,并且纹理被绑定到着色器拾取的特定纹理地址。这不是我想要的效果,因为我希望程序员能够在绘制时明确说明应将哪些纹理传递给制服(就像设置任何其他制服一样)

如何将纹理传递给它的采样器,做什么绑定纹理时需要更改吗?

I am writing a rendering engine using Qt and am running into problems with texturing my models

I have a very simple shader to test texturing:
vertex shader:

Attribute vec4 Vertex; 
Attribute vec2 texcoords;   
uniform mat4 mvp;   
varying vec2 outTexture;   

void main() {     
  gl_Position = mvp * Vertex;     
  outTexture = texcoords; 
} 

and fragment shader:

uniform sampler2D tex;   
varying vec2 outTexture;
void main() {
  vec4 color = texture2D(tex, outTexture);
  gl_FragColor = color; 
} 

I am passing my texture coordinates to the shaders correctly
My problem is with binding a QImage and sending it to its texture uniform.

I am using the following code to bind the texture:

const QString& filename; 
GLuint m_texture;
QImage image(filename);
image = image.convertToFormat(QImage::Format_ARGB32);
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexParameteri(GL_TEXTURE2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.width(), image.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, image.bits());
glGenerateMipmap(GL_TEXTURE2D);
glEnable(GL_TEXTURE_2D); 

The shader works and I can pass a uniform to the matrix and attributes to the vertex and texture coordinates, but when I try to send a uniform to the texture the same way as such:

effect->setUniformValue(effect->uniformLocation("tex", texture->m_texture)); 

the program crashes with an “access violation reading location” error with glGetError() returning “invalid enumerant”

Interestingly, when I try running the program without attempting to send the texture to the sampler, the texture is actually appearing on the model. Which makes me think the way I’m binding it has something to do with the legacy texture handling and the texture is being bound to a particular texture address which is being picked up by the shader. This is not the effect I want because I want the programmer to be able to explicitly state at draw time what texture should be passed to the uniform (just as any other uniform is set)

How can I pass the texture to it’s sampler, what do I need to change when binding a texture?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金橙橙 2024-12-06 03:39:48

将其更改为

effect->setUniformValue(effect->uniformLocation("tex"), texture->m_texture);

effect->setUniformValue("tex", texture->m_texture); 

Change it to

effect->setUniformValue(effect->uniformLocation("tex"), texture->m_texture);

or

effect->setUniformValue("tex", texture->m_texture); 
指尖上的星空 2024-12-06 03:39:48

尝试使用以下方法转换 QImage:

image = QGLWidget::convertToGLFormat(image);

另一个想法,如果您使用 ES2,则 GL_RGBA8 无效。我认为 GL_BGRA 可能是一个可选扩展,或者不是 ES 2。希望这会有所帮助。

Try converting the QImage using:

image = QGLWidget::convertToGLFormat(image);

Another thought, if you are using ES2, then GL_RGBA8 is not valid. I think GL_BGRA may be an optional extension, or not ES 2. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文