我的GLJPanel display()方法需要太多时间才能运行

时间:2017-04-18 15:58:58

标签: java jogl display

这是我的问题。我正在使用JOGL库来创建3D游戏。到目前为止一切正常,但出现了问题。我使用while循环,只重复gljpanel.display();。问题是这个方法在执行我的面板的display(GLAutoDrawable glad)方法之前会休眠大约4秒,导致帧速率非常低且无法忍受。我只是想知道什么可以使这个方法睡觉。以下是我使GLJPanel工作的代码:

package Main;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Frame extends JFrame {

    private static Panel panel = new Panel();
    private static keyListener klistener = new keyListener();
    private static mouseListener mlistener = new mouseListener();
    private static mouseWheelListener mwlistener = new mouseWheelListener();
    public int[] screenSize = new int[] {getToolkit().getScreenSize().width, getToolkit().getScreenSize().height};

    public void init() { //called firstly
        setTitle("Title");
        setBounds(0, 0, screenSize[0], screenSize[1]);
        setLocationRelativeTo(null);
        setUndecorated(true);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addKeyListener(klistener);
        addMouseListener(mlistener);
        addMouseWheelListener(mwlistener);

        setContentPane(panel);
    }

    public void trame() { //called secondly
        while (true)
            panel.display();
    }
}

这是我的GLJPanel:

package Main;

import java.awt.Cursor;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLJPanel;
import javax.media.opengl.glu.GLU;
import BasicModels.*;
import LivingBeings.Player;

@SuppressWarnings("serial")
public class Panel extends GLJPanel implements GLEventListener {

    public static int[] mouse = new int[2];

    public Panel() {
        super();
        this.initComponents();
    }

    private void initComponents() {
        this.addGLEventListener(this);
    }

    public void init(GLAutoDrawable glad) {
        GL2 gl2 = glad.getGL().getGL2();
        gl2.glShadeModel(GL2.GL_SMOOTH);
        gl2.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl2.glClearDepth(1.0f);
        gl2.glEnable(GL2.GL_DEPTH_TEST);
        gl2.glDepthFunc(GL2.GL_LEQUAL);
        gl2.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
    }

    public void dispose(GLAutoDrawable glad) {}

    public void display(GLAutoDrawable glad) {
        System.out.println("test"); //printed around 4 seconds after "panel.display();" 
        GL2 gl2 = glad.getGL().getGL2();
        GLU glu = new GLU();

        gl2.glViewport(0, 0, Main.frame.screenSize[0], Main.frame.screenSize[1]);

        gl2.glMatrixMode(GL2.GL_PROJECTION);
        gl2.glLoadIdentity();

        glu.gluPerspective(45, (double) Main.frame.screenSize[0] / (double) Main.frame.screenSize[1], 0.1, 100.0);

        Camera.refresh(5, 5, 15, true);

        Point target = Camera.pos.add(Camera.target);
        glu.gluLookAt(Camera.pos.x, Camera.pos.y, Camera.pos.z, target.x, target.y, target.z, 0, 1, 0);

        gl2.glMatrixMode(GL2.GL_MODELVIEW);
        gl2.glLoadIdentity();
        gl2.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

        setOpaque(false);

        gl2.glEnable(GL2.GL_LIGHTING);
        if (Light.activeLights[0] != null) gl2.glEnable(GL2.GL_LIGHT0);
        if (Light.activeLights[1] != null) gl2.glEnable(GL2.GL_LIGHT1);
        if (Light.activeLights[2] != null) gl2.glEnable(GL2.GL_LIGHT2);
        if (Light.activeLights[3] != null) gl2.glEnable(GL2.GL_LIGHT3);
        if (Light.activeLights[4] != null) gl2.glEnable(GL2.GL_LIGHT4);
        if (Light.activeLights[5] != null) gl2.glEnable(GL2.GL_LIGHT5);
        if (Light.activeLights[6] != null) gl2.glEnable(GL2.GL_LIGHT6);
        if (Light.activeLights[7] != null) gl2.glEnable(GL2.GL_LIGHT7);

        gl2.glPolygonMode(GL2.GL_FRONT, GL2.GL_FILL);

        gl2.glEnable(GL2.GL_BLEND);
        gl2.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);

        Drawer3D.draw(gl2);

        gl2.glMatrixMode(GL2.GL_PROJECTION);
        gl2.glPushMatrix();
        gl2.glLoadIdentity();
        glu.gluOrtho2D(0, Main.frame.screenSize[0], Main.frame.screenSize[1], 0);
        gl2.glMatrixMode(GL2.GL_MODELVIEW);
        gl2.glPushMatrix();
        gl2.glLoadIdentity();

        Drawer2D.draw(gl2);

        gl2.glPopMatrix();
        gl2.glMatrixMode(GL2.GL_PROJECTION);
        gl2.glPopMatrix();

        synchronized (Keys.loadingWorldKey) {
            for (int i = 0; i < Player.players.size(); i++)
                Player.players.get(i).draw();
        }

        controllerListener.poll();

        gl2.glBegin(GL2.GL_LINES);
            gl2.glVertex3d(0, 0, 0);
            gl2.glVertex3d(1, 0, 0);
            gl2.glVertex3d(0, 0, 0);
            gl2.glVertex3d(0, 1, 0);
            gl2.glVertex3d(0, 0, 0);
            gl2.glVertex3d(0, 0, 1);
        gl2.glEnd();
    }

    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
        setSize(Main.frame.screenSize[0], Main.frame.screenSize[1]);
    }
}

感谢您的回答。

0 个答案:

没有答案