Jump to content
  • Advertisement
Sign in to follow this  
neo187

OpenGL Transparent bitmaps help - lodePNG/SOIL problem

This topic is 2582 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I am working on a 3D game, I'd like to overlay the 3D scene with some GUI elements with transparency... The engine I am working with has got loaders for TGA and BMP files, my first guess was to try incorporate PNG in it. I am trying to use lodePNG, which any recommend. I included lodepng.h to the header files and lodepng.c to the source files. I included the header in my class and created this function from a sample I saw:



GLuint GLTexture::LoadPNG(char* filename){
unsigned char* buffer;
unsigned char* image;
size_t buffersize, imagesize;
GLuint texName;
LodePNG_Decoder decoder;

LodePNG_loadFile(&buffer, &buffersize, filename);
LodePNG_Decoder_init(&decoder);
decoder.infoRaw.color.colorType = 6; /* Load image as RGBA */
LodePNG_decode(&decoder, &image, &imagesize, buffer, buffersize);
if(decoder.error) {
printf("Error reading in png image: %d\n", decoder.error);
exit(1);
} else {
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1,&texture[0]);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,decoder.infoPng.width,
decoder.infoPng.height,0, GL_RGBA,GL_UNSIGNED_BYTE,image);
}
return texName;
}


But when I compile I get a bunch of unresolved symbol errors:

lodepng Error 1 error LNK2019: unresolved external symbol "void __cdecl LodePNG_decode
...

And many other references to functions....

I tried with SOIL (Simple Opengl Image Library) too, where I included the libSOIL.a file and the SOIL.h file to my project, still a bunch of unresolved simbols when compiling...

I am not sure what I am doing wrong.... But I don't necessarily need to read PNG, all I need is a quick and easy way to render 2D sprites with transparency...

Could anyone help with this?

Thank you!

Share this post


Link to post
Share on other sites
Advertisement
The problem is linker couldn't find an implementation of these functions.

Are they on library? If yes, use next line:



#pragma comment(lib,"<put there path to library file")









Best wishes, FXACE.

Share this post


Link to post
Share on other sites

The problem is linker couldn't find an implementation of these functions.

Are they on library? If yes, use next line:



#pragma comment(lib,"<put there path to library file")







Thanks, I was actually missing the lodepng.cpp file in the solution, but once linked I get a landslide of compiler errors, it doesn;t seem to like the file at all:


Error 48 error C2040: 'LodePNG_append_chunk' : 'unsigned int (unsigned char **,size_t *,const unsigned char *)' differs in levels of indirection from 'unsigned char *(unsigned char **,size_t *,const unsigned char *)'

Error 34 error C2065: 'LodePNG_CompressSettings' : undeclared identifier

Error 7 error C2065: 'settings' : undeclared identifier

Error 40 error C2078: too many initializers

and many more.... these are all the files that they mention to link on their site: http://lodev.org/lodepng/

I may give up on that.... isn't there any other loaders apart from PNG ones that support alpha information?

Share this post


Link to post
Share on other sites
I eventually found a quick and simple function that renders transparent BMPs using colour key:


void GLTexture::LoadAlphaBMP(char *name){

AUX_RGBImageRec *pImage_RGB = auxDIBImageLoad(name);
unsigned char *pImage_RGBA = NULL;

if( pImage_RGB != NULL )
{
int imageSize_RGB = pImage_RGB->sizeX * pImage_RGB->sizeY * 3;
int imageSize_RGBA = pImage_RGB->sizeX * pImage_RGB->sizeY * 4;

// allocate buffer for a RGBA image
pImage_RGBA = new unsigned char[imageSize_RGBA];

//
// Loop through the original RGB image buffer and copy it over to the
// new RGBA image buffer setting each pixel that matches the key color
// transparent.
//

int i, j;

for( i = 0, j = 0; i < imageSize_RGB; i += 3, j += 4 )
{
// Does the current pixel match the selected color key?
if( pImage_RGB->data == g_keyColor[0] &&
pImage_RGB->data[i+1] == g_keyColor[1] &&
pImage_RGB->data[i+2] == g_keyColor[2] )
{
pImage_RGBA[j+3] = 0; // If so, set alpha to fully transparent.
}
else
{
pImage_RGBA[j+3] = 255; // If not, set alpha to fully opaque.
}

pImage_RGBA[j] = pImage_RGB->data;
pImage_RGBA[j+1] = pImage_RGB->data[i+1];
pImage_RGBA[j+2] = pImage_RGB->data[i+2];
}

glGenTextures( 1, &texture[0] );
glBindTexture( GL_TEXTURE_2D, texture[0] );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);

// Don't forget to use GL_RGBA for our new image data... we support Alpha transparency now!
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, pImage_RGB->sizeX, pImage_RGB->sizeY, 0,
GL_RGBA, GL_UNSIGNED_BYTE, pImage_RGBA );
}

