1

I want to draw a texture of the dimensions 200px (width) x 80px (height) to a rectangle in openGL (which is 200px wide and 80px height). As textures in openGL ES 1.1 has to be of power two I want to load an image with the dimensions of 256px x 128px that contains my original 200px x 80px image in the upper left corner, the rest is left blank. How do I use glTexSubImage2D to get that part of interest from the 256px x 128px texture? I don't know how to provide that function correctly with the last argument, which is pixels.

Furthermore, I read that I have to somehow use glPixelStorei?

Could anybody please provide my with a snippet of sample code, please? I've been working really hard to figure this out, but I just don't get it.

BTW, I also tried not to use glTexSubImage2D, but to adjust the TextureCoords between 0...1, but that didn't work eiter.

genpfault
  • 51,148
  • 11
  • 85
  • 139
user661577
  • 201
  • 1
  • 2
  • 5
  • 1
    You can indeed use non-power-of-two textures on most iOS devices under certain circumstances. See details here: http://stackoverflow.com/questions/4760174/rendering-to-non-power-of-two-texture-on-iphone – Ben Zotto May 14 '11 at 22:06
  • Is the iPhone 3G compatible with non-POT textures or only the iPhone3GS upwards? Thanks! – user661577 May 15 '11 at 00:49
  • 3GS upwards (everything on the SGX platform). Compatibility charts and details here: http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/OpenGLESPlatforms/OpenGLESPlatforms.html – Ben Zotto May 15 '11 at 13:38

1 Answers1

2

You will inevitably have to select a subrange from 0…1 for the texture coordinates. glTexSubImage2D only (re-)defines a portion of the texture, while leaving the total dimensions as they were.

Calculating the proper subrange values is a bit nonintuitve: Say your total texture is 8 pixels wide, with the pixels 3…6 having actual content, then the texture coordinates map like following:

        2/8             6/8 
 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
 ^                               ^
0.0                             1.0

So the texture coordinates to use are:

(t0 - 1)/N … (t1)/N ; t0, t1, N in pixels.

If you want to address your subtexture in the range 0…1 on parameter tau the formula is

w := t1 - t0 u = (t0 - 1)/N + w * tau

datenwolf
  • 159,371
  • 13
  • 185
  • 298