Advertisement
Pitzap

egioc lab5

May 12th, 2024 (edited)
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.22 KB | Source Code | 0 0
  1. package cg.helloworld;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.opengl.GLSurfaceView;
  8. import android.opengl.GLUtils;
  9. import android.os.Bundle;
  10. import android.view.WindowManager;
  11.  
  12. import javax.microedition.khronos.egl.EGLConfig;
  13. import javax.microedition.khronos.opengles.GL10;
  14. import javax.microedition.khronos.opengles.GL11;
  15.  
  16. import androidx.activity.EdgeToEdge;
  17. import androidx.appcompat.app.AppCompatActivity;
  18. import androidx.core.graphics.Insets;
  19. import androidx.core.view.ViewCompat;
  20. import androidx.core.view.WindowInsetsCompat;
  21.  
  22. import java.nio.ByteBuffer;
  23. import java.nio.ByteOrder;
  24. import java.nio.FloatBuffer;
  25.  
  26.  
  27. public class MainActivity extends AppCompatActivity {
  28.  
  29.     @Override
  30.     public void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  33.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
  34.         GLSurfaceView view = new GLSurfaceView(this);
  35.         view.setRenderer(new SquareRenderer(this.getApplicationContext()));
  36.         setContentView(view);
  37.     }
  38. }
  39.  
  40. class Square {
  41.     private FloatBuffer mFVertexBuffer;
  42.     private ByteBuffer mColorBuffer;
  43.     private ByteBuffer mIndexBuffer;
  44.     public FloatBuffer mTextureBuffer;
  45.     //public float[] textureCoords;
  46.     //public float texIncrease;
  47.     private int[] textures = new int[1];
  48.     public Square(){
  49.  
  50.         float vertices[] =
  51.                 {
  52.                         -1.0f, -0.7f,
  53.                         1.0f, -0.30f,
  54.                         -1.0f, 0.70f,
  55.                         1.0f, 0.30f,
  56.                 };
  57.  
  58.         byte maxColor=(byte)255;
  59.         byte colors[] =
  60.                 {
  61.                         maxColor,maxColor , maxColor, 0,
  62.                         maxColor,maxColor , maxColor, 0,
  63.                         maxColor,maxColor , maxColor, 0,
  64.                         maxColor,maxColor , maxColor, 0,
  65.                 };
  66.         byte indices[] =
  67.                 {
  68.                         0, 3, 1,
  69.                         0, 2, 3
  70.                 };
  71.  
  72.         float[] textureCoords =
  73.                 {
  74.                         0.0f , 2.0f,
  75.                         2.0f , 2.0f,
  76.                         0.0f , 0.0f,
  77.                         2.0f , 0.0f
  78.                 };
  79.         ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
  80.         vbb.order(ByteOrder.nativeOrder());
  81.         mFVertexBuffer = vbb.asFloatBuffer();
  82.         mFVertexBuffer.put(vertices);
  83.         mFVertexBuffer.position(0);
  84.         mColorBuffer = ByteBuffer.allocateDirect(colors.length);
  85.         mColorBuffer.put(colors);
  86.         mColorBuffer.position(0);
  87.         mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
  88.         mIndexBuffer.put(indices);
  89.         mIndexBuffer.position(0);ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
  90.         tbb.order(ByteOrder.nativeOrder());
  91.         mTextureBuffer = tbb.asFloatBuffer();
  92.         mTextureBuffer.put(textureCoords);
  93.         mTextureBuffer.position(0);
  94.  
  95.     }
  96.  
  97.     public void draw(GL10 gl){
  98.         gl.glFrontFace(GL11.GL_CW);
  99.         gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);
  100.         //gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);
  101.         gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, mIndexBuffer);
  102.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  103.         gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, mColorBuffer);
  104.         gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  105.         gl.glEnable(GL10.GL_TEXTURE_2D);
  106.         gl.glEnable(GL10.GL_BLEND);
  107.         gl.glBlendFunc(GL10.GL_ONE, GL10.GL_SRC_COLOR);
  108.         gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  109.         gl.glTexCoordPointer(2, GL10.GL_FLOAT,0, mTextureBuffer);
  110.         gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  111.         textureCoords[0]+=texIncrease;
  112.         textureCoords[1]+=texIncrease;
  113.         textureCoords[2]+=texIncrease;
  114.         textureCoords[3]+=texIncrease;
  115.         textureCoords[4]+=texIncrease;
  116.         textureCoords[5]+=texIncrease;
  117.         textureCoords[6]+=texIncrease;
  118.         textureCoords[7]+=texIncrease;
  119.        
  120.     }
  121.  
  122.     public void createTexture(GL10 gl, Context contextRegf, int resource){
  123.         Bitmap image = BitmapFactory.decodeResource(contextRegf.getResources(), resource);
  124.         gl.glGenTextures(1, textures, 0);
  125.         gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  126.         GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0);
  127.         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
  128.         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  129.         image.recycle();
  130.     }
  131. }
  132.  
  133.  
  134. class SquareRenderer implements GLSurfaceView.Renderer {
  135.  
  136.     private Square mSquare;
  137.     private float mTransY;
  138.     private Context context;
  139.  
  140.     public SquareRenderer(Context context)
  141.     {
  142.         this.context = context;
  143.         this.mSquare = new Square();
  144.     }
  145.  
  146.     public void onDrawFrame(GL10 gl) {
  147.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  148.         gl.glMatrixMode(GL10.GL_MODELVIEW);
  149.         gl.glLoadIdentity();
  150.         gl.glTranslatef(0.0f, (float) Math.sin(mTransY), -3.0f);
  151.         mTransY += 0.075f;
  152.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  153.         gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  154.  
  155.         mSquare.draw(gl);
  156.     }
  157.  
  158.     public void onSurfaceChanged(GL10 gl, int width, int height){
  159.         gl.glViewport(0, 0, width, height);
  160.         gl.glMatrixMode(GL10.GL_PROJECTION);
  161.         gl.glLoadIdentity();
  162.         float ratio = (float) width / height;
  163.         gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
  164.     }
  165.  
  166.     public void onSurfaceCreated(GL10 gl, EGLConfig config){
  167.         gl.glDisable(GL10.GL_DITHER);
  168.         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
  169.         gl.glClearColor(0,0,0,0);
  170.         gl.glEnable(GL10.GL_CULL_FACE);
  171.         gl.glShadeModel(GL10.GL_SMOOTH);
  172.         gl.glEnable(GL10.GL_DEPTH_TEST);
  173.         int resid = R.drawable.obama;
  174.         mSquare.createTexture(gl, this.context, resid);
  175.     }
  176. }
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement