newly created texture is filled with color

first of all hi to OpenGL community : )

my problem is a bit strange im creating texture using this code


glGenTextures(1,&m_iID);
glBindTexture(GL_TEXTURE_2D,m_iID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

m_iWidth=1;
m_iHeight=1;
m_iSurfaceWidth=pSurface->w;
m_iSurfaceHeight=pSurface->h;

while (m_iWidth < iWidth) m_iWidth <<= 1;
while (m_iHeight < iHeight) m_iHeight <<= 1;
glTexImage2D( GL_TEXTURE_2D, 0, _nOfColors, m_iWidth, m_iHeight, 0, _texture_format, GL_UNSIGNED_BYTE, NULL );

creating it with dimensions like 64x64 128x64 256x32 gives this result:

the color is constant and doesnt change.

but using same code but dimensions like 128x128 256x64 etc gives clear texture:

im using SDL as API i know i can just create new Surface and blit it onto texture but i was just wondering whats the cause of it? or is there any alternate method to do it?

Since you haven’t sent any data to the texture (data=NULL), it just contains uninitialized garbage values. I’m not aware of any OpenGL implementation that would zero out the texture as it’s too much of a performance hit.

Send some data to the texture and it should be fine.

well i thought so too but glGetError() gives no error on this one.
also why it affects only textures smaller than 2^14 ?
if it was garbage it would be some random color instead it always gives same values

“why undefined is not random ?”
This is not a very interesting question to explore. Just listen to malexander, and properly initialize your data.

It is not an error.

im am now( thought you can give a hint how to make it in OpenGL but if it cant be done this way ill stick with SDL_Surface method )
i was just curious why its 2^14 threshold and why exactly this color and not just bunch of 0 like in dimension >= 2^14

anyway thanks for your responses

im am now( thought you can give a hint how to make it in OpenGL but if it cant be done this way ill stick with SDL_Surface method)

usually a texture is initialized with the contents of some image file. in that case you load the image file content into a buffer in main memory and pass the pointer to this buffer to the call to glTexImage2D as last parameter instead of NULL as you do.

i was just curious why its 2^14 threshold and why exactly this color and not just bunch of 0 like in dimension >= 2^14

undefined does not mean it has to be consistently wrong or even repeatable, you just get whatever happens to be in the GPU memory at the position your texture is allocated to.
2^14 = 16k which is either the maximum texture size supported by your card or perhaps even larger already, so it may just fail to create a texture at all at those sizes.