Ok,
Let me ask my question with a code sample.
This is Lesson 5, and I modified it for the mouse events.
It works fine except I do two things together:
- Add (line 13) “canvas.setGL(new DebugGL(canvas.getGL()));”
- Add (line 154) “gl.glInitNames();”
then get “java.lang.OutOfMemoryError”, actually not crashing but freezes for a considerable time.
Any hint is deeply appreciated.
Thanks.
import java.awt.;
import java.awt.event.;
import net.java.games.jogl.;
import net.java.games.jogl.util.;
public class Lesson05 {
public static void main(String[] args) {
Frame frame = new Frame(“Lesson 5: 3D Shapes”);
GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new
GLCapabilities());
canvas.setGL(new DebugGL(canvas.getGL()));
L_5_Renderer r = new L_5_Renderer();
canvas.addGLEventListener®;
canvas.addKeyListener®;
canvas.addMouseMotionListener®;
canvas.addMouseListener®;
frame.add(canvas);
frame.setSize(640, 480);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.show();
canvas.requestFocus();
}
static class L_5_Renderer
implements GLEventListener,
KeyListener, MouseMotionListener, MouseListener {
GL gl;
GLU glu;
private float rquadX = 0.0f;
private float rtriX = 0.0f;
private float rquadY = 0.0f;
private float rtriY = 0.0f;
private float rquadZ = 0.0f;
public void init(GLDrawable gLDrawable) {
gl = gLDrawable.getGL();
glu = gLDrawable.getGLU();
gl.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
gl.glClearDepth(1.0f); // Depth Buffer Setup
gl.glEnable(GL.GL_DEPTH_TEST); // Enables Depth Testing
gl.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing To Do
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // Really Nice Perspective Calculations
}
public void reshape(GLDrawable gLDrawable, int x, int y, int width,
int height) {
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(GLDrawable gLDrawable) {
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -6.0f);
gl.glRotatef(rquadX, 0.0f, 1.0f, 0.0f);
gl.glRotatef(rquadY, 1.0f, 0.0f, 0.0f);
gl.glRotatef(rquadZ, 0.0f, 0.0f, 1.0f);
//Rotate end....
gl.glBegin(GL.GL_QUADS); // Draw A Quad
gl.glColor3f(0.0f, 1.0f, 0.0f); // Set The Color To Green
gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
gl.glVertex3f( -1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
gl.glVertex3f( -1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
gl.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
gl.glColor3f(1.0f, 0.5f, 0.0f); // Set The Color To Orange
gl.glVertex3f(1.0f, -1.0f, 1.0f); // Top Right Of The Quad (Bottom)
gl.glVertex3f( -1.0f, -1.0f, 1.0f); // Top Left Of The Quad (Bottom)
gl.glVertex3f( -1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Bottom)
gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Bottom)
gl.glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red
gl.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
gl.glVertex3f( -1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
gl.glVertex3f( -1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Front)
gl.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Front)
gl.glColor3f(1.0f, 1.0f, 0.0f); // Set The Color To Yellow
gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Back)
gl.glVertex3f( -1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Back)
gl.glVertex3f( -1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back)
gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back)
gl.glColor3f(0.0f, 0.0f, 1.0f); // Set The Color To Blue
gl.glVertex3f( -1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
gl.glVertex3f( -1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left)
gl.glVertex3f( -1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Left)
gl.glVertex3f( -1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Left)
gl.glColor3f(1.0f, 0.0f, 1.0f); // Set The Color To Violet
gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right)
gl.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
gl.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Right)
gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Right)
gl.glEnd(); // Done Drawing The Quad
gl.glFlush();
}
public void displayChanged(GLDrawable gLDrawable, boolean modeChanged,
boolean deviceChanged) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
private int prevMouseX, prevMouseY;
private boolean mouseRButtonDown = false;
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
Dimension size = e.getComponent().getSize();
GLCanvas canvas = (GLCanvas) e.getSource();
canvas.repaint();
//System.out.println(size.width + " " + size.height);
float thetaX = 360.0f * (float) (x - prevMouseX) / (float) size.width;
float thetaY = 360.0f * (float) ( -prevMouseY + y) / (float) size.height;
prevMouseX = x;
prevMouseY = y;
rquadX += thetaX;
rtriX += thetaX;
rquadY += thetaY;
rtriY += thetaY;
}
public void mouseMoved(MouseEvent e) {
}
// Methods required for the implementation of MouseListener
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
prevMouseX = e.getX();
prevMouseY = e.getY();
gl.glInitNames();
}
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {}