# 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 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 VARIABLEGLuint 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 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 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 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 on other sites
You need to include il.h I guess.
You aren't using ilu and ilut, so why do you include those?

##### 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

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627671
• Total Posts
2978550
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 11
• 10
• 10
• 12
• 22