I’m experimenting with using a J2D graphic as a texture in Jogl. The following code is supposed to do that, but unfortunately it doesn’t actually seem to draw the image.
Does anybody know what I might be doing wrong?
...
public class Pic
{
public BufferedImage image;
public Pic()
{
try {
image = ImageIO.read(new File(
"day of the dragons.jpg"));
} catch(IOException e) {
System.err.println("File does not exist");
System.exit(0);
}
}
public void render(Graphics2D g2d)
{
g2d.drawImage(image, 0, 0, null);
}
}
...
public class Main implements GLEventListener, KeyListener, MouseListener,
MouseMotionListener, ComponentListener
{
GLU glu = new GLU();
GLUT glut = new GLUT();
static Animator animator;
static TextureRenderer picRenderer;
static Pic pic;
static int w, h;
public static void main(String[]args)
{
pic = new Pic();
w = pic.image.getWidth();
h = pic.image.getHeight();
picRenderer = new TextureRenderer(w, h, false);
Frame frame = new Frame("Gluttony");
GLCapabilities caps = new GLCapabilities();
caps.setAlphaBits(8);
caps.setStencilBits(8);
caps.setAccumAlphaBits(16);
caps.setAccumRedBits(16);
caps.setAccumGreenBits(16);
caps.setAccumBlueBits(16);
System.out.println(caps.toString());
GLCanvas canvas = new GLCanvas(caps);
canvas.addGLEventListener(new Main());
frame.add(canvas);
frame.setSize(w, h);
animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
frame.setVisible(true);
animator.start();
}
public void updatePicRenderer()
{
Graphics2D g2d= picRenderer.createGraphics();
pic.render(g2d);
g2d.dispose();
picRenderer.markDirty(0, 0, w, h);
}
public void displayPicRenderer(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
updatePicRenderer();
Texture tex = picRenderer.getTexture();
TextureCoords tc = tex.getImageTexCoords();
float tx1 = tc.left();
float ty1 = tc.top();
float tx2 = tc.right();
float ty2 = tc.bottom();
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE,
GL.GL_MODULATE);
float lx = -1f;
float ly = -1f;
float lw = (float)w;
float lh = (float)h;
tex.bind();
tex.enable();
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(tx1, ty1); gl.glVertex3f(lx, ly, 0f);
gl.glTexCoord2f(tx2, ty1); gl.glVertex3f(lx+lw, ly, 0f);
gl.glTexCoord2f(tx2, ty2); gl.glVertex3f(lx+lw, ly+lh, 0f);
gl.glTexCoord2f(tx1, ty2); gl.glVertex3f(lx, ly+lh, 0f);
gl.glEnd();
tex.disable();
gl.glDisable(GL.GL_BLEND);
}
//*********************************************************************
//* Needed for GLEventListener *
//*********************************************************************
public void display(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
displayPicRenderer(drawable);
}
...
public void init(GLAutoDrawable drawable)
{
drawable.addKeyListener(this);
drawable.setAutoSwapBufferMode(true);
GL gl = drawable.getGL();
gl.setSwapInterval(0);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glClearColor(0f, 0f, 0f, 0f);
gl.glViewport(0, 0, w, h);
}
...