Chip Burwell
Chip Burwell

Reputation: 443

GL_TEXTURE_RECTANGLE_ARB not working with shader and OS X

I've got an OSX app that uses OpenGL. I'm drawing most of my stuff with textures of the type GL_TEXTURE_2D, and everything works fine as long as I stick to GL_TEXTURE_2D. But I need to have a couple of textures of type GL_TEXTURE_RECTANGLE_ARB.

To create a texture of type GL_TEXTURE_RECTANGLE_ARB I do the following:

// 4x4 RGBA texture data
GLubyte  TexData[4*4*4] =
{
    0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff,
    0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
    0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
    0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff,
    0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
    0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
    0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff,
    0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
};

GLuint myArbTexture;

glEnable(GL_TEXTURE_RECTANGLE_ARB);
glGenTextures(1, &myArbTexture);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, myArbTexture);

glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAX_LEVEL, 0 );
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, TexData);

To draw the texture I do the following:

SetupMyShader();
SetupMyMatrices();

glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, myArbTexture);

DrawMyQuads();

My shader is very simple:

    void main (void)
    {
       gl_FragColor = texture2D(Tex, v_texcoords)*u_color;
    }

Using the above code, my shader always references the last texture used in:

glBindTexture(GL_TEXTURE_2D, lastTexture)

instead of referencing the texture specified in:

glBindTexture(GL_TEXTURE_RECTANGLE_ARB, myArbTexture);

Some things to make note of:

  1. After every GL call I'm checking for errors glGetError() and I don't get any errors.
  2. If I replace GL_TEXTURE_RECTANGLE_ARB with GL_TEXTURE_2D everything works fine
  3. This is not an issue with uv (st) coordinates. When drawing with GL_TEXTURE_2D my uv are 0->1 and with GL_TEXTURE_RECTANGLE_ARB my uv are 0->texWidth or texHeight
  4. I'm running a pretty new mac that reports GL Version 2.1 NVIDIA-10.2.1 310.41.15f01 so it certainly should support GL_TEXTURE_RECTANGLE_ARB. (I would think anyway)
  5. I've narrowed down the issue enough to be pretty darn sure that when rendering the shader always refers to the previous texture that was bound as GL_TEXTURE_2D. My quad always draws in the right place and with sensible uv coords, it's just that it is referencing the wrong texture.

So, anyone got any guesses what I'm missing? Is there some call that I should be making other than glBindTexture that my shader would need when using GL_TEXTURE_RECTANGLE_ARB?

Upvotes: 1

Views: 722

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54642

You will need to update your shader code to use rectangle textures. The uniform needs to be declared as:

uniform sampler2DRect Tex;

and accessed with:

    gl_FragColor = texture2DRect(Tex, v_texcoords)*u_color;

Another aspect to keep in mind is that texture coordinates are defined differently for rectangle textures. Instead of the normalized texture coordinates in the range 0.0 to 1.0 used by all other texture types, rectangle textures use non-normalized texture coordinates in the range 0.0 to width and 0.0 to height.

Upvotes: 5

Related Questions