import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import javax.media.opengl.GL; import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import com.jogamp.newt.event.WindowAdapter; import com.jogamp.newt.event.WindowEvent; import com.jogamp.newt.opengl.GLWindow; import com.jogamp.opengl.util.awt.Screenshot; import com.jogamp.opengl.util.gl2.GLUT; import com.jogamp.opengl.util.texture.Texture; import com.jogamp.opengl.util.texture.TextureIO; /** * Adapted from * http://www.java-tips.org/other-api-tips/jogl/how-to-draw-a-texture * -mapped-teapot-with-automatically-generated-texture-coordi.html */ public class JoglNewtTeapot implements GLEventListener { private GLUT glut; private static final int stripeImageWidth = 32; private ByteBuffer stripeImageBuf = ByteBuffer .allocate(3 * stripeImageWidth); /* glTexGen stuff: */ private float sgenparams[] = { 1.0f, 1.0f, 1.0f, 0.0f }; @Override public void display(GLAutoDrawable gLDrawable) { final GL2 gl = gLDrawable.getGL().getGL2(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glPushMatrix(); gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f); glut.glutSolidTeapot(2.0f); gl.glPopMatrix(); gl.glFlush(); try { Screenshot.writeToFile( new File("testjo" + System.currentTimeMillis() + ".png"), 640, 480, true); } catch (Exception e) { e.printStackTrace(); } } @Override public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); glut = new GLUT(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); Texture tex = null; try { tex = TextureIO .newTexture(getClass().getClassLoader() .getResourceAsStream("ardor3d_white_256.jpg"), false, "jpg"); } catch (Exception e) { e.printStackTrace(); } tex.bind(gl); // uncomment this and comment the above to see a working texture // makeStripeImage(); // gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); // gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, // GL2.GL_MODULATE); // gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL.GL_TEXTURE_WRAP_S, // GL.GL_REPEAT); // gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL.GL_TEXTURE_MAG_FILTER, // GL.GL_LINEAR); // gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL.GL_TEXTURE_MIN_FILTER, // GL.GL_LINEAR); // gl.glTexImage1D(GL2.GL_TEXTURE_1D, 0, 3, stripeImageWidth, 0, // GL.GL_RGB, GL.GL_UNSIGNED_BYTE, stripeImageBuf); gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_OBJECT_LINEAR); gl.glTexGenfv(GL2.GL_S, GL2.GL_OBJECT_PLANE, sgenparams, 0); gl.glEnable(GL.GL_DEPTH_TEST); gl.glDepthFunc(GL.GL_LESS); gl.glEnable(GL2.GL_TEXTURE_GEN_S); // gl.glEnable(GL2.GL_TEXTURE_1D); gl.glEnable(GL2.GL_TEXTURE_2D); gl.glEnable(GL2.GL_CULL_FACE); gl.glEnable(GL2.GL_LIGHTING); gl.glEnable(GL2.GL_LIGHT0); gl.glEnable(GL2.GL_AUTO_NORMAL); gl.glEnable(GL2.GL_NORMALIZE); gl.glFrontFace(GL.GL_CW); gl.glCullFace(GL.GL_BACK); gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 64.0f); } private void makeStripeImage() { for (int j = 0; j < stripeImageWidth; j++) { stripeImageBuf.put(((j <= 4) ? (byte) 255 : (byte) 0)); stripeImageBuf.put(((j > 4) ? (byte) 255 : (byte) 0)); stripeImageBuf.put((byte) 0); } stripeImageBuf.rewind(); } @Override public void reshape(GLAutoDrawable gLDrawable, int x, int y, int w, int h) { GL2 gl = gLDrawable.getGL().getGL2(); gl.glViewport(0, 0, w, h); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); if (w <= h) gl.glOrtho(-3.5, 3.5, -3.5 * (float) h / (float) w, 3.5 * (float) h / (float) w, -3.5, 3.5); else gl.glOrtho(-3.5 * (float) w / (float) h, // 3.5 * (float) w / (float) h, -3.5, 3.5, -3.5, 3.5); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); } @Override public void dispose(GLAutoDrawable gLDrawable) { } public static void main(String[] args) throws InterruptedException, IOException { final GLCapabilities caps = new GLCapabilities( GLProfile.getMaxFixedFunc(true)); caps.setHardwareAccelerated(true); caps.setDoubleBuffered(true); caps.setAlphaBits(8); caps.setDepthBits(8); caps.setNumSamples(0); caps.setSampleBuffers(false); caps.setStencilBits(0); caps.setRedBits(8); caps.setBlueBits(8); caps.setGreenBits(8); // caps.setPBuffer(true); caps.setFBO(true); final GLWindow glWindow = GLWindow.create(caps); glWindow.setSize(640, 480); glWindow.addGLEventListener(new JoglNewtTeapot()); glWindow.addWindowListener(new WindowAdapter() { @Override public void windowDestroyed(WindowEvent arg0) { System.exit(0); } }); glWindow.setVisible(true); for (int x = 0; x < 5; x++) { glWindow.display(); } } }