PDA

View Full Version : I'm getting lost in the details..please help



Ace_Man
05-10-2000, 05:31 PM
I have been struggling for quite some time in learning(and understanding) how to load a bitmap in OpenGL (Just get it on the screen)
I'm getting really confused in all the "red tape" involved so I thought somebody could answer these questions:

1-Does OpenGl have support for loading bitmaps? or is this a Windows only thing(embedded in an Opengl program)

2- Every tutorial I see on the net about this uses glaux. Why? I never see tutorials(of any kind)using glutCreateWindow().

3-Do I really have to study windows programming to understand how loading the bitmap works?

I don't want to copy/use anyone else's code to do this. I want to understand (by looking at someone's code) and then write the code myself. So far every tutorial I look at (this one is from NeHe) goes something like this:

HDC hDC=NULL;
HGLRC hRC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance

bool keys[256];
bool active=TRUE;
bool fullscreen=TRUE;

GLfloat xrot;
GLfloat yrot;
GLfloat zrot;

GLuint texture[1];

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

AUX_RGBImageRec *LoadBMP(char *Filename
{
FILE *File=NULL;
if (!Filename)
{
return NULL;
}

File=fopen(Filename,"r");
if (File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}

return NULL;
}

int LoadGLTextures()
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("Data/NeHe.bmp"))
{
Status=TRUE; // Set The Status To TRUE

glGenTextures(1, &texture[0]); // Create The Texture

// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}

if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}

free(TextureImage[0]); // Free The Image Structure
}

return Status; // Return The Status
}


5-Does it really have to be this complicated? I am not an advanced OpenGl/C programmer but this code makes zero sense to me...I'm guessing I can't just call some function with the name of my file and load it into memory...then call another function to display it....Is any of the above code OpenGl at all? Or is all that Windows95/98 stuff....

Any clarification would be great..Thanks so much....

Gorg
05-10-2000, 05:48 PM
To show a bitmap on the screen, you can use glDrawPixels(slow on PCs) or you can just make a texture out of it(like nehe does) and apply this texture to a polygon.

Now to load the bitmap in memory, you can use auxloadbitmap or something like that or you can make your own function.

This is a basic C function for loading a bitmap using some Win32 data structures :

bool loadBitmap(char* filename, unsigned char alpha, GLubyte* color)
{
FILE* bmpfile;
BITMAPFILEHEADER bmfileheader;
BITMAPINFOHEADER bminfoheader;
RGBTRIPLE rgb;

int texpos = 0;

bmpfile = fopen(filename, "rb");

if (!bmpfile)
return false;

fread(&bmfileheader, sizeof(BITMAPFILEHEADER), 1, bmpfile);
fread(&bminfoheader, sizeof(BITMAPINFOHEADER), 1, bmpfile);


iWidth = bminfoheader.biWidth;
iHeight = bminfoheader.biHeight;


colors = (GLubyte*)malloc(tex->width * tex->height *4);

// read color
for (int i = 0; i < iWidth; i++)
{
for(int j = iHeight - 1; j>=0 ; j--) //start with the height because the first data you read in the color array of the bitmap
{ // is coord (1.0).
fread(&rgb, sizeof(RGBTRIPLE),1,bmpfile);
colors[texpos] = rgb.rgbtRed ;
colors[texpos+1] = rgb.rgbtGreen;
colors[texpos+2] = rgb.rgbtBlue;
colors[texpos+3] = alpha

texpos += 4;
}

}
fclose(bmpfile);

return true;
}

It is not complicated to load a bitmap, it is only that the documentation is bad.

a bitmap is composed of 3 things:
you can get the description of those things at msdn.com. You have to look in platformSDK, win32, references and then look for Bitmap. There is actually two types of bitmap, you can check wich type it is by looking at the type parameter of the info header. But from experience, this methods works in 99.9% of the time

1.Bitmap File Header

2.Bitmap Info Header

3.Colors values wich are RBGTRIPLE. Wich is just a structure of 3 floats. One for red, one for green, one for blue.

So what you do is :

read the file header,
read the info header, get the height and width.

allocate memory for data.

then get all the RGB triplets.

Then use opengl calls to load it in a texture.

The function above also have an alpha parameter to specify a default alpha value if you d'like to use RGBA instead of RGB

Of course, you'll still have to learn how to do texture mapping with opengl.

to learn that, you can buy the red book, and/or check tutorials.

Nehe tutorials are really good.
Romka also have good tutorials. he often write on the forum on update on his site.



[This message has been edited by Gorg (edited 05-10-2000).]

Ace_Man
05-10-2000, 06:37 PM
Thanks for the speedy (and great reply) It makes a lot more sense now...