Advertisement
Guest User

renderer

a guest
Jul 23rd, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.71 KB | None | 0 0
  1. public class NewMap {
  2.  
  3.     int vaoID;
  4.     int vertexBufferID;
  5.     int indexBufferID;
  6.     Image texture;
  7.  
  8.     Shader shader = ShaderManager.getShader("SimpleColor");
  9.  
  10.     int indexCount;
  11.  
  12.     public NewMap()
  13.     {
  14.         //--------------------------------------------------------
  15.         VertexData v0 = new VertexData();
  16.         v0.setXYZ(0, 0, 0); v0.setNXYZ(0, 0, -1); v0.setUV(0, 1);
  17.  
  18.         VertexData v1 = new VertexData();
  19.         v1.setXYZ(1, 0, 0); v1.setNXYZ(0, 0, -1); v1.setUV(1, 1);
  20.  
  21.         VertexData v2 = new VertexData();
  22.         v2.setXYZ(1, 1, 0); v2.setNXYZ(0, 0, -1); v2.setUV(1, 0);
  23.  
  24.         VertexData v3 = new VertexData();
  25.         v3.setXYZ(0, 1, 0); v3.setNXYZ(0, 0, -1); v3.setUV(0, 0);
  26.  
  27.         //--------------------------------------------------------
  28.         VertexData v4 = new VertexData();
  29.         v4.setXYZ(0, 0, 1); v4.setNXYZ(0, 0, 1); v4.setUV(0, 1);
  30.  
  31.         VertexData v5 = new VertexData();
  32.         v5.setXYZ(1, 0, 1); v5.setNXYZ(0, 0, 1); v5.setUV(1, 1);
  33.  
  34.         VertexData v6 = new VertexData();
  35.         v6.setXYZ(1, 1, 1); v6.setNXYZ(0, 0, 1); v6.setUV(1, 0);
  36.  
  37.         VertexData v7 = new VertexData();
  38.         v7.setXYZ(0, 1, 1); v7.setNXYZ(0, 0, 1); v7.setUV(0, 0);
  39.  
  40.         //--------------------------------------------------------
  41.         VertexData v8 = new VertexData();
  42.         v8.setXYZ(0, 0, 1); v8.setNXYZ(-1, 0, 0); v8.setUV(0, 1);
  43.  
  44.         VertexData v9 = new VertexData();
  45.         v9.setXYZ(0, 1, 1); v9.setNXYZ(-1, 0, 0); v9.setUV(1, 1);
  46.  
  47.         VertexData v10 = new VertexData();
  48.         v10.setXYZ(0, 1, 0); v10.setNXYZ(-1, 0, 0); v10.setUV(1, 0);
  49.  
  50.         VertexData v11 = new VertexData();
  51.         v11.setXYZ(0, 0, 0); v11.setNXYZ(-1, 0, 0); v11.setUV(0, 0);
  52.  
  53.         //--------------------------------------------------------
  54.         VertexData v12 = new VertexData();
  55.         v12.setXYZ(1, 0, 1); v12.setNXYZ(1, 0, 0); v12.setUV(0, 1);
  56.  
  57.         VertexData v13 = new VertexData();
  58.         v13.setXYZ(1, 1, 1); v13.setNXYZ(1, 0, 0); v13.setUV(1, 1);
  59.  
  60.         VertexData v14 = new VertexData();
  61.         v14.setXYZ(1, 1, 0); v14.setNXYZ(1, 0, 0); v14.setUV(1, 0);
  62.  
  63.         VertexData v15 = new VertexData();
  64.         v15.setXYZ(1, 0, 0); v15.setNXYZ(1, 0, 0); v15.setUV(0, 0);
  65.  
  66.         VertexData[] data = new VertexData[] {v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15};
  67.  
  68.         FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(data.length * VertexData.elementCount);
  69.  
  70.         for(int i = 0; i < data.length; i++)
  71.         {
  72.             vertBuffer.put(data[i].getElements());
  73.         }
  74.  
  75.         vertBuffer.flip();
  76.  
  77.         byte[] indices = new byte[]
  78.                 {
  79.                     0, 1, 2,
  80.                     2, 3, 0,
  81.  
  82.                     4, 5, 6,
  83.                     6, 7, 4,
  84.  
  85.                     8, 9, 10,
  86.                     10, 11, 8,
  87.  
  88.                     12, 13, 14,
  89.                     14, 15, 12
  90.                 };
  91.  
  92.         indexCount = indices.length;
  93.         ByteBuffer indexBuffer = BufferUtils.createByteBuffer(indices.length);
  94.         indexBuffer.put(indices);
  95.         indexBuffer.flip();
  96.  
  97.         vaoID = GL30.glGenVertexArrays();
  98.  
  99.         GL30.glBindVertexArray(vaoID);
  100.  
  101.         vertexBufferID = GL15.glGenBuffers();
  102.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferID);
  103.         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
  104.  
  105.         GL20.glVertexAttribPointer(0, VertexData.positionElementCount, GL11.GL_FLOAT, false, VertexData.stride, VertexData.positionOffset);
  106.         GL20.glVertexAttribPointer(1, VertexData.textureElementCount, GL11.GL_FLOAT, false, VertexData.stride, VertexData.textureOffset);
  107.         GL20.glVertexAttribPointer(2, VertexData.normalElementCount, GL11.GL_FLOAT, false, VertexData.stride, VertexData.normalOffset);
  108.  
  109.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  110.  
  111.         GL30.glBindVertexArray(0);
  112.  
  113.         indexBufferID = GL15.glGenBuffers();
  114.         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
  115.         GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL15.GL_STATIC_DRAW);
  116.         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
  117.  
  118.         GL20.glUseProgram(shader.getID());
  119.  
  120.         GL20.glBindAttribLocation(shader.getID(), 0, "inPosition");
  121.         GL20.glBindAttribLocation(shader.getID(), 1, "inTexCoord");
  122.         GL20.glBindAttribLocation(shader.getID(), 2, "inNormal");
  123.  
  124.         GL20.glUseProgram(0);
  125.     }
  126.  
  127.     public void render(Camera cam)
  128.     {
  129.         GL20.glUseProgram(shader.getID());
  130.  
  131.         shader.setUniformMatrix("projMat", cam.getProjection());
  132.         shader.setUniformMatrix("viewMat", cam.getView());
  133.         shader.setUniformMatrix("modelMat", new Matrix4f());
  134.         shader.setUniformMatrix("normalMat", new Matrix3f());
  135.         shader.setUniformFloat("shininess", 100);
  136.         shader.setUniformVec("diffuse", new Vector4f(0, 0.5f, 0, 1f));
  137.         shader.setUniformVec("ambient", new Vector4f(0, 0, 1,1));
  138.         shader.setUniformVec("specular", new Vector4f(0, 0, 0, 1f));
  139.         shader.setUniformVec("lightPos", cam.getPosition());
  140.  
  141.         GL30.glBindVertexArray(vaoID);
  142.  
  143.         GL20.glEnableVertexAttribArray(0);
  144.         GL20.glEnableVertexAttribArray(1);
  145.         GL20.glEnableVertexAttribArray(2);
  146.  
  147.         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
  148.  
  149.         GL11.glDrawElements(GL11.GL_TRIANGLES, indexCount, GL11.GL_UNSIGNED_BYTE, 0);
  150.  
  151.         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
  152.  
  153.         GL20.glDisableVertexAttribArray(0);
  154.         GL20.glDisableVertexAttribArray(1);
  155.         GL20.glDisableVertexAttribArray(2);
  156.  
  157.         GL30.glBindVertexArray(0);
  158.  
  159.         GL20.glUseProgram(0);
  160.     }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement