0

I'm trying to write a simple shader which would add textures attached to FBOs. There is no problem with FBO initialization and such (I've tested it). The problem is I believe with 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);

 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:

 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(); 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)?

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 )

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..

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)?

genpfault
  • 51,148
  • 11
  • 85
  • 139
mathew
  • 9
  • 1
  • 4
  • 1
    why do you use texture rectangles? it could work if you use "normal" textures. In OpenGL 2.0 and above npot textures are fully supported. – fen May 10 '12 at 09:15
  • I dont understund what you mean . I use GL_TEXTURE_RECTANGLE_NV with GL_RGB16F its a NPOT texture i believe. Btw ive tried to use GL_FLOAT_R32_NV but it didnt work ( do i need to shader for that if so which exactly?) – mathew May 10 '12 at 18:08
  • "Also another stupid problem:" You don't ask two questions. You ask one question at a time. If you have "another stupid problem" then you ask another question with the `Ask Question` button. – Nicol Bolas May 10 '12 at 18:34
  • sorry for being not so precise... "normal" textures for me mean "GL_TEXTURE_2D" not "GL_TEXTURE_RECTANGLE_NV" – fen May 10 '12 at 21:35

2 Answers2

2

It's not clear what does your xShader.bind(), I can gues you do glUseProgram(...) there. But uniform variables (sampler index in your case) should be set up after the glUseProgram(...) is called. 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

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.

ChatCloud
  • 1,152
  • 2
  • 8
  • 22
2
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. 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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Thanks a lot. I believe that im on the right track now :)) It remains only to upgrade my shaders to version 140.. Can you recommend some good tutorial? – mathew May 11 '12 at 13:23