Skip to main content
GameDev.net gamedev.net
🔒 Locked

texture mapping problems

Started by xg0blin Nov 28, 2002 at 1:50 PM 2 replies 650+ views
Original Post
xg0blin
xg0blin
Heres the deal, everytime I see something on loading .bmp''s, it''s written in c. I wanted to write one using C++ style file i/o, and it''s not working. I wrote this using the equivalent c++ functions for the c code used in my book (opengl game programming). I just like c++ stuff I guess cause I''m more familiar with it. Here''s the code

  
#ifndef ImageLoad_h
#define ImageLoad_h

namespace ImageLoad
{
	#define BITMAPID 0x4D42

	struct BITMAPINFOHEADER
	{
		unsigned int Size;
		int Width;
		int Height; 
		unsigned short int Planes;
		unsigned short int BitCount;
		unsigned int Compression; 
		unsigned int SizeImage; 
		int XPelsPerMeter;
		int YPelsPerMeter;
		unsigned int ColorsUsed;
		unsigned int ColorsImportant;
		BITMAPINFOHEADER();
	};

	struct BITMAPFILEHEADER
	{
		unsigned short int Type;
		unsigned int Size;
		unsigned short int Reserved1; 
		unsigned short int Reserved2;
		unsigned int OffBits;
		BITMAPFILEHEADER();
	};

	class ImageLoader
	{
		private:
			
		public:
			ImageLoader() { };
			~ImageLoader() { };
			unsigned char *LoadBitmapFile(char *, BITMAPINFOHEADER *);
	};
}



#endif

  
that is the first file. Then there''s this one

  
#include "ImageLoad.h"
#include <fstream>
using std::fstream;
using std::ios;

namespace ImageLoad
{
	BITMAPINFOHEADER::BITMAPINFOHEADER()
	{
		Size=Compression=SizeImage=ColorsUsed=ColorsImportant=0;
		Width=Height=XPelsPerMeter=YPelsPerMeter=0;
		Planes=BitCount=0;
	}

	BITMAPFILEHEADER::BITMAPFILEHEADER()
	{
		Type=Reserved1=Reserved2=0;
		Size=OffBits=0;
	}

	unsigned char * ImageLoader::LoadBitmapFile(char *FileName, BITMAPINFOHEADER *BitMapInfoHeader)
	{
		fstream InputFile;
		BITMAPFILEHEADER BitMapFileHeader;
		unsigned char *BitMapImage;
		int ImageIndex = 0;
		unsigned char TempRGB;

		InputFile.open(FileName, ios::in|ios::binary); 

		if(!InputFile)
			return NULL;

		InputFile >> BitMapFileHeader.Type >> BitMapFileHeader.Size 
			      >> BitMapFileHeader.Reserved1 >> BitMapFileHeader.Reserved2 
				  >> BitMapFileHeader.OffBits;

		if(BitMapFileHeader.Type != BITMAPID)
		{
			InputFile.close();
			return NULL;
		}

		InputFile >> BitMapInfoHeader->Size >> BitMapInfoHeader->Width 
			      >> BitMapInfoHeader->Height >> BitMapInfoHeader->Planes
				  >> BitMapInfoHeader->BitCount >> BitMapInfoHeader->Compression
				  >> BitMapInfoHeader->SizeImage >> BitMapInfoHeader->XPelsPerMeter
				  >> BitMapInfoHeader->YPelsPerMeter >> BitMapInfoHeader->ColorsUsed
				  >> BitMapInfoHeader->ColorsImportant;


 		InputFile.seekg(BitMapFileHeader.OffBits);
		
		BitMapImage = new unsigned char[BitMapInfoHeader->SizeImage];

		if(!BitMapImage)
		{
			delete BitMapImage;
			InputFile.close();
			return NULL;
		}

		for(unsigned long i = 0; i < BitMapInfoHeader->SizeImage; i++)
			InputFile >> BitMapImage[i];

		if(BitMapImage == NULL)
		{
			InputFile.close();
			return NULL;
		}

		for(ImageIndex = 0; ImageIndex < BitMapInfoHeader->SizeImage; ImageIndex += 3)
		{
			TempRGB = BitMapImage[ImageIndex];
			BitMapImage[ImageIndex] = BitMapImage[ImageIndex + 2];
			BitMapImage[ImageIndex + 2] = TempRGB;
		}

		InputFile.close();
		return BitMapImage;
	}
}
  
and then here''s the main file

  
#include <iostream>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include "ImageLoad.h"
using namespace std;
using namespace ImageLoad;

ImageLoader MyLoader;
ImageLoad::BITMAPINFOHEADER BMInfoHeader;
unsigned char *data;
unsigned int texture;

void Init(void)
{
	data = MyLoader.LoadBitmapFile("MyBitmap.bmp", &BMInfoHeader);
	if(!data)
		exit(0);
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, BMInfoHeader.Width, 
		         BMInfoHeader.Height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

	glShadeModel(GL_SMOOTH);
	glHint(GL_NICEST, GL_POLYGON_SMOOTH_HINT);
	glEnable(GL_DEPTH_TEST);

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glColor3f(0.0f, 1.0f, 0.2f);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-100, 100, -75, 75, -10, 10);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void MyKeyboard(unsigned char key, int x, int y)
{
	if(key)
		exit(0);
}

void MyDisplay(void)
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
	glBegin(GL_POLYGON);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-25, -25, 0);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f( 25, -25, 0);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f( 25,  25, 0);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-25,  25, 0);
	glEnd();

	glutSwapBuffers();
}

int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutCreateWindow("ImageLoader");
	glutFullScreen();
	Init();
	glutKeyboardFunc(MyKeyboard);
	glutDisplayFunc(MyDisplay);
	glutMainLoop();
	return 0;
}
  
Does anyone see what might be keeping this from working? It''s never putting anything into my BITMAPINFOHEADER or BITMAPFILEHEADER struct, and returnint 0, and I can''t seem to get it.
Mathematix
Mathematix
I must admit that the code could do with alot of restructuring. I've failed to notice where you are reading the files in byte chunks and verifying that the pixel sizes of the bitmaps are a power of 2 in both the x and y directions.

Peace out,
Mathematix.
Q3A + UT1999 = UT2003
therefore UT2003 - Q3A = UT1999
and UT2003 - UT1999 = Q3A!

[edited by - Mathematix on November 28, 2002 4:05:17 PM]
xg0blin
xg0blin
Actually, I haven''t done a lot of texture mapping. I''ve only ever done it before using glaux. I took the code almost line per line out of a book, and only changed the functions from like fread to InputFile >> and things of that nature. I''m just learning
Mathematix
Mathematix
I would suggest that you look at quadrics if you want to do any serious texture mapping. Auxiliary objects should only really be used to fill a scene, something not in the foreground that requires any kind of manipulation.

I would only suggest that you continue with the auxilliary objects if you are still learning about the transformations (rotation, reflection and translation).

Best wishes dude!

Peace out,
Mathematix.
Q3A + UT1999 = UT2003
therefore UT2003 - Q3A = UT1999
and UT2003 - UT1999 = Q3A!

[edited by - Mathematix on December 2, 2002 2:54:26 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.