I have an JOGL based opengl program that uses textures on flat surfaces. After a texture is put on a surface, it will eventually crash. If the textures are small, it takes a while. I managed to reproduce it with a simple program:
package org.yourorghere;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
/**
* SimpleJOGL.java <BR>
* author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P>
*
* This version is equal to Brian Paul's version 1.2 1999/10/21
*/
public class SimpleJOGL implements GLEventListener, KeyListener {
GLCanvas canvas;
int lists;
float angle = 0.f;
boolean needsRotate = false;
boolean needsTexture = false;
Texture texture1;
Texture texture2;
Color c1 = Color.red;
Color c2 = Color.green;
public SimpleJOGL(GLCanvas canvas) {
this.canvas = canvas;
}
public static void main(String[] args) {
Frame frame = new Frame("Simple JOGL Application");
GLCanvas canvas = new GLCanvas();
SimpleJOGL sjogl = new SimpleJOGL(canvas);
canvas.addKeyListener(sjogl);
canvas.addGLEventListener(sjogl);
frame.add(canvas);
frame.setSize(640, 480);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
// Center frame
frame.setLocationRelativeTo(null);
frame.setVisible(true);
animator.start();
}
public void init(GLAutoDrawable drawable) {
// Use debug pipeline
// drawable.setGL(new DebugGL(drawable.getGL()));
GL gl = drawable.getGL();
System.err.println("INIT GL IS: " + gl.getClass().getName());
// Enable VSync
gl.setSwapInterval(1);
// double buffer
gl.glEnable(GL.GL_DOUBLEBUFFER);
drawable.setAutoSwapBufferMode(false);
// Enable VSync
gl.setSwapInterval(1);
// Setup the drawing area and shading mode
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glDisable(GL.GL_BLEND);
// -- lighting --
gl.glEnable(GL.GL_COLOR_MATERIAL);
gl.glLightModelf(GL.GL_LIGHT_MODEL_TWO_SIDE, 1.0f);
gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
gl.glEnable(GL.GL_MAP1_VERTEX_3);
gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, new float[]{.3f,.3f,.3f,1.f},0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, new float[]{.55f,.55f,.55f,1.f},0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, new float[]{.05f,.05f,.05f,1.f}, 0);
gl.glFrontFace(GL.GL_CCW);
gl.glEnable(GL.GL_LIGHT0);
gl.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE, new float[]{.5f,.5f,.5f,1.0f}, 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, new float[]{.05f, .05f, .05f, 1.0f}, 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_EMISSION, new float[]{0.01f, 0.01f, 0.01f, 1.f}, 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, new float[]{30}, 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_COLOR_INDEXES, new float[]{1.0f, 0.0f, 0.0f, 1.0f}, 0);
gl.glCullFace(GL.GL_BACK);
gl.glDisable(GL.GL_BLEND);
gl.glEnable(GL.GL_RESCALE_NORMAL);
lists = gl.glGenLists(3);
this.needsRotate = true;
this.needsTexture = true;
}
private void reTexture(GL gl) {
BufferedImage image1 = makeImage(c1);
BufferedImage image2 = makeImage(c2);
makeShape(gl, image1, image2);
}
private BufferedImage makeImage(Color c) {
int y = 1200;
int x = 1024;
BufferedImage image = new BufferedImage(y, y, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setBackground(Color.white);
g.clearRect(0, 0, y,y);
Rectangle2D r = new Rectangle2D.Float(128,128,x-256,x-256);
g.setColor(c);
g.fill(r);
g.dispose();
return image;
}
private void makeShape(GL gl, BufferedImage image1, BufferedImage image2) {
float x = 1024.f/1200.f;
texture1 = TextureIO.newTexture(image1, false) ;
texture2 = TextureIO.newTexture(image2, false) ;
gl.glNewList(lists+1, GL.GL_COMPILE);
gl.glColor3f(0f, .8f, .4f);
// Drawing Using Triangles
gl.glBegin(GL.GL_POLYGON);
gl.glTexCoord2f(0, 0);
gl.glVertex3f(0.0f, 0.0f, 0.0f); // Top
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(x, 0);
gl.glVertex3f(1.0f, 0.0f, 0.0f); // Bottom Left
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(x, x);
gl.glVertex3f(1.0f, 1.0f, 0.0f); // Bottom Right
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(0, x);
gl.glVertex3f(0f, 1.0f, 0.0f); // Bottom Right
gl.glNormal3f(0, 0, 1);
gl.glEnd();
texture1.disable();
gl.glEndList();
gl.glNewList(lists+2, GL.GL_COMPILE);
texture2.enable();
texture2.bind();
gl.glBegin(GL.GL_POLYGON);
gl.glTexCoord2f(0, 0);
gl.glVertex3f(0.0f, 0.0f, 0.0f); // Top
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(x, 0);
gl.glVertex3f(1.0f, 0.0f, 0.0f); // Bottom Left
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(x, x);
gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
gl.glNormal3f(0, 0, 1);
gl.glTexCoord2f(0, x);
gl.glVertex3f(0f, -1.0f, 0.0f); // Bottom Right
gl.glNormal3f(0, 0, 1);
gl.glEnd();
texture2.disable();
gl.glEndList();
}
public void rotate(GL gl) {
gl.glNewList(lists, GL.GL_COMPILE);
gl.glLoadIdentity();
gl.glTranslatef(-2.f, 0.f, -6.f);
gl.glRotatef(angle, 1.0f, 0.f, 0.f);
gl.glCallList(lists+1);
gl.glRotatef(-45, 1.f, 0.f, 0.f);
gl.glCallList(lists+2);
gl.glEndList();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
GLU glu = new GLU();
if (height <= 0) { // avoid a divide by zero error!
height = 1;
}
final float h = (float) width / (float) height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
if (needsTexture) {
reTexture(gl);
needsTexture = false;
}
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[]{1.f, 1.f, 1.f, 0.f}, 0);
gl.glLoadIdentity();
gl.glTranslatef(-2.f, 0.f, -6.f);
gl.glRotatef(angle, 1.0f, 0.f, 0.f);
texture1.enable();
texture1.bind();
gl.glCallList(lists+1);
texture1.disable();
gl.glRotatef(-45, 1.f, 0.f, 0.f);
gl.glCallList(lists+2);
//
// if (needsRotate) {
// rotate(gl);
// needsRotate = false;
// }
//
// gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
//
// gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[]{1.f, 1.f, 1.f, 0.f}, 0);
// gl.glCallList(lists);
// Flush all drawing operations to the graphics card
drawable.swapBuffers();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
angle -= 5;
needsRotate = true;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
angle += 5;
needsRotate = true;
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
Color c = c1;
c1 = c2;
c2 = c;
needsTexture = true;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
canvas.repaint();
}
}
pressing up and down arrows rotates the object, pressing enter swaps the textures. After a while of doing this (1-2 minutes), the java vm crashes and it is either in the ati drivers or in a kernel system call. I've observed this on at least a dozen computers, PC and linux. Every one an ATI card. I'm sure I'm dealing with the texture resources improperly, but since I am using display lists for these surfaces, if I dispose of the texture it isn't there when the display list is called again. Any ideas?
Thank you,
--Douglas