Advertisement
Guest User

Main

a guest
Sep 21st, 2014
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. import java.nio.DoubleBuffer;
  2. import java.nio.IntBuffer;
  3. import ...
  4.  
  5. public class Main {
  6.  
  7.     static List<double[]> vertices;
  8.     static List<int[]> faces;
  9.  
  10.     public static void main(String[] args) {
  11.  
  12.         try {
  13.             create();
  14.         } catch (Exception ex) {
  15.         }
  16.     }
  17.  
  18.     private static void create() throws Exception {
  19.  
  20.         PixelFormat pixelFormat = new PixelFormat();
  21.         ContextAttribs contextAtrributes = new ContextAttribs(3, 3).withProfileCore(true);
  22.  
  23.         Display.setDisplayMode(new DisplayMode(640, 480));
  24.         Display.create(pixelFormat, contextAtrributes);
  25.        
  26.         GL11.glDisable(GL11.GL_CULL_FACE);
  27.        
  28.         vertices = new ArrayList<double[]>();
  29.         faces = new ArrayList<int[]>();
  30.        
  31.         vertices.add(new double[] { -1, -1, 0 });
  32.         vertices.add(new double[] { 0, 1, 0 });
  33.         vertices.add(new double[] { 1, -1, 0 });
  34.         vertices.add(new double[] { 0, -1, 1 });
  35.         faces.add(new int[] { 0, 1, 3 });
  36.         faces.add(new int[] { 3, 1, 2 });
  37.         faces.add(new int[] { 2, 1, 0 });
  38.         faces.add(new int[] { 0, 2, 3 });
  39.  
  40.         vbo = GL15.glGenBuffers();
  41.         ibo = GL15.glGenBuffers();
  42.        
  43.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
  44.         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, createVertexBuffer(vertices), GL15.GL_STATIC_DRAW);
  45.        
  46.         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
  47.         GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, createFaceBuffer(faces), GL15.GL_STATIC_DRAW);
  48.  
  49.         Shader s = new Shader(new ResourceLocation("/shader.vs"), new ResourceLocation("/shader.fs"));
  50.         s.load();
  51.         s.compile();
  52.         s.bind();
  53.        
  54.         while (!Display.isCloseRequested()) {
  55.             render();
  56.            
  57.             Display.update();
  58.             Display.sync(60);
  59.         }
  60.         Display.destroy();
  61.     }
  62.    
  63.     private static int vbo = -1, ibo = -1;
  64.    
  65.     private static void render() {
  66.    
  67.         for (int i = 0; i < 1; i++)
  68.             GL20.glEnableVertexAttribArray(i);
  69.        
  70.         // Set pointer at the vertices' position
  71.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
  72.         GL20.glVertexAttribPointer(0, 3, GL11.GL_DOUBLE, false, Vertex.BYTES, 0);
  73.        
  74.         // Draw faces
  75.         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
  76.         GL11.glDrawElements(GL11.GL_TRIANGLES, vertices.size(), GL11.GL_UNSIGNED_INT, 0);
  77.        
  78.         // Disable attributes
  79.         for (int i = 0; i < 1; i++)
  80.             GL20.glDisableVertexAttribArray(i);
  81.     }
  82.    
  83.     public static DoubleBuffer createVertexBuffer(List<double[]> vertices) {
  84.    
  85.         DoubleBuffer buffer = BufferUtils.createDoubleBuffer(vertices.size() * 3);
  86.         for (double[] d : vertices)
  87.             buffer.put(d);
  88.         buffer.flip();
  89.        
  90.         return buffer;
  91.     }
  92.    
  93.     public static IntBuffer createFaceBuffer(List<int[]> faces) {
  94.    
  95.         IntBuffer buffer = BufferUtils.createIntBuffer(faces.size() * 3);
  96.         for (int[] f : faces)
  97.             buffer.put(f);
  98.         buffer.flip();
  99.        
  100.         return buffer;
  101.     }
  102.    
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement