Advertisement
Guest User

Java Texturing and Indices // OpenGL

a guest
Feb 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. //RawModel.java
  2. public class RawModel {
  3.  
  4.     private int vaoID;
  5.     private int vertexCount;
  6.  
  7.     public void RawMOdel(int vaoID, int vertexCount){
  8.         this.vaoID = vaoID;
  9.         this.vertexCount = vertexCount;
  10.     }
  11.    
  12.     public in getVaoID(){
  13.         return vaoID;
  14.     }
  15.     public int getVertexCount(){
  16.         return vertexCount;
  17.     }
  18. }
  19.  
  20. //Loader.java
  21. public class Loader {
  22.  
  23.     public RawModel loadToVAO(float[] positions){
  24.         int vaoID= createVAO();
  25.         storeDataInAttributeList(0, positions);
  26.         unbindVAO();
  27.         return new RawMOdel(vaoID, positions.length / 3);
  28.     }
  29.     private int createVAO(){
  30.         int vaoID = GL30.glGenVertexArrays();
  31.         GL30.glBindVertexArray(vaoID);
  32.         return vaoID;
  33.     }
  34.    
  35.     private void storeDataInAttributeList(int attributeNumber, float[] data){
  36.         int vboID = GL15.glGenBuffers();
  37.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
  38.     }
  39.    
  40.     private void unbindVAO(){
  41.         GL30.glBindVertexArray(0);
  42.     }
  43. }
  44.  
  45. //Index Buffers Rendering
  46.     //In Loader.java
  47.     private void bindIndicesBuffer(int[] indices){
  48.         int vboID = GL15.glGenBuffers();
  49.         vbos.add(vboID);
  50.         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID();
  51.         IntBuffer buffer = storeDataInIntBuffer(indices);
  52.         GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
  53.     }
  54.  
  55.     //Add:in public RawModel(constructor)
  56.     float[] posistions, int[] indices
  57.     bindIndicesBuffer(indices);
  58.     //Change in renderer:
  59.     GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
  60.     private IntBuffer storeDataInIntBuffer(int[] data){
  61.         IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
  62.         buffer.put(data);
  63.         buffer.flip();
  64.         return buffer;
  65.     }
  66.    
  67.     //In MaingameLoop.java
  68.     float[] vertices = {
  69.     -0.5f, 0.5f, 0,
  70.     -0.5f, -0.5f, 0,
  71.     0.5f, -0.5f, 0,
  72.     0.5f, 0.5f, 0f
  73.     };
  74.  
  75.     int[] indices = {
  76.     0, 1, 3,
  77.     3, 1, 2
  78.     };
  79.  
  80. //Texturing
  81.     //Add:
  82.     private List<Integer> texture = new ArrayList<Integer>();
  83.  
  84.     public int loadTexture(String fileName){
  85.         Texture texture = null;
  86.         try{
  87.         texture = TextureLoader.getTexture("PNG", new FileInputStream("res/"+fileName+".png"));
  88.         } catch (FileNotFoundException e){
  89.             e.printStackTrace();
  90.         } catch (IOException e) {
  91.             e.printStackTrace();
  92.         }
  93.         int textureID = texture.getTextureID();
  94.         return textureID;
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement