Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3086 in orxonox.OLD for orxonox/branches/images/importer/material.cc


Ignore:
Timestamp:
Dec 4, 2004, 6:05:54 PM (19 years ago)
Author:
bensch
Message:

orxonox/branches/images: ability to readIn jpg-files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/images/importer/material.cc

    r3085 r3086  
    319319    printf ("setting Diffuse Map %s\n", dMap);
    320320
    321   diffuseTextureSet = loadBMP(dMap, &diffuseTexture);
     321  //  diffuseTextureSet = loadBMP(dMap, &diffuseTexture);
     322  diffuseTextureSet = loadJPG(dMap, &diffuseTexture);
    322323
    323324}
     
    384385}
    385386
    386 
    387 
    388 
     387bool Material::loadJPG (char* jpgName, GLuint* texture)
     388{
     389  struct jpeg_decompress_struct cinfo;
     390  tImageJPG *pImage = NULL;
     391  FILE *pFile;
     392 
     393  // Open a file pointer to the jpeg file and check if it was found and opened
     394  if((pFile = fopen(jpgName, "rb")) == NULL)
     395    {
     396      // Display an error message saying the file was not found, then return NULL
     397      printf("Unable to load JPG File %s.\n", jpgName);
     398      return false;
     399    }
     400 
     401  // Create an error handler
     402  jpeg_error_mgr jerr;
     403 
     404  // Have our compression info object point to the error handler address
     405  cinfo.err = jpeg_std_error(&jerr);
     406 
     407  // Initialize the decompression object
     408  jpeg_create_decompress(&cinfo);
     409 
     410  // Specify the data source (Our file pointer)
     411  jpeg_stdio_src(&cinfo, pFile);
     412 
     413  // Allocate the structure that will hold our eventual jpeg data (must free it!)
     414  pImage = (tImageJPG*)malloc(sizeof(tImageJPG));
     415 
     416  // Decode the jpeg file and fill in the image data structure to pass back
     417  decodeJPG(&cinfo, pImage);
     418 
     419  // This releases all the stored memory for reading and decoding the jpeg
     420  jpeg_destroy_decompress(&cinfo);
     421 
     422  // Close the file pointer that opened the file
     423  fclose(pFile);
     424 
     425
     426  if(pImage == NULL)                                                                    // If we can't load the file, quit!
     427    exit(0);
     428
     429  // Generate a texture with the associative texture ID stored in the array
     430  glGenTextures(1, texture);
     431 
     432  // Bind the texture to the texture arrays index and init the texture
     433  glBindTexture(GL_TEXTURE_2D, *texture);
     434 
     435  // Build Mipmaps (builds different versions of the picture for distances - looks better)
     436  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->sizeX, pImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
     437 
     438  // Lastly, we need to tell OpenGL the quality of our texture map.  GL_LINEAR_MIPMAP_LINEAR
     439  // is the smoothest.  GL_LINEAR_MIPMAP_NEAREST is faster than GL_LINEAR_MIPMAP_LINEAR,
     440  // but looks blochy and pixilated.  Good for slower computers though.
     441 
     442  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
     443  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
     444 
     445
     446  // Now we need to free the image data that we loaded since OpenGL stored it as a texture
     447 
     448  if (pImage)                                                                           // If we loaded the image
     449    {
     450      if (pImage->data)                                                 // If there is texture data
     451        {
     452          free(pImage->data);                                           // Free the texture data, we don't need it anymore
     453        }
     454     
     455      free(pImage);                                                             // Free the image structure
     456    }
     457
     458}
     459
     460void Material::decodeJPG(jpeg_decompress_struct* cinfo, tImageJPG *pImageData)
     461{
     462  // Read in the header of the jpeg file
     463  jpeg_read_header(cinfo, TRUE);
     464 
     465  // Start to decompress the jpeg file with our compression info
     466  jpeg_start_decompress(cinfo);
     467 
     468  // Get the image dimensions and row span to read in the pixel data
     469  pImageData->rowSpan = cinfo->image_width * cinfo->num_components;
     470  pImageData->sizeX   = cinfo->image_width;
     471  pImageData->sizeY   = cinfo->image_height;
     472 
     473  // Allocate memory for the pixel buffer
     474  pImageData->data = new unsigned char[pImageData->rowSpan * pImageData->sizeY];
     475 
     476  // Here we use the library's state variable cinfo.output_scanline as the
     477  // loop counter, so that we don't have to keep track ourselves.
     478 
     479  // Create an array of row pointers
     480  unsigned char** rowPtr = new unsigned char*[pImageData->sizeY];
     481  for (int i = 0; i < pImageData->sizeY; i++)
     482    rowPtr[i] = &(pImageData->data[i*pImageData->rowSpan]);
     483 
     484  // Now comes the juice of our work, here we extract all the pixel data
     485  int rowsRead = 0;
     486  while (cinfo->output_scanline < cinfo->output_height)
     487    {
     488      // Read in the current row of pixels and increase the rowsRead count
     489      rowsRead += jpeg_read_scanlines(cinfo, &rowPtr[rowsRead], cinfo->output_height - rowsRead);
     490    }
     491 
     492  // Delete the temporary row pointers
     493  delete [] rowPtr;
     494 
     495  // Finish decompressing the data
     496  jpeg_finish_decompress(cinfo);
     497}
     498
Note: See TracChangeset for help on using the changeset viewer.