JOGL:我的立方体旋转速度不规则

发布于 2024-12-09 08:23:02 字数 5507 浏览 0 评论 0原文

我刚刚在 JOGL 中编写了一个简单的动画:一个 6 色立方体绕 y 轴旋转。但我的动画似乎有一点速度解决方法:有时它会加速,有时会减慢,尽管我只是使用一个浮点变量调用 glRotate() ,我在每次显示调用时都会增加该变量(我的代码如下)。

  • 我的处理器是奔腾双核 t3400
  • 我的操作系统是 Xubuntu 11.04 32 位
  • 我的显卡是 ATI Radeon HD4330 并且我安装了最新的专有驱动程序:11.6(在 AMD 网站上下载,而不是在 ubuntu 软件管理器上下载)

在 Eclipse EDI 中(我正在使用 Indigo 3.7.0),这就是我设置用户库的方式:

  • 我已经下载了JOGL 2.0 beta 23(从 2011 年 3 月 3 日开始)适用于 i586 linux
  • 在用户库中我导入了以下 jar:newt.all.jar、nativewindow.all.jar、jogl.all.jar、gluegen-rt.jar
  • 所有导入的 jar 链接到未压缩的 jogl 存档的 lib 子文件夹:所有 *.so 文件在哪里

这是我的三个类。

ArcballExperiment.java

package com.gmail.bernabe.laurent.jogl.arcball_experiment;

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

import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

import com.jogamp.opengl.util.Animator;

public class ArcBallExperiment extends JFrame {

    public ArcBallExperiment(){
        setTitle("ArcBall Experimentation");
        setSize(500, 500);
        setLocationRelativeTo(null);

        GLCanvas glCanvas = new GLCanvas();
        TheGLEventListener glEventListener = new TheGLEventListener();
        glCanvas.addGLEventListener(glEventListener);
        add(glCanvas);

        final Animator animator = new Animator(glCanvas);
        addWindowListener(new WindowAdapter() {

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


        });

        animator.start();
    }

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

    private static final long serialVersionUID = 1L;

}

Cube.java :

package com.gmail.bernabe.laurent.jogl.arcball_experiment;

import javax.media.opengl.GL2;

public class Cube {

    /**
     * Draws a cube in a GL2 context
     * @param gl - GL2 - GL2 context
     */
    public static void draw(GL2 gl){
        gl.glBegin(GL2.GL_QUADS);

            // front : blue
            gl.glColor3f(0.0f, 0.0f, 1.0f);
            gl.glVertex3f(-0.5f, +0.5f, +0.5f);
            gl.glVertex3f(-0.5f, -0.5f, +0.5f);
            gl.glVertex3f(+0.5f, -0.5f, +0.5f);
            gl.glVertex3f(+0.5f, +0.5f, +0.5f);

            // back : green
            gl.glColor3f(0.0f, 1.0f, 0.0f);
            gl.glVertex3f(+0.5f, -0.5f, -0.5f);
            gl.glVertex3f(+0.5f, +0.5f, -0.5f);
            gl.glVertex3f(-0.5f, +0.5f, -0.5f);
            gl.glVertex3f(-0.5f, -0.5f, -0.5f);

            // left : red
            gl.glColor3f(1.0f, 0.0f, 0.0f);
            gl.glVertex3f(-0.5f, +0.5f, +0.5f);
            gl.glVertex3f(-0.5f, +0.5f, -0.5f);
            gl.glVertex3f(-0.5f, -0.5f, -0.5f);
            gl.glVertex3f(-0.5f, -0.5f, +0.5f);

            // right : orange
            gl.glColor3f(1.0f, 0.4f, 0.0f);
            gl.glVertex3f(+0.5f, +0.5f, -0.5f);
            gl.glVertex3f(+0.5f, +0.5f, +0.5f);
            gl.glVertex3f(+0.5f, -0.5f, +0.5f);
            gl.glVertex3f(+0.5f, -0.5f, -0.5f);

            // top : white
            gl.glColor3f(1.0f, 1.0f, 1.0f);
            gl.glVertex3f(+0.5f, +0.5f, +0.5f);
            gl.glVertex3f(-0.5f, +0.5f, +0.5f);
            gl.glVertex3f(-0.5f, +0.5f, -0.5f);
            gl.glVertex3f(+0.5f, +0.5f, -0.5f);

            // bottom : yellow
            gl.glColor3f(1.0f, 1.0f, 0.0f);
            gl.glVertex3f(+0.5f, -0.5f, +0.5f);
            gl.glVertex3f(+0.5f, -0.5f, -0.5f);
            gl.glVertex3f(-0.5f, -0.5f, -0.5f);
            gl.glVertex3f(-0.5f, -0.5f, +0.5f);

        gl.glEnd();
    }

}

TheGLEventListener.java :

package com.gmail.bernabe.laurent.jogl.arcball_experiment;

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.gl2.GLUgl2;

public class TheGLEventListener implements GLEventListener {

    @Override
    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();

        glu.gluLookAt(
                0f, 0f, 3f,
                0f, 0f, 0f,
                0f, 1f, 0f
        );

        gl.glRotatef(rotationAngle, 0f, 1f, 0f);
        Cube.draw(gl);

        rotationAngle += 0.3f;
        if (rotationAngle >= 360f)
            rotationAngle %= 360f;
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
        // TODO Auto-generated method stub

    }

    @Override
    public void init(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glEnable(GL2.GL_DEPTH_TEST);
        gl.glDepthFunc(GL2.GL_LEQUAL);
        gl.glClearDepthf(1.0f);
    }

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

        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(60.0f, (float) width/height, 1f, 10f);

        gl.glMatrixMode(GL2.GL_MODELVIEW);
    }

    private GLUgl2 glu = new GLUgl2();
    private float rotationAngle = 0f;

}

我想这可能是因为我在为 JOGL eclipse 项目制作的用户库中导入了不兼容的 jar,但我不确定。

提前致谢

(ArcBall 实验的标题似乎不恰当,但它是对的:这个立方体旋转只是我在进行 arcball 实现之前再次记住 JOGL 编程的一个阶段。)

I've just written a simple animation in JOGL : a 6-colored cube spins around y axis. But it seems my animation has a little speed workaround : sometimes it accelerate, and sometimes it slows though i just call glRotate() with a float variable that I increment on each display call (my code is below).

  • My processor is a pentium dual core t3400
  • My operating system is Xubuntu 11.04 32 bits
  • My graphic card is ATI Radeon HD4330 and i've installed a recent proprietary driver : 11.6 (downloaded on AMD website, not on ubuntu software manager)

In Eclipse EDI (I'm using Indigo 3.7.0), this is how i've set my user library :

  • I've downloaded the JOGL 2.0 beta 23 (from 2011, march 03) for i586 linux
  • In the user library I imported the following jars : newt.all.jar, nativewindow.all.jar, jogl.all.jar, gluegen-rt.jar
  • All of the imported jars link to the lib subfolder of uncompressed jogl archive : where are all the *.so files

Here my three classes.

ArcballExperiment.java

package com.gmail.bernabe.laurent.jogl.arcball_experiment;

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

import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

import com.jogamp.opengl.util.Animator;

public class ArcBallExperiment extends JFrame {

    public ArcBallExperiment(){
        setTitle("ArcBall Experimentation");
        setSize(500, 500);
        setLocationRelativeTo(null);

        GLCanvas glCanvas = new GLCanvas();
        TheGLEventListener glEventListener = new TheGLEventListener();
        glCanvas.addGLEventListener(glEventListener);
        add(glCanvas);

        final Animator animator = new Animator(glCanvas);
        addWindowListener(new WindowAdapter() {

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


        });

        animator.start();
    }

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

    private static final long serialVersionUID = 1L;

}

Cube.java :

package com.gmail.bernabe.laurent.jogl.arcball_experiment;

import javax.media.opengl.GL2;

public class Cube {

    /**
     * Draws a cube in a GL2 context
     * @param gl - GL2 - GL2 context
     */
    public static void draw(GL2 gl){
        gl.glBegin(GL2.GL_QUADS);

            // front : blue
            gl.glColor3f(0.0f, 0.0f, 1.0f);
            gl.glVertex3f(-0.5f, +0.5f, +0.5f);
            gl.glVertex3f(-0.5f, -0.5f, +0.5f);
            gl.glVertex3f(+0.5f, -0.5f, +0.5f);
            gl.glVertex3f(+0.5f, +0.5f, +0.5f);

            // back : green
            gl.glColor3f(0.0f, 1.0f, 0.0f);
            gl.glVertex3f(+0.5f, -0.5f, -0.5f);
            gl.glVertex3f(+0.5f, +0.5f, -0.5f);
            gl.glVertex3f(-0.5f, +0.5f, -0.5f);
            gl.glVertex3f(-0.5f, -0.5f, -0.5f);

            // left : red
            gl.glColor3f(1.0f, 0.0f, 0.0f);
            gl.glVertex3f(-0.5f, +0.5f, +0.5f);
            gl.glVertex3f(-0.5f, +0.5f, -0.5f);
            gl.glVertex3f(-0.5f, -0.5f, -0.5f);
            gl.glVertex3f(-0.5f, -0.5f, +0.5f);

            // right : orange
            gl.glColor3f(1.0f, 0.4f, 0.0f);
            gl.glVertex3f(+0.5f, +0.5f, -0.5f);
            gl.glVertex3f(+0.5f, +0.5f, +0.5f);
            gl.glVertex3f(+0.5f, -0.5f, +0.5f);
            gl.glVertex3f(+0.5f, -0.5f, -0.5f);

            // top : white
            gl.glColor3f(1.0f, 1.0f, 1.0f);
            gl.glVertex3f(+0.5f, +0.5f, +0.5f);
            gl.glVertex3f(-0.5f, +0.5f, +0.5f);
            gl.glVertex3f(-0.5f, +0.5f, -0.5f);
            gl.glVertex3f(+0.5f, +0.5f, -0.5f);

            // bottom : yellow
            gl.glColor3f(1.0f, 1.0f, 0.0f);
            gl.glVertex3f(+0.5f, -0.5f, +0.5f);
            gl.glVertex3f(+0.5f, -0.5f, -0.5f);
            gl.glVertex3f(-0.5f, -0.5f, -0.5f);
            gl.glVertex3f(-0.5f, -0.5f, +0.5f);

        gl.glEnd();
    }

}

TheGLEventListener.java :

package com.gmail.bernabe.laurent.jogl.arcball_experiment;

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.gl2.GLUgl2;

public class TheGLEventListener implements GLEventListener {

    @Override
    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();

        glu.gluLookAt(
                0f, 0f, 3f,
                0f, 0f, 0f,
                0f, 1f, 0f
        );

        gl.glRotatef(rotationAngle, 0f, 1f, 0f);
        Cube.draw(gl);

        rotationAngle += 0.3f;
        if (rotationAngle >= 360f)
            rotationAngle %= 360f;
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
        // TODO Auto-generated method stub

    }

    @Override
    public void init(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glEnable(GL2.GL_DEPTH_TEST);
        gl.glDepthFunc(GL2.GL_LEQUAL);
        gl.glClearDepthf(1.0f);
    }

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

        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(60.0f, (float) width/height, 1f, 10f);

        gl.glMatrixMode(GL2.GL_MODELVIEW);
    }

    private GLUgl2 glu = new GLUgl2();
    private float rotationAngle = 0f;

}

