美文网首页
OpenGL Android

OpenGL Android

作者: 贼噶人 | 来源:发表于2021-01-15 08:44 被阅读0次
    package com.example;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.opengl.GLES30;
    import android.opengl.GLSurfaceView;
    import android.opengl.Matrix;
    import android.os.Bundle;
    import android.view.MotionEvent;
    
    import com.example.test_webview_demo.R;
    
    import java.nio.ByteBuffer;
    import java.nio.ByteOrder;
    import java.nio.FloatBuffer;
    import java.nio.IntBuffer;
    
    import javax.microedition.khronos.egl.EGLConfig;
    import javax.microedition.khronos.opengles.GL10;
    
    import androidx.annotation.Nullable;
    
    public class OpenGLActivity extends Activity {
    
      int sumX;
      int sunY;
      float x = 0;
      float y = 0;
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final GLSurfaceView glSurfaceView = new GLSurfaceView(this) {
          @Override
          public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()){
              case MotionEvent.ACTION_DOWN:
                x = event.getX();
                y = event.getY();
                break;
              case MotionEvent.ACTION_MOVE:
                float mX = event.getX();
                float mY = event.getY();
                sumX += x - mX;
                sunY += mY - y;
                x = mX;
                y = mY;
                break;
            }
            return true;
          }
        };
        glSurfaceView.setEGLContextClientVersion(3);
        glSurfaceView.setRenderer(
            new GLSurfaceView.Renderer() {
              int programId = -1;
              int vbo;
              int vao;
              @Override
              public void onSurfaceCreated(GL10 gl, EGLConfig config) {
                GLES30.glClearColor(1, 0, 0, 1);
                final int vShader = GLES30.glCreateShader(GLES30.GL_VERTEX_SHADER);
                GLES30.glShaderSource(
                    vShader,
                    "#version 320 es\n"
                        + "layout(location = 0) in vec3 Position;\n"
                        + "layout(location = 1) in vec2 textureCoordinate;\n"
                        + "out vec2 textureCoord;\n"
                        + "uniform mat4 modelMatrix;\n"
                        + "uniform mat4 orthoMatrix;\n"
                        + "uniform mat4 viewMatrix;\n"
                        + "void main()\n"
                        + "{\n"
                        + "gl_Position = orthoMatrix * viewMatrix * modelMatrix * vec4(Position,1"
                        + ".0);\n"
                        + "textureCoord = textureCoordinate;\n"
                        + "}\n");
                GLES30.glCompileShader(vShader);
                IntBuffer intBuffer = IntBuffer.allocate(1);
                GLES30.glGetShaderiv(vShader, GLES30.GL_COMPILE_STATUS, intBuffer);
                if (GLES30.GL_TRUE != intBuffer.get()) {
                  System.out.println("GL_VERTEX_SHADER-->" + GLES30.glGetShaderInfoLog(vShader));
                  return;
                }
                final int fShader = GLES30.glCreateShader(GLES30.GL_FRAGMENT_SHADER);
                GLES30.glShaderSource(
                    fShader,
                    "#version 320 es\n"
                        + "precision mediump float;\n"
                        + "uniform sampler2D texture0;\n"
                        + "in vec2 textureCoord;\n"
                        + "out vec4 fragColor;\n"
                        + "void main()\n"
                        + "{\n"
                        + "fragColor = texture(texture0, vec2(textureCoord.x,1.0 - textureCoord.y));\n"
                        + "}\n");
                GLES30.glCompileShader(fShader);
                intBuffer.clear();
                GLES30.glGetShaderiv(fShader, GLES30.GL_COMPILE_STATUS, intBuffer);
                if (GLES30.GL_TRUE != intBuffer.get()) {
                  System.out.println("GL_FRAGMENT_SHADER-->" + GLES30.glGetShaderInfoLog(fShader));
                  return;
                }
                this.programId = GLES30.glCreateProgram();
                GLES30.glAttachShader(programId, vShader);
                GLES30.glAttachShader(programId, fShader);
                GLES30.glLinkProgram(programId);
                intBuffer.clear();
                GLES30.glGetProgramiv(programId, GLES30.GL_LINK_STATUS, intBuffer);
                if (GLES30.GL_TRUE != intBuffer.get()) {
                  System.out.println("GL_LINK_STATUS--->" + GLES30.glGetProgramInfoLog(programId));
                  return;
                }
                GLES30.glDeleteShader(vShader);
                GLES30.glDeleteShader(fShader);
                float triangleCoords[] = {
                  -0.5f, -0.5f, 1.0f, 0, 0, -0.5f, 0.5f, 1.0f, 0, 1, 0.5f, 0.5f, 1.0f, 1, 1, 0.5f, 0.5f,
                  1.0f, 1, 1, 0.5f, -0.5f, 1.0f, 1, 0, -0.5f, -0.5f, 1.0f, 0, 0
                };
                ByteBuffer buffer = ByteBuffer.allocateDirect(triangleCoords.length * 4);
                buffer.order(ByteOrder.nativeOrder());
                FloatBuffer vertexBuffer = buffer.asFloatBuffer();
                vertexBuffer.put(triangleCoords);
                vertexBuffer.position(0);
                final IntBuffer vbo = IntBuffer.allocate(1);
                final IntBuffer vao = IntBuffer.allocate(1);
                GLES30.glGenBuffers(1, vao);
                GLES30.glGenBuffers(1, vbo);
                this.vao = vao.get(0);
                this.vbo = vbo.get(0);
                GLES30.glBindVertexArray(this.vao);
                GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, this.vbo);
                GLES30.glBufferData(
                    GLES30.GL_ARRAY_BUFFER, buffer.capacity(), vertexBuffer, GLES30.GL_STATIC_DRAW);
                GLES30.glVertexAttribPointer(0, 3, GLES30.GL_FLOAT, false, 5 * 4, 0);
                GLES30.glEnableVertexAttribArray(0);
                GLES30.glVertexAttribPointer(1, 2, GLES30.GL_FLOAT, false, 5 * 4, 3 * 4);
                GLES30.glEnableVertexAttribArray(1);
                GLES30.glClearColor(1.0f, 0, 0, 1);
                final IntBuffer textures = IntBuffer.allocate(1);
                GLES30.glGenTextures(1, textures);
                GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textures.get(0));
                GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER,
                        GLES30.GL_LINEAR);
                GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER,
                        GLES30.GL_LINEAR);
                final Bitmap bitmap =
                    BitmapFactory.decodeStream(getResources().openRawResource(R.raw.timg));
                final ByteBuffer pixelsBuffer = ByteBuffer.allocateDirect(bitmap.getByteCount());
                bitmap.copyPixelsToBuffer(pixelsBuffer);
                pixelsBuffer.position(0);
                GLES30.glTexImage2D(
                    GLES30.GL_TEXTURE_2D,
                    0,
                    GLES30.GL_RGBA,
                    bitmap.getWidth(),
                    bitmap.getHeight(),
                    0,
                    GLES30.GL_RGBA,
                    GLES30.GL_UNSIGNED_BYTE,
                    pixelsBuffer);
                Matrix.scaleM(modelMatrix, 0, 1000,
                        1000 * (1.0f * bitmap.getHeight() / bitmap.getWidth()), 1);
                bitmap.recycle();
                texture = textures.get(0);
              }
    
              float[] modelMatrix = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
              float[] orthoMatrix = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
              float[] viewMatrix = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
              int texture;
    
              @Override
              public void onSurfaceChanged(GL10 gl, int width, int height) {
                GLES30.glViewport(0, 0, width, height);
                Matrix.setLookAtM(viewMatrix, 0, sumX, sunY, -3
                        , sumX, sunY, 0, 0f, 1.0f, 0.0f);
                Matrix.orthoM(
                    orthoMatrix,
                    0,
                    width / 2.0f,
                    -width / 2.0f,
                    -height / 2.0f,
                    height / 2.0f,
                    0.01f,
                    100);
              }
    
              @Override
              public void onDrawFrame(GL10 gl) {
                Matrix.setLookAtM(viewMatrix, 0, sumX, sunY, -3
                        , sumX, sunY, 0, 0f, 1.0f, 0.0f);
                GLES30.glClear(GL10.GL_COLOR_BUFFER_BIT);
                GLES30.glUseProgram(programId);
                GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
                GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texture);
                int orthoMatrixLocation = GLES30.glGetUniformLocation(programId, "orthoMatrix");
                GLES30.glUniformMatrix4fv(orthoMatrixLocation, 1, false, orthoMatrix, 0);
                int viewMatrixLocation = GLES30.glGetUniformLocation(programId, "viewMatrix");
                GLES30.glUniformMatrix4fv(viewMatrixLocation, 1, false, viewMatrix, 0);
                int modelMatrixLocation = GLES30.glGetUniformLocation(programId, "modelMatrix");
                GLES30.glUniformMatrix4fv(modelMatrixLocation, 1, false, modelMatrix, 0);
                GLES30.glBindVertexArray(vao);
                GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, 6);
                GLES30.glBindVertexArray(0);
                GLES30.glUseProgram(0);
              }
            });
          setContentView(glSurfaceView);
      }
    }
    
    

    相关文章

      网友评论

          本文标题:OpenGL Android

          本文链接:https://www.haomeiwen.com/subject/ckuzlktx.html