What are u all using to load textures for OpenGL?

I have been trying to load a texture for a while now with no luck. All the tutorials I find use glaux , are for .raw files (I prefer .bmp, .png, or .jpg), or the majority just skip over that part and talk about the actual texturing code…

What do you guys use to load textures? I am very curious, openGL has been around forever, I thought texturing would be absolutely trivial to get working, please help :slight_smile:

Try DevIL

I looked at DevIL, but I cannot figure out how to install it on VS2008…

I am trying to get SOIL, simple opengl image loader to work, but I get some linking error, probably because I can’t find the static library file or I am linking it in the wrong spot. The file in SOIL’s lib folder is a .a file, wth is a .a file…

Any help on DevIL or SOIL, or anything that can load a image would be greatly appreciated :slight_smile:

http://freeimage.sourceforge.net/index.html

I found SOIL the best, but all of them works correctly.
.a is the lib file for gcc, mingw.
To make soil work, you have to compile and copy the LIB file, and copy the SOIL.h into your compiler.
Go to the SOIL/projects/VC? folder, and open the project to do it.
Then you find everything to load an image at the official website:
http://lonesock.net/soil.html

I use Codeblocks btw, but it should work with VS2008 too.

The projects that come with SOIL won’t compile for me :frowning:

I am trying to get FreeImage to work, I found an example project on codesampler.com that compiles and works, but I am having trouble getting it to work in my own project.

UPDATE
I am still having trouble. The example of FreeImage and Opengl I got from codesampler.com does not use GLUT, and I am getting a lot of errors. If anyone has a good GLUT + OpenGL texture example that will compile in Visual Studio let me know :slight_smile:

i wrote my own TGA loader (as a lesson for nehe.gamedev.net)
feel free to use it if you like
nehe_tga.rar

thanks for the offer, but I really want to load bmps, jpegs, or pngs.

I am finding this process extremely painful. There are so many examples and libraries to get this simple task done, but I cannot get any of them to work with my existing project, and sometimes not at all. How do you all cope with all the differences in IDE, libraries, C++ random crap, and more?

Loading textures is far from simple if you want to support some common file formats. DevIL is actually pretty easy to use with Visual Studio but i haven’t used it in a while. I wrote my own importers for TGAs and DDS files. TGAs are really easy to handle, that was the reason for me to use them. DDS files are the most interesting files, because they can store all the stuff like DXT compressed images, 1D, 2D, 3D, Cubemap textures, with mipmaps or without. It was a pain to write a loader for that format but it was really worth the effort. Textures load like 5x faster than from TGAs now and i have full control over the data.

However it seems that you have to get a bit more used to how to get other peoples software working in your projects. I would pick one library and not hop to the next only because it doesn’t work right away. C++ is difficult in that regard, but that’s a skill that you need to learn.

Jan.

I am happy to hear that this isn’t a trivial process. I am all for any library that has some good documentation on how to use it. Can anybody point me in a good direction? I am dying to mess around with cube maps, multi-texturing, and “glyph-bombs” as GLSL book puts it (the orange one).

I am going to try and stick to using SOIL, simple OpenGL Image Library.

It appears to do everything I need without extra stuff complicating the process. I have the SOIL.h file setup correctly, I renamed the libSOIL.a to libSOIL.lib, went to project->properties->linker->input and added it in with the other .lib files. I then added in:

/* load an image file directly as a new OpenGL texture */
GLuint tex_2d = SOIL_load_OGL_texture
(
“img_test.png”,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);

I get some error C2099: initializer is not a constant c:\Users\Danny\Desktop\ogl_texture2\ogl2brick.c 95, which I don’t understand. Line 95 is the “);” from the code above. Does this mean I didn’t install SOIL properly? I have the img_test.png in the right folder…

You can’t use an .a file by renaming it to .lib. .a files are usually compiled for Linux and even if they are compiled for Windows, they are created by GCC which is incompatible with Visual Studio.

You need to find a .lib file to link to. Either SOIL provides that somewhere on their website or you need to get the sources and compile it yourself. It is usually not necessary to “install” a library, just put files in the proper directory.

For example for DevIL, you download it, there are a few .h files, a .lib file and possibly .exp and .dll files. You put the headers somewhere in your include-path. The others you put into your library-path, where the linker will find it. Then you compile your stuff, and tell the linker that it should link to “devil.lib” or whatever it is called. That should do the trick. If you run your app, it might be necessary to copy the dll to your apps directory.

About your error: Is tex_2d by chance a global variable? You can’t initialize global vars with a function-call. You need to call that function at run-time.

Jan.

I have that error above in a function call now, but I get the same error again.

Anyways, I cannot find a windows static library file in SOIL’s download. There is the .a file which I renamed, but that might not be good. I cannot compile my own, their VS9 project doesn’t run correctly. Can anybody try to get SOIL to run on a windows machine with visual studio?

I let Windows decode JPG/stuff for me (via OleLoadPicture), and the several lines of code necessary to load .DDS (compressed or not).

I am not trying to bite your head off, but that doesn’t really help me, I googled OleLoadPicture and didn’t get anything substantial.

Compile SOIL, and it will work :slight_smile: It took 5 minutes for me to compile, and I am a beginner too.

Glad to hear it isn’t rocket science, Can you send me the compiled SOIL file, did you make one for VS 2008? Did you actually use it at all or did you just compile it?

My email is: meatwadShake357@gmail.com

I used Codeblocks, so I can’t send it. But it’s easy to compile. Go to: <folder of SOIL>/projects/VC9
Then hit the compile button. Copy the soil.lib to the right place. Easy :slight_smile:

Can you try running something with this code and the SOIL library?

GLuint GetTexture(std::string Filename)
{
GLuint tex_ID;

tex_ID = SOIL_load_OGL_texture(
			Filename.c_str(),
			SOIL_LOAD_AUTO,
			SOIL_CREATE_NEW_ID,
			SOIL_FLAG_POWER_OF_TWO
			| SOIL_FLAG_MIPMAPS
			| SOIL_FLAG_MULTIPLY_ALPHA
			| SOIL_FLAG_COMPRESS_TO_DXT
			| SOIL_FLAG_DDS_LOAD_DIRECT
			| SOIL_FLAG_INVERT_Y
			);

	if( tex_ID &gt; 0 )
	{
		glEnable( GL_TEXTURE_2D );
		glBindTexture( GL_TEXTURE_2D, tex_ID );
		
		return tex_ID;
	}
	else
		return 0;

}

When I copy and paste it in, I get some syntax error: missing ‘)’ before ‘:’ which I cannot figure out…


#include <windows.h>
#include "ILX.h"
#include <olectl.h>
#include <gl\gl.h>
#include <gl\glext.h>



//====================[[ JPG ]]================================================================[[
static ILTEX _ILXLoadTextureJPG(U8* data,int DataSize){

	//----[ copy data ]-------------------------------------[
	// yeah, I know it's bloated. 
	// But jpegs sent here would be small anyway
	HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE,DataSize);
	memcpy(GlobalLock(hGlobal),data,DataSize);
	GlobalUnlock(hGlobal);
	//------------------------------------------------------/

	LPSTREAM pstm;
	CreateStreamOnHGlobal(hGlobal,1,&pstm);

	IPicture* PictuBaka;
	HBITMAP hBmp;
	if(OleLoadPicture( pstm, DataSize,0,IID_IPicture,(LPVOID*)&PictuBaka)){
		//trace("Cannot load image %s",fName);
		pstm->Release();
		GlobalFree(hGlobal);
		return 0;
	}
	if(PictuBaka->get_Handle((OLE_HANDLE*)&hBmp)){
		PictuBaka->Release();
		pstm->Release();
		GlobalFree(hGlobal);
		return 0;
	}

	BITMAP bi;
	GetObject(hBmp,sizeof(BITMAP),&bi);

	int wid = bi.bmWidth;
	int hei = bi.bmHeight;

	char* pixels = (char*)xmalloc(wid*hei*4);


	BITMAPINFOHEADER hdr1;
	memset(&hdr1,0,sizeof(hdr1));

	hdr1.biSize=sizeof(BITMAPINFOHEADER);
	hdr1.biWidth=wid;
	hdr1.biHeight=hei;
	hdr1.biPlanes=1;
	hdr1.biBitCount=32;
	hdr1.biCompression=BI_RGB;
	hdr1.biSizeImage=wid*hei*4;

	HDC dc = CreateCompatibleDC(GetDC(0));
	GetDIBits(dc,hBmp,0,hei,pixels,(LPBITMAPINFO)&hdr1,DIB_RGB_COLORS);

	DeleteObject(hBmp);
	DeleteObject(dc);

	PictuBaka->Release();
	pstm->Release();
	GlobalFree(hGlobal);
	for(int j=wid*hei*4;j-=4;)pixels[j+3]= -1; // set alpha=1.0f;

	ILTEX tex = ilCreateTexture2D(wid,hei,ILTEXFMT_BGRA8);
	ilUploadTex2D_LOD(tex,0,pixels);
	tex->GenerateMips();
	xfree(pixels);
	return tex;
}
//=============================================================================================//

//====================[[ DDS ]]================================================================[[
static ILTEX _ILXLoadTextureDDS(U8* data,int DataSize){
	int* ddsd = (int*)(data+4);

	int wid,hei,numMipMaps,nBlockSize,fourcc;
	const U8* pixels;
	IL_TEX_FORMAT format;


	wid = ddsd[3];
	hei = ddsd[2];
	numMipMaps = ddsd[6];
	fourcc = ddsd[20];
	pixels = data + (4 + 124);

	switch(fourcc){
		case '1TXD':
			// DXT1's compression ratio is 8:1
			format = ILTEXFMT_DXT1;
			nBlockSize = 8;
			break;
		case '3TXD':
			// DXT3's compression ratio is 4:1
			format = ILTEXFMT_DXT3;
			nBlockSize = 16;
			break;
		case '5TXD':
			// DXT5's compression ratio is 4:1
			format = ILTEXFMT_DXT5;
			nBlockSize = 16;
			break;
		default:
			return null;
	}

	ILTEX tex = ilCreateTexture2D(wid,hei,format,numMipMaps!=1);
	if(!tex)return null;


	int nWid=wid,nHei=hei;

	for(int i=0;i<numMipMaps;i++){
		if(nWid==0)nWid=1;
		if(nHei==0)nHei=1;
		int nSize = ((nWid+3)/4) * ((nHei+3)/4) * nBlockSize;
		ilUploadTex2D_LOD(tex,i,(void*)pixels);
		pixels += nSize;
		nWid/=2;
		nHei/=2;
	}

	return tex;	
}
//=============================================================================================//



ILTEX ILXLoadTexture(const char* FNameOrData,int DataSize){
	bool WasFile=false;
	U8* data = (U8*)FNameOrData;
	U8* dataalloc=null;
	if(!DataSize){
		WasFile=true;
		data = (U8*)uffetch(FNameOrData,&DataSize);
		if(!data)return null;
		dataalloc = data;
	}
	ILTEX res = null;
	if(data[0]=='D' && data[1]=='D' && data[2]=='S'){
		res = _ILXLoadTextureDDS(data,DataSize);
	}else{
		res = _ILXLoadTextureJPG(data,DataSize);
	}
	xfree(dataalloc);
	return res;
}