package fancy.jf.texture;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.nio.FloatBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.BufferUtil;
import com.sun.opengl.util.FPSAnimator;
import com.sun.opengl.util.texture.TextureData;
import com.sun.opengl.util.texture.TextureIO;
public class Tex3D implements GLEventListener {
// for full screen plane
private static final float[] staticVertices = new float[] {
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f};
// texture cords for full screen plane
private static final float[] staticTexCord = new float[] {
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f};
private GL gl = null;
private FloatBuffer vertexArrayVertices = null;
private FloatBuffer vertexArrayTexture = null;
@Override
public void init(GLAutoDrawable drawable) {
gl = drawable.getGL();
// setup vertex array
vertexArrayVertices = BufferUtil.newFloatBuffer(staticVertices.length);
vertexArrayVertices.put(staticVertices, 0, staticVertices.length);
vertexArrayVertices.rewind();
vertexArrayTexture = BufferUtil.newFloatBuffer(staticTexCord.length);
vertexArrayTexture.put(staticTexCord, 0, staticTexCord.length);
vertexArrayTexture.rewind();
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
// setup 3D texture
final int[] handleTex3DBuffer = new int[1];
gl.glGenTextures(1, handleTex3DBuffer, 0);
final int handleTex3D = handleTex3DBuffer[0];
gl.glBindTexture(GL.GL_TEXTURE_3D, handleTex3D);
gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_WRAP_R, GL.GL_REPEAT);
try {
final TextureData data = TextureIO.newTextureData(getClass().getResource("/3d.jpg"), false, null);
gl.glTexImage3D(GL.GL_TEXTURE_3D, 0, GL.GL_RGB8, 500, 500, 10, 0, GL.GL_BGR, GL.GL_UNSIGNED_BYTE, data.getBuffer());
} catch (final IOException e) {
e.printStackTrace();
}
gl.glEnable(GL.GL_TEXTURE_3D);
}
@Override
public void display(GLAutoDrawable drawable) {
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
// walk through texture
for (int i = 0; i < 6; i++)
vertexArrayTexture.put(i * 3 + 2, vertexArrayTexture.get(i * 3 + 2) + 0.001f);
// draw vertex array with current texture part
gl.glTexCoordPointer(3, GL.GL_FLOAT, 0, vertexArrayTexture);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertexArrayVertices);
gl.glDrawArrays(GL.GL_TRIANGLES, 0, staticVertices.length);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
if (height <= 0) height = 1;
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
public static void main(String[] args) {
final Frame frame = new Frame();
final GLCanvas canvas = new GLCanvas();
final Animator animator = new FPSAnimator(canvas, 60, true);
canvas.addGLEventListener(new Tex3D());
frame.add(canvas);
frame.setSize(500, 500);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
frame.setVisible(true);
animator.start();
}
}