Exemplo n.º 1
0
	//Texture* TextureManager::LoadFromFile(const string& szTexturePath, const string& uniName)
	//{
	//	return LoadFromFile(TypeCast::stringToString(const_cast<string&>(szTexturePath))
	//		,TypeCast::stringToString(const_cast<string&>(uniName)));
	//}
	Texture* TextureManager::LoadFromFile(const String& szTexturePath, const String& uniName)
	{
		REQUIRES(m_currentRenderer);
		REQUIRES( !szTexturePath.empty() );

		//TODO check if the texture is loaded before : return the old tex

		//ilInit();

		ilEnable( IL_ORIGIN_SET );
		ilOriginFunc( IL_ORIGIN_LOWER_LEFT );

		ILuint idImage = ilGenImage();
		ilBindImage( idImage );
		ilLoadImage( szTexturePath.c_str() );


		if(IL_NO_ERROR != ilGetError())
		{
			NLOG("IL Load From File Error:\n",0);
			return getDefaultTex();
			//throw NException(TextureDevilError, String(TEXT("IL Load From File Error:\n")) + szTexturePath);
		}

		Texture* newTex = new Texture(szTexturePath,uniName);

		ilConvertImage(IL_RGBA,IL_UNSIGNED_BYTE);
		//convert to this type

		newTex->width = ilGetInteger( IL_IMAGE_WIDTH );
		newTex->height = ilGetInteger( IL_IMAGE_HEIGHT );
		newTex->format = NBE_COLOR_RGBA;
		newTex->type = NBE_UNSIGNED_BYTE;

		newTex->textureIdx = m_currentRenderer->createTexture(szTexturePath.c_str(),
			ilGetData(),newTex->width, newTex->height, newTex->format, newTex->type, true);
																						
		m_loadedTextures.push_back(newTex);
		
		// Delete the DevIL image.

		ilDeleteImage( idImage );
		return newTex;

	}
