Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GUIElement {
- public static Matrix4f OrthoMatrix;
- public Shader shader;
- float x1,y1,x2,y2;
- float[] verts;
- float[] texCoords;
- int[] indices;
- int orthoMatrix_loc;
- int modelMatrix_loc;
- int xOffset;
- int yOffset;
- Texture tex;
- VAO vao;
- public GUIElement(Texture tex, int x, int y) {
- this.tex=tex;
- float width;
- float height;
- width = tex.getImageWidth();
- height = tex.getImageHeight();
- x1 = 0;
- y1 = 0;
- x2 = width;
- y2 = height;
- xOffset = x;
- yOffset = y;
- verts = new float[] {
- x2,y1,0f,
- x2,y2,0f,
- x1,y2,0f,
- x1,y1,0f
- };
- indices = new int[] {
- 0, 1, 3,
- 3, 1, 2
- };
- texCoords = new float[] {
- 0, 1,
- 1, 1,
- 1, 0,
- 0, 0
- };
- vao = new VAO(verts, new float[]{}, texCoords, indices);
- shader = new Shader("guiShader") {
- @Override
- protected void bindAttributes() {
- bindAttribute(0, "position");
- bindAttribute(2, "texCoord");
- }
- @Override
- protected void getAllUniformLocations() {
- orthoMatrix_loc = getUniformLocation("orthoMatrix");
- modelMatrix_loc = getUniformLocation("modelMatrix");
- }
- };
- shader.start();
- shader.loadMatrix(orthoMatrix_loc, OrthoMatrix);
- shader.stop();
- System.out.println(String.format("%1f %2f %3f %4f", x1+xOffset,y1+yOffset,x2,y2));
- }
- public void draw() {
- glBindVertexArray(vao.getVaoID());
- GL20.glEnableVertexAttribArray(0);
- GL20.glEnableVertexAttribArray(2);
- shader.start();
- shader.loadMatrix(modelMatrix_loc, Util.createTransformationMatrix(new Vector3f(xOffset,yOffset,0),0,0,0,1));
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, tex.getTextureID());
- glDisable(GL_CULL_FACE);
- glDisable(GL_DEPTH_TEST);
- glDrawElements(GL_TRIANGLES, indices.length, GL_UNSIGNED_INT, 0);
- glActiveTexture(0);
- glBindTexture(GL_TEXTURE_2D, 0);
- glEnable(GL_CULL_FACE);
- glEnable(GL_DEPTH_TEST);
- shader.stop();
- GL20.glDisableVertexAttribArray(0);
- GL20.glDisableVertexAttribArray(2);
- glBindVertexArray(0);
- }
- public static void createOrtho() {
- OrthoMatrix = new Matrix4f();
- OrthoMatrix.setIdentity();
- float zNear = 1;
- float zFar = -1;
- float m00 = 2 / (Display.getWidth());
- float m11 = 2 / (Display.getHeight());
- float m22 = -2 / (zFar-zNear);
- float m23 = -(zFar+zNear)/(zFar-zNear);
- float m33 = 1;
- OrthoMatrix.m00 = m00;
- OrthoMatrix.m11 = m11;
- OrthoMatrix.m22 = m22;
- OrthoMatrix.m23 = m23;
- OrthoMatrix.m33 = m33;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement