Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.pyraetos;
- import java.nio.FloatBuffer;
- import java.util.ArrayList;
- import java.util.List;
- import org.lwjgl.opengl.ContextAttribs;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.opengl.PixelFormat;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.opengl.GL15.*;
- import static org.lwjgl.opengl.GL20.*;
- import static org.lwjgl.opengl.GL30.*;
- public class Graphics {
- public static int vertexArrayObjectID;
- public static List<Integer> vertexBufferObjectIDs = new ArrayList<Integer>();
- private static void initialize(){
- try{
- Display.setDisplayMode(new DisplayMode(800, 800));
- Display.setResizable(true);
- Display.create(new PixelFormat(), new ContextAttribs(3, 0));
- }catch(Exception e){
- e.printStackTrace();
- System.exit(0);
- }
- glViewport(0, 0, Display.getWidth(), Display.getHeight());
- //Creates our VAO, the "array-to-contain-arrays"
- vertexArrayObjectID = glGenVertexArrays();
- }
- private static void mainLoop(){
- //Constructs two Quads with different offsets from origin
- Quad quad0 = Quad.originSquare(.5f, 0f, 0f);
- Quad quad1 = Quad.originSquare(-.5f, 0f, 0f);
- //Quad extends Shape, so invokes render(Shape)
- render(quad0);
- render(quad1);
- while(!Display.isCloseRequested()){
- if(Display.wasResized()){
- glViewport(0, 0, Display.getWidth(), Display.getHeight());
- }
- render();
- Display.sync(60);
- }
- Display.destroy();
- }
- private static void render(){
- glClear(GL_COLOR_BUFFER_BIT);
- //Selects the VAO
- glBindVertexArray(vertexArrayObjectID);
- //Loop through each VBO in the VAO and draw them one by one
- for(int i = 0; i < vertexBufferObjectIDs.size(); i++){
- glEnableVertexAttribArray(i);
- glDrawArrays(GL_TRIANGLES, 0, 6);
- glDisableVertexAttribArray(i);
- }
- Display.update();
- //Deselects the VAO for now
- glBindVertexArray(vertexArrayObjectID);
- }
- public static void render(Shape shape){
- //Safely converts homemade Shape object to GL-compatible FloatBuffer
- FloatBuffer buffer = shape.toFloatBuffer();
- //Selects previously created VAO
- glBindVertexArray(vertexArrayObjectID);
- //Creates a new VBO for our Shape to go in the VAO
- int id = glGenBuffers();
- vertexBufferObjectIDs.add(id);
- //Selects the new VBO
- glBindBuffer(GL_ARRAY_BUFFER, id);
- //Loads up our Shape into the selected VBO
- glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
- /*
- * Puts the VBO into the VAO using the amount of VBOs
- * we have created minus one as the index
- */
- glVertexAttribPointer(vertexBufferObjectIDs.size() - 1, 3, GL_FLOAT, false, 0, 0);
- //Deselects the VBO for now
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- //Deselects the VAO for now
- glBindVertexArray(0);
- }
- public static void main(String[] args){
- initialize();
- mainLoop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment