Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cg.bouncysquare;
- import android.opengl.GLSurfaceView;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import javax.microedition.khronos.opengles.GL11;
- import android.content.Context;
- /**
- * Created by P17-10 on 3/20/2017.
- */
- public class SquareRenderer implements GLSurfaceView.Renderer{
- private final Context context;
- private Square mSquare;
- private Square mSquare2;
- private float mTransY;
- public SquareRenderer(Context context) {
- this.context = context;
- this.mSquare = new Square(1);
- this.mSquare2 = new Square(2);
- }
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
- int resid = R.drawable.hedly;
- int resid2 = R.drawable.goldengate;
- mSquare.createTexture(gl, this.context, resid);
- mSquare2.createTexture(gl, this.context, resid2);
- 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);
- }
- @Override
- 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);
- }
- @Override
- public void onDrawFrame(GL10 gl) {
- gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
- gl.glEnable(GL10.GL_BLEND);
- gl.glMatrixMode(GL11.GL_MODELVIEW);
- gl.glLoadIdentity();
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- //gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
- gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -3.0f);
- gl.glColor4f(1.0f, 1.0f, 1.0f, 0.75f);
- // gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
- gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
- mSquare.draw(gl);
- mTransY += .075f;
- gl.glLoadIdentity();
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- //gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
- gl.glTranslatef((float)(Math.sin(mTransY)/2.0f),0.0f, -2.9f);
- gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
- mSquare2.draw(gl);
- }
- }
- ****
- package cg.bouncysquare;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import android.graphics.*;
- import android.content.Context;
- import android.opengl.*;
- import javax.microedition.khronos.opengles.GL10;
- import javax.microedition.khronos.opengles.GL11;
- /**
- * Created by P17-10 on 3/20/2017.
- */
- public class Square {
- private FloatBuffer mFVertexBuffer;
- private FloatBuffer mColorBuffer;
- private ByteBuffer mIndexBuffer;
- public FloatBuffer mTextureBuffer;
- private int[] textures = new int[1];
- public Square(int sw) {
- 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
- };
- float vertices[] =
- {
- -1.0f, -1.0f,
- 1.0f, -1.0f,
- -1.0f, 1.0f,
- 1.0f, 1.0f
- };
- byte indices[] =
- {
- 0, 3, 1,
- 0, 2, 3
- };
- float[] textureCoords =
- {
- 0.0f, 0.0f,
- 1.0f, 0.0f,
- 0.0f, 1.0f,
- 1.0f, 1.0f
- };
- ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
- tbb.order(ByteOrder.nativeOrder());
- mTextureBuffer = tbb.asFloatBuffer();
- mTextureBuffer.put(textureCoords);
- mTextureBuffer.position(0);
- ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
- ByteBuffer vbbYMCA = ByteBuffer.allocateDirect(squareColorsYMCA.length*4);
- vbb.order(ByteOrder.nativeOrder());
- vbbYMCA.order(ByteOrder.nativeOrder());
- mFVertexBuffer = vbb.asFloatBuffer();
- mFVertexBuffer.put(vertices);
- mFVertexBuffer.position(0);
- mColorBuffer = vbbYMCA.asFloatBuffer();
- if(sw==1)
- mColorBuffer.put(squareColorsRGBA);
- else
- mColorBuffer.put(squareColorsYMCA);
- 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_FLOAT, 0, mColorBuffer);
- gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, mIndexBuffer);
- 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);
- }
- }
- ***
- package cg.bouncysquare;
- import android.app.Activity;
- import android.opengl.GLSurfaceView;
- import android.support.v7.app.AppCompatActivity;
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement