JFrame + JOGL仅在调整大小或最小化+最大化时显示内容

时间:2012-05-11 09:43:42

标签: java eclipse opengl graphics jogl

我正在尝试JOGL,我遇到了一个对我来说真的很新的问题。启动程序后,我看到一个空白窗口。调整大小后,我通常可以看到它的内容,或者我是否进行了最小化/恢复。我想事件中有一些东西。创建窗口后不会调用 init(),但是在第一次触发调整大小或最小化之后。

以下是我用于创建窗口和设置OpenGL的代码:

package com.cogwheel.framework.graphics;

import java.awt.BorderLayout;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.*;
import javax.swing.JFrame;

import com.cogwheel.framework.init.CWGPreferences;
import com.cogwheel.framework.util.CWGDebug;
import com.jogamp.opengl.util.Animator;

public class CWGOpenGLScreen extends JFrame implements GLEventListener {
private static final String TAG = "CWGOpenGLScreen";

private GLCanvas mCanvas;
private long fpsLast = System.currentTimeMillis();

public CWGOpenGLScreen(){
    this.setTitle(CWGPreferences.WINDOW_NAME);
    this.setSize(CWGPreferences.WINDOW_SIZE);
    this.setLayout(new BorderLayout());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ///this.setResizable(false);
    this.setVisible(true);

    CWGDebug.info(TAG, "Window created!");

    CWGSetupGL();
}
private void CWGSetupGL(){
    GLCapabilities mCaps = new GLCapabilities(null);
    mCaps.setHardwareAccelerated(true);
    mCaps.setDoubleBuffered(true);

    mCanvas = new GLCanvas(mCaps);
    mCanvas.addGLEventListener(this);

    this.add(mCanvas, BorderLayout.CENTER);

    Animator animator = new Animator(mCanvas);
    animator.start();
}

public void CWGDrawScene(GLAutoDrawable drawable)
{
    CWGCalculateFPS();

    GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    gl.glLoadIdentity();


    gl.glBegin(GL.GL_TRIANGLES);
        gl.glColor3f(1.0f, 0.0f, 0.0f);
        gl.glVertex3f(1.0f / 5 , 0.0f, 0.0f);

        gl.glColor3f(0.0f, 1.0f, 0.0f);
        gl.glVertex3f(1.0f / 5, 1.0f / 5, 0.0f);

        gl.glColor3f(0.0f, 0.0f, 1.0f);
        gl.glVertex3f(0.0f, 1.0f / 5, 1.0f / 5);
    gl.glEnd();
    gl.glFlush();
}

public void CWGCalculateFPS(){
    this.setTitle(CWGPreferences.WINDOW_NAME + " [" + 1000 / (System.currentTimeMillis() - fpsLast) + "]");
    fpsLast = System.currentTimeMillis();
}

public void init(GLAutoDrawable drawable){
        /*GL2 gl = drawable.getGL().getGL2();

        gl.glClearColor(0, 0, 0, 0);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(0, 1, 0, 1, -1, 1);
        */
        CWGDebug.info(TAG, "Init called!");
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height){}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged){}
public void display(GLAutoDrawable drawable){ CWGDrawScene(drawable); }
public void dispose(GLAutoDrawable drawable){}

}

我知道,代码质量很差,没有时间进行清理。对不起。

编辑:遇到问题,在GLEventListener未初始化之前,不应显示JFrame。

1 个答案:

答案 0 :(得分:0)

我发现了你的错误(它与eclipse没有关系):每当画布调整大小时你应该设置视口(调用glViewport)=>所以你在GLEventListener的reshape()方法中调用glViewport(x,y,widht,height)。这样OpenGL总是知道在哪里绘制场景,而不仅仅是在调整帧大小时。

此外,不要忘记在显示之前设置屏幕(调用CWGSetupGL()) (否则你仍会遇到同样的问题,但只有一段时间)。

这里修改过的代码(我已经删除了对CWGDebug.info的调用,因为你没有提供这个类,但你可以把它们放回去):

package com.cogwheel.framework.graphics;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

import com.jogamp.opengl.util.Animator;

public class CWGOpenGLScreen extends JFrame implements GLEventListener {

    public static void main(String[] args) {
        new CWGOpenGLScreen().setVisible(true);
    }

private static final long serialVersionUID = 635066680731362587L;

private static final String TAG = "CWGOpenGLScreen";

private GLCanvas mCanvas;
private long fpsLast = System.currentTimeMillis();

public CWGOpenGLScreen(){
    this.setTitle(TAG);
    this.setSize(640,480);
    this.setLayout(new BorderLayout());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ///this.setResizable(false);

    CWGSetupGL();
    this.setVisible(true);

//    CWGDebug.info(TAG, "Window created!");
}
private void CWGSetupGL(){
    GLCapabilities mCaps = new GLCapabilities(null);
    mCaps.setHardwareAccelerated(true);
    mCaps.setDoubleBuffered(true);

    mCanvas = new GLCanvas(mCaps);
    mCanvas.addGLEventListener(this);

    this.add(mCanvas, BorderLayout.CENTER);

    final Animator animator = new Animator(mCanvas);
    animator.start();

    addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            animator.stop();
            System.exit(0);
        }



    });
}

public void CWGDrawScene(GLAutoDrawable drawable)
{
    CWGCalculateFPS();

    GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    gl.glLoadIdentity();


    gl.glBegin(GL.GL_TRIANGLES);
        gl.glColor3f(1.0f, 0.0f, 0.0f);
        gl.glVertex3f(1.0f / 5 , 0.0f, 0.0f);

        gl.glColor3f(0.0f, 1.0f, 0.0f);
        gl.glVertex3f(1.0f / 5, 1.0f / 5, 0.0f);

        gl.glColor3f(0.0f, 0.0f, 1.0f);
        gl.glVertex3f(0.0f, 1.0f / 5, 1.0f / 5);
    gl.glEnd();
    gl.glFlush();
}

public void CWGCalculateFPS(){
    this.setTitle(TAG + " [" + 1000 / (System.currentTimeMillis() - fpsLast) + "]");
    fpsLast = System.currentTimeMillis();
}

public void init(GLAutoDrawable drawable){
        /*GL2 gl = drawable.getGL().getGL2();

        gl.glClearColor(0, 0, 0, 0);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(0, 1, 0, 1, -1, 1);
        */
//        CWGDebug.info(TAG, "Init called!");
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height){
    GL2 gl = drawable.getGL().getGL2();
    gl.glViewport(x, y, width, height);
}
//public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged){}
public void display(GLAutoDrawable drawable){ CWGDrawScene(drawable); }
public void dispose(GLAutoDrawable drawable){}
}

问候:)

编辑:道歉,我没有看到你通过编辑回答了你自己的问题。 无论如何,我仍然认为你最好在reshape()方法中调用glViewport。