alpha channel texture problem

Started by
20 comments, last by Brother Bob 18 years, 1 month ago
hi guys i have a problem ... i can't load a tga file with alpha values in my program and i don't know what is wrong can you hel me? My init function is :
[source c++]void glInit (void)
{
    glEnable (GL_DEPTH_TEST);
    glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
	glEnable (GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    //ShadeModel (GL_FLAT);
	if (!loadTGA ("terreno.tga", 1))
        printf ("texture.tga not found!\n");
	if (!loadTGA ("clouds.tga", 2))
        printf ("texture.tga not found!\n");
	if (!loadTGA ("omino.tga", 3))
        printf ("texture.tga not found!\n");
}
is it right?


and then in the polygon i have


{glColor4f(1,1,1,0);
		glEnable (GL_TEXTURE_2D);
		glBindTexture (GL_TEXTURE_2D, 3);
		glPushMatrix();//OMINO CHE SALTA//
	glLoadIdentity();
	glTranslatef(x_coord,y_salto,0.0);
	
	glBegin(GL_POLYGON);
		glTexCoord2f(0,0);	
		glVertex2f(-0.5, -0.5);
		glTexCoord2f(0,1);
		glVertex2f(-0.5, 2.5);
		glTexCoord2f(1,1);
		glVertex2f(2.5, 2.5);
		glTexCoord2f(1,0);
		glVertex2f(2.5, -0.5);
	glEnd();
	glDisable (GL_TEXTURE_2D);
	glPopMatrix();
	}

where is the problem? thanks [Edited by - BigBoss_Dv on March 14, 2006 4:20:17 PM]
Advertisement
The problem is not in the code you have posted. What about loadTGA?
it's a function of the loader of the tga files... maybe the problem is in the tga file... i have done the alpha channel with pain.net but the loader tga don't load the immage... what program can i use to make a tga with alpha channel?
please some on can say to me an easy but good textture loader for opengl pleaseeeeeeeeeeeeeeeeee
Well, my reply implicitly asked for the code for loadTGA.
/*    Copyright (C) 1999    For non-commercial use only.    File	: tga.c    Date	: 05/05/1999    Author	: Nate Miller    Contact	: vandals1@home.com    Change Log    **********    7/26/99 - added code to fclose () a file when there is an error    6/11/99 - added support for 8bit images, changed commenting*/#include "tga.h"#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;#include &lt;string.h&gt;#include &lt;windows.h&gt;#include &lt;gl/gl.h&gt;GLenum texFormat;/*=============checkSizeMake sure its a power of 2.=============*/int checkSize (int x){    if (x == 2	 || x == 4 ||         x == 8	 || x == 16 ||         x == 32  || x == 64 ||        x == 128 || x == 256 || x == 512)        return 1;    else return 0;}/*=============getRGBAReads in RGBA data for a 32bit image. =============*/unsigned char *getRGBA (FILE *s, int size){    unsigned char *rgba;    unsigned char temp;    int bread;    int i;    rgba = (unsigned char*) malloc (size * 4);     if (rgba == NULL)        return 0;    bread = fread (rgba, sizeof (unsigned char), size * 4, s);     /* TGA is stored in BGRA, make it RGBA */    if (bread != size * 4)    {        free (rgba);        return 0;    }    for (i = 0; i &lt; size * 4; i += 4 )    {        temp = rgba;        rgba = rgba;<br>        rgba = temp;<br>    }<br><br>    texFormat = GL_RGBA;<br>    <span class="cpp-keyword">return</span> rgba;<br>}<br><span class="cpp-comment">/*<br>=============<br>getRGB<br><br>Reads in RGB data for a 24bit image. <br>=============<br>*/</span><br><span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> *getRGB (FILE *s, <span class="cpp-keyword">int</span> size)<br>{<br>    <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> *rgb;<br>    <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> temp;<br>    <span class="cpp-keyword">int</span> bread;<br>    <span class="cpp-keyword">int</span> i;<br><br>    rgb = (<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span>*) malloc (size * <span class="cpp-number">3</span>); <br><br>    <span class="cpp-keyword">if</span> (rgb == NULL)<br>        <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br><br>    bread = fread (rgb, <span class="cpp-keyword">sizeof</span> (<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span>), size * <span class="cpp-number">3</span>, s);<br><br>    <span class="cpp-keyword">if</span> (bread != size * <span class="cpp-number">3</span>)<br>    {<br>        free (rgb);<br>        <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br>    }<br><br>    <span class="cpp-comment">/* TGA is stored in BGR, make it RGB */</span><br>    <span class="cpp-keyword">for</span> (i = <span class="cpp-number">0</span>; i &amp;lt; size * <span class="cpp-number">3</span>; i += <span class="cpp-number">3</span>)<br>    {<br>        temp = rgb<span style="font-weight:bold;">;<br>        rgb<span style="font-weight:bold;"> = rgb;<br>        rgb = temp;<br>    }<br><br>    texFormat = GL_RGB;<br><br>    <span class="cpp-keyword">return</span> rgb;<br>}<br><span class="cpp-comment">/*<br>=============<br>getGray<br><br>Gets the grayscale image data.  Used as an alpha channel.<br>=============<br>*/</span><br><span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> *getGray (FILE *s, <span class="cpp-keyword">int</span> size)<br>{<br>    <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> *grayData;<br>    <span class="cpp-keyword">int</span> bread;<br><br>    grayData = (<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span>*) malloc (size);<br><br>    <span class="cpp-keyword">if</span> (grayData == NULL)<br>        <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br><br>    bread = fread (grayData, <span class="cpp-keyword">sizeof</span> (<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span>), size, s);<br><br>    <span class="cpp-keyword">if</span> (bread != size)<br>    {<br>        free (grayData);<br>        <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br>    }<br><br>    texFormat = GL_ALPHA;<br><br>    <span class="cpp-keyword">return</span> grayData;<br>}<br><span class="cpp-comment">/*<br>=============<br>getData<br><br>Gets the image data for the specified bit depth.<br>=============<br>*/</span><br><span class="cpp-keyword">char</span> *getData (FILE *s, <span class="cpp-keyword">int</span> sz, <span class="cpp-keyword">int</span> iBits)<br>{<br>    <span class="cpp-keyword">if</span> (iBits == <span class="cpp-number">32</span>)<br>        <span class="cpp-keyword">return</span> (<span class="cpp-keyword">char</span>*) getRGBA (s, sz);<br>    <span class="cpp-keyword">else</span> <span class="cpp-keyword">if</span> (iBits == <span class="cpp-number">24</span>)<br>        <span class="cpp-keyword">return</span> ( <span class="cpp-keyword">char</span>*) getRGB (s, sz);	<br>    <span class="cpp-keyword">else</span> <span class="cpp-keyword">if</span> (iBits == <span class="cpp-number">8</span>)<br>        <span class="cpp-keyword">return</span> ( <span class="cpp-keyword">char</span>*) getGray (s, sz);<br>}<br><span class="cpp-comment">/*<br>=============<br>returnError<br>=============<br>Called when there is an error loading the .tga file.<br>*/</span><br><span class="cpp-keyword">int</span> returnError (FILE *s, <span class="cpp-keyword">int</span> error)<br>{<br>    fclose (s);<br>    <span class="cpp-keyword">return</span> error;<br>}<br><span class="cpp-comment">/*<br>=============<br>loadTGA<br><br>Loads up a targa file.  Supported types are 8,24 and 32 uncompressed images.  <br>id is the texture ID to bind too.<br>=============<br>*/</span><br><span class="cpp-keyword">int</span> loadTGA (<span class="cpp-keyword">char</span> *name, <span class="cpp-keyword">int</span> id)<br>{<br>    <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> type[<span class="cpp-number">4</span>];<br>    <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> info[<span class="cpp-number">7</span>];<br>    <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> *imageData = NULL;<br>    <span class="cpp-keyword">int</span> imageWidth, imageHeight;<br>    <span class="cpp-keyword">int</span> imageBits, size;<br>    FILE *s;<br><br>    <span class="cpp-keyword">if</span> (!(s = fopen (name, <span class="cpp-literal">"r+bt"</span>)))<br>        <span class="cpp-keyword">return</span> TGA_FILE_NOT_FOUND;<br><br>    fread (&amp;type, <span class="cpp-keyword">sizeof</span> (<span class="cpp-keyword">char</span>), <span class="cpp-number">3</span>, s); <span class="cpp-comment">// read in colormap info and image type, byte 0 ignored</span><br>    fseek (s, <span class="cpp-number">12</span>, SEEK_SET);			<span class="cpp-comment">// seek past the header and useless info</span><br>    fread (&amp;info, <span class="cpp-keyword">sizeof</span> (<span class="cpp-keyword">char</span>), <span class="cpp-number">6</span>, s);<br><br>    <span class="cpp-keyword">if</span> (type[<span class="cpp-number">1</span>] != <span class="cpp-number">0</span> || (type[<span class="cpp-number">2</span>] != <span class="cpp-number">2</span> &amp;&amp; type[<span class="cpp-number">2</span>] != <span class="cpp-number">3</span>))<br>        returnError (s, TGA_BAD_IMAGE_TYPE);<br><br>    imageWidth = info[<span class="cpp-number">0</span>] + info[<span class="cpp-number">1</span>] * <span class="cpp-number">256</span>; <br>    imageHeight = info[<span class="cpp-number">2</span>] + info[<span class="cpp-number">3</span>] * <span class="cpp-number">256</span>;<br>    imageBits =	info[<span class="cpp-number">4</span>]; <br><br>    size = imageWidth * imageHeight; <br><br>    <span class="cpp-comment">/* make sure dimension is a power of 2 */</span><br>    <span class="cpp-keyword">if</span> (!checkSize (imageWidth) || !checkSize (imageHeight))<br>        returnError (s, TGA_BAD_DIMENSION);<br><br>    <span class="cpp-comment">/* make sure we are loading a supported type */</span><br>    <span class="cpp-keyword">if</span> (imageBits != <span class="cpp-number">32</span> &amp;&amp; imageBits != <span class="cpp-number">24</span> &amp;&amp; imageBits != <span class="cpp-number">8</span>)<br>        returnError (s, TGA_BAD_BITS);<br><br>    imageData = (<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> *) getData (s, size, imageBits);<br><br>    <span class="cpp-comment">/* no image data */</span><br>    <span class="cpp-keyword">if</span> (imageData == NULL)<br>        returnError (s, TGA_BAD_DATA);<br><br>    fclose (s);<br><br>    glBindTexture (GL_TEXTURE_2D, id);<br>    glPixelStorei (GL_UNPACK_ALIGNMENT, <span class="cpp-number">1</span>);<br>    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);<br>    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);<br>    <span class="cpp-comment">/* glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); */</span><br>    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);<br>    <span class="cpp-comment">/* glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); */</span><br>    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);<br>    glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);<br>    glTexImage2D (GL_TEXTURE_2D, <span class="cpp-number">0</span>, texFormat, imageWidth, imageHeight, <span class="cpp-number">0</span>, texFormat, GL_UNSIGNED_BYTE, imageData);<br><br>    <span class="cpp-comment">/* release data, its been uploaded */</span><br>    free (imageData);<br><br>    <span class="cpp-keyword">return</span> <span class="cpp-number">1</span>;<br>}<br></pre></div><!–ENDSCRIPT–><br><br><br><!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - BigBoss_Dv on March 14, 2006 12:40:54 AM]<!–EDIT–></span><!–/EDIT–>
Edit your post and post the code inside source-tags. See the FAQ for more information on how to do that. Pesonally, I tent to just ignore posts that just dumps poorly formatted code in the post like that.

Looks like a pre-made function though, so likely the file itself that is wrong. But as I said, I haven't bothered looking much at the code in the way it is now.
here it is :)
Can't see anything wrong with the TGA loader itself. What do you actually get? Correct RGB but wrong A, or no texture at all? Do you know for sure the image is correct? Tried debugging the code to see if it fails somewhere? It is just RGBA images that fails, or does RGB images fail aswell?
only rgba fails... i have tried with this other loader but either this doesn't work with alpha channel
[source c++]/* * PNG loader library for OpenGL v1.45 (10/07/00) * by Ben Wyatt ben@wyatt100.freeserve.co.uk * Using LibPNG 1.0.2 and ZLib 1.1.3 * * This software is provided 'as-is', without any express or implied warranty. * In no event will the author be held liable for any damages arising from the * use of this software. * * Permission is hereby granted to use, copy, modify, and distribute this * source code, or portions hereof, for any purpose, without fee, subject to * the following restrictions: * * 1. The origin of this source code must not be misrepresented. You must not *    claim that you wrote the original software. If you use this software in *    a product, an acknowledgment in the product documentation would be *    appreciated but is not required. * 2. Altered versions must be plainly marked as such and must not be *    misrepresented as being the original source. * 3. This notice must not be removed or altered from any source distribution. */#ifndef _GLPNG_H_#define _GLPNG_H_#include <stdio.h>#ifdef __cplusplusextern "C" {#endif#ifdef _MSC_VER	#ifdef _DEBUG		#pragma comment (lib, "glpngd.lib")	#else		#pragma comment (lib, "glpng.lib")	#endif#endif/* XXX This is from Win32's <windef.h> */#ifndef APIENTRY	#if (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED)		#define APIENTRY    __stdcall	#else		#define APIENTRY	#endif#endif/* Mipmapping parameters */#define PNG_NOMIPMAPS      0 /* No mipmapping                        */#define PNG_BUILDMIPMAPS  -1 /* Calls a clone of gluBuild2DMipmaps() */#define PNG_SIMPLEMIPMAPS -2 /* Generates mipmaps without filtering  *//* Who needs an "S" anyway? */#define PNG_NOMIPMAP     PNG_NOMIPMAPS#define PNG_BUILDMIPMAP  PNG_BUILDMIPMAPS#define PNG_SIMPLEMIPMAP PNG_SIMPLEMIPMAPS/* Transparency parameters */#define PNG_CALLBACK  -3 /* Call the callback function to generate alpha   */#define PNG_ALPHA     -2 /* Use alpha channel in PNG file, if there is one */#define PNG_SOLID     -1 /* No transparency                                */#define PNG_STENCIL    0 /* Sets alpha to 0 for r=g=b=0, 1 otherwise       */#define PNG_BLEND1     1 /* a = r+g+b                                      */#define PNG_BLEND2     2 /* a = (r+g+b)/2                                  */#define PNG_BLEND3     3 /* a = (r+g+b)/3                                  */#define PNG_BLEND4     4 /* a = r*r+g*g+b*b                                */#define PNG_BLEND5     5 /* a = (r*r+g*g+b*b)/2                            */#define PNG_BLEND6     6 /* a = (r*r+g*g+b*b)/3                            */#define PNG_BLEND7     7 /* a = (r*r+g*g+b*b)/4                            */#define PNG_BLEND8     8 /* a = sqrt(r*r+g*g+b*b)                          */typedef struct {	unsigned int Width;	unsigned int Height;	unsigned int Depth;	unsigned int Alpha;} pngInfo;typedef struct {	unsigned int Width;	unsigned int Height;	unsigned int Depth;	unsigned int Alpha;	unsigned int Components;	unsigned char *Data;	unsigned char *Palette;} pngRawInfo;extern int APIENTRY pngLoadRaw(const char *filename, pngRawInfo *rawinfo);extern int APIENTRY pngLoadRawF(FILE *file, pngRawInfo *rawinfo);extern int APIENTRY pngLoad(const char *filename, int mipmap, int trans, pngInfo *info);extern int APIENTRY pngLoadF(FILE *file, int mipmap, int trans, pngInfo *info);extern unsigned int APIENTRY pngBind(const char *filename, int mipmap, int trans, pngInfo *info, int wrapst, int minfilter, int magfilter);extern unsigned int APIENTRY pngBindF(FILE *file, int mipmap, int trans, pngInfo *info, int wrapst, int minfilter, int magfilter);extern void APIENTRY pngSetStencil(unsigned char red, unsigned char green, unsigned char blue);extern void APIENTRY pngSetAlphaCallback(unsigned char (*callback)(unsigned char red, unsigned char green, unsigned char blue));extern void APIENTRY pngSetViewingGamma(double viewingGamma);extern void APIENTRY pngSetStandardOrientation(int standardorientation);#ifdef __cplusplus}#endif#endif


This topic is closed to new replies.

Advertisement