Guest User

Untitled

a guest
Jul 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. package com.shpaker.experiment;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.ByteOrder;
  5. import java.nio.IntBuffer;
  6.  
  7. import javax.microedition.khronos.opengles.GL10;
  8.  
  9. import android.content.Context;
  10. import android.graphics.Bitmap;
  11. import android.graphics.BitmapFactory;
  12. import android.opengl.GLUtils;
  13.  
  14. public class Image {
  15.  
  16.     private final IntBuffer mTextureBuffer;
  17.     private int x, y, width, height;
  18.     private int texCoords[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  19.  
  20.     private void setVerticles(int x, int y, int width, int height) {
  21.         this.x(x);
  22.         this.y(y);
  23.         this.width(width);
  24.         this.height(height);
  25.         this.texCoords[0] = x;
  26.         this.texCoords[1] = y;
  27.         this.texCoords[2] = x + width;
  28.         this.texCoords[3] = y;
  29.         this.texCoords[4] = x;
  30.         this.texCoords[5] = y + height;
  31.         this.texCoords[6] = x + width;
  32.         this.texCoords[7] = y + height;
  33.     }
  34.  
  35.     public Image() {
  36.         setVerticles(0, 0, 0, 0);
  37.         ByteBuffer tdd = ByteBuffer.allocateDirect(texCoords.length * 4);
  38.         tdd.order(ByteOrder.nativeOrder());
  39.         mTextureBuffer = tdd.asIntBuffer();
  40.     }
  41.  
  42.     public void src(GL10 gl, Context context, int resource) {
  43.         Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),
  44.                 resource);
  45.         GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
  46.         gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
  47.                 GL10.GL_LINEAR);
  48.         gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
  49.                 GL10.GL_LINEAR);
  50.         bmp.recycle();
  51.     }
  52.  
  53.  
  54.     public void drawImage(GL10 gl, int x, int y, int width, int height) {
  55.         gl.glEnable(GL10.GL_TEXTURE_2D);
  56.         gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, mTextureBuffer);
  57.         setVerticles(x, y, width, height);
  58.         mTextureBuffer.put(texCoords);
  59.         mTextureBuffer.position(0);
  60.         gl.glVertexPointer(2, GL10.GL_FIXED, 0, mTextureBuffer);
  61.         // gl.glColor4f(1, 1, 1, 0);
  62.         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
  63.     }
  64. }
Add Comment
Please, Sign In to add comment