Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cg.bouncycube;
- import android.app.Activity;
- import android.opengl.GLSurfaceView;
- import android.os.Bundle;
- import android.view.WindowManager;
- public class BouncyCubeActivity 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 CubeRenderer());
- setContentView(view);
- }
- }
- //-----
- package cg.bouncycube;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import javax.microedition.khronos.opengles.GL10;
- import javax.microedition.khronos.opengles.GL11;
- class Cube
- {
- private FloatBuffer mFVertexBuffer;
- private ByteBuffer mColorBuffer;
- private ByteBuffer mTFan1;
- private ByteBuffer mTFan2;
- private FloatBuffer mNormalBuffer;
- public Cube()
- {
- float[] normals =
- {
- -1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3),
- 1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3),
- 1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3),
- -1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3),
- -1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3),
- 1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3),
- 1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3),
- -1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3)
- };
- float vertices[] =
- {
- -1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f, -1.0f, 1.0f,
- -1.0f, -1.0f, 1.0f,
- -1.0f, 1.0f, -1.0f,
- 1.0f, 1.0f, -1.0f,
- 1.0f, -1.0f, -1.0f,
- -1.0f, -1.0f, -1.0f
- };
- byte maxColor=(byte)255;
- byte colors[] =
- {
- maxColor, 0, 0, maxColor,
- maxColor, 0, 0, maxColor,
- maxColor, 0, 0, maxColor,
- maxColor, 0, 0, maxColor,
- 0, 0, 0, maxColor,
- 0, 0, 0, maxColor,
- 0, 0, 0, maxColor,
- 0, 0, 0, maxColor,
- };
- byte tFan1[] =
- {
- 1,0,3,
- 1,3,2,
- 1,2,6,
- 1,6,5,
- 1,5,4,
- 1,4,0
- };
- byte tFan2[] =
- {
- 7,4,5,
- 7,5,6,
- 7,6,2,
- 7,2,3,
- 7,3,0,
- 7,0,4
- };
- 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);
- mTFan1 = ByteBuffer.allocateDirect(tFan1.length);
- mTFan1.put(tFan1);
- mTFan1.position(0);
- mTFan2 = ByteBuffer.allocateDirect(tFan2.length);
- mTFan2.put(tFan2);
- mTFan2.position(0);
- ByteBuffer nbb = ByteBuffer.allocateDirect(normals.length * 4);
- nbb.order(ByteOrder.nativeOrder());
- mNormalBuffer = nbb.asFloatBuffer();
- mNormalBuffer.put(normals);
- mNormalBuffer.position(0);
- }
- public void draw(GL10 gl)
- {
- gl.glVertexPointer(3, GL11.GL_FLOAT, 0, mFVertexBuffer);
- gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);
- gl.glDrawElements( gl.GL_TRIANGLE_FAN, 6 * 3, gl.GL_UNSIGNED_BYTE, mTFan1);
- gl.glDrawElements( gl.GL_TRIANGLE_FAN, 6 * 3, gl.GL_UNSIGNED_BYTE, mTFan2);
- gl.glNormalPointer(GL10.GL_FLOAT, 0, mNormalBuffer);
- gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
- }
- }
- //-----
- package cg.bouncycube;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.opengl.GLSurfaceView;
- import java.lang.Math;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- class CubeRenderer implements GLSurfaceView.Renderer
- {
- private Cube mCube;
- private float mTransY;
- private float mAngle;
- public final static int SS_SUNLIGHT = GL10.GL_LIGHT0;
- public CubeRenderer()
- {
- mCube = new Cube();
- }
- 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), -7.0f);
- mTransY += 0.075f;
- gl.glRotatef(mAngle, 0.0f, 1.0f, 0.0f);
- gl.glRotatef(mAngle, 1.0f, 0.0f, 0.0f);
- mAngle += 0.4;
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
- mCube.draw(gl);
- }
- protected static FloatBuffer makeFloatBuffer(float[] array)
- {
- ByteBuffer bb = ByteBuffer.allocateDirect(array.length*4);
- bb.order(ByteOrder.nativeOrder());
- FloatBuffer fb = bb.asFloatBuffer();
- fb.put(array);
- fb.position(0);
- return fb;
- }
- public void initLighting(GL10 gl){
- float[] green = {0.0f, 1.0f, 0.0f, 1.0f};
- //float[] position = {0.0f, 5.0f, 0.0f, 1.0f};
- float[] position = {10.0f,0.0f,3.0f,1.0f};
- float[] red = {1.0f, 0.0f, 0.0f, 1.0f};
- float[] blue = {0.0f,0.0f,1.0f,1.0f};
- float[] yellow = {1.0f,1.0f,0.0f,1.0f};
- gl.glLightfv(SS_SUNLIGHT, GL10.GL_POSITION, makeFloatBuffer(position));
- // gl.glLightfv(SS_SUNLIGHT, GL10.GL_DIFFUSE, makeFloatBuffer(green));
- //gl.glLightfv(SS_SUNLIGHT, GL10.GL_SPECULAR, makeFloatBuffer(red));
- gl.glLightfv(SS_SUNLIGHT, GL10.GL_AMBIENT, makeFloatBuffer(blue));
- gl.glShadeModel(GL10.GL_SMOOTH);
- gl.glEnable(GL10.GL_LIGHTING);
- gl.glEnable(SS_SUNLIGHT);
- gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, makeFloatBuffer(red));
- gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, makeFloatBuffer(red));
- // gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, makeFloatBuffer(blue));
- gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, 0);
- //gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_EMISSION, makeFloatBuffer(yellow));
- gl.glLightf(SS_SUNLIGHT, GL10.GL_LINEAR_ATTENUATION, 0.025f);
- float[] colorVector={0.2f, 0.2f, 0.2f, 1.0f};
- gl.glLightModelfv(GL10.GL_LIGHT_MODEL_AMBIENT, makeFloatBuffer(colorVector));
- float direction[]={1.0f,0.0f,0.0f};
- gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPOT_DIRECTION, makeFloatBuffer(direction));
- }
- public void onSurfaceChanged(GL10 gl, int width, int height)
- {
- gl.glViewport(0, 0, width, height);
- float aspectRatio = (float)width/(float)height;
- float zNear = 0.1f;
- float zFar = 1000;
- float fieldOfView = 30.0f/57.3f;
- float size = zNear * (float)(Math.tan((double)(fieldOfView/2.0f)));
- gl.glEnable(GL10.GL_NORMALIZE);
- gl.glMatrixMode(GL10.GL_PROJECTION);
- gl.glFrustumf(-size, size, -size/aspectRatio, size/aspectRatio, zNear, zFar);
- gl.glMatrixMode(GL10.GL_MODELVIEW);
- gl.glDepthMask(false);
- initLighting(gl);
- }
- public void onSurfaceCreated(GL10 gl, EGLConfig config)
- {
- gl.glDisable(GL10.GL_DITHER);
- gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
- GL10.GL_FASTEST);
- gl.glClearColor(255,255,255,255);
- 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