PNG Texture not showing (new guy)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

PNG Texture not showing (new guy)

midramble
Hello all. I'm pretty new to JOGL and I've created the below code just to display a png with an alpha channel as a texture on a simple quad. The code throws no exceptions; however, the texture doesn't show and neither does the background color of the quad. As you can also see I've tried other methods of binding textures (the code that is /* */ blocked out) that also didn't work. I just can't seem to see what I'm doing wrong.

[Below is my main class]
public class redoubt
{
    public static void main(String[] args)
    {
    // setup OpenGL Version 2
    GLProfile profile = GLProfile.get(GLProfile.GL2);
    GLCapabilities capabilities = new GLCapabilities(profile);
    capabilities.setRedBits(8);
        capabilities.setBlueBits(8);
        capabilities.setGreenBits(8);
        capabilities.setAlphaBits(8);
        capabilities.setSampleBuffers(true);
 
    // Create canvas
    GLCanvas glcanvas = new GLCanvas(capabilities);
    glcanvas.addGLEventListener(new mainrend());
    glcanvas.setSize( 500, 500 );
    glcanvas.setBackground(Color.WHITE);
 
        JFrame frame = new JFrame( "Redoubt" );
        frame.getContentPane().add(glcanvas);
 
        // shutdown the program on windows close event
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
                System.exit(0);
            }
        });
 
        frame.setSize( frame.getContentPane().getPreferredSize() );
        frame.setVisible( true );
    }
}


[below is my render class]
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.gl2.GLUgl2;

import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureData;
import com.jogamp.opengl.util.texture.TextureIO;
 
class mainrend implements GLEventListener
{
        private GLUgl2 glu = new GLUgl2();
    private Texture dude;
 
    public void init(GLAutoDrawable gLDrawable)
    {
    System.out.println("init() called");
        GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL2.GL_FLAT);
        gl.glEnable(GL.GL_TEXTURE_2D);
        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
        gl.glEnable(GL.GL_BLEND);
       
        /*
        try {
            InputStream stream = getClass().getResourceAsStream("dude.png");
            TextureData data = TextureIO.newTextureData(GLProfile.get(GLProfile.GL2) ,stream, false, "png");
            dude = TextureIO.newTexture(data);
            System.out.println("texture load complete");
        }
        catch (IOException exc) {
            System.out.println("text load went to crap with " + exc);
        }
        */
       
    }
   
    public void display(GLAutoDrawable gLDrawable)
    {
    System.out.println("display called");
        final GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
       
       
       
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, -10.0f);
       
        //texture prep
        int Text;
        int retval[]= new int[1];
        gl.glGenTextures(1, retval, 0);
        Text = retval[0];
        gl.glBindTexture(GL.GL_TEXTURE_2D, Text);
       
        mmify(glu, "dude.png");

       
                //Load texture
        /*
        dude.enable(gl);
        dude.bind(gl);
        */
        System.out.println("texture loaded?");
       
       
       
        gl.glBegin(GL2.GL_QUADS);
        gl.glVertex3f(-1.0f, 1.0f, 0.0f);
        gl.glVertex3f(1.0f, 1.0f, 0.0f);
        gl.glVertex3f(1.0f, -1.0f, 0.0f);
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);
        gl.glEnd();
        gl.glFlush();
    }
 
 
    public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged)
    {
    System.out.println("displayChanged called");
    }
 
   
 
    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height)
    {
    System.out.println("reshape() called: x = "+x+", y = "+y+", width = "+width+", height = "+height);
        final GL2 gl = gLDrawable.getGL().getGL2();
 
        if (height <= 0) // avoid a divide by zero error!
        {
            height = 1;
        }
 
        final float h = (float) width / (float) height;
 
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 1.0, 20.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
 
 
        public void dispose(GLAutoDrawable arg0)
        {
                System.out.println("dispose() called");
        }
       
        //create mipmap from png... hence mipmapify
        public void mmify(GLU glu, String imgname) {
                try {
                        InputStream inpstr = ClassLoader.getSystemResourceAsStream(imgname);
                        BufferedInputStream buffinst = new BufferedInputStream(inpstr);
                        BufferedImage buffimg = ImageIO.read(buffinst);
                        Raster rast = buffimg.getRaster();
                        DataBufferByte dbuffb = (DataBufferByte)rast.getDataBuffer();
                        byte b[] = dbuffb.getData();
                        int textsize = b.length;
                        ByteBuffer texture = ByteBuffer.allocateDirect(textsize);
                        texture.order(ByteOrder.nativeOrder());
                        texture.put(b, 0, textsize);
                        texture.rewind();
                        int iwidth = buffimg.getWidth();
                        System.out.println("made iwidth int " + iwidth);
                        int iheight = buffimg.getHeight();
                        System.out.println("made iheight int " + iheight);
                        System.out.println("buffer size = " + textsize);
                        System.out.println("middle 4 bytes are: " + b[(textsize/2)] + ", " + b[textsize/2+1] + ", " + b[textsize/2+2] + ", " + b[textsize/2+3]);
                       
                        glu.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, GL.GL_RGBA8, iwidth, iheight, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, texture);
                }
                catch (Exception e) {
                        System.out.println("Some crap happend in mmify " + e);
                }
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: PNG Texture not showing (new guy)

gouessej
Administrator
Hi

You should use glTexCoords in order to pass texture coordinates:
http://www.opengl.org/sdk/docs/man/xhtml/glTexCoord.xml

I advise you to learn the basics of OpenGL.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: PNG Texture not showing (new guy)

Gene
Additionally, TextureIO.newTexture will take care of the mipmap generation for you, eliminating other chances for error.  texture.enable and texture.bind work fine once you give texture coords.