Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.util.glu.GLU.gluPerspective;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.input.Keyboard;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- public class StarField {
- public static class Star {
- public static final Random rnd = new Random();
- public static final int SIZE_DIM = 100;
- public static final int NUM_PIZZA_SLICES = 16;
- public static final int SUBDIVISIONS = 16;
- int render;
- float x, y, z, size, half;
- boolean compiled;
- public Star() {
- x = rnd.nextFloat() * SIZE_DIM * 2 - SIZE_DIM;
- y = rnd.nextFloat() * SIZE_DIM * 2 - SIZE_DIM;
- z = -rnd.nextFloat() * SIZE_DIM;
- size = (rnd.nextFloat() + 0.5F) * 2 - 0.5F;
- half = size / 2;
- }
- void render() {
- if (compiled)
- glCallList(render);
- else {
- compiled = true;
- render = glGenLists(1);
- glNewList(render, GL_COMPILE);
- glPushMatrix();
- glTranslatef(x, y, z);
- glScalef(half, half, 1);
- glBegin(GL_TRIANGLE_FAN);
- glVertex2f(0, 0);
- for (int i = 0; i <= NUM_PIZZA_SLICES; i++) {
- double angle = Math.PI * 2 * i / SUBDIVISIONS;
- glVertex3f((float) Math.cos(angle), (float) Math.sin(angle), 0);
- }
- glEnd();
- glPopMatrix();
- glEndList();
- }
- }
- }
- public static final int WIDTH = 800;
- public static final int HEIGHT = 600;
- public List<Star> stars = new ArrayList<Star>();
- public StarField() {
- for (int i = 0; i < 500; i++)
- stars.add(new Star());
- try {
- Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
- Display.setTitle("Star Field - GenuineSounds");
- Display.create();
- Display.setVSyncEnabled(true);
- Keyboard.create();
- }
- catch (LWJGLException e) {
- e.printStackTrace();
- }
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(70, (float) WIDTH / (float) HEIGHT, 0.001F, 1000);
- glMatrixMode(GL_MODELVIEW);
- float cameraX = 0;
- float cameraY = 0;
- float cameraZ = 0;
- float cameraXDelta = 0;
- float cameraYDelta = 0;
- float cameraZDelta = 0;
- float cameraSpeed = 0.05f;
- while (!Display.isCloseRequested()) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glTranslatef(cameraX, cameraY, cameraZ);
- cameraXDelta *= 0.975;
- cameraYDelta *= 0.975;
- cameraZDelta *= 0.975;
- if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
- cameraXDelta -= 0.1f;
- if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
- cameraXDelta += 0.1f;
- if (Keyboard.isKeyDown(Keyboard.KEY_UP))
- cameraYDelta -= 0.1f;
- if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
- cameraYDelta += 0.1f;
- if (Keyboard.isKeyDown(Keyboard.KEY_S))
- cameraZDelta -= 0.1f;
- if (Keyboard.isKeyDown(Keyboard.KEY_W))
- cameraZDelta += 0.1f;
- cameraX += cameraSpeed * cameraXDelta;
- cameraY += cameraSpeed * cameraYDelta;
- cameraZ += cameraSpeed * cameraZDelta;
- glShadeModel(GL_SMOOTH);
- glEnable(GL_POINT_SMOOTH);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- for (Star star : stars)
- star.render();
- glPopMatrix();
- Display.update();
- Display.sync(60);
- }
- Keyboard.destroy();
- Display.destroy();
- System.exit(0);
- }
- public static void main(String[] args) {
- new StarField();
- }
- }
Advertisement