C++ File I/O

Published April 18, 2009
Advertisement
This is just a short journal entry, again not featuring any code, simply because I'm not on my machine.

I have converted over my TGA image loader from VB(6) to C++ and now I am working on converting my Model Loader. I wrote my own exporter for 3d Studio Max which exports just the information I require for models. It is a binary format, which I'm particularly proud of.

For loading binary files in C++ I am using the ifstream procedures. And I must say that I'm having a good amount of success with it. When I had used C++ previously, I had been using the fopen, fread, etc commands and found them sufficient. However, ifstream stuff just seems much....better, for some reason.

As I said, texture loading is working, however I'm still trying to verify whether model loading is working, as since I'm avoiding all 'older' OpenGL code, I'm not using any Immediate Mode. So I'm not sure whether it's my Vertex Array code or Model code that is incorrect, since my debugger does want to co-operate.

I'm sure that it will be up and running in the next day or so and I will post the code that I've used.

EDIT: I have now attached my TGA Loader Code

#pragma pack(1)typedef struct{	GLbyte		identSize;			// 0	GLbyte		colourMapType;		// 1	GLbyte		imageType;			// 2	GLshort		colourMapStart;		// 3	GLshort		colourMapLength;	// 5	GLbyte		colourMapBits;		// 7	GLshort		xStart;				// 8	GLshort		yStart;				// 10	GLshort		width;				// 12	GLshort		height;				// 14	GLbyte		bits;				// 16	GLbyte		descriptor;			// 17} TGAHeader;GLuint LoadMaterialFromFile(LPCSTR sFilename){	TGAHeader 			header;	long				dataSize;	unsigned long		        i;        GLuint                          textureID;	ifstream file (sFilename, ios::in | ios::binary);	if (file.is_open())	{				file.seekg (0, ios::beg);		file.read ((char*) &header, 18);		dataSize = header.width * header.height * (header.bits / 8);				vData.clear();		vData.resize(dataSize, 0);						for (i = 0 ; i < vData.size() ; i++)		{			file.read ((char*) &vData, 1 );		}		file.close();		glGenTextures(1, &textureID );		glBindTexture(GL_TEXTURE_2D, textureID );		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);		if (header.bits == 32)		{			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, header.width, header.height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, &vData[0] );		} 		else 		{			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, header.width, header.height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, &vData[0] );		}		return textureID;	}	else	{		return 0;	}};
Previous Entry C++ vs VB for OpenGL
Next Entry Frame Buffer Object
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement