I wrote codes to demo the pixel transparent technology, but it doesn’t work. What is my problem, texture parameters, or texture data?
The entire code is the following:
#include <iostream>
#include <gl/glut.h>
#include <gl/GLAux.h>
#pragma comment(lib, “glaux.lib”)
unsigned char *texture = NULL;
void init()
{
AUX_RGBImageRec *pImage = NULL;
pImage = auxDIBImageLoadA(“pic\NPC_02.BMP”);
if (pImage == NULL)
{
std::cout << “error load npc_01.bmp” << std::endl;
return;
}
// the picture is a bitmap file, and it has no alpha channel, we
// create one for it
// save the rgb components of left-up corner pixel, which is to be set as alpha
unsigned char r = 0, g = 0, b = 0;
r = *(pImage->data), g = *(pImage->data + 1), b = *(pImage->data + 2);
// how many bytes does the texture with alpha channel require?
texture = new unsigned char[pImage->sizeX*pImage->sizeY*4];
// copy pixel
for (int i = 0; i < pImage->sizeX; i++)
for (int j = 0; j < pImage->sizeY; j++)
{
*(texture + i*pImage->sizeX*4 + j*4) = *(pImage->data + i*pImage->sizeX*3 + j*3); // r
*(texture + i*pImage->sizeX*4 + j*4 + 1)= *(pImage->data + i*pImage->sizeX*3 + j*3 + 1); // g
*(texture + i*pImage->sizeX*4 + j*4 + 2)= *(pImage->data + i*pImage->sizeX*3 + j*3 + 2); // b
if (*(pImage->data + i*3 + j*3) == r &&
*(pImage->data + i*3 + j*3 + 1) == g &&
*(pImage->data + i*3 + j*3 + 2) == b)
{
*(texture + i*pImage->sizeX*4 + j*4 + 3) = 0; // transparent
}
else
{
*(texture + i*pImage->sizeX*4 + j*4 + 3) = 255; // opacity
}
}
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
// texture parameters
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, pImage->sizeX, pImage->sizeY, GL_RGBA, GL_UNSIGNED_BYTE, texture);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
free(pImage->data);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5f);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, -1.0f / 8.0f);
glVertex2i(-32, -32);
glTexCoord2f(1.0f / 8.0f, -1.0 / 8.0f);
glVertex2i(32, -32);
glTexCoord2f(1.0f / 8.0f, 0.0f);
glVertex2i(32, 32);
glTexCoord2f(0.0f, 0.0f);
glVertex2i(-32, 32);
glEnd();
glDisable(GL_ALPHA_TEST);
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
void resize(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-96, 96, -96, 96);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(512, 512);
glutInitWindowPosition(0, 0);
glutCreateWindow(“Alpha, Texturing, and Walk”);
glutReshapeFunc(resize);
glutDisplayFunc(display);
init();
glutMainLoop();
// release memory
delete texture;
return 0;
}