Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

randomZ

Texture from PNG - problems

This topic is 5781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m trying to load a texture from a PNG file. However, the texture I get is black, only a few lines of colorful pixels at the rim of the texture. I can''t figure what''s wrong with my code. Here it goes:
  
GLuint tex;

bool loadPngTex(string fname)
{
  // ... PNG loading stuff

  // which I know is correct ''cause I''ve displayed a PNG in 2D

  // with the same code


  png_byte **imageData = png_get_rows(pngStruct, pngInfo);
  GLubyte* newImage = new GLubyte[imageHeight * imageWidth * bytesPerPixel];
  
  // reverse row order so that the lower left corner is 0,0

	for (int i = imageHeight-1; i >= 0; i--)
	{
    memcpy(&newImage[imageHeight - i - 1], imageData[i], imageWidth * bytesPerPixel);
	}

  glGenTextures(1, &tex);
  glBindTexture(GL_TEXTURE_2D, tex);
  glTexImage2D(GL_TEXTURE_2D, 0, bytesPerPixel, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, newImage);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	// Linear Filtering

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering


  delete newImage;
  png_destroy_read_struct(&pngStruct, &pngInfo, NULL);
  return true;
}

What''s wrong with this?
  

"George W. Bush Geography Simplification Initiative"
More info on George W. Bush My Homepage (C++ SDL OpenGL Game Programming)

Share this post


Link to post
Share on other sites
Advertisement

  
memcpy(&newImage[(imageHeight - i - 1)*imageWidth], imageData[i], imageWidth * bytesPerPixel);

Share this post


Link to post
Share on other sites
OK; I can see what I was doing wrong now.
However, your version is not 100% correct, as you forget to multiply with bytesPerPixel.
I use this version, and it works fine:


  
memcpy(&newImage[(imageHeight - i - 1) * imageWidth * bytesPerPixel],
imageData[i], imageWidth * bytesPerPixel);


Thank you!


"George W. Bush Geography Simplification Initiative"

More info on George W. Bush
My Homepage (C++ SDL OpenGL Game Programming)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!