Well, The code I use to load textures is found here http://www.cs.plu.edu/~dwolff/talks/jogl-ccsc/src/f_Textures/TextureLoader.java
Notice that the Texture class can bind the texture to the gl context given. and it recognizes automatically which texture has alpha and which don’t. The image I am testing has an alpha channel and is in PNG format.
The code used in the init() method is
public void init(GLAutoDrawable drawable) {
// Getting the GL object
GL gl = drawable.getGL();
// Setting a GLU object
this.glu = new GLU();
// Setting smooth shading
gl.glShadeModel(GL.GL_SMOOTH);
// Setting depth buffer and function
gl.glClearDepth(1.0D);
gl.glDepthFunc(GL.GL_LEQUAL);
// Enabling 2D textures
gl.glEnable(GL.GL_TEXTURE_2D);
// Showing only the front face
//gl.glEnable(GL.GL_DEPTH_TEST);
// Setting nicest perspective
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
// Setting the backgroud color to be black
gl.glClearColor(0.0F, 0.0F, 0.0F, 0.5F);
// Setting the ambient light parameters
gl.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, lightAmbient, 0);
// Setting the diffuse light parameters
gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, lightDiffuse, 0);
gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, lightPosition, 0);
gl.glEnable(GL.GL_LIGHT1);
gl.glEnable(GL.GL_LIGHTING);
/*
* Alpha blending
*/
// Setting alpha
gl.glColor4f(1.0f, 1.0f, 0.0f, 0.0f);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);
gl.glEnable(GL.GL_BLEND);
try {
this.image = TextureManager.LoadTexture("ship_full.png").toGL(gl, this.glu, true);
}
catch (IOException ioe) {
ioe.printStackTrace();
}
Notice that depth testing is commented, as it makes weird effect such as - the alpha is black on some polygons, except for some faces which the black is transparent for them and they show through the black. I’ve tried different blending functions and turned depth testing on and off, but with no luck.
the display method() is
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
// Clearing the screen
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
/*
* Drawing a Cube
*/
// Resestting the modelview matrix
gl.glLoadIdentity();
// Move right 3.0 units
gl.glTranslatef(0.0F, 0.0F, -6.0F);
gl.glRotatef(this.rotRecty, 0.0f, 1.0f, 0.0f);
gl.glRotatef(this.rotRectx, 1.0f, 0.0f, 0.0f);
// Setting the color
gl.glColor4f(1.0F, 1.0F, 0.0F, 1.0f);
gl.glBindTexture(GL.GL_TEXTURE_2D, this.image);
// Drawing the rectangle
gl.glBegin(GL.GL_QUADS);
{
// Front Face
gl.glNormal3f(0.0f, 0.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
gl.glNormal3f(0.0f, 0.0f, -1.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
gl.glNormal3f(0.0f, 1.0f, 0.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
gl.glNormal3f(0.0f, -1.0f, 0.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
gl.glNormal3f(1.0f, 0.0f, 0.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
gl.glNormal3f(-1.0f, 0.0f, 0.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
}
gl.glEnd();
this.rotRectx = (this.rotRectx + this.rectXmodifier) % 360;
this.rotRecty = (this.rotRecty + this.rectYModifier) % 360;
}
Can anyone see the problem here?
Thanks in advance.