Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cg.bouncysquare;
- import android.app.Activity;
- import android.opengl.GLSurfaceView;
- import android.os.Bundle;
- import android.view.WindowManager;
- public class BouncySquareActivity extends Activity
- {
- 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);
- }
- }
- //--------------------------------
- package cg.bouncysquare;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import javax.microedition.khronos.opengles.GL10;
- import javax.microedition.khronos.opengles.GL11;
- import android.graphics.*;
- import android.opengl.*;
- import android.content.Context;
- class Square
- {
- private FloatBuffer mFVertexBuffer;
- private ByteBuffer mColorBuffer;
- private ByteBuffer mIndexBuffer;
- private int[] textures=new int[1];
- public FloatBuffer mTextureBuffer;
- float[] textureCoords={
- 0.0f, 2.0f,
- 2.0f, 2.0f,
- 0.0f, 0.0f,
- 2.0f, 0.0f
- };
- 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[] =
- {
- 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);
- 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.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- //gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);
- gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
- gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, mIndexBuffer);
- gl.glFrontFace(GL11.GL_CCW);
- gl.glVertexPointer(2, GL10.GL_FLOAT,0,mFVertexBuffer);
- 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);
- }
- 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();
- }
- }
- //----------------------------------
- package cg.bouncysquare;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.opengl.GLSurfaceView;
- import java.lang.Math;
- import android.content.Context;
- 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);
- mSquare.draw(gl);
- mTransY += .075f;
- }
- public void onSurfaceChanged(GL10 gl, int width, int height)
- {
- gl.glViewport(0, 0, width, height);
- float ratio = (float) width / height;
- gl.glMatrixMode(GL10.GL_PROJECTION);
- gl.glLoadIdentity();
- gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
- }
- public void onSurfaceCreated(GL10 gl, EGLConfig config)
- {
- int resid=R.drawable.hedly;
- mSquare.createTexture(gl, this.context, resid);
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement