Advertisement
Guest User

Main.java

a guest
May 30th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package net.test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.nio.ByteBuffer;
  9. import java.nio.FloatBuffer;
  10.  
  11. import org.lwjgl.BufferUtils;
  12. import org.lwjgl.LWJGLException;
  13. import org.lwjgl.LWJGLUtil;
  14. import org.lwjgl.input.Keyboard;
  15. import org.lwjgl.opengl.ContextAttribs;
  16. import org.lwjgl.opengl.Display;
  17. import org.lwjgl.opengl.DisplayMode;
  18. import org.lwjgl.opengl.GL11;
  19. import org.lwjgl.opengl.GL15;
  20. import org.lwjgl.opengl.GL20;
  21. import org.lwjgl.opengl.GL30;
  22. import org.lwjgl.opengl.PixelFormat;
  23. import org.lwjgl.util.glu.GLU;
  24.  
  25. public class Main {
  26.     // Entry point for the application
  27.     public static void main(String[] args) {
  28.         System.setProperty("org.lwjgl.librarypath",
  29.                 new File(new File(System.getProperty("user.dir"), "native"),
  30.                         LWJGLUtil.getPlatformName()).getAbsolutePath());
  31.         new Main();
  32.     }
  33.  
  34.     private final String WINDOW_TITLE = "CubeTest";
  35.     private final int WIDTH = 640;
  36.     private final int HEIGHT = 360;
  37.  
  38.     private Container cont;
  39.  
  40.     public Main() {
  41.         this.setupOpenGL();
  42.  
  43.         cont = new Container(
  44.                 new Vertex[] { new Vertex(0.0f, 0.5f, 0.0f),
  45.                         new Vertex(-0.5f, -0.5f, 0.0f),
  46.                         new Vertex(0.5f, -0.5f, 0.0f) }, new byte[] { 0, 1, 2 });
  47.  
  48.         while (!Display.isCloseRequested()) {
  49.             this.loopCycle();
  50.  
  51.             Display.sync(60);
  52.             Display.update();
  53.         }
  54.  
  55.         cont.destroyResources();
  56.        
  57.         Display.destroy();
  58.     }
  59.  
  60.     public void setupOpenGL() {
  61.         try {
  62.             PixelFormat pixelFormat = new PixelFormat();
  63.             ContextAttribs contextAtrributes = new ContextAttribs(3, 0)
  64.                     .withForwardCompatible(true);
  65.  
  66.             Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
  67.             Display.setTitle(WINDOW_TITLE);
  68.             Display.create(pixelFormat, contextAtrributes);
  69.  
  70.             GL11.glViewport(0, 0, WIDTH, HEIGHT);
  71.         } catch (LWJGLException e) {
  72.             e.printStackTrace();
  73.             System.exit(-1);
  74.         }
  75.  
  76.         GL11.glClearColor(0.4f, 0.6f, 0.9f, 0f);
  77.  
  78.         GL11.glViewport(0, 0, WIDTH, HEIGHT);
  79.     }
  80.  
  81.     public void loopCycle() {
  82.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
  83.  
  84.         while (Keyboard.next()) {
  85.             if (Keyboard.getEventKeyState()) {
  86.                 if (Keyboard.getEventKey() == Keyboard.KEY_A) {
  87.                     cont.destroyResources();
  88.                     cont = new Container(new Vertex[] {
  89.                             new Vertex(0.0f, 0.5f, 0.0f),
  90.                             new Vertex(-0.5f, -0.5f, 0.0f),
  91.                             new Vertex(0.5f, -0.5f, 0.0f) }, new byte[] { 0, 1,
  92.                             2 });
  93.                 } else if (Keyboard.getEventKey() == Keyboard.KEY_S) {
  94.                     cont.destroyResources();
  95.                     cont = new Container(
  96.                             new Vertex[] {
  97.                                     new Vertex(0.0f, 0.5f, 0.0f).setRGB(1,
  98.                                             0.5f, 0.2f),
  99.                                     new Vertex(-0.5f, -0.5f, 0.0f).setRGB(1,
  100.                                             0.5f, 0.2f),
  101.                                     new Vertex(0.5f, -0.5f, 0.0f).setRGB(1,
  102.                                             0.5f, 0.2f) },
  103.                             new byte[] { 0, 1, 2 });
  104.                 } else if (Keyboard.getEventKey() == Keyboard.KEY_D) {
  105.                     cont.setVertices(new Vertex[] {
  106.                             new Vertex(0.0f, 0.5f, 0.0f).setRGB(1, 0.5f, 0.2f),
  107.                             new Vertex(-0.5f, -0.5f, 0.0f)
  108.                                     .setRGB(0.5f, 1, 0.2f),
  109.                             new Vertex(0.5f, -0.5f, 0.0f).setRGB(0.2f, 0.5f, 1) });
  110.                     cont.setIndices(new byte[] { 0, 1, 2 });
  111.                     cont.updateResources();
  112.                 } else if (Keyboard.getEventKey() == Keyboard.KEY_C) {
  113.                     System.gc();
  114.                 }
  115.             }
  116.         }
  117.  
  118.         cont.render();
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement