glNewList

Can anyone explain me why glNewList version is the slower one ?

THANKS

WITH glNewList

import net.java.games.jogl.*;

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

public class GLNewList extends JFrame implements GLEventListener,
MouseListener, MouseMotionListener {

public GLCanvas glCanvas;
private int x1, y1, x0, y0;
int[] lid = new int[100000];

public GLNewList() {
    glCanvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());

    glCanvas.addGLEventListener(this);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(glCanvas, BorderLayout.CENTER);
    setSize(200, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void init(GLDrawable drawable) {
    drawable.addMouseListener(this);
    drawable.addMouseMotionListener(this);

    GL gl = drawable.getGL();
    for (int i = 0; i < 100000; i++) {
        lid[i] = gl.glGenLists(1);
        gl.glNewList(lid[i], GL.GL_COMPILE);
        gl.glBegin(gl.GL_LINE_STRIP);
        gl.glVertex2f(0.0f, 0.0f);
        gl.glVertex2f(10.0f, 0.0f);
        gl.glVertex2f(10.0f, 10.0f);
        gl.glVertex2f(0.0f, 0.0f);
        gl.glEnd();
        gl.glEndList();
    }
}

public void display(GLDrawable drawable) {
    GL gl = drawable.getGL();

    gl.glClear(gl.GL_COLOR_BUFFER_BIT);

    long currentTime = System.nanoTime();
    gl.glPushMatrix();
    gl.glTranslatef(x1 - x0, y0 - y1, 0.0f);
    gl.glCallList(lid[0]);
    for (int i = 1; i < 100000; i++) {
        gl.glCallList(lid[i]);
    }

    gl.glPopMatrix();

    System.err.print((System.nanoTime() - currentTime) + " ");
}

public void reshape(GLDrawable drawable,
                    int x,
                    int y,
                    int width,
                    int height) {
    GL gl = drawable.getGL();
    gl.glViewport(x, y, width, height);
    gl.glMatrixMode(gl.GL_PROJECTION);
    gl.glLoadIdentity();

    GLU glu = glCanvas.getGLU();
    glu.gluOrtho2D(-width / 2, width / 2, -height / 2, height / 2);
    gl.glMatrixMode(gl.GL_MODELVIEW);
    gl.glLoadIdentity();
}

public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}

public void mouseDragged(MouseEvent e) {
    x1 = e.getX();
    y1 = e.getY();
    glCanvas.display();
}

public void mouseMoved(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
    x0 = e.getX();
    y0 = e.getY();
}

public void mouseReleased(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public static void main(String[] args) {
    new GLNewList().show();
}

}

++++++++++++++++++++++++++++++++++++

WITHOUT glNewList

import net.java.games.jogl.*;

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

public class GLNoNewList extends JFrame implements GLEventListener,
MouseListener, MouseMotionListener {

public GLCanvas glCanvas;
private int x1, y1, x0, y0;

public GLNoNewList() {
    glCanvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());

    glCanvas.addGLEventListener(this);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(glCanvas, BorderLayout.CENTER);
    setSize(200, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void init(GLDrawable drawable) {
    drawable.addMouseListener(this);
    drawable.addMouseMotionListener(this);
}

public void display(GLDrawable drawable) {
    GL gl = drawable.getGL();
    gl.glClear(gl.GL_COLOR_BUFFER_BIT);
    long currentTime = System.nanoTime();
    gl.glPushMatrix();
    gl.glTranslatef(x1 - x0, y0 - y1, 0.0f);

    for (int i = 1; i < 100000; i++) {
        gl.glBegin(gl.GL_LINE_STRIP);
        gl.glVertex2f(0.0f, 0.0f);
        gl.glVertex2f(10.0f, 0.0f);
        gl.glVertex2f(10.0f, 10.0f);
        gl.glVertex2f(0.0f, 0.0f);
        gl.glEnd();
    }

    gl.glPopMatrix();

    System.err.print((System.nanoTime() - currentTime) + " ");
}

public void reshape(GLDrawable drawable,
                    int x,
                    int y,
                    int width,
                    int height) {
    GL gl = drawable.getGL();
    gl.glViewport(x, y, width, height);
    gl.glMatrixMode(gl.GL_PROJECTION);
    gl.glLoadIdentity();

    GLU glu = glCanvas.getGLU();
    glu.gluOrtho2D(-width / 2, width / 2, -height / 2, height / 2);
    gl.glMatrixMode(gl.GL_MODELVIEW);
    gl.glLoadIdentity();
}

public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}

public void mouseDragged(MouseEvent e) {
    x1 = e.getX();
    y1 = e.getY();
    glCanvas.display();
}

public void mouseMoved(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
    x0 = e.getX();
    y0 = e.getY();
}

public void mouseReleased(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public static void main(String[] args) {
    new GLNoNewList().show();
}

}

Hi there!

I haven’t read all your code so I may be wrong, but you are compiling 100000 display lists with not much to do…

Try this instead:

lid = gl.glGenLists(1);
gl.glNewList(lid, GL.GL_COMPILE);
for (int i = 0; i < 100000; i++) {
gl.glBegin(gl.GL_LINE_STRIP);
gl.glVertex2f(0.0f, 0.0f);
gl.glVertex2f(10.0f, 0.0f);
gl.glVertex2f(10.0f, 10.0f);
gl.glVertex2f(0.0f, 0.0f);
gl.glEnd();
}

gl.glEndList();

and then in the draw function…

gl.glCallList(lid);

Hope it helps,
cheers,
hellder

It did it :-/