Jump to content
  • Advertisement
Sign in to follow this  
littleeboy

Help! Load 16bit grayscale PNG heightmap texture error!

This topic is 3352 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 plan to use vertex texture fetch to render terrain, so I want to load puget sound 1k*1k png image as heightmap texture. The png image is 16bit grayscale. When I load the 16bit grayscale image using libpng,I allocate iWidth*iHieht*2 bytes space. But how to assemble the two bytes to GL_LUMINANCE16 texture internalformat. I am confused about that. I have read the libpng project from www.libpng.org , but I found 16bit grayscale was always translated to 8bit. I can dispay 8bit grayscale texture. If I translate the original 16bit grayscale puget sound data to 8bit grayscale, the height data will be less accurate. The following is my code written in C and OpenGL, NV Geforce 9500 card,Windows XP OS.
/* OpenGL texture info */
typedef struct
{
  GLsizei width;
  GLsizei height;

  GLenum format;
  GLint internalFormat;
  GLuint id;

  GLubyte *texels;

} gl_texture_t;


/* texture Id */
GLuint texId;

gl_texture_t *png_tex = NULL;


void
GetPNGtextureInfo (int color_type, gl_texture_t *texinfo)
{
  switch (color_type)
    {
    case PNG_COLOR_TYPE_GRAY:
      texinfo->format = GL_LUMINANCE;
      texinfo->internalFormat =GL_LUMINANCE16 ;//
      break;

    default:
      /* Badness */
      break;
    }
}


gl_texture_t *
ReadPNGFromFile (const char *filename)
{
  gl_texture_t *texinfo;
  png_byte magic[8];

  int bit_depth, color_type;
  FILE *fp = NULL;
  png_bytep *row_pointers = NULL;
  int i;

  /* open image file */
  fp = fopen (filename, "rb");
  if (!fp)
    {
      fprintf (stderr, "error: couldn't open \"%s\"!\n", filename);
      return NULL;
    }

  /* read magic number */
  fread (magic, 1, sizeof (magic), fp);

  /* check for valid magic number */
  if (!png_check_sig (magic, sizeof (magic)))
    {
      fprintf (stderr, "error: \"%s\" is not a valid PNG image!\n",
	       filename);
      fclose (fp);
      return NULL;
    }

  /* create a png read struct */
  png_ptr = png_create_read_struct
    (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (!png_ptr)
    {
      fclose (fp);
      return NULL;
    }

  /* create a png info struct */
  info_ptr = png_create_info_struct (png_ptr);
  if (!info_ptr)
    {
      fclose (fp);
      png_destroy_read_struct (&png_ptr, NULL, NULL);
      return NULL;
    }

  /* create our OpenGL texture object */
  texinfo = (gl_texture_t *)malloc (sizeof (gl_texture_t));

  /* initialize the setjmp for returning properly after a libpng
     error occured */
  if (setjmp (png_jmpbuf (png_ptr)))
    {
      fclose (fp);
      png_destroy_read_struct (&png_ptr, &info_ptr, NULL);

      if (row_pointers)
	free (row_pointers);

      if (texinfo)
	{
	  if (texinfo->texels)
	    free (texinfo->texels);

	  free (texinfo);
	}

      return NULL;
    }

  /* setup libpng for using standard C fread() function
     with our FILE pointer */
  png_init_io (png_ptr, fp);

  /* tell libpng that we have already read the magic number */
  png_set_sig_bytes (png_ptr, sizeof (magic));

  /* read png info */
  png_read_info (png_ptr, info_ptr);

  /* get some usefull information from header */
  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
  color_type = png_get_color_type (png_ptr, info_ptr);

  /* convert index color images to RGB images */
  if (color_type == PNG_COLOR_TYPE_PALETTE)
    png_set_palette_to_rgb (png_ptr);

  /* convert 1-2-4 bits grayscale images to 8 bits
     grayscale. */
  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
    png_set_gray_1_2_4_to_8 (png_ptr);

  if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
    png_set_tRNS_to_alpha (png_ptr);

/*
  if (bit_depth == 16)
    png_set_strip_16 (png_ptr);
  else if (bit_depth < 8)
    png_set_packing (png_ptr);
*/

  /* update info structure to apply transformations */
  png_read_update_info (png_ptr, info_ptr);

  /* retrieve updated information */
  png_get_IHDR (png_ptr, info_ptr,
		(png_uint_32*)(&texinfo->width),
		(png_uint_32*)(&texinfo->height),
		&bit_depth, &color_type,
		NULL, NULL, NULL);

  /* get image format and components per pixel */
  GetPNGtextureInfo (color_type, texinfo);

  /* we can now allocate memory for storing pixel data */
  texinfo->texels = (GLubyte *)malloc (sizeof (GLubyte) * texinfo->width
	       * texinfo->height * 2 );

  /* setup a pointer array.  Each one points at the begening of a row. */
  row_pointers = (png_bytep *)malloc (sizeof (png_bytep) * texinfo->height);

  for (i = 0; i < texinfo->height; ++i)
    {
      row_pointers = (png_bytep)(texinfo->texels + i * texinfo->width * 2); 
    }

  /* read pixel data using row pointers */
  png_read_image (png_ptr, row_pointers);

  /* finish decompression and release memory */
  png_read_end (png_ptr, NULL);
  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);

  /* we don't need row pointers anymore */
  free (row_pointers);

  fclose (fp);
  return texinfo;
}


GLuint
loadPNGTexture (const char *filename)
{

  GLuint tex_id = 0;

  png_tex = ReadPNGFromFile (filename);

  if (png_tex && png_tex->texels)
    {
      glGenTextures (1, &png_tex->id);
      glBindTexture (GL_TEXTURE_2D, png_tex->id);

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

      glTexImage2D (GL_TEXTURE_2D, 0, png_tex->internalFormat,
		    png_tex->width, png_tex->height, 0, png_tex->format,
		    GL_UNSIGNED_BYTE, png_tex->texels);
     
     tex_id = png_tex->id;

     free (png_tex->texels);
     free (png_tex);

    }
  

  return tex_id;
}


Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by littleeboy
But how to assemble the two bytes to GL_LUMINANCE16 texture internalformat. I am confused about that.


Quote:

I have read the libpng project from www.libpng.org , but I found 16bit grayscale was always translated to 8bit. I can dispay 8bit grayscale texture.


Which of these is the problem?
Do you have 16-bit data correctly loaded and need to send it to an OpenGL texture, or do you only get 8-bit data when reading the texture from the file?

Share this post


Link to post
Share on other sites
Thank Erik Rufelt.


Quote: glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, data).

But I think the png_tex->texels was stored in GLubye format in memory ,not the GL_UNSIGNED_SHORT.

I have tried using glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE16 ,
png_tex->width, png_tex->height, 0, GL_LUMINANCE ,
GL_UNSIGNED_SHORT, png_tex->texels);

The texture is still not correct in my head.

Share this post


Link to post
Share on other sites
What OpenGL version are you using?
There are other formats such as GL_R16UI, or GL_R16F, that might be a better match for you. With GL_UNSIGNED_BYTE I believe each data-element will be just 8 bits, and you need GL_UNSIGNED_SHORT to tell OpenGL that the data is in 16-bit format.

Share this post


Link to post
Share on other sites
Thanks again.

Are you sure that the png_tex->texels was stored in GLubye is correct?

I use microsoft viual c++ 6.0 envioment. Because I use Geforce 9500 card, Do you mean I need update gl.h glut.h .

I will go my office to try in your guidence.

Hope you can focus on this topic.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!