Advertisement
Pitzap

lab3

May 12th, 2024
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | Source Code | 0 0
  1. package cg.helloworld;
  2.  
  3. import android.app.Activity;
  4. import android.opengl.GLSurfaceView;
  5. import android.os.Bundle;
  6. import android.view.WindowManager;
  7.  
  8. import javax.microedition.khronos.egl.EGLConfig;
  9. import javax.microedition.khronos.opengles.GL10;
  10. import javax.microedition.khronos.opengles.GL11;
  11.  
  12. import androidx.activity.EdgeToEdge;
  13. import androidx.appcompat.app.AppCompatActivity;
  14. import androidx.core.graphics.Insets;
  15. import androidx.core.view.ViewCompat;
  16. import androidx.core.view.WindowInsetsCompat;
  17.  
  18. import java.nio.ByteBuffer;
  19. import java.nio.ByteOrder;
  20. import java.nio.FloatBuffer;
  21.  
  22.  
  23. public class MainActivity extends AppCompatActivity {
  24.  
  25.     @Override
  26.     public void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  29.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
  30.         GLSurfaceView view = new GLSurfaceView(this);
  31.         view.setRenderer(new SquareRenderer());
  32.         setContentView(view);
  33.     }
  34. }
  35.  
  36. class HelloWorldRenderer implements GLSurfaceView.Renderer {
  37.     @Override
  38.     public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig eglConfig) {
  39.         gl.glClearColor(1, 0, 0, 1);
  40.     }
  41.  
  42.     public void onSurfaceChanged(GL10 gl, int width, int height) {
  43.         gl.glViewport(0, 0, width, height);
  44.     }
  45.  
  46.     public void onDrawFrame(GL10 gl) {
  47.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  48.     }
  49.  
  50. }
  51.  
  52. class Square {
  53.     private FloatBuffer mFVertexBuffer;
  54.     private ByteBuffer mColorBuffer;
  55.     private ByteBuffer mIndexBuffer;
  56.     public Square() {
  57.  
  58.         float vertices[] =
  59.                 {
  60.                         -1.0f, -1.0f,
  61.                         1.0f, -1.0f,
  62.                         -1.0f, 1.0f,
  63.                         1.0f, 1.0f
  64.                 };
  65.         byte maxColor = (byte) 255;
  66.         byte colors[] =
  67.                 {
  68.                         0, 0, 0, maxColor,
  69.                         maxColor, 0, 0, maxColor,
  70.                         0, 0, 0, maxColor,
  71.                         maxColor, 0, 0, maxColor,
  72.                 };
  73.         byte indices[] =
  74.                 {
  75.                         0, 3, 1,
  76.                         0, 2, 3
  77.                 };
  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);
  90.     }
  91.     public void draw(GL10 gl){
  92.         gl.glFrontFace(GL11.GL_CW);
  93.         gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);
  94.         gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);
  95.         gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, mIndexBuffer);
  96.     }
  97. }
  98.  
  99. class SquareRenderer implements GLSurfaceView.Renderer{
  100.     private Square mSquare;
  101.     private float mTransY;
  102.  
  103.     public SquareRenderer(){
  104.         mSquare = new Square();
  105.     }
  106.  
  107.     @Override
  108.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  109.         gl.glDisable(GL10.GL_DITHER);
  110.         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
  111.         gl.glClearColor(0,0,0,0);
  112.         gl.glEnable(GL10.GL_CULL_FACE);
  113.         gl.glShadeModel(GL10.GL_SMOOTH);
  114.         gl.glEnable(GL10.GL_DEPTH_TEST);
  115.     }
  116.  
  117.     @Override
  118.     public void onSurfaceChanged(GL10 gl, int width, int height) {
  119.         gl.glViewport(0, 0, width, height);
  120.         gl.glMatrixMode(GL10.GL_PROJECTION);
  121.         gl.glLoadIdentity();
  122.         float ratio = (float) width / height;
  123.         gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
  124.     }
  125.  
  126.     @Override
  127.     public void onDrawFrame(GL10 gl) {
  128.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  129.         gl.glMatrixMode(GL10.GL_MODELVIEW);
  130.         gl.glLoadIdentity();
  131.         gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -3.0f);
  132.         mTransY += 0.075f;
  133.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  134.         gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  135.         mSquare.draw(gl);
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement