在JOGL2中绘制图像

时间:2012-04-24 11:06:22

标签: java opengl jogl

我想绘制一张图片,但不管我做什么都看不到它。 我的代码用于渲染:

BufferedImage bi = null;
int w = 0;
int h = 0;
try {
    bi = ImageIO.read(new File("win.png"));
    w = bi.getWidth();
    h = bi.getHeight();
} catch (Exception e) {
    System.out.println(e.getMessage());
}

final GL2 gl = gLDrawable.getGL().getGL2();

        WritableRaster raster = 
            Raster.createInterleavedRaster (DataBuffer.TYPE_BYTE,
                    w,
                    h,
                    4,
                    null);
        ComponentColorModel colorModel=
            new ComponentColorModel (ColorSpace.getInstance(ColorSpace.CS_sRGB),
                    new int[] {8,8,8,8},
                    true,
                    false,
                    ComponentColorModel.TRANSLUCENT,
                    DataBuffer.TYPE_BYTE);
        BufferedImage dukeImg = 
            new BufferedImage (colorModel,
                    raster,
                    false,
                    null);

        Graphics2D g = dukeImg.createGraphics();
        g.drawImage(bi, null, null);
        DataBufferByte dukeBuf =
            (DataBufferByte)raster.getDataBuffer();
        byte[] dukeRGBA = dukeBuf.getData();
        ByteBuffer bb = ByteBuffer.wrap(dukeRGBA);

        bb.position(0);
        bb.mark();
    gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);

    gl.glEnable(GL2.GL_TEXTURE_2D);
    gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
        gl.glBindTexture(GL2.GL_TEXTURE_2D, 13);
        gl.glPixelStorei(GL2.GL_UNPACK_ALIGNMENT, 1);



        gl.glTexImage2D (GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, w, h, 0, GL2.GL_RGBA, 
                GL2.GL_UNSIGNED_BYTE, bb);

        int left = 100;
        int top = 100;

    gl.glBegin(GL2.GL_QUADS); 
        gl.glTexCoord2d (0, 0);
        gl.glVertex3f  (left,top, 0);
        gl.glTexCoord2d(1,0);
        gl.glVertex3f  (left + w, top, 0);
        gl.glTexCoord2d(1,1);
        gl.glVertex3f  (left + w, top + h, 0);
        gl.glTexCoord2d(0,1);
        gl.glVertex3f  (left, top + h, 0);
        gl.glEnd ();    
        gl.glFlush();

1 个答案:

答案 0 :(得分:1)

搞定了。 http://wiki.tankaar.com/index.php?title=Displaying_an_Image_in_JOGL_%28Part_1%29 只将GL替换为GL2,就是这样!