简体   繁体   English

GLSL和FBO-glActiveTexture不起作用?

[英]GLSL and FBOs - glActiveTexture doesn't work?

I'm trying to write a simple shader which would add textures attached to FBOs. 我正在尝试编写一个简单的着色器,它将添加附加到FBO的纹理。 There is no problem with FBO initialization and such (I've tested it). FBO初始化并没有问题(我已经对其进行了测试)。 The problem is I believe with glActiveTexture(GL_TEXTURE0). 问题是我相信使用glActiveTexture(GL_TEXTURE0)。 It doesn't seem to be doing anything- here is my frag shader: (but generally shader is called - I've tested that by putting gl_FragColor = vec4(0,1,0,1); 它似乎没有做任何事情-这是我的片段着色器:(但通常称为着色器-我已经通过放置gl_FragColor = vec4(0,1,0,1)进行了测试;

 uniform sampler2D Texture0;
 uniform sampler2D Texture1;
 varying vec2 vTexCoord;
 void main()
 {
     vec4 texel0 = texture2D(Texture0, gl_TexCoord[0].st);
     vec4 vec = texel0; 
     gl_FragColor = texel0;     
 }

And in C++ code i have: 在C ++代码中,我有:

 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, iFrameBufferAccumulation); 

 glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
 ( Render something - it works fine to iTextureImgAccumulation texture attached to     GL_COLOR_ATTACHMENT0_EXT )
 glClear (GL_COLOR_BUFFER_BIT );
 glEnable(GL_TEXTURE_RECTANGLE_NV);


 glActiveTexture(GL_TEXTURE0); 

 glBindTexture( GL_TEXTURE_RECTANGLE_NV, iTextureImgAccumulation ); // Bind our frame buffer texture 
 xShader.setUniform1i("Texture0", 0);

 glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations 
 glTranslatef(0.0f, 0.0f, -2.0f); 
 xShader.bind();        
 glBegin(GL_QUADS);

     glTexCoord2f(0,OPT.m_nHeight);
     glVertex3f(-1,-1,0);

 glTexCoord2f(OPT.m_nWidth,OPT.m_nHeight);
 glVertex3f(1,-1,0);

 glTexCoord2f(OPT.m_nWidth,0);
 glVertex3f(1,1,0);

 glTexCoord2f(0,0);
 glVertex3f(-1,1,0);

 glEnd();

 glBindTexture( GL_TEXTURE_RECTANGLE_NV, NULL ); 

 xShader.unbind();

Result: black screen (when displaying second texture and using shader (without using shader its fine). I'm aware that this shader shouldn't do much but he doesn't even display the first texture. I'm in the middle of testing things but idea is that after rendering to first texture I would add first texture to the second one. To do this I imagine that this fragment shader would work : 结果:黑屏(当显示第二个纹理并使用着色器时(不使用着色器就可以了)。我知道该着色器不应该做太多,但他甚至不显示第一个纹理。测试的东西,但想法是渲染到第一个纹理后,我会在第二个纹理中添加第一个纹理,为此我想这个片段着色器会起作用:

 uniform sampler2D Texture0;
 uniform sampler2D Texture1;
 varying vec2 vTexCoord;
 void main()
 {
     vec4 texel0 = texture2D(Texture0, gl_TexCoord[0].st);
     vec4 texel1 = texture2D(Texture1, gl_TexCoord[0].st);
     vec4 vec = texel0 + texel1;
     vec.w = 1.0;
     gl_FragColor = vec;    
 }

And whole idea is that in a loop tex2 = tex2 + tex1 ( would it be possible that i use tex2 in this shader to render to GL_COLOR_ATTACHMENT1_EXT which is attached to tex2 ?) I've tested both xShader.bind(); 整个想法是,在一个循环中tex2 = tex2 + tex1(我是否有可能在此着色器中使用tex2渲染到与tex2相连的GL_COLOR_ATTACHMENT1_EXT?)我已经测试了xShader.bind(); before initializing uniform variables and after. 在初始化统一变量之前和之后。 Both cases - black screen. 两种情况-黑屏。

Anyway for a moment, I'm pretty sure that there is some problem with initialization of sampler for textures (maybe cos they are attached to FBO)? 无论如何,我可以肯定的是,纹理采样器的初始化存在一些问题(可能是因为它们被附加到了FBO上)?

I've checked the rest and it works fine. 我检查了其余的,它工作正常。

Also another stupid problem: 还有另一个愚蠢的问题:

How can i render texture on whole screen ? 如何在整个屏幕上渲染纹理?

I've tried something like that but it doesn't work ( i have to translate a bit this quad ) 我已经尝试过类似的方法,但是它不起作用(我必须对此quad进行一点翻译)

glViewport(0,0 , OPT.m_nWidth, OPT.m_nHeight);

glBindTexture( GL_TEXTURE_RECTANGLE_NV, iTextureImg/*iTextureImgAccumulation*/ ); // Bind our frame buffer texture 

glBegin(GL_QUADS);
glTexCoord2f(0,OPT.m_nHeight);
glVertex3f(-1,-1,0);

glTexCoord2f(OPT.m_nWidth,OPT.m_nHeight);
glVertex3f(1,-1,0);

glTexCoord2f(OPT.m_nWidth,0);
glVertex3f(1,1,0);

glTexCoord2f(0,0);
glVertex3f(-1,1,0);

glEnd();

Doesnt work with glVertex2f also.. 也不能与glVertex2f一起使用。

Edit: I've checked out and I can initialise some uniform variables only textures are problematic. 编辑:我已经签出,我可以初始化一些统一变量,只有纹理有问题。

I've changed order but it still doesn't work.:( Btw other uniforms values are working well. I've displayed texture I want to pass to shader too. It works fine. But for unknown reason texture sampler isn't initialized in fragment shader. Maybe it has something to do that this texture is glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB16F / GL_FLOAT_R32_NV /, OPT.m_nWidth, OPT.m_nHeight, 0, GL_RED, GL_FLOAT, NULL); (its not GL_TEXTURE_2D)? 我更改了顺序,但仍然无法使用。:(顺便说一下,其他制服值也正常。我已经显示了纹理,我也想传递给着色器。它工作正常。但是由于未知原因,纹理采样器未初始化在片段着色器中。也许要做的事情就是这个纹理是glTexImage2D(GL_TEXTURE_RECTANGLE_NV,0,GL_RGB16F / GL_FLOAT_R32_NV /,OPT.m_nWidth,OPT.m_nHeight,0,GL_RED,GL_FLOAT,NULL)?

It's not clear what does your xShader.bind() , I can gues you do glUseProgram(...) there. 不清楚您的xShader.bind()是什么,我可以glUseProgram(...)您在那里执行glUseProgram(...) But uniform variables (sampler index in your case) should be set up after the glUseProgram(...) is called. 但是应在glUseProgram(...)之后设置统一变量(在您的情况下为采样器索引glUseProgram(...) In this order: 按此顺序:

glUseProgram(your_shaders); //probably your xShader.bind() does it.

GLuint sampler_idx = 0; 
GLint location = glGetUniformLocation(your_shaders, "Texture0");
if(location != -1) glUniform1i(location, sampler_idx);
else error("cant, get uniform location");

glActiveTexture(GL_TEXTURE0 + sampler_idx);
glBindTexture(GL_TEXTURE_2D, iTextureImg);

and 'yes' you can render FBO texture and use it in shader in another context 和“是”,您可以渲染FBO纹理并在其他上下文中的着色器中使用它

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, your_fbo_id);
// render to FBO there
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

then use your FBO texture the same way as you use regular textures. 然后以与使用常规纹理相同的方式使用FBO纹理。

glActiveTexture(GL_TEXTURE0); 

glBindTexture( GL_TEXTURE_RECTANGLE_NV, iTextureImgAccumulation ); // Bind our frame buffer texture 
xShader.setUniform1i("Texture0", 0);

This is a rectangle texture. 这是一个矩形纹理。

uniform sampler2D Texture0;

This is a 2D texture. 这是2D纹理。 They are not the same thing . 他们不是一回事 The sampler type must match the texture type . 采样器类型 必须纹理类型匹配。 You need to use a samplerRect , assuming your version of GLSL supports that. 您需要使用samplerRectsamplerRect您的GLSL版本支持该功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM