Jump to content
  • Advertisement
Sign in to follow this  
hmcen

FBO+MRT(solved, Can same textures be used both as render source and render target?)

This topic is 4058 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, i'm writing a program using FBO and MRT to bind and render to multiple textures at same time. The program is nothing but a test to show how FBO and MRT work together. It could not be more simpler: bind four textures with FBO, initialize these textures with some specific value, then render to four textures at same time in a single rendering pass. I guarantee the correctness of the setup of FBO and textures, so i just post the latter two steps, and, the problem i met. Step one: Initialize textures with some specific value, i draw a quad the same size as these textures, setup value in a shader:
//bind FBO
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
    
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
	
cgGLEnableProfile(cgFragmentProfile);
cgGLBindProgram(begin_fp);
// Draw a quad to initialize the textures
glBegin(GL_QUADS);
{  
  glVertex2f(0.0, 0.0); 
  glVertex2f(width, 0.0);
  glVertex2f(width, height);
  glVertex2f(0.0, height);
}
glEnd();

cgGLDisableProfile(cgFragmentProfile);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);






In Cg fragment shader(begin_fp), values are set:
void main(out float4 color0 : COLOR0,
	  out float4 color1 : COLOR1,
	  out float4 color2 : COLOR2,
	  out float4 color3 : COLOR3)
{
   color0 = float4(0,0,1,1);
   color1 = float4(1,0,0,1);
   color2 = float4(0,1,0,1);
   color3 = float4(0,0,0,1);
}






Step two: Render a teapot to four textures:
//bind FBO
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (float)width/(float)height, 0.1, 200);

glTranslatef(0,0,-5);

//set Cg parameters
cgGLSetStateMatrixParameter(modelViewProj_param, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);

cgGLBindProgram(render2texture_vp);
cgGLEnableProfile(cgVertexProfile);

//four textures attached to FBO
cgGLSetTextureParameter( tex0_param, texture[0] );
cgGLSetTextureParameter( tex1_param, texture[1] );
cgGLSetTextureParameter( tex2_param, texture[2] );
cgGLSetTextureParameter( tex3_param, texture[3] );

cgGLBindProgram(render2texture_fp);
cgGLEnableProfile(cgFragmentProfile);

cgGLEnableTextureParameter(tex0_param);
cgGLEnableTextureParameter(tex1_param);
cgGLEnableTextureParameter(tex2_param);
cgGLEnableTextureParameter(tex3_param);

glutWireTeapot(1.5);    

cgGLDisableTextureParameter(tex0_param);
cgGLDisableTextureParameter(tex1_param);
cgGLDisableTextureParameter(tex2_param);
cgGLDisableTextureParameter(tex3_param);

cgGLDisableProfile(cgVertexProfile);
cgGLDisableProfile(cgFragmentProfile);
    
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);






Cg vertex shader(render2texture_vp)
void main(float4 position : POSITION,

          uniform float4x4 modelViewProj,

	  out float4 oPosition : POSITION,
	  out float4 NDCcoord: TEXCOORD0) //Normalized Device Coordinates 
{
	oPosition = mul(modelViewProj, position);
	
	NDCcoord = oPosition / oPosition,w;
        NDCcoord = 0.5 * NDCcoord + 0.5;
}






Cg fragment shader(render2texture_fp)
void main(float4 NDCcoord : TEXCOORD0,

	  uniform sampler2D cbuffer0,
	  uniform sampler2D cbuffer1,
	  uniform sampler2D cbuffer2,
	  uniform sampler2D cbuffer3,

	  out float4 color0 : COLOR0,
	  out float4 color1 : COLOR1,
	  out float4 color2 : COLOR2,
	  out float4 color3 : COLOR3)
{
   float4 P = NDCcoord;

   float4 k0 = tex2D(kbuffer0, P.xy);
   float4 k1 = tex2D(kbuffer1, P.xy); 
   float4 k2 = tex2D(kbuffer2, P.xy); 
   float4 k3 = tex2D(kbuffer3, P.xy); 
	
   //simply lookup the texture and pass the value
   color0 = k0;
   color1 = k1;
   color2 = k2;
   color3 = k3;
}






Now the problem is when i test thest textures, i can't see anything. I made some modifications to the program: created another FBO, fbo1, attached it with four new textures, newTexture[0]~newTexture[3]. In step2, i bind fbo1 instead of fbo, and in render2texture_fp, texture[0]~texture[3] are used as render source and newTexture[0]~newTexture[3] now as render target. The result is correct! So i doubt whether same textures could be used both as render source and render target,using FBO and MRT? If the answer is YES, then how could i explain this? Any one knows FBO & MRT please give me a hand! Thank you! [Edited by - hmcen on May 17, 2007 9:04:14 AM]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!