Sign in to follow this  
BigBoss_Dv

alpha channel texture problem

Recommended Posts

BigBoss_Dv    122
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]

Share this post


Link to post
Share on other sites
BigBoss_Dv    122
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?

Share this post


Link to post
Share on other sites
BigBoss_Dv    122

/*
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <gl/gl.h>

GLenum texFormat;

/*
=============
checkSize

Make 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;
}
/*
=============
getRGBA

Reads 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 < size * 4; i += 4 )
{
temp = rgba[i];
rgba[i] = rgba[i + 2];
rgba[i + 2] = temp;
}

texFormat = GL_RGBA;
return rgba;
}
/*
=============
getRGB

Reads in RGB data for a 24bit image.
=============
*/

unsigned char *getRGB (FILE *s, int size)
{
unsigned char *rgb;
unsigned char temp;
int bread;
int i;

rgb = (unsigned char*) malloc (size * 3);

if (rgb == NULL)
return 0;

bread = fread (rgb, sizeof (unsigned char), size * 3, s);

if (bread != size * 3)
{
free (rgb);
return 0;
}

/* TGA is stored in BGR, make it RGB */
for (i = 0; i < size * 3; i += 3)
{
temp = rgb[i];
rgb[i] = rgb[i + 2];
rgb[i + 2] = temp;
}

texFormat = GL_RGB;

return rgb;
}
/*
=============
getGray

Gets the grayscale image data. Used as an alpha channel.
=============
*/

unsigned char *getGray (FILE *s, int size)
{
unsigned char *grayData;
int bread;

grayData = (unsigned char*) malloc (size);

if (grayData == NULL)
return 0;

bread = fread (grayData, sizeof (unsigned char), size, s);

if (bread != size)
{
free (grayData);
return 0;
}

texFormat = GL_ALPHA;

return grayData;
}
/*
=============
getData

Gets the image data for the specified bit depth.
=============
*/

char *getData (FILE *s, int sz, int iBits)
{
if (iBits == 32)
return (char*) getRGBA (s, sz);
else if (iBits == 24)
return ( char*) getRGB (s, sz);
else if (iBits == 8)
return ( char*) getGray (s, sz);
}
/*
=============
returnError
=============
Called when there is an error loading the .tga file.
*/

int returnError (FILE *s, int error)
{
fclose (s);
return error;
}
/*
=============
loadTGA

Loads up a targa file. Supported types are 8,24 and 32 uncompressed images.
id is the texture ID to bind too.
=============
*/

int loadTGA (char *name, int id)
{
unsigned char type[4];
unsigned char info[7];
unsigned char *imageData = NULL;
int imageWidth, imageHeight;
int imageBits, size;
FILE *s;

if (!(s = fopen (name, "r+bt")))
return TGA_FILE_NOT_FOUND;

fread (&type, sizeof (char), 3, s); // read in colormap info and image type, byte 0 ignored
fseek (s, 12, SEEK_SET); // seek past the header and useless info
fread (&info, sizeof (char), 6, s);

if (type[1] != 0 || (type[2] != 2 && type[2] != 3))
returnError (s, TGA_BAD_IMAGE_TYPE);

imageWidth = info[0] + info[1] * 256;
imageHeight = info[2] + info[3] * 256;
imageBits = info[4];

size = imageWidth * imageHeight;

/* make sure dimension is a power of 2 */
if (!checkSize (imageWidth) || !checkSize (imageHeight))
returnError (s, TGA_BAD_DIMENSION);

/* make sure we are loading a supported type */
if (imageBits != 32 && imageBits != 24 && imageBits != 8)
returnError (s, TGA_BAD_BITS);

imageData = (unsigned char *) getData (s, size, imageBits);

/* no image data */
if (imageData == NULL)
returnError (s, TGA_BAD_DATA);

fclose (s);

glBindTexture (GL_TEXTURE_2D, id);
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
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_NEAREST); */
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); */
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D (GL_TEXTURE_2D, 0, texFormat, imageWidth, imageHeight, 0, texFormat, GL_UNSIGNED_BYTE, imageData);

/* release data, its been uploaded */
free (imageData);

return 1;
}



[Edited by - BigBoss_Dv on March 14, 2006 12:40:54 AM]

Share this post


Link to post
Share on other sites
Brother Bob    10344
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.

Share this post


Link to post
Share on other sites
Brother Bob    10344
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?

Share this post


Link to post
Share on other sites
BigBoss_Dv    122
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 __cplusplus
extern "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




Share this post


Link to post
Share on other sites
Brother Bob    10344
Hmm, only now did I notice a small detail; in your original code you set the primary color to white with alpha to zero. Default texture combiner function is GL_MODULATE which multiplies the primary and texture color. Since the primary alpha is zero, the resulting alpha is always going to be zero. Well, try figure out what that means given the blending function you use...

But why this would work for non-RGBA, I don't know. Should produce the same result for RGB also. Anyway, if this isn't the problem, you must provide more information. Other than posting some third-party code, you really haven't said anything other than "it doesn't work".

Share this post


Link to post
Share on other sites
BigBoss_Dv    122
Quote:
Original post by Brother Bob
Hmm, only now did I notice a small detail; in your original code you set the primary color to white with alpha to zero. Default texture combiner function is GL_MODULATE which multiplies the primary and texture color. Since the primary alpha is zero, the resulting alpha is always going to be zero. Well, try figure out what that means given the blending function you use...

But why this would work for non-RGBA, I don't know. Should produce the same result for RGB also. Anyway, if this isn't the problem, you must provide more information. Other than posting some third-party code, you really haven't said anything other than "it doesn't work".


now i have set the value of the color of the polygon at:
[source c++]
glEnable (GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, texture2);
glPushMatrix();//OMINO CHE SALTA//
glLoadIdentity();
glTranslatef(x_coord,y_salto,0.0);
glColor4f(0.2, 0.3, 0.4,0.2);
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();
}




and in my initi function i have set :

[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);
glClearColor (1.0, 1.0, 1.0, 0.0);

texture1 = pngBind("clouds.png", PNG_NOMIPMAP, PNG_ALPHA, &info1, GL_CLAMP, GL_NEAREST, GL_NEAREST);
texture2 = pngBind("omino.png", PNG_NOMIPMAP, PNG_ALPHA, &info2, GL_CLAMP, GL_NEAREST, GL_NEAREST);
texture3 = pngBind("terreno2.png", PNG_NOMIPMAP, PNG_ALPHA, &info3, GL_CLAMP, GL_NEAREST, GL_NEAREST);



I don't use blending function because i'm trying to use texture with alpha channel already set.... the texture is correct i have already seen so for some kind of mistery the texture is always opaque and if i try to print the value of the texture that i load with this instructions ( printf("depth->%d\n", info2.Depth); printf("alpha->%d\n", info2.Alpha);) the values of both are 8. is it correct? sorry for my bad english :) and thanks for the help

Share this post


Link to post
Share on other sites
Brother Bob    10344
If this is all code there is related to OpenGL, then I really don't know what could be wrong. Is it possible for you to post an entire application demonstrating the problem, including full source, image, the PNG loader; everything needed to compile and run the program?

Share this post


Link to post
Share on other sites
BigBoss_Dv    122
OK here it is my entire source program that i wrote:
[source c++]
//SPARO by Francesco Portoso & Davide Casalena

/* Da implementare
nome gioco
vite
piattaforme
cercare texture rulez <--------------------------------------------- IMPORTANTE
velocita random nemici
nuovo loader e trasparenze texture <------------------------------------------------------ IMPORTANTE
skermate di avvio e indicatori vita e punteggio
mostro finale?
livelli?
*/


#include <stdlib.h>
#include <time.h>
#include <GL/glut.h>
#include <stdio.h>
#include "glpng.h"
#include <string.h>

float x_coord = -13.0, y_temp=0;
float y_coord = -9.5;
float x_sfondo;
float x_sfondo2=30;
float y_sfondo;
int salto=0;
pngInfo info1;
pngInfo info2;
pngInfo info3;
float x=0,b,c;
float y_salto=-9.5;
int n=0, a=0,s=0,p=0;
int colpito_n1,colpito_n2,dx=0;
#define MAX_S 40
//#define RAND_MAX 10
float x_n1,x_n2;
int morto;
float vel2=0.0785;
GLuint texture1;
GLuint texture2;
GLuint texture3;

struct StrutturaS{
float x_sparo;
float y_sparo;
int salto2;
int sparo;

//int visibile;
//int colpito;
// float distanza;
}Spari[MAX_S]; //vettore di spari
int ContatoreSpari=0;//conto le volte che sparo

void *font = GLUT_BITMAP_TIMES_ROMAN_24;

void output(int x, int y, char *string)
{
int len, i;

glRasterPos3f(x, y,1);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
glutPostRedisplay();
}

void funz2 (int value)
{
colpito_n1=0;
}

void funz3 (int value)
{
morto=0;
}

void funz4 (int value)
{
colpito_n2=0;
// vel2=(rand())/1000+0.01;
printf("%f\n",vel2);
}

void idle()
{


if(x_coord>=6 && dx==1)
{
if(x_sfondo>=-30) //funzione x ciclare lo sfondo
{ x_sfondo-=0.0725;
//glutPostRedisplay();

}
else
x_sfondo=30;

if(x_sfondo2>=-30)
{
x_sfondo2-=0.0725;
//glutPostRedisplay();
}
else
x_sfondo2=30;

}

if(x_n1>-18 && colpito_n1==0) //funzione x far spostare il nemico1
{

x_n1-=0.0925;
if(x_n1+3> x_coord && x_n1 < x_coord && y_coord==-9.5 && y_salto==0) //controllo morte omino
{ morto=1;
glutTimerFunc(1000,funz3,1);

}
}
else
x_n1=20;

if(x_n2>-18 && colpito_n2==0) //funzione x far spostare il nemico2
{

x_n2-=vel2; //0.0185;
if(x_n2+3> x_coord && x_n2 < x_coord && y_coord==-9.5 && y_salto==0) //controllo morte omino
{ morto=1;
glutTimerFunc(1000,funz3,1);

}
}
else
x_n2=25;

for(s=0;s<MAX_S;s++){ //FUNZIONE XSPARO//
if(Spari[s].sparo == 1 && Spari[s].x_sparo < 16 )
{

Spari[s].x_sparo += 0.425; //0.425
//

if(Spari[s].x_sparo>x_n1 && Spari[s].x_sparo<x_n1+2 && Spari[s].y_sparo<=-9.3 ) //controllo colpito nemico 1
{
//printf("%f\n",Spari[s].y_sparo);

colpito_n1=1;
glutTimerFunc(100,funz2,1);
}

if(Spari[s].x_sparo>x_n2 && Spari[s].x_sparo<x_n2+2 && Spari[s].y_sparo<=-9.3 )//controllo colpito nemico 2
{
//printf("%f\n",Spari[s].y_sparo);

colpito_n2=1;
glutTimerFunc(100,funz4,1);
}


}

if(Spari[s].x_sparo > 16 || Spari[s].x_sparo>x_n1 && Spari[s].x_sparo<x_n1+2 && Spari[s].y_sparo<=-9.3 ) //x riazzerare lo sparo qndo colpisco il nemico1
{
Spari[s].sparo = 0;
}

if(Spari[s].x_sparo > 16 || Spari[s].x_sparo>x_n2 && Spari[s].x_sparo<x_n2+2 && Spari[s].y_sparo<=-9.3 ) //x riazzerare lo sparo qndo colpisco il nemico2
{
Spari[s].sparo = 0;
}

//printf("%d\n",salto);
// colpito_n1=0;
}
if(salto == 1 && y_salto < 15 && y_salto>=-9.5) //FUNZIONE SALTO//
{
y_salto = (-1*x*x + 22.5*x )/18;
x += 0.275;//0.275;
if(y_salto<= -9.5)
salto=0;
//printf("%f\n",y_salto);
//Spari[s].x_sparo += 0.425;
//glutPostRedisplay();
}
else
{
x=-5.999999;
y_salto=0;

}





//printf("%d\n",Spari[ContatoreSpari].salto2);

// dx=0;
glutPostRedisplay();
}



void display(void)
{
//c=clock();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT);

//dx=0;


for(ContatoreSpari=0;ContatoreSpari<MAX_S;ContatoreSpari++){
if (Spari[ContatoreSpari].sparo == 1)
{

glColor3f(1,0,0);
glPushMatrix();
glLoadIdentity();

if (Spari[ContatoreSpari].salto2 == 1 )//SPARO SALTANDO//
// a=1;
{
glTranslatef(Spari[ContatoreSpari].x_sparo,Spari[ContatoreSpari].y_sparo+2,0.0);
//printf("y= %f\n",Spari[ContatoreSpari].y_sparo);
//printf("x= %f\n",Spari[ContatoreSpari].x_sparo);
}
else
glTranslatef(Spari[ContatoreSpari].x_sparo,y_coord+2,0.0);//SPARO A TERRA//

glBegin(GL_POLYGON);
glutSolidSphere(0.2, 20, 20);
glEnd();
glPopMatrix();




}
}

if(colpito_n1==0 ) //NEMICO1//
{
//printf("%f\n",Spari[ContatoreSpari].x_sparo);
glPushMatrix();
glLoadIdentity();
glTranslatef(x_n1,-9.5,0.0);
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 2.5);
glVertex2f(2.5, 2.5);
glVertex2f(2.5, -0.5);
glEnd();
glPopMatrix();

}


if(colpito_n2==0 ) //NEMICO2//
{
//printf("%f\n",Spari[ContatoreSpari].x_sparo);
glPushMatrix();
glLoadIdentity();
glTranslatef(x_n2,-9.5,0.0);
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 2.5);
glVertex2f(2.5, 2.5);
glVertex2f(2.5, -0.5);
glEnd();
glPopMatrix();

}



if(morto==0){
if(salto == 1)
{
glEnable (GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, texture2);
glPushMatrix();//OMINO CHE SALTA//
glLoadIdentity();
glTranslatef(x_coord,y_salto,0.0);
glColor4f(0.2, 0.3, 0.4,0.2);
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();
}
else
{


// pngSetAlphaCallback(alpha_callback);
glEnable (GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, texture2);

printf("depth->%d\n", info2.Depth);
printf("alpha->%d\n", info2.Alpha);

glPushMatrix();//OMINO CHE non SALTA//
glLoadIdentity();
glTranslatef(x_coord,y_coord,0.0);
glColor4f(0.2, 0.3, 0.4,0.2);
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();
}
}
glColor3f(1, 1, 1);

glEnable (GL_TEXTURE_2D); /* enable texture mapping TERRENO */
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glBindTexture (GL_TEXTURE_2D, texture3);
glPushMatrix();
glLoadIdentity();
glTranslatef(x_sfondo,y_sfondo,0.0);
glBegin(GL_POLYGON);
glTexCoord3f(-1,-1,-0.5);
glVertex3f(-15.0, -15.0,-0.5);
glTexCoord3f(3,-1,-0.5);
glVertex3f(-15.0, -10.0,-0.5);
glTexCoord3f(3,3,-0.5);
glVertex3f(15.2, -10.0,-0.5);
glTexCoord3f(-1,3,-0.5);
glVertex3f(15.2, -15.0,-0.5);

glEnd();
glDisable (GL_TEXTURE_2D);
glPopMatrix();

glEnable (GL_TEXTURE_2D); /* enable texture mapping TERRENO2 */
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glBindTexture (GL_TEXTURE_2D, texture3);
glPushMatrix();
glLoadIdentity();
glTranslatef(x_sfondo2,y_sfondo,0.0);
glBegin(GL_POLYGON);
glTexCoord3f(-1,-1,-0.5);
glVertex3f(-15.0, -15.0,-0.5);
glTexCoord3f(3,-1,-0.5);
glVertex3f(-15.0, -10.0,-0.5);
glTexCoord3f(3,3,-0.5);
glVertex3f(15.2, -10.0,-0.5);
glTexCoord3f(-1,3,-0.5);
glVertex3f(15.2, -15.0,-0.5);

glEnd();
glDisable (GL_TEXTURE_2D);
glPopMatrix();

glColor3f(1, 1, 1);

glEnable (GL_TEXTURE_2D); /* enable texture mapping SFONDO */
glPushMatrix();
glLoadIdentity();
glBindTexture (GL_TEXTURE_2D, texture1);
glTranslatef(x_sfondo,y_sfondo,0.0);

