Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GLTexture {
- public static final FloatBuffer DEFAULT_UV_COORDS = GLRasterizer.allocateFloatBuffer(new float[] {
- 0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f
- });
- private static Map<Integer, GLTexture> cacheTextures = new HashMap<Integer, GLTexture>();
- private static boolean textureActive;
- private static int boundTexture = -1;
- private IntBuffer pixels;
- private int textureID;
- private int width;
- private int height;
- private FloatBuffer triangleVertices;
- private FloatBuffer triangleUVs;
- private ByteBuffer triangleColors;
- private int triangleVertexCount;
- public GLTexture(int[] pixelArray) { // 3d model textures only here!
- this((int) Math.sqrt(pixelArray.length), (int) Math.sqrt(pixelArray.length), pixelArray);
- triangleVertices = GLRasterizer.allocateFloatBuffer(9 * 4096);
- triangleUVs = GLRasterizer.allocateFloatBuffer(6 * 4096);
- triangleColors = GLRasterizer.allocateByteBuffer(9 * 4096);
- }
- public GLTexture(int width, int height, int[] pixelArray) {
- this.width = width;
- this.height = height;
- setPixels(pixelArray);
- textureID = glGenTextures();
- bind();
- //glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
- unbind();
- }
- public boolean bind() {
- if (textureActive && boundTexture == textureID) {
- return false;
- }
- glEnable(GL_TEXTURE_2D);
- if (boundTexture != textureID) {
- glBindTexture(GL_TEXTURE_2D, textureID);
- boundTexture = textureID;
- }
- textureActive = true;
- return true;
- }
- public boolean unbind() {
- return unbindGLTexture();
- }
- public void update(int[] pixelArray) {
- if (pixelArray.length != pixels.limit()) {
- width = height = (int) Math.sqrt(pixelArray.length);
- }
- update(width, height, pixelArray);
- }
- public void update(int width, int height, int[] pixelArray) {
- this.width = width;
- this.height = height;
- setPixels(pixelArray);
- update();
- }
- public void update() {
- bind();
- glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
- unbind();
- }
- public void incrimentTriangles() {
- triangleVertexCount += 3;
- }
- public int getWidth() {
- return width;
- }
- public int getHeight() {
- return height;
- }
- public IntBuffer getPixels() {
- return pixels;
- }
- public FloatBuffer getTriangleVertices() {
- return triangleVertices;
- }
- public FloatBuffer getTriangleUVs() {
- return triangleUVs;
- }
- public ByteBuffer getTriangleColors() {
- return triangleColors;
- }
- public boolean draw() {
- if (triangleVertices.position() == 0) {
- return false;
- }
- boolean neededBind = bind();
- triangleColors.flip();
- triangleUVs.flip();
- triangleVertices.flip();
- glColorPointer(3, GL_UNSIGNED_BYTE, 0, triangleColors);
- glTexCoordPointer(2, 0, triangleUVs);
- glVertexPointer(3, 0, triangleVertices);
- glDrawArrays(GL_TRIANGLES, 0, triangleVertexCount);
- triangleColors.clear();
- triangleUVs.clear();
- triangleVertices.clear();
- triangleVertexCount = 0;
- return neededBind;
- }
- private void setPixels(int[] pixelArray) {
- if (pixels != null && pixelArray.length <= pixels.capacity()) {
- pixels.clear();
- } else {
- pixels = GLRasterizer.allocateIntBuffer(pixelArray.length);
- }
- for (int i = 0; i < pixelArray.length; i++) {
- pixels.put(pixelArray[i] | ((pixelArray[i] != 0) ? (255 << 24) : 0));
- }
- pixels.flip();
- }
- public static GLTexture getGLTexture(int cacheID, int[] pixels) {
- GLTexture texture = cacheTextures.get(cacheID);
- if (texture == null) {
- cacheTextures.put(cacheID, (texture = new GLTexture(pixels)));
- }
- return texture;
- }
- public static int drawGLTextures() {
- int count = 0;
- for (GLTexture texture : cacheTextures.values()) {
- count += (texture.draw() ? 1 : 0);
- }
- return count;
- }
- public static void updateGLTexture(int cacheID, int[] pixels) {
- GLTexture texture = cacheTextures.get(cacheID);
- if (texture == null) {
- return;
- }
- texture.update(pixels);
- }
- public static boolean unbindGLTexture() {
- if (!textureActive) {
- return false;
- }
- glDisable(GL_TEXTURE_2D);
- textureActive = false;
- return true;
- }
- public static FloatBuffer getDefaultUVCoords() {
- DEFAULT_UV_COORDS.rewind();
- return DEFAULT_UV_COORDS;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement