import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.imageio.ImageIO; import javax.media.opengl.*; import javax.media.opengl.glu.GLU; import javax.swing.JPanel; import java.awt.BorderLayout; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.nio.ByteBuffer; import com.sun.opengl.util.Animator; public class gluBuild2DMipmapsTEST extends JFrame { public static void main(String[] args) { int result = JOptionPane.showConfirmDialog(null, "Do you want to disable java implementation of GLU", "jogl.glu.nojava", JOptionPane.YES_NO_OPTION); if(result == JOptionPane.YES_OPTION) System.setProperty("jogl.glu.nojava", "true"); //Disable java implementation else System.setProperty("jogl.glu.nojava", "false"); //Enable java implementation new gluBuild2DMipmapsTEST().setVisible(true); } private GLCanvas glScene = null; private JPanel jContentPane = null; private int texture[] = new int[1]; public gluBuild2DMipmapsTEST() { super("gluBuild2DMipmaps TEST"); initialize(); } private void initialize() { this.setContentPane(getJContentPane()); this.pack(); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } private JPanel getJContentPane() { if(jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(getGlScene(), BorderLayout.CENTER); } return jContentPane; } private GLCanvas getGlScene() { if(glScene == null) { glScene = new GLCanvas(); glScene.addGLEventListener(new Scene()); glScene.setSize(400, 300); } return glScene; } class Scene implements GLEventListener { private Animator animator; public void displayChanged(GLAutoDrawable glDrawable, boolean modeChanged, boolean deviceChanged){} public void reshape(GLAutoDrawable glDrawable, int x, int y, int width, int height) { final GL gl = glDrawable.getGL(); final GLU glu = new GLU(); if(height <= 0) height = 1; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0f, width / height, 0.1f, 1000.0f); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); } public void init(GLAutoDrawable glDrawable) { final GL gl = glDrawable.getGL(); if(!loadTexture(glDrawable)) { JOptionPane.showMessageDialog(null, "Fail to load texture. Exiting."); System.exit(0); } gl.glEnable(GL.GL_TEXTURE2); gl.glDepthFunc(GL.GL_LEQUAL); gl.glEnable(GL.GL_DEPTH_TEST); animator = new Animator(glDrawable); animator.start(); } private boolean loadTexture(GLAutoDrawable glDrawable) { BufferedImage image; try { image = ImageIO.read(new FileInputStream(new File("PLATEOX2.bmp"))); } catch(Exception e) { return false; } int width = image.getWidth(); int height = image.getHeight(); byte[] pixels = new byte[width*height*3]; for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { int i = (height-1-y)*width+x; //Flip picture int color = image.getRGB(x, y); pixels[3*i ] = (byte) ((color >> 16) & 0xFF); //r pixels[3*i+1] = (byte) ((color >> 8) & 0xFF); //g pixels[3*i+2] = (byte) ( color & 0xFF); //b } } GL gl = glDrawable.getGL(); gl.glGenTextures(1, texture, 0); //Texture offset = 0 gl.glBindTexture(GL.GL_TEXTURE_2D, texture[0]); gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); new GLU().gluBuild2DMipmaps(GL.GL_TEXTURE_2D, GL.GL_RGB, width, height, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, ByteBuffer.wrap(pixels)); return gl.glIsTexture(texture[0]); } public void display(GLAutoDrawable glDrawable) { final GL gl = glDrawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glEnable(GL.GL_TEXTURE_2D); gl.glBindTexture(GL.GL_TEXTURE_2D, texture[0]); begin2D(glDrawable); gl.glBegin(GL.GL_QUADS); float width = glDrawable.getWidth(); float height = glDrawable.getHeight(); gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-width, height, 0); gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-width, -height, 0); gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( width, -height, 0); gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( width, height, 0); gl.glEnd(); end2D(glDrawable); } private void begin2D(GLAutoDrawable glDrawable) { final GL gl = glDrawable.getGL(); gl.glMatrixMode(GL.GL_PROJECTION); gl.glPushMatrix(); gl.glLoadIdentity(); gl.glOrtho(0, glDrawable.getWidth(), 0, glDrawable.getHeight(), -1, 1); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glPushMatrix(); gl.glLoadIdentity(); } private void end2D(GLAutoDrawable glDrawable) { final GL gl = glDrawable.getGL(); gl.glMatrixMode(GL.GL_PROJECTION); gl.glPopMatrix(); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glPopMatrix(); } } }