glsl fragmentshader render objectID

glsl, opengl

Solution

There are several problems with your question.

First, `GL_LUMINANCE16` is not an "integer texture." It is a texture that contains normalized unsigned integer values. It uses integers to represent floats on the range [0, 1]. If you want to store actual integers, you must use an actual integer image format.

Second, you cannot render to a luminance texture; they are not color-renderable formats. If you actually want to render to a single-channel texture, you must create a single-channel image format. So instead of `GL_LUMINANCE16`, you use `GL_R16UI`, which is a 16-bit single-channel unsigned integral image format.

Now that you have this set up correctly, it's pretty trivial. Define a `uint` fragment shader output and have your fragment shader write your `uint` value to it. This `uint` could come from the vertex shader or from a uniform; however you want to do it.

Obviously you'll also need to attach your texture or renderbuffer to an FBO, but I'm fairly sure you know that.

One final thing: don't use the phrase "texture buffer" unless you mean one of these. Otherwise, it gets confusing.

Problem

How do I properly render an integer ID of an object to an integer texture buffer? Say I have a texture2D with internal format GL_LUMINANCE16 and i attach it as color attachment to my FBO. When rendering an object, i pass an integer ID to the shader and would like to render this id into my integer texture. fragmentshader output is of type vec4 however. How do I properly transform my ID to four component float and avoid conversion inaccuracies such that in the end the integervalue in my integer texture target corresponds to the integer ID i wanted to render?

Original source