|
| 1 | +package sample.java.project; |
| 2 | + |
| 3 | +import lombok.extern.java.Log; |
| 4 | +import org.lwjgl.LWJGLException; |
| 5 | +import org.lwjgl.opengl.Display; |
| 6 | +import org.lwjgl.opengl.DisplayMode; |
| 7 | +import org.lwjgl.opengl.GL11; |
| 8 | + |
| 9 | +/** |
| 10 | + * The main class. |
| 11 | + * |
| 12 | + * This is the main class of the application. It contains the main() |
| 13 | + * method, the first method called. |
| 14 | + */ |
| 15 | +@Log |
| 16 | +public class SampleLwjglProject implements Runnable { |
| 17 | + |
| 18 | + /** The delay between frames. */ |
| 19 | + private static final int FPS = 30; |
| 20 | + |
| 21 | + /** Width of the display. */ |
| 22 | + private static final int WIDTH = 800; |
| 23 | + |
| 24 | + /** Height of the display. */ |
| 25 | + private static final int HEIGHT = 600; |
| 26 | + |
| 27 | + /** The length of one second in milliseconds. */ |
| 28 | + private static final double SECOND = 1000d; |
| 29 | + |
| 30 | + /** The size of the quad. */ |
| 31 | + private static final float QUAD_SIZE = 200; |
| 32 | + |
| 33 | + /** Rate of red oscillation. */ |
| 34 | + private static final float RED_RATE = 1f; |
| 35 | + |
| 36 | + /** Rate of green oscillation. */ |
| 37 | + private static final float GREEN_RATE = 2.2f; |
| 38 | + |
| 39 | + /** Rate of blue oscillation. */ |
| 40 | + private static final float BLUE_RATE = 0.125f; |
| 41 | + |
| 42 | + /** Rate of rotation oscillation. */ |
| 43 | + private static final float ROT_RATE = 4f; |
| 44 | + |
| 45 | + /** Range of rotation oscillation. */ |
| 46 | + private static final float ROT_RANGE = 12; |
| 47 | + |
| 48 | + /** Height division for quad. */ |
| 49 | + private static final float HDIV = 4; |
| 50 | + |
| 51 | + /** Width division for quad. */ |
| 52 | + private static final float WDIV = 2; |
| 53 | + |
| 54 | + /** |
| 55 | + * The main class. |
| 56 | + * |
| 57 | + * Print the "Hello, world!" string. |
| 58 | + * |
| 59 | + * @param args application input arguments |
| 60 | + */ |
| 61 | + public static void main(final String[] args) { |
| 62 | + try { |
| 63 | + com.nullprogram.lwjgl.Lwjgl.setup(); |
| 64 | + } catch (java.io.IOException e) { |
| 65 | + log.severe("could not prepare libraries: " + e); |
| 66 | + System.exit(0); |
| 67 | + } |
| 68 | + new SampleLwjglProject().run(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public final void run() { |
| 73 | + try { |
| 74 | + Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); |
| 75 | + Display.create(); |
| 76 | + init(); |
| 77 | + } catch (LWJGLException e) { |
| 78 | + log.severe("could not prepare display: " + e); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + while (!Display.isCloseRequested()) { |
| 83 | + repaint(); |
| 84 | + Display.update(); |
| 85 | + Display.sync(FPS); |
| 86 | + } |
| 87 | + Display.destroy(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Initial display configuration. |
| 92 | + */ |
| 93 | + private void init() { |
| 94 | + GL11.glMatrixMode(GL11.GL_PROJECTION); |
| 95 | + GL11.glLoadIdentity(); |
| 96 | + GL11.glOrtho(0, WIDTH, HEIGHT, 0, 1, -1); |
| 97 | + GL11.glMatrixMode(GL11.GL_MODELVIEW); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Draw the OpenGL display. |
| 102 | + */ |
| 103 | + private void repaint() { |
| 104 | + double time = System.currentTimeMillis() / SECOND; |
| 105 | + |
| 106 | + /* Clear the screen and depth buffer */ |
| 107 | + GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); |
| 108 | + |
| 109 | + /* set the color of the quad (R,G,B,A) */ |
| 110 | + float red = (float) Math.abs(Math.sin(time * RED_RATE)); |
| 111 | + float green = (float) Math.abs(Math.cos(time * GREEN_RATE)); |
| 112 | + float blue = (float) Math.abs(Math.tan(time * BLUE_RATE)); |
| 113 | + GL11.glColor3f(red, green, blue); |
| 114 | + |
| 115 | + /* draw quad */ |
| 116 | + GL11.glPushMatrix(); |
| 117 | + float r = (float) (Math.sin(time * ROT_RATE) * ROT_RANGE + ROT_RANGE); |
| 118 | + GL11.glRotatef(r, 0f, 0f, 1f); |
| 119 | + |
| 120 | + GL11.glBegin(GL11.GL_QUADS); |
| 121 | + GL11.glVertex2f(WIDTH / WDIV, HEIGHT / HDIV); |
| 122 | + GL11.glVertex2f(WIDTH / WDIV + QUAD_SIZE, HEIGHT / HDIV); |
| 123 | + GL11.glVertex2f(WIDTH / WDIV + QUAD_SIZE, HEIGHT / HDIV + QUAD_SIZE); |
| 124 | + GL11.glVertex2f(WIDTH / WDIV, HEIGHT / HDIV + QUAD_SIZE); |
| 125 | + GL11.glEnd(); |
| 126 | + |
| 127 | + GL11.glPopMatrix(); |
| 128 | + } |
| 129 | +} |
0 commit comments