glBegin(GL_POLYGON);
glTexCoord3f(0,0,-0.5);
glVertex3f(-15.0, -10.0,-0.5);
glTexCoord3f(0,1,-0.5);
glVertex3f(-15.0, +15.0,-0.5);
glTexCoord3f(1,1,-0.5);
glVertex3f(15.1, 15.0,-0.5);
glTexCoord3f(1,0,-0.5);
glVertex3f(15.1, -10.0,-0.5);
glEnd();
glDisable (GL_TEXTURE_2D);
glPopMatrix();

glColor3f(1, 1, 1);
//texture1 = pngBind("terra.png", PNG_NOMIPMAP, PNG_ALPHA, &info1, GL_CLAMP, GL_NEAREST, GL_NEAREST);
glEnable (GL_TEXTURE_2D); /* enable texture mapping SFONDO2 */
glPushMatrix();
glLoadIdentity();
glBindTexture (GL_TEXTURE_2D, texture1);
glTranslatef(x_sfondo2,y_sfondo,0.0);

glBegin(GL_POLYGON);
glTexCoord3f(0,0,-0.5);
glVertex3f(-15.0, -10.0,-0.5);
glTexCoord3f(0,1,-0.5);
glVertex3f(-15.0, +15.0,-0.5);
glTexCoord3f(1,1,-0.5);
glVertex3f(15.1, 15.0,-0.5);
glTexCoord3f(1,0,-0.5);
glVertex3f(15.1, -10.0,-0.5);
glEnd();
glDisable (GL_TEXTURE_2D);
glPopMatrix();




output(100, 100, "This is written in a GLUT bitmap font.");


glutSwapBuffers();

//b=clock();
//while(b-c<10)
//b=clock();
}

void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (-15.0, 15.0,-15.0,15.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
}

void funz1(int value)
{
dx=0;
}

void keyboard (unsigned char key, int x, int y)
{
if(morto==0){
switch (key) {
case 27:
exit(1);
break;



case 'd': //AVANTI//
dx=1;
glutTimerFunc(10,funz1,1);
if (x_coord < 6)
{


x_coord += 0.425;

//dx=1;
//
glutPostRedisplay();




}
break;


case 'a'://INDIETRO//
if (x_coord > -14.5)
{
x_coord -= 0.425;
glutPostRedisplay();
}
break;

case 'w'://SALTO//
if (y_coord < 13.5)
{
salto= 1;
y_salto = y_coord;
}
break;




case ' '://SPARO//
//if(ContatoreSpari<MAX_S)
//{
//sound;
//printf("\a");
for(ContatoreSpari=0;ContatoreSpari<MAX_S;ContatoreSpari++)
{printf("contatore spari: %d\n",ContatoreSpari);
if (Spari[ContatoreSpari].sparo==0)
{
Spari[ContatoreSpari].x_sparo=x_coord+2.3;
Spari[ContatoreSpari].sparo = 1;
if(salto==1)
{
Spari[ContatoreSpari].salto2=1;
Spari[ContatoreSpari].y_sparo = y_salto;
break;
}
else
{
Spari[ContatoreSpari].y_sparo = y_coord;
Spari[ContatoreSpari].salto2=0;
break;
}
}



}



break;

default:
break;
}
}
}


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);
glClearColor (1.0, 1.0, 1.0, 0.0);

texture1 = pngBind("clouds.png", PNG_NOMIPMAP, PNG_ALPHA, &info1, GL_CLAMP, GL_NEAREST, GL_NEAREST);
texture2 = pngBind("omino.png", PNG_NOMIPMAP, PNG_ALPHA, &info2, GL_CLAMP, GL_NEAREST, GL_NEAREST);
texture3 = pngBind("terreno2.png", PNG_NOMIPMAP, PNG_ALPHA, &info3, GL_CLAMP, GL_NEAREST, GL_NEAREST);
// if (!loadTGA ("terreno.tga", 1))
// printf ("texture.tga not found!\n");
//if (!loadTGA ("clouds.tga", 2))
// printf ("texture.tga not found!\n");
}

int main(int argc, char** argv)
{
// x=0;

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (700, 700);
glutCreateWindow("Example 10");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
glInit ();
glutMainLoop();

return 0;
}



