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があなたの水平方向であると仮定しています。

頭に浮かぶ最も効率的な解決策は、画面の終わりに達したときに「ポイント」の位置を初期状態にリセットすることです。画面サイズに合った領域に星のセットを分散させます。そして、星を2回描画し、連続性をシミュレートするために、2番目の星を横軸の1画面幅にシフトします。

以下に擬似コードを書きました。それはおそらく欠陥がありますが、それはあなたにアイデアを与えることを目的としています。

    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 に答える