android opengl es舞い散る星
他の人の七色の星のフレームを使っていますが、
自分で自分の手でデバッグして、まだ分からないところがたくさんありますが、android opengl esのフレームワークについて一定の認識を持っています.
2つのクラス
StarsActivity.class
MyRenderer.class
自分で自分の手でデバッグして、まだ分からないところがたくさんありますが、android opengl esのフレームワークについて一定の認識を持っています.
2つのクラス
StarsActivity.class
package wjw.stars;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class StarsActivity extends Activity {
private GLSurfaceView glSurfaceView;
private MyRenderer renderer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Load.loadBitmap(getResources());
renderer = new MyRenderer();
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setRenderer(renderer);
setContentView(glSurfaceView);
}
}
class Load {
public static Bitmap bitmap;
public static void loadBitmap(Resources res) {
bitmap = BitmapFactory.decodeResource(res, R.drawable.star);
}
}
MyRenderer.class
package wjw.stars;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.Random;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
public class MyRenderer implements Renderer {
private int one = 0x10000;
private IntBuffer vertexBuffer;
private int[] vertex = new int[] { -one, -one, 0, one, -one, 0, -one, one,
0, one, one, 0 };
private IntBuffer coordBuffer;
private int[] coord = { 0, 0, one, 0, 0, one, one, one };
private int[] textures = new int[1];
private Random random = new Random();
//
boolean twinkle = true;
// star
int num = 50;
// star
NiceStar[] star = new NiceStar[num];
// star
float tilt = 90.0f;
// star dist
float zoom = -10.0f;
float spin; //
int PortWidth;
int PortHeight;
// init
public void initData() {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertex.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuffer.asIntBuffer();
vertexBuffer.put(vertex);
vertexBuffer.position(0);
ByteBuffer coordByteBuffer = ByteBuffer
.allocateDirect(coord.length * 4);
coordByteBuffer.order(ByteOrder.nativeOrder());
coordBuffer = coordByteBuffer.asIntBuffer();
coordBuffer.put(coord);
coordBuffer.position(0);
}
@Override
public void onDrawFrame(GL10 gl) {
//
gl.glClear(GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);
//
initData();
for (int i = 0; i < num; i++) {
gl.glLoadIdentity();
// zoom
gl.glTranslatef(0.0f, 0.0f, zoom);
// 、
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTranslatef(star[i].x, star[i].y, 0.0f);
if (twinkle) //
{
// byte
gl.glColor4f((float) star[(num - i) - 1].r / 255.0f,
(float) star[(num - i) - 1].g / 255.0f,
(float) star[(num - i) - 1].b / 255.0f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, coordBuffer);
//
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
//
gl.glFinish();
}
//
gl.glRotatef(star[i].angle, 0.0f, 0.0f, 1.0f); // z
// byte
gl.glColor4f((float) star[(num - i) - 1].r / 255.0f,
(float) star[(num - i) - 1].g / 255.0f,
(float) star[(num - i) - 1].b / 255.0f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, coordBuffer);
//
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
// 、
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
//
star[i].angle += (float) (i+1) / (float) num; //
//
star[i].y -= (float) 0.05;
//
if (star[i].y < -11.0)
{
star[i].y += 22.0f;
//
star[i].r = random.nextInt(256);
//
star[i].g = random.nextInt(256);
//
star[i].b = random.nextInt(256);
}
}
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
PortWidth = width;
// String ans = "";
// Log.v("PortWidth",ans.valueOf(PortWidth));
PortHeight = 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);
//
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
//
gl.glClearColorx(0, 0, 0, 0);
//
gl.glShadeModel(GL10.GL_SMOOTH);
//
gl.glEnable(GL10.GL_DEPTH_TEST);
//
gl.glDepthFunc(GL10.GL_LEQUAL);
//
gl.glClearDepthf(1.0f);
//
gl.glEnable(GL10.GL_TEXTURE_2D);
//
gl.glGenTextures(1, textures, 0);
//
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, Load.bitmap, 0);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE); //
gl.glEnable(GL10.GL_BLEND); //
// all star
for (int i = 0; i < num; i++) {
NiceStar starTmp = new NiceStar();
// all star from 0 angle
starTmp.angle = 0.0f;
//
starTmp.x = (float)(i+1)/(float)num * (float)20.0;
starTmp.x -= 10.0;
// String ans = "";
// Log.v("starTmp", ans.valueOf(starTmp.x));
starTmp.y = ((float) random.nextInt(22)) ;
// set red
starTmp.r = random.nextInt(256);
// set green
starTmp.g = random.nextInt(256);
// set blue
starTmp.b = random.nextInt(256);
star[i] = starTmp;
}
}
}
class NiceStar {
// star
int r, g, b;
// star
float x, y;
// current star angle
float angle = 0.0f;
}