import java.io.IOException; import java.nio.FloatBuffer; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; import com.jogamp.common.nio.Buffers; import com.jogamp.opengl.GL; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLCapabilities; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.GLProfile; import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.util.Animator; import com.jogamp.opengl.util.texture.Texture; import com.jogamp.opengl.util.texture.TextureIO; public class TextureTest { public static void main( String[] args ) throws Exception { GLProfile.initSingleton(); GLProfile glp = GLProfile.get( GLProfile.GL2 ); GLCapabilities caps = new GLCapabilities( glp ); SwingUtilities.invokeAndWait( () -> { GLCanvas canvas = new GLCanvas( caps ); canvas.addGLEventListener( new Scene() ); JFrame jf = new JFrame( "TextureTest" ); jf.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE ); jf.add( canvas ); jf.setSize( 300, 300 ); jf.setResizable( false ); jf.setVisible( true ); new Animator( canvas ).start(); } ); } static class Scene implements GLEventListener { private static final float ANGLE_DEG_INCR = 1f; private final FloatBuffer vertexBuf; private final FloatBuffer texCoordBuf; private final int texUnit = GL.GL_TEXTURE0; private final boolean mipmaps = true; private int texId; private float angleDeg = 0; public Scene() { vertexBuf = Buffers.newDirectFloatBuffer( new float[] { -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f } ); texCoordBuf = Buffers.newDirectFloatBuffer( new float[] { 0, 0, 0, 1, 1, 1, 1, 0 } ); } @Override public void init( GLAutoDrawable drawable ) { GL2 gl = drawable.getGL().getGL2(); gl.setSwapInterval( 1 ); gl.glClearColor( 0.4f, 0.6f, 0.9f, 0f ); gl.glViewport( 0, 0, 300, 300 ); gl.glDisable( GL2.GL_DEPTH_TEST ); gl.glShadeModel( GL2.GL_SMOOTH ); gl.glMatrixMode( GL2.GL_MODELVIEW ); gl.glEnable( GL.GL_TEXTURE_2D ); try { gl.glClientActiveTexture( texUnit ); Texture tex = TextureIO.newTexture( TextureTest.class.getResource( "square.dds" ), mipmaps, "dds" ); tex.enable( gl ); } catch ( IOException e ) { throw new RuntimeException( "Could not load texture", e ); } } @Override public void display( GLAutoDrawable drawable ) { angleDeg += ANGLE_DEG_INCR; GL2 gl = drawable.getGL().getGL2(); gl.glClear( GL2.GL_COLOR_BUFFER_BIT ); gl.glClientActiveTexture( texUnit ); gl.glEnableClientState( GL2.GL_VERTEX_ARRAY ); gl.glEnableClientState( GL2.GL_TEXTURE_COORD_ARRAY ); gl.glVertexPointer ( 2, GL.GL_FLOAT, 0, vertexBuf ); gl.glTexCoordPointer( 2, GL.GL_FLOAT, 0, texCoordBuf ); gl.glLoadIdentity(); gl.glRotatef( angleDeg, 0, 0, 1 ); gl.glDrawArrays( GL2.GL_QUADS, 0, 4 ); gl.glDisableClientState( GL2.GL_VERTEX_ARRAY ); gl.glDisableClientState( GL2.GL_TEXTURE_COORD_ARRAY ); } @Override public void dispose( GLAutoDrawable drawable ) { GL2 gl = drawable.getGL().getGL2(); gl.glBindTexture( GL.GL_TEXTURE_2D, 0 ); gl.glDeleteTextures( 1, new int[] { texId }, 0 ); gl.glDisable( GL.GL_TEXTURE_2D ); } @Override public void reshape( GLAutoDrawable drawable, int x, int y, int w, int h ) { } } }