package night.fireworks;
import java.nio.FloatBuffer;
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;
import android.util.Log;
import android.view.KeyEvent;
public class GLRender implements Renderer
{
final static int MAX_FIRE = 5;
boolean rainbow = true; /* Toggle rainbow effect */
Random random = new Random();
float slowdown = 0.5f; /* Slow Down Particles */
float xspeed = 1; /* Base X Speed (To Allow Keyboard Direction Of Tail) */
float yspeed = 3; /* Base Y Speed (To Allow Keyboard Direction Of Tail) */
float zoom = -30.0f; /* Used To Zoom Out */
//int loop; /* Misc Loop Variable */
int col = 0; /* Current Color Selection */
int delay; /* Rainbow Effect Delay */
int texture; /* Storage For Our Particle Texture */
int texPearl;
/* Rainbow of colors */
static float colors[][]=
{
{1.0f, 0.5f, 0.5f},
{1.0f, 0.75f, 0.5f},
{1.0f, 1.0f, 0.5f},
{0.75f, 1.0f, 0.5f},
{0.5f, 1.0f, 0.5f},
{0.5f, 1.0f, 0.75f},
{0.5f, 1.0f, 1.0f},
{0.5f, 0.75f, 1.0f},
{0.5f, 0.5f, 1.0f},
{0.75f, 0.5f, 1.0f},
{1.0f, 0.5f, 1.0f},
{1.0f, 0.5f, 0.75f}
};
/* Our beloved array of particles */
Fire fire[] = new Fire[MAX_FIRE];
@Override
public void onDrawFrame(GL10 gl)
{
FloatBuffer vertices = FloatBuffer.wrap(new float[12]);
FloatBuffer texcoords = FloatBuffer.wrap(new float[8]);
/* Clear The Screen And The Depth Buffer */
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
/* Enable vertices and texcoords arrays */
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
/* Set pointers to vertices and texcoords */
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texcoords);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texPearl);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
gl.glOrthof(0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 200.0f);
texcoords.clear(); vertices.clear();
/* Top Right */
texcoords.put(1.0f); texcoords.put(0.0f);
vertices.put(1.0f); vertices.put(1.0f); vertices.put(1.0f);
/* Top Left */
texcoords.put(0.0f); texcoords.put(0.0f);
vertices.put(0.0f); vertices.put(1.0f); vertices.put(1.0f);
/* Bottom Right */
texcoords.put(1.0f); texcoords.put(1.0f);
vertices.put(1.0f); vertices.put(0.0f); vertices.put(1.0f);
/* Bottom Left */
texcoords.put(0.0f); texcoords.put(1.0f);
vertices.put(0.0f); vertices.put(0.0f); vertices.put(1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glPopMatrix();
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
for(int loop = 0; loop < MAX_FIRE; loop++) {
for (int loop1 = 0; loop1 < Fire.MAX_PARTICLES; loop1++) {
for(int loop2 = 0; loop2 < Fire.MAX_TAIL; loop2++) {
float x = fire[loop].particle[loop1][loop2].x;
float y = fire[loop].particle[loop1][loop2].y;
float z = fire[loop].particle[loop1][loop2].z;
float dt = 1 - (float)loop2/Fire.MAX_TAIL;
gl.glColor4f(fire[loop].particle[loop1][0].r,
fire[loop].particle[loop1][0].g,
fire[loop].particle[loop1][0].b,
fire[loop].life * dt);
float size = 0.5f * dt;
texcoords.clear(); vertices.clear();
texcoords.put(1.0f); texcoords.put(1.0f);
vertices.put(x + size); vertices.put(y + size); vertices.put(z);
texcoords.put(0.0f); texcoords.put(1.0f);
vertices.put(x - size); vertices.put(y + size); vertices.put(z);
texcoords.put(1.0f); texcoords.put(0.0f);
vertices.put(x + size); vertices.put(y - size); vertices.put(z);
texcoords.put(0.0f); texcoords.put(0.0f);
vertices.put(x - size); vertices.put(y - size); vertices.put(z);
/* Build Quad From A Triangle Strip */
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
// update position
for(int loop2 = Fire.MAX_TAIL-1; loop2 > 0; loop2--) {
fire[loop].particle[loop1][loop2].x = fire[loop].particle[loop1][loop2-1].x;
fire[loop].particle[loop1][loop2].y = fire[loop].particle[loop1][loop2-1].y;
fire[loop].particle[loop1][loop2].z = fire[loop].particle[loop1][loop2-1].z;
} //for loop2 end
fire[loop].particle[loop1][0].x += fire[loop].particle[loop1][0].xs / (slowdown * 1000);
fire[loop].particle[loop1][0].y += fire[loop].particle[loop1][0].ys / (slowdown * 1000);
fire[loop].particle[loop1][0].z += fire[loop].particle[loop1][0].zs / (slowdown * 1000);
// update y speed
float yd = fire[loop].particle[loop1][0].yg / (slowdown * 500);
fire[loop].particle[loop1][0].ys += yd;
if (fire[loop].particle[loop1][0].up && fire[loop].particle[loop1][0].ys < -yd) {
fire[loop].particle[loop1][0].up = false;
int color = rand()%colors.length;
fire[loop].particle[loop1][0].r = colors[color][0];
fire[loop].particle[loop1][0].g = colors[color][1];
fire[loop].particle[loop1][0].b = colors[color][2];
// x-z speed
double radian = 3.14159*loop1*15.0/180.0;//loop1*15.0/(2*3.14159);
fire[loop].particle[loop1][0].xs = (float)(fire[loop].rad*Math.sin(radian));
fire[loop].particle[loop1][0].zs = (float)(fire[loop].rad*Math.cos(radian));
}
}
fire[loop].life -= fire[loop].fade;
if (fire[loop].life < 0) {
resetFire(loop);
}
}
/* Disable texcoords and vertices arrays */
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
/* Draw it to the screen */
gl.glFinish();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
/* Height / width ration */
float ratio;
/* Protect against a divide by zero */
if (height == 0) {
height = 1;
}
ratio = (float)width/(float)height;
/* Setup our viewport. */
gl.glViewport(0, 0, (int)width, (int)height);
/* change to the projection matrix and set our viewing volume. */
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
/* Set our perspective */
gl.glFrustumf(-ratio, ratio, -1, 1, 1.0f, 200.0f);
/* Make sure we're chaning the model view and not the projection */
gl.glMatrixMode(GL10.GL_MODELVIEW);
/* Reset The View */
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, 0.0f);
gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
}
public void LoadGLTextures(GL10 gl)
{
IntBuffer textureBuffer = IntBuffer.allocate(2);
gl.glGenTextures(2, textureBuffer);
texture = textureBuffer.get(0);
/* Typical Texture Generation Using Data From The Bitmap */
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
/* Linear Filtering */
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
/* Generate The Texture */
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap, 0);
texPearl = textureBuffer.get(1);
gl.g
- 1
- 2
- 3
- 4
- 5
前往页