glTexImage2D does nothing

Started by
11 comments, last by Adrian Bibby Walther 11 years, 11 months ago
I'm using SDL_image to load textures for my game, I then try to load them into OpenGL using glTexImage2D.

Using the following code I've found out, that the problem lies in glTexImage2D, as it simply does nothing. (at least the width and height of the texture remains unchanged)

int ID,Width,Height;

glGetIntegerv(GL_TEXTURE_BINDING_2D,&ID);
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&Width);
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&Height);

std::cout << ID << ": " << Width << "*" << Height << std::endl;


The weird thing is that I have another program, and it uses the exact same code, but it works in that program.

glGetError returns 0 ie no error.

The code loading the texture and all is compiled and linked into a static library, and I have a test project that links with it, so I can see, if the code I've written works or not.
Advertisement
Post the relevant code where glTexImage2D is actually called.
Here you go:

Clear();

SDL_Surface *Surf = IMG_Load(File.c_str());

if(!Surf) return false;

GLenum Mode = GL_RGB;
if(Surf->format->BytesPerPixel == 4){
Mode = GL_RGBA;
}

Width = Surf->w;
Height = Surf->h;

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

glTexImage2D(GL_TEXTURE_2D,0,Mode,Width,Height,0,Mode,GL_UNSIGNED_BYTE,Surf->pixels);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

int Min,Mag;

glGetTexParameteriv(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,&Min);
glGetTexParameteriv(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,&Mag);

SDL_FreeSurface(Surf);

glBindTexture(GL_TEXTURE_2D,0);
Have you checked to make sure that the GL context is valid and initialized?
wglGetCurrentContext() doesn't return NULL. I don't know if there is more to check than that.
Check your width and height to make sure that they're powers of two if you have older hardware that doesn't support non-power-of-two textures. Also given that you're using GL_RGB formats you should be setting your glPixelStorei parameters appropriately.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


Check your width and height to make sure that they're powers of two if you have older hardware that doesn't support non-power-of-two textures. Also given that you're using GL_RGB formats you should be setting your glPixelStorei parameters appropriately.


If it is a power of 2 problem, then glGetError() would return something other than GL_NO_ERROR.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
After fiddling around with the code for a bit, it appears that I've solved the problem.
I don't know what caused it though.
I'm not sure, but shouldn't glTexParameters be called before glTexImage2D? Maybe that fixed it?

Derp


I'm not sure, but shouldn't glTexParameters be called before glTexImage2D? Maybe that fixed it?


No, it doesn't matter of you call it after or before.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement