Advertisement
Guest User

Window

a guest
May 9th, 2015
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. package net.lesson.graphics;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.nio.FloatBuffer;
  7. import java.awt.*;
  8. import java.nio.IntBuffer;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import net.lesson.graphics.models.RawModel;
  13.  
  14. import org.lwjgl.BufferUtils;
  15. import org.lwjgl.opengl.GL11;
  16. import org.lwjgl.opengl.GL15;
  17. import org.lwjgl.opengl.GL20;
  18. import org.lwjgl.opengl.GL30;
  19. import org.newdawn.slick.opengl.Texture;
  20. import org.newdawn.slick.opengl.TextureLoader;
  21.  
  22. public class Loader {
  23.    
  24.     private List<Integer> vaos = new ArrayList<Integer>();
  25.     private List<Integer> vbos = new ArrayList<Integer>();
  26.     private List<Integer> textures = new ArrayList<Integer>();
  27.    
  28.     public RawModel loadToVAO(float[] positions,float[] textureCoords, int[] indices){
  29.         int vaoID = createVao();
  30.         bindIndicesBuffer(indices);
  31.         storeDataInAttributeList(0,3, positions);
  32.         storeDataInAttributeList(1,2, positions);
  33.         unbindVAO();
  34.         return new RawModel(vaoID, indices.length);
  35.        
  36.     }
  37.    
  38.     public int loadTexture(String fileName){
  39.         Texture texture = null;
  40.         try{
  41.         texture = TextureLoader.getTexture("PNG", new FileInputStream("res/"+fileName+".png"));
  42.         }catch(FileNotFoundException e){
  43.             e.printStackTrace();
  44.         }catch (IOException e){
  45.             e.printStackTrace();
  46.         }
  47.         int textureID = texture.getTextureID();
  48.         textures.add(textureID);
  49.         return textureID;
  50.     }
  51.    
  52.     public void cleanUp(){
  53.         for(int vao:vaos){
  54.             GL30.glDeleteVertexArrays(vao);
  55.            
  56.         }
  57.         for(int vbo:vbos){
  58.             GL15.glDeleteBuffers(vbo);
  59.         }
  60.         for(int texture:textures){
  61.             GL11.glDeleteTextures(texture);
  62.         }
  63.     }
  64.    
  65.     private int createVao(){
  66.       int vaoID = GL30.glGenVertexArrays();
  67.       vaos.add(vaoID);
  68.       GL30.glBindVertexArray(vaoID);
  69.       return vaoID;
  70.     }
  71.    
  72.     private void storeDataInAttributeList(int attributeNumber,int coridinateSize, float[] data){
  73.         int vboID = GL15.glGenBuffers();
  74.         vbos.add(vboID);
  75.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
  76.         FloatBuffer buffer = storeDataInFloatBuffer(data);
  77.         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
  78.         GL20.glVertexAttribPointer(attributeNumber, coridinateSize, GL11.GL_FLOAT, false, 0,0);
  79.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  80.     }
  81.  
  82.    public void unbindVAO(){
  83.        GL30.glBindVertexArray(0);
  84.        
  85.    }
  86.    
  87.       private void bindIndicesBuffer(int[] indices){
  88.        int vboID = GL15.glGenBuffers();
  89.        vbos.add(vboID);
  90.        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID);
  91.        IntBuffer buffer = storeDataInIntBuffer(indices);
  92.        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
  93.       }
  94.      
  95.       private IntBuffer storeDataInIntBuffer(int[] data){
  96.            IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
  97.            buffer.put(data);
  98.            buffer.flip();
  99.            return buffer;
  100.      
  101.    }
  102.    
  103.    private FloatBuffer storeDataInFloatBuffer(float[] data){
  104.        FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
  105.        buffer.put(data);
  106.        buffer.flip();
  107.        return buffer;
  108.    }
  109.    
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement