This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jul 23rd, 2013  |  syntax: None  |  size: 3.57 KB  |  views: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class NewMap {
  2.  
  3.     int vaoID;
  4.     int vertexBufferID;
  5.     int indexBufferID;
  6.  
  7.     Shader shader = ShaderManager.getShader("SimpleColor");
  8.  
  9.     int indexCount;
  10.  
  11.     public NewMap()
  12.     {
  13.         VertexData v0 = new VertexData();
  14.         v0.setXYZ(-1, -1, 0); v0.setNXYZ(0, 0, -1); v0.setUV(0, 0);
  15.  
  16.         VertexData v1 = new VertexData();
  17.         v1.setXYZ(0, 1, 0); v1.setNXYZ(0, 0, -1); v1.setUV(1, 1);
  18.  
  19.         VertexData v2 = new VertexData();
  20.         v2.setXYZ(1, -1, 0); v2.setNXYZ(0, 0, -1); v2.setUV(1, 0);
  21.  
  22.         VertexData[] data = new VertexData[] {v0, v1, v2};
  23.  
  24.         FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(data.length * VertexData.elementCount);
  25.  
  26.         for(int i = 0; i < data.length; i++)
  27.         {
  28.             vertBuffer.put(data[i].getElements());
  29.         }
  30.  
  31.         vertBuffer.flip();
  32.  
  33.         byte[] indices = new byte[]
  34.                 {
  35.                     0, 1, 2
  36.                 };
  37.  
  38.         indexCount = indices.length;
  39.         ByteBuffer indexBuffer = BufferUtils.createByteBuffer(indices.length);
  40.         indexBuffer.put(indices);
  41.         indexBuffer.flip();
  42.  
  43.         vaoID = GL30.glGenVertexArrays();
  44.  
  45.         GL30.glBindVertexArray(vaoID);
  46.  
  47.         vertexBufferID = GL15.glGenBuffers();
  48.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferID);
  49.         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
  50.  
  51.         GL20.glVertexAttribPointer(0, VertexData.positionElementCount, GL11.GL_FLOAT, false, VertexData.stride, VertexData.positionOffset);
  52.         GL20.glVertexAttribPointer(1, VertexData.textureElementCount, GL11.GL_FLOAT, false, VertexData.stride, VertexData.textureOffset);
  53.         GL20.glVertexAttribPointer(2, VertexData.normalElementCount, GL11.GL_FLOAT, false, VertexData.stride, VertexData.normalOffset);
  54.  
  55.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  56.  
  57.         GL30.glBindVertexArray(0);
  58.  
  59.         indexBufferID = GL15.glGenBuffers();
  60.         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
  61.         GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL15.GL_STATIC_DRAW);
  62.         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
  63.  
  64.         GL20.glUseProgram(shader.getID());
  65.  
  66.         GL20.glBindAttribLocation(shader.getID(), 0, "inPosition");
  67.         GL20.glBindAttribLocation(shader.getID(), 1, "inTexCoord");
  68.         GL20.glBindAttribLocation(shader.getID(), 2, "inNormal");
  69.  
  70.         GL20.glUseProgram(0);
  71.     }
  72.  
  73.     public void render(Camera cam)
  74.     {
  75.         GL20.glUseProgram(shader.getID());
  76.  
  77.         shader.setUniformMatrix("projMat", cam.getProjection());
  78.         shader.setUniformMatrix("viewMat", cam.getView());
  79.         shader.setUniformMatrix("modelMat", new Matrix4f());
  80.         shader.setUniformVec("lightDirection", new Vector3f(0, 0, 1));
  81.         shader.setUniformVec("diffuseColor", new Vector4f(1, 1, 1, 1));
  82.         shader.setUniformVec("ambientColor", new Vector4f(0.15f, 0.15f, 0.15f, 1f));
  83.  
  84.         GL30.glBindVertexArray(vaoID);
  85.  
  86.         GL20.glEnableVertexAttribArray(0);
  87.         GL20.glEnableVertexAttribArray(1);
  88.         GL20.glEnableVertexAttribArray(2);
  89.  
  90.         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
  91.  
  92.         GL11.glDrawElements(GL11.GL_TRIANGLES, indexCount, GL11.GL_UNSIGNED_BYTE, 0);
  93.  
  94.         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
  95.  
  96.         GL20.glDisableVertexAttribArray(0);
  97.         GL20.glDisableVertexAttribArray(1);
  98.         GL20.glDisableVertexAttribArray(2);
  99.  
  100.         GL30.glBindVertexArray(0);
  101.  
  102.         GL20.glUseProgram(0);
  103.     }
  104.  
  105. }
clone this paste RAW Paste Data