我制作了代码来绘制点(看起来像星星)并且我让它移动但是我如何重复这个动画而不是在几秒后停止?最好的方法是什么,因为我不想在此动画上浪费资源(如果它没有删除丢失屏幕的点数);
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;
}
}
}
更新
现在我有这个代码,但循环是非常obvios,有谁知道我怎么能解决这个问题?
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;
}
}
}
答案 0 :(得分: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);
}