Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- planet.java:
- package cg.solarsystem;
- 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 Planet
- {
- private FloatBuffer mFVertexBuffer;
- private ByteBuffer mColorBuffer;
- private ByteBuffer mTFan1;
- private ByteBuffer mTFan2;
- public Planet()
- {
- 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);
- }
- public void draw(GL10 gl)
- {
- float angleA, angleB;
- float cos, sin;
- float r1, r2;
- float h1, h2;
- float step = 15.0f;
- float[][] v = new float[32][3];
- ByteBuffer vbb;
- FloatBuffer vBuf;
- vbb = ByteBuffer.allocateDirect(v.length * v[0].length * 4);
- vbb.order(ByteOrder.nativeOrder());
- vBuf = vbb.asFloatBuffer();
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
- for (angleA = -90.0f; angleA <90.0f; angleA += step) {
- int n = 0;
- r1 = (float)Math.cos(angleA * Math.PI / 180.0);
- r2 = (float)Math.cos((angleA + step) * Math.PI / 180.0);
- h1 = (float)Math.sin(angleA * Math.PI / 180.0);
- h2 = (float)Math.sin((angleA + step) * Math.PI / 180.0);
- // Fixed latitude, 360 degrees rotation to traverse a weft
- for (angleB = 0.0f; angleB <= 360.0f; angleB += step) {
- cos = (float)Math.cos(angleB * Math.PI / 180.0);
- sin = -(float)Math.sin(angleB * Math.PI / 180.0);
- v[n][0] = (r2 * cos);
- v[n][1] = (h2);
- v[n][2] = (r2 * sin);
- v[n + 1][0] = (r1 * cos);
- v[n + 1][1] = (h1);
- v[n + 1][2] = (r1 * sin);
- vBuf.put(v[n]);
- vBuf.put(v[n + 1]);
- n += 2;
- if(n>31){
- vBuf.position(0);
- gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vBuf);
- gl.glNormalPointer(GL10.GL_FLOAT, 0, vBuf);
- gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, n);
- n = 0;
- angleB -= step;
- }
- }
- vBuf.position(0);
- gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vBuf);
- gl.glNormalPointer(GL10.GL_FLOAT, 0, vBuf);
- gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, n);
- }
- gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
- }
- }
- planetRenderer.java:
- package cg.solarsystem;
- 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;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.opengl.GLU;
- import android.opengl.GLSurfaceView.Renderer;
- class PlanetRenderer implements GLSurfaceView.Renderer
- {
- // Ambient light
- private final float[] mat_ambient = { 0.2f, 0.3f, 0.4f, 1.0f };
- private FloatBuffer mat_ambient_buf;
- // Parallel incident light
- private final float[] mat_diffuse = { 0.4f, 0.6f, 0.8f, 1.0f };
- private FloatBuffer mat_diffuse_buf;
- // The highlighted area
- private final float[] mat_specular = { 0.2f * 0.4f, 0.2f * 0.6f, 0.2f * 0.8f, 1.0f };
- private FloatBuffer mat_specular_buf;
- private Planet mSphere = new Planet();
- public volatile float mLightX = 10f;
- public volatile float mLightY = 10f;
- public volatile float mLightZ = 10f;
- private float mAngle;
- private float mTransY;
- @Override
- public void onDrawFrame(GL10 gl) {
- // To clear the screen and the depth buffer
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
- //gl.glMatrixMode(GL10.GL_MODELVIEW);
- // Reset the modelview matrix
- 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 += 1;
- gl.glEnable(GL10.GL_LIGHTING);
- gl.glEnable(GL10.GL_LIGHT0);
- // Texture of material
- gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, mat_ambient_buf);
- gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, mat_diffuse_buf);
- gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, mat_specular_buf);
- // Specular exponent 0~128 less rough
- gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, 96.0f);
- //The position of the light source
- float[] light_position = {mLightX, mLightY, mLightZ, 0.0f};
- ByteBuffer mpbb = ByteBuffer.allocateDirect(light_position.length * 4);
- mpbb.order(ByteOrder.nativeOrder());
- FloatBuffer mat_posiBuf = mpbb.asFloatBuffer();
- mat_posiBuf.put(light_position);
- mat_posiBuf.position(0);
- // final float lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
- // final float lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
- // gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, FloatBuffer.wrap(lightColor1));
- // gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, FloatBuffer.wrap(lightPos1));
- gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, mat_posiBuf);
- //gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- //gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
- gl.glTranslatef(0.0f, 0.0f, -3.0f);
- mSphere.draw(gl);
- }
- @Override
- public void onSurfaceChanged(GL10 gl, int width, int height) {
- // Set the output screen size
- gl.glViewport(0, 0, width, height);
- // Projection matrix
- gl.glMatrixMode(GL10.GL_PROJECTION);
- // Reset the projection matrix
- gl.glLoadIdentity();
- // Set the viewport size
- // gl.glFrustumf(0, width, 0, height, 0.1f, 100.0f);
- GLU.gluPerspective(gl, 90.0f, (float) width / height, 0.1f, 50.0f);
- // Select the model view matrix
- gl.glMatrixMode(GL10.GL_MODELVIEW);
- // Reset the modelview matrix
- gl.glLoadIdentity();
- gl.glClearColor(0,0,0,0);
- }
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
- // On the perspective correction
- gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
- // Background: Black
- gl.glClearColor(0, 0.0f, 2.0f, 0.0f);
- // Start the smooth shading
- gl.glShadeModel(GL10.GL_SMOOTH);
- // Reset the depth buffer
- gl.glClearDepthf(1.0f);
- // Start the depth test
- gl.glEnable(GL10.GL_DEPTH_TEST);
- // Type the depth test
- gl.glDepthFunc(GL10.GL_LEQUAL);
- initBuffers();
- }
- private void initBuffers() {
- ByteBuffer bufTemp = ByteBuffer.allocateDirect(mat_ambient.length * 4);
- bufTemp.order(ByteOrder.nativeOrder());
- mat_ambient_buf = bufTemp.asFloatBuffer();
- mat_ambient_buf.put(mat_ambient);
- mat_ambient_buf.position(0);
- bufTemp = ByteBuffer.allocateDirect(mat_diffuse.length * 4);
- bufTemp.order(ByteOrder.nativeOrder());
- mat_diffuse_buf = bufTemp.asFloatBuffer();
- mat_diffuse_buf.put(mat_diffuse);
- mat_diffuse_buf.position(0);
- bufTemp = ByteBuffer.allocateDirect(mat_specular.length * 4);
- bufTemp.order(ByteOrder.nativeOrder());
- mat_specular_buf = bufTemp.asFloatBuffer();
- mat_specular_buf.put(mat_specular);
- mat_specular_buf.position(0);
- }
- }
- planetActivity.java:
- package cg.solarsystem;
- import android.app.Activity;
- import android.opengl.GLSurfaceView;
- import android.os.Bundle;
- import android.view.WindowManager;
- public class PlanetActivity 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 PlanetRenderer());
- setContentView(view);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement