Original Post
Alright, I'm having a bear of a time with this texture mapping. I've included code, I'm really hoping some one can give me a hand. If anyone can figure out why the texture isn't displaying, I'd really appreciate it. I'm suspicious of glTexImage2D. Sorry about the length. [Edited by - Modena_au on November 19, 2004 9:37:18 PM]
#include <gl\glut.h>
#include <fstream>
#include <vector>
using namespace std;
struct sImage
{
GLuint uiFormat;
GLuint uiWidth;
GLuint uiHeight;
GLuint uiLength;
GLuint uiChannels;
GLubyte *pubData;
};
#pragma pack(1) // Makes sure that our struct isn't packed
struct sBMPHeader
{
char cIdentifier[2];
unsigned int uiFileSize;
unsigned int uiReserved;
unsigned int uiOffset;
unsigned int uiHeaderSize;
unsigned int uiWidth;
unsigned int uiHeight;
unsigned short usPlanes;
unsigned short usBPP;
unsigned int uiCompression;
unsigned int uiDataSize;
unsigned int uiHResolution;
unsigned int uiVResolution;
unsigned int uiColors;
unsigned int uiImportantColors;
unsigned int uiPalette[4];
};
#pragma pack() // Sets packing back to default (faster for non-file structs)
sImage *loadImage(string fileName);
void display();
GLuint gTextureID = 0;
sImage *IMAGE;
void main()
{
IMAGE = loadImage("c:\\Grass00.bmp");
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800, 600);
glutCreateWindow("BMP");
glutDisplayFunc(display);
// Generate a texture id
glGenTextures(1, &gTextureID);
// bind it to the texture we're about to load
glBindTexture(GL_TEXTURE_2D, gTextureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// set up the image we loaded as a openGL texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_BGR_EXT, IMAGE->uiWidth,
IMAGE->uiHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, IMAGE->pubData);
glEnable(GL_TEXTURE_2D);
glutMainLoop();
glDisable(GL_TEXTURE_2D);
};
sImage *loadImage(string fileName)
{
fstream imageFile;
imageFile.open(fileName.c_str(), ios::in | ios::binary);
sBMPHeader *imageHeader = new sBMPHeader;
sImage *imageData = new sImage;
imageFile.read((char *)imageHeader, sizeof(*imageHeader));
imageData->uiLength = imageHeader->uiDataSize;
imageData->uiWidth = imageHeader->uiWidth;
imageData->uiHeight = imageHeader->uiHeight;
imageData->uiChannels = imageHeader->usPlanes;
if (strcmp(imageHeader->cIdentifier, "BM"))
{
if (imageHeader->usBPP == 32)
imageData->uiFormat = GL_BGRA_EXT;
else if (imageHeader->usBPP == 24)
imageData->uiFormat = GL_BGR_EXT;
}
else
imageData->uiFormat = GL_RGB;
imageData->pubData = new GLubyte[imageData->uiLength];
imageFile.seekg(imageHeader->uiOffset);
// Read it in one at a time, or else it will load in a string and not get
// the entire file.
for (int i = 0; i < imageData->uiLength; i++)
imageFile.read(reinterpret_cast<char *>(&imageData->pubData), 1);
return imageData;
};
void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, gTextureID);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex2f(0.5f, -0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(0.5f, 0.5f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5f, 0.5f);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5f, -0.5f);
glEnd();
//Just to make sure that the texture information was loaded properly
glRasterPos2f(0.5f, -0.5f);
glDrawPixels(64, 64, GL_BGR_EXT, GL_UNSIGNED_BYTE, IMAGE->pubData);
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
//glFlush();
};