LodePNG Function.

I really hope someone can help with this because I have been stuck with this,what could be simple problem, for days.
I am fairly new to c++ ,completely self taught and only 14 so I’ve probably made some bad errors. I wan’t to be able to load lots of different PNG textures like this.


GLuint * Player_Current_Texture;
GLuint Vietcong_Texture = loadPNG("Data/Vietcong.png");
GLuint Vietcong_Firing_Texture = loadPNG("Data/Vietcong_Firing.png");

so that I can use them like this:


Draw_Texture(48,*Player_Current_Texture);

my current method of loading PNGs is like this


GLuint loadPNG(const char* filename)
{
	GLuint texture;
	vector<unsigned char> buffer, image;
	LodePNG::loadFile(buffer, filename);
	LodePNG::Decoder decoder;
	decoder.decode(image, buffer.empty() ? 0 : &buffer[0], (unsigned)buffer.size());

	if(decoder.hasError()) PostQuitMessage(0);

	unsigned char* pixels=(unsigned char*)malloc(decoder.getWidth()*decoder.getHeight()*decoder.getChannels());

	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
	

	return texture;
}

and my method of drawing them is like so:

void Draw_Texture(float size, GLuint texture)
{
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glBindTexture( GL_TEXTURE_2D, texture );
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	float X = size/2;
	float O = -size/2;
	glColor3f(1,1,1);
	glBegin(GL_QUADS);
      glTexCoord2d(0.0,0.0); glVertex2d(O,O);
      glTexCoord2d(1.0,0.0); glVertex2d(X,O);
      glTexCoord2d(1.0,1.0); glVertex2d(X,X);
      glTexCoord2d(0.0,1.0); glVertex2d(O,X);
	glEnd();
}

However this just displays a white rectangle. Any help would be much appreciated as it would be very nice to get this problem out of the way. feel free too ask for more code if needs be.

thanks Rowan.

Hello,

try setting glEnable(GL_TEXTURE_2D); in your loadPNG function before you set up your texture. Try texturing without blending first. You don’t need to set the filter parameters in your Draw_Texture function if it was already done at image loading time. Why do you call Draw_Texture(48,Player_Current_Texture); (is Player_Current_Texture a GLint ?).

I assume you use the fixed function pipeline from OpenGL 2 and the quads are visible (so you see white quads and not the whole screen stays white)?

Thanks for your reply.
Player_Current_Texture is a GLint but for the purpose of testing I have now changed that to Vietcong_Texture, and the quad is drawn. I have tried the three things you suggested but without success. I would imagine it would be somthing wrong with this line:

unsigned char* pixels=(unsigned char*)malloc(decoder.getWidth()*decoder.getHeight()*decoder.getChannels());

can anyone confirm whether this is the correct way of asigning the pixels and then loading them into a texture like this? :

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);