Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

map2.cpp

Go to the documentation of this file.
00001 /*
00002     toxic - A Global Illumination Renderer
00003     Copyright (C) 2003-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of toxic.
00007 
00008     toxic is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     toxic is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with toxic; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #include "map2.h"   // include first
00024 #include "common/meshio/imeshloader.h"
00025 #include "common/misc/types.h"
00026 
00027 #include <IL/il.h>
00028 
00029 using namespace sheep;
00030 using namespace std;
00031 using namespace toxic;
00032 
00033 namespace {
00034     inline
00035     Real convert_uint8_to_real(uint8 x) {
00036         return static_cast<Real>(x) / 255.0;
00037     }
00038 }
00039 
00040 Map2::Map2(const string &filename) {
00041     //!\todo Remove this constructor. Loading images must be made outside the renderer.
00042 
00043     ILuint image_id;
00044 
00045     ilGenImages(1, &image_id);
00046     ilBindImage(image_id);
00047 
00048     if(!LoadImage(filename))
00049         throw FileNotFoundException(filename);
00050 
00051     m_width = ilGetInteger(IL_IMAGE_WIDTH);
00052     m_height = ilGetInteger(IL_IMAGE_HEIGHT);
00053 
00054     const int size = m_width * m_height;
00055     uint8 *buffer = new uint8[size * 3];
00056 
00057     ilCopyPixels(0, 0, 0, m_width, m_height, 1, IL_RGB, IL_UNSIGNED_BYTE, buffer);
00058     ilDeleteImages(1, &image_id);
00059 
00060     m_texels.resize(size);
00061 
00062     uint8 *b = buffer;
00063 
00064     for(int i = 0; i < size; ++i) {
00065         m_texels[i].m_r = convert_uint8_to_real(*b++);
00066         m_texels[i].m_g = convert_uint8_to_real(*b++);
00067         m_texels[i].m_b = convert_uint8_to_real(*b++);
00068     }
00069 
00070     delete [] buffer;
00071 }
00072 
00073 Map2::Map2(int w, int h, const unsigned char *texels) { //!< 24-bit RGB format.
00074     assert(w > 0 && h > 0);
00075     assert(texels);
00076 
00077     m_width = w;
00078     m_height = h;
00079 
00080     const int size = m_width * m_height;
00081 
00082     m_texels.resize(size);
00083 
00084     for(int i = 0; i < size; ++i) {
00085         m_texels[i].m_r = convert_uint8_to_real(*texels++);
00086         m_texels[i].m_g = convert_uint8_to_real(*texels++);
00087         m_texels[i].m_b = convert_uint8_to_real(*texels++);
00088     }
00089 }

Generated on Tue May 11 01:31:50 2004 for toxic by doxygen 1.3.6