Possible to glTexImage2d an NPOT image onto a Pow-2 texture without extra allocation?
Asked Answered
S

2

0

I have discovered that there are still a fair number of drivers out there that don't support NPOT textures so I'm trying to retro-fit my 2D engine (based on OpenTK, which is in turn based on OpenGL) with Texture2D support instead of relying on GL_ARB_texture_rectangle. As part of this I am forcing all NPOTS texture bitmaps to allocate extra space up to the next power-of-2 size so they won't cause errors on these drivers. My question is, do I really have to resize the real bitmap and texture and allocate all that extra memory, or is there a way to tell OpenGL that I want a power-of-2 size texture, but I'm only going to use a portion of it in the upper left?

Right now my call looks like this:

GL.TexImage2D(texTarget, 0, PixelInternalFormat.Rgba8, bmpUse.Width, bmpUse.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, bits.Scan0);

This is after I have made bmpUse be a copy of my real texture bitmap with extra space on the right and bottom.

Superheterodyne answered 22/2, 2011 at 3:9 Comment(0)
S
6

Use glTexImage2D with empty data to initialize the texture and glTexSubImage2D to fill a portion of it with data. Technically OpenGL allows the data parameter given to glTexImage{1,2,3}D to be a null pointer, indicating that the texture object is just to be initializd. It depends on the language binding, if that feature remains supported in the target language – just test what happens if you pass a null pointer.

Semblance answered 22/2, 2011 at 8:25 Comment(3)
Is any extension/feature required in order to use this? Am I better off just making a larger texture or using glTexSubImage2D if I'm concerned about compatibility with a wide range of drivers?Superheterodyne
No, it's standard behaviour, part of OpenGL since ancient ages. But you must initialize the texture with glTexImage before filling it with content using glTexSubImage.Semblance
For the record, 'NULL' as an acceptable data pointer to glTexImage2D dates back to OpenGL 1.1, which is already a requirement if you're using glGenTextures and is so old that even Microsoft's default GL driver should get it right.Smaragdine
H
0

datenwolf is right on how to initialize the texture with just a partial image, but there are 2 issues with this you need to be aware of:

  • you need to remap the texture coordinates of your mesh, as the [0-1] texture range of the full texture now also contains uninitialized data, as opposed to your full texture. The useful range is now [0-orig_width/padded_width]
  • wrapping of your texture will only wrap the whole texture, not your sub-part.
Haarlem answered 22/2, 2011 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.