Exemplo n.º 2
0
void QTWindow::setupWindow(QTWindow * window, const char * title, const char * imagePath)
{
	ilInit();
	ilLoadImage((const ILstring)imagePath); 

	imageInputWidth  = (int) ilGetInteger(IL_IMAGE_WIDTH);
	imageInputHeight = (int) ilGetInteger(IL_IMAGE_HEIGHT);

	pixmapInput = new BYTE[3 * imageInputWidth * imageInputHeight];

	ilCopyPixels(0, 0, 0, imageInputWidth, imageInputWidth, 1, IL_RGB, IL_UNSIGNED_BYTE, pixmapInput);

	window->setWindowTitle(QString::fromUtf8(title));
    window->resize(imageInputWidth, imageInputHeight);
	QPixmap pm(imagePath);
	window->setPixmap(pm);
	window->show();
}
Exemplo n.º 3
0
bool ILContainer::Initialize(const char * file_name)
{
	assert(this->il_handle == BAD_IL_VALUE);
	if ((this->il_handle = ilGenImage()) == BAD_IL_VALUE)
		return false;
	ilBindImage(this->il_handle);
	if (!ilLoadImage(file_name))
		return false;

	glGenTextures(1, &this->il_texture_handle);
	glBindTexture(GL_TEXTURE_2D, this->il_texture_handle);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), this->width = ilGetInteger(IL_IMAGE_WIDTH), this->height = ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
	return true;
}
Exemplo n.º 4
0
//-----------------------------------------------------------------------------
//Функция загрузки изображения текстуры
void Load_Tex_Image()
{
    int width, height, bpp;

    ilLoad(IL_JPG, reinterpret_cast<const ILstring>(TEX_IMAGE_NAME));
    int err = ilGetError();                          // Считывание кода ошибки
    if (err != IL_NO_ERROR)
    {
        const char* strError = iluErrorString(err);  // Считываем строку ошибки
        std::cout << "Error load texture image: " << strError << std::endl;
        exit(EXIT_FAILURE);
    }
    else
    {
        std::cout << "Load texture image completed!" << std::endl;
        width  = ilGetInteger(IL_IMAGE_WIDTH);
        height = ilGetInteger(IL_IMAGE_HEIGHT);
        bpp    = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
        std::cout << "width:  "<< width << std::endl << "height: "
                  << height << std::endl << "bpp:    " << bpp << std::endl;
    }

    unsigned char* data = ilGetData();
    unsigned int type;

    switch (bpp) {
    case 1:
      type  = GL_RGB8;
      break;
    case 3:
      type = GL_RGB;
      break;
    case 4:
      type = GL_RGBA;
      break;
    }
    glGenTextures(1, &texture[0]);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0,
    GL_RGB, GL_UNSIGNED_BYTE, data);
}
Exemplo n.º 5
0
Image* ImagesLoader::loadFromFile(const std::string& file){
    ILuint texture;
    ilGenImages(1, &texture);
    ilBindImage(texture);

    if(!(ilLoadImage(const_cast<ILstring>(file.c_str()))))
        throw LoadingFailed(file);

    glm::ivec2 size;
    size.x = ilGetInteger(IL_IMAGE_WIDTH);
    size.y = ilGetInteger(IL_IMAGE_HEIGHT);

    const unsigned char* pix = ilGetData();

    Image* img = new Image(size, PXF_A8R8G8B8, pix);
    img->flip();

    return img;
}
Exemplo n.º 6
0
/*
=================
- create the texture for use.
=================
*/
bool cTexture::createTexture(LPCSTR theFilename) 	// create the texture for use.
{

	ILboolean success = false;

	if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION)
	{
		return false;
	}

	ilInit();  /*Initialize the DevIL library*/
	ilGenImages(1, &ilTextureID); //Generate DevIL image objects
	ilBindImage(ilTextureID); /* Binding of image object */
	success = ilLoadImage((const ILstring)theFilename); /* Loading of image*/

	if (!success)
	{
		ilDeleteImages(1, &ilTextureID);
		return false;
	}

	success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); // Convert every colour component into unsigned byte.
	if (!success)
	{
		return false;
	}

	textureWidth = ilGetInteger(IL_IMAGE_WIDTH);
	textureHeight = ilGetInteger(IL_IMAGE_HEIGHT);

	glGenTextures(1, &GLTextureID); // GLTexture name generation 
	glBindTexture(GL_TEXTURE_2D, GLTextureID); // Binding of GLtexture name 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Use linear interpolation for magnification filter
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Use linear interpolation for minifying filter 
	glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), textureWidth,
		textureHeight, 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
		ilGetData()); /* Texture specification */
	glBindTexture(GL_TEXTURE_2D, GLTextureID); // Binding of GLtexture name 

	ilDeleteImages(1, &ilTextureID);

	return true;
}
Exemplo n.º 7
0
ILuint CopyImage(Image *src, Image *dest)
{
    ILuint newImageID;

    ilGenImages(1, &newImageID);
    ilBindImage(newImageID);

    if (!ilCopyImage(src->id))
       return 0;

    dest->id      = newImageID;
    dest->rawData = ilGetData();
    dest->width   = ilGetInteger(IL_IMAGE_WIDTH);
    dest->height  = ilGetInteger(IL_IMAGE_HEIGHT);
    dest->depth   = ilGetInteger(IL_IMAGE_DEPTH);
    dest->format  = ilGetInteger(IL_IMAGE_FORMAT);

    return 1;
}
Exemplo n.º 8
0
	bool TextureHandler::load_texture(std::wstring file_name) {
		m_devil_image_id = ilGenImage();
		ilBindImage(m_devil_image_id);
		ilLoadImage(file_name.c_str());
		get_DevIL_error();
		m_width = ilGetInteger(IL_IMAGE_WIDTH);
		m_height= ilGetInteger(IL_IMAGE_HEIGHT);
		ILint format = ilGetInteger(IL_IMAGE_FORMAT);
		ILint type = ilGetInteger(IL_IMAGE_TYPE);
		ilLoadImage(file_name.c_str());
		get_DevIL_error();
		//we want to load unsigned char RGBA images
		m_data = new unsigned char[m_width * m_height * 4];
		ilCopyPixels(0, 0, 0, m_width, m_height, 1, format, type, m_data);
		get_DevIL_error();
		ilDeleteImage(m_devil_image_id);
		send_to_gpu();
		return true;
	}
Exemplo n.º 9
0
GLuint Renderer::loadTexture(const std::string &fname)
{
	ILuint imageID;
	GLuint textureID;
	ILboolean success;
	ILenum error;
	ilGenImages(1, &imageID); 
	ilBindImage(imageID); 
	success = ilLoadImage(fname.c_str());
	if (success)
	{
		ILinfo ImageInfo;
		iluGetImageInfo(&ImageInfo);
		if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
		{
			iluFlipImage();
		}
	
		success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

		if (!success)
		{
			error = ilGetError();
			return 0;
		}

		glGenTextures(1, &textureID);
		glBindTexture(GL_TEXTURE_2D, textureID);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_FORMAT), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
	}
	else
	{
		error = ilGetError();
		return 0;
	}

	ilDeleteImages(1, &imageID);
	return textureID;
}
Exemplo n.º 10
0
ILboolean ilSetPixels2D(ILimage* image, ILint XOff, ILint YOff, ILuint Width, ILuint Height, void *Data)
{
	ILuint	c, SkipX = 0, SkipY = 0, NewBps, PixBpp;
	ILint	x, y, NewWidth, NewHeight;
	ILubyte	*Temp = (ILubyte*)Data, *TempData = image->Data;

	if (ilIsEnabled(IL_ORIGIN_SET)) {
		if ((ILenum)ilGetInteger(IL_ORIGIN_MODE) != image->Origin) {
			TempData = iGetFlipped(image);
			if (TempData == NULL)
				return IL_FALSE;
		}
	}

	PixBpp = image->Bpp * image->Bpc;

	if (XOff < 0) {
		SkipX = abs(XOff);
		XOff = 0;
	}
	if (YOff < 0) {
		SkipY = abs(YOff);
		YOff = 0;
	}

	if (image->Width < XOff + Width)
		NewWidth = image->Width - XOff;
	else
		NewWidth = Width;
	NewBps = Width * PixBpp;

	if (image->Height < YOff + Height)
		NewHeight = image->Height - YOff;
	else
		NewHeight = Height;

	NewWidth -= SkipX;
	NewHeight -= SkipY;

	for (y = 0; y < NewHeight; y++) {
		for (x = 0; x < NewWidth; x++) {
			for (c = 0; c < PixBpp; c++) {
				TempData[(y + YOff) * image->Bps + (x + XOff) * PixBpp + c] =
					Temp[(y + SkipY) * NewBps + (x + SkipX) * PixBpp + c];					
			}
		}
	}

	if (TempData != image->Data) {
		ifree(image->Data);
		image->Data = TempData;
	}

	return IL_TRUE;
}
Exemplo n.º 11
0
bool M_loadImage(const char * filename, void * data)
{
	DevILInit();

	// gen image
	ILuint ImgId = 0;
	ilGenImages(1, &ImgId);

	// bind image
	ilBindImage(ImgId);

	// load image
	if(! ilLoadImage(filename))
	{
		ilDeleteImages(1, &ImgId);
		DevILShutDown();
		return false;
	}

	// get properties
	int bytePerPix = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);

	int width  = ilGetInteger(IL_IMAGE_WIDTH);
	int height = ilGetInteger(IL_IMAGE_HEIGHT);

	if(bytePerPix == 4)
		ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
	else
		ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

	// create image
	MImage * image = (MImage *)data;
	image->create(M_UBYTE, (unsigned int)width, (unsigned int)height, (unsigned int)bytePerPix);

	// copy data
	unsigned int size = image->getSize();
	memcpy(image->getData(), ilGetData(), size);

	ilDeleteImages(1, &ImgId);
	DevILShutDown();
	return true;
}
Exemplo n.º 12
0
bool CBitmap::LoadGrayscale(const std::string& filename)
{
	type = BitmapTypeStandardAlpha;
	channels = 1;

	CFileHandler file(filename);
	if (!file.FileExists()) {
		return false;
	}

	unsigned char* buffer = new unsigned char[file.FileSize() + 1];
	file.Read(buffer, file.FileSize());

	boost::mutex::scoped_lock lck(devilMutex);
	ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
	ilEnable(IL_ORIGIN_SET);

	ILuint ImageName = 0;
	ilGenImages(1, &ImageName);
	ilBindImage(ImageName);

	const bool success = !!ilLoadL(IL_TYPE_UNKNOWN, buffer, file.FileSize());
	ilDisable(IL_ORIGIN_SET);
	delete[] buffer;

	if (success == false) {
		return false;
	}

	ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE);
	xsize = ilGetInteger(IL_IMAGE_WIDTH);
	ysize = ilGetInteger(IL_IMAGE_HEIGHT);

	delete[] mem;
	mem = NULL; // to prevent a dead-pointer in case of an out-of-memory exception on the next line
	mem = new unsigned char[xsize * ysize];
	memcpy(mem, ilGetData(), xsize * ysize);

	ilDeleteImages(1, &ImageName);

	return true;
}
Exemplo n.º 13
0
bool JTexture::loadPixelsFromFile(std::string path)
{
	//deallocate previous texture data
	freeTexture();

	bool pixelsLoaded = false;

	ILuint imgID = 0;
	ilGenImages(1, &imgID);
	ilBindImage(imgID);

	ILboolean success = ilLoadImage(path.c_str());

	if(success == IL_TRUE)
	{
		success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
		
		if(success == IL_TRUE)
		{
			GLuint imgWidth = (GLuint)ilGetInteger(IL_IMAGE_WIDTH);
			GLuint imgHeight = (GLuint)ilGetInteger(IL_IMAGE_HEIGHT);
			
			GLuint size = imgWidth * imgHeight;
			mPixels = new GLuint[size];

			mTextureWidth = imgWidth;
			mTextureHeight = imgHeight;

			memcpy(mPixels, ilGetData(), size * 4);
			pixelsLoaded = true;
		}

		ilDeleteImages(1, &imgID);
	}

	if(!pixelsLoaded)
	{
		printf("Unable to load %s\n", path.c_str());
	}

	return pixelsLoaded;
}
Exemplo n.º 14
0
//! Sets Param equal to the current value of the Mode
void ILAPIENTRY ilGetBooleanv(ILenum Mode, ILboolean *Param)
{
    if (Param == NULL) {
        ilSetError(IL_INVALID_PARAM);
        return;
    }

    *Param = ilGetInteger(Mode);

    return;
}
Exemplo n.º 15
0
void Image::loadImage(const char *filename)
{
    ILboolean ok;
    ilGenImages(1, &image);
    CHECK_IL_ERROR("ilGenImages")

    ilBindImage(image);
    CHECK_IL_ERROR("ilBindImage")

    ok = ilLoadImage(filename);
    if (!ok) {
        CHECK_IL_ERROR("ilLoadImage")
    }

    size.x = ilGetInteger(IL_IMAGE_WIDTH);
    size.y = ilGetInteger(IL_IMAGE_HEIGHT);

    buffer = new char[(int)size.x * (int)size.y * 4];
    ilCopyPixels(0, 0, 0, size.x, size.y, 1, IL_RGBA, IL_UNSIGNED_BYTE, buffer);
}
Exemplo n.º 16
0
// First part of a 2 stage file loader...
bit DevilLoadStart(cstrconst filename,nat32 & handle,nat32 & outWidth,nat32 & outHeight)
{
 ilGenImages(1,(unsigned int *)&handle);
 ilBindImage(handle);
 ilEnable(0x0600);
 ilOriginFunc(0x0601);
 
 ilLoadImage(filename);
 if (ilGetError())
 {
  ilDeleteImages(1,(unsigned int *)&handle);
  return false;
 } 
 else
 {
  outWidth = ilGetInteger(0x0DE4);
  outHeight = ilGetInteger(0x0DE5);
  return true;
 }
}
Exemplo n.º 17
0
    template <typename T> void ilToOgreInternal(uint8 *tar, PixelFormat ogrefmt, 
        T r, T g, T b, T a)
    {
        const int ilfmt = ilGetInteger( IL_IMAGE_FORMAT );
        T *src = (T*)ilGetData();
        T *srcend = (T*)((uint8*)ilGetData() + ilGetInteger( IL_IMAGE_SIZE_OF_DATA ));
        const size_t elemSize = PixelUtil::getNumElemBytes(ogrefmt);
        while(src < srcend) {
            switch(ilfmt) {
			case IL_RGB:
				r = src[0];	g = src[1];	b = src[2];
				src += 3;
				break;
			case IL_BGR:
				b = src[0];	g = src[1];	r = src[2];
				src += 3;
				break;
			case IL_LUMINANCE:
				r = src[0];	g = src[0];	b = src[0];
				src += 1;
				break;
			case IL_LUMINANCE_ALPHA:
				r = src[0];	g = src[0];	b = src[0];	a = src[1];
				src += 2;
				break;
			case IL_RGBA:
				r = src[0];	g = src[1];	b = src[2];	a = src[3];
				src += 4;
				break;
			case IL_BGRA:
				b = src[0];	g = src[1];	r = src[2];	a = src[3];
				src += 4;
				break;
			default:
				return;
            }
            packI(r, g, b, a, ogrefmt, tar);
            tar += elemSize;
        }

    }   