and the source of the loader png i have already posted before in my post.... the loader have also 2 library files called glpng.lib and glpngd that i have already included at the project... if you can't find the problem could you suggest my a new way (some easy loader) from zero to load texture with alpha channel that you have test and works... To compile i use microsoft visual c++ 6.0. Thanks for your help :)



Share this post


Link to post
Share on other sites
Brother Bob    10344
OK, I think I know what's wrong; you're drawing the objects in the wrong order.

First you draw that jumping object which is supposed to be transparent in some parts. Blending works as it should, and the transparent parts blend with the background since that's what is in the frame buffer when drawing it. Then you draw background, but since the background is drawn further away, it will not draw where the jumping object was drawn.

So what you end up with is a white background around the jumping object, since that's what was being the jumping object when it was drawn. Blending works perfectly, but it just doesn't blend with what you want it to blend with.

Blending somtimes, as in this case, requires great care when it comes to drawing order. You must draw your objects from back to front, meaning first background, then the middle objects, and then the closest objects.

Share this post


Link to post
Share on other sites
BigBoss_Dv    122
Another one question.... I want to write on the screen some sentences and as you can see from the source i use this function:

[source c++]void *font = GLUT_BITMAP_TIMES_ROMAN_24;

void output(int x, int y, char *string)
{
int len, i;

glRasterPos3f(x, y,1);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
glutPostRedisplay();
}


but i can't see the words when i use "output(100, 100, "This is written in a GLUT bitmap font.");" in the display ... i have try to force the z dimension of this instruction to 1 when the rest is draw on 0( glRasterPos3f(x, y,1); ) but i still can't see the words ... where is the problem? thank you again for your help

Share this post


Link to post
Share on other sites
Brother Bob    10344
The coordinate you pass to glRasterPos is treated like any other vertex and will pass through the modelview matrix, the projection matrix, clipping stage, and so on. If you want to place the raster position at a certain spot on the screen, you have to make sure that all transformations acting on the raster position will, at the end, place it on particular spot on the screen.

This means you need some special modelview and projection matrices. Nothing difficult, but you still have to make sure you have the correct matrices. Instead, I suggest you use glWindowPos instead. It places the raster position directly in screen space instead of in model space.

Check the FAQ for more information. It talks about newer versions of OpenGL, and that's the theory you need to get glWindowPos to work.
BigBoss_Dv    122
in my source there are this instrucions:
[source c++]
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (-15.0, 15.0,-15.0,15.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
}


they are not correct?

Share this post


Link to post
Share on other sites
Brother Bob    10344
For drawing the text, no, it's not what you want.

If you insist on not using glWindowPos, this is what output should look like.

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, window_width, 0, window_height, -1, 1);
glTranslated(0.5, 0.5, 0);

glMatrixMode(GL_MODLVIEW);
glPushMatrix();
glLoadIdentity();

glRasterPos2f(x, y);
// draw text here

glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODLVIEW);

Or use glWindowPos and forget about the matrices being ocrrect or not.

Share this post


Link to post
Share on other sites
BigBoss_Dv    122
Quote:
Original post by Brother Bob
For drawing the text, no, it's not what you want.

If you insist on not using glWindowPos, this is what output should look like.

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, window_width, 0, window_height, -1, 1);
glTranslated(0.5, 0.5, 0);

glMatrixMode(GL_MODLVIEW);
glPushMatrix();
glLoadIdentity();

glRasterPos2f(x, y);
// draw text here

glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODLVIEW);

Or use glWindowPos and forget about the matrices being ocrrect or not.

It gave me a lot of problem... how should i use glWindowPos ? thanks again

Share this post


Link to post
Share on other sites
Brother Bob    10344
Did you read the link I poste above? The function pointer typedef you need is PFNGLWINDOWPOS2IPROC, and the function name to get is glWindowPos2i.

Define the function pointer in file scope.

PFNGLWINDOWPOS2IPROC glWindowPos2i;

Get the address when you have a rendering context.

glWindowPos2i = reinterpret_cast<PFNGLWINDOWPOS2IPROC>(wglGetProcAddress("glWindowPos2i"));


Also, make sure you disable everything that affects the redering. Disable blending and texture mapping before rendering the text. And depth testing is probably not needed either.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this