Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. public class GLTexture {
  2. public static final FloatBuffer DEFAULT_UV_COORDS = GLRasterizer.allocateFloatBuffer(new float[] {
  3. 0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f
  4. });
  5.  
  6. private static Map<Integer, GLTexture> cacheTextures = new HashMap<Integer, GLTexture>();
  7. private static boolean textureActive;
  8. private static int boundTexture = -1;
  9.  
  10. private IntBuffer pixels;
  11. private int textureID;
  12. private int width;
  13. private int height;
  14. private FloatBuffer triangleVertices;
  15. private FloatBuffer triangleUVs;
  16. private ByteBuffer triangleColors;
  17. private int triangleVertexCount;
  18.  
  19. public GLTexture(int[] pixelArray) { // 3d model textures only here!
  20. this((int) Math.sqrt(pixelArray.length), (int) Math.sqrt(pixelArray.length), pixelArray);
  21. triangleVertices = GLRasterizer.allocateFloatBuffer(9 * 4096);
  22. triangleUVs = GLRasterizer.allocateFloatBuffer(6 * 4096);
  23. triangleColors = GLRasterizer.allocateByteBuffer(9 * 4096);
  24. }
  25.  
  26. public GLTexture(int width, int height, int[] pixelArray) {
  27. this.width = width;
  28. this.height = height;
  29. setPixels(pixelArray);
  30. textureID = glGenTextures();
  31. bind();
  32. //glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  33. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  34. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  35. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  36. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  37. //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  38. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
  39. unbind();
  40. }
  41.  
  42. public boolean bind() {
  43. if (textureActive && boundTexture == textureID) {
  44. return false;
  45. }
  46. glEnable(GL_TEXTURE_2D);
  47. if (boundTexture != textureID) {
  48. glBindTexture(GL_TEXTURE_2D, textureID);
  49. boundTexture = textureID;
  50. }
  51. textureActive = true;
  52. return true;
  53. }
  54.  
  55. public boolean unbind() {
  56. return unbindGLTexture();
  57. }
  58.  
  59. public void update(int[] pixelArray) {
  60. if (pixelArray.length != pixels.limit()) {
  61. width = height = (int) Math.sqrt(pixelArray.length);
  62. }
  63. update(width, height, pixelArray);
  64. }
  65.  
  66. public void update(int width, int height, int[] pixelArray) {
  67. this.width = width;
  68. this.height = height;
  69. setPixels(pixelArray);
  70. update();
  71. }
  72.  
  73. public void update() {
  74. bind();
  75. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
  76. unbind();
  77. }
  78.  
  79. public void incrimentTriangles() {
  80. triangleVertexCount += 3;
  81. }
  82.  
  83. public int getWidth() {
  84. return width;
  85. }
  86.  
  87. public int getHeight() {
  88. return height;
  89. }
  90.  
  91. public IntBuffer getPixels() {
  92. return pixels;
  93. }
  94.  
  95. public FloatBuffer getTriangleVertices() {
  96. return triangleVertices;
  97. }
  98.  
  99. public FloatBuffer getTriangleUVs() {
  100. return triangleUVs;
  101. }
  102.  
  103. public ByteBuffer getTriangleColors() {
  104. return triangleColors;
  105. }
  106.  
  107. public boolean draw() {
  108. if (triangleVertices.position() == 0) {
  109. return false;
  110. }
  111. boolean neededBind = bind();
  112. triangleColors.flip();
  113. triangleUVs.flip();
  114. triangleVertices.flip();
  115. glColorPointer(3, GL_UNSIGNED_BYTE, 0, triangleColors);
  116. glTexCoordPointer(2, 0, triangleUVs);
  117. glVertexPointer(3, 0, triangleVertices);
  118. glDrawArrays(GL_TRIANGLES, 0, triangleVertexCount);
  119. triangleColors.clear();
  120. triangleUVs.clear();
  121. triangleVertices.clear();
  122. triangleVertexCount = 0;
  123. return neededBind;
  124. }
  125.  
  126. private void setPixels(int[] pixelArray) {
  127. if (pixels != null && pixelArray.length <= pixels.capacity()) {
  128. pixels.clear();
  129. } else {
  130. pixels = GLRasterizer.allocateIntBuffer(pixelArray.length);
  131. }
  132. for (int i = 0; i < pixelArray.length; i++) {
  133. pixels.put(pixelArray[i] | ((pixelArray[i] != 0) ? (255 << 24) : 0));
  134. }
  135. pixels.flip();
  136. }
  137.  
  138. public static GLTexture getGLTexture(int cacheID, int[] pixels) {
  139. GLTexture texture = cacheTextures.get(cacheID);
  140. if (texture == null) {
  141. cacheTextures.put(cacheID, (texture = new GLTexture(pixels)));
  142. }
  143. return texture;
  144. }
  145.  
  146. public static int drawGLTextures() {
  147. int count = 0;
  148. for (GLTexture texture : cacheTextures.values()) {
  149. count += (texture.draw() ? 1 : 0);
  150. }
  151. return count;
  152. }
  153.  
  154. public static void updateGLTexture(int cacheID, int[] pixels) {
  155. GLTexture texture = cacheTextures.get(cacheID);
  156. if (texture == null) {
  157. return;
  158. }
  159. texture.update(pixels);
  160. }
  161.  
  162. public static boolean unbindGLTexture() {
  163. if (!textureActive) {
  164. return false;
  165. }
  166. glDisable(GL_TEXTURE_2D);
  167. textureActive = false;
  168. return true;
  169. }
  170.  
  171. public static FloatBuffer getDefaultUVCoords() {
  172. DEFAULT_UV_COORDS.rewind();
  173. return DEFAULT_UV_COORDS;
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement