Advertisement
Guest User

texture broken

a guest
Sep 13th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.50 KB | None | 0 0
  1. package eu.robinkuester.opengltest;
  2.  
  3. import org.apache.commons.io.IOUtils;
  4. import org.lwjgl.BufferUtils;
  5. import org.lwjgl.glfw.Callbacks;
  6. import org.lwjgl.glfw.GLFWErrorCallback;
  7. import org.lwjgl.glfw.GLFWKeyCallback;
  8. import org.lwjgl.opengl.GL;
  9. import org.lwjgl.opengl.GLUtil;
  10. import org.lwjgl.stb.STBImage;
  11. import org.lwjgl.system.libffi.Closure;
  12.  
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.nio.ByteBuffer;
  17. import java.nio.FloatBuffer;
  18. import java.nio.IntBuffer;
  19. import java.nio.ShortBuffer;
  20.  
  21. import static org.lwjgl.glfw.GLFW.*;
  22. import static org.lwjgl.opengl.GL11.*;
  23. import static org.lwjgl.opengl.GL12.GL_CLAMP_TO_EDGE;
  24. import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
  25. import static org.lwjgl.opengl.GL13.glActiveTexture;
  26. import static org.lwjgl.opengl.GL15.*;
  27. import static org.lwjgl.opengl.GL20.*;
  28. import static org.lwjgl.opengl.GL30.*;
  29. import static org.lwjgl.system.MemoryUtil.NULL;
  30.  
  31. /**
  32.  * @author Robin Küster
  33.  * @since 2015-09-13
  34.  */
  35. public class OpenGLTest {
  36.     private GLFWKeyCallback keyCallback = new GLFWKeyCallback() {
  37.         @Override
  38.         public void invoke(long window, int key, int scancode, int action, int mods) {
  39.             if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
  40.                 glfwSetWindowShouldClose(window, GL_TRUE);
  41.             }
  42.         }
  43.     };
  44.  
  45.     private Closure glError;
  46.  
  47.     private GLFWErrorCallback errorCallback = Callbacks.errorCallbackPrint();
  48.  
  49.     private long windowID;
  50.  
  51.     private static int height, width;
  52.  
  53.     private int texUni;
  54.     private int tex;
  55.     private int uvAttrib;
  56.     private int posAttrib;
  57.  
  58.     public static void main(String[] args) {
  59.         String path = new File("natives").getAbsolutePath();
  60.         System.setProperty("org.lwjgl.librarypath", path);
  61.  
  62.         new OpenGLTest().run();
  63.     }
  64.  
  65.     private static FloatBuffer generateFloatBuffer(float[] array) {
  66.         FloatBuffer buffer = BufferUtils.createFloatBuffer(array.length);
  67.         buffer.put(array);
  68.         buffer.flip();
  69.         return buffer;
  70.     }
  71.  
  72.     private static ShortBuffer generateShortBuffer(short[] array) {
  73.         ShortBuffer buffer = BufferUtils.createShortBuffer(array.length);
  74.         buffer.put(array);
  75.         buffer.flip();
  76.         return buffer;
  77.     }
  78.  
  79.     private static ByteBuffer generateByteBuffer(byte[] array) {
  80.         ByteBuffer buffer = BufferUtils.createByteBuffer(array.length);
  81.         buffer.put(array);
  82.         buffer.flip();
  83.         return buffer;
  84.     }
  85.  
  86.     private static int compileShader(int type, String source) {
  87.         int shader = glCreateShader(type);
  88.         glShaderSource(shader, source);
  89.         glCompileShader(shader);
  90.  
  91.         int info = glGetShaderi(shader, GL_COMPILE_STATUS);
  92.         if (info == GL_FALSE) {
  93.             System.err.println(glGetShaderInfoLog(shader));
  94.             return 0;
  95.         }
  96.  
  97.         return shader;
  98.     }
  99.  
  100.     private static int linkProgram(int vertexShader, int fragmentShaer) {
  101.         int program = glCreateProgram();
  102.         glAttachShader(program, vertexShader);
  103.         glAttachShader(program, fragmentShaer);
  104.         glLinkProgram(program);
  105.  
  106.         int info = glGetProgrami(program, GL_LINK_STATUS);
  107.         if (info == GL_FALSE) {
  108.             System.err.println(glGetProgramInfoLog(program));
  109.             return 0;
  110.         }
  111.         return program;
  112.     }
  113.  
  114.     private static ByteBuffer getTexture(String texture) {
  115.         ByteBuffer imageFileBuffer;
  116.  
  117.         InputStream is = OpenGLTest.class.getResourceAsStream(texture);
  118.         try {
  119.             byte array[] = IOUtils.toByteArray(is);
  120.             imageFileBuffer = generateByteBuffer(array);
  121.             is.close();
  122.         } catch (IOException e) {
  123.             throw new RuntimeException(e);
  124.         }
  125.  
  126.         IntBuffer w = BufferUtils.createIntBuffer(1);
  127.         IntBuffer h = BufferUtils.createIntBuffer(1);
  128.         IntBuffer comp = BufferUtils.createIntBuffer(1);
  129.  
  130.         ByteBuffer buffer = STBImage.stbi_load_from_memory(imageFileBuffer, w, h, comp, 0);
  131.         if (buffer == null) {
  132.             throw new RuntimeException("Failed to load image: " + STBImage.stbi_failure_reason());
  133.         }
  134.  
  135.         if (comp.get(0) != 4) {
  136.             throw new RuntimeException("No alpha channel!");
  137.         }
  138.  
  139.         width = w.get(0);
  140.         height = h.get(0);
  141.  
  142.         return buffer;
  143.     }
  144.  
  145.     public void run() {
  146.         setupContext();
  147.         setupOpenGL();
  148.         while (glfwWindowShouldClose(windowID) != GL_TRUE) {
  149.             loop();
  150.             glfwSwapBuffers(windowID);
  151.             glfwPollEvents();
  152.         }
  153.         glfwDestroyWindow(windowID);
  154.         glfwTerminate();
  155.     }
  156.  
  157.     public void setupContext() {
  158.         glfwSetErrorCallback(errorCallback);
  159.  
  160.         glfwInit();
  161.  
  162.         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  163.         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
  164.         glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  165.         glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  166.         glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
  167.         glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  168.  
  169.         windowID = glfwCreateWindow(900, 900, "TEXTURE TEST", NULL, NULL);
  170.  
  171.         glfwMakeContextCurrent(windowID);
  172.  
  173.         glfwSetKeyCallback(windowID, keyCallback);
  174.         GL.createCapabilities();
  175.  
  176.         glError = GLUtil.setupDebugMessageCallback();
  177.     }
  178.  
  179.     public void setupOpenGL() {
  180.         glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
  181.         glEnable(GL_BLEND);
  182.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  183.         int vao = glGenVertexArrays();
  184.         glBindVertexArray(vao);
  185.  
  186.         int vbo = glGenBuffers();
  187.  
  188.         float vboArray[] = new float[] {
  189.                 -0.75f, -0.75f, 0.0f, 0.0f,
  190.                 -0.75f, 0.75f, 0.0f, 1.0f,
  191.                 0.75f, 0.75f, 1.0f, 1.0f,
  192.                 0.75f, -0.75f, 1.0f, 0.0f
  193.         };
  194.  
  195.         FloatBuffer vboBuffer = generateFloatBuffer(vboArray);
  196.  
  197.         glBindBuffer(GL_ARRAY_BUFFER, vbo);
  198.         glBufferData(GL_ARRAY_BUFFER, vboBuffer, GL_STATIC_DRAW);
  199.  
  200.         int ebo = glGenBuffers();
  201.  
  202.         short eboArray[] = new short[] {
  203.                 0, 1, 2,
  204.                 2, 3, 0
  205.         };
  206.  
  207.         ShortBuffer eboBuffer = generateShortBuffer(eboArray);
  208.  
  209.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  210.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, eboBuffer, GL_STATIC_DRAW);
  211.  
  212.         String vertexShaderSource = "#version 150 core\n" +
  213.                 "in vec2 pos;\n" +
  214.                 "in vec2 uv;\n" +
  215.                 "out vec2 UV;\n" +
  216.                 "void main() {\n" +
  217.                 "UV = uv;\n" +
  218.                 "gl_Position = vec4(pos, 0.0, 1.0);\n" +
  219.                 "}";
  220.  
  221.         String fragmentShaderSource = "#version 150 core\n" +
  222.                 "in vec2 UV;\n" +
  223.                 "uniform sampler2D tex;\n" +
  224.                 "out vec4 fragColor;\n" +
  225.                 "void main() {\n" +
  226.                 "fragColor = texture(tex, UV);\n" +
  227.                 "}";
  228.  
  229.         int vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
  230.         int fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
  231.         int program = linkProgram(vertexShader, fragmentShader);
  232.  
  233.         glValidateProgram(program);
  234.         glUseProgram(program);
  235.  
  236.         posAttrib = glGetAttribLocation(program, "pos");
  237.         glEnableVertexAttribArray(posAttrib);
  238.         glVertexAttribPointer(posAttrib, 2, GL_FLOAT, false, 4, 0);
  239.  
  240.         uvAttrib = glGetAttribLocation(program, "uv");
  241.         glEnableVertexAttribArray(uvAttrib);
  242.         glVertexAttribPointer(uvAttrib, 2, GL_FLOAT, false, 4, 2);
  243.  
  244.         glBindFragDataLocation(program, 0, "fragColor");
  245.  
  246.         texUni = glGetUniformLocation(program, "tex");
  247.  
  248.         tex = glGenTextures();
  249.         glActiveTexture(GL_TEXTURE0);
  250.         glBindTexture(GL_TEXTURE_2D, tex);
  251.         ByteBuffer textureBuffer = getTexture("/borderPanelTest.png");
  252.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureBuffer);
  253.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  254.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  255.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  256.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  257.     }
  258.  
  259.     public void loop() {
  260.         glClear(GL_COLOR_BUFFER_BIT);
  261.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement