Sign in to follow this  
giugio

OpenGL create texture from jpg

Recommended Posts

Hy. Is possible in opengl to create a texture from a .jpg? There are some library(possibly opensource) that import the texture from a jpg? Then when i have the raw image how i can create texture? thanks

Share this post


Link to post
Share on other sites
Of course!

All you need to do is read in the data fro mthe image file and pass it over to opengl. I use the DevIL library. http://openil.sourceforge.net/ It supports a variety of image formats. You then use glTexImage2D() and as the last parameter you pass it ilGetData(). Heres a quick example of how to incorporate DevIL and opengl together




//GLOBAL VARIABLE
GLuint textureID;


//Load Image



glGenTextures( 1, &textureID ); //create 1 tex

filename = fname;
ILuint imageID = -1;
ILboolean imageLoaded;

GLuint width;
GLuint height;
GLuint bpp;
GLuint format;

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


imageLoaded = ilLoadImage( filename );

if ( !imageLoaded )
{
ilDeleteImages( 1, &imageID );
return false;
}
else
{
imageLoaded = ilConvertImage( IL_RGBA, IL_UNSIGNED_BYTE );

if ( !imageLoaded )
{
ilDeleteImages( 1, &imageID );
return false;
}

width = ilGetInteger( IL_IMAGE_WIDTH );
height = ilGetInteger( IL_IMAGE_HEIGHT );
bpp = ilGetInteger( IL_IMAGE_BPP );
format = ilGetInteger( IL_IMAGE_FORMAT );

IMGWidth = (int)width;
IMGHeight = (int)height;


glBindTexture( GL_TEXTURE_2D, textureID );

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, bpp, width, height, 0, format, GL_UNSIGNED_BYTE, ilGetData() );


ilDeleteImages(1, &imageID); //YOU MUST DELETE THE IMAGE WHEN YOUR DONE LOADING IT, IF YOU DONT YOU WILL HAVE MEMORY LEAKS
return true;




When you wan't to use the texture just call glBindTexture( GL_TEXTURE_2D, textureID); and make sure textures are enabled with glEnable(GL_TEXTURE_2D);

Share this post


Link to post
Share on other sites
thanks, the library is ok, but i don't understand how compile it, i use vs2008 c++, but there are a lot of problem with the vc++ solution downloaded by this url:http://openil.sourceforge.net/download.php, if i must download the sdk or the dll version?
there is a guide or tutorial for compile it?
thanks.

Share this post


Link to post
Share on other sites
I got the dll version and I think the dll files were just included so I didn't need to compile anything. If you have to compile stuff just open the project file and press compile... When you make an app you must link to the DevIL.lib and the ILU.lib files. (well ILU.lib only if you use the ilu functions) To let your compiler know about the library files. For me, since I have visual studio 2003 I go to tools then options then projects then VC++ Directories. Once there I did two things. On the drop down menu I scrolled down to where it said Library files, I then added the directory which I installed DevIL to. so it looks like... C:\DevIL\lib and do the same thing except for include files in that drop down menu. I then added C:\DevIL\include

If you don't have the same compiler as I do, you should find some tutorials on how to use your compiler.

Share this post


Link to post
Share on other sites
thanks , but i do that that you say and i can't compile successiful .
This is my code, i create a lib file with:

#include "Wm4GraphicsPCH.h"
#include "ImageEx.h"
#include <string>
#include "il/Ilut.h"
#include "il/Ilu.h"
ImageEx::ImageEx(void)
{
}

ImageEx::~ImageEx(void)
{
}

ImageEx* ImageEx::Load(const std::string& rkImageName, bool bAbsolutePath)
{
ilInit();
iluInit();
ilutInit();
ILuint imageID = -1;
ILboolean imageLoaded;

GLuint width;
GLuint height;
GLuint bpp;
GLuint format;

//ilGenImages( 1, &imageID );


imageLoaded = ilLoadImage( rkImageName.c_str() );

if ( !imageLoaded )
{
ilDeleteImages( 1, &imageID );
return false;
}
else
{
imageLoaded = ilConvertImage( IL_RGBA, IL_UNSIGNED_BYTE );

if ( !imageLoaded )
{
ilDeleteImages( 1, &imageID );
return false;
}

width = ilGetInteger( IL_IMAGE_WIDTH );
height = ilGetInteger( IL_IMAGE_HEIGHT );
bpp = ilGetInteger( IL_IMAGE_BPP );
format = ilGetInteger( IL_IMAGE_FORMAT );

int IMGWidth, IMGHeight;

IMGWidth = (int)width;
IMGHeight = (int)height;
m_aucData = (unsigned char*) ilGetData();
}
return this;
}


and i import Devil.lib ilu.lib and ilut.lib


then i get the .lib file generated and i link in in another project:


// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ImageEx.h"

int _tmain(int argc, _TCHAR* argv[])
{
/*TriMesh* t = new TriMesh();*/
ImageEx* im = new ImageEx();
return 0;
}

these are the errors:

1>Wm4Graphics90d.lib(ImageEx.obj) : error LNK2019: unresolved external symbol __imp__ilGetData@0 referenced in function "public: class ImageEx * __thiscall ImageEx::Load(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?Load@ImageEx@@QAEPAV1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z)
1>Wm4Graphics90d.lib(ImageEx.obj) : error LNK2019: unresolved external symbol __imp__ilGetInteger@4 referenced in function "public: class ImageEx * __thiscall ImageEx::Load(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?Load@ImageEx@@QAEPAV1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z)
1>Wm4Graphics90d.lib(ImageEx.obj) : error LNK2019: unresolved external symbol __imp__ilConvertImage@8 referenced in function "public: class ImageEx * __thiscall ImageEx::Load(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?Load@ImageEx@@QAEPAV1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z)
1>Wm4Graphics90d.lib(ImageEx.obj) : error LNK2019: unresolved external symbol __imp__ilDeleteImages@8 referenced in function "public: class ImageEx * __thiscall ImageEx::Load(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?Load@ImageEx@@QAEPAV1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z)
1>Wm4Graphics90d.lib(ImageEx.obj) : error LNK2019: unresolved external symbol __imp__ilLoadImage@4 referenced in function "public: class ImageEx * __thiscall ImageEx::Load(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?Load@ImageEx@@QAEPAV1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z)
1>Wm4Graphics90d.lib(ImageEx.obj) : error LNK2019: unresolved external symbol __imp__ilutInit@0 referenced in function "public: class ImageEx * __thiscall ImageEx::Load(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?Load@ImageEx@@QAEPAV1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z)
1>Wm4Graphics90d.lib(ImageEx.obj) : error LNK2019: unresolved external symbol __imp__iluInit@0 referenced in function "public: class ImageEx * __thiscall ImageEx::Load(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?Load@ImageEx@@QAEPAV1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z)
1>Wm4Graphics90d.lib(ImageEx.obj) : error LNK2019: unresolved external symbol __imp__ilInit@0 referenced in function "public: class ImageEx * __thiscall ImageEx::Load(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?Load@ImageEx@@QAEPAV1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z)
1>C:\programmazione\wild magic 4\GeometricTools\WildMagic4\Builds\Builds\Debug\test.exe : fatal error LNK1120: 8 unresolved externals
1>Build log was saved at "file://c:\programmazione\wild magic 4\GeometricTools\WildMagic4\Builds\Builds\test\Debug\BuildLog.htm"
1>test - 9 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

i use vs 2008.
thanks

Share this post


Link to post
Share on other sites
thanks.
But i'm change to:

#include "stdafx.h"
#include "IL/Il.h"
#include "imageex.h"
using namespace Wm4;

int _tmain(int argc, _TCHAR* argv[])
{
ImageEx* im = new ImageEx();
return 0;
}



and

#include "Wm4GraphicsPCH.h"
#include "ImageEx.h"
#include <string>
#include "il/Il.h"

ImageEx::ImageEx(void)
{
}

ImageEx::~ImageEx(void)
{
}

ImageEx* ImageEx::Load(const std::string& rkImageName, bool bAbsolutePath)
{

ilInit();
iluInit();
ilutInit();
ILuint imageID = -1;
ILboolean imageLoaded;

GLuint width;
GLuint height;
GLuint bpp;
GLuint format;

ilGenImages( 1, &imageID );


imageLoaded = ilLoadImage( rkImageName.c_str() );

if ( !imageLoaded )
{
ilDeleteImages( 1, &imageID );
return false;
}
else
{
imageLoaded = ilConvertImage( IL_RGBA, IL_UNSIGNED_BYTE );

if ( !imageLoaded )
{
ilDeleteImages( 1, &imageID );
return false;
}

width = ilGetInteger( IL_IMAGE_WIDTH );
height = ilGetInteger( IL_IMAGE_HEIGHT );
bpp = ilGetInteger( IL_IMAGE_BPP );
format = ilGetInteger( IL_IMAGE_FORMAT );

int IMGWidth, IMGHeight;

IMGWidth = (int)width;
IMGHeight = (int)height;
m_aucData = (unsigned char*) ilGetData();
}
return this;
}


It compile but hen i build i get the same error.
I'm try to put the .dll files under debug/ but dosn't change anything.
How?
thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this