Make a triangle rotate

Hi,
I’m currently using JOGL ver. 1.1.1 and it seems to me that it calls method display(GLAutoDrawable) only once, whereas the code for C calls it every time it loops. Here is my code:


this is in Main.java:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test;
import javax.media.opengl.*;
import javax.swing.*;
/**
 *
 * @author ramon
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JFrame frame = new JFrame("Jogl Test");
        frame.setSize(640,480);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GLCanvas canvas = new GLCanvas();

        SimpleJogl glevent = new SimpleJogl();
        canvas.addGLEventListener(glevent);
        frame.add(canvas);
        frame.setVisible(true);
    }

}

this is in SimpleJogl.java:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;

/**
 *
 * @author ramon
 */
public class SimpleJogl implements GLEventListener {
    public SimpleJogl() {
    }

    public void init(GLAutoDrawable glad) {
        GL gl = glad.getGL();
        gl.setSwapInterval(1);
        gl.glShadeModel(GL.GL_SMOOTH);
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glClearDepth(1.0f);
        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glDepthFunc(GL.GL_LEQUAL);
        gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);

        
    }

    public void display(GLAutoDrawable glad) {
        GL gl = glad.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(-1.5f, 0.0f, -6.0f);
      gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f);
      gl.glBegin(GL.GL_TRIANGLES);		    // Drawing Using Triangles
        gl.glColor3f(1.0f, 0.0f, 0.0f);   // Set the current drawing color to red
        gl.glVertex3f( 0.0f, 1.0f, 0.0f);	// Top
        gl.glColor3f(0.0f, 1.0f, 0.0f);   // Set the current drawing color to green
        gl.glVertex3f(-1.0f,-1.0f, 0.0f);	// Bottom Left
        gl.glColor3f(0.0f, 0.0f, 1.0f);   // Set the current drawing color to blue
        gl.glVertex3f( 1.0f,-1.0f, 0.0f);	// Bottom Right
      gl.glEnd();				// Finished Drawing The Triangle
      gl.glLoadIdentity();
      gl.glTranslatef(1.5f, 0.0f, -6.0f);
      gl.glRotatef(rquad, 1.0f, 0.0f, 0.0f);
      gl.glBegin(GL.GL_QUADS);           	// Draw A Quad
        gl.glColor3f(0.5f, 0.5f, 1.0f);   // Set the current drawing color to light blue
        gl.glVertex3f(-1.0f, 1.0f, 0.0f);	// Top Left
        gl.glVertex3f( 1.0f, 1.0f, 0.0f);	// Top Right
        gl.glVertex3f( 1.0f,-1.0f, 0.0f);	// Bottom Right
        gl.glVertex3f(-1.0f,-1.0f, 0.0f);	// Bottom Left
      gl.glEnd();				// Done Drawing The Quad
      gl.glFlush();
      rtri += 0.2f;
      rquad += 0.15f;

    }

    public void reshape(GLAutoDrawable glad, int x, int y, int width, int height) {
        final GL gl = glad.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 displayChanged(GLAutoDrawable glad, boolean bln, boolean bln1) {
    }
    private float rquad = 0.0f;
    private float rtri = 0.0f;
}


I’d like to see the triangle rotating always.
Please help!

JOGL 1.1.1a is extremely outdated. Please use the latest version from www.jogamp.org

In JOGL (any version), display() gets called in three ways:

  1. In response to repaint() requests from AWT, so you get one display() when the frame is made visible
  2. If you use an Animator or FPSAnimotor to repeatedly call display()
  3. If you call it yourself (not recommended unless you know what you’re doing).

Are people not supposed to be using the GL object nowadays? Are my friend and I reading some old outdated tutorials? Is it a complete waste to not use a GL4?

JOGL has a very thin abstraction layer above OpenGL. In C/C++, this layer is replaced by GLUT for example. Both in Java and in C you have to use something to update your display frequently. In C, when using GLUT, you call glutPostRedisplay() as far as I know. In Java, when using JOGL, you use lhkbob’s suggestions, especially the 2 first ones.

Anyway, it does not mean that you don’t have to use GL objects. You seem not to understand what a GL object is. It is explained here:
http://michael-bien.com/mbien/entry/jogl_2_opengl_profiles_explained

There is an example of drawing of a triangle in JOGL 2 here:
http://jogamp.org/wiki/index.php/Using_JOGL_in_Java_Web_Start

Look at my example on Wikipedia.

You have to use GL objects to perform the actual GL operations. As you’ve been doing, you get the GL objects from the GLAutoDrawables passed into the methods from the GLEventListener. This is still the way to do things.

Simple answer is that you need to use the FPSAnimator to make it constantly redraw like it does in C based OpenGL.

Option 3 that lhkbob suggested is what I use and I don’t think it is a big deal - I just don’t want to have my display constantly repainting the same scene that doesn’t change. So I force a display update only on keyboard/mouse inputs or various event updates

When I said “not supposed to use a GL object” i meant a GL object as opposed to a GL2, 3, or 4. All of the later ones have the exact same fields as the original GL, so is there any reason to not use a GL4?

The numbers after them correspond to the OpenGL version profile that is used. Since OpenGL 3, OpenGL is no longer backwards compatible. Additionally, there are different profiles for desktop devices and mobile devices. The GL object represents the operations defined in OpenGL that are shared by every profile.

Another example is the GL2GL3 object, this is the extension of GL that defines the OpenGL operations shared between version 2.0 and 3.0. Depending on what you want to do, you must choose a different version to work with. I generally use GL2GL3 for a lot of my engine code that can be shared between versions (such as textures and VBOs) so I won’t have to rewrite it.

If you want to use the fixed functionality of OpenGL or run on older graphics cards, you’ll need to use GL2. If you want to use GL4, you’ll have to have OpenGL version 4.0 installed and supported on your graphics card.

If you can get away with programming to the GL interface without picking a specific version, I would try that because it will allow you to run your code across more computers more easily. If you’re the only one using your program and you want to program to 4.0 and you have OpenGL 4 (or are fine with only having it run on newer computers), then you can do that too.

Gotcha. So if we’re intending to make our game compatible with as many computers and GPU’s as we can, we should try to stick to GL or GL2. Currently we are using GL, but from what you’re saying it sounds like there’s really no reason we wouldn’t want to use GL2 instead.

Correct, GL2 should be a good starting point for you.