Guest User

Untitled

a guest
Jan 6th, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. package net.pyraetos;
  2.  
  3. import java.nio.FloatBuffer;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.lwjgl.opengl.ContextAttribs;
  8. import org.lwjgl.opengl.Display;
  9. import org.lwjgl.opengl.DisplayMode;
  10. import org.lwjgl.opengl.PixelFormat;
  11.  
  12. import static org.lwjgl.opengl.GL11.*;
  13. import static org.lwjgl.opengl.GL15.*;
  14. import static org.lwjgl.opengl.GL20.*;
  15. import static org.lwjgl.opengl.GL30.*;
  16.  
  17. public class Graphics {
  18.    
  19.     public static int vertexArrayObjectID;
  20.     public static List<Integer> vertexBufferObjectIDs = new ArrayList<Integer>();
  21.    
  22.     private static void initialize(){
  23.         try{
  24.             Display.setDisplayMode(new DisplayMode(800, 800));
  25.             Display.setResizable(true);
  26.             Display.create(new PixelFormat(), new ContextAttribs(3, 0));
  27.         }catch(Exception e){
  28.             e.printStackTrace();
  29.             System.exit(0);
  30.         }
  31.         glViewport(0, 0, Display.getWidth(), Display.getHeight());
  32.         //Creates our VAO, the "array-to-contain-arrays"
  33.         vertexArrayObjectID = glGenVertexArrays();
  34.     }
  35.    
  36.     private static void mainLoop(){
  37.         //Constructs two Quads with different offsets from origin
  38.         Quad quad0 = Quad.originSquare(.5f, 0f, 0f);
  39.         Quad quad1 = Quad.originSquare(-.5f, 0f, 0f);
  40.         //Quad extends Shape, so invokes render(Shape)
  41.         render(quad0);
  42.         render(quad1);
  43.         while(!Display.isCloseRequested()){
  44.             if(Display.wasResized()){
  45.                 glViewport(0, 0, Display.getWidth(), Display.getHeight());
  46.             }
  47.             render();
  48.             Display.sync(60);
  49.         }
  50.         Display.destroy();
  51.     }
  52.    
  53.     private static void render(){
  54.         glClear(GL_COLOR_BUFFER_BIT);
  55.         //Selects the VAO
  56.         glBindVertexArray(vertexArrayObjectID);
  57.         //Loop through each VBO in the VAO and draw them one by one
  58.         for(int i = 0; i < vertexBufferObjectIDs.size(); i++){
  59.             glEnableVertexAttribArray(i);
  60.             glDrawArrays(GL_TRIANGLES, 0, 6);
  61.             glDisableVertexAttribArray(i);
  62.         }
  63.         Display.update();
  64.         //Deselects the VAO for now
  65.         glBindVertexArray(vertexArrayObjectID);
  66.     }
  67.    
  68.     public static void render(Shape shape){
  69.         //Safely converts homemade Shape object to GL-compatible FloatBuffer
  70.         FloatBuffer buffer = shape.toFloatBuffer();
  71.         //Selects previously created VAO
  72.         glBindVertexArray(vertexArrayObjectID);
  73.         //Creates a new VBO for our Shape to go in the VAO
  74.         int id = glGenBuffers();
  75.         vertexBufferObjectIDs.add(id);
  76.         //Selects the new VBO
  77.         glBindBuffer(GL_ARRAY_BUFFER, id);
  78.         //Loads up our Shape into the selected VBO
  79.         glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
  80.         /*
  81.          * Puts the VBO into the VAO using the amount of VBOs
  82.          * we have created minus one as the index
  83.          */
  84.         glVertexAttribPointer(vertexBufferObjectIDs.size() - 1, 3, GL_FLOAT, false, 0, 0);
  85.         //Deselects the VBO for now
  86.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  87.         //Deselects the VAO for now
  88.         glBindVertexArray(0);
  89.     }
  90.    
  91.     public static void main(String[] args){
  92.         initialize();
  93.         mainLoop();
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment