0

我编写了代码来绘制点(看起来像星星)并让它移动但是我如何让这个动画重复而不​​是在几秒钟后停止?最好的方法是什么,因为我不想在这个动画上浪费资源(如果它不删除丢失屏幕的点);

package game;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import java.util.Random;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.lwjgl.*;

public class Main {

    public Main() {
        try {
            Display.setDisplayMode(Display.getDesktopDisplayMode());
            Display.setFullscreen(true);
            Display.create();
        } catch(LWJGLException e) {
            e.printStackTrace();
        }

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        int width = Display.getWidth();
        int height = Display.getHeight();

        System.out.println("" + width);
        System.out.println("" + height);

        gluPerspective((float) 30, 1280f / 800f, 0.001f, 100);
        glMatrixMode(GL_MODELVIEW);

        Point[] points = new Point[10000];
        Random random = new Random();

        for(int i=0;i<points.length;i++) {
            points[i] = new Point((random.nextFloat() - 0.5f) * 100f, (random.nextFloat() - 0.5f) * 100f, random.nextInt(200) - 200);
        }

        float speed = 0.25f;

        while(!Display.isCloseRequested()) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            glTranslatef(0, 0, speed);

            glBegin(GL_POINTS);
            for(Point p : points) {
                glVertex3f(p.x, p.y, p.z);
            }
            glEnd();

            System.out.println("" + speed);

            Display.update();

            Display.sync(60);
        }
    }

    public static class Point {
        float x, y, z;

        public Point(float x, float y, float z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }
}

更新

现在我有这段代码,但循环非常明显,有谁知道我该如何解决这个问题?

package game;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import java.util.Random;

import org.lwjgl.opengl.*;
import org.lwjgl.*;

public class Main {

    public Main() {
        try {
            Display.setDisplayMode(Display.getDesktopDisplayMode());
            Display.setFullscreen(true);
            Display.create();
        } catch(LWJGLException e) {
            e.printStackTrace();
        }

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        int width = Display.getWidth();
        int height = Display.getHeight();

        System.out.println("" + width);
        System.out.println("" + height);

        gluPerspective((float) 30, 1280f / 800f, 0.001f, 100);
        glMatrixMode(GL_MODELVIEW);

        Point[] points = new Point[10000];
        Random random = new Random();

        for(int i=0;i<points.length;i++) {
            points[i] = new Point((random.nextFloat() - 0.5f) * 100f, (random.nextFloat() - 0.5f) * 100f, random.nextInt(200) - 200);
        }

        float speed = 0.25f;

        int count = 0;

        while(!Display.isCloseRequested()) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            count++;
            if(count < 100)
                glTranslatef(0, 0, speed);
              else
              {
                glTranslatef(0, 0, -100 * speed);
                count = 0;
              }

            glBegin(GL_POINTS);
            for(Point p : points) {
                glVertex3f(p.x, p.y, p.z);
            }

            glTranslatef(0, 0, width);

            glBegin(GL_POINTS);
              for(Point p : points) {
                glVertex3f(p.x, p.y, p.z);
              }

            glEnd();

            Display.update();

            Display.sync(60);
        }
    }

    public static class Point {
        float x, y, z;

        public Point(float x, float y, float z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }
}
4

1 回答 1

0

我看到您正在翻译整组星星(“点”数组)以产生运动效果。我猜过了一会儿,星星的集合到达尽头,你的屏幕变黑了。(所有星星都在屏幕外)我假设星星在水平滑动,z 是你的水平方向。

想到的最有效的解决方案是在到达屏幕末尾时将“点”的位置重置为初始状态。使一组星星分布在适合您的屏幕尺寸的区域中。并绘制星星两次,第二次在水平轴上移动一个屏幕宽度,以模拟连续性。

我在下面写了一个伪代码;它可能有问题,但它旨在给你这个想法。

    int count = 0;
    while(!Display.isCloseRequested()) {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

      count++;
      if(count < 100)
        glTranslatef(0, 0, speed);
      else
      {
        glTranslatef(0, 0, -100*speed); // To initial position
        count = 0;
      }

      glBegin(GL_POINTS);
      for(Point p : points) {
        glVertex3f(p.x, p.y, p.z);
      }

      glTranslatef(0, 0, screenWidth);

      glBegin(GL_POINTS);
      for(Point p : points) {
        glVertex3f(p.x, p.y, p.z);
      }

      glEnd();

      System.out.println("" + speed);

      Display.update();

      Display.sync(60);
    }
于 2012-05-23T14:21:39.007 回答