I cannot find any documentation for a separate stencil texture for framebuffer objects. Basically here is what I have so far:
glBindTexture(GL_TEXTURE_2D, this->mTextures[FRAMEBUFFER_STENCIL_TEXTURE]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8I, width, height, 0, GL_RED_INTEGER, GL_INT, 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_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, this->mTextures[FRAMEBUFFER_STENCIL_TEXTURE], 0);
But this causes a creation error. Is my format wrong or is it something else?
Yes, those format values are wrong for a stencil texture. You need to use specific stencil enums for both the internalFormat and format. The call should look like this for an 8-bit stencil texture:
glTexImage2D(GL_TEXTURE_2D, 0, GL_STENCIL_INDEX8,
width, height, 0, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, 0);
As has been pointed out in comments, pure stencil textures are only available in OpenGL 4.4 and higher.
An alternative is to allocate a depth/stencil texture, and just not use the depth part. However, sampling from the stencil part of a depth/stencil texture requires OpenGL 4.3 or higher, so that only reduces the version requirement by one minor version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With