GenuineSounds

Star Field

May 26th, 2012
242
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. import static org.lwjgl.opengl.GL11.*;
  2. import static org.lwjgl.util.glu.GLU.gluPerspective;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Random;
  7.  
  8. import org.lwjgl.LWJGLException;
  9. import org.lwjgl.input.Keyboard;
  10. import org.lwjgl.opengl.Display;
  11. import org.lwjgl.opengl.DisplayMode;
  12.  
  13. public class StarField {
  14.  
  15.     public static class Star {
  16.  
  17.         public static final Random rnd = new Random();
  18.  
  19.         public static final int SIZE_DIM = 100;
  20.  
  21.         public static final int NUM_PIZZA_SLICES = 16;
  22.  
  23.         public static final int SUBDIVISIONS = 16;
  24.  
  25.         int render;
  26.  
  27.         float x, y, z, size, half;
  28.  
  29.         boolean compiled;
  30.  
  31.         public Star() {
  32.             x = rnd.nextFloat() * SIZE_DIM * 2 - SIZE_DIM;
  33.             y = rnd.nextFloat() * SIZE_DIM * 2 - SIZE_DIM;
  34.             z = -rnd.nextFloat() * SIZE_DIM;
  35.             size = (rnd.nextFloat() + 0.5F) * 2 - 0.5F;
  36.             half = size / 2;
  37.         }
  38.  
  39.         void render() {
  40.             if (compiled)
  41.                 glCallList(render);
  42.             else {
  43.                 compiled = true;
  44.                 render = glGenLists(1);
  45.                 glNewList(render, GL_COMPILE);
  46.                 glPushMatrix();
  47.                 glTranslatef(x, y, z);
  48.                 glScalef(half, half, 1);
  49.                 glBegin(GL_TRIANGLE_FAN);
  50.                 glVertex2f(0, 0);
  51.                 for (int i = 0; i <= NUM_PIZZA_SLICES; i++) {
  52.                     double angle = Math.PI * 2 * i / SUBDIVISIONS;
  53.                     glVertex3f((float) Math.cos(angle), (float) Math.sin(angle), 0);
  54.                 }
  55.                 glEnd();
  56.                 glPopMatrix();
  57.                 glEndList();
  58.             }
  59.         }
  60.     }
  61.  
  62.     public static final int WIDTH = 800;
  63.  
  64.     public static final int HEIGHT = 600;
  65.  
  66.     public List<Star> stars = new ArrayList<Star>();
  67.  
  68.     public StarField() {
  69.         for (int i = 0; i < 500; i++)
  70.             stars.add(new Star());
  71.  
  72.         try {
  73.             Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
  74.             Display.setTitle("Star Field - GenuineSounds");
  75.             Display.create();
  76.             Display.setVSyncEnabled(true);
  77.             Keyboard.create();
  78.         }
  79.         catch (LWJGLException e) {
  80.             e.printStackTrace();
  81.         }
  82.  
  83.         glMatrixMode(GL_PROJECTION);
  84.         glLoadIdentity();
  85.         gluPerspective(70, (float) WIDTH / (float) HEIGHT, 0.001F, 1000);
  86.         glMatrixMode(GL_MODELVIEW);
  87.  
  88.         float cameraX = 0;
  89.         float cameraY = 0;
  90.         float cameraZ = 0;
  91.         float cameraXDelta = 0;
  92.         float cameraYDelta = 0;
  93.         float cameraZDelta = 0;
  94.         float cameraSpeed = 0.05f;
  95.  
  96.         while (!Display.isCloseRequested()) {
  97.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  98.  
  99.             glPushMatrix();
  100.             glTranslatef(cameraX, cameraY, cameraZ);
  101.  
  102.             cameraXDelta *= 0.975;
  103.             cameraYDelta *= 0.975;
  104.             cameraZDelta *= 0.975;
  105.  
  106.             if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
  107.                 cameraXDelta -= 0.1f;
  108.             if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
  109.                 cameraXDelta += 0.1f;
  110.             if (Keyboard.isKeyDown(Keyboard.KEY_UP))
  111.                 cameraYDelta -= 0.1f;
  112.             if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
  113.                 cameraYDelta += 0.1f;
  114.             if (Keyboard.isKeyDown(Keyboard.KEY_S))
  115.                 cameraZDelta -= 0.1f;
  116.             if (Keyboard.isKeyDown(Keyboard.KEY_W))
  117.                 cameraZDelta += 0.1f;
  118.  
  119.             cameraX += cameraSpeed * cameraXDelta;
  120.             cameraY += cameraSpeed * cameraYDelta;
  121.             cameraZ += cameraSpeed * cameraZDelta;
  122.  
  123.             glShadeModel(GL_SMOOTH);
  124.  
  125.             glEnable(GL_POINT_SMOOTH);
  126.             glEnable(GL_BLEND);
  127.             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  128.  
  129.             for (Star star : stars)
  130.                 star.render();
  131.  
  132.             glPopMatrix();
  133.  
  134.             Display.update();
  135.             Display.sync(60);
  136.         }
  137.  
  138.         Keyboard.destroy();
  139.         Display.destroy();
  140.         System.exit(0);
  141.     }
  142.  
  143.     public static void main(String[] args) {
  144.         new StarField();
  145.     }
  146.  
  147. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment