glCallList在JOGL中没有效果编码

时间:2014-09-11 16:53:04

标签: java opengl jogl displaylist

当我尝试使用显示列表渲染三角形时,没有显示任何内容。谁能告诉我为什么?

在display()方法中,调用renderTriangle(gl,100);直接按预期工作,屏幕上出现一个200像素宽的三角形。但gl.glCallList(列表);电话似乎没有效果。屏幕上根本没有显示任何内容。

对于那些不熟悉JOGL的人,这里是方法调用顺序。初始化,重塑,显示,显示,显示...

我的buildDisplayList方法可以吗?调用glCallList是对的吗?我是否打算进行任何额外设置才能使其正常工作?

public class TriDemo extends GLJPanel implements GLEventListener {

private int list;

public TriDemo(GLCapabilities glCapabilities, int width, int height) {
    super(glCapabilities);
    this.setPreferredSize(new Dimension(width, height));
    this.addGLEventListener(this);
}

private void renderTriangle(GL2 gl, float size) {
    gl.glBegin(GL_TRIANGLES);
        gl.glVertex3f(-1*size, -1*size, 0);
        gl.glVertex3f(1*size, -1*size, 0);
        gl.glVertex3f(0*size, 1*size, 0);
    gl.glEnd();
}

private void buildDisplayList(GL2 gl) {
    list = gl.glGenLists(1);
    gl.glNewList(list, GL_COMPILE);
        renderTriangle(gl, 100);
    gl.glEndList();
}

@Override
public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glClearColor(0, 0, 0.2f, 1);

    buildDisplayList(gl);
}

@Override
public void dispose(GLAutoDrawable drawable) { }

@Override
public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();

    gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    gl.glColor3f(0, 1, 0);
    //renderTriangle(gl, 100);
    gl.glCallList(list);
}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    GL2 gl = drawable.getGL().getGL2();
    GLU glu = GLU.createGLU(gl);

    gl.glMatrixMode(GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluOrtho2D(-width/2, width/2, -height/2, height/2);
    gl.glMatrixMode(GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glViewport(0, 0, width, height);
}

}

可运行示例的其他类:

public class AppWindow extends JFrame {

public AppWindow() {
    super("Tri Test);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    GLProfile glProfile = GLProfile.getDefault();
    GLCapabilities glCapabilities = new GLCapabilities(glProfile);
    GLJPanel panel = new TriDemo(glCapabilities, 800, 600);
    FPSAnimator animator = new FPSAnimator(panel, 60);
    animator.start();
    this.getContentPane().add(panel);
    this.pack();
    this.setVisible(true);
}

public static void main(String[] args) {
    new AppWindow();
}

1 个答案:

答案 0 :(得分:0)

在视口准备好之前调用buildDisplayList()函数。这就是屏​​幕上没有任何内容呈现的原因。该 应在buildDisplayList()函数中调用display(),即在视口和GL初始化之后。