if( pImage_RGB )
{
if( pImage_RGB->data )
free( pImage_RGB->data );

free( pImage_RGB );
}

if( pImage_RGBA )
delete [] pImage_RGBA;

}


Which works great.

Was wondering if anyone would know of a way to create a fadeout effect using a texture that covers the screen? So that it starts out as barely visible and gradually becomes fully visible, obscuring the 3d scene? I am currently using


glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );


to render the transparent bits of the image, is there a parameter that regulates the overall transparency of the image?

Thank you

Share this post


Link to post
Share on other sites
Hello,

First of all, I think it is easier with the following code by using LodePNG_decode32_file which does everything (loading file, decoding, using RGBA format) at once. Also note the "free" at the end, otherwise you'll have a memory leak, the complete memory of the texture will remain in RAM for nothing. OpenGL makes a copy of this data, the original one can be freed.


GLuint GLTexture::LoadPNG(char* filename){
unsigned char* image;
unsigned w, h;
unsigned error;
GLuint texName;
error = LodePNG_decode32_file(&image, &w, &h, filename);
if(error) {
printf("Error reading in png image: %d\n", error);
exit(1);
} else {
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1,&texture[0]);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,w,
h,0, GL_RGBA,GL_UNSIGNED_BYTE,image);
}
free(image);
return texName;
}


If you're working in C++, you can also use the C++ version of LodePNG.

If you include all source files, normally it should be able to link. What happens if you use the code above? Probably the sample was for an older version of LodePNG. Does your project already exist out of multiple source files, is using functions from other source files possible, and are there differences in settings related to this file in your IDE? Does your IDE support mixing C and C++ code? Try renaming lodepng.c into lodepng.cpp?


Hello,

I am working on a 3D game, I'd like to overlay the 3D scene with some GUI elements with transparency... The engine I am working with has got loaders for TGA and BMP files, my first guess was to try incorporate PNG in it. I am trying to use lodePNG, which any recommend. I included lodepng.h to the header files and lodepng.c to the source files. I included the header in my class and created this function from a sample I saw:



GLuint GLTexture::LoadPNG(char* filename){
unsigned char* buffer;
unsigned char* image;
size_t buffersize, imagesize;
GLuint texName;
LodePNG_Decoder decoder;

LodePNG_loadFile(&buffer, &buffersize, filename);
LodePNG_Decoder_init(&decoder);
decoder.infoRaw.color.colorType = 6; /* Load image as RGBA */
LodePNG_decode(&decoder, &image, &imagesize, buffer, buffersize);
if(decoder.error) {
printf("Error reading in png image: %d\n", decoder.error);
exit(1);
} else {
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1,&texture[0]);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,decoder.infoPng.width,
decoder.infoPng.height,0, GL_RGBA,GL_UNSIGNED_BYTE,image);
}
return texName;
}


But when I compile I get a bunch of unresolved symbol errors:

lodepng Error 1 error LNK2019: unresolved external symbol "void __cdecl LodePNG_decode
...

And many other references to functions....

I tried with SOIL (Simple Opengl Image Library) too, where I included the libSOIL.a file and the SOIL.h file to my project, still a bunch of unresolved simbols when compiling...

I am not sure what I am doing wrong.... But I don't necessarily need to read PNG, all I need is a quick and easy way to render 2D sprites with transparency...

Could anyone help with this?

Thank you!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!