PDA

View Full Version : Problem using glCopyTexImage2d



sergi
05-22-2002, 06:03 AM
Hi all,

The problem is the next:

I'm trying to create a billboard texture from a sphere but I can't get copy the alpha channel correctly using glCopyTexImage2d.

Code goes here:

void generateSphereBillboard()
{
glGenTextures(1,&sphereTexture);
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

gluPerspective(65,1,0,10);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

gluLookAt(0, 0, 6, 0, 0, 1, 0, 1, 0);

glPushAttrib(GL_VIEWPORT_BIT);

glViewport(0, 0, 256,256);

GLUquadricObj *pObj = gluNewQuadric();
gluQuadricNormals(pObj,GL_SMOOTH);
gluSphere(pObj,1,16,16);
gluDeleteQuadric(pObj);

glBindTexture(GL_TEXTURE_2D,sphereTexture);

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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 256, 256, 0);

glPopAttrib();

glPopMatrix(); // pop modelview
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}

The image is copied to texture memory successfully but when I draw it, the billboards aren't transparents ( I guess alpha channel is bad?)

The base code to draw the billboard is the next:

void DrawBillboard(float[] a,float [] b, float []c, float [] d)
{
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,sphereTexture);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_NOTEQUAL,0);
glBegin(GL_QUADS);
glTexCoord2f(0,0);glVertex3fv(a.getValue());
glTexCoord2f(1,0);glVertex3fv(b.getValue());
glTexCoord2f(1,1);glVertex3fv(c.getValue());
glTexCoord2f(0,1);glVertex3fv(d.getValue());
glEnd();

glDisable(GL_TEXTURE_2D);
glDisable(GL_ALPHA_TEST);
glEnable(GL_LIGHTING);

}

I don't understand why the same code using a TGA file with alpha channel from the disk works fine. Any idea?

Thanks a lot!

Mazy
05-22-2002, 06:48 AM
And you are sure that your gl pixelmode has a destination alpha? and that you clear it correctly?

sergi
05-22-2002, 06:56 AM
Pixelmode is RGBA. And I clear it with

glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

What's wrong?

Bob
05-22-2002, 08:37 AM
Use glGetInteger with GL_ALPHA_BITS to see how many alpha bits you really have in the color buffer.

sergi
05-22-2002, 09:00 AM
First at all, thanks

The sentence:
glGetIntegerv(GL_ALPHA_BITS, &bits);
returned 0 bits. Therefore, glCopyTexImage2d can't copy alpha values that doesn't exist. Here it's the problem.

I think the code with TGA's works because the texture object is uploaded with alpha values and isn't uploaded from framebuffer.

I'll try to fix this ...

I'm using Coin (an Open Inventor-based library). Probably, the pixelformatdescriptor of the window is RGB instead of RGBA.

Thanks again!

sergi
05-22-2002, 09:02 AM
Errata: The library that creates the window is SoQt (not Coin).