2012-05-23 3 views
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; 
     } 
    } 
} 

UPDATE

는 지금은이 코드를 가지고 있지만 루프는 사람이 내가이 문제를 해결할 수있는 방법을 알고 않는, 매우 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

아이디어가 있으십니까? 어쩌면 나는 그것이 사라졌을 때 그것을 다시 설정할 수 있습니다. 하지만 그 중 하나를 수행하는 방법을 모르겠다 – core16

+0

게시물을 업데이 트되었습니다 ... – core16

답변

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); 
    } 
+0

에 의해, 나는 속도가 상수라고 가정, 그렇지 않으면 initialPosition의 계산을 변경해야합니다. – bmkorkut

+0

그것은 작동하지만, 정말 분명하기 때문에 전환을 원활하게 할 수있는 방법을 알고 있습니까? 난 무작위를 사용하기 때문에 이것은있을 수 있습니다? – core16

+0

랜덤 사용은 괜찮지 만, 0부터 screenWidth까지 Z에 대한 임의의 점을 얻어야합니다. 마찬가지로, 세로축의 경우; 0부터 screnHeight까지 시험; points [i] = new Point (random.nextInt (screenHeight + 20), 0, random.nextInt (screenWidth + 20)); [가정 된 x가 세로 축임] – bmkorkut

관련 문제