"On-demand" mode modification to tutoria

How about it? :slight_smile:

If I should give up the hope of seeing this, tell me now!

Then hate me when thou wilt; if ever, now;
Now, while the world is bent my deeds to cross,
Join with the spite of fortune, make me bow,
And do not drop in for an after-loss:

LOL

sorry, no help, just snicker :wink:

I see that nnevatie is back, so there’s hope for me. Otherwise, another sad Shakespeare sonnet.

Okey, I have to ask:
what do you mean by: " “On-demand” mode modification to tutoria"?

// Gregof

He probably means that it should run on an IBM server ;D

Pahidla, you must be referring to this thread:

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=jogl;action=display;num=1103394116

…in that case, my bad, sorry - I did promise to deliver a simple example that shows the basic usage of “on-demand” usage of JOGL (manual refreshing of GLDrawables).

The thing is I’ve been busy with my studies lately - I’m currently working on a 100% Java rigid body dynamics framework/library.

But, if there is any specific question about JOGL, that I could help you with, please do ask without hesitation.

Funny, rigid bodies is the subject of my research, too. Maybe we can collaborate on that!

For example, I’m thinking right now of how to intelligently add energy dissipation to the rigid body or how to describe it in non-affine coordinates. Will your framework have support for planetary motion of rigid bodies?

As far as on-demand jogl, I just can’t figure out how to get things started. How about an application this simple:
When the application starts it shows a triangle.

For me, the init() method does not trigger until I manually resize the parent frame.

Thanks a lot!

Pahidla

On-demand rendering is just calling repaint() on your canvas. As opposed to using an animator with just calls repaint continuously on it’s own thread. That is why your canvas is initialized when you resize the screen. Just throw a repaint() call in your mouselistener or keylistener or what ever mechanism you use to change what ever you are visualizing.

But how do I make it show anything before the first mouse or key event?

[quote]But how do I make it show anything before the first mouse or key event?
[/quote]
What keeps you from calling glCanvas.repaint() right after you’ve created it? :slight_smile:

Remember, that repaint method does not cause an immediate refresh of the component but an event that requests the refresh in the AWT thread.

OK, thanks for all the help!
After simplifying my code things just worked. Here’s my on-demand mode demo if anyone finds it useful. Drag to rotate, shift-drag to zoom, double-click to change the presence of perspective, ctrl-drag to change the degree of perspective.

Please comment if I’m doing something wrong.

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import net.java.games.jogl.*;

public class OnDemandDemo {
public static void main(String[] inArgs) { new OnDemandDemo(); }

private GLCanvas myGLCanvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());

private double myTheta = Math.PI/2;
private double myPhi = -Math.PI/2;
private double myZoom = 2.0;
private double myPerspAngle = Math.PI/3;
private boolean myPerspectivePresent = true;

public OnDemandDemo() {
super();

MouseListeners il = new MouseListeners();
myGLCanvas.addMouseListener(il);
myGLCanvas.addMouseMotionListener(il);
myGLCanvas.addGLEventListener(new OnScreenGLEventListener());
myGLCanvas.setSize(400, 400);
myGLCanvas.repaint();

javax.swing.JFrame frame = new javax.swing.JFrame("On Demand Mode Demonstration");
frame.getContentPane().add(myGLCanvas);
frame.pack();
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}

public class OnScreenGLEventListener implements GLEventListener {
public void init(GLDrawable inDrawable) {
GL gl = inDrawable.getGL();

  gl.glClearColor(1f, 1f, 1f, 1f);

  gl.glEnable(GL.GL_DEPTH_TEST);
  gl.glEnable(GL.GL_POLYGON_OFFSET_FILL);
  gl.glEnable(GL.GL_POLYGON_OFFSET_LINE);

  gl.glEnable(GL.GL_LIGHTING);
  gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, new float[] { 1f, 1f, 1f, 1f });

  gl.glDisable(GL.GL_CULL_FACE) ;
  gl.glLightModeli(GL.GL_LIGHT_MODEL_TWO_SIDE, GL.GL_TRUE) ;
}