Exemplo n.º 18
0
bool TextureControl::SaveToTexSet(string imageName, string basepath, string setName, string channel, string subset) {
	TargetTextureSet* tts = TexSetFromName(setName);
	if(!tts) 
		return false;

	if(namedImages.find(imageName) == namedImages.end())				
		return false;
	
	ilBindImage(namedImages[imageName]);

	string outputPath = FilePathForTexSet(setName, channel, subset);
	outputPath = basepath + outputPath;

	ilEnable(IL_FILE_OVERWRITE);
	bool hasAlpha = tts->SubSetHasAlpha(channel,subset);
	string format = tts->SubSetFormat(channel, subset);

	if(format == "DXT1") {
		ilEnable(IL_SQUISH_COMPRESS);
		ilSetInteger(IL_DXTC_FORMAT, IL_DXT1);
	} else if(format == "DXT5") {
		ilEnable(IL_SQUISH_COMPRESS);
		ilSetInteger(IL_DXTC_FORMAT, IL_DXT5);
	} else if(format == "RGB") {
		ilSetInteger(IL_DXTC_FORMAT, IL_DXT_NO_COMP);
		if(ilGetInteger(IL_IMAGE_FORMAT) == IL_RGBA)
			ilConvertImage(IL_RGB,ilGetInteger(IL_IMAGE_TYPE));
	} else if(format == "RGBA") {
		ilSetInteger(IL_DXTC_FORMAT, IL_DXT_NO_COMP);

	}

	size_t pos= outputPath.rfind("\\");
	if(pos != string::npos) {
		SHCreateDirectoryEx(NULL,outputPath.substr(0,pos).c_str(),NULL);
	}	
	if(!ilSaveImage((const ILstring)outputPath.c_str())) {
		return false;
	}
	return true;
}
Exemplo n.º 19
0
ILuint LoadImage(const std::string& filename, Image *image)
{
    ILuint newImageID;

    ilGenImages(1, &newImageID);
    ilBindImage(newImageID);

    if (!ilLoadImage(strdup(filename.c_str())))
       return 0;

    ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

    image->id      = newImageID;
    image->rawData = ilGetData();
    image->width   = ilGetInteger(IL_IMAGE_WIDTH);
    image->height  = ilGetInteger(IL_IMAGE_HEIGHT);
    image->depth   = ilGetInteger(IL_IMAGE_DEPTH);
    image->format   = ilGetInteger(IL_IMAGE_FORMAT);

    return 1;
}
Exemplo n.º 20
0
const Texture* ReadPNG(const std::string& name)
{
    Texture *texture;
    ILuint img;
    ilGenImages(1, &img);
    ilBindImage(img);
    if(!ilLoadImage(name.c_str())){
	    ilDeleteImages(1, &img);
        return nullptr;
    }
    iluFlipImage();

    texture = new Texture;
    texture->width = ilGetInteger(IL_IMAGE_WIDTH);
    texture->height = ilGetInteger(IL_IMAGE_HEIGHT);
    texture->texels.resize(texture->width * texture->height);
    ilCopyPixels(0, 0, 0, texture->width, texture->height, 1, IL_RGBA, IL_UNSIGNED_BYTE, &texture->texels[0]);
    ilDeleteImages(1, &img);

    return texture;
}
Exemplo n.º 21
0
TextureLoader::TextureLoader(const char* filename)
{
	data = NULL;

	textureID = ilGenImage();
	ilBindImage(textureID);

	ilLoadImage(filename);


	isSRGB = false;

	width = ilGetInteger(IL_IMAGE_WIDTH);
	height = ilGetInteger(IL_IMAGE_HEIGHT);
	totalSize = ilGetInteger(IL_IMAGE_SIZE_OF_DATA);
	bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
	format = ilGetInteger(IL_IMAGE_FORMAT);
	type = ilGetInteger(IL_IMAGE_TYPE);

	if (format == IL_RGBA || format == IL_BGRA || format == IL_LUMINANCE_ALPHA)
		hasAlpha = true;
	else hasAlpha = false;

	LoadData(format, type);
}
Exemplo n.º 22
0
bool ImageLoader::loadImage(const char* image)
{
	freeImageData();

	// Create the final path...
	char filePath[260];
	sprintf(filePath, "%s%s", m_path, image);

	// Now let's switch over to using devIL...
	ILuint handle;

	// In the next section, we load one image
	ilGenImages(1, &handle);
	ilBindImage(handle);
	const ILboolean loaded = ilLoadImage(filePath);

	if (loaded == IL_FALSE)
	{
		ILenum error = ilGetError();
		return false; // Error encountered during loading
	}

	// Let’s spy on it a little bit
	m_width  = (u32)ilGetInteger(IL_IMAGE_WIDTH);  // getting image width
	m_height = (u32)ilGetInteger(IL_IMAGE_HEIGHT); // and height

	// Make sure our buffer is big enough.
	assert( m_width <= MAX_IMAGE_WIDTH && m_height <= MAX_IMAGE_WIDTH );

	// Finally get the image data
	ilCopyPixels(0, 0, 0, m_width, m_height, 1, IL_RGBA, IL_UNSIGNED_BYTE, m_imageData_Work);

	// (Now flip the image for OpenGL... does not seem to be necessary so just copy for now)
	memcpy( m_imageData, m_imageData_Work, 4*m_width*m_height );

	// Finally, clean the mess!
	ilDeleteImages(1, &handle);

	return true;
}
Exemplo n.º 23
0
unsigned char* il_load( const char* filename, unsigned int* size, int* width, int* height )
{
  if ( filename == NULL )
    return NULL;
  
  ILboolean success;
  unsigned int imageID;
 
  // generate an image name
  ilGenImages(1, &imageID); 
  // bind it
  ilBindImage(imageID); 
  // match image origin to OpenGL’s
  ilEnable(IL_ORIGIN_SET);
  ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
  // load  the image
  success = ilLoadImage(filename);
  // check to see if everything went OK
  if (!success) 
  {
    ilDeleteImages(1, &imageID); 
 
    return NULL;
  }

  /* Convert image to RGBA with unsigned byte data type */
  ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

  unsigned char* data = NULL;
  *width    = ilGetInteger(IL_IMAGE_WIDTH);
  *height   = ilGetInteger(IL_IMAGE_HEIGHT);  
  *size     = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL)*(*width)*(*height);
  data      = malloc( *size );
  
  memcpy( data, ilGetData(), *size );
  
  ilDeleteImages(1, &imageID); 
  
  return data;  
}
Exemplo n.º 24
0
Texture::Texture(std::string pszFileName)
{
    ILuint  _iImageId   = 0;
    size_t  _iImageSize = 0;

    _iImageId = ilGenImage();
    ilBindImage(_iImageId);
    WriteCommandLine("\nLoading Image %s", pszFileName.c_str());
    VERIFY( ilLoadImage(pszFileName.c_str() ) == IL_TRUE, "Texture: Unable to load file!");

    switch(ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL))
    {
    case 3:
        ASSERT(  ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE) == IL_TRUE  , "Texture: Unable to load file!");
        m_Type = IL_RGB;
        break;

    case 4:
        ASSERT(  ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE) == IL_TRUE  , "Texture: Unable to load file!");
        m_Type = IL_RGBA;
        break;

    default:
        ASSERT(0, "Texture: Unable to load file!");
        break;
    }
    m_iHeight   = ilGetInteger(IL_IMAGE_HEIGHT);
    m_iWidth    = ilGetInteger(IL_IMAGE_WIDTH);

    _iImageSize = ilGetInteger(IL_IMAGE_SIZE_OF_DATA);

    m_pData     =   new unsigned char[_iImageSize];
    m_pfData    =   new float[_iImageSize];
    memcpy(m_pData, ilGetData(), _iImageSize);

    for(size_t i = 0; i < _iImageSize; i++)
        m_pfData[i] = ((float)m_pData[i]) / 255.0f;

    ilDeleteImage(_iImageId);
}
Exemplo n.º 25
0
GLuint TextureUtils::createTexture(const GLchar *path){
   ilInit();
   ILuint image = ilGenImage();

   ilBindImage(image);

   ILboolean loadSuccess = ilLoadImage(path);
   if(!loadSuccess){
      std::cerr<<"Failed to load image: "<<path<<std::endl;
      ilBindImage(NULL);
      ilDeleteImage(image);
      return NULL;
   }

   ILboolean convertSuccess = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
   if(!convertSuccess){
      std::cerr<<"Failed to convert image: "<<path<<std::endl;
      ilBindImage(NULL);
      ilDeleteImage(image);
      return NULL;
   }

   GLuint texture;
   glGenTextures(1, &texture);
   glBindTexture(GL_TEXTURE_2D, texture);

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);


   glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_FORMAT), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), ilGetInteger(IL_IMAGE_TYPE), ilGetData());
   glBindTexture(GL_TEXTURE_2D, NULL);

   ilBindImage(NULL);
   ilDeleteImage(image);

   return texture;
}
Exemplo n.º 26
0
	//------------------------------------------------------------------------------------
	// Loads an image from a file
	//------------------------------------------------------------------------------------
	bool Image::LoadImageFromFile()
	{
		// Generate the main image name to use.
		ilGenImages (1, &m_iImgID);

		// Bind this image name.
		ilBindImage (m_iImgID);
		ilFormatFunc(IL_BGRA);

		// Loads the image specified by File into the ImgId image.
		if (!ilLoadImage (m_pFileName))
		{
			CheckDevilErrors( m_pFileName );
			return false;
		}

		m_iWidth  = ilGetInteger(IL_IMAGE_WIDTH);			// width
		m_iHeight = ilGetInteger(IL_IMAGE_HEIGHT);			// height
		m_ibpp    = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);	// Bits per pixel
		m_iBpp    = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); // bytes per pixel

		if (m_ibpp == 8)
		{
//			ilConvertImage(IL_BGR, IL_UNSIGNED_BYTE);
//			m_ibpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
			m_eFmt = EIF_A;
			m_iBpp = 1;
		}
		else if (m_ibpp==16)
		{
			ilConvertImage(IL_BGR, IL_UNSIGNED_BYTE);
			m_ibpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
			m_eFmt = EIF_BGR;
		}

		CheckDevilErrors( m_pFileName );

		return true;

	} // LoadImageFromFile
