Problem with texture transparency

Hello,

I’ve started playing with textures that have transparent parts in them, and I’m having some difficulties getting them to display properly. My problem is that I can’t get the transparent parts to actually be transparent. Instead, they get the color that was assigned to the triangle, as illustrated by the picture in attachment. (EDIT: ok I don’t know why my picture became so small, but anyway, on the top left is the result I would like to achieve, and on the top right is what is actually rendered. On the bottom left is the original texture.)

As, you can see, the texture is yellow (because I called gl.glColor4f(1, 1, 0, 1f)) while I would like it to be transparent.

Any ideas?

Here is my entire code:


package davidstuff;

import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES1;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.fixedfunc.GLLightingFunc;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;
import java.awt.AWTException;
import java.awt.Component;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

import static javax.media.opengl.GL.GL_COLOR_BUFFER_BIT;
import static javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT;

public class TransparencyTests extends Component implements GLEventListener {

    private GLU glu;

    int texture;

    public static void main(String[] args) throws AWTException {
        new TransparencyTests();
    }

    public TransparencyTests() throws AWTException {
        GLProfile glp = GLProfile.getDefault();
        GLCapabilities caps = new GLCapabilities(glp);
        GLCanvas canvas = new GLCanvas(caps);
        canvas.addGLEventListener(this);

        Frame frame = new Frame("My test");
        frame.setSize(500, 500);
        frame.add(canvas);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        FPSAnimator animator = new FPSAnimator(canvas, 60);
        animator.start();
    }

    @Override
    public void display(GLAutoDrawable drawable) {
        float z = -2.5f;

        GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers
        gl.glLoadIdentity();

        gl.glBegin(GL.GL_TRIANGLES);

        gl.glColor3fv(new float[]{0,0,1}, 0);
        gl.glVertex3f(-1, 0, z);
        gl.glVertex3f(0, 0, z);
        gl.glVertex3f(-1, 1, z);


        z = -2f;
        gl.glColor4f(0, 1, 0, 1f);
        gl.glVertex3f(-.5f, 0, z);
        gl.glVertex3f(0, 0, z);
        gl.glVertex3f(-1, 1, z);
        gl.glEnd();

        gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
        gl.glEnable(GL.GL_TEXTURE_2D);
        gl.glBindTexture(GL.GL_TEXTURE_2D, texture);

        z = -1.5f;
        gl.glBegin(GL.GL_TRIANGLES);
        gl.glColor4f(1, 1, 0, 1f);
        gl.glTexCoord2f(0.0f, 0.0f);
        gl.glVertex3f(-.5f, 0, z);
        gl.glTexCoord2f(1.0f, 0.0f);
        gl.glVertex3f(.5f, 0, z);
        gl.glTexCoord2f(0.0f, 1.0f);
        gl.glVertex3f(-.5f, 1, z);
        gl.glEnd();

        gl.glBindTexture(GL.GL_TEXTURE_2D, 0);
    }

    protected int loadTexture(GL2 gl, File imageFile) throws IOException {
        Texture t = TextureIO.newTexture(imageFile, true);

        return t.getTextureObject(gl);
    }


    @Override
    public void dispose(GLAutoDrawable drawable) { }

    @Override
    public void init(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();      // get the OpenGL graphics context
        glu = new GLU();                         // get GL Utilities
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background (clear) color
        gl.glClearDepth(1.0f);      // set clear depth value to farthest
        gl.glEnable(GL.GL_DEPTH_TEST); // enables depth testing
        gl.glEnable(GL.GL_BLEND);
        gl.glEnable(GL2.GL_ALPHA_TEST);
        gl.glAlphaFunc(GL.GL_GREATER, 0.1f);
        gl.glEnable(GL.GL_ALPHA);
        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
        gl.glEnable(GL2ES1.GL_COLOR_MATERIAL);
        gl.glDepthFunc(GL.GL_LEQUAL);  // the type of depth test to do
        gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // best perspective correction
        gl.glShadeModel(GLLightingFunc.GL_SMOOTH); // blends colors nicely, and smooths out lighting


        gl.glEnable(GL2ES1.GL_LIGHTING);
        gl.glEnable(GL2ES1.GL_LIGHT0);
        gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_AMBIENT, new float[]{.1f, .1f, .1f, .1f}, 0);


        try {
            texture = loadTexture(gl, new File("e:\	exttest.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL2 gl = drawable.getGL().getGL2();  // get the OpenGL 2 graphics context

        if (height == 0) height = 1;   // prevent divide by zero

        gl.glViewport(0, 0, width, height);

        gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);  // choose projection matrix
        gl.glLoadIdentity();             // reset projection matrix
        glu.gluPerspective(45.0, (float) width / height, 0.1, 100.0); // fovy, aspect, zNear, zFar

        gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
        gl.glLoadIdentity(); // reset
    }
}

AFAIK it is because of this line:

    gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);

I didn’t do that much with legacy OpenGL® but AFAIK the texture environment setting should be MODULATE to multiply the texture color with the diffuse geometry color to get the effect you want.

Woohoo, that did the trick! Thank you!