Advertisement
Guest User

Untitled

a guest
Oct 13th, 2011
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. public class Renderfield3D implements IDrawable{
  2.     private int _verticesID = 0;
  3.     private int _indicesID = 0;
  4.     private int _coloursID = 0;
  5.    
  6.     @Override
  7.     public void load() {
  8.         float[] vertices = new float[]
  9.         {  // X     Y     Z
  10.             0.0f, 0.0f, 0.0f, // 0 LBF
  11.             1.0f, 0.0f, 0.0f, // 1 RBF
  12.             0.0f, 0.0f, 1.0f, // 2 LBB
  13.             1.0f, 0.0f, 1.0f, // 3 RBB
  14.            
  15.             0.0f, 1.0f, 0.0f, // 4 LTF
  16.             1.0f, 1.0f, 0.0f, // 5 RTF
  17.             0.0f, 1.0f, 1.0f, // 6 LTB
  18.             1.0f, 1.0f, 1.0f  // 7 RTB  
  19.         };
  20.        
  21.         int[] indices = new int[]
  22.         {
  23.             4, 5, 1, 0, // FRONT
  24.             5, 7, 3, 1, // RIGHT
  25.             7, 6, 2, 3, // BACK
  26.             6, 4, 0, 2, // LEFT
  27.             0, 1, 3, 2, // BOTTOM
  28.             6, 7, 5, 4 // TOP
  29.         };
  30.        
  31.         float[] colours = new float[]
  32.         {
  33.             1.0f, 1.0f, 1.0f, 1.0f
  34.         };
  35.        
  36.         _verticesID = VBOHandler.createVBO();
  37.         _indicesID  = VBOHandler.createVBO();
  38.         _coloursID  = VBOHandler.createVBO();
  39.        
  40.         FloatBuffer bufferedColours = BufferUtils.createFloatBuffer(colours.length);
  41.                     bufferedColours.put(colours);
  42.        
  43.         FloatBuffer bufferedvertices = BufferUtils.createFloatBuffer(vertices.length);
  44.                     bufferedvertices.put(vertices);
  45.                    
  46.         IntBuffer   bufferedindices  = BufferUtils.createIntBuffer(indices.length);
  47.                     bufferedindices.put(indices);
  48.                    
  49.         VBOHandler.bufferData(_verticesID, bufferedvertices);
  50.         VBOHandler.bufferElementData(_indicesID, bufferedindices);
  51.     }
  52.  
  53.     @Override
  54.     public void draw() {
  55.       GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
  56.       ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, _verticesID);
  57.       GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
  58.      
  59.       GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
  60.       ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, _coloursID);
  61.       GL11.glColorPointer(4, GL11.GL_FLOAT, 0, 0);
  62.  
  63.       ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, _indicesID);
  64.       GL12.glDrawRangeElements(GL11.GL_QUADS, 0, 24, 24,
  65.                         GL11.GL_UNSIGNED_INT, 0);
  66.     }
  67.    
  68. }
  69.  
  70. public class VBOHandler {
  71.     public static IntBuffer createVBOs(int amount)
  72.     {
  73.         if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
  74.         {
  75.             IntBuffer buffer = BufferUtils.createIntBuffer(amount);
  76.             ARBVertexBufferObject.glGenBuffersARB(buffer);
  77.             return buffer;
  78.         }
  79.        
  80.         return null;
  81.     }
  82.    
  83.     public static int createVBO()
  84.     {
  85.         return createVBOs(1).get(0);
  86.     }
  87.    
  88.     public static void bufferData(int id, FloatBuffer buffer)
  89.     {
  90.         if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
  91.         {
  92.             ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id);
  93.             ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, buffer,
  94.                     ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
  95.         }
  96.     }
  97.    
  98.     public static void bufferElementData(int id, IntBuffer buffer)
  99.     {
  100.         if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
  101.         {
  102.            
  103.             ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, id);
  104.             ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, buffer,
  105.                     ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
  106.         }        
  107.     }
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement