与mrt的opengl着色器问题

时间:2013-12-30 15:00:58

标签: c++ opengl shader fbo

我有两个相同尺寸的纹理附加到一个fbo,我想将另一个纹理渲染到其中一个附加纹理中,我想将输入纹理图形的蓝色形状渲染到另一个fbo附加纹理

(更新的)顶点着色器:

#version 330
in vec2 coord;
out vec2 f_coord;
void main() {
   gl_Position = vec4(coord, 0.0, 1.0);
   f_coord = (1.0+coord)/2.0;
}

(更新的)片段着色器:

#version 330
uniform sampler2D my_texture;
in vec2 f_coord;
out vec4 FragData[2];
void main() {
   if(texture(my_texture,f_coord).a == 0.0)
      discard;
   FragData[0]=texture(my_texture,f_coord);
   FragData[1]=vec4(0.5, 0.5, 1.0, 1.0);
}

使用此片段着色器一切正常。纹理和纹理的蓝色印章正确呈现。

以下是截图: http://s14.directupload.net/file/d/3487/kvkqdbl7_png.htm

但是如果我想将纹理渲染到gl_FragData [1]并将蓝色conture标记渲染到gl_FragData [0]

#version 330
uniform sampler2D my_texture;
in vec2 f_coord;
out vec4 FragData[2];
void main() {
   if(texture(my_texture,f_coord).a == 0.0)
      discard;
   FragData[0]=vec4(0.5, 0.5, 1.0, 1.0);
   FragData[1]=texture(my_texture,f_coord);
}

比我得到两个完全蓝色的纹理。奇怪的是:两个目标纹理都是蓝色的,但我已经将glViewport设置为纹理的一部分。

这是一个截图: http://s1.directupload.net/file/d/3487/9loqibdc_png.htm

fbo附件具有半屏尺寸

 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT), 0, GL_RGBA, GL_BYTE, 0);

所以我可以将两个纹理渲染到屏幕上进行检查。

这就是我创建framebufferobject

的方法
 void init_fbo()
 {

     glGenTextures(1, &fbo.texture0);
     glBindTexture(GL_TEXTURE_2D, fbo.texture0);
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT), 0, GL_RGBA, GL_BYTE, 0);

     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);


     glGenTextures(1, &fbo.texture1);
     glBindTexture(GL_TEXTURE_2D, fbo.texture1);
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT), 0, GL_RGBA, GL_BYTE, 0);

     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

     glBindTexture(GL_TEXTURE_2D, 0);  

     glGenFramebuffers(1, &fbo.fbo);

     glBindFramebuffer(GL_FRAMEBUFFER, fbo.fbo);

     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
                            fbo.texture0, 0);
     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D,
                            fbo.texture1, 0);

     glBindFramebuffer(GL_FRAMEBUFFER, 0);
 }

我使用那些全局结构变量

 struct
 {
     GLuint fbo;
     GLuint texture0;
     GLuint texture1;
 } fbo;

 struct
 {
     GLuint program;

     GLint uni_texture;
     GLint att_coord;
 } shader_fbo;

 struct
 {
     GLuint program;

     GLint uni_texture;
     GLint att_coord;
 } shader_screen;

这是我的渲染功能

 void render()
 {   
     GLenum buffers[2];
     GLfloat vertices[] = { -1.0, -1.0,
                                -1.0,  1.0,
                                 1.0, -1.0,
                                 1.0,  1.0  };




     GLubyte indices[] = { 0, 1, 2,
                           1, 2, 3  };


     /*render to mrt fbo*/
     glUseProgram(shader_fbo.program);

     glActiveTexture(GL_TEXTURE0);
     glBindTexture(GL_TEXTURE_2D, texture.texID);
     glUniform1i(shader_fbo.uni_texture, 0); 

     glEnableVertexAttribArray(shader_fbo.att_coord);
     glVertexAttribPointer(shader_fbo.att_coord,
                           2,
                           GL_FLOAT,
                           GL_FALSE,
                           0,
                           vertices);

     glBindFramebuffer(GL_FRAMEBUFFER, fbo.fbo);

     buffers[0] = GL_COLOR_ATTACHMENT0;
     buffers[1] = GL_COLOR_ATTACHMENT1;

     glDrawBuffers(2, buffers);

     glViewport(0, 0,
                texture.width, texture.height);

     glActiveTexture(GL_TEXTURE0);
     glBindTexture(GL_TEXTURE_2D, texture.texID);
     glUniform1i(shader_fbo.uni_texture, 0);


     glClear(GL_COLOR_BUFFER_BIT);

     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);

     glDisableVertexAttribArray(shader_fbo.att_coord);
     glBindFramebuffer(GL_FRAMEBUFFER, 0);
     glBindTexture(GL_TEXTURE_2D, 0);


     /*render both textures now to screen*/


     glUseProgram(shader_screen.program);

     glEnableVertexAttribArray(shader_screen.att_coord);
     glVertexAttribPointer(shader_screen.att_coord,
                           2,
                           GL_FLOAT,
                           GL_FALSE,
                           0,
                           vertices);



     /*render the first texture to the left side of the screen*/

     glViewport(0, 0,
                glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT));


     glClear(GL_COLOR_BUFFER_BIT);


     glActiveTexture(GL_TEXTURE0);
     glBindTexture(GL_TEXTURE_2D, fbo.texture0);
     glUniform1i(shader_screen.uni_texture, 0); 

     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);

     /*render the second texture to the right side of the screen*/

     glViewport(glutGet(GLUT_WINDOW_WIDTH)/2, 0,
                glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT));


     glActiveTexture(GL_TEXTURE0);
     glBindTexture(GL_TEXTURE_2D, fbo.texture1);
     glUniform1i(shader_screen.uni_texture, 0); 


     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);


     glutSwapBuffers();

 }

0 个答案:

没有答案