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