渲染到纹理(Render to texture):使用FrameBuffer

弄了一天,找啊找,没找到什么例子,幸好irrlicht里有个使用FrameBuffer渲染到纹理的例子,看了下,一copy,哇,ok了啊。

主要代码:

 // generate color texture
 glGenTextures(1, &TextureName);
 glBindTexture(GL_TEXTURE_2D, TextureName);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width,height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

 // generate depth texture
 glGenTextures(1, &DepthRenderBuffer);
 glBindTexture(GL_TEXTURE_2D, DepthRenderBuffer);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

 glGenFramebuffersEXT(1, &ColorFrameBuffer);
 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, ColorFrameBuffer);
 
 // attach color texture to frame buffer
 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
  GL_COLOR_ATTACHMENT0_EXT,
  GL_TEXTURE_2D,
  TextureName,
  0);
 // attach depth texture to depth buffer
 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
  GL_DEPTH_ATTACHMENT_EXT,
  GL_TEXTURE_2D,
  DepthRenderBuffer,
  0); 

不过,发现上面使用的DepthBuffer也是纹理,而我开始找到的是另一种方式的,如下:

 //depth buffer
 glGenRenderbuffersEXT(1, &m_db);
 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_db);
 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height);
 //bind frame and depth buffer
 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_db);

如果使用这个代替上面的depthBuffer的纹理时,使用glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)会报GL_FRAMEBUFFER_UNSUPPORTED_EXT。

现在也还不知道,希望有谁知道的可以给些留言,谢谢。

你可能感兴趣的:(ext,filter,buffer,byte)