I'm thinking it could be because I imported incompatible jars in the user library made for my JOGL eclipse projects, but I'm not sure.

Thanks in advance

(The title ArcBall experiment seems inapropriated, and it's right : this cube spinning is juste a phase for me to remember again the JOGL programming before going to arcball implementation.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈年往事 2024-12-16 08:23:02

最后我解决了这个问题:)
我将动画师实例更改为 FPSAnimator 而不是 Animator,以保持恒定的 FPS。只有一类需要更新:
ArcBallExperiment.java

package com.gmail.bernabe.laurent.jogl.arcball_experiment;

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

import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

import com.jogamp.opengl.util.FPSAnimator;

public class ArcBallExperiment extends JFrame {

    public ArcBallExperiment(){
        setTitle("ArcBall Experimentation");
        setSize(500, 500);
        setLocationRelativeTo(null);

        GLCanvas glCanvas = new GLCanvas();
        TheGLEventListener glEventListener = new TheGLEventListener();
        glCanvas.addGLEventListener(glEventListener);
        add(glCanvas);

        /*
         *  Here I limit the FPS to a quite lower value than default,
         *  thanks to the FPSAnimator class : 180 FPS here.
         *  That way, cube rotation speed will really be constant
         */
        final FPSAnimator animator = new FPSAnimator(glCanvas, 180);
        addWindowListener(new WindowAdapter() {

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


        });

        animator.start();
    }

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

    private static final long serialVersionUID = 1L;

}

Finally I solved the problem :)
I changed the animator instance to FPSAnimator instead of Animator, in order to keep a constant FPS. There is just one class to update :
ArcBallExperiment.java

package com.gmail.bernabe.laurent.jogl.arcball_experiment;

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

import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

import com.jogamp.opengl.util.FPSAnimator;

public class ArcBallExperiment extends JFrame {

    public ArcBallExperiment(){
        setTitle("ArcBall Experimentation");
        setSize(500, 500);
        setLocationRelativeTo(null);

        GLCanvas glCanvas = new GLCanvas();
        TheGLEventListener glEventListener = new TheGLEventListener();
        glCanvas.addGLEventListener(glEventListener);
        add(glCanvas);

        /*
         *  Here I limit the FPS to a quite lower value than default,
         *  thanks to the FPSAnimator class : 180 FPS here.
         *  That way, cube rotation speed will really be constant
         */
        final FPSAnimator animator = new FPSAnimator(glCanvas, 180);
        addWindowListener(new WindowAdapter() {

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


        });

        animator.start();
    }

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

    private static final long serialVersionUID = 1L;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文