Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cg.helloworld;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.opengl.GLSurfaceView;
- import android.opengl.GLUtils;
- import android.os.Bundle;
- import android.view.WindowManager;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import javax.microedition.khronos.opengles.GL11;
- import androidx.activity.EdgeToEdge;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.graphics.Insets;
- import androidx.core.view.ViewCompat;
- import androidx.core.view.WindowInsetsCompat;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- public class MainActivity extends AppCompatActivity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- GLSurfaceView view = new GLSurfaceView(this);
- view.setRenderer(new SquareRenderer(this.getApplicationContext()));
- setContentView(view);
- }
- }
- class Square {
- private FloatBuffer mFVertexBuffer;
- private ByteBuffer mColorBuffer;
- private ByteBuffer mIndexBuffer;
- public FloatBuffer mTextureBuffer;
- //public float[] textureCoords;
- //public float texIncrease;
- private int[] textures = new int[1];
- public Square(){
- float vertices[] =
- {
- -1.0f, -0.7f,
- 1.0f, -0.30f,
- -1.0f, 0.70f,
- 1.0f, 0.30f,
- };
- byte maxColor=(byte)255;
- byte colors[] =
- {
- maxColor,maxColor , maxColor, 0,
- maxColor,maxColor , maxColor, 0,
- maxColor,maxColor , maxColor, 0,
- maxColor,maxColor , maxColor, 0,
- };
- byte indices[] =
- {
- 0, 3, 1,
- 0, 2, 3
- };
- float[] textureCoords =
- {
- 0.0f , 2.0f,
- 2.0f , 2.0f,
- 0.0f , 0.0f,
- 2.0f , 0.0f
- };
- ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
- vbb.order(ByteOrder.nativeOrder());
- mFVertexBuffer = vbb.asFloatBuffer();
- mFVertexBuffer.put(vertices);
- mFVertexBuffer.position(0);
- mColorBuffer = ByteBuffer.allocateDirect(colors.length);
- mColorBuffer.put(colors);
- mColorBuffer.position(0);
- mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
- mIndexBuffer.put(indices);
- mIndexBuffer.position(0);ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
- tbb.order(ByteOrder.nativeOrder());
- mTextureBuffer = tbb.asFloatBuffer();
- mTextureBuffer.put(textureCoords);
- mTextureBuffer.position(0);
- }
- public void draw(GL10 gl){
- gl.glFrontFace(GL11.GL_CW);
- gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);
- //gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);
- gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, mIndexBuffer);
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, mColorBuffer);
- gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
- gl.glEnable(GL10.GL_TEXTURE_2D);
- gl.glEnable(GL10.GL_BLEND);
- gl.glBlendFunc(GL10.GL_ONE, GL10.GL_SRC_COLOR);
- gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
- gl.glTexCoordPointer(2, GL10.GL_FLOAT,0, mTextureBuffer);
- gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
- textureCoords[0]+=texIncrease;
- textureCoords[1]+=texIncrease;
- textureCoords[2]+=texIncrease;
- textureCoords[3]+=texIncrease;
- textureCoords[4]+=texIncrease;
- textureCoords[5]+=texIncrease;
- textureCoords[6]+=texIncrease;
- textureCoords[7]+=texIncrease;
- }
- public void createTexture(GL10 gl, Context contextRegf, int resource){
- Bitmap image = BitmapFactory.decodeResource(contextRegf.getResources(), resource);
- gl.glGenTextures(1, textures, 0);
- gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
- GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0);
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
- image.recycle();
- }
- }
- class SquareRenderer implements GLSurfaceView.Renderer {
- private Square mSquare;
- private float mTransY;
- private Context context;
- public SquareRenderer(Context context)
- {
- this.context = context;
- this.mSquare = new Square();
- }
- public void onDrawFrame(GL10 gl) {
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
- gl.glMatrixMode(GL10.GL_MODELVIEW);
- gl.glLoadIdentity();
- gl.glTranslatef(0.0f, (float) Math.sin(mTransY), -3.0f);
- mTransY += 0.075f;
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
- mSquare.draw(gl);
- }
- public void onSurfaceChanged(GL10 gl, int width, int height){
- gl.glViewport(0, 0, width, height);
- gl.glMatrixMode(GL10.GL_PROJECTION);
- gl.glLoadIdentity();
- float ratio = (float) width / height;
- gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
- }
- public void onSurfaceCreated(GL10 gl, EGLConfig config){
- gl.glDisable(GL10.GL_DITHER);
- gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
- gl.glClearColor(0,0,0,0);
- gl.glEnable(GL10.GL_CULL_FACE);
- gl.glShadeModel(GL10.GL_SMOOTH);
- gl.glEnable(GL10.GL_DEPTH_TEST);
- int resid = R.drawable.obama;
- mSquare.createTexture(gl, this.context, resid);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement