Monet R. Adams
Monet R. Adams

Reputation: 123

X,Y Width,Height To OpenGL Texture Coord

If I got a 256 x 256 texture, and a image that's 32 x 32 at X: 192 Y: 128, what algerithm would be used to use glTexCoord2f to only draw the 32 x 32 image at X: 192 Y: 128 (to cut out the other parts of the image)?

Here is an example of what I want to do. The blue/red box would be what I'd want to use. But I only want to draw that box, nothing surrounding it, or the whole texture.

https://i.sstatic.net/8Zefq.png

Upvotes: 0

Views: 254

Answers (1)

Johny
Johny

Reputation: 1997

Is this what you want?

float f = 1.0f/256.0f;
glBegin( GL_QUADS );
    glTexCoord2f( 192 * f, 128  * f );
    glVertex2f( 192, 128 );

    glTexCoord2f( (192 + 32) * f, 128  * f );
    glVertex2f( 192 + 32, 128 );

    glTexCoord2f( (192 + 32) * f, (128 + 32) * f );
    glVertex2f( 192 + 32, 128 + 32 );

    glTexCoord2f( 192 * f, (128 + 32) * f );
    glVertex2f( 192, 128 + 32 );
glEnd();

Remeber that texture coordinates are scaled to the <0,1> interval. Also intermediate mode was deprecated in OpenGL 3.

Upvotes: 1

Related Questions