public void display(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
public void displayChanged(GLDrawable inDrawable, boolean inWhat, boolean inWhen) {}
public void reshape(GLDrawable drawable, int x, int y, int width, int height) {}

public void display(GLDrawable inDrawable) {
  GL gl = inDrawable.getGL();
  gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

  pSetView(inDrawable, false);

  gl.glColor3d(0, 0, 0);
  gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
  gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_EMISSION, new float[] { 0f, 0f, 0f } );
  gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE, new float[] { 0f, 0f, 0f });
  gl.glBegin(GL.GL_LINE_LOOP);
  gl.glVertex3d( 1, -1, -1);
  gl.glVertex3d( 1, -1,  1);
  gl.glVertex3d(-1, -1,  1);
  gl.glVertex3d(-1, -1, -1);
  gl.glEnd();

  gl.glBegin(GL.GL_LINE_LOOP);
  gl.glVertex3d( 1, 1, -1);
  gl.glVertex3d( 1, 1,  1);
  gl.glVertex3d(-1, 1,  1);
  gl.glVertex3d(-1, 1, -1);
  gl.glEnd();

  gl.glFlush();
}

}

public void pSetView(GLDrawable inDrawable, boolean inFlip) {
pSetView( (double) myGLCanvas.getSize().width / myGLCanvas.getSize().height, inDrawable, inFlip);
}

public void pSetView(double inAspect, GLDrawable inDrawable, boolean inFlip) {
GL gl = inDrawable.getGL();
GLU glu = inDrawable.getGLU();

gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();

double dim = myZoom;
if (myPerspectivePresent) {
  glu.gluPerspective(myPerspAngle*180/Math.PI, inAspect, .1, 100000.0);
  dim = myZoom/Math.tan(myPerspAngle/ 2);
}
else
  gl.glOrtho(-myZoom*inAspect, myZoom*inAspect, -myZoom, myZoom, -10.0, 100.0);

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
double upOrDown = 1 - 2 * (int) (gMod(myTheta, 2*Math.PI)/Math.PI);
glu.gluLookAt(dim*Math.sin(myTheta)*Math.cos(myPhi),
              dim*Math.sin(myTheta)*Math.sin(myPhi),
              dim*Math.cos(myTheta),
              0.0, 0.0, 0.0,
              0.0, 0.0, (!inFlip)?upOrDown:-upOrDown);

}

public static double gMod(double inA, double inM) {
while (inA > inM) inA -= inM;
while (inA < 0) inA += inM;
return inA;
}

private class MouseListeners implements MouseListener, MouseMotionListener {
private int myLastX, myLastY;
public void mousePressed(MouseEvent inME) {
if (inME.getButton() == MouseEvent.BUTTON1) {
myLastX = inME.getX();
myLastY = inME.getY();
}
}

public void mouseDragged(MouseEvent inME) {
  if ((inME.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0) return;

  if ((inME.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0 )
    myZoom += (inME.getY() - myLastY)/30.0;
  else if ((inME.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0 )
    myPerspAngle += -(inME.getY() - myLastY)/600.0;
  else {
    myPhi   += -(inME.getX() - myLastX)/300.0;
    myTheta += -(inME.getY() - myLastY)/300.0;
  }
  myLastX = inME.getX();
  myLastY = inME.getY();

  myGLCanvas.display();
}

public void mouseMoved(MouseEvent inME) { }
public void mouseReleased(MouseEvent inME) { }

public void mouseClicked(MouseEvent inME) {
  if (inME.getButton() == MouseEvent.BUTTON1 && inME.getClickCount() == 2)
    myPerspectivePresent = !myPerspectivePresent;
  myGLCanvas.display();
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }

}
}

Thanks for the demo - I’ve been looking for something like this!

I see two problems on a Mac PowerBook with a Radeon 9600.

  • maximizing the window does not resize the canvas, although resizing the window usually does. The result is the figures are cropped to their previous window size and ocassionally there is junk in the rest of the window. I also see some occasional shards during resize. It’s as if the adjusted viewport sometimes lags behind the window changes.

  • The application hangs. I can’t find a simple trigger, but resizing, rotating, maximizing, restoring to the previous size when repeated for a while (10 seconds to a few minutes) cause a hang. Here’s a thread dump:

    pdj