what would be a good way to load images to use for texture mapping in opengl?

I have looked at tutorials such as nehe lesson 6 and some other resources but they all use something old and depreciated that I cant compile.

Recommended Answers

All 22 Replies

Good to see you are still keeping up with this. I can give you a tga loader
if you want. You can load a tga image. I also have some loadBMP functions
that loads images if you need. But anyways, check this out first. Its a tutorial for loading in bitmap images.

well, I copied and pasted the code first to see if it worked before writing it myself and I didn't get any compiler errors, that's a start. but I don't know if it will work yet.

what are the advantage of using tga files?

and its nice you remember me :)

Bitmaps can't use alpha blending. Bitmaps are a max of 24 bits, 8 per channel (RGB) where targas can be 24 bit RGB, or 32 bit RGBA (A for alpha).

what sodabread said. You do not need to worry about the alpha channels
right now. For now, your goal is to load in a image and texture it.

Did you try out their .exe to see the result.

i cant compile or run their code straight from downloading it. i get the error:

This application has failed to start because glut32.dll was not found. Re-installing the application may fix this problem.

but i did include "texture.h" in my own project and followed the instructions and it loads without errors but the image is very distorted

I remember some of the sides in their cube was purposely distorted,
while other sides were not. Did first you just unzip it and run the .exe ?

The error means that you do not have glut.h ? Do you ?

When I get home from work, I can post some snippets of my graphics code, 2D and 3D if you want to take a look at it.

thank you that would be great:icon_cheesygrin:.

and no, I don't have glut.h. I wouldn't use it (I make my windows with the win32 api) but its a pain when I'm trying to see how other peoples code works

thank you that would be great:icon_cheesygrin:.

and no, I don't have glut.h. I wouldn't use it (I make my windows with the win32 api) but its a pain when I'm trying to see how other peoples code works

You could still download glut just to see other programs, but you
don't have to use it.

yeah I know but I don't know how to download it???

This explains it nicely. Follow the video for your
system. It gives on how to install glut with opengl.

that's for Microsoft visual studio though, i use code blocks. I copied glut.h to include/GL/' but in lib, where it tells me to put glut32.dll, glut32.lib and glut.def the folder currently only contains .a and .o files, should I Put them there anyway?

Here's my bmp loader from an older project. Some stuff in here is using C conventions, and some may be deprecated. I'm not sure. I'm still using VS2003 =) I'm including windows.h, gl.h, and glu.h on mostly everything in this project.

void AETextureManager::LoadBMP(const char *filePath)
{
	AETexture *tempTexture = new AETexture;
	tempTexture->SetTexID(curTexID);

	glGenTextures(1, tempTexture->GetTexIDpt());

	tempTexture->SetFilePath(filePath);

	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, tempTexture->GetTexID());

	BYTE *bytes = bmpReadBits(tempTexture->GetFilePath(), tempTexture->GetWidthpt(), tempTexture->GetHeightpt());

	if(bytes == NULL)
	{
		MessageBox(NULL, "Broke in \"LoadBMP\"", "Error!", MB_OK);
		return;
	}

	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, tempTexture->GetWidth(), tempTexture->GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, bytes);
	delete [] bytes;

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

	vTextures.push_back(tempTexture);
}

BYTE *AETextureManager::bmpReadBits(const char *filePath, AEuint *iWidth, AEuint *iHeight)
{
	//TODO: Finish up the LoadBMP function, then rock this out and get the dang game started.
	HANDLE handle;
	BITMAPINFO *bmpInfo = NULL;
	AEulong infoSize = 0;
	AEulong bitSize = 0;
	BYTE *bits = NULL;

	handle = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);

	if(handle == INVALID_HANDLE_VALUE)
		return NULL;

	BITMAPFILEHEADER bmpHeader;
	DWORD bytes;
	
	ReadFile(handle, &bmpHeader, sizeof(BITMAPFILEHEADER), &bytes, NULL);

	if(bytes != sizeof(BITMAPFILEHEADER))
		return NULL;

	if(bmpHeader.bfType != 'MB')
		return NULL;

	infoSize = bmpHeader.bfOffBits - sizeof(BITMAPFILEHEADER);
	bmpInfo = (BITMAPINFO *)new BYTE[infoSize];
	
	ReadFile(handle, bmpInfo, infoSize, &bytes, NULL);

	if(bytes != infoSize)
	{
		delete [] bmpInfo;
		CloseHandle(handle);
		return NULL;
	}

	*iWidth = bmpInfo->bmiHeader.biWidth;
	*iHeight = bmpInfo->bmiHeader.biHeight;
	bitSize = bmpInfo->bmiHeader.biSizeImage;

	if(bmpInfo->bmiHeader.biBitCount != 24)
	{
		delete [] bmpInfo;
		return NULL;
	}

	if(bitSize == 0)
		bitSize = (*iWidth * bmpInfo->bmiHeader.biBitCount + 7) / 8 * abs(*iHeight);

	delete [] bmpInfo;
	bits = new BYTE[bitSize];

	if(!ReadFile(handle, bits, bitSize, &bytes, NULL) || bytes != (sizeof(BYTE) * bitSize))
		bits = NULL;

	CloseHandle(handle);

	return bits;
}

thank you :) i shall get to work and report back when (if) it works. and i wont copy + paste, i will only copy the method.

I have made my own image load function that reads the file extension and loads the image with the appropriate method. but at the moment it can only load bitmaps. and i know now why the image wasn't loading properly, the image was 8 bit but the function was reading it assuming it was 24 bit. how could i discard the pixels that are a certain colour (the colour around the edge)?

I have made my own image load function that reads the file extension and loads the image with the appropriate method. but at the moment it can only load bitmaps. and i know now why the image wasn't loading properly, the image was 8 bit but the function was reading it assuming it was 24 bit. how could i discard the pixels that are a certain colour (the colour around the edge)?

You'll need to do some research on "color keys". I've never actually implemented it myself as I've always used .tga files for my OpenGL projects and DirectX has built in color key functionality.

I have made my own image load function that reads the file extension and loads the image with the appropriate method. but at the moment it can only load bitmaps. and i know now why the image wasn't loading properly, the image was 8 bit but the function was reading it assuming it was 24 bit. how could i discard the pixels that are a certain colour (the colour around the edge)?

just use a software to convert it into a 24bit bitmap

I did:).

did you get the texture to load onto a shape?

yep :).
i texture mapped the image onto two triangles.
and i made the images object oriented.

class Image
{
// Attributes
public:
    std::string path;
    std::string extension;

    GLuint texture_num;

// Methods
public:
    Image() {};
    ~Image() {};
    bool load(std::string FileName);
    void reload();
};

and I added a reload function for when the window goes into full screen mode and shuts down opengl.

Glad to see things are working well. Keep learning & practicing. There's a lot to learn with OpenGL, but it's mostly fun along the way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.