Advertisement
Guest User

GUIElement.class

a guest
Dec 22nd, 2014
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. public class GUIElement {
  2.     public static Matrix4f OrthoMatrix;
  3.     public Shader shader;
  4.     float x1,y1,x2,y2;
  5.     float[] verts;
  6.     float[] texCoords;
  7.     int[] indices;
  8.    
  9.     int orthoMatrix_loc;
  10.     int modelMatrix_loc;
  11.    
  12.     int xOffset;
  13.     int yOffset;
  14.    
  15.     Texture tex;
  16.    
  17.     VAO vao;
  18.    
  19.     public GUIElement(Texture tex, int x, int y) {
  20.         this.tex=tex;
  21.        
  22.         float width;
  23.         float height;
  24.        
  25.         width = tex.getImageWidth();
  26.         height = tex.getImageHeight();
  27.        
  28.         x1 = 0;
  29.         y1 = 0;
  30.         x2 = width;
  31.         y2 = height;
  32.        
  33.         xOffset = x;
  34.         yOffset = y;
  35.        
  36.         verts = new float[] {
  37.             x2,y1,0f,
  38.             x2,y2,0f,
  39.             x1,y2,0f,
  40.             x1,y1,0f
  41.         };
  42.        
  43.         indices = new int[] {
  44.             0, 1, 3,
  45.             3, 1, 2
  46.         };
  47.        
  48.         texCoords = new float[] {
  49.             0, 1,
  50.             1, 1,
  51.             1, 0,
  52.             0, 0
  53.         };
  54.        
  55.         vao = new VAO(verts, new float[]{}, texCoords, indices);
  56.        
  57.         shader = new Shader("guiShader") {
  58.             @Override
  59.             protected void bindAttributes() {
  60.                 bindAttribute(0, "position");
  61.                 bindAttribute(2, "texCoord");
  62.             }
  63.             @Override
  64.             protected void getAllUniformLocations() {
  65.                 orthoMatrix_loc = getUniformLocation("orthoMatrix");
  66.                 modelMatrix_loc = getUniformLocation("modelMatrix");
  67.             }
  68.         };
  69.        
  70.         shader.start();
  71.             shader.loadMatrix(orthoMatrix_loc, OrthoMatrix);
  72.         shader.stop();
  73.        
  74.         System.out.println(String.format("%1f %2f %3f %4f", x1+xOffset,y1+yOffset,x2,y2));
  75.     }
  76.    
  77.     public void draw() {
  78.         glBindVertexArray(vao.getVaoID());
  79.         GL20.glEnableVertexAttribArray(0);
  80.         GL20.glEnableVertexAttribArray(2);
  81.         shader.start();
  82.         shader.loadMatrix(modelMatrix_loc, Util.createTransformationMatrix(new Vector3f(xOffset,yOffset,0),0,0,0,1));
  83.         glActiveTexture(GL_TEXTURE0);
  84.         glBindTexture(GL_TEXTURE_2D, tex.getTextureID());
  85.         glDisable(GL_CULL_FACE);
  86.         glDisable(GL_DEPTH_TEST);
  87.         glDrawElements(GL_TRIANGLES, indices.length, GL_UNSIGNED_INT, 0);
  88.         glActiveTexture(0);
  89.         glBindTexture(GL_TEXTURE_2D, 0);
  90.         glEnable(GL_CULL_FACE);
  91.         glEnable(GL_DEPTH_TEST);
  92.         shader.stop();
  93.         GL20.glDisableVertexAttribArray(0);
  94.         GL20.glDisableVertexAttribArray(2);
  95.         glBindVertexArray(0);
  96.     }
  97.    
  98.     public static void createOrtho() {
  99.         OrthoMatrix = new Matrix4f();
  100.         OrthoMatrix.setIdentity();
  101.        
  102.         float zNear = 1;
  103.         float zFar = -1;
  104.         float m00 = 2 / (Display.getWidth());
  105.         float m11 = 2 / (Display.getHeight());
  106.         float m22 = -2 / (zFar-zNear);
  107.         float m23 = -(zFar+zNear)/(zFar-zNear);
  108.         float m33 = 1;
  109.        
  110.         OrthoMatrix.m00 = m00;
  111.         OrthoMatrix.m11 = m11;
  112.         OrthoMatrix.m22 = m22;
  113.         OrthoMatrix.m23 = m23;
  114.         OrthoMatrix.m33 = m33;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement