Guest User

Untitled

a guest
Oct 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. public class Texture {
  2.  
  3. /**
  4. * Stores the handle of the texture.
  5. */
  6. private final int id;
  7.  
  8. /**
  9. * Width of the texture.
  10. */
  11. private int width;
  12.  
  13. /**
  14. * Height of the texture.
  15. */
  16. private int height;
  17.  
  18. /** Creates a texture. */
  19. public Texture() {
  20. id = glGenTextures();
  21. }
  22.  
  23. /**
  24. * Binds the texture.
  25. */
  26. public void bind() {
  27. glBindTexture(GL_TEXTURE_2D, id);
  28. }
  29.  
  30. /**
  31. * Sets a parameter of the texture.
  32. *
  33. * @param name Name of the parameter
  34. * @param value Value to set
  35. */
  36. public void setParameter(int name, int value) {
  37. glTexParameteri(GL_TEXTURE_2D, name, value);
  38. }
  39.  
  40. /**
  41. * Uploads image data with specified width and height.
  42. *
  43. * @param width Width of the image
  44. * @param height Height of the image
  45. * @param data Pixel data of the image
  46. */
  47. public void uploadData(int width, int height, ByteBuffer data) {
  48. uploadData(GL_RGBA8, width, height, GL_RGBA, data);
  49. }
  50.  
  51. /**
  52. * Uploads image data with specified internal format, width, height and
  53. * image format.
  54. *
  55. * @param internalFormat Internal format of the image data
  56. * @param width Width of the image
  57. * @param height Height of the image
  58. * @param format Format of the image data
  59. * @param data Pixel data of the image
  60. */
  61. public void uploadData(int internalFormat, int width, int height, int format, ByteBuffer data) {
  62. glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, data);
  63. }
  64.  
  65. /**
  66. * Delete the texture.
  67. */
  68. public void delete() {
  69. glDeleteTextures(id);
  70. }
  71.  
  72. /**
  73. * Gets the texture width.
  74. *
  75. * @return Texture width
  76. */
  77. public int getWidth() {
  78. return width;
  79. }
  80.  
  81. /**
  82. * Sets the texture width.
  83. *
  84. * @param width The width to set
  85. */
  86. public void setWidth(int width) {
  87. if (width > 0) {
  88. this.width = width;
  89. }
  90. }
  91.  
  92. /**
  93. * Gets the texture height.
  94. *
  95. * @return Texture height
  96. */
  97. public int getHeight() {
  98. return height;
  99. }
  100.  
  101. /**
  102. * Sets the texture height.
  103. *
  104. * @param height The height to set
  105. */
  106. public void setHeight(int height) {
  107. if (height > 0) {
  108. this.height = height;
  109. }
  110. }
  111.  
  112. /**
  113. * Creates a texture with specified width, height and data.
  114. *
  115. * @param width Width of the texture
  116. * @param height Height of the texture
  117. * @param data Picture Data in RGBA format
  118. *
  119. * @return Texture from the specified data
  120. */
  121. public static Texture createTexture(int width, int height, ByteBuffer data) {
  122. Texture texture = new Texture();
  123. texture.setWidth(width);
  124. texture.setHeight(height);
  125.  
  126. texture.bind();
  127.  
  128. texture.setParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  129. texture.setParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  130. texture.setParameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  131. texture.setParameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  132. texture.setParameter(GL14.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
  133.  
  134. texture.uploadData(GL_RGBA8, width, height, GL_RGBA, data);
  135.  
  136. return texture;
  137. }
  138.  
  139. /**
  140. * Load texture from file.
  141. *
  142. * @param path File path of the texture
  143. *
  144. * @return Texture from specified file
  145. */
  146. public static Texture loadTexture(String path) {
  147. ByteBuffer image;
  148. int width, height;
  149.  
  150. try (MemoryStack stack = MemoryStack.stackPush()) {
  151. /* Prepare image buffers */
  152. IntBuffer w = stack.mallocInt(1);
  153. IntBuffer h = stack.mallocInt(1);
  154. IntBuffer comp = stack.mallocInt(1);
  155.  
  156. /* Load image */
  157. URL url = Thread.currentThread().getContextClassLoader().getResource(path);
  158. image = stbi_load(url.getPath(), w, h, comp, 4);
  159. if (image == null) {
  160. throw new RuntimeException("Failed to load a texture file!" + System.lineSeparator() + stbi_failure_reason());
  161. }
  162.  
  163. /* Get width and height of image */
  164. width = w.get();
  165. height = h.get();
  166. }
  167.  
  168. stbi_image_free(image);
  169. return createTexture(width, height, image);
  170. }
  171. }
  172.  
  173. GL11.glPushMatrix();
  174. GL11.glEnable(GL11.GL_TEXTURE_2D);
  175.  
  176. background.bind();
  177.  
  178. GL11.glBegin(GL11.GL_QUADS);
  179.  
  180. GL11.glTexCoord2f(0F, 0F);
  181. GL11.glVertex2f(-1F, 1F);
  182.  
  183. GL11.glTexCoord2f(1F, 0F);
  184. GL11.glVertex2f(1F, 1F);
  185.  
  186. GL11.glTexCoord2f(1F, 1F);
  187. GL11.glVertex2f(1F, -1F);
  188.  
  189. GL11.glTexCoord2f(0F, 1F);
  190. GL11.glVertex2f(-1F, -1F);
  191.  
  192. GL11.glDisable(GL11.GL_TEXTURE_2D);
  193. GL11.glEnd();
  194. GL11.glPopMatrix();
Add Comment
Please, Sign In to add comment