ofFbo and ofTexture

Hello forum,

I have a functional opengl application and i am trying to encapsulate it in OF. Here goes the code snippet:

   //setup the shadowmap texture
   glGenTextures(1, &shadowMapTexID);
   glActiveTexture(GL_TEXTURE0);
   glBindTexture(GL_TEXTURE_2D, shadowMapTexID);

   //set texture parameters
   GLfloat border[4]={1,0,0,0};
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_BORDER);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_BORDER);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_MODE,GL_COMPARE_REF_TO_TEXTURE);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_FUNC,GL_LEQUAL);
   glTexParameterfv(GL_TEXTURE_2D,GL_TEXTURE_BORDER_COLOR,border);

   glTexImage2D(GL_TEXTURE_2D,        // target # texData.textureTarget
		0,                    // mentions the level of detail number, 0 is the base image level
		GL_DEPTH_COMPONENT24, // internalFormat - specifies the internal format of the texture # texData.glTypeInternal
		SHADOWMAP_WIDTH,      // width  # (GLint)texData.tex_w
		SHADOWMAP_HEIGHT,     // height # (GLint)texData.tex_h
		0,                    // specifies the width of the border
		GL_DEPTH_COMPONENT,   // specifies the format of the texel data # glFormat
		GL_UNSIGNED_BYTE,     // specifies the data type of the texel data # pixelType
		NULL);                // specifies the pointer to the image data in memory
   
   //set up FBO to get the depth component
   glGenFramebuffers(1,&fboID);
   glBindFramebuffer(GL_FRAMEBUFFER,fboID);
   glFramebufferTexture2D(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D,shadowMapTexID,0);

Now i am trying to create the above with the OF encapsulation as follows:

   //initialize the shadow map width and height
   mShadowMapWidth = 512;
   mShadowMapHeight = 512;

   //create the shadow map texture
   ofTextureData shadowTextureInfo;

   shadowTextureInfo.textureTarget = GL_TEXTURE_2D;
   shadowTextureInfo.width = mShadowMapWidth;
   shadowTextureInfo.height = mShadowMapHeight;
   shadowTextureInfo.glTypeInternal = GL_DEPTH_COMPONENT24;

   shadowmapTexture.allocate(shadowTextureInfo,GL_DEPTH_COMPONENT,GL_UNSIGNED_BYTE);

   //set some of the texture parameters
   GLfloat border[4]={1,0,0,0};
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_BORDER);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_BORDER);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_MODE,GL_COMPARE_REF_TO_TEXTURE);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_FUNC,GL_LEQUAL);
   glTexParameterfv(GL_TEXTURE_2D,GL_TEXTURE_BORDER_COLOR,border);

Now i am bit confused to send the above texture as color attachment to the fbo. The only function that is available within ofFbo is attachTexture(…) and inside the function there is the following statement that raise the confusion:

   glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachmentPoint, tex.texData.textureTarget, tex.texData.textureID, 0);

I want to send GL_DEPTH_ATTACHMENT as attachmentPoint . Will it be valid ?

Thanks

no but you can create an fbo with a texture as a depth buffer using:

ofFbo::Settings settings;
settings.witdth = w;
settings.height = h;
...
settings.useDepth = true;
settings.depthStencilAsTexture = true;
fbo.allocate(settings);

then access the depth texture using:

fbo.getDepthTexture()

but yes we could refactor the attachTexture method to also have a attachDepthTexture method, if you want to give it a try and send a pull request in github?

2 Likes