glTexCoord2f - How can I set this to accept 'absolute' coordinates?

Started by ilazarte, May 27, 2010, 06:25:11

Previous topic - Next topic

ilazarte

I'm trying to understand how I can draw directly portions of a single texture file.
Take this quad I'm drawing from a pic which is  64x64 pixels. 

        GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(0.0f, 0.0f, 0.0f);
        GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(0.0f, 64.0f, 0.0f);
        GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(64.0f, 64.0f, 0.0f);
        GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(64.0f, 0.0f, 0.0f);

The above works fine, but if I wanted to draw only the lower left quadrant, I have to divide by 2 for the actual tex coords.  Like so below.
        GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(0.0f, 0.0f, 0.0f);
        GL11.glTexCoord2f(0.0f, 0.5f); GL11.glVertex3f(0.0f, 32.0f, 0.0f);
        GL11.glTexCoord2f(0.5f, 0.5f); GL11.glVertex3f(32.0f, 32.0f, 0.0f);
        GL11.glTexCoord2f(0.5f, 0.0f); GL11.glVertex3f(32.0f, 0.0f, 0.0f);


Is there some way I can just use the non-clamped values for the texture coordinates instead? 

So my second example would look like:
        GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(0.0f, 0.0f, 0.0f);
        GL11.glTexCoord2f(0.0f, 32.0f); GL11.glVertex3f(0.0f, 32.0f, 0.0f);
        GL11.glTexCoord2f(32.0f, 32.0f); GL11.glVertex3f(32.0f, 32.0f, 0.0f);
        GL11.glTexCoord2f(32.0f, 0.0f); GL11.glVertex3f(32.0f, 0.0f, 0.0f);


my texture settings are as follows, not sure I understand completely what there doing.
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);


The reason I'd like to do this is so I can simply display slices of an image - I'm trying to code what I think a sprite sheet is.  It's part learning experience for me.  Thanks! 

Ciardhubh

In theory you could use the texture rectangle extension (enable and then use GL_TEXTURE_RECTANGLE_ARB instead of GL_TEXTURE_2D):
http://www.opengl.org/registry/specs/ARB/texture_rectangle.txt

Textures bound via this extension use absolute values as coordinates. However there are some restrictions (see spec), it's rumoured to be slower than regular power of two textures (not sure if this applies if you use POT textures with this extension) and non-POT-textures aren't that well supported by some drivers.

AFAIK your best bet is to normalise texture coordinates to the range of [0..1].

ilazarte

powers of 2 it is.  thanks ciardhubh, btw, your redbook lwjgl examples have been invaluable.