Exemplo n.º 27
0
GLuint toGLTexture (const char* path)
{
    // this will be a badass function
    ILuint singleimg;
    ILboolean success;
    GLuint textureID;
    
    ilGenImages(1, &singleimg); /* Generation of one image name */
    ilBindImage(singleimg); /* Binding of image name */
    success = ilLoadImage(path); /* Loading of image "image.jpg" */
    if (success) /* If no error occured: */
    {
        success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); /* Convert every colour component into
                                                             unsigned byte. If your image contains alpha channel you can replace IL_RGB with IL_RGBA */
        if (!success)
        {
            /* Error occured */
            return -1;
        }
        glGenTextures(1, &textureID); /* Texture name generation */
        glBindTexture(GL_TEXTURE_2D, textureID); /* Binding of texture name */
        // Set texture clamping method
        
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //Puts a magnification filter on the texture
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //Puts a minification filter on the texture
        
        glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
                     ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
                     ilGetData()); /* Texture specification */
    }
    else
    {
        /* Error occured */
        return -1;
    }
    ilDeleteImages(1, &singleimg); /* Because we have already copied image data into texture data
                                    we can release memory used by image. */
    
    return textureID;
}
Exemplo n.º 28
0
void tiny_gl::GLMesh::load_specular(tinyobj::material_t & mat, texture_group_t & tex_group)
{
	ILuint ilTexName;
	ilGenImages(1, &ilTexName);
	ilBindImage(ilTexName);

	if (ilLoadImage(mat.specular_texname.c_str()) && ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE))
	{
		unsigned char *data = new unsigned char[ilGetInteger(IL_IMAGE_WIDTH) * ilGetInteger(IL_IMAGE_HEIGHT) * 4];
		ilCopyPixels(0, 0, 0, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 1, IL_RGBA, IL_UNSIGNED_BYTE, data);

		glGenTextures(1, &tex_group.specularid);
		glBindTexture(GL_TEXTURE_2D, tex_group.specularid);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

		delete[] data;

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	ilDeleteImages(1, &ilTexName);
}
Exemplo n.º 29
0
std::shared_ptr<Texture> Texture::loadTexture(const std::string& filename) {
    std::shared_ptr<Texture> texture(new Texture);
    
    ILuint imageName;
    ilGenImages(1, &imageName);
    ilBindImage(imageName);
    if (!ilLoadImage(filename.c_str())) {
		throw r2ExceptionIOM("Failed to load texture image: " + filename);
    }

    GLCheck(glBindTexture(GL_TEXTURE_2D, texture->getId()));

    ILuint imageWidth = ilGetInteger(IL_IMAGE_WIDTH);
    ILuint imageHeight = ilGetInteger(IL_IMAGE_HEIGHT);
    ILuint imageBpp = ilGetInteger(IL_IMAGE_BPP);
    ILubyte* imageData = ilGetData();
    GLenum format = (imageBpp == 3) ? GL_RGB : GL_RGBA;

    if (imageBpp != 3 && imageBpp != 4) {
		std::stringstream ss;
		ss << "Texture image in bad format (bytes per pixel = " << imageBpp << "): " << filename;

		throw r2ExceptionIOM(ss.str());
    }

    GLCheck(glTexImage2D(GL_TEXTURE_2D,
                0,
                format,
                imageWidth,
                imageHeight,
                0,
                format,
                GL_UNSIGNED_BYTE,
                (const GLvoid*)imageData));

    ilBindImage(0);
    ilDeleteImages(1, &imageName);

    return texture;
}
Exemplo n.º 30
0
//Modified slightly from the OpenIL tutorials
ILuint loadTexFile(const char* filename){
	
	ILboolean success; /* ILboolean is type similar to GLboolean and can equal GL_FALSE (0) or GL_TRUE (1)
    it can have different value (because it's just typedef of unsigned char), but this sould be
    avoided.
    Variable success will be used to determine if some function returned success or failure. */


	/* Before calling ilInit() version should be checked. */
	  if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION)
	  {
		/* wrong DevIL version */
		printf("Wrong IL version");
		exit(1);
	  }
 
	  
	  success = ilLoadImage((LPTSTR)filename); /* Loading of image from file */
	if (success){ /* If no error occured: */
		//We need to figure out whether we have an alpha channel or not
		  if(ilGetInteger(IL_IMAGE_BPP) == 3){
			success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE); /* Convert every color component into
		  unsigned byte. If your image contains alpha channel you can replace IL_RGB with IL_RGBA */
		  }else if(ilGetInteger(IL_IMAGE_BPP) == 4){
			  success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
		  }else{
			  success = false;
		  }
		if (!success){
		  /* Error occured */
		 printf("failed conversion to unsigned byte");
		 exit(1);
		}
	}else{
		/* Error occured */
	   printf("Failed to load image ");
	   printf(filename);
		exit(1);
	}
}