Transparency help [Solved, I've been stupid]
Okay, after many tutorials and references, I am convinced that Alpha Channel is a far better option than Masking. Could anyone recommend a free program to convert 24 bit BMP files to 32 bit TGA files?
oh, i realize that you are using opengl. i was just suggesting allegro because it fits 2d games very well. however, it obviously is fairly limited in what it can do at times. i was just suggesting it because it does the masking automatically.
however, if you used allegrogl (i think that is what it is called) you might be able to use that functionality with opengl too. not sure, haven't looked into it myself.
[edit]: you edited while i was posting. :D
however, if you used allegrogl (i think that is what it is called) you might be able to use that functionality with opengl too. not sure, haven't looked into it myself.
[edit]: you edited while i was posting. :D
Quote:Original post by horizon981
Okay, after many tutorials and references, I am convinced that Alpha Channel is a far better option than Masking. Could anyone recommend a free program to convert 24 bit BMP files to 32 bit TGA files?
Quote:Original post by cignox1
DOWNLOAD GIMP and draw your sprites. When you've done this, select the parts of your image you want to be trasparent, and delete them. Then save the image as TGA (that can keep an alpha channel).
;)
I download GIMP(nice one), opened the bitmap image, chose 'select by color'->selected brown background->Cut. The background was replaced by a checkerboard, which i'll take as a transparent one.Added an alpha channel.Then, I saved it as .TGA file.
Have I done anything wrong till now? Is this the way to do it?
If yes, i'll procced to TGA loader functions...
Have I done anything wrong till now? Is this the way to do it?
If yes, i'll procced to TGA loader functions...
sounds about right... get the loader functions... their easy enough to find kicking about the internet now :D
Here's my image.
Please download it, run it in GIMP and see what's wrong.
I am unable to load it using IMG_Load.
When I used the NeHe TGA loader, I am getting a white texture [crying] [crying]
Looks like there is no end to my problems!
I did remove the background in GIMP as I mentioned above, but somehow, GIMP shows the image to be 24bpp [crying]
How can I convert it to 32bpp in GIMP? There seems to be no particular option for that.
HELP!! [bawling] [bawling]
Please download it, run it in GIMP and see what's wrong.
I am unable to load it using IMG_Load.
When I used the NeHe TGA loader, I am getting a white texture [crying] [crying]
Looks like there is no end to my problems!
I did remove the background in GIMP as I mentioned above, but somehow, GIMP shows the image to be 24bpp [crying]
How can I convert it to 32bpp in GIMP? There seems to be no particular option for that.
HELP!! [bawling] [bawling]
Quote:Original post by horizon981
How can I convert it to 32bpp in GIMP? There seems to be no particular option for that.
Link 1
Link 2
Also, post your call to glTexImage2D.
Quote:Original post by horizon981
Here's my image.
Please download it, run it in GIMP and see what's wrong.
I am unable to load it using IMG_Load.
When I used the NeHe TGA loader, I am getting a white texture [crying] [crying]
Looks like there is no end to my problems!
I did remove the background in GIMP as I mentioned above, but somehow, GIMP shows the image to be 24bpp [crying]
How can I convert it to 32bpp in GIMP? There seems to be no particular option for that.
HELP!! [bawling] [bawling]
Your image works fine for me. It looks correct in GIMP, and also looks correct when loaded and used in OpenGL. I guess the issue might lie in your rendering code. Make sure you are properly enabling texturing at the right time and binding the texture before rendering the quads. Post up some code so we can see.
The following code was used before rendering the image to enable the alpha test and blending mode:
// enable and set modes for blending and alpha testingglEnable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);glEnable(GL_ALPHA_TEST);glAlphaFunc(GL_GREATER, 0);// bind and draw imageglBindTexture(GL_TEXTURE_2D,texName[0]);.. render image (glTexCoord* and glVertex* calls)
hth
F451
Im going nuts. [crying]
#include <SDL/SDL.h>#include <SDL/SDL_Image.h>#include <GL/gl.h>#include <GL/glu.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#ifndef GL_BGR#define GL_BGR 0x80E0#endifstruct TextureImage // Create A Structure{ GLubyte *imageData; // Image Data (Up To 32 Bits) GLuint bpp; // Image Color Depth In Bits Per Pixel GLuint width; // Image Width GLuint height; // Image Height GLuint texID;}textures;bool LoadTGA(TextureImage *texture, char *filename) // Loads A TGA File Into Memory{ GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header GLubyte TGAcompare[12]; // Used To Compare TGA Header GLubyte header[6]; // First 6 Useful Bytes From The Header GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram GLuint temp; // Temporary Variable GLuint type=GL_RGBA; FILE *file = fopen(filename, "rb"); // Open The TGA File if( file==NULL || // Does File Even Exist? fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || // Are There 12 Bytes To Read? memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || // Does The Header Match What We Want? fread(header,1,sizeof(header),file)!=sizeof(header)) // If So Read Next 6 Header Bytes { if (file == NULL) // Did The File Even Exist? *Added Jim Strong* return false; // Return False else { fclose(file); // If Anything Failed, Close The File return false; // Return False } } texture->width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte) texture->height = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte) if( texture->width <=0 || // Is The Width Less Than Or Equal To Zero texture->height <=0 || // Is The Height Less Than Or Equal To Zero (header[4]!=24 && header[4]!=32)) // Is The TGA 24 or 32 Bit? { fclose(file); // If Anything Failed, Close The File return false; // Return False } texture->bpp = header[4]; // Grab The TGA's Bits Per Pixel (24 or 32) bytesPerPixel = texture->bpp/8; // Divide By 8 To Get The Bytes Per Pixel imageSize = texture->width*texture->height*bytesPerPixel; texture->imageData=(GLubyte *)malloc(imageSize); // Reserve Memory To Hold The TGA Data if( texture->imageData==NULL || // Does The Storage Memory Exist? fread(texture->imageData, 1, imageSize, file)!=imageSize) // Does The Image Size Match The Memory Reserved? { if(texture->imageData!=NULL) // Was Image Data Loaded free(texture->imageData); // If So, Release The Image Data fclose(file); // Close The File return false; // Return False } for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) // Loop Through The Image Data { // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue) temp=texture->imageData; // Temporarily Store The Value At Image Data 'i' texture->imageData = texture->imageData; <span class="cpp-comment">// Set The 1st Byte To The Value Of The 3rd Byte</span><br> texture->imageData = temp; <span class="cpp-comment">// Set The 3rd Byte To The Value In 'temp' (1st Byte Value)</span><br> }<br><br> fclose (file);<br> <br> glGenTextures(<span class="cpp-number">1</span>, &texture[<span class="cpp-number">0</span>].texID); <span class="cpp-comment">// Generate OpenGL texture IDs</span><br><br> glBindTexture(GL_TEXTURE_2D, texture[<span class="cpp-number">0</span>].texID); <span class="cpp-comment">// Bind Our Texture</span><br> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); <span class="cpp-comment">// Linear Filtered</span><br> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); <br> <br> <span class="cpp-keyword">if</span> (texture[<span class="cpp-number">0</span>].bpp==<span class="cpp-number">24</span>) <span class="cpp-comment">// Was The TGA 24 Bits</span><br> {<br> type=GL_RGB; <span class="cpp-comment">// If So Set The 'type' To GL_RGB</span><br> }<br> glTexImage2D(GL_TEXTURE_2D, <span class="cpp-number">0</span>, type, texture[<span class="cpp-number">0</span>].width, texture[<span class="cpp-number">0</span>].height, <span class="cpp-number">0</span>, type, GL_UNSIGNED_BYTE, texture[<span class="cpp-number">0</span>].imageData);<br><br> <span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>; <span class="cpp-comment">// Texture Building Went Ok, Return True</span><br>}<br><br><span class="cpp-keyword">static</span> <span class="cpp-keyword">void</span> quit_tutorial( <span class="cpp-keyword">int</span> code )<br>{<br> SDL_Quit( );<br> glDeleteTextures(<span class="cpp-number">1</span>,&textures.texID);<br> exit( code );<br>}<br><br>GLuint LoadTexturesTGA(<span class="cpp-keyword">char</span>* filename)<br>{<br> glEnable(GL_BLEND);<br> glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);<br> glEnable(GL_ALPHA_TEST);<br> glAlphaFunc(GL_GREATER, <span class="cpp-number">0</span>);<br> <br> GLuint texture;<br> glGenTextures( <span class="cpp-number">1</span>, &texture );<br> glBindTexture( GL_TEXTURE_2D, texture );<br> <br> SDL_Surface *surf = IMG_Load( filename );<br> SDL_LockSurface( surf );<br> gluBuild2DMipmaps( GL_TEXTURE_2D, <span class="cpp-number">3</span>, surf->w, surf->h, GL_BGR, GL_UNSIGNED_BYTE, surf->pixels );<br> SDL_UnlockSurface( surf );<br> SDL_FreeSurface( surf );<br> <br> glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );<br> glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );<br> glBindTexture( GL_TEXTURE_2D, texture );<br> <span class="cpp-keyword">return</span> texture;<br> <br>}<br><br><span class="cpp-keyword">static</span> <span class="cpp-keyword">void</span> handle_key_down( SDL_keysym* keysym )<br>{<br><br> <span class="cpp-keyword">switch</span>( keysym->sym ) {<br> <span class="cpp-keyword">case</span> SDLK_ESCAPE:<br> quit_tutorial(<span class="cpp-number">1</span>);<br> <span class="cpp-keyword">break</span>;<br> <span class="cpp-keyword">default</span>:<br> <span class="cpp-keyword">break</span>;<br> }<br><br>}<br><br><span class="cpp-keyword">static</span> <span class="cpp-keyword">void</span> process_events( <span class="cpp-keyword">void</span> )<br>{<br> <span class="cpp-comment">/* Our SDL event placeholder. */</span><br> SDL_Event event;<br><br> <span class="cpp-comment">/* Grab all the events off the queue. */</span><br> <span class="cpp-keyword">while</span>( SDL_PollEvent( &event ) ) {<br><br> <span class="cpp-keyword">switch</span>( event.type ) {<br> <span class="cpp-keyword">case</span> SDL_KEYDOWN:<br> <span class="cpp-comment">/* Handle key presses. */</span><br> handle_key_down( &event.key.keysym );<br> <span class="cpp-keyword">break</span>;<br> <span class="cpp-keyword">case</span> SDL_QUIT:<br> <span class="cpp-comment">/* Handle quit requests (like Ctrl-c). */</span><br> quit_tutorial( <span class="cpp-number">0</span> );<br> <span class="cpp-keyword">break</span>;<br> }<br><br> }<br><br>}<br><br> <br><span class="cpp-keyword">static</span> <span class="cpp-keyword">void</span> draw_screen( <span class="cpp-keyword">void</span> )<br>{<br><br> glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );<br> glMatrixMode(GL_MODELVIEW);<br> glLoadIdentity();<br> <br> glColor3f(<span class="cpp-number">1</span>.0f,<span class="cpp-number">1</span>.0f,<span class="cpp-number">1</span>.0f);<br> glTranslatef(<span class="cpp-number">0</span>.0f,<span class="cpp-number">0</span>.0f,-<span class="cpp-number">2</span>.5f);<br> <br> glBegin( GL_QUADS );<br> glTexCoord2d(<span class="cpp-number">0</span>.<span class="cpp-number">0</span>,<span class="cpp-number">0</span>.<span class="cpp-number">0</span>); glVertex2d(<span class="cpp-number">0</span>.<span class="cpp-number">0</span>,<span class="cpp-number">0</span>.<span class="cpp-number">0</span>);<br> glTexCoord2d(<span class="cpp-number">1</span>.<span class="cpp-number">0</span>,<span class="cpp-number">0</span>.<span class="cpp-number">0</span>); glVertex2d(<span class="cpp-number">0</span>.<span class="cpp-number">5</span>,<span class="cpp-number">0</span>.<span class="cpp-number">0</span>);<br> glTexCoord2d(<span class="cpp-number">1</span>.<span class="cpp-number">0</span>,<span class="cpp-number">1</span>.<span class="cpp-number">0</span>); glVertex2d(<span class="cpp-number">0</span>.<span class="cpp-number">5</span>,<span class="cpp-number">0</span>.<span class="cpp-number">5</span>);<br> glTexCoord2d(<span class="cpp-number">0</span>.<span class="cpp-number">0</span>,<span class="cpp-number">1</span>.<span class="cpp-number">0</span>); glVertex2d(<span class="cpp-number">0</span>.<span class="cpp-number">0</span>,<span class="cpp-number">0</span>.<span class="cpp-number">5</span>);<br> glEnd();<br> <span class="cpp-comment">/*<br> * Swap the buffers. This tells the driver to<br> * render the next frame from the contents of the<br> * back-buffer, and to set all rendering operations<br> * to occur on what was the front-buffer.<br> *<br> * Double buffering prevents nasty visual tearing<br> * from the application drawing on areas of the<br> * screen that are being updated at the same time.<br> */</span><br> SDL_GL_SwapBuffers( );<br>}<br><br><span class="cpp-keyword">static</span> <span class="cpp-keyword">void</span> setup_opengl( <span class="cpp-keyword">int</span> width, <span class="cpp-keyword">int</span> height )<br>{<br> <span class="cpp-keyword">float</span> ratio = (<span class="cpp-keyword">float</span>) width / (<span class="cpp-keyword">float</span>) height;<br><br> <span class="cpp-comment">/* Our shading model–Gouraud (smooth). */</span><br> glShadeModel( GL_SMOOTH );<br><br> <span class="cpp-comment">/* Culling. */</span><br> glCullFace( GL_BACK );<br> glFrontFace( GL_CCW );<br> glEnable( GL_CULL_FACE );<br> glEnable( GL_DEPTH_TEST );<br> glEnable(GL_TEXTURE_2D);<br> <span class="cpp-comment">/* Set the clear color. */</span><br> glClearColor( <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, <span class="cpp-number">1</span> );<br><br> <span class="cpp-comment">/* Setup our viewport. */</span><br> glViewport( <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, width, height );<br> <br> glEnable(GL_BLEND);<br> glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);<br> glEnable(GL_ALPHA_TEST);<br> glAlphaFunc(GL_GREATER, <span class="cpp-number">0</span>);<br> <br> glMatrixMode(GL_TEXTURE);<br> glScalef(<span class="cpp-number">1</span>.0f, -<span class="cpp-number">1</span>.0f, <span class="cpp-number">1</span>.0f);<br> glMatrixMode(GL_MODELVIEW);<br> <br> glEnable(GL_BLEND);<br> <br> <br> glMatrixMode( GL_PROJECTION );<br> glLoadIdentity( );<br> <span class="cpp-comment">/*<br> * EXERCISE:<br> * Replace this with a call to glFrustum.<br> */</span><br> gluPerspective( <span class="cpp-number">60</span>.<span class="cpp-number">0</span>, ratio, <span class="cpp-number">1</span>.<span class="cpp-number">0</span>, <span class="cpp-number">1024</span>.<span class="cpp-number">0</span> );<br>}<br><br><span class="cpp-keyword">int</span> main( <span class="cpp-keyword">int</span> argc, <span class="cpp-keyword">char</span>* argv[] )<br>{<br> <span class="cpp-comment">/* Information about the current video settings. */</span><br> <span class="cpp-keyword">const</span> SDL_VideoInfo* info = NULL;<br> <span class="cpp-comment">/* Dimensions of our window. */</span><br> <span class="cpp-keyword">int</span> width = <span class="cpp-number">0</span>;<br> <span class="cpp-keyword">int</span> height = <span class="cpp-number">0</span>;<br> <span class="cpp-comment">/* Color depth in bits of our window. */</span><br> <span class="cpp-keyword">int</span> bpp = <span class="cpp-number">0</span>;<br> <span class="cpp-comment">/* Flags we will pass into SDL_SetVideoMode. */</span><br> <span class="cpp-keyword">int</span> flags = <span class="cpp-number">0</span>;<br><br> <span class="cpp-comment">/* First, initialize SDL's video subsystem. */</span><br> <span class="cpp-keyword">if</span>( SDL_Init( SDL_INIT_VIDEO ) < <span class="cpp-number">0</span> ) {<br> <span class="cpp-comment">/* Failed, exit. */</span><br> fprintf( stderr, <span class="cpp-literal">"Video initialization failed: %s\n"</span>,<br> SDL_GetError( ) );<br> quit_tutorial( <span class="cpp-number">1</span> );<br> }<br><br> <span class="cpp-comment">/* Let's get some video information. */</span><br> info = SDL_GetVideoInfo( );<br><br> <span class="cpp-keyword">if</span>( !info ) {<br> <span class="cpp-comment">/* This should probably never happen. */</span><br> fprintf( stderr, <span class="cpp-literal">"Video query failed: %s\n"</span>,<br> SDL_GetError( ) );<br> quit_tutorial( <span class="cpp-number">1</span> );<br> }<br><br> <span class="cpp-comment">/*<br> * Set our width/height to 640/480 (you would<br> * of course let the user decide this in a normal<br> * app). We get the bpp we will request from<br> * the display. On X11, VidMode can't change<br> * resolution, so this is probably being overly<br> * safe. Under Win32, ChangeDisplaySettings<br> * can change the bpp.<br> */</span><br> width = <span class="cpp-number">640</span>;<br> height = <span class="cpp-number">480</span>;<br> bpp = info->vfmt->BitsPerPixel;<br><br> SDL_GL_SetAttribute( SDL_GL_RED_SIZE, <span class="cpp-number">5</span> );<br> SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, <span class="cpp-number">5</span> );<br> SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, <span class="cpp-number">5</span> );<br> SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, <span class="cpp-number">16</span> );<br> SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, <span class="cpp-number">1</span> );<br><br> <span class="cpp-comment">/*<br> * We want to request that SDL provide us<br> * with an OpenGL window, in a fullscreen<br> * video mode.<br> *<br> * EXERCISE:<br> * Make starting windowed an option, and<br> * handle the resize events properly with<br> * glViewport.<br> */</span><br> flags = SDL_OPENGL | SDL_FULLSCREEN;<br><br> <span class="cpp-comment">/*<br> * Set the video mode<br> */</span><br> <span class="cpp-keyword">if</span>( SDL_SetVideoMode( width, height, bpp, flags ) == <span class="cpp-number">0</span> ) {<br> <span class="cpp-comment">/* <br> * This could happen for a variety of reasons,<br> * including DISPLAY not being set, the specified<br> * resolution not being available, etc.<br> */</span><br> fprintf( stderr, <span class="cpp-literal">"Video mode set failed: %s\n"</span>,<br> SDL_GetError( ) );<br> quit_tutorial( <span class="cpp-number">1</span> );<br> }<br><br> <span class="cpp-comment">/*<br> * At this point, we should have a properly setup<br> * double-buffered window for use with OpenGL.<br> */</span><br> setup_opengl( width, height );<br> <br> <span class="cpp-keyword">char</span> file1[<span class="cpp-number">50</span>] = <span class="cpp-literal">"Data/TGA/player_attack_"</span>;<br> <span class="cpp-keyword">char</span> file2[] = <span class="cpp-literal">"east_1.tga"</span>;<br> strcat(file1,file2); <br> LoadTGA(&textures, file1);<br> <span class="cpp-comment">//textures.texID = LoadTexturesTGA(file1); </span><br> <span class="cpp-comment">/*<br> * Now we want to begin our normal app process–<br> * an event loop with a lot of redrawing.<br> */</span><br> <span class="cpp-keyword">while</span>( <span class="cpp-number">1</span> ) {<br> <span class="cpp-comment">/* Process incoming events. */</span><br> process_events( );<br> <span class="cpp-comment">/* Draw the screen. */</span><br> draw_screen( );<br> }<br><br> <span class="cpp-comment">/*<br> * EXERCISE:<br> * Record timings using SDL_GetTicks() and<br> * and print out frames per second at program<br> * end.<br> */</span><br><br> <span class="cpp-comment">/* Never reached. */</span><br> <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br>}<br><br><br></pre></div><!–ENDSCRIPT–><br><br>Still a white rectangle.<br>Note that I have two functions to load textures<br>1) LoadTGA() from NeHe #24<br>2) LoadTexturesTGA() that calls IMG_Load()<br><br>When function 2 is used, the application simply closes, maybe indicating failure to load the image.<br>[crying] [crying]<br>And LoadTGA() shows me a plain white box.<br><br>Are you loading it from IMG_Load()? Could you provide some sample code?<br><br>It doesn't matter where I put: <br><b><br>glEnable(GL_BLEND);<br>glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);<br>glEnable(GL_ALPHA_TEST);<br>glAlphaFunc(GL_GREATER, 0);<br></b><br><b>HELLLLLLLLLLLLLLLLP!!</b><br><br><!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - horizon981 on July 4, 2006 8:37:43 AM]<!–EDIT–></span><!–/EDIT–>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement