Render
public class GameRender implements Renderer{
private Background loader; // obrazek ladowania
private Watek thread; // watek towrzacy modele i tekstury
public GameRender(Manager manager) {
loader = new Background();
}
public void onDrawFrame(GL10 gl) {
try {
Thread.sleep(30);
} catch (Exception e) {
}
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
if(thread.getLoad()) // jezeli tekstury sie laduja
loader.draw(gl);
else
thread.draw(gl); // wyswietl obiekt gry
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0f, 100f, 100f, 0f, -1f, 1f);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
loader.loadGLTexture(gl, R.drawable.loader, GameView.CONTEXT);
thread = new Watek(gl);
thread.start();
}
Wątek wczytujacy model i tekstury
public class Watek extends Thread{
private GL10 gl;
private ArrayList<Star> stars; // lista modeli
private boolean load; // czy laduje tekstury
public Watek(GL10 gl) {
this.gl = gl;
load = false;
stars = new ArrayList<Star>();
}
@Override
public void run() { // towrzy modele i tekstury gwiazd
load = true;
for(int i=0; i<5; i++){
stars.add(new Star((int) (Math.random()*50), (int) (Math.random()*50), 0));
stars.get(i).loadGLTexture(gl, R.drawable.tloo, GameView.CONTEXT);
}
interrupt();
load = false;
}
public boolean getLoad(){
return load;
}
public void draw(GL10 gl){
for(Star s: stars)
s.draw(gl);
}
}
Abstrakcyjny Model 2D
public abstract class Model2D {
private FloatBuffer vertexBuffer;
private FloatBuffer textureBuffer;
private ByteBuffer indexBuffer;
private int[] textures;
private float vertices[];
private float texture[];
private byte indices[];
public Model2D() {
vertices = new float[12];
texture = new float[8];
indices = new byte[6];
}
protected void drawGL(GL10 gl) {
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
public void loadGLTexture(GL10 gl, int texture, Context context){
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(context.getResources().openRawResource(texture));
} catch (Exception e) {
e.printStackTrace();
}
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glClearDepthf(1.0f);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 4);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
protected void setTextures(int textures[]){
this.textures = textures;
}
protected void setVertices(float vertices[]){
this.vertices = vertices;
ByteBuffer byteBuf = ByteBuffer.allocateDirect(this.vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(this.vertices);
vertexBuffer.position(0);
}
protected void setTexture(float texture[]){
this.texture = texture;
ByteBuffer byteBuf = ByteBuffer.allocateDirect(this.texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(this.texture);
textureBuffer.position(0);
}
protected void setIndices(byte indices[]){
this.indices = indices;
indexBuffer = ByteBuffer.allocateDirect(this.indices.length);
indexBuffer.put(this.indices);
indexBuffer.position(0);
}
public abstract void draw(GL10 gl);
public abstract float getVerticesWidth();
public abstract float getVerticesHeight();
Obiekt gwiazda
public class Star extends Model2D {
private float x, y, z;
private int[] textures = new int[1];
private float vertices[] = {
0.0f, 0.0f, 0.0f,
50.0f, 0.0f, 0.0f,
50.0f, 50.0f, 0.0f,
0.0f, 50.0f, 0.0f
};
private float texture[] = {
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
};
private byte indices[] = {
0, 1, 2,
0, 2, 3
};
public Star(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
setTextures(textures);
setVertices(vertices);
setTexture(texture);
setIndices(indices);
}
@Override
public void draw(GL10 gl) {
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glTranslatef(x, y, z);
drawGL(gl);
gl.glPopMatrix();
gl.glLoadIdentity();
}