OK, well this is the first time that anyone has release how to draw a textured 2d box (I Think).
First of all I would like to thank,
Xero - For pointing me in the right direction.
openglforums.com - [link]http://openglforums.com/[/link] - for their great advice
Azorbix - for answering my n00bish questions.
Anyway....
The first thing you want to do is make the bitmap that you want to be the texture...
It can be anything. NOTE: What ever you want to be transparent set the color to be True Pink (255, 0, 255). You must save it into the Half-Life Folder.
Heres an example of what mine looks like.
/*Screenshot not Availible*/
OK well after you make the image itself you can start the fun part of this tutorial...The coding...
Now lets start by creating the texture:
bool CreateTexture(UINT textureArray[], LPSTR strFileName, int textureID, int width, int height)
{
First thing we have to do is creat the image record,
AUX_RGBImageRec *pBitmap = NULL;
Also we should check to make sure that a filename has been entered, just for NON-Crashing Reasons
if(!strFileName)
return false;
OK Now we are going to load the bitmap into memory and check to make sure it worked:
pBitmap = auxDIBImageLoad(strFileName);
if(pBitmap == NULL)
return false;
Now the next step we want to do is change the bitmap information so that it include an alpha value not just RGA. Also in this step we change our A values to 0 if it is true pink...
COLORREF Color;
Color = RGB(255, 0, 255); //Setting the color we want transparent
pBitmap->data = CreateRGBA(pBitmap->data, Color, width, height);//I will give you this function later
Now we make openGL do the rest of the work for us...
glGenTextures(1, &textureArray[textureID]); //Generate the Texture
glBindTexture(GL_TEXTURE_2D, textureArray[textureID]); //Bind the Texture to GL_TEXTURE_2D
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, pBitmap->sizeX, pBitmap->sizeY, GL_RGBA, GL_UNSIGNED_BYTE, pBitmap->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);//These set the quality for the image that we want..
Now we clean up our Mess ( ) and free up some memory..
if (pBitmap) // If we loaded the bitmap
{
if (pBitmap->data) // If there is texture data
{
free(pBitmap->data); // Free the texture data, we don't need it anymore
}
free(pBitmap); // Free the bitmap structure
}
return true;
}
Now you try and compile but you get an error saying undeclaired identifier .. blah .. blah .. blah .. This is because you dont have this:
It needs to be placed ABOVE the function that i just gave you...
Thanks to Xer0 for helping me with this one
unsigned char* CreateRGBA(unsigned char * oImage,COLORREF ZeroAlpha, int Width, int Height)
{
if(oImage == NULL) return NULL; //Error Checking
int nSize = (Height*Width) * 4;
unsigned char * Image = (unsigned char *) malloc(nSize); //Create Temporary Image Record
if(Image == NULL) return NULL; //Error Checking
for(int n=0,i=0; n < (Height*Width)*3; n+=3,i+=4)
{
Image = oImage[n]; //
Image[i+1] = oImage[n+1]; //Copies Origional Information to the Temporary one
Image[i+2] = oImage[n+2]; //
if(ZeroAlpha == -1) { Image[i+3]= (int)255; continue; } //Error Checking
if(Image == GetRValue(ZeroAlpha) && Image[i+1] == GetGValue(ZeroAlpha) && Image[i+2] == GetBValue(ZeroAlpha)) //Checks to see if the values are (255, 0, 255)
Image[i+3] = 0; //If Yes then set the A Value to 0
else Image[i+3] = 255; //Else to full
}
return Image; //Return the new image
}
OK well now we have to Call the CreateTexture Function
So first we create a location for the Image(s) to be stored pernamently...
#define MAX_TEXTURES 1 // This says how many texture we will be using
UINT g_Texture[MAX_TEXTURES];
And then in glEnable we will call the CreateTexture ONLY once!
static int = false;
if(init == false)
{
if(CreateTexture(g_Texture, "menu0.bmp", 0, 400, 200))
{
TexOK = true;
}
}
Also create a glabal variable called TexOK and set to false.
OK so now we have created our texture...Now we want to draw it
Firstly We Need a Couple of Functions:
void OrthoMode(int left, int top, int right, int bottom)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho( left, right, bottom, top, 0, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void PerspectiveMode() // Set Up A Perspective View
{
glMatrixMode( GL_PROJECTION );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
}
Im too lazy to explain them so i wont....
Next is where we actually draw th Box with the texture on it....YAY
float MenuInt = 1.0;
void DrawTexMenu()
{
glColor4f(1.0, 1.0, 1.0, MenuInt);
glEnable(GL_TEXTURE_2D);
OrthoMode(0, 0, Viewport[2], Viewport[3]);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Looks the best
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D, g_Texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex2f(X, Y);//top left
glTexCoord2f(0.0f, 0.0f); glVertex2f(X, Y + 200);//bottom left
glTexCoord2f(1.0f, 0.0f); glVertex2f(X + 400, Y + 200);//bottom right
glTexCoord2f(1.0f, 1.0f); glVertex2f(X + 400, Y);//top right
glEnd();
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
PerspectiveMode();
}
EDIT: Call that in glEnable after the CreateTexture Command...
Please note that I may have missed a couple of things in here because it is such a complicated thing to do, please feel free to reply to this post or PM me for any help.