Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cg.myapplication;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.app.Activity;
- import android.opengl.GLSurfaceView;
- import android.os.Bundle;
- import android.view.WindowManager;
- public class BouncySquareActivity extends Activity {
- @Override
- protected 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);
- }
- }
- package cg.myapplication;
- /**
- * Created by P17-7 on 3/27/2017.
- */
- import javax.microedition.khronos.opengles.GL10;
- import javax.microedition.khronos.opengles.GL11;
- import javax.microedition.khronos.egl.EGLConfig;
- import android.opengl.GLSurfaceView;
- import java.lang.Math;
- import android.content.Context;
- public 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.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
- gl.glMatrixMode(GL11.GL_MODELVIEW);
- gl.glLoadIdentity();
- gl.glTranslatef(0.0f, (float) Math.sin(mTransY), -3.0f);
- gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
- mSquare.draw(gl);
- gl.glLoadIdentity();
- gl.glTranslatef((float)(Math.sin(mTransY)/2.0f),0.0f, -2.9f);
- gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
- mSquare.draw(gl);
- mTransY += .075f;
- /* 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);
- int resid = R.drawable.hedly;
- mSquare.createTexture(gl, this.context, resid);
- /*int resid1 = R.drawable.splash;
- mSquare.createTexture(gl, this.context, resid1);*/
- }
- 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);
- }
- }
- package cg.myapplication;
- /**
- * Created by P17-7 on 3/27/2017.
- */
- import javax.microedition.khronos.opengles.GL10;
- import javax.microedition.khronos.opengles.GL11;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import android.graphics.*;
- import android.opengl.*;
- import android.content.Context;
- public class Square {
- private FloatBuffer mFVertexBuffer;
- private ByteBuffer mColorBuffer;
- private ByteBuffer mIndexBuffer;
- private int[] textures = new int[1];
- public FloatBuffer mTextureBuffer;
- float[] textureCoords =
- {
- 0.0f, 0.0f,
- 1.0f, 0.0f,
- 0.0f, 1.0f,
- 1.0f, 1.0f
- };
- /*float[] textureCoords =
- {
- 0.0f , 0.0f,
- 0.5f , 0.0f,
- 0.0f , 0.5f,
- 0.5f , 0.5f
- };*/
- /*float[] textureCoords =
- {
- 0.0f , 2.0f,
- 2.0f , 2.0f,
- 0.0f , 0.0f,
- 2.0f , 0.0f
- };*/
- private float texIncrease=0.2f;
- public Square()
- {
- float vertices[] =
- {
- -1.0f, -1.0f,
- 1.0f, -1.0f,
- -1.0f, 1.0f,
- 1.0f, 1.0f
- };
- /*float vertices[] =
- {
- -1.0f, -0.7f,
- 1.0f, -0.30f,
- -1.0f, 0.70f,
- 1.0f, 0.30f,
- };*/
- float squareColorsYMCA[] =
- {
- 1.0f, 1.0f, 0.0f, 1.0f,
- 0.0f, 1.0f, 1.0f, 1.0f,
- 0.0f, 0.0f, 0.0f, 1.0f,
- 1.0f, 0.0f, 1.0f, 1.0f
- };
- float squareColorsRGBA[] =
- {
- 1.0f, 0.0f, 0.0f, 1.0f,
- 0.0f, 1.0f, 0.0f, 1.0f,
- 0.0f, 0.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f, 1.0f
- };
- byte maxColor=(byte)255;
- byte colors[] =
- {
- 0, 0, 0, maxColor,
- maxColor, 0, 0, maxColor,
- 0, 0, 0, maxColor,
- maxColor, 0, 0, maxColor,
- };
- byte indices[] =
- {
- 0, 3, 1,
- 0, 2, 3
- };
- ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
- vbb.order(ByteOrder.nativeOrder());
- mFVertexBuffer = vbb.asFloatBuffer();
- mFVertexBuffer.put(vertices);
- mFVertexBuffer.position(0);
- ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
- tbb.order(ByteOrder.nativeOrder());
- mTextureBuffer = tbb.asFloatBuffer();
- mTextureBuffer.put(textureCoords);
- mTextureBuffer.position(0);
- mColorBuffer = ByteBuffer.allocateDirect(colors.length);
- mColorBuffer.put(colors);
- mColorBuffer.position(0);
- mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
- mIndexBuffer.put(indices);
- mIndexBuffer.position(0);
- }
- 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();
- }
- 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.glEnableClientState(GL10.GL_COLOR_ARRAY);
- gl.glEnable(GL10.GL_TEXTURE_2D);
- gl.glEnable(GL10.GL_BLEND);
- gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
- // 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);
- gl.glEnable(GL10.GL_TEXTURE_2D);
- // gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexture0);
- textureCoords[0]+=texIncrease;
- textureCoords[1]+=texIncrease;
- textureCoords[2]+=texIncrease;
- textureCoords[3]+=texIncrease;
- textureCoords[4]+=texIncrease;
- textureCoords[5]+=texIncrease;
- textureCoords[6]+=texIncrease;
- textureCoords[7]+=texIncrease;
- mTextureBuffer.put(textureCoords);
- mTextureBuffer.position(0);
- }
- /* public void draw(GL10 gl) {
- gl.glEnable(GL10.GL_TEXTURE_2D);
- gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexture0);
- gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
- gl.glFrontFace(GL11.GL_CW);
- gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glColorPointer(4, GL11.GL_FLOAT, 0, mColorBuffer